Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call glFinish on render breaks #575

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -103,15 +110,27 @@ class GLThread constructor(
}

while (!finished) {
if ((!isDirty.get() && glRunList.isEmpty()) || isPaused) {
if ((!isDirty.get() && glRunList.isEmpty()) && (isPaused || System.currentTimeMillis() - lastDirtyTimestamp.get() > BREAK_MIN_FINISH_MS)) {
var wasPaused = false
do {
var firstPause = false
if (isPaused && !wasPaused) {
onPauseCallback?.invoke()
wasPaused = true
firstPause = true
}
var finishDuration = 0L
if (firstPause || !hasFinishedSinceDirty) {
finishDuration = System.currentTimeMillis()
GLES32.glFinish()
hasFinishedSinceDirty = true
finishDuration = System.currentTimeMillis() - finishDuration
}

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 All @@ -136,7 +155,6 @@ class GLThread constructor(
glRunList.poll()?.invoke()
i++
}

renderer.onDrawFrame(gl10)
if (BuildConfig.DEBUG) {
GLES32.glGetError().let {
Expand Down Expand Up @@ -360,6 +378,8 @@ class GLThread constructor(
}

fun requestRender() {
lastDirtyTimestamp.set(System.currentTimeMillis())
hasFinishedSinceDirty = false
isDirty.set(true)
synchronized(runNotifier) { runNotifier.notify() }
}
Expand Down
Loading