Scoop API

scoop:scoop.graphics:0.1.0

src/native/resource.scoop

Selected production source · src/native/resource.scoop

1package scoop.graphics
3import scoop.platform.concurrent.currentThreadId
5/** A native graphics operation failed. Invalid values and closed resources use standard exceptions. */
6public class GraphicsException public constructor(message: String): Exception(Some(message))
8internal class GraphicsResource(pointer: Ptr<Unit>): Closeable {
9 private val thread = currentThreadId()
10 private var pointer: Ptr<Unit>? = Some(pointer)
11 val isClosed: Boolean get() = pointer == None
12 fun checkThread() {
13 if (thread != currentThreadId())
14 throw IllegalStateException(Some("graphics resource requires its owner thread"))
15 }
16 fun get(): Ptr<Unit> {
17 val value = getForClose()
18 @Unsafe
19 {
20 platformGraphicsAccess(value)
21 }
22 return value
23 }
24 fun getForClose(): Ptr<Unit> {
25 checkThread()
26 return pointer ?: throw IllegalStateException(Some("graphics resource is closed"))
27 }
28 public override fun close() {
29 checkThread()
30 when (pointer) {
31 case Some(val value) -> {
32 pointer = None;
33 @Unsafe
34 {
35 // Closing an abandoned GPU resource remains safe.
36 platformGraphicsDrop(value)
37 }
38 }
39 case None -> {}
40 }
41 }
44@Unsafe
45internal fun platformGraphicsDrop(pointer: Ptr<Unit>) {
46 try {
47 platformGraphicsAccess(pointer)
48 }
49 catch (_: GraphicsException) {}
50 graphicsDrop(pointer)
53internal fun ownedGraphics(pointer: Ptr<Unit>?, operation: String): GraphicsResource =
54 GraphicsResource(pointer ?: throw GraphicsException(operation))
56internal fun graphicsResult(result: Int, operation: String) {
57 if (result < 0)
58 throw IllegalArgumentException(Some(operation))
59 if (result == 0)
60 throw GraphicsException(operation)
63@Extern(name = "scoop_graphics_drop")
64internal fun graphicsDrop(pointer: Ptr<Unit>)