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

Further simplification.

parent a69f27f2
package SkijaInjectSample
import org.jetbrains.skiko.OpenGLApi
import org.jetbrains.skiko.Engine
import org.jetbrains.skiko.SkiaWindow
import java.awt.event.MouseEvent
......@@ -18,7 +16,6 @@ fun main(args: Array<String>) {
}
fun createWindow(title: String) {
val engine: Engine = Engine.get()
var mouseX = 0
var mouseY = 0
......@@ -37,7 +34,7 @@ fun createWindow(title: String) {
override fun mouseMoved(event: MouseEvent) {
mouseX = event.x
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
open class HardwareLayer : Canvas(), Drawable {
override fun paint(g: Graphics) {
Engine.get().render(this)
display()
}
open fun display() {
this.updateLayer()
this.redrawLayer()
}
open fun draw() {}
external override fun redrawLayer()
external override fun updateLayer()
......
......@@ -14,7 +14,7 @@ object Library {
fun load(resourcePath: String, name: String) {
if (loaded) return
val libFileName = "$libPrefix$name$libExtension"
val libFileName = System.mapLibraryName(name)
val url = Library::class.java.getResource(resourcePath + libFileName)
val libFile = when {
url == null -> error("Library $libFileName is not found in $resourcePath")
......@@ -28,33 +28,7 @@ object Library {
}
}
}
//println("Loading $url from ${libFile.absolutePath}")
System.load(libFile.absolutePath)
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 {
MAC_OS, LINUX, WINDOWS, UNKNOWN;
companion object {
private lateinit var currentOSType: OSType
fun getCurrent(): OSType {
if (!this::currentOSType.isInitialized) {
currentOSType = defineOsType()
}
return currentOSType
}
val currentOs = defineOsType()
private fun defineOsType(): OSType {
val osName = System.getProperty("os.name").toLowerCase()
if (isWindows(osName)) {
return WINDOWS
}
if (isMac(osName)) {
return MAC_OS
return when {
isWindows(osName) -> WINDOWS
isMac(osName) -> MAC_OS
isUnix(osName) -> LINUX
else -> UNKNOWN
}
return if (isUnix(osName)) {
LINUX
} else UNKNOWN
}
private fun isWindows(name: String): Boolean {
......
......@@ -22,12 +22,6 @@ class OpenGLApi private constructor() {
external fun glGetIntegerv(pname: Int): Int
companion object {
private lateinit var instance: OpenGLApi
fun get(): OpenGLApi {
if (!this::instance.isInitialized) {
instance = OpenGLApi()
}
return instance
}
val instance = OpenGLApi()
}
}
\ No newline at end of file
......@@ -57,7 +57,7 @@ open class SkiaLayer : HardwareLayer() {
}
skijaState.apply {
val gl = OpenGLApi.get()
val gl = OpenGLApi.instance
gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
......@@ -72,7 +72,7 @@ open class SkiaLayer : HardwareLayer() {
private fun initSkija() {
val dpi = contentScale
skijaState.clear()
val gl: OpenGLApi = OpenGLApi.get()
val gl: OpenGLApi = OpenGLApi.instance
val fbId = gl.glGetIntegerv(gl.GL_DRAW_FRAMEBUFFER_BINDING)
skijaState.renderTarget = BackendRenderTarget.makeGL(
(width * dpi).toInt(),
......@@ -111,8 +111,12 @@ open class SkiaWindow : JFrame() {
override fun componentResized(e: ComponentEvent) {
layer.reinit()
layer.setSize(width, height)
Engine.get().render(layer)
display()
}
})
}
fun display() {
layer.display()
}
}
......@@ -46,11 +46,14 @@ JavaVM *jvm;
CGLSetCurrentContext(ctx);
if (self.jvm != NULL) {
// TODO: cache wndClass and drawMethod.
JNIEnv *env;
(*self.jvm)->AttachCurrentThread(self.jvm, (void **)&env, NULL);
jclass wndClass = (*env)->GetObjectClass(env, self.windowRef);
jmethodID drawMethod = (*env)->GetMethodID(env, wndClass, "draw", "()V");
static jclass wndClass = NULL;
if (!wndClass) wndClass = (*env)->GetObjectClass(env, self.windowRef);
static jmethodID drawMethod = NULL;
if (!drawMethod) drawMethod = (*env)->GetMethodID(env, wndClass, "draw", "()V");
if (NULL == drawMethod) {
NSLog(@"The method Window.draw() not found!");
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