Skip to content

Commit

Permalink
Added code to stop gl thread
Browse files Browse the repository at this point in the history
 terminate the thread using callOnDetachFromWindow and finalize method
 present in GLSurafceView
  • Loading branch information
Devashishbasu committed Apr 16, 2024
1 parent c53f25c commit 11f0d4a
Showing 1 changed file with 35 additions and 0 deletions.
@@ -1,15 +1,50 @@
package org.robolectric.shadows;

import android.opengl.GLSurfaceView;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

/** Fake implementation of GLSurfaceView */
@Implements(GLSurfaceView.class)
public class ShadowGLSurfaceView extends ShadowSurfaceView {

private static final String TAG = "ShadowGLSurfaceView";

@Implementation
protected void onPause() {}

@Implementation
protected void onResume() {}

@Implementation
protected void onDetachedFromWindow() {
stopGLThread();
}

@Implementation
protected void finalize() throws Throwable {
try {
stopGLThread();
} finally {
super.finalize();
}
}

private void stopGLThread() {
try {
Field glThreadField = GLSurfaceView.class.getDeclaredField("mGLThread");
glThreadField.setAccessible(true);
Thread glThread = (Thread) glThreadField.get(realView);
if (glThread != null) {
Method requestExitAndWait = glThread.getClass().getMethod("requestExitAndWait");
requestExitAndWait.invoke(glThread);
}
} catch (Exception e) {
Log.e(TAG, "Exception occurred while stopping GLThread: " + e.getMessage());
e.printStackTrace();
}
}
}

0 comments on commit 11f0d4a

Please sign in to comment.