scoop:scoop.ui:0.1.0
src/layout/scene_cache.scoop
Selected production source · src/layout/scene_cache.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.surface.FrameContext5
import scoop.surface.InputArea6
import scoop.platform.time.MonotonicInstant8
/** An explicit revision of a cache boundary, without a global identity table. */9
internal class SceneEpoch11
/** Only value geometry and owned drawing records may outlive a scene callback. */12
internal enum SceneCommand {13
Concat(Transform), Clip(Rect), Save, SaveLayer(Double, BlendMode),14
SaveBackdropLayer(Double, Rect?), Restore,15
Place(InputArea, Rect), Anchor(LayoutAnchor, Rect), Semantics(SemanticsHandle, Rect),16
Children(Array<Long>?, Array<SceneEpoch>), Drawing(CachedPaintLayer), Scope(Array<SceneCommand>)17
}19
internal class SceneRecorder {20
private val commands = ArrayList<SceneCommand>()21
fun add(value: SceneCommand) {22
commands.add(value)23
}24
fun finish(): Array<SceneCommand> = commands.toArray()25
}27
/** Replays local operations into this frame; it never stores submitted input geometry. */28
internal class CachedScene(29
private val version: Long,30
private val epochs: Array<SceneEpoch>,31
private val commands: Array<SceneCommand>,32
private val layers: Array<CachedPaintLayer>,33
private val paintKeys: ArrayList<PaintKey>,34
private val presentedChildren: Boolean35
) {36
fun matches(37
node: MountedNode,38
current: Array<SceneEpoch>,39
canvas: Canvas,40
time: MonotonicInstant41
): Boolean {42
if (version != node.sceneVersion || epochs.size != current.size) return false43
for (index in 0L..<epochs.size)44
if (epochs[index] !== current[index]) return false45
for (layer in layers)46
if (!layer.compatible(canvas) || !layer.current(Some(time))) return false47
return true48
}49
fun replay(50
layout: NodeLayout,51
frame: FrameContext,52
overlay: OverlayLayout?,53
scrollPath: Array<ScrollReveal>,54
paintCache: PaintCachePolicy55
) {56
replayCommands(commands, layout, frame, overlay, scrollPath, paintCache)57
val node = layout.node58
if (node.active && node.root.isOpen) {59
if (!presentedChildren)60
layout.presentChildren(frame, overlay, scrollPath, paintCache, epochs, None)61
node.paintLayers.retain(paintKeys)62
}63
}64
}66
/** The recorder has already validated the child order and the balanced borrowing scopes. */67
private fun replayCommands(68
commands: Array<SceneCommand>,69
layout: NodeLayout,70
frame: FrameContext,71
overlay: OverlayLayout?,72
scrollPath: Array<ScrollReveal>,73
paintCache: PaintCachePolicy74
) {75
val node = layout.node76
val canvas = frame.canvas77
for (command in commands)78
when (command) {79
case Concat(val value) -> canvas.concat(value)80
case Clip(val value) -> canvas.clipRect(value)81
case Save -> canvas.save()82
case SaveLayer(val opacity, val blend) -> canvas.saveLayer(opacity, blendMode = blend)83
case SaveBackdropLayer(val opacity, val bounds) ->84
canvas.saveBackdropLayer(opacity, bounds = bounds)85
case Restore -> canvas.restore()87
case Place(val area, val rect) -> {88
frame.place(area, rect)89
node.root.focus.place(area, scrollPath)90
}92
case Anchor(val handle, val rect) ->93
handle.capture(node, node.root.overlays.frame, canvas, rect)95
case Semantics(val handle, val rect) -> {96
checkSemanticOwner(node, handle)97
handle.controller.capture(handle, canvas, rect, scrollPath)98
}100
case Children(val order, val childEpochs) ->101
layout.presentChildren(frame, overlay, scrollPath, paintCache, childEpochs, order)102
case Drawing(val layer) -> layer.paint(canvas, paintCache)103
case Scope(val nested) ->104
frame105
.withState<Unit> { child ->106
replayCommands(nested, layout, child, overlay, scrollPath, paintCache)107
}108
}109
}