-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from JetBrains/raster
Raster
- Loading branch information
Showing
9 changed files
with
308 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ open class SkiaPanel: JLayeredPane { | |
} | ||
|
||
override fun removeNotify() { | ||
layer.dispose() | ||
super.removeNotify() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package org.jetbrains.skiko | ||
|
||
enum class GraphicsApi { | ||
UNKNOWN, OPENGL, VULKAN, METAL | ||
UNKNOWN, SOFTWARE, OPENGL, DIRECT3D, VULKAN, METAL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
skiko/src/jvmMain/kotlin/org/jetbrains/skiko/context/ContextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jetbrains.skiko.context | ||
|
||
import org.jetbrains.skija.BackendRenderTarget | ||
import org.jetbrains.skija.Canvas | ||
import org.jetbrains.skija.DirectContext | ||
import org.jetbrains.skija.Picture | ||
import org.jetbrains.skija.Surface | ||
import org.jetbrains.skiko.GraphicsApi | ||
import org.jetbrains.skiko.HardwareLayer | ||
import org.jetbrains.skiko.hostOs | ||
import org.jetbrains.skiko.OS | ||
|
||
internal val renderApi: GraphicsApi by lazy { | ||
val environment = System.getenv("SKIKO_RENDER_API") | ||
val property = System.getProperty("skiko.renderApi") | ||
if (environment != null) { | ||
parseRenderApi(environment) | ||
} else { | ||
parseRenderApi(property) | ||
} | ||
} | ||
|
||
private fun parseRenderApi(text: String?): GraphicsApi { | ||
when(text) { | ||
"SOFTWARE" -> return GraphicsApi.SOFTWARE | ||
"OPENGL" -> return GraphicsApi.OPENGL | ||
else -> return GraphicsApi.OPENGL | ||
} | ||
} | ||
|
||
internal fun createContextHandler(layer: HardwareLayer): ContextHandler { | ||
return when (renderApi) { | ||
GraphicsApi.SOFTWARE -> SoftwareContextHandler(layer) | ||
GraphicsApi.OPENGL -> OpenGLContextHandler(layer) | ||
else -> TODO("Unsupported yet") | ||
} | ||
} | ||
|
||
internal abstract class ContextHandler(val layer: HardwareLayer) { | ||
open val bleachConstant = if (hostOs == OS.MacOS) 0 else -1 | ||
var context: DirectContext? = null | ||
var renderTarget: BackendRenderTarget? = null | ||
var surface: Surface? = null | ||
var canvas: Canvas? = null | ||
|
||
abstract fun initContext(): Boolean | ||
|
||
abstract fun initCanvas() | ||
|
||
fun clearCanvas() { | ||
canvas?.clear(bleachConstant) | ||
} | ||
|
||
open fun drawOnCanvas(picture: Picture) { | ||
canvas?.drawPicture(picture) | ||
} | ||
|
||
open fun flush() { | ||
context?.flush() | ||
} | ||
|
||
fun dispose() { | ||
surface?.close() | ||
renderTarget?.close() | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
skiko/src/jvmMain/kotlin/org/jetbrains/skiko/context/MetalContextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.jetbrains.skiko.context | ||
|
||
import org.jetbrains.skija.ColorSpace | ||
import org.jetbrains.skija.Surface | ||
import org.jetbrains.skija.SurfaceColorFormat | ||
import org.jetbrains.skija.SurfaceOrigin | ||
import org.jetbrains.skiko.GraphicsApi | ||
import org.jetbrains.skiko.HardwareLayer | ||
import org.jetbrains.skiko.makeMetalContext | ||
import org.jetbrains.skiko.makeMetalRenderTarget | ||
|
||
internal class MetalContextHandler(layer: HardwareLayer) : ContextHandler(layer) { | ||
override fun initContext(): Boolean { | ||
try { | ||
if (context == null) { | ||
context = makeMetalContext() | ||
} | ||
} catch (e: Exception) { | ||
println("Failed to create Skia Metal context!") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
override fun initCanvas() { | ||
dispose() | ||
|
||
val scale = layer.contentScale | ||
val w = (layer.width * scale).toInt().coerceAtLeast(0) | ||
val h = (layer.height * scale).toInt().coerceAtLeast(0) | ||
|
||
renderTarget = makeMetalRenderTarget(w, h, 0) | ||
|
||
surface = Surface.makeFromBackendRenderTarget( | ||
context!!, | ||
renderTarget!!, | ||
SurfaceOrigin.BOTTOM_LEFT, | ||
SurfaceColorFormat.RGBA_8888, | ||
ColorSpace.getSRGB() | ||
) | ||
|
||
canvas = surface!!.canvas | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
skiko/src/jvmMain/kotlin/org/jetbrains/skiko/context/OpenGLContextHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.jetbrains.skiko.context | ||
|
||
import org.jetbrains.skija.ColorSpace | ||
import org.jetbrains.skija.FramebufferFormat | ||
import org.jetbrains.skija.Picture | ||
import org.jetbrains.skija.Surface | ||
import org.jetbrains.skija.SurfaceColorFormat | ||
import org.jetbrains.skija.SurfaceOrigin | ||
import org.jetbrains.skiko.HardwareLayer | ||
import org.jetbrains.skiko.makeGLContext | ||
import org.jetbrains.skiko.makeGLRenderTarget | ||
import org.jetbrains.skiko.OpenGLApi | ||
|
||
internal class OpenGLContextHandler(layer: HardwareLayer) : ContextHandler(layer) { | ||
override fun initContext(): Boolean { | ||
try { | ||
if (context == null) { | ||
context = makeGLContext() | ||
} | ||
} catch (e: Exception) { | ||
println("Failed to create Skia OpenGL context!") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
override fun initCanvas() { | ||
dispose() | ||
|
||
val scale = layer.contentScale | ||
val w = (layer.width * scale).toInt().coerceAtLeast(0) | ||
val h = (layer.height * scale).toInt().coerceAtLeast(0) | ||
|
||
val gl = OpenGLApi.instance | ||
val fbId = gl.glGetIntegerv(gl.GL_DRAW_FRAMEBUFFER_BINDING) | ||
renderTarget = makeGLRenderTarget( | ||
w, | ||
h, | ||
0, | ||
8, | ||
fbId, | ||
FramebufferFormat.GR_GL_RGBA8 | ||
) | ||
|
||
surface = Surface.makeFromBackendRenderTarget( | ||
context!!, | ||
renderTarget!!, | ||
SurfaceOrigin.BOTTOM_LEFT, | ||
SurfaceColorFormat.RGBA_8888, | ||
ColorSpace.getSRGB() | ||
) | ||
|
||
canvas = surface!!.canvas | ||
} | ||
} |
Oops, something went wrong.