Skip to content

Commit 6a7123e

Browse files
igordmnWojciech Liberda
authored andcommitted
Benchmarks. Support GPU on Windows (JetBrains#5216)
Partially fixes https://youtrack.jetbrains.com/issue/CMP-7475/Benchmarks.-Add-ability-to-test-GPU-on-desktop-JVM (for Windows) ## Release notes N/A
1 parent e65ec14 commit 6a7123e

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ fun main(args: Array<String>) {
1313
// Start the benchmark server to receive results from browsers
1414
BenchmarksSaveServer.start()
1515
} else {
16-
runBlocking(Dispatchers.Main) { runBenchmarks() }
16+
runBlocking(Dispatchers.Main) { runBenchmarks(graphicsContext = graphicsContext()) }
1717
}
1818
}

0 commit comments

Comments
 (0)