package com.margelo.nitro.thor import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas import android.util.Log import android.view.View /** * CPU view: rasterizes the scene into an ARGB_8888 Bitmap via JNI and draws it. */ class ThorCanvasView(context: Context) : View(context) { private val density = resources.displayMetrics.density private val marshaller = ThorSceneMarshal() // Guards the bitmap + marshaller + render call. The imperative fast-path // (drawFromWorklet) rasterizes on a worklet thread, while onDraw / onSizeChanged // touch the same bitmap on the UI thread — this serializes them. private val renderLock = Any() private var bitmap: Bitmap? = null private var shapes: Array = emptyArray() private var lastRendered: Array? = null private var frame = 0L // PERF instrumentation (windowed averages, logged under "ThorPerf"). private var marshalNs = 0L private var rasterNs = 0L private var maxTotalNs = 0L // This view's own native ThorVG renderer (retains its SwCanvas + shape pool // across frames). 0L once released — every rasterize path checks it. private var nativePtr: Long = ThorNative.nativeCpuCreate() fun setShapes(value: Array) { shapes = value } /** * Destroy the native renderer and free the bitmap. Idempotent; safe to call * from the UI thread (invoked from HybridThorView.onDropView). */ fun release() { synchronized(renderLock) { val ptr = nativePtr nativePtr = 0L if (ptr != 0L) ThorNative.nativeCpuDestroy(ptr) bitmap?.recycle() bitmap = null lastRendered = null } } /** * Rasterize if the scene changed. Safe to call from both the Fabric batch * (afterUpdate) and directly from the `shapes` prop setter — the reference * dedup means whichever fires first renders, the other is a no-op. Called on * the UI thread. */ fun renderIfNeeded() { synchronized(renderLock) { val bmp = bitmap ?: return if (shapes === lastRendered) return lastRendered = shapes rasterizeLocked(bmp, shapes) } invalidate() } /** * Imperative worklet fast-path: rasterize [value] straight into the bitmap on * the CALLING thread — a reanimated `useFrameCallback` or a VisionCamera frame * processor, i.e. NOT the JS thread — then post a UI-thread blit. This is the * off-JS-thread update path; the JS thread can be fully blocked and this keeps * drawing. Guarded by renderLock so it can't race onDraw or a bitmap resize. */ fun drawFromWorklet(value: Array) { synchronized(renderLock) { val bmp = bitmap ?: return shapes = value lastRendered = value // an afterUpdate() with the old prop scene then no-ops rasterizeLocked(bmp, value) } postInvalidateOnAnimation() } /** Must be called while holding [renderLock]. */ private fun rasterizeLocked(bmp: Bitmap, scene: Array) { val ptr = nativePtr if (ptr == 0L) return val t0 = System.nanoTime() val m = marshaller.marshal(scene, density) val t1 = System.nanoTime() ThorNative.nativeRenderScene( ptr, bmp, bmp.width, bmp.height, m.types, m.fills, m.strokes, m.params, m.pointsFlat, m.pointCounts ) val t2 = System.nanoTime() // PERF: windowed per-frame native cost (marshal + ThorVG rasterize). This is // the pure rendering work, independent of vsync/display rate. marshalNs += t1 - t0 rasterNs += t2 - t1 val total = t2 - t0 if (total > maxTotalNs) maxTotalNs = total frame++ if (frame % PERF_WINDOW == 0L) { val n = PERF_WINDOW.toDouble() Log.i( "ThorPerf", "CPU %dx%d shapes=%d marshal=%.3fms raster=%.3fms total=%.3fms max=%.3fms thread=%s".format( bmp.width, bmp.height, scene.size, marshalNs / n / 1e6, rasterNs / n / 1e6, (marshalNs + rasterNs) / n / 1e6, maxTotalNs / 1e6, Thread.currentThread().name ) ) marshalNs = 0L; rasterNs = 0L; maxTotalNs = 0L } } override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) synchronized(renderLock) { bitmap?.recycle() bitmap = if (w > 0 && h > 0) Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) else null lastRendered = null // force a redraw into the new bitmap } renderIfNeeded() } override fun onDraw(canvas: Canvas) { synchronized(renderLock) { bitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) } } } private companion object { const val PERF_WINDOW = 120L } }