Unverified Commit e8b56bd4 authored by Nikolay Igotti's avatar Nikolay Igotti Committed by GitHub

Cleaner backend integration. (#14)

parent 2faf120c
......@@ -243,7 +243,8 @@ tasks.withType(CppCompile::class.java).configureEach {
"-fvisibility=hidden",
"-fvisibility-inlines-hidden",
"-I$jdkHome/include/darwin",
"-DSK_BUILD_FOR_MAC"
"-DSK_BUILD_FOR_MAC",
"-DSK_METAL"
)
)
}
......@@ -312,6 +313,7 @@ tasks.withType(LinkSharedLibrary::class.java).configureEach {
"-framework", "CoreServices",
"-framework", "CoreText",
"-framework", "Foundation",
"-framework", "Metal",
"-framework", "OpenGL",
"-framework", "QuartzCore" // for CoreAnimation
)
......
#include <jni.h>
#include "GrBackendSurface.h"
#include "GrContext.h"
extern "C" {
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_RenderTargetsKt_makeGLRenderTargetNative(
JNIEnv * env, jclass jclass,
jint width, jint height,
jint sampleCnt, jint stencilBits,
jint fbId, jint fbFormat
) {
GrGLFramebufferInfo glInfo = { static_cast<unsigned int>(fbId), static_cast<unsigned int>(fbFormat) };
GrBackendRenderTarget* obj = new GrBackendRenderTarget(width, height, sampleCnt, stencilBits, glInfo);
return reinterpret_cast<jlong>(obj);
}
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_RenderTargetsKt_makeGLContextNative(JNIEnv* env, jclass jclass) {
return reinterpret_cast<jlong>(GrContext::MakeGL().release());
}
extern void getMetalDeviceAndQueue(void** device, void** queue);
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_RenderTargetsKt_makeMetalRenderTargetNative(
JNIEnv * env, jclass jclass, jint width, jint height, jint sampleCnt) {
#ifdef SK_METAL
// TODO: create properly.
GrMtlTextureInfo mtlInfo;
GrBackendRenderTarget* obj = new GrBackendRenderTarget(width, height, sampleCnt, mtlInfo);
return reinterpret_cast<jlong>(obj);
#else
return 0;
#endif
}
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_RenderTargetsKt_makeMetalContextNative(JNIEnv* env, jclass jclass) {
#ifdef SK_METAL
void* device = nullptr;
void* queue = nullptr;
getMetalDeviceAndQueue(&device, &queue);
return reinterpret_cast<jlong>(GrContext::MakeMetal(device, queue).release());
#else
return 0;
#endif
}
} // extern "C"
\ No newline at end of file
......@@ -37,7 +37,7 @@ object Library {
if (loaded) return
loadOrGet(resourcePath, name, true)
val loadIcu = System.getProperty("os.name").toLowerCase().startsWith("win")
val loadIcu = OSType.currentOs == OSType.WINDOWS
if (loadIcu) {
loadOrGet(resourcePath, "icudtl.dat", false)
}
......
......@@ -4,28 +4,14 @@ enum class OSType {
MAC_OS, LINUX, WINDOWS, UNKNOWN;
companion object {
val currentOs = defineOsType()
private fun defineOsType(): OSType {
val osName = System.getProperty("os.name").toLowerCase()
return when {
isWindows(osName) -> WINDOWS
isMac(osName) -> MAC_OS
isUnix(osName) -> LINUX
val currentOs by lazy {
val name = System.getProperty("os.name").toLowerCase()
when {
name.indexOf("win") >= 0 -> WINDOWS
name.indexOf("mac") >= 0 -> MAC_OS
name.indexOf("linux") >= 0 -> LINUX
else -> UNKNOWN
}
}
private fun isWindows(name: String): Boolean {
return name.indexOf("win") >= 0
}
private fun isMac(name: String): Boolean {
return name.indexOf("mac") >= 0
}
private fun isUnix(name: String): Boolean {
return name.indexOf("nix") >= 0 || name.indexOf("nux") >= 0 || name.indexOf("aix") >= 0
}
}
}
\ No newline at end of file
package org.jetbrains.skiko
import org.jetbrains.skija.BackendRenderTarget
import org.jetbrains.skija.Context
internal fun makeGLContext() = Context(
makeGLContextNative()
)
internal fun makeGLRenderTarget(width: Int, height: Int, sampleCnt: Int, stencilBits: Int, fbId: Int, fbFormat: Int) = BackendRenderTarget(
makeGLRenderTargetNative(width, height, sampleCnt, stencilBits, fbId, fbFormat)
)
internal fun makeMetalRenderTarget(width: Int, height: Int, sampleCnt: Int) = BackendRenderTarget(
makeMetalRenderTargetNative(width, height, sampleCnt).also { if (it == 0L) TODO("not yet supported") }
)
internal fun makeMetalContext() = Context(
makeMetalContextNative().also { if (it == 0L) TODO("not yet supported") }
)
external private fun makeGLRenderTargetNative(width: Int, height: Int, sampleCnt: Int, stencilBits: Int, fbId: Int, fbFormat: Int): Long
external private fun makeGLContextNative(): Long
external private fun makeMetalRenderTargetNative(width: Int, height: Int, sampleCnt: Int): Long
external private fun makeMetalContextNative(): Long
......@@ -31,7 +31,9 @@ interface SkiaRenderer {
fun onDispose()
}
open class SkiaLayer : HardwareLayer() {
open class SkiaLayer() : HardwareLayer() {
open val api: GraphicsApi = GraphicsApi.OPENGL
var renderer: SkiaRenderer? = null
private val skijaState = SkijaState()
......@@ -49,7 +51,11 @@ open class SkiaLayer : HardwareLayer() {
override fun draw() {
if (!inited) {
if (skijaState.context == null) {
skijaState.context = Context.makeGL()
skijaState.context = when (api) {
GraphicsApi.OPENGL -> makeGLContext()
GraphicsApi.METAL -> makeMetalContext()
else -> TODO("Unsupported yet")
}
}
renderer?.onInit()
inited = true
......@@ -73,9 +79,11 @@ open class SkiaLayer : HardwareLayer() {
private fun initRenderTarget(dpi: Float) {
skijaState.apply {
clear()
val gl: OpenGLApi = OpenGLApi.instance
renderTarget = when (api) {
GraphicsApi.OPENGL -> {
val gl = OpenGLApi.instance
val fbId = gl.glGetIntegerv(gl.GL_DRAW_FRAMEBUFFER_BINDING)
renderTarget = BackendRenderTarget.makeGL(
makeGLRenderTarget(
(width * dpi).toInt(),
(height * dpi).toInt(),
0,
......@@ -84,6 +92,14 @@ open class SkiaLayer : HardwareLayer() {
FramebufferFormat.GR_GL_RGBA8
)
}
GraphicsApi.METAL -> makeMetalRenderTarget(
(width * dpi).toInt(),
(height * dpi).toInt(),
0
)
else -> TODO("Unsupported yet")
}
}
}
private fun initSurface() {
......
......@@ -8,6 +8,8 @@
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
#import <OpenGL/gl3.h>
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
JavaVM *jvm = NULL;
......@@ -252,3 +254,10 @@ JNIEXPORT jfloat JNICALL Java_org_jetbrains_skiko_HardwareLayer_getContentScale(
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_HardwareLayer_getWindowHandle(JNIEnv *env, jobject window) {
return (jlong)kNullWindowHandle;
}
void getMetalDeviceAndQueue(void** device, void** queue) {
id<MTLDevice> fDevice = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> fQueue = [fDevice newCommandQueue];
*device = (__bridge void*)fDevice;
*queue = (__bridge void*)fQueue;
}
\ No newline at end of file
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