|
| 1 | +import org.jetbrains.skia.ColorSpace |
| 2 | +import org.jetbrains.skia.PixelGeometry |
| 3 | +import org.jetbrains.skia.Surface |
| 4 | +import org.jetbrains.skia.SurfaceColorFormat |
| 5 | +import org.jetbrains.skia.SurfaceOrigin |
| 6 | +import org.jetbrains.skia.SurfaceProps |
| 7 | +import org.jetbrains.skiko.ExperimentalSkikoApi |
| 8 | +import org.jetbrains.skiko.graphicapi.DirectXOffscreenContext |
| 9 | +import org.jetbrains.skiko.hostOs |
| 10 | + |
| 11 | +@OptIn(ExperimentalSkikoApi::class) |
| 12 | +fun graphicsContext(): GraphicsContext? = when { |
| 13 | + hostOs.isWindows -> DirectXGraphicsContext() |
| 14 | + else -> { |
| 15 | + println("Unsupported desktop host OS: $hostOs. Using non-GPU graphic context") |
| 16 | + null |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +@OptIn(ExperimentalSkikoApi::class) |
| 21 | +class DirectXGraphicsContext() : GraphicsContext { |
| 22 | + // Note: we don't close `context` and `texture` after using, |
| 23 | + // because it is created once in the main function |
| 24 | + private val context = DirectXOffscreenContext() |
| 25 | + private var texture: DirectXOffscreenContext.Texture? = null |
| 26 | + |
| 27 | + override fun surface(width: Int, height: Int): Surface { |
| 28 | + texture?.close() |
| 29 | + texture = context.Texture(width, height) |
| 30 | + return Surface.makeFromBackendRenderTarget( |
| 31 | + context.directContext, |
| 32 | + texture!!.backendRenderTarget, |
| 33 | + SurfaceOrigin.TOP_LEFT, |
| 34 | + SurfaceColorFormat.BGRA_8888, |
| 35 | + ColorSpace.sRGB, |
| 36 | + SurfaceProps(pixelGeometry = PixelGeometry.UNKNOWN) |
| 37 | + ) ?: throw IllegalStateException("Can't create Surface") |
| 38 | + } |
| 39 | + |
| 40 | + override suspend fun awaitGPUCompletion() { |
| 41 | + texture?.waitForCompletion() |
| 42 | + } |
| 43 | +} |
0 commit comments