package com.margelo.nitro.thor /** * Parallel primitive arrays describing a scene, ready to hand to JNI. Fields are * mutable so one instance can be reused across frames (see [ThorSceneMarshal]). */ internal class MarshalledScene( var types: IntArray, var fills: IntArray, var strokes: IntArray, var params: FloatArray, var pointsFlat: FloatArray, var pointCounts: IntArray, ) /** * Converts a ThorShape[] into a MarshalledScene (device pixels). One instance * per view (single-threaded use). Reuses its buffers AND the returned * MarshalledScene across frames and caches parsed colors, so steady-state * animation does no per-frame allocation. */ internal class ThorSceneMarshal { private var types = IntArray(0) private var fills = IntArray(0) private var strokes = IntArray(0) private var params = FloatArray(0) private var pointCounts = IntArray(0) private var pointsFlat = FloatArray(0) // Reused output holder — its array fields are repointed when a buffer grows. private val out = MarshalledScene(types, fills, strokes, params, pointsFlat, pointCounts) private val colorCache = HashMap() fun marshal(shapes: Array, density: Float): MarshalledScene { val n = shapes.size if (types.size != n) { types = IntArray(n); fills = IntArray(n); strokes = IntArray(n) params = FloatArray(n * 8); pointCounts = IntArray(n) } var totalPoints = 0 for (s in shapes) if (s.type == ThorShapeType.POLYGON) totalPoints += s.points?.size ?: 0 if (pointsFlat.size != totalPoints) pointsFlat = FloatArray(totalPoints) // Clear stale per-shape geometry from the reused params buffer. java.util.Arrays.fill(params, 0f) var pOff = 0 for (i in 0 until n) { val s = shapes[i] types[i] = s.type.value fills[i] = parseColor(s.fill) strokes[i] = parseColor(s.stroke) pointCounts[i] = 0 val b = i * 8 params[b] = sc(s.strokeWidth, density) params[b + 7] = if (s.closed == false) 0f else 1f when (s.type) { ThorShapeType.RECT -> { params[b + 1] = sc(s.x, density); params[b + 2] = sc(s.y, density) params[b + 3] = sc(s.width, density); params[b + 4] = sc(s.height, density) params[b + 5] = sc(s.rx, density); params[b + 6] = sc(s.ry, density) } ThorShapeType.CIRCLE -> { params[b + 1] = sc(s.cx, density); params[b + 2] = sc(s.cy, density) params[b + 3] = sc(s.radius, density) } ThorShapeType.LINE -> { params[b + 1] = sc(s.x1, density); params[b + 2] = sc(s.y1, density) params[b + 3] = sc(s.x2, density); params[b + 4] = sc(s.y2, density) } ThorShapeType.POLYGON -> { val pts = s.points if (pts != null) { pointCounts[i] = pts.size for (k in pts.indices) pointsFlat[pOff + k] = (pts[k] * density).toFloat() pOff += pts.size } } } } // Repoint the reused holder at the current buffers (no allocation unless a // buffer grew this frame). out.types = types; out.fills = fills; out.strokes = strokes out.params = params; out.pointsFlat = pointsFlat; out.pointCounts = pointCounts return out } private fun sc(v: Double?, density: Float): Float = ((v ?: 0.0) * density).toFloat() /** "#RGB" / "#RRGGBB" / "#AARRGGBB" -> 0xAARRGGBB; null -> 0. Cached + alloc-free. */ private fun parseColor(hex: String?): Int { if (hex == null) return 0 val cached = colorCache[hex] if (cached != null) return cached val value = parseHex(hex) // Bound the cache: callers that vary alpha per frame (e.g. a crossfade that // builds a fresh "#aarrggbb" every frame) would otherwise grow it without // limit — a slow leak. Parsing is cheap, so just drop the cache when it gets // large; stable palettes stay comfortably under the cap and keep hitting. if (colorCache.size >= MAX_COLOR_CACHE) colorCache.clear() colorCache[hex] = value return value } private fun parseHex(hex: String): Int { val i = if (hex.isNotEmpty() && hex[0] == '#') 1 else 0 return when (hex.length - i) { 3 -> { val r = digit(hex[i]); val g = digit(hex[i + 1]); val b = digit(hex[i + 2]) val rr = (r shl 4) or r; val gg = (g shl 4) or g; val bb = (b shl 4) or b (0xFF shl 24) or (rr shl 16) or (gg shl 8) or bb } 6 -> { var c = 0 for (k in 0 until 6) c = (c shl 4) or digit(hex[i + k]) (0xFF shl 24) or c } 8 -> { var c = 0L for (k in 0 until 8) c = (c shl 4) or digit(hex[i + k]).toLong() c.toInt() } else -> 0xFF000000.toInt() } } private fun digit(c: Char): Int = Character.digit(c, 16).let { if (it < 0) 0 else it } private companion object { // Generous for real palettes; small enough that a runaway per-frame colour // stream can never accumulate unbounded memory. const val MAX_COLOR_CACHE = 256 } }