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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
// Copyright 2007-2023 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
// Needs to be included before GL/gl.h
#include "lib.h"
// Needs to be included before glext.h
#include <GL/gl.h>
#include <ctime>
#include "../3rdparty/GL/glext.h"
#define TDEF(ret, name, arg) typedef ret(__stdcall *t##name) arg
#define GLDEF(ret, name, arg) \
TDEF(ret, name, arg); \
t##name o##name = nullptr
GLDEF(HGLRC, wglCreateContext, (HDC));
GLDEF(void, glGenTextures, (GLsizei, GLuint *) );
GLDEF(void, glDeleteTextures, (GLsizei, GLuint *) );
GLDEF(void, glEnable, (GLenum));
GLDEF(void, glDisable, (GLenum));
GLDEF(void, glBlendFunc, (GLenum, GLenum));
GLDEF(void, glViewport, (GLint, GLint, GLsizei, GLsizei));
GLDEF(void, glMatrixMode, (GLenum));
GLDEF(void, glLoadIdentity, (void) );
GLDEF(void, glOrtho, (GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble));
GLDEF(void, glBindTexture, (GLenum, GLuint));
GLDEF(void, glPushMatrix, (void) );
GLDEF(void, glBegin, (GLenum));
GLDEF(void, glEnd, (void) );
GLDEF(void, glTexCoord2f, (GLfloat, GLfloat));
GLDEF(void, glVertex2f, (GLfloat, GLfloat));
GLDEF(void, glPopMatrix, (void) );
GLDEF(void, glTexParameteri, (GLenum, GLenum, GLint));
GLDEF(void, glTexEnvi, (GLenum, GLenum, GLint));
GLDEF(void, glTexImage2D, (GLenum, GLint, GLint, GLsizei, GLsizei, GLint, GLenum, GLenum, const GLvoid *) );
GLDEF(void, glTexSubImage2D, (GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const GLvoid *) );
GLDEF(void, glPixelStorei, (GLenum, GLint));
GLDEF(void, wglMakeCurrent, (HDC, HGLRC));
GLDEF(HGLRC, wglGetCurrentContext, (void) );
GLDEF(HDC, wglGetCurrentDC, (void) );
GLDEF(int, GetDeviceCaps, (HDC, int) );
#define INJDEF(ret, name, arg) \
GLDEF(ret, name, arg); \
static HardHook hh##name
INJDEF(BOOL, wglSwapBuffers, (HDC));
static bool bHooked = false;
class Context : protected Pipe {
public:
Context(HDC hdc);
void draw(HDC hdc);
protected:
virtual void blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h);
virtual void setRect();
virtual void newTexture(unsigned int width, unsigned int height);
private:
HGLRC ctx;
GLuint texture;
clock_t timeT;
unsigned int frameCount;
void initContext();
void doDraw(HDC hdc);
};
Context::Context(HDC hdc) {
timeT = clock();
frameCount = 0;
texture = ~0;
ctx = owglCreateContext(hdc);
HGLRC oldctx = owglGetCurrentContext();
HDC oldhdc = owglGetCurrentDC();
owglMakeCurrent(hdc, ctx);
initContext();
owglMakeCurrent(oldhdc, oldctx);
}
void Context::initContext() {
// Here we go. From the top. Where is glResetState?
oglDisable(GL_ALPHA_TEST);
oglDisable(GL_AUTO_NORMAL);
oglEnable(GL_BLEND);
// Skip clip planes, there are thousands of them.
oglDisable(GL_COLOR_LOGIC_OP);
oglDisable(GL_COLOR_MATERIAL);
oglDisable(GL_COLOR_TABLE);
oglDisable(GL_CONVOLUTION_1D);
oglDisable(GL_CONVOLUTION_2D);
oglDisable(GL_CULL_FACE);
oglDisable(GL_DEPTH_TEST);
oglDisable(GL_DITHER);
oglDisable(GL_FOG);
oglDisable(GL_HISTOGRAM);
oglDisable(GL_INDEX_LOGIC_OP);
oglDisable(GL_LIGHTING);
// Skip line smmooth
// Skip map
oglDisable(GL_MINMAX);
// Skip polygon offset
oglDisable(GL_SEPARABLE_2D);
oglDisable(GL_SCISSOR_TEST);
oglDisable(GL_STENCIL_TEST);
oglEnable(GL_TEXTURE_2D);
oglDisable(GL_TEXTURE_GEN_Q);
oglDisable(GL_TEXTURE_GEN_R);
oglDisable(GL_TEXTURE_GEN_S);
oglDisable(GL_TEXTURE_GEN_T);
oglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
void Context::blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
ods("OpenGL: Blit %d %d %d %d -- %d %d : %d", x, y, w, h, uiWidth, uiHeight, texture);
if (texture == ~0)
return;
oglBindTexture(GL_TEXTURE_2D, texture);
if ((x == 0) && (y == 0) && (w == uiWidth) && (h == uiHeight)) {
oglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, uiWidth, uiHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, a_ucTexture);
} else {
if (w != uiWidth)
oglPixelStorei(GL_UNPACK_ROW_LENGTH, uiWidth);
oglTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_BGRA, GL_UNSIGNED_BYTE, a_ucTexture + 4 * (y * uiWidth + x));
if (w != uiWidth)
oglPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
}
void Context::setRect() {
ods("OpenGL: setRect");
}
void Context::newTexture(unsigned int width, unsigned int height) {
ods("OpenGL: newTex");
if (texture == ~0) {
oglBindTexture(GL_TEXTURE_2D, 0);
oglDeleteTextures(1, &texture);
texture = ~0;
}
oglGenTextures(1, &texture);
oglBindTexture(GL_TEXTURE_2D, texture);
oglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
oglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
oglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
oglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
unsigned char *ptr = new unsigned char[width * height * 4];
memset(ptr, 0, width * height * 4);
oglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, ptr);
delete[] ptr;
}
void Context::draw(HDC hdc) {
HGLRC oldctx = owglGetCurrentContext();
HDC oldhdc = owglGetCurrentDC();
owglMakeCurrent(hdc, ctx);
doDraw(hdc);
owglMakeCurrent(oldhdc, oldctx);
}
void Context::doDraw(HDC hdc) {
// DEBUG
// sm->bDebug = true;
clock_t t = clock();
float elapsed = static_cast< float >(t - timeT) / CLOCKS_PER_SEC;
++frameCount;
if (elapsed > OVERLAY_FPS_INTERVAL) {
OverlayMsg om;
om.omh.uiMagic = OVERLAY_MAGIC_NUMBER;
om.omh.uiType = OVERLAY_MSGTYPE_FPS;
om.omh.iLength = sizeof(OverlayMsgFps);
om.omf.fps = frameCount / elapsed;
sendMessage(om);
frameCount = 0;
timeT = t;
}
unsigned int width, height;
width = oGetDeviceCaps(hdc, HORZRES);
height = oGetDeviceCaps(hdc, VERTRES);
HWND hwnd = WindowFromDC(hdc);
if (hwnd) {
RECT r;
if (GetClientRect(hwnd, &r)) {
width = r.right - r.left;
height = r.bottom - r.top;
}
}
ods("OpenGL: DrawStart: Screen is %d x %d", width, height);
checkMessage(width, height);
oglViewport(0, 0, width, height);
oglMatrixMode(GL_PROJECTION);
oglLoadIdentity();
oglOrtho(0, width, height, 0, -100.0, 100.0);
oglMatrixMode(GL_MODELVIEW);
oglBindTexture(GL_TEXTURE_2D, texture);
oglPushMatrix();
oglLoadIdentity();
float w = static_cast< float >(uiWidth);
float h = static_cast< float >(uiHeight);
float left = static_cast< float >(uiLeft);
float top = static_cast< float >(uiTop);
float right = static_cast< float >(uiRight);
float bottom = static_cast< float >(uiBottom);
float xm = (left) / w;
float ym = (top) / h;
float xmx = (right) / w;
float ymx = (bottom) / h;
oglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
oglBegin(GL_QUADS);
oglTexCoord2f(xm, ymx);
oglVertex2f(left, bottom);
oglTexCoord2f(xm, ym);
oglVertex2f(left, top);
oglTexCoord2f(xmx, ym);
oglVertex2f(right, top);
oglTexCoord2f(xmx, ymx);
oglVertex2f(right, bottom);
oglEnd();
oglPopMatrix();
}
static map< HDC, Context * > contexts;
static void doSwap(HDC hdc) {
Context *c = contexts[hdc];
if (!c) {
ods("OpenGL: New context for device %p", hdc);
c = new Context(hdc);
contexts[hdc] = c;
} else {
ods("OpenGL: Reusing old context");
}
c->draw(hdc);
}
static BOOL __stdcall mywglSwapBuffers(HDC hdc) {
ods("OpenGL: wglSwapBuffers");
doSwap(hdc);
hhwglSwapBuffers.restore();
BOOL ret = owglSwapBuffers(hdc);
hhwglSwapBuffers.inject();
return ret;
}
/// Ensure that all the symbols that the OpenGL overlay requires have been
/// looked up.
/// @return true if all symbols have been looked up and are available.
/// Otherwise false.
static bool lookupSymbols(HMODULE hGL) {
#define FNFIND(handle, name) \
{ \
if (!o##name) { \
o##name = reinterpret_cast< t##name >(GetProcAddress(handle, #name)); \
if (!o##name) { \
ods("OpenGL: Could not resolve symbol %s in %s", #name, #handle); \
return false; \
} \
} \
}
if (!hGL) {
return false;
}
HMODULE hGDI = GetModuleHandle("GDI32.DLL");
if (!hGDI) {
ods("OpenGL: Failed to identify GDI32");
return false;
}
// Lookup OpenGL32.DLL symbols
FNFIND(hGL, wglCreateContext);
FNFIND(hGL, glGenTextures);
FNFIND(hGL, glDeleteTextures);
FNFIND(hGL, glEnable);
FNFIND(hGL, glDisable);
FNFIND(hGL, glBlendFunc);
FNFIND(hGL, glViewport);
FNFIND(hGL, glMatrixMode);
FNFIND(hGL, glLoadIdentity);
FNFIND(hGL, glOrtho);
FNFIND(hGL, glBindTexture);
FNFIND(hGL, glPushMatrix);
FNFIND(hGL, glBegin);
FNFIND(hGL, glEnd);
FNFIND(hGL, glTexCoord2f);
FNFIND(hGL, glVertex2f);
FNFIND(hGL, glPopMatrix);
FNFIND(hGL, glTexParameteri);
FNFIND(hGL, glTexEnvi);
FNFIND(hGL, glTexImage2D);
FNFIND(hGL, glTexSubImage2D);
FNFIND(hGL, glPixelStorei);
FNFIND(hGL, wglMakeCurrent);
FNFIND(hGL, wglGetCurrentContext);
FNFIND(hGL, wglGetCurrentDC);
// Lookup GDI32.DLL symbols
FNFIND(hGDI, GetDeviceCaps);
return true;
}
void checkOpenGLHook() {
static bool bCheckHookActive = false;
if (bCheckHookActive) {
ods("OpenGL: Recursion in checkOpenGLHook");
return;
}
bCheckHookActive = true;
if (!bHooked) {
HMODULE hGL = GetModuleHandle("OpenGL32.DLL");
if (lookupSymbols(hGL)) {
char procname[1024];
GetModuleFileName(nullptr, procname, 1024);
ods("OpenGL: Hooking into OpenGL App %s", procname);
// Add a ref to ourselves; we do NOT want to get unloaded directly from this process.
HMODULE hTempSelf = nullptr;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, reinterpret_cast< LPCTSTR >(&checkOpenGLHook),
&hTempSelf);
#define INJECT(handle, name) \
{ \
o##name = reinterpret_cast< t##name >(GetProcAddress(handle, #name)); \
if (o##name) { \
hh##name.setup(reinterpret_cast< voidFunc >(o##name), reinterpret_cast< voidFunc >(my##name)); \
o##name = (t##name) hh##name.call; \
} else { \
ods("OpenGL: Could not resolve symbol %s in %s", #name, #handle); \
} \
}
INJECT(hGL, wglSwapBuffers);
}
} else {
hhwglSwapBuffers.check();
}
bCheckHookActive = false;
}
|