Scoop API

scoop:scoop.ui:0.1.0

src/layout/scene_cache.scoop

Selected production source · src/layout/scene_cache.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.surface.FrameContext
5import scoop.surface.InputArea
6import scoop.platform.time.MonotonicInstant
8/** An explicit revision of a cache boundary, without a global identity table. */
9internal class SceneEpoch
11/** Only value geometry and owned drawing records may outlive a scene callback. */
12internal 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>)
19internal 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()
27/** Replays local operations into this frame; it never stores submitted input geometry. */
28internal 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: Boolean
35) {
36 fun matches(
37 node: MountedNode,
38 current: Array<SceneEpoch>,
39 canvas: Canvas,
40 time: MonotonicInstant
41 ): Boolean {
42 if (version != node.sceneVersion || epochs.size != current.size) return false
43 for (index in 0L..<epochs.size)
44 if (epochs[index] !== current[index]) return false
45 for (layer in layers)
46 if (!layer.compatible(canvas) || !layer.current(Some(time))) return false
47 return true
48 }
49 fun replay(
50 layout: NodeLayout,
51 frame: FrameContext,
52 overlay: OverlayLayout?,
53 scrollPath: Array<ScrollReveal>,
54 paintCache: PaintCachePolicy
55 ) {
56 replayCommands(commands, layout, frame, overlay, scrollPath, paintCache)
57 val node = layout.node
58 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 }
66/** The recorder has already validated the child order and the balanced borrowing scopes. */
67private fun replayCommands(
68 commands: Array<SceneCommand>,
69 layout: NodeLayout,
70 frame: FrameContext,
71 overlay: OverlayLayout?,
72 scrollPath: Array<ScrollReveal>,
73 paintCache: PaintCachePolicy
74) {
75 val node = layout.node
76 val canvas = frame.canvas
77 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 frame
105 .withState<Unit> { child ->
106 replayCommands(nested, layout, child, overlay, scrollPath, paintCache)
107 }
108 }