scoop:scoop.graphics:0.1.0
src/native/resource.scoop
Selected production source · src/native/resource.scoop
1
package scoop.graphics3
import scoop.platform.concurrent.currentThreadId5
/** A native graphics operation failed. Invalid values and closed resources use standard exceptions. */6
public class GraphicsException public constructor(message: String): Exception(Some(message))8
internal 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 == None12
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
@Unsafe19
{20
platformGraphicsAccess(value)21
}22
return value23
}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
@Unsafe34
{35
// Closing an abandoned GPU resource remains safe.36
platformGraphicsDrop(value)37
}38
}39
case None -> {}40
}41
}42
}44
@Unsafe45
internal fun platformGraphicsDrop(pointer: Ptr<Unit>) {46
try {47
platformGraphicsAccess(pointer)48
}49
catch (_: GraphicsException) {}50
graphicsDrop(pointer)51
}53
internal fun ownedGraphics(pointer: Ptr<Unit>?, operation: String): GraphicsResource =54
GraphicsResource(pointer ?: throw GraphicsException(operation))56
internal fun graphicsResult(result: Int, operation: String) {57
if (result < 0)58
throw IllegalArgumentException(Some(operation))59
if (result == 0)60
throw GraphicsException(operation)61
}63
@Extern(name = "scoop_graphics_drop")64
internal fun graphicsDrop(pointer: Ptr<Unit>)