Scoop API

scoop:scoop.graphics:0.1.0

src/native/target.scoop

Selected production source · src/native/target.scoop

1package scoop.graphics.native
3import scoop.graphics.*
5/** A renderer-owned target. Canvas borrows end on every normal and exceptional callback exit. */
6public class RenderTarget internal constructor(
7 internal val resource: GraphicsResource,
8 public val pixelWidth: Int,
9 public val pixelHeight: Int,
10 public val scale: Double
11): Closeable {
12 private var borrowed = false
13 public val isClosed: Boolean get() = resource.isClosed
14 public fun <T> draw(block: (Canvas) -> T): T {
15 val pointer = available()
16 @Unsafe
17 {
18 val canvas = Canvas(ownedGraphics(targetCanvas(pointer, scale), "borrow canvas"))
19 borrowed = true
20 try {
21 return block(canvas)
22 }
23 finally {
24 borrowed = false;
25 canvas.resource.close()
26 }
27 }
28 }
29 /** Submits recorded GPU work without waiting for completion. */
30 public fun submit() {
31 @Unsafe
32 {
33 graphicsResult(targetSubmit(available(), false), "submit graphics target")
34 }
35 }
36 /** Ends use of this target; already recorded GPU work is safely submitted without presentation. */
37 public fun abort() {
38 submit()
39 }
40 /** Explicit readback is a synchronization point and returns straight RGBA8. */
41 public fun readPixels(): Array<Byte> {
42 val pointer = available()
43 val result = MutableArray<Byte>(pixelBytes(pixelWidth, pixelHeight)) { _ -> 0.toByte() }
44 @Unsafe
45 {
46 result.withDataPointer {
47 bytes,
48 _ -> @Unsafe
49 {
50 graphicsResult(targetRead(pointer, bytes), "read pixels")
51 }
52 }
53 }
54 return result.toArray()
55 }
56 /** Returns an independent CPU-readable image; GPU targets perform explicit readback. */
57 public fun snapshot(): Image {
58 @Unsafe
59 {
60 return Image(ownedGraphics(targetSnapshot(available()), "snapshot image"))
61 }
62 }
63 internal fun compositeSnapshot(): Image {
64 @Unsafe
65 {
66 return Image(
67 ownedGraphics(targetCompositeSnapshot(available()), "snapshot compositing image")
68 )
69 }
70 }
71 internal fun <T> withExclusive(block: (Ptr<Unit>) -> T): T {
72 val pointer = available()
73 borrowed = true
74 try {
75 return block(pointer)
76 } finally {
77 borrowed = false
78 }
79 }
80 private fun available(): Ptr<Unit> {
81 val pointer = resource.get()
82 if (borrowed)
83 throw IllegalStateException(Some("render target is borrowed"))
84 return pointer
85 }
86 public override fun close() {
87 resource.checkThread()
88 if (borrowed)
89 throw IllegalStateException(Some("cannot close a borrowed render target"))
90 resource.close()
91 }
94@Extern(name = "scoop_graphics_target_canvas")
95private fun targetCanvas(target: Ptr<Unit>, scale: Double): Ptr<Unit>?
97@Extern(name = "scoop_graphics_target_submit")
98internal fun targetSubmit(target: Ptr<Unit>, wait: Boolean): Int
100@Extern(name = "scoop_graphics_target_read")
101private fun targetRead(target: Ptr<Unit>, bytes: Ptr<Byte>): Int
103@Extern(name = "scoop_graphics_target_snapshot")
104private fun targetSnapshot(target: Ptr<Unit>): Ptr<Unit>?
106@Extern(name = "scoop_graphics_target_composite_snapshot")
107private fun targetCompositeSnapshot(target: Ptr<Unit>): Ptr<Unit>?