-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopen_gl.cpp
More file actions
184 lines (130 loc) · 4.86 KB
/
open_gl.cpp
File metadata and controls
184 lines (130 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// open_gl.cpp - FINAL FIXED VERSION (context-loss proof + state safe)
// Created by mrjar on 12/1/2025.
#include "imgui/imgui.h"
#include "imgui/backends/imgui_impl_android.h"
#include "imgui/backends/imgui_impl_opengl3.h"
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <dlfcn.h>
#include <stdio.h>
extern "C" {
#include "inlinehook.h"
#include "dynlib.h"
}
static int g_Width = 0, g_Height = 0;
static bool g_Setup = false;
static EGLDisplay g_LastDisplay = EGL_NO_DISPLAY;
static EGLSurface g_LastSurface = EGL_NO_SURFACE;
static EGLBoolean (*orig_eglSwapBuffers)(EGLDisplay, EGLSurface) = nullptr;
extern void render_ui();
extern void cleanup_ui();
struct SavedGLState {
GLint viewport[4] = {0};
GLint program = 0;
GLint texture = 0;
GLint array_buffer = 0;
GLint vertex_array = 0;
GLboolean blend = GL_FALSE;
GLboolean depth = GL_FALSE;
GLboolean cull = GL_FALSE;
GLboolean scissor = GL_FALSE;
};
static void SaveGLState(SavedGLState& state) {
glGetIntegerv(GL_VIEWPORT, state.viewport);
glGetIntegerv(GL_CURRENT_PROGRAM, &state.program);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &state.texture);
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &state.array_buffer);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &state.vertex_array);
state.blend = glIsEnabled(GL_BLEND);
state.depth = glIsEnabled(GL_DEPTH_TEST);
state.cull = glIsEnabled(GL_CULL_FACE);
state.scissor = glIsEnabled(GL_SCISSOR_TEST);
}
static void RestoreGLState(const SavedGLState& state) {
glViewport(state.viewport[0], state.viewport[1], state.viewport[2], state.viewport[3]);
glUseProgram(state.program);
glBindTexture(GL_TEXTURE_2D, state.texture);
glBindBuffer(GL_ARRAY_BUFFER, state.array_buffer);
glBindVertexArray(state.vertex_array);
if (state.blend) glEnable(GL_BLEND); else glDisable(GL_BLEND);
if (state.depth) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
if (state.cull) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
if (state.scissor) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST);
}
static void InitializeImGui(EGLDisplay display, EGLSurface surface) {
eglQuerySurface(display, surface, EGL_WIDTH, &g_Width);
eglQuerySurface(display, surface, EGL_HEIGHT, &g_Height);
if (g_Width <= 1 || g_Height <= 1) {
return;
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::GetIO().DisplaySize = ImVec2((float)g_Width, (float)g_Height);
ImGui::GetIO().IniFilename = nullptr;
ImGui_ImplAndroid_Init(nullptr);
ImGui_ImplOpenGL3_Init("#version 300 es");
ImGui::StyleColorsDark();
ImGui::GetStyle().ScaleAllSizes(2.0f);
ImFontConfig cfg{};
cfg.SizePixels = 32.0f;
ImGui::GetIO().Fonts->AddFontDefault(&cfg);
ImGui::GetIO().Fonts->Build();
g_Setup = true;
g_LastDisplay = display;
g_LastSurface = surface;
}
static EGLBoolean hook_eglSwapBuffers(EGLDisplay display, EGLSurface surface) {
int width = 0, height = 0;
eglQuerySurface(display, surface, EGL_WIDTH, &width);
eglQuerySurface(display, surface, EGL_HEIGHT, &height);
if (width <= 1 || height <= 1 ||
display != g_LastDisplay ||
surface != g_LastSurface) {
if (g_Setup) {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplAndroid_Shutdown();
ImGui::DestroyContext();
g_Setup = false;
}
}
if (!g_Setup) {
InitializeImGui(display, surface);
if (!g_Setup) {
return orig_eglSwapBuffers(display, surface);
}
}
eglQuerySurface(display, surface, EGL_WIDTH, &g_Width);
eglQuerySurface(display, surface, EGL_HEIGHT, &g_Height);
ImGui::GetIO().DisplaySize = ImVec2((float)g_Width, (float)g_Height);
SavedGLState saved;
SaveGLState(saved);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplAndroid_NewFrame(g_Width, g_Height);
ImGui::NewFrame();
render_ui();
ImGui::Render();
glViewport(0, 0, g_Width, g_Height);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
RestoreGLState(saved);
return orig_eglSwapBuffers(display, surface);
}
void setup_opengl_hooks() {
printf("[HOOK] Setting up OpenGL hooks...\n");
void* egl_handle = dlopen("libEGL.so", RTLD_LAZY | RTLD_GLOBAL);
if (!egl_handle) {
printf("[HOOK] ERROR: Failed to dlopen libEGL.so!\n");
return;
}
void* swap_addr = gpwn_dlsym("libEGL.so", "eglSwapBuffers");
if (swap_addr) {
printf("[HOOK] Found eglSwapBuffers @ %p\n", swap_addr);
hook_addr(swap_addr,
(void*)hook_eglSwapBuffers,
(void**)&orig_eglSwapBuffers,
GPWN_AARCH64_NANOHOOK);
printf("[HOOK] eglSwapBuffers successfully hooked.\n");
} else {
printf("[HOOK] ERROR: Could not find eglSwapBuffers symbol.\n");
}
dlclose(egl_handle);
}