Recent

Author Topic: Drawing on Memory Bitmap crashes in a Thread on Linux.  (Read 4694 times)

kris

  • Jr. Member
  • **
  • Posts: 59
Drawing on Memory Bitmap crashes in a Thread on Linux.
« on: September 27, 2016, 08:54:12 am »
I found this topic: http://forum.lazarus.freepascal.org/index.php?topic=16654.0
and although generally an advice has been given to synchronize drawing onto Canvas, I really think this is only walking away from the problem. I am writing a simple game, and drawing a frame out of background and sprites. It is generally a lot of computation, just compoziting the final frame. I would like all of that to happen in the thread, I would only ever draw on a memory TBitmap, that would never go to the screen. Then, once I have the frame ready, I would Synchronize a method that prints it as one single Cavas.Draw(x,y,MyFrame);
Why I cannot do this on Linux? While I aparently can do this on Windows. I know I can synchronize all the Canvas.something calls, but that defeats the purpose, it would cause a delay in my main thread every time a heavy bitmap compoziting operation is happening, which happens a lot for every frame.
How to go about this? Does anyone know?

kris

  • Jr. Member
  • **
  • Posts: 59
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #1 on: September 27, 2016, 09:08:40 am »
Does it mean any use of Canvas on Linux will crash in Thread because of invalid Handle or something?
If so, is there any other way of compoziting a bitmap in memory without Canvas??

kris

  • Jr. Member
  • **
  • Posts: 59
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #2 on: September 27, 2016, 09:11:18 am »

Laksen

  • Hero Member
  • *****
  • Posts: 754
    • J-Software
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #3 on: September 27, 2016, 09:15:20 am »
How are you allocating the bitmap you are drawing on in a thread?

kris

  • Jr. Member
  • **
  • Posts: 59
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #4 on: September 27, 2016, 06:35:03 pm »
in Thread\s Constructor, before Execute method I have:

 FBitmap := TBitmap.Create;
 FBitmap.Assign(Form1.Image1.Picture.Bitmap);

where FBitmap is a protected variable of the TThread class.

Then in the Execute I want to do:

  FFrame := TBitmap.Create;
  FFrame.SetSize(100,100);
  FFrame.Canvae.Draw(0,0,FBitmap);

and this already will crash.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #5 on: September 27, 2016, 07:57:42 pm »
Try this, I think there is more wrong but at least this part is documented in TCanvas:
- You should lock /unlock the canvas while drawing in a thread.
- You should create an array of two bitmaps in the main thread, say Bitmaps := array[false,true] of TBitmap;
- In the thread, simply lock, draw, unlock then synchonize the swap of the bitmaps from memory to screen and the other one from screen to memory , not create the bitmap  there.
That way you have one bitmap for display and one bitmap for threadsafe drawing. It is also very,very speedy. Because interlockedExchange just swaps the pointers. (Hint...)
« Last Edit: September 27, 2016, 08:01:45 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

kris

  • Jr. Member
  • **
  • Posts: 59
Re: Drawing on Memory Bitmap crashes in a Thread on Linux.
« Reply #6 on: October 02, 2016, 11:03:48 am »
Thaddy, thank you for this hint, I never tried doing things this way, but I will try!

 

TinyPortal © 2005-2018