Skip to content

Commit a701208

Browse files
committed
Support GPU on Windows
1 parent d5c93f7 commit a701208

File tree

7 files changed

+65
-7
lines changed

7 files changed

+65
-7
lines changed

benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/Benchmarks.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ suspend fun runBenchmark(
154154
}
155155
}
156156

157-
suspend fun runBenchmarks(
157+
private suspend fun runBenchmarks(
158158
width: Int = 1920,
159159
height: Int = 1080,
160160
targetFps: Int = 120,
161-
graphicsContext: GraphicsContext? = null
161+
graphicsContext: GraphicsContext?
162162
) {
163163
println()
164164
println("Running emulating $targetFps FPS")
@@ -168,4 +168,14 @@ suspend fun runBenchmarks(
168168
runBenchmark("VisualEffects", width, height, targetFps, 1000, graphicsContext) { NYContent(width, height) }
169169
runBenchmark("LazyList", width, height, targetFps, 1000, graphicsContext) { MainUiNoImageUseModel()}
170170
runBenchmark("Example1", width, height, targetFps, 1000, graphicsContext) { Example1() }
171-
}
171+
}
172+
173+
suspend fun benchmarksMain(
174+
graphicsContext: GraphicsContext? = null
175+
) {
176+
try {
177+
runBenchmarks(graphicsContext = graphicsContext)
178+
} finally {
179+
graphicsContext?.close()
180+
}
181+
}

benchmarks/multiplatform/benchmarks/src/commonMain/kotlin/MeasureComposable.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ interface GraphicsContext {
1717
fun surface(width: Int, height: Int): Surface
1818

1919
suspend fun awaitGPUCompletion()
20+
21+
fun close() = Unit
2022
}
2123

2224
expect fun runGC()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
private val context = DirectXOffscreenContext()
23+
private var texture: DirectXOffscreenContext.Texture? = null
24+
25+
override fun surface(width: Int, height: Int): Surface {
26+
texture?.close()
27+
texture = context.Texture(width, height)
28+
return Surface.makeFromBackendRenderTarget(
29+
context.directContext,
30+
texture!!.backendRenderTarget,
31+
SurfaceOrigin.TOP_LEFT,
32+
SurfaceColorFormat.BGRA_8888,
33+
ColorSpace.sRGB,
34+
SurfaceProps(pixelGeometry = PixelGeometry.UNKNOWN)
35+
) ?: throw IllegalStateException("Can't create Surface")
36+
}
37+
38+
override suspend fun awaitGPUCompletion() {
39+
texture?.waitForCompletion()
40+
}
41+
42+
override fun close() {
43+
texture?.close()
44+
context.close()
45+
}
46+
}

benchmarks/multiplatform/benchmarks/src/desktopMain/kotlin/main.desktop.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ import kotlinx.coroutines.runBlocking
88

99
fun main(args: Array<String>) {
1010
Args.parseArgs(args)
11-
runBlocking(Dispatchers.Main) { runBenchmarks() }
11+
runBlocking(Dispatchers.Main) { benchmarksMain(graphicsContext = graphicsContext()) }
1212
}

benchmarks/multiplatform/benchmarks/src/iosMain/kotlin/main.ios.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import kotlinx.coroutines.runBlocking
77

88
fun main(args : List<String>) {
99
Args.parseArgs(args.toTypedArray())
10-
runBlocking { runBenchmarks(graphicsContext = graphicsContext()) }
10+
runBlocking { benchmarksMain(graphicsContext = graphicsContext()) }
1111
}

benchmarks/multiplatform/benchmarks/src/macosMain/kotlin/main.macos.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import kotlinx.coroutines.runBlocking
66

77
fun main(args : Array<String>) {
88
Args.parseArgs(args)
9-
runBlocking { runBenchmarks(graphicsContext = graphicsContext()) }
9+
runBlocking { benchmarksMain(graphicsContext = graphicsContext()) }
1010
}

benchmarks/multiplatform/benchmarks/src/wasmJsMain/kotlin/main.wasmJs.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fun main() {
99
val args = generateSequence { urlParams.get("arg${i++}") }.toList().toTypedArray()
1010
Args.parseArgs(args)
1111
MainScope().launch {
12-
runBenchmarks()
12+
benchmarksMain()
1313
println("Completed!")
1414
}
1515
}

0 commit comments

Comments
 (0)