Scoop API

scoop:scoop.graphics:0.1.0

src/drawing/offscreen.scoop

Selected production source · src/drawing/offscreen.scoop

1package scoop.graphics
3import scoop.graphics.native.RenderTarget
5/** Independent pixels compatible with the canvas that created this surface. */
6public class OffscreenSurface internal constructor(private val target: RenderTarget): Closeable {
7 public val pixelWidth: Int get() = target.pixelWidth
8 public val pixelHeight: Int get() = target.pixelHeight
9 public val scale: Double get() = target.scale
10 public val isClosed: Boolean get() = target.isClosed
11 public fun <T> draw(block: (Canvas) -> T): T = target.draw(block)
12 /** Keeps GPU pixels on their context; later draws cannot change this image. */
13 public fun snapshot(): Image = target.compositeSnapshot()
14 public fun readPixels(): Array<Byte> = target.readPixels()
15 public override fun close() {
16 target.close()
17 }
20internal fun createOffscreen(
21 canvas: Ptr<Unit>,
22 width: Int,
23 height: Int,
24 scale: Double
25): OffscreenSurface {
26 pixelBytes(width, height)
27 checkScalar(scale)
28 requireGraphics(scale > 0.0 && scale.toFloat() > 0.0, "invalid offscreen scale")
29 @Unsafe
30 {
31 return OffscreenSurface(
32 RenderTarget(
33 ownedGraphics(
34 offscreenCreate(canvas, width, height),
35 "create compatible offscreen surface"
36 ),
37 width,
38 height,
39 scale
40 )
41 )
42 }
45@Extern(name = "scoop_graphics_offscreen_create")
46private fun offscreenCreate(canvas: Ptr<Unit>, width: Int, height: Int): Ptr<Unit>?