Scoop API

scoop:scoop.graphics:0.1.0

src/native/gpu_device.scoop

Selected production source · src/native/gpu_device.scoop

1package scoop.graphics.native
3import scoop.graphics.*
5/** An owner-thread handle to the canvas's existing GPU context and native queue. */
6public class GpuDevice internal constructor(
7 private val resource: GraphicsResource,
8 public val backend: GpuBackend
9): Closeable {
10 private var borrowed = false
11 public val isClosed: Boolean get() = resource.isClosed
13 public fun createBuffer(
14 width: Int,
15 height: Int,
16 format: NativeGpuFormat,
17 layout: GpuPixelLayout,
18 colorSpace: GpuColorSpace = GpuColorSpace.Srgb,
19 scale: Double = 1.0
20 ): 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 @Unsafe
27 {
28 return GpuBuffer(
29 RenderTarget(
30 ownedGraphics(
31 gpuBuffer(
32 pointer,
33 width,
34 height,
35 format.code(),
36 layout.code(),
37 colorSpace == GpuColorSpace.LinearSrgb
38 ),
39 "create GPU buffer with native format"
40 ),
41 width,
42 height,
43 scale
44 ),
45 format,
46 layout,
47 colorSpace
48 )
49 }
50 }
52 /**
53 * Imports immutable native pixels without copying. The owner must keep storage alive and
54 * unchanged until release. Submit producers on this device's queue before importing.
55 */
56 @Unsafe
57 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.Premultiplied
67 ): 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 @Unsafe
98 public fun <T> withNative(block: (NativeGpuDevice) -> T): T {
99 val pointer = available()
100 borrowed = true
101 try {
102 return GpuBorrow(ownedGraphics(gpuAccess(pointer), "borrow native GPU device")).use {
103 access -> @Unsafe
104 {
105 return@use block(gpuAccessDevice(access.get()).native())
106 }
107 }
108 } finally {
109 borrowed = false
110 }
111 }
113 public fun drain() {
114 @Unsafe
115 {
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 pointer
124 }
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 return
131 try {
132 drain()
133 } finally {
134 resource.close()
135 }
136 }
139internal class GpuBorrow(private val resource: GraphicsResource): Closeable {
140 fun get(): Ptr<Unit> = resource.get()
141 public override fun close() {
142 if (resource.isClosed)
143 return
144 try {
145 @Unsafe
146 {
147 graphicsResult(gpuAccessEnd(resource.get()), "finish native GPU access")
148 }
149 } finally {
150 resource.close()
151 }
152 }
155@Extern(name = "scoop_graphics_gpu_access_end")
156private fun gpuAccessEnd(access: Ptr<Unit>): Int
158internal fun canvasGpuDevice(canvas: Ptr<Unit>): GpuDevice? {
159 @Unsafe
160 {
161 if (!canvasHasGpu(canvas))
162 return None
163 val resource = ownedGraphics(canvasGpu(canvas), "retain canvas GPU device")
164 return Some(GpuDevice(resource, gpuInfo(resource.get()).backend()))
165 }
168@Extern(name = "scoop_graphics_canvas_has_gpu")
169private fun canvasHasGpu(canvas: Ptr<Unit>): Boolean
171@Extern(name = "scoop_graphics_canvas_gpu")
172private fun canvasGpu(canvas: Ptr<Unit>): Ptr<Unit>?
174@Extern(name = "scoop_graphics_gpu_info")
175private fun gpuInfo(device: Ptr<Unit>): GpuDeviceInfo
177@Extern(name = "scoop_graphics_gpu_drain")
178private fun gpuDrain(device: Ptr<Unit>): Int
180@Extern(name = "scoop_graphics_gpu_buffer")
181private fun gpuBuffer(
182 device: Ptr<Unit>,
183 width: Int,
184 height: Int,
185 format: ULong,
186 layout: Int,
187 linear: Boolean
188): Ptr<Unit>?
190@Extern(name = "scoop_graphics_gpu_access")
191private fun gpuAccess(device: Ptr<Unit>): Ptr<Unit>?
193@Extern(name = "scoop_graphics_gpu_access_device")
194internal fun gpuAccessDevice(access: Ptr<Unit>): GpuDeviceInfo
196@Extern(name = "scoop_graphics_gpu_import")
197private 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: Int
208): Ptr<Unit>?