Skip to content

Commit 7ae4c0f

Browse files
author
Lalumo Admin
committed
WIP 2: add EGL context synchronization for Android to prevent thread access violations
try to fix tux4kids#43
1 parent 65ad531 commit 7ae4c0f

File tree

9 files changed

+1989
-53
lines changed

9 files changed

+1989
-53
lines changed

app/src/main/java/org/tuxpaint/tuxpaintActivity.java

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ public class tuxpaintActivity extends SDLActivity {
2121
// Lock object for OpenGL context synchronization
2222
private static final Object sGLLock = new Object();
2323

24-
// Native methods for EGL context synchronization to fix EGL_BAD_ACCESS errors
25-
private static native void initEGLContextManager();
26-
private static native boolean lockEGLContext();
27-
private static native boolean unlockEGLContext();
24+
// Native methods for SDL EGL patch to fix EGL_BAD_ACCESS errors
25+
private static native void initSDLEGLPatch();
26+
private static native boolean isSDLEGLPatchInitialized();
27+
private static native void logCurrentThread(String tag);
2828

2929
@Override
3030
protected void onCreate(Bundle savedInstanceState) {
3131
Log.v(TAG, "onCreate()");
3232

33+
// Initialize SDL EGL patch early to prevent EGL_BAD_ACCESS errors
34+
initSDLEGLContextManager();
35+
36+
// Log the current thread ID for debugging
37+
logCurrentThread("onCreate");
38+
3339
boolean requestPermissions = false;
3440
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
3541
if (this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
@@ -49,13 +55,21 @@ protected void onCreate(Bundle savedInstanceState) {
4955
// Synchronize OpenGL context access
5056
@Override
5157
public void onResume() {
58+
// Using Thread.currentThread().getName() instead of deprecated getId()
59+
Log.v(TAG, "onResume() - current thread: " + Thread.currentThread().getName());
60+
logCurrentThread("onResume");
61+
5262
synchronized (sGLLock) {
5363
super.onResume();
5464
}
5565
}
5666

5767
@Override
5868
public void onPause() {
69+
// Using Thread.currentThread().getName() instead of deprecated getId()
70+
Log.v(TAG, "onPause() - current thread: " + Thread.currentThread().getName());
71+
logCurrentThread("onPause");
72+
5973
synchronized (sGLLock) {
6074
super.onPause();
6175
}
@@ -78,15 +92,75 @@ public void onLowMemory() {
7892
super.onLowMemory();
7993
}
8094
}
95+
96+
/**
97+
* Initializes the SDL EGL patch to prevent EGL_BAD_ACCESS errors
98+
* This method sets up native code to synchronize EGL context access
99+
*/
100+
private static void initSDLEGLContextManager() {
101+
Log.i(TAG, "Initializing SDL EGL Context Manager");
102+
// Initialize the patch in the native code
103+
initSDLEGLPatch();
104+
Log.i(TAG, "SDL EGL Patch initialized: " + isSDLEGLPatchInitialized());
105+
}
81106

82107
static {
83-
System.loadLibrary("c++_shared");
84-
System.loadLibrary("tuxpaint_png");
85-
System.loadLibrary("tuxpaint_fribidi");
86-
System.loadLibrary("SDL2");
87-
System.loadLibrary("tp_android_assets_fopen");
88-
System.loadLibrary("tuxpaint_intl");
89-
System.loadLibrary("tuxpaint_iconv");
108+
try {
109+
System.loadLibrary("c++_shared");
110+
Log.i(TAG, "Loaded c++_shared");
111+
} catch (UnsatisfiedLinkError e) {
112+
Log.e(TAG, "Failed to load c++_shared: " + e.getMessage());
113+
}
114+
115+
try {
116+
System.loadLibrary("tuxpaint_png");
117+
Log.i(TAG, "Loaded tuxpaint_png");
118+
} catch (UnsatisfiedLinkError e) {
119+
Log.e(TAG, "Failed to load tuxpaint_png: " + e.getMessage());
120+
}
121+
122+
try {
123+
System.loadLibrary("tuxpaint_fribidi");
124+
Log.i(TAG, "Loaded tuxpaint_fribidi");
125+
} catch (UnsatisfiedLinkError e) {
126+
Log.e(TAG, "Failed to load tuxpaint_fribidi: " + e.getMessage());
127+
}
128+
129+
try {
130+
System.loadLibrary("SDL2");
131+
Log.i(TAG, "Loaded SDL2");
132+
} catch (UnsatisfiedLinkError e) {
133+
Log.e(TAG, "Failed to load SDL2: " + e.getMessage());
134+
}
135+
136+
try {
137+
System.loadLibrary("tp_android_assets_fopen");
138+
Log.i(TAG, "Loaded tp_android_assets_fopen");
139+
} catch (UnsatisfiedLinkError e) {
140+
Log.e(TAG, "Failed to load tp_android_assets_fopen: " + e.getMessage());
141+
}
142+
143+
try {
144+
System.loadLibrary("tuxpaint_intl");
145+
Log.i(TAG, "Loaded tuxpaint_intl");
146+
} catch (UnsatisfiedLinkError e) {
147+
Log.e(TAG, "Failed to load tuxpaint_intl: " + e.getMessage());
148+
}
149+
150+
try {
151+
System.loadLibrary("tuxpaint_iconv");
152+
Log.i(TAG, "Loaded tuxpaint_iconv");
153+
} catch (UnsatisfiedLinkError e) {
154+
Log.e(TAG, "Failed to load tuxpaint_iconv: " + e.getMessage());
155+
}
156+
157+
// Load our SDL EGL patch native library
158+
try {
159+
System.loadLibrary("sdl_egl_patch");
160+
Log.i(TAG, "Loaded sdl_egl_patch");
161+
} catch (UnsatisfiedLinkError e) {
162+
Log.e(TAG, "Failed to load sdl_egl_patch: " + e.getMessage());
163+
}
90164
System.loadLibrary("tuxpaint_pixman");
91165
System.loadLibrary("tuxpaint_xml2");
92166
System.loadLibrary("tuxpaint_freetype");
@@ -107,6 +181,6 @@ public void onLowMemory() {
107181
System.loadLibrary("tuxpaint");
108182

109183
// Initialize the EGL context manager to prevent EGL_BAD_ACCESS errors
110-
initEGLContextManager();
184+
initSDLEGLContextManager();
111185
}
112186
}

app/src/main/jni/Android.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1+
# Include all subdirectory makefiles
12
include $(call all-subdir-makefiles)
23

4+
# Build sdl_egl_patch directly here instead of including a separate makefile
5+
6+
# Build sdl_egl_patch shared library for EGL synchronization
7+
include $(CLEAR_VARS)
8+
9+
LOCAL_MODULE := sdl_egl_patch
10+
LOCAL_CFLAGS := -Wall -Werror
11+
LOCAL_LDLIBS := -llog -lGLESv2 -lEGL -landroid
12+
LOCAL_SRC_FILES := tuxpaint/src/sdl_egl_patch.c \
13+
tuxpaint/src/sdl_egl_jni_bridge.c \
14+
tuxpaint/src/tuxpaint_egl_sync.c
15+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/SDL2/include
16+
17+
# For compatibility with libSDL2
18+
LOCAL_SHARED_LIBRARIES := SDL2
19+
20+
include $(BUILD_SHARED_LIBRARY)
21+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
LOCAL_PATH := $(call my-dir)
2+
3+
# Build sdl_egl_patch shared library for EGL synchronization
4+
include $(CLEAR_VARS)
5+
6+
LOCAL_MODULE := sdl_egl_patch
7+
LOCAL_CFLAGS := -Wall -Werror
8+
LOCAL_LDLIBS := -llog -lGLESv2 -lEGL -landroid
9+
LOCAL_SRC_FILES := sdl_egl_patch.c sdl_egl_jni_bridge.c
10+
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../../src/main/jni/SDL2/include
11+
12+
# For compatibility with libSDL2
13+
LOCAL_SHARED_LIBRARIES := SDL2
14+
15+
include $(BUILD_SHARED_LIBRARY)

0 commit comments

Comments
 (0)