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

Fix cases where OpenGL init fails later (#129)

parent 1edf9a7d
...@@ -80,9 +80,13 @@ extern "C" ...@@ -80,9 +80,13 @@ extern "C"
JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_createContext(JNIEnv *env, jobject redrawer, jlong displayPtr) JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_redrawer_LinuxOpenGLRedrawerKt_createContext(JNIEnv *env, jobject redrawer, jlong displayPtr)
{ {
Display *display = fromJavaPointer<Display *>(displayPtr); Display *display = fromJavaPointer<Display *>(displayPtr);
if (!display) return 0;
GLint att[] = {GLX_RGBA, GLX_DOUBLEBUFFER, True, None}; GLint att[] = {GLX_RGBA, GLX_DOUBLEBUFFER, True, None};
XVisualInfo *vi = glXChooseVisual(display, 0, att); XVisualInfo *vi = glXChooseVisual(display, 0, att);
if (!vi) return 0;
GLXContext *context = new GLXContext(glXCreateContext(display, vi, NULL, GL_TRUE)); GLXContext *context = new GLXContext(glXCreateContext(display, vi, NULL, GL_TRUE));
return toJavaPointer(context); return toJavaPointer(context);
} }
...@@ -92,7 +96,9 @@ extern "C" ...@@ -92,7 +96,9 @@ extern "C"
Display *display = fromJavaPointer<Display *>(displayPtr); Display *display = fromJavaPointer<Display *>(displayPtr);
GLXContext *context = fromJavaPointer<GLXContext *>(contextPtr); GLXContext *context = fromJavaPointer<GLXContext *>(contextPtr);
glXDestroyContext(display, *context); if (display && context) {
delete context; glXDestroyContext(display, *context);
delete context;
}
} }
} }
\ No newline at end of file
...@@ -112,11 +112,27 @@ open class SkiaLayer( ...@@ -112,11 +112,27 @@ open class SkiaLayer(
private val pictureRecorder = PictureRecorder() private val pictureRecorder = PictureRecorder()
private val pictureLock = Any() private val pictureLock = Any()
private fun findNextWorkingRenderApi(redraw: Boolean) {
var thrown: Boolean
do {
thrown = false
try {
renderApi = fallbackRenderApiQueue.removeAt(0)
println("Trying $renderApi rendering...")
contextHandler?.dispose()
redrawer?.dispose()
contextHandler = createContextHandler(this, renderApi)
redrawer = platformOperations.createRedrawer(this, renderApi, properties)
if (redraw) redrawer!!.redrawImmediately()
} catch (e: IllegalArgumentException) {
thrown = true
}
} while (thrown)
}
open fun init() { open fun init() {
backedLayer.init() backedLayer.init()
renderApi = fallbackRenderApiQueue.removeAt(0) findNextWorkingRenderApi(false)
contextHandler = createContextHandler(this, renderApi)
redrawer = platformOperations.createRedrawer(this, renderApi, properties)
onInit.complete(Unit) onInit.complete(Unit)
} }
...@@ -270,7 +286,7 @@ open class SkiaLayer( ...@@ -270,7 +286,7 @@ open class SkiaLayer(
return withContext(Dispatchers.Swing) { return withContext(Dispatchers.Swing) {
check(!isDisposed) check(!isDisposed)
onInit.await() onInit.await()
redrawer!!.awaitRedraw() redrawer?.awaitRedraw() != false
} }
} }
...@@ -315,7 +331,7 @@ open class SkiaLayer( ...@@ -315,7 +331,7 @@ open class SkiaLayer(
check(!isDisposed) check(!isDisposed)
contextHandler?.apply { contextHandler?.apply {
if (!initContext()) { if (!initContext()) {
fallbackToNextApi() findNextWorkingRenderApi(true)
return false return false
} }
initCanvas() initCanvas()
...@@ -373,16 +389,6 @@ open class SkiaLayer( ...@@ -373,16 +389,6 @@ open class SkiaLayer(
) )
} }
private fun fallbackToNextApi() {
renderApi = fallbackRenderApiQueue.removeAt(0)
println("Falling back to $renderApi rendering...")
contextHandler?.dispose()
redrawer?.dispose()
contextHandler = createContextHandler(this, renderApi)
redrawer = platformOperations.createRedrawer(this, renderApi, properties)
redrawer!!.redrawImmediately()
}
private fun roundSize(value: Int): Int { private fun roundSize(value: Int): Int {
var rounded = value * contentScale var rounded = value * contentScale
val diff = rounded - rounded.toInt() val diff = rounded - rounded.toInt()
......
...@@ -16,7 +16,7 @@ internal class LinuxOpenGLRedrawer( ...@@ -16,7 +16,7 @@ internal class LinuxOpenGLRedrawer(
private val properties: SkiaLayerProperties private val properties: SkiaLayerProperties
) : Redrawer { ) : Redrawer {
private val context = layer.backedLayer.lockDrawingSurface { private val context = layer.backedLayer.lockDrawingSurface {
it.createContext() it.createContext().also { if (it == 0L) throw IllegalArgumentException("Cannot create Linux GL context") }
} }
private var isDisposed = false private var isDisposed = false
......
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