Commit 2e73540a authored by Nikolay Igotti's avatar Nikolay Igotti

Further simplification.

parent a69f27f2
package SkijaInjectSample package SkijaInjectSample
import org.jetbrains.skiko.OpenGLApi
import org.jetbrains.skiko.Engine
import org.jetbrains.skiko.SkiaWindow import org.jetbrains.skiko.SkiaWindow
import java.awt.event.MouseEvent import java.awt.event.MouseEvent
...@@ -18,7 +16,6 @@ fun main(args: Array<String>) { ...@@ -18,7 +16,6 @@ fun main(args: Array<String>) {
} }
fun createWindow(title: String) { fun createWindow(title: String) {
val engine: Engine = Engine.get()
var mouseX = 0 var mouseX = 0
var mouseY = 0 var mouseY = 0
...@@ -37,7 +34,7 @@ fun createWindow(title: String) { ...@@ -37,7 +34,7 @@ fun createWindow(title: String) {
override fun mouseMoved(event: MouseEvent) { override fun mouseMoved(event: MouseEvent) {
mouseX = event.x mouseX = event.x
mouseY = event.y mouseY = event.y
engine.render(window.layer) window.display()
} }
}) })
......
package org.jetbrains.skiko
class Engine private constructor() {
private var api = GraphicsApi.UNKNOWN
fun currentApi(): GraphicsApi {
return api
}
/**
* GAG - At the moment always initializes OpenGL.
* @param osType The current OS type of the device.
*/
private fun initSuitableGraphicsApi(osType: OSType) {
when {
osType === OSType.MAC_OS -> {
OpenGLApi.get() // for Metal
instance.api = GraphicsApi.OPENGL
}
osType === OSType.LINUX -> {
OpenGLApi.get() // for Vulkan and OpenGL
instance.api = GraphicsApi.OPENGL
}
osType === OSType.WINDOWS -> {
OpenGLApi.get() // for Vulkan and OpenGL
instance.api = GraphicsApi.OPENGL
}
else -> {
OpenGLApi.get() // default
instance.api = GraphicsApi.OPENGL
}
}
}
fun render(drawable: Drawable) {
drawable.updateLayer()
drawable.redrawLayer()
}
companion object {
private lateinit var instance: Engine
fun get(): Engine {
if (!this::instance.isInitialized) {
instance = Engine()
instance.initSuitableGraphicsApi(OSType.getCurrent())
}
return instance
}
}
}
...@@ -5,9 +5,12 @@ import java.awt.Canvas ...@@ -5,9 +5,12 @@ import java.awt.Canvas
open class HardwareLayer : Canvas(), Drawable { open class HardwareLayer : Canvas(), Drawable {
override fun paint(g: Graphics) { override fun paint(g: Graphics) {
Engine.get().render(this) display()
}
open fun display() {
this.updateLayer()
this.redrawLayer()
} }
open fun draw() {} open fun draw() {}
external override fun redrawLayer() external override fun redrawLayer()
external override fun updateLayer() external override fun updateLayer()
......
...@@ -14,7 +14,7 @@ object Library { ...@@ -14,7 +14,7 @@ object Library {
fun load(resourcePath: String, name: String) { fun load(resourcePath: String, name: String) {
if (loaded) return if (loaded) return
val libFileName = "$libPrefix$name$libExtension" val libFileName = System.mapLibraryName(name)
val url = Library::class.java.getResource(resourcePath + libFileName) val url = Library::class.java.getResource(resourcePath + libFileName)
val libFile = when { val libFile = when {
url == null -> error("Library $libFileName is not found in $resourcePath") url == null -> error("Library $libFileName is not found in $resourcePath")
...@@ -28,33 +28,7 @@ object Library { ...@@ -28,33 +28,7 @@ object Library {
} }
} }
} }
//println("Loading $url from ${libFile.absolutePath}")
System.load(libFile.absolutePath) System.load(libFile.absolutePath)
loaded = true loaded = true
} }
private val libPrefix: String
get() = if (currentOS == OS.Windows) "" else "lib"
private val libExtension: String
get() = when (currentOS) {
OS.Mac -> ".dylib"
OS.Linux -> ".so"
OS.Windows -> ".dll"
}
private val currentOS by lazy {
val osName = System.getProperty("os.name") ?: error("Unknown OS: 'os.name' is null")
val os = osName.toLowerCase(Locale.ROOT)
when {
os.contains("mac") || os.contains("darwin") -> OS.Mac
os.contains("win") -> OS.Windows
os.contains("nux") -> OS.Linux
else -> error("Unknown OS: $osName")
}
}
private enum class OS {
Linux, Mac, Windows
}
} }
\ No newline at end of file
...@@ -4,25 +4,16 @@ enum class OSType { ...@@ -4,25 +4,16 @@ enum class OSType {
MAC_OS, LINUX, WINDOWS, UNKNOWN; MAC_OS, LINUX, WINDOWS, UNKNOWN;
companion object { companion object {
private lateinit var currentOSType: OSType val currentOs = defineOsType()
fun getCurrent(): OSType {
if (!this::currentOSType.isInitialized) {
currentOSType = defineOsType()
}
return currentOSType
}
private fun defineOsType(): OSType { private fun defineOsType(): OSType {
val osName = System.getProperty("os.name").toLowerCase() val osName = System.getProperty("os.name").toLowerCase()
if (isWindows(osName)) { return when {
return WINDOWS isWindows(osName) -> WINDOWS
} isMac(osName) -> MAC_OS
if (isMac(osName)) { isUnix(osName) -> LINUX
return MAC_OS else -> UNKNOWN
} }
return if (isUnix(osName)) {
LINUX
} else UNKNOWN
} }
private fun isWindows(name: String): Boolean { private fun isWindows(name: String): Boolean {
......
...@@ -22,12 +22,6 @@ class OpenGLApi private constructor() { ...@@ -22,12 +22,6 @@ class OpenGLApi private constructor() {
external fun glGetIntegerv(pname: Int): Int external fun glGetIntegerv(pname: Int): Int
companion object { companion object {
private lateinit var instance: OpenGLApi val instance = OpenGLApi()
fun get(): OpenGLApi {
if (!this::instance.isInitialized) {
instance = OpenGLApi()
}
return instance
}
} }
} }
\ No newline at end of file
...@@ -57,7 +57,7 @@ open class SkiaLayer : HardwareLayer() { ...@@ -57,7 +57,7 @@ open class SkiaLayer : HardwareLayer() {
} }
skijaState.apply { skijaState.apply {
val gl = OpenGLApi.get() val gl = OpenGLApi.instance
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f) gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glClear(gl.GL_COLOR_BUFFER_BIT)
...@@ -72,7 +72,7 @@ open class SkiaLayer : HardwareLayer() { ...@@ -72,7 +72,7 @@ open class SkiaLayer : HardwareLayer() {
private fun initSkija() { private fun initSkija() {
val dpi = contentScale val dpi = contentScale
skijaState.clear() skijaState.clear()
val gl: OpenGLApi = OpenGLApi.get() val gl: OpenGLApi = OpenGLApi.instance
val fbId = gl.glGetIntegerv(gl.GL_DRAW_FRAMEBUFFER_BINDING) val fbId = gl.glGetIntegerv(gl.GL_DRAW_FRAMEBUFFER_BINDING)
skijaState.renderTarget = BackendRenderTarget.makeGL( skijaState.renderTarget = BackendRenderTarget.makeGL(
(width * dpi).toInt(), (width * dpi).toInt(),
...@@ -111,8 +111,12 @@ open class SkiaWindow : JFrame() { ...@@ -111,8 +111,12 @@ open class SkiaWindow : JFrame() {
override fun componentResized(e: ComponentEvent) { override fun componentResized(e: ComponentEvent) {
layer.reinit() layer.reinit()
layer.setSize(width, height) layer.setSize(width, height)
Engine.get().render(layer) display()
} }
}) })
} }
fun display() {
layer.display()
}
} }
...@@ -46,11 +46,14 @@ JavaVM *jvm; ...@@ -46,11 +46,14 @@ JavaVM *jvm;
CGLSetCurrentContext(ctx); CGLSetCurrentContext(ctx);
if (self.jvm != NULL) { if (self.jvm != NULL) {
// TODO: cache wndClass and drawMethod.
JNIEnv *env; JNIEnv *env;
(*self.jvm)->AttachCurrentThread(self.jvm, (void **)&env, NULL); (*self.jvm)->AttachCurrentThread(self.jvm, (void **)&env, NULL);
jclass wndClass = (*env)->GetObjectClass(env, self.windowRef); static jclass wndClass = NULL;
jmethodID drawMethod = (*env)->GetMethodID(env, wndClass, "draw", "()V"); if (!wndClass) wndClass = (*env)->GetObjectClass(env, self.windowRef);
static jmethodID drawMethod = NULL;
if (!drawMethod) drawMethod = (*env)->GetMethodID(env, wndClass, "draw", "()V");
if (NULL == drawMethod) { if (NULL == drawMethod) {
NSLog(@"The method Window.draw() not found!"); NSLog(@"The method Window.draw() not found!");
return; return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment