Skip to content

Commit

Permalink
Call glFinish on render breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
maurhofer-ubique committed Jan 22, 2024
1 parent 4787352 commit 9e9a2bd
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.util.Log
import io.openmobilemaps.mapscore.BuildConfig
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong
import javax.microedition.khronos.egl.*
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.egl.EGLContext
Expand All @@ -38,6 +39,10 @@ class GLThread constructor(
private const val TAG = "GLThread"
private const val EGL_OPENGL_ES3_BIT = 0x00000040

private const val PAUSE_RENDER_INTERVAL = 30000L
private const val BREAK_RENDER_INTERVAL = 1000L
private const val BREAK_MIN_FINISH_MS = BREAK_RENDER_INTERVAL / 2

const val MAX_NUM_GRAPHICS_PRE_TASKS = 16

private val defaultConfig = intArrayOf(
Expand Down Expand Up @@ -69,6 +74,8 @@ class GLThread constructor(
val runNotifier = Object()
var glRunList = ConcurrentLinkedQueue<() -> Unit>()
val isDirty = AtomicBoolean(false)
val lastDirtyTimestamp = AtomicLong(0)
var hasFinishedSinceDirty = false

var renderer: GLSurfaceView.Renderer? = null
var surface: SurfaceTexture? = null
Expand Down Expand Up @@ -106,12 +113,23 @@ class GLThread constructor(
if ((!isDirty.get() && glRunList.isEmpty()) || isPaused) {
var wasPaused = false
do {
var firstPause = false
if (isPaused && !wasPaused) {
onPauseCallback?.invoke()
wasPaused = true
firstPause = true
}
val preFinish = System.currentTimeMillis()
if (firstPause || (!hasFinishedSinceDirty && preFinish - lastDirtyTimestamp.get() > BREAK_MIN_FINISH_MS)) {
GLES32.glFinish()
hasFinishedSinceDirty = true
}
val finishDuration = System.currentTimeMillis() - preFinish

try {
synchronized(runNotifier) { runNotifier.wait(1000) }
if (finishDuration < BREAK_RENDER_INTERVAL) {
synchronized(runNotifier) { runNotifier.wait(if (isPaused) PAUSE_RENDER_INTERVAL else BREAK_RENDER_INTERVAL - finishDuration) }
}
} catch (e: InterruptedException) {
e.printStackTrace()
}
Expand Down Expand Up @@ -360,7 +378,10 @@ class GLThread constructor(
}

fun requestRender() {
lastDirtyTimestamp.set(System.currentTimeMillis())
hasFinishedSinceDirty = false
isDirty.set(true)

synchronized(runNotifier) { runNotifier.notify() }
}

Expand Down

0 comments on commit 9e9a2bd

Please sign in to comment.