scoop:scoop.graphics:0.1.0
src/native/gpu_device.scoop
Selected production source · src/native/gpu_device.scoop
1
package scoop.graphics.native3
import scoop.graphics.*5
/** An owner-thread handle to the canvas's existing GPU context and native queue. */6
public class GpuDevice internal constructor(7
private val resource: GraphicsResource,8
public val backend: GpuBackend9
): Closeable {10
private var borrowed = false11
public val isClosed: Boolean get() = resource.isClosed13
public fun createBuffer(14
width: Int,15
height: Int,16
format: NativeGpuFormat,17
layout: GpuPixelLayout,18
colorSpace: GpuColorSpace = GpuColorSpace.Srgb,19
scale: Double = 1.020
): GpuBuffer {21
val pointer = available()22
requireGraphics(width > 0 && height > 0, "invalid GPU buffer dimensions")23
requireGraphics(format.backend() == backend, "GPU format belongs to another backend")24
checkScalar(scale)25
requireGraphics(scale > 0.0 && scale.toFloat() > 0.0, "invalid GPU buffer scale")26
@Unsafe27
{28
return GpuBuffer(29
RenderTarget(30
ownedGraphics(31
gpuBuffer(32
pointer,33
width,34
height,35
format.code(),36
layout.code(),37
colorSpace == GpuColorSpace.LinearSrgb38
),39
"create GPU buffer with native format"40
),41
width,42
height,43
scale44
),45
format,46
layout,47
colorSpace48
)49
}50
}52
/**53
* Imports immutable native pixels without copying. The owner must keep storage alive and54
* unchanged until release. Submit producers on this device's queue before importing.55
*/56
@Unsafe57
public fun importImage(58
texture: NativeGpuTexture,59
width: Int,60
height: Int,61
format: NativeGpuFormat,62
layout: GpuPixelLayout,63
owner: NativeGpuImageOwner,64
origin: GpuImageOrigin = GpuImageOrigin.TopLeft,65
colorSpace: GpuColorSpace = GpuColorSpace.Srgb,66
alpha: GpuImageAlpha = GpuImageAlpha.Premultiplied67
): Image {68
val pointer = available()69
requireGraphics(width > 0 && height > 0, "invalid GPU image dimensions")70
requireGraphics(71
texture.backend() == backend && format.backend() == backend,72
"GPU image belongs to another backend"73
)74
return Image(75
ownedGraphics(76
gpuImport(77
pointer,78
texture.info(),79
width,80
height,81
format.code(),82
layout.code(),83
owner,84
origin == GpuImageOrigin.BottomLeft,85
colorSpace == GpuColorSpace.LinearSrgb,86
alpha.code()87
),88
"import native GPU image"89
)90
)91
}93
/**94
* Flushes pending work before a synchronous native borrow. Submit on the supplied queue;95
* do not reenter this context, suspend, save unretained handles, or mutate a published Image.96
*/97
@Unsafe98
public fun <T> withNative(block: (NativeGpuDevice) -> T): T {99
val pointer = available()100
borrowed = true101
try {102
return GpuBorrow(ownedGraphics(gpuAccess(pointer), "borrow native GPU device")).use {103
access -> @Unsafe104
{105
return@use block(gpuAccessDevice(access.get()).native())106
}107
}108
} finally {109
borrowed = false110
}111
}113
public fun drain() {114
@Unsafe115
{116
graphicsResult(gpuDrain(available()), "drain GPU device")117
}118
}119
private fun available(): Ptr<Unit> {120
val pointer = resource.get()121
if (borrowed)122
throw IllegalStateException(Some("GPU device is borrowed"))123
return pointer124
}125
public override fun close() {126
resource.checkThread()127
if (borrowed)128
throw IllegalStateException(Some("cannot close a borrowed GPU device"))129
if (isClosed)130
return131
try {132
drain()133
} finally {134
resource.close()135
}136
}137
}139
internal class GpuBorrow(private val resource: GraphicsResource): Closeable {140
fun get(): Ptr<Unit> = resource.get()141
public override fun close() {142
if (resource.isClosed)143
return144
try {145
@Unsafe146
{147
graphicsResult(gpuAccessEnd(resource.get()), "finish native GPU access")148
}149
} finally {150
resource.close()151
}152
}153
}155
@Extern(name = "scoop_graphics_gpu_access_end")156
private fun gpuAccessEnd(access: Ptr<Unit>): Int158
internal fun canvasGpuDevice(canvas: Ptr<Unit>): GpuDevice? {159
@Unsafe160
{161
if (!canvasHasGpu(canvas))162
return None163
val resource = ownedGraphics(canvasGpu(canvas), "retain canvas GPU device")164
return Some(GpuDevice(resource, gpuInfo(resource.get()).backend()))165
}166
}168
@Extern(name = "scoop_graphics_canvas_has_gpu")169
private fun canvasHasGpu(canvas: Ptr<Unit>): Boolean171
@Extern(name = "scoop_graphics_canvas_gpu")172
private fun canvasGpu(canvas: Ptr<Unit>): Ptr<Unit>?174
@Extern(name = "scoop_graphics_gpu_info")175
private fun gpuInfo(device: Ptr<Unit>): GpuDeviceInfo177
@Extern(name = "scoop_graphics_gpu_drain")178
private fun gpuDrain(device: Ptr<Unit>): Int180
@Extern(name = "scoop_graphics_gpu_buffer")181
private fun gpuBuffer(182
device: Ptr<Unit>,183
width: Int,184
height: Int,185
format: ULong,186
layout: Int,187
linear: Boolean188
): Ptr<Unit>?190
@Extern(name = "scoop_graphics_gpu_access")191
private fun gpuAccess(device: Ptr<Unit>): Ptr<Unit>?193
@Extern(name = "scoop_graphics_gpu_access_device")194
internal fun gpuAccessDevice(access: Ptr<Unit>): GpuDeviceInfo196
@Extern(name = "scoop_graphics_gpu_import")197
private fun gpuImport(198
device: Ptr<Unit>,199
texture: GpuTextureInfo,200
width: Int,201
height: Int,202
format: ULong,203
layout: Int,204
owner: NativeGpuImageOwner,205
bottomLeft: Boolean,206
linear: Boolean,207
alpha: Int208
): Ptr<Unit>?