Recent

Author Topic: Drawing text on Tbitmap canvas  (Read 5264 times)

BigSid56

  • New Member
  • *
  • Posts: 12
Drawing text on Tbitmap canvas
« on: June 27, 2012, 07:25:38 pm »
I need to draw text on to a bitmap in one thread and then copy the bitmap to a subcontrol in the main thread.
When I try to use
ScreenBitmap.canvas.TextOut(X,Y,atext);
it crashes.
Is TBitmap.canvas.TextOut not supported?

Many thanks.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Drawing text on Tbitmap canvas
« Reply #1 on: June 28, 2012, 07:34:20 am »
It is supported, of course, but only from the main thread. TextOut uses the native Android text drawing APIs, which are accessed via JNI. Any kind of JNI call from a non-registered thread will necessarely crash. The JNI pointers are invalid in other thread. You could call AttachCurrentThread, but the LCL code is not prepared to use another JNI pointer which it would receive.

So you have various options:

1> Call text drawing routines in a Synchronize call, which will make it run in the main thread. The rest of the drawing routines are non-native, so they should work fine in the secondary thread.
or
2> Draw the text using LazFreeType which is non-native, see here: http://wiki.lazarus.freepascal.org/LazFreeType
or
3> Send a patch or open a bug report for adding support via a define in CustomDrawn-Android for using LazFreeType instead of the native text drawing APIs. LazFreeType has more changes of working in secondary threads although I never tested it. It shouldn't be a hard task, but is not a huge priority for me.

BigSid56

  • New Member
  • *
  • Posts: 12
Re: Drawing text on Tbitmap canvas
« Reply #2 on: June 28, 2012, 02:12:55 pm »
Thanks Filipe.
I've used
synchronize(SyncScreenWrite);
but the procedure SyncScreenWrite doesn't get called.
I added cthreads to the uses, is there anything else synchonize needs?


felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Drawing text on Tbitmap canvas
« Reply #3 on: June 28, 2012, 02:34:30 pm »
Ummmm, usually syncronize is called in the main loop. But in Android we have no access to the main loop. So I guess that the main thread would need to have a TTimer running which calls CheckSynchronize(). To me it looks like it would be better in your case to use LazFreeType

BigSid56

  • New Member
  • *
  • Posts: 12
Re: Drawing text on Tbitmap canvas
« Reply #4 on: June 28, 2012, 06:52:26 pm »
I've tried LazFreeType. It works well but is a bit slow to draw.
Thanks for all your help!

 

TinyPortal © 2005-2018