burhanrashid52 / PhotoEditor

A Photo Editor library with simple, easy support for image editing using paints,text,filters,emoji and Sticker like stories.
MIT License
4.13k stars 991 forks source link

Solution to Bitmap not available when needed for Draw on buildBitmap #377

Closed anderbytes closed 3 years ago

anderbytes commented 3 years ago

Issue #374 forced me to try to understand what was happening that was breaking the save of Bitmaps.

It happens that in PhotoSaverTask.java in captureView(view) method a blank Bitmap is generated, and used in a new Canvas to be filled by the PhotoeditorView.

It seems that if this process happens too quickly, the Bitmap can't be generated quickly enough to be used, so it crashes everything.

I finally understood the main issue when I was debugging and when in RUN mode it would crash, but not in DEBUG mode.... JUST BECAUSE the debugger "pauses" when asked, giving it time in background to end the bitmap creation.

So I added an optional delay (default is ZERO, ok?) that enters just after the bitmap object creation, and before it's subsequential use.

Also, I removed the bitmap object blank creation from inside captureView, as we want it to be created as soon as possible and as far as possible from it's final use.

so... in SaveSettings, we now can set how many milliseconds between creating the createBitmap and the draw.

anderbytes commented 3 years ago

PS: if you could somehow think of a better way to guarantee that the Bitmap is fully available and created at the moment of the DRAW, I'm open to suggestions

burhanrashid52 commented 3 years ago

Thanks for debugging the issue.

I don't think Thread.sleep is good solution for this problem. If the bitmap creation is the issue then based on device spec the delay might differ.

I don't have the good solution right now. But it seems like if we could find a way to wait for bitmap creation than that would work just fine.

anderbytes commented 3 years ago

Ok but in that mean time, a delay is all we have.

Also the developer can make the delay smaller or higher depending on the performance of the device. I have a very old device used for testing where 1000ms was enough.

So why not this temporary solution instead of "no solution"?

anderbytes commented 3 years ago

Another solution may envolve some kind of loop that only exits when the bitmap is created after the createBitmap command... but how is it checked? how can I know that createBitmap has finished 100% ?

anderbytes commented 3 years ago

@burhanrashid52 I think I have found a way... as If there's no flag that would confirm me that the Bitmap is created, I had to improvisate one.

anderbytes commented 3 years ago

ps: this turned out to be MUCH MUCH more graceful than I thought it would be... when the Bitmap is not ready, I shouted a Log.i just for information. I expected several rounds of while and several Logs until the "Bitmap ready", but no... I always get only one (sometimes two) logs... so it's more lightweight than I thought.

anderbytes commented 3 years ago

ui-test taking too long. Would it be possible that the test got somehow stuck with the new "while" ? Because here doing manual tests, it got in and got out of "While" normally.

That or the ui-test considers that getting into a "catch" as something wrong.

anderbytes commented 3 years ago

@burhanrashid52 hello, we need to talk about this test error. Why didn't it pass? Is it required or optional?

burhanrashid52 commented 3 years ago

We need to find the root cause of the issue. This approach is hack and I am not sure it's a right way do. I've re run the test. Also please expect delay in my responses for few weeks.

anderbytes commented 3 years ago

I'm not good enough at Java or async routines to perfectly debug this Bitmap issue. I can't do this alone.

I'm afraid more and more users will be experiencing this

anderbytes commented 3 years ago

Hmmmm just came to me that this "automated test tool" may not be getting the same ErrorException code as we get manually. After this new loop ends, I'll try to be more generic, getting an exception catch for any exception, unfortunately this may be the cause of the endless loop at the automated test

anderbytes commented 3 years ago

Now I removed the while and the async part. The bitmap creation is synchronous and its drawing, too.

In my tests now it's working OK. Also that part is so fast I couldn't think a real need for the "async"

What was the point?

EDIT: In last case... there could be a SYNC path/method (saveAsBitmapSync and saveAsFileSync) and the old ASYNC with it's own 2 methods as usual

anderbytes commented 3 years ago

ui-test now working even after removing new while and all async code. Something's wrong, I'm removing this multi-commit PR and drafting a new clean one.