Composite background-mode plots with hardware acceleration; harden Redrawer - #138
Merged
Conversation
…drawer (#96) Plot forced a software layer on every plot type because the renderers use canvas operations older hardware canvases didn't support. In background mode those operations happen on an offscreen bitmap and onDraw only copies that bitmap to the view, which hardware acceleration handles natively, so the software layer just added a full-size CPU copy of the view on the UI thread every frame. Only force it in main-thread mode. Redrawer holds plots weakly but dereferenced them without a null check, crashing the process when a plot was collected before finish() was called. Skip and drop collected plots, exit the thread once none remain, and make the control flags volatile. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #138 +/- ##
============================================
+ Coverage 71.02% 71.79% +0.77%
- Complexity 1244 1261 +17
============================================
Files 109 109
Lines 5194 5202 +8
Branches 544 547 +3
============================================
+ Hits 3689 3735 +46
+ Misses 1150 1111 -39
- Partials 355 356 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #96
The size/performance correlation
The reporter found that a background-mode plot's cost scaled with its on-screen size, to the point that shrinking the view doubled their app's frame rate. That's inherent to how background mode composites, but one third of it was avoidable.
Every frame does three full-size pixel passes: the render thread clears and draws into an ARGB bitmap the size of the view; the UI thread copies that bitmap into a software layer; and the framework uploads the layer as a texture. All scale with width times height, and two of the three run on the UI thread.
The middle pass exists because
Plot.isHwAccelerationSupported()returns false for every plot type, soonSizeChangedforcesLAYER_TYPE_SOFTWARE. That's needed in main-thread mode, where the renderers draw paths and clips straight onto the view canvas and older hardware canvases didn't support some of those operations. In background mode those operations already happen on the offscreen bitmap, and the view'sonDrawdoes nothing butdrawBitmap, which hardware acceleration handles natively. This PR only forces the software layer in main-thread mode, removing a full-size CPU copy from the UI thread on every background-mode frame. Rendered output is identical.Cost still scales with plot area; the release note says so and advises keeping high-rate plots small.
Redrawer crash
The crash in the issue comments is a library bug.
Redrawerholds plots by weak reference so it doesn't keep them alive, but its loop calledplotRef.get().redraw()unconditionally. When an activity is destroyed and the plot is collected beforefinish()is called, the thread dies with a NullPointerException, which on Android takes the process down. Now collected plots are skipped and dropped, the thread exits on its own once nothing remains, and thekeepRunning/keepAliveflags are volatile since the thread reads them without synchronization.Verification
PlotTest: withisHardwareAccelerated()stubbed true, a background-mode plot never callssetLayerType(SOFTWARE)and a main-thread plot still does. The background case fails on the old code.RedrawerTest(new): unit cases for the collected-plot handling; a thread case verifying redraws at the configured rate; and a case that constructs a real plot, drops the reference, forces GC, and asserts the thread exits without an uncaught exception. On the old loop that last case fails with the reporter's exactNullPointerException, captured via an uncaught-exception handler.Needs a device check
I can't measure the frame-rate change from here. The ECG demo in background mode, sized to fill the screen, is the closest thing to the reporter's setup; GPU profiling before and after should show the UI-thread draw time drop.