-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Backport the gooey morph to API 29-32 (OpenGL β hardware Bitmap) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blazejkustra
merged 2 commits into
blazejkustra:main
from
desugar-64:syaremych/api29-32-gl-morph-backport
Jun 28, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
150 changes: 150 additions & 0 deletions
150
android/src/main/java/com/morphview/internal/HardwareBufferMorphRenderer.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package com.morphview.internal | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.graphics.Paint | ||
| import android.graphics.RectF | ||
| import android.os.Build | ||
| import android.os.Handler | ||
| import android.os.HandlerThread | ||
| import androidx.annotation.RequiresApi | ||
| import com.morphview.internal.gl.SwapChain | ||
| import java.util.ArrayDeque | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * API 29β32 backend: renders the morph in OpenGL on a dedicated [HandlerThread] and presents the | ||
| * result as a hardware [Bitmap] via [Bitmap.wrapHardwareBuffer] β the "present via hardware Bitmap, | ||
| * no SurfaceView" path. | ||
| * | ||
| * Async is hidden behind the synchronous [draw] contract: each draw submits the latest state to the | ||
| * GL thread (coalesced to newest-only) and blits the latest ready frame. The result is stale by at | ||
| * most one frame β invisible mid-morph, where everything is already blurred β and the very first | ||
| * frames fall back to a plain crossfade until GL catches up. | ||
| */ | ||
| @RequiresApi(Build.VERSION_CODES.Q) | ||
| internal class HardwareBufferMorphRenderer( | ||
| private val onFrameReady: () -> Unit, | ||
| ) : MorphRenderer { | ||
|
|
||
| private val fallback = FitCenterPainter() | ||
| private val blitPaint = Paint(Paint.FILTER_BITMAP_FLAG) | ||
| private val blitDst = RectF() | ||
|
|
||
| private var glThread: HandlerThread? = null | ||
| private var glHandler: Handler? = null | ||
|
|
||
| // GL-thread-confined. | ||
| private var pipeline: MorphGlPipeline? = null | ||
| private val leases = ArrayDeque<SwapChain.Lease>() | ||
|
|
||
| // Latest requested frame: UI thread writes, GL thread reads. | ||
| @Volatile | ||
| private var pending: RenderRequest? = null | ||
| private var lastSubmitted: RenderRequest? = null // UI-thread only | ||
| private val renderScheduled = AtomicBoolean(false) | ||
|
|
||
| // Newest GL output: GL thread writes, UI thread blits. | ||
| @Volatile | ||
| private var displayBitmap: Bitmap? = null | ||
|
|
||
| override fun draw( | ||
| canvas: Canvas, | ||
| width: Int, | ||
| height: Int, | ||
| from: Bitmap?, | ||
| to: Bitmap?, | ||
| progress: Float, | ||
| blurRadiusPx: Float, | ||
| tintColor: Int?, | ||
| ) { | ||
| if (width <= 0 || height <= 0) return | ||
| ensureThread() | ||
|
|
||
| // Submit only when the frame changed β otherwise each produced frame's invalidate would | ||
| // re-trigger an identical render, spinning the GL thread at rest. | ||
| val req = RenderRequest(width, height, from, to, progress, blurRadiusPx, tintColor) | ||
| if (req != lastSubmitted) { | ||
| lastSubmitted = req | ||
| pending = req | ||
| scheduleRender() | ||
| } | ||
|
|
||
| val bmp = displayBitmap | ||
| if (bmp != null && !bmp.isRecycled) { | ||
| blitDst.set(0f, 0f, width.toFloat(), height.toFloat()) | ||
| canvas.drawBitmap(bmp, null, blitDst, blitPaint) | ||
| } else { | ||
| // No GL frame yet β crossfade so the view isn't blank on the first frames. | ||
| fallback.draw(canvas, from, width, height, 1f - progress, tintColor) | ||
| fallback.draw(canvas, to, width, height, progress, tintColor) | ||
| } | ||
| } | ||
|
|
||
| override fun release() { | ||
| val thread = glThread ?: return // never started, or already released β nothing to reset | ||
| displayBitmap = null | ||
|
blazejkustra marked this conversation as resolved.
|
||
| glHandler?.post { | ||
| pipeline?.release() | ||
| pipeline = null | ||
| while (leases.isNotEmpty()) leases.removeFirst().close() | ||
| } | ||
| glThread = null | ||
| glHandler = null | ||
| // Drain the GL thread before returning so a re-attach can't race this teardown for pipeline/leases. | ||
| thread.quitSafely() | ||
| thread.join() | ||
| // Reset submit state, or a re-attach at the same size/progress never re-renders. | ||
| lastSubmitted = null | ||
| pending = null | ||
| renderScheduled.set(false) | ||
| } | ||
|
|
||
| private fun ensureThread() { | ||
| if (glThread != null) return | ||
| val thread = HandlerThread("MorphViewGL").apply { start() } | ||
| glThread = thread | ||
| glHandler = Handler(thread.looper) | ||
| } | ||
|
|
||
| // Collapse a burst of draws into a single render of the newest state. | ||
| private fun scheduleRender() { | ||
| if (renderScheduled.compareAndSet(false, true)) { | ||
| glHandler?.post { | ||
| renderScheduled.set(false) | ||
| pending?.let { renderFrame(it) } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // GL thread. | ||
| private fun renderFrame(req: RenderRequest) { | ||
| val lease = produceLease(req) ?: return | ||
| leases.addLast(lease) | ||
| // Keep newest + one previous (HWUI may still be compositing last frame); free older buffers. | ||
| while (leases.size > 2) leases.removeFirst().close() | ||
| displayBitmap = lease.bitmap | ||
|
blazejkustra marked this conversation as resolved.
|
||
| onFrameReady() | ||
| } | ||
|
|
||
| /** | ||
| * GL thread. Lazily creates the pipeline and renders [req]. Returns null if GL initialisation or | ||
| * the render fails (e.g. context loss) β the caller skips the frame and retries on the next draw. | ||
| */ | ||
| private fun produceLease(req: RenderRequest): SwapChain.Lease? = try { | ||
| val gl = pipeline ?: MorphGlPipeline().also { pipeline = it } | ||
| gl.render(req.width, req.height, req.from, req.to, req.progress, req.blurRadiusPx, req.tintColor) | ||
| } catch (e: Exception) { | ||
| null | ||
| } | ||
|
|
||
| private data class RenderRequest( | ||
| val width: Int, | ||
| val height: Int, | ||
| val from: Bitmap?, | ||
| val to: Bitmap?, | ||
| val progress: Float, | ||
| val blurRadiusPx: Float, | ||
| val tintColor: Int?, | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.