scoop:scoop.graphics:0.1.0
src/drawing/offscreen.scoop
Selected production source · src/drawing/offscreen.scoop
1
package scoop.graphics3
import scoop.graphics.native.RenderTarget5
/** Independent pixels compatible with the canvas that created this surface. */6
public class OffscreenSurface internal constructor(private val target: RenderTarget): Closeable {7
public val pixelWidth: Int get() = target.pixelWidth8
public val pixelHeight: Int get() = target.pixelHeight9
public val scale: Double get() = target.scale10
public val isClosed: Boolean get() = target.isClosed11
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
}18
}20
internal fun createOffscreen(21
canvas: Ptr<Unit>,22
width: Int,23
height: Int,24
scale: Double25
): OffscreenSurface {26
pixelBytes(width, height)27
checkScalar(scale)28
requireGraphics(scale > 0.0 && scale.toFloat() > 0.0, "invalid offscreen scale")29
@Unsafe30
{31
return OffscreenSurface(32
RenderTarget(33
ownedGraphics(34
offscreenCreate(canvas, width, height),35
"create compatible offscreen surface"36
),37
width,38
height,39
scale40
)41
)42
}43
}45
@Extern(name = "scoop_graphics_offscreen_create")46
private fun offscreenCreate(canvas: Ptr<Unit>, width: Int, height: Int): Ptr<Unit>?