Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectXOffscreenContext to allow drawing into offscreen texture #1016

Merged
merged 1 commit into from
Jan 27, 2025

Conversation

igordmn
Copy link
Collaborator

@igordmn igordmn commented Jan 27, 2025

Needed to run benchmarks without vsync interference.

The code extracted from Direct3DSwingRedrawer with minimal changes.

  • implementation for other API will be added when it is needed (Metal is considered later)
  • there are no plans to make commonMain abstraction in skiko as it is not needed at the moment

Testing

  • manually in benchmarks with this code:
@OptIn(ExperimentalSkikoApi::class)
class DirectXGraphicsContext() : DesktopGraphicsContext {
    private val context = DirectXOffscreenContext()
    private var texture: DirectXOffscreenContext.Texture? = null

    override fun surface(width: Int, height: Int): Surface {
        texture?.close()
        texture = context.Texture(width, height)
        return Surface.makeFromBackendRenderTarget(
            context.directContext,
            texture!!.backendRenderTarget,
            SurfaceOrigin.TOP_LEFT,
            SurfaceColorFormat.BGRA_8888,
            ColorSpace.sRGB,
            SurfaceProps(pixelGeometry = PixelGeometry.UNKNOWN)
        ) ?: throw IllegalStateException("Can't create Surface")
    }

    override suspend fun awaitGPUCompletion() {
        texture?.waitForCompletion()
    }

    override fun close() {
        texture?.close()
        context.close()
    }
}

interface DesktopGraphicsContext : GraphicsContext, AutoCloseable
  • new test for SkiaSwingLayer
  • new test for DirectXOffscreenContext

@igordmn igordmn force-pushed the igor.demin/DirectXOffscreenContext branch 3 times, most recently from c20da90 to 823aecb Compare January 27, 2025 01:24
Needed to run benchmarks without vsync interference

The code extracted from Direct3DSwingRedrawer with minimal changes

## Testing
- manually in benchmarks with this code:
```
@OptIn(ExperimentalSkikoApi::class)
class DirectXGraphicsContext() : DesktopGraphicsContext {
    private val context = DirectXOffscreenContext()
    private var texture: DirectXOffscreenContext.Texture? = null

    override fun surface(width: Int, height: Int): Surface {
        texture?.close()
        texture = context.Texture(width, height)
        return Surface.makeFromBackendRenderTarget(
            context.directContext,
            texture!!.backendRenderTarget,
            SurfaceOrigin.TOP_LEFT,
            SurfaceColorFormat.BGRA_8888,
            ColorSpace.sRGB,
            SurfaceProps(pixelGeometry = PixelGeometry.UNKNOWN)
        ) ?: throw IllegalStateException("Can't create Surface")
    }

    override suspend fun awaitGPUCompletion() {
        texture?.waitForCompletion()
    }

    override fun close() {
        texture?.close()
        context.close()
    }
}

interface DesktopGraphicsContext : GraphicsContext, AutoCloseable
```
- new test for SkiaSwingLayer
- new test for DirectXOffscreenContext
@@ -28,15 +28,14 @@ open class SkiaSwingLayer(
renderDelegate: SkikoRenderDelegate,
analytics: SkiaLayerAnalytics = SkiaLayerAnalytics.Empty,
externalAccessibleFactory: ((Component) -> Accessible)? = null,
private val properties: SkiaLayerProperties = SkiaLayerProperties()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems unrelated + breaking compatibility for no reason. Can we avoid it in this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed in the test - to pass the graphic API:
https://github.com/JetBrains/skiko/pull/1016/files#diff-f09f8e25b6e613b4acc42736872562fec343ba85cd033a84464e2ee426a1cbd7R188

As for compatibility, we don't support it in Skiko

@igordmn igordmn merged commit 1af72c1 into master Jan 27, 2025
6 checks passed
@igordmn igordmn deleted the igor.demin/DirectXOffscreenContext branch January 27, 2025 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants