domus 1 Posted December 31, 2024 (edited) Hi, I'm using TBitmap.LoadFromStream in a background thread (TTask.Run), but it appears to be blocking the main thread (and UI), since I see pauses in execution when this function is executing. It's loading a PNG into a TBitmap. There is absolutely no interaction needed with the UI. The bitmap is not linked to any component. If this function needs to synchronize with the main thread for a part of its algorithm, is there an alternative one available that doesn't? Thx! Edited December 31, 2024 by domus Share this post Link to post
Olli73 5 Posted December 31, 2024 8 hours ago, domus said: is there an alternative one available that doesn't? Try Bitmap32 from https://github.com/graphics32 TBitmap is not Threadsafe in most cases. Share this post Link to post
DelphiUdIT 188 Posted December 31, 2024 (edited) TBitMap is Thread Safe since Tokyo release: https://docwiki.embarcadero.com/RADStudio/Tokyo/en/Multi-Threading_for_TBitmap,_TCanvas,_and_TContext3D Of course this doesn't means that it doesnt'use main thread in sync way. Edited December 31, 2024 by DelphiUdIT Share this post Link to post
domus 1 Posted December 31, 2024 (edited) 1 hour ago, Olli73 said: Try Bitmap32 from https://github.com/graphics32 TBitmap is not Threadsafe in most cases. Does Graphics32 support FMX? Just stumbled upon Image32. Anyone know how it handles TBitmap streaming...? Edited December 31, 2024 by domus Share this post Link to post
domus 1 Posted December 31, 2024 Just now, DelphiUdIT said: Of course this doesn't means that it doesnt'use main thread in sync way. Indeed, that's what I'm trying to find out. If it does, it kind of defeats the purpose of having it run in the background. Share this post Link to post
vfbb 285 Posted December 31, 2024 @domus It seems to be specific to the D2D implementation (as the TBitmap.LoadFromStream indirectly uses map/unmap): On this case, you can switch to another render, like Skia. (Just right click on your app > Enable Skia). Although TBitmap.LoadFromStream does not use TCanvas, it is worth remembering that another UI block occurs when using the TCanvas of the bitmap in the background thread (as reported in the docwiki posted earlier). In this case, using Skia, you can add `GlobalSkiaBitmapsInParallel := True;` on your dpr to avoid this. Share this post Link to post
domus 1 Posted December 31, 2024 Just now, vfbb said: On this case, you can switch to another render, like Skia. (Just right click on your app > Enable Skia). After quite a bit of back and forth with the o1 GPT model, it suggested using TBitmapSurface directly, like TBitmapCodecManager.LoadFromStream(strm, Surf) (where strm is the PNG stream), which it claims uses only the CPU, steering clear from any blocking caused by GPU access, but having tried that, I see no difference. Still on Delphi 11, but I could try Skia anyway. It's just that the last time I tried it, a while back, it seemed slower than the native FMX library (using D2D). Thanks! Share this post Link to post
domus 1 Posted 1 hour ago My goodness. The thing that was blocking everything was a TThreadedQueue. Days of debugging, just to find out this thing is absolutely unreliable inside a TTask (in the sense that it absolutely blocks the main thread when popping an item). Share this post Link to post
Remy Lebeau 1442 Posted 51 minutes ago 1 hour ago, domus said: My goodness. The thing that was blocking everything was a TThreadedQueue. Days of debugging, just to find out this thing is absolutely unreliable inside a TTask (in the sense that it absolutely blocks the main thread when popping an item). I very much doubt that. You are probably just using it incorrectly. Can you provide some code? 1 Share this post Link to post