scoop:scoop.ui:0.1.0
src/paint/cache.scoop
Selected production source · src/paint/cache.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.platform.time.MonotonicInstant6
/** One element's small set of independently versioned drawing layers. */7
internal class PaintLayers: Closeable {8
val entries = ArrayList<CachedPaintLayer>()10
fun draw(11
canvas: Canvas,12
key: PaintKey,13
input: PaintLayerInput,14
policy: PaintCachePolicy,15
time: MonotonicInstant? = None,16
reads: FrameReads? = None,17
block: (Canvas) -> Unit18
): CachedPaintLayer? {19
input.check()20
var found: CachedPaintLayer? = None21
for (entry in entries) {22
if (23
entry.key ===24
key &&25
entry.input ==26
input &&27
entry.compatible(canvas) &&28
entry.current(time)29
) {30
found = Some(entry)31
break32
}33
}34
if (!paintLayerVisible(canvas, input.bounds)) {35
when (found) {36
case Some(val value) -> value.clearTiles()37
case None -> remove(key)38
}39
return found40
}41
val layer = found ?: create(canvas, key, input, reads, block)42
try {43
layer.paint(canvas, policy)44
return Some(layer)45
} catch (error: Throwable) {46
throwUi(cleanupUi(Some(error)) { remove(key) })47
throw error48
}49
}50
private fun create(51
canvas: Canvas,52
key: PaintKey,53
input: PaintLayerInput,54
reads: FrameReads?,55
block: (Canvas) -> Unit56
): CachedPaintLayer {57
remove(key)58
val drawing = canvas.recordDrawing(input.bounds, block)59
try {60
val layer = CachedPaintLayer(key, input, drawing, reads?.finish() ?: FrameInputs())61
entries.add(layer)62
return layer63
} catch (error: Throwable) {64
throw cleanupUi(Some(error)) { drawing.close() }!!65
}66
}67
fun retain(keys: ArrayList<PaintKey>) {68
var index = entries.size69
while (index > 0L) {70
index -= 1L71
val entry = entries[index]72
var found = false73
for (key in keys)74
if (entry.key === key)75
found = true76
if (!found) {77
entries.removeAt(index)78
entry.close()79
}80
}81
}82
private fun remove(key: PaintKey) {83
var index = entries.size84
while (index > 0L) {85
index -= 1L86
val entry = entries[index]87
if (entry.key === key) {88
entries.removeAt(index)89
entry.close()90
return91
}92
}93
}94
public override fun close() {95
var failure: Throwable? = None96
for (entry in entries)97
failure = cleanupUi(failure) { entry.close() }98
entries.clear()99
throwUi(failure)100
}101
}103
private fun paintLayerVisible(canvas: Canvas, bounds: Rect): Boolean =104
when (canvas.localClipBounds) {105
case Some(val clip) -> bounds.width >106
0.0 &&107
bounds.height >108
0.0 &&109
clip.width >110
0.0 &&111
clip.height >112
0.0 &&113
bounds.x <114
clip.x +115
clip.width &&116
clip.x <117
bounds.x +118
bounds.width &&119
bounds.y <120
clip.y +121
clip.height &&122
clip.y <123
bounds.y +124
bounds.height125
case None -> false126
}