scoop:scoop.surface:0.1.0
src/platform/metal/renderer.scoop
Selected production source · src/platform/metal/renderer.scoop
1
package scoop.surface3
import scoop.graphics.*4
import scoop.graphics.native.RenderTarget5
import scoop.graphics.native.metal.MetalContext6
import scoop.window.*7
import scoop.window.native.*9
internal fun createRendererHost(): RendererHost {10
@Unsafe11
{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 error21
}22
}23
}25
private class MetalRendererHost(26
private val native: Ptr<Unit>,27
private val context: MetalContext28
) : RendererHost {29
private var closed = false30
public override fun create(window: Window, proxy: ApplicationProxy): WindowRenderer {31
if (closed) throw IllegalStateException(Some("renderer host is closed"))32
@Unsafe33
{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 renderer40
} catch (error: Throwable) {41
lease.close();42
throw error43
}44
}45
}46
public override fun close() {47
if (closed) return48
closed = true49
val failure = cleanupFailure(None) { context.close() }50
@Unsafe51
{52
metalHostClose(native)53
}54
throwFailure(failure)55
}56
}58
private class MetalWindowRenderer(59
private val window: Window,60
private val proxy: ApplicationProxy,61
private val lease: NativeWindowLease,62
private val context: MetalContext63
) : WindowRenderer {64
private var native: Ptr<Unit>? = None65
private var callback: ForeignCallback<(Ptr<Unit>, Int) -> Int>? = None66
private var metrics: WindowMetrics? = None67
private var wanted = false69
@Unsafe70
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.Reusable75
)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 error85
}86
}87
private fun notify(reason: Int): Int {88
if (native == None) return 089
val accepted = proxy90
.post { _ ->91
if (native != None && (reason == 2 || wanted) && window.state == WindowState.Open)92
window.requestRedraw()93
}94
return if (accepted) 1 else 095
}96
private fun checkCallback() {97
@Unsafe98
{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
@Unsafe111
{112
metalResult(113
metalSurfaceConfigure(114
pointer(),115
value.physicalSize.width,116
value.physicalSize.height,117
value.scaleFactor118
),119
"configure Metal surface"120
)121
}122
metrics = Some(value)123
}124
public override fun setFrameDemand(required: Boolean) {125
wanted = required126
when (native) {127
case Some(val value) ->128
@Unsafe129
{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.Deferred141
@Unsafe142
{143
val result = metalSurfaceAcquire(pointer())144
if (result.status < 0) throw GraphicsException("acquire Metal drawable")145
if (result.pointer == None) return FrameAcquisition.Deferred146
val frame = result.pointer!!147
try {148
val target = context149
.target(150
metalFrameTexture(frame),151
configuration.physicalSize.width,152
configuration.physicalSize.height,153
configuration.scaleFactor154
)155
return FrameAcquisition.Ready(MetalFrame(window, frame, target))156
} catch (error: Throwable) {157
metalFrameClose(frame);158
throw error159
}160
}161
}162
public override fun close() {163
val previous = native164
native = None165
wanted = false166
var failure: Throwable? = None167
when (previous) {168
case Some(val value) -> {169
failure = cleanupFailure(failure) {170
@Unsafe171
{172
metalResult(metalSurfaceClose(value), "close Metal surface")173
}174
}175
}177
case None -> {}178
}179
@Unsafe180
{181
when (callback) {182
case Some(val value) -> {183
callback = None184
failure = cleanupFailure(failure) {185
@Unsafe186
{187
throwFailure(foreignCallbackFailure(value))188
}189
}190
releaseForeignCallback(value)191
}193
case None -> {}194
}195
}196
lease.close()197
throwFailure(failure)198
}199
}201
private class MetalFrame(202
private val window: Window,203
private val native: Ptr<Unit>,204
private val target: RenderTarget205
) : RenderFrame {206
private var submitted = false207
private var closed = false208
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 false218
@Unsafe219
{220
val current = metalFrameCurrent(native)221
if (current < 0) throw GraphicsException("check Metal frame configuration")222
if (current == 0) return false223
}224
target.submit()225
window.prePresentNotify()226
@Unsafe227
{228
val result = metalFramePresent(native)229
if (result < 0) throw GraphicsException("present Metal drawable")230
submitted = true231
return result == 1232
}233
}234
public override fun close() {235
if (closed) return236
closed = true237
var failure: Throwable? = None238
if (!submitted) failure = cleanupFailure(failure) { target.abort() }239
failure = cleanupFailure(failure) { target.close() }240
@Unsafe241
{242
metalFrameClose(native)243
}244
throwFailure(failure)245
}246
}248
private fun metalResult(result: Int, operation: String) {249
if (result != 1) throw GraphicsException(operation)250
}252
@CLayout253
private struct MetalAcquiredFrame(val pointer: Ptr<Unit>?, val status: Int)255
@Extern(name = "scoop_surface_metal_host_create")256
private fun metalHostCreate(): Ptr<Unit>?258
@Extern(name = "scoop_surface_metal_host_object")259
private fun metalHostObject(host: Ptr<Unit>, kind: Int): Ptr<Unit>261
@Extern(name = "scoop_surface_metal_host_close")262
private fun metalHostClose(host: Ptr<Unit>)264
@Extern(name = "scoop_surface_metal_surface_create")265
private 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")273
private fun metalSurfaceConfigure(surface: Ptr<Unit>, width: Int, height: Int, scale: Double): Int275
@Extern(name = "scoop_surface_metal_surface_demand")276
private fun metalSurfaceDemand(surface: Ptr<Unit>, required: Boolean): Int278
@Extern(name = "scoop_surface_metal_surface_acquire")279
private fun metalSurfaceAcquire(surface: Ptr<Unit>): MetalAcquiredFrame281
@Extern(name = "scoop_surface_metal_surface_close")282
private fun metalSurfaceClose(surface: Ptr<Unit>): Int284
@Extern(name = "scoop_surface_metal_frame_texture")285
private fun metalFrameTexture(frame: Ptr<Unit>): Ptr<Unit>287
@Extern(name = "scoop_surface_metal_frame_current")288
private fun metalFrameCurrent(frame: Ptr<Unit>): Int290
@Extern(name = "scoop_surface_metal_frame_present")291
private fun metalFramePresent(frame: Ptr<Unit>): Int293
@Extern(name = "scoop_surface_metal_frame_close")294
private fun metalFrameClose(frame: Ptr<Unit>)