Scoop API

scoop:scoop.surface:0.1.0

src/platform/metal/renderer.scoop

Selected production source · src/platform/metal/renderer.scoop

1package scoop.surface
3import scoop.graphics.*
4import scoop.graphics.native.RenderTarget
5import scoop.graphics.native.metal.MetalContext
6import scoop.window.*
7import scoop.window.native.*
9internal fun createRendererHost(): RendererHost {
10 @Unsafe
11 {
12 val native = metalHostCreate() ?: throw GraphicsException("create Metal device and queue")
13 try {
14 return MetalRendererHost(
15 native,
16 MetalContext.create(metalHostObject(native, 0), metalHostObject(native, 1))
17 )
18 } catch (error: Throwable) {
19 metalHostClose(native);
20 throw error
21 }
22 }
25private class MetalRendererHost(
26 private val native: Ptr<Unit>,
27 private val context: MetalContext
28) : RendererHost {
29 private var closed = false
30 public override fun create(window: Window, proxy: ApplicationProxy): WindowRenderer {
31 if (closed) throw IllegalStateException(Some("renderer host is closed"))
32 @Unsafe
33 {
34 val lease = window.acquireNativeWindow()
35 try {
36 val view = metalContentView(lease)
37 val renderer = MetalWindowRenderer(window, proxy, lease, context)
38 renderer.install(native, view)
39 return renderer
40 } catch (error: Throwable) {
41 lease.close();
42 throw error
43 }
44 }
45 }
46 public override fun close() {
47 if (closed) return
48 closed = true
49 val failure = cleanupFailure(None) { context.close() }
50 @Unsafe
51 {
52 metalHostClose(native)
53 }
54 throwFailure(failure)
55 }
58private class MetalWindowRenderer(
59 private val window: Window,
60 private val proxy: ApplicationProxy,
61 private val lease: NativeWindowLease,
62 private val context: MetalContext
63) : WindowRenderer {
64 private var native: Ptr<Unit>? = None
65 private var callback: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None
66 private var metrics: WindowMetrics? = None
67 private var wanted = false
69 @Unsafe
70 fun install(host: Ptr<Unit>, view: Ptr<Unit>) {
71 val registration = foreignCallback<(Ptr<Unit>, Int) -> Int>(
72 { reason: Int -> notify(reason) },
73 0,
74 ForeignCallbackMode.Reusable
75 )
76 try {
77 native = Some(
78 metalSurfaceCreate(host, view, registration.function, registration.context) ?:
79 throw GraphicsException("install CAMetalLayer and display link")
80 )
81 callback = Some(registration)
82 } catch (error: Throwable) {
83 releaseForeignCallback(registration);
84 throw error
85 }
86 }
87 private fun notify(reason: Int): Int {
88 if (native == None) return 0
89 val accepted = proxy
90 .post { _ ->
91 if (native != None && (reason == 2 || wanted) && window.state == WindowState.Open)
92 window.requestRedraw()
93 }
94 return if (accepted) 1 else 0
95 }
96 private fun checkCallback() {
97 @Unsafe
98 {
99 when (callback) {
100 case Some(val value) -> throwFailure(foreignCallbackFailure(value))
101 case None -> throw IllegalStateException(Some("window renderer is closed"))
102 }
103 }
104 }
105 private fun pointer(): Ptr<Unit> {
106 checkCallback()
107 return native ?: throw IllegalStateException(Some("window renderer is closed"))
108 }
109 public override fun configure(value: WindowMetrics) {
110 @Unsafe
111 {
112 metalResult(
113 metalSurfaceConfigure(
114 pointer(),
115 value.physicalSize.width,
116 value.physicalSize.height,
117 value.scaleFactor
118 ),
119 "configure Metal surface"
120 )
121 }
122 metrics = Some(value)
123 }
124 public override fun setFrameDemand(required: Boolean) {
125 wanted = required
126 when (native) {
127 case Some(val value) ->
128 @Unsafe
129 {
130 metalResult(metalSurfaceDemand(value, required), "set Metal frame demand")
131 }
133 case None -> {
134 if (required) throw IllegalStateException(Some("window renderer is closed"))
135 }
136 }
137 if (required) checkCallback()
138 }
139 public override fun acquire(): FrameAcquisition {
140 val configuration = metrics ?: return FrameAcquisition.Deferred
141 @Unsafe
142 {
143 val result = metalSurfaceAcquire(pointer())
144 if (result.status < 0) throw GraphicsException("acquire Metal drawable")
145 if (result.pointer == None) return FrameAcquisition.Deferred
146 val frame = result.pointer!!
147 try {
148 val target = context
149 .target(
150 metalFrameTexture(frame),
151 configuration.physicalSize.width,
152 configuration.physicalSize.height,
153 configuration.scaleFactor
154 )
155 return FrameAcquisition.Ready(MetalFrame(window, frame, target))
156 } catch (error: Throwable) {
157 metalFrameClose(frame);
158 throw error
159 }
160 }
161 }
162 public override fun close() {
163 val previous = native
164 native = None
165 wanted = false
166 var failure: Throwable? = None
167 when (previous) {
168 case Some(val value) -> {
169 failure = cleanupFailure(failure) {
170 @Unsafe
171 {
172 metalResult(metalSurfaceClose(value), "close Metal surface")
173 }
174 }
175 }
177 case None -> {}
178 }
179 @Unsafe
180 {
181 when (callback) {
182 case Some(val value) -> {
183 callback = None
184 failure = cleanupFailure(failure) {
185 @Unsafe
186 {
187 throwFailure(foreignCallbackFailure(value))
188 }
189 }
190 releaseForeignCallback(value)
191 }
193 case None -> {}
194 }
195 }
196 lease.close()
197 throwFailure(failure)
198 }
201private class MetalFrame(
202 private val window: Window,
203 private val native: Ptr<Unit>,
204 private val target: RenderTarget
205) : RenderFrame {
206 private var submitted = false
207 private var closed = false
208 private fun checkOpen() {
209 if (closed || submitted) throw IllegalStateException(Some("frame has ended"))
210 }
211 public override fun draw(block: (Canvas) -> Unit) {
212 checkOpen();
213 target.draw(block)
214 }
215 public override fun submit(): Boolean {
216 checkOpen()
217 if (window.state != WindowState.Open) return false
218 @Unsafe
219 {
220 val current = metalFrameCurrent(native)
221 if (current < 0) throw GraphicsException("check Metal frame configuration")
222 if (current == 0) return false
223 }
224 target.submit()
225 window.prePresentNotify()
226 @Unsafe
227 {
228 val result = metalFramePresent(native)
229 if (result < 0) throw GraphicsException("present Metal drawable")
230 submitted = true
231 return result == 1
232 }
233 }
234 public override fun close() {
235 if (closed) return
236 closed = true
237 var failure: Throwable? = None
238 if (!submitted) failure = cleanupFailure(failure) { target.abort() }
239 failure = cleanupFailure(failure) { target.close() }
240 @Unsafe
241 {
242 metalFrameClose(native)
243 }
244 throwFailure(failure)
245 }
248private fun metalResult(result: Int, operation: String) {
249 if (result != 1) throw GraphicsException(operation)
252@CLayout
253private struct MetalAcquiredFrame(val pointer: Ptr<Unit>?, val status: Int)
255@Extern(name = "scoop_surface_metal_host_create")
256private fun metalHostCreate(): Ptr<Unit>?
258@Extern(name = "scoop_surface_metal_host_object")
259private fun metalHostObject(host: Ptr<Unit>, kind: Int): Ptr<Unit>
261@Extern(name = "scoop_surface_metal_host_close")
262private fun metalHostClose(host: Ptr<Unit>)
264@Extern(name = "scoop_surface_metal_surface_create")
265private fun metalSurfaceCreate(
266 host: Ptr<Unit>,
267 view: Ptr<Unit>,
268 callback: FunPtr<(Ptr<Unit>, Int) -> Int>,
269 context: Ptr<Unit>
270): Ptr<Unit>?
272@Extern(name = "scoop_surface_metal_surface_configure")
273private fun metalSurfaceConfigure(surface: Ptr<Unit>, width: Int, height: Int, scale: Double): Int
275@Extern(name = "scoop_surface_metal_surface_demand")
276private fun metalSurfaceDemand(surface: Ptr<Unit>, required: Boolean): Int
278@Extern(name = "scoop_surface_metal_surface_acquire")
279private fun metalSurfaceAcquire(surface: Ptr<Unit>): MetalAcquiredFrame
281@Extern(name = "scoop_surface_metal_surface_close")
282private fun metalSurfaceClose(surface: Ptr<Unit>): Int
284@Extern(name = "scoop_surface_metal_frame_texture")
285private fun metalFrameTexture(frame: Ptr<Unit>): Ptr<Unit>
287@Extern(name = "scoop_surface_metal_frame_current")
288private fun metalFrameCurrent(frame: Ptr<Unit>): Int
290@Extern(name = "scoop_surface_metal_frame_present")
291private fun metalFramePresent(frame: Ptr<Unit>): Int
293@Extern(name = "scoop_surface_metal_frame_close")
294private fun metalFrameClose(frame: Ptr<Unit>)