Jump to content
domus

Is TBitmap.LoadFromStream blocking when used in a BG thread?

Recommended Posts

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 by domus

Share this post


Link to post
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 by domus

Share this post


Link to post
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

@domus It seems to be specific to the D2D implementation (as the TBitmap.LoadFromStream indirectly uses map/unmap):

image.thumb.png.49859575856a4b1d6e4ffffba39b6a8f.png

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
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

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
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? 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×