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

Swing interop: off-screen rendering on graphics #601

Merged
merged 19 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compose/desktop/desktop/samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ task runSwing(type: JavaExec) {
compilation.runtimeDependencyFiles
}

task runSwingOffscreenRendering(type: JavaExec) {
dependsOn(":compose:desktop:desktop:jar")
main = "androidx.compose.desktop.examples.swingexample.Main_jvmKt"
def compilation = kotlin.jvm().compilations["main"]
classpath =
compilation.output.allOutputs +
compilation.runtimeDependencyFiles
jvmArgs("-Dcompose.swing.render.on.graphics=true")
}

task runMouseClicks(type: JavaExec) {
dependsOn(":compose:desktop:desktop:jar")
main = "androidx.compose.desktop.examples.mouseclicks.Main_jvmKt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ fun SwingComposeWindow() {
size = IntSize(40, 40),
action = {
panel.remove(composePanelBottom)
panel.revalidate()
panel.repaint()
}
),
BorderLayout.SOUTH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import org.jetbrains.skiko.*
*
* Inheritors should call [attachComposeToComponent], so events that came to [component] will be transferred to [ComposeScene]
*/
internal abstract class ComposeLayer {
internal abstract class ComposeBridge {
private var isDisposed = false

val sceneAccessible = ComposeSceneAccessible(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.ui.window.WindowExceptionHandler
import java.awt.*
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import javax.swing.JComponent
import javax.swing.JLayeredPane
import javax.swing.SwingUtilities.isEventDispatchThread
import org.jetbrains.skiko.ClipComponent
Expand Down Expand Up @@ -74,7 +73,7 @@ class ComposePanel @ExperimentalComposeUiApi constructor(
private val _focusListeners = mutableSetOf<FocusListener?>()
private var _isFocusable = true
private var _isRequestFocusEnabled = false
private var layer: ComposeLayer? = null
private var layer: ComposeBridge? = null
Walingar marked this conversation as resolved.
Show resolved Hide resolved
private val clipMap = mutableMapOf<Component, ClipComponent>()
private var content: (@Composable () -> Unit)? = null

Expand Down Expand Up @@ -151,12 +150,12 @@ class ComposePanel @ExperimentalComposeUiApi constructor(
}

@OptIn(ExperimentalComposeUiApi::class)
private fun createComposeLayer(): ComposeLayer {
private fun createComposeLayer(): ComposeBridge {
val renderOnGraphics = System.getProperty("compose.swing.render.on.graphics").toBoolean()
val layer: ComposeLayer = if (renderOnGraphics) {
ComposeSwingLayer(skiaLayerAnalytics)
val layer: ComposeBridge = if (renderOnGraphics) {
SwingComposeBridge(skiaLayerAnalytics)
} else {
ComposeWindowLayer(skiaLayerAnalytics)
WindowComposeBridge(skiaLayerAnalytics)
}
return layer.apply {
scene.releaseFocus()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,53 @@ import org.jetbrains.skiko.swing.SkiaSwingLayer
* Provides [component] that can be used as a Swing component.
* Content set in [setContent] will be drawn on this [component].
*
* [ComposeSwingLayer] provides smooth integration with Swing, so z-ordering, double-buffering etc. from Swing will be taken into account.
* [SwingComposeBridge] provides smooth integration with Swing, so z-ordering, double-buffering etc. from Swing will be taken into account.
*
* However, if smooth interop with Swing is not needed, consider using [androidx.compose.ui.awt.ComposeWindowLayer]
* However, if smooth interop with Swing is not needed, consider using [androidx.compose.ui.awt.WindowComposeBridge]
*/
internal class ComposeSwingLayer(
internal class SwingComposeBridge(
private val skiaLayerAnalytics: SkiaLayerAnalytics
) : ComposeLayer() {
private val _component = ComposeSwingSkiaLayer()
) : ComposeBridge() {
/**
* See also backendLayer for standalone Compose in [androidx.compose.ui.awt.WindowComposeBridge]
*/
@OptIn(ExperimentalSkikoApi::class)
private val _component =
Walingar marked this conversation as resolved.
Show resolved Hide resolved
object : SkiaSwingLayer(skikoView = skikoView, analytics = skiaLayerAnalytics) {

override fun addNotify() {
super.addNotify()
resetSceneDensity()
initContent()
updateSceneSize()
setParentWindow(SwingUtilities.getWindowAncestor(this))
}

override fun removeNotify() {
setParentWindow(window = null)
super.removeNotify()
}

override fun paint(g: Graphics) {
resetSceneDensity()
super.paint(g)
}

override fun getInputMethodRequests() = currentInputMethodRequests

override fun doLayout() {
super.doLayout()
updateSceneSize()
}

override fun getPreferredSize(): Dimension {
return if (isPreferredSizeSet) super.getPreferredSize() else sceneDimension
}

override fun getAccessibleContext(): AccessibleContext? {
return sceneAccessible.accessibleContext
}
}

@OptIn(ExperimentalSkikoApi::class)
override val component: SkiaSwingLayer get() = _component
Expand Down Expand Up @@ -71,45 +110,4 @@ internal class ComposeSwingLayer(
override fun disposeComponentLayer() {
_component.dispose()
}

/**
* See also backendLayer for standalone compose [androidx.compose.ui.awt.ComposeWindowLayer.ComposeWindowSkiaLayer]
*/
@OptIn(ExperimentalSkikoApi::class)
private inner class ComposeSwingSkiaLayer :
SkiaSwingLayer(skikoView = skikoView, analytics = skiaLayerAnalytics) {

override fun addNotify() {
super.addNotify()
resetSceneDensity()
initContent()
updateSceneSize()
setParentWindow(SwingUtilities.getWindowAncestor(this))
}

override fun removeNotify() {
setParentWindow(window = null)
super.removeNotify()
}

override fun paint(g: Graphics) {
resetSceneDensity()
super.paint(g)
}

override fun getInputMethodRequests() = currentInputMethodRequests

override fun doLayout() {
super.doLayout()
updateSceneSize()
}

override fun getPreferredSize(): Dimension {
return if (isPreferredSizeSet) super.getPreferredSize() else sceneDimension
}

override fun getAccessibleContext(): AccessibleContext? {
return sceneAccessible.accessibleContext
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class ComposeWindowDelegate(
// (see https://github.com/JetBrains/compose-jb/issues/1688),
// so we nullify layer on dispose, to prevent keeping
// big objects in memory (like the whole LayoutNode tree of the window)
private var _layer: ComposeWindowLayer? = ComposeWindowLayer(skiaLayerAnalytics)
private var _layer: WindowComposeBridge? = WindowComposeBridge(skiaLayerAnalytics)
private val layer
get() = requireNotNull(_layer) {
"ComposeLayer is disposed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,50 @@ import org.jetbrains.skiko.SkiaLayerAnalytics
/**
* Provides a heavyweight AWT [component] used to render content (from [setContent]) on-screen with Skia.
*
* If smooth interop with Swing is needed, consider using [androidx.compose.ui.awt.ComposeWindowLayer]
* If smooth interop with Swing is needed, consider using [androidx.compose.ui.awt.SwingComposeBridge]
*/
internal class ComposeWindowLayer(
internal class WindowComposeBridge(
private val skiaLayerAnalytics: SkiaLayerAnalytics
) : ComposeLayer() {
private val _component = ComposeWindowSkiaLayer()
) : ComposeBridge() {
/**
* See also backend layer for swing interop in [androidx.compose.ui.awt.SwingComposeBridge]
*/
private val _component =
object : SkiaLayer(
externalAccessibleFactory = { sceneAccessible },
analytics = skiaLayerAnalytics
), Accessible {

override fun addNotify() {
super.addNotify()
resetSceneDensity()
initContent()
updateSceneSize()
setParentWindow(SwingUtilities.getWindowAncestor(this))
}

override fun removeNotify() {
setParentWindow(null)
super.removeNotify()
}

override fun paint(g: Graphics) {
resetSceneDensity()
super.paint(g)
}

override fun getInputMethodRequests() = currentInputMethodRequests

override fun doLayout() {
super.doLayout()
updateSceneSize()
}

override fun getPreferredSize(): Dimension {
return if (isPreferredSizeSet) super.getPreferredSize() else sceneDimension
}
}

override val component: SkiaLayer get() = _component

override val renderApi: GraphicsApi
Expand Down Expand Up @@ -62,44 +100,4 @@ internal class ComposeWindowLayer(
override fun disposeComponentLayer() {
_component.dispose()
}

/**
* See also backend layer for swing interop: [androidx.compose.ui.awt.ComposeSwingLayer.ComposeSwingSkiaLayer]
*/
private inner class ComposeWindowSkiaLayer :
SkiaLayer(
externalAccessibleFactory = { sceneAccessible },
analytics = skiaLayerAnalytics
),
Accessible {

override fun addNotify() {
super.addNotify()
resetSceneDensity()
initContent()
updateSceneSize()
setParentWindow(SwingUtilities.getWindowAncestor(this))
}

override fun removeNotify() {
setParentWindow(null)
super.removeNotify()
}

override fun paint(g: Graphics) {
resetSceneDensity()
super.paint(g)
}

override fun getInputMethodRequests() = currentInputMethodRequests

override fun doLayout() {
super.doLayout()
updateSceneSize()
}

override fun getPreferredSize(): Dimension {
return if (isPreferredSizeSet) super.getPreferredSize() else sceneDimension
}
}
}