package com.margelo.nitro.thor import android.view.View import android.widget.FrameLayout import androidx.annotation.Keep import com.facebook.proguard.annotations.DoNotStrip import com.facebook.react.uimanager.ThemedReactContext /** * Nitro HybridView backing the JS `` on Android. Hosts a CPU * (Bitmap) or GPU (GL) renderer, switched by the `backend` prop. * * Fabric lays out the container but does not run a measure/layout pass on the * child views we add imperatively, so we force children to fill the container on * every layout (critical for GLSurfaceView, which otherwise stays 0x0). */ @Keep @DoNotStrip class HybridThorView(val context: ThemedReactContext) : HybridThorViewSpec() { private val container = object : FrameLayout(context) { override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { val w = r - l val h = b - t for (i in 0 until childCount) layoutChildToFill(getChildAt(i), w, h) } } private var cpuView: ThorCanvasView? = null private var glView: ThorGlView? = null private var usingGpu = false override val view: View = container override var shapes: Array = emptyArray() set(value) { field = value if (usingGpu) { glView?.setShapes(value) // requestRender() — draws on the GL thread } else { cpuView?.setShapes(value) // Render immediately so UI-thread / worklet-driven prop updates draw // without waiting for the Fabric afterUpdate() batch. The normal React // path's afterUpdate() then no-ops (reference dedup in renderIfNeeded). cpuView?.renderIfNeeded() } } override var backend: ThorBackend? = ThorBackend.CPU set(value) { field = value applyBackend(value == ThorBackend.GPU) } init { applyBackend(false) // default to CPU } private fun applyBackend(gpu: Boolean) { if (gpu == usingGpu && container.childCount > 0) return usingGpu = gpu container.removeAllViews() val params = FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT ) val child: View = if (gpu) { (glView ?: ThorGlView(context).also { glView = it }) } else { (cpuView ?: ThorCanvasView(context).also { cpuView = it }) } container.addView(child, params) // If the container is already sized (e.g. switching at runtime), lay the new // child out immediately so a GLSurfaceView gets its surface. if (container.width > 0 && container.height > 0) { layoutChildToFill(child, container.width, container.height) } if (gpu) glView?.setShapes(shapes) else { cpuView?.setShapes(shapes) cpuView?.renderIfNeeded() } } private fun layoutChildToFill(child: View, w: Int, h: Int) { if (w <= 0 || h <= 0) return child.measure( View.MeasureSpec.makeMeasureSpec(w, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(h, View.MeasureSpec.EXACTLY) ) child.layout(0, 0, w, h) } /** * Imperative worklet fast-path (see [ThorViewMethods.drawShapes]). Runs on the * caller's thread — a worklet, not the JS thread — and rasterizes immediately. * GLSurfaceView.requestRender() and View.postInvalidate() are both thread-safe. */ override fun drawShapes(shapes: Array) { // Imperative path only — do NOT touch the `shapes` prop backing field (its // setter renders synchronously via invalidate(), unsafe off the UI thread). if (usingGpu) { glView?.setShapes(shapes) // requestRender() — draws on the GL thread } else { cpuView?.drawFromWorklet(shapes) } } override fun afterUpdate() { if (!usingGpu) cpuView?.renderIfNeeded() } override fun onDropView() { glView?.release() cpuView?.release() } }