/
egl_hooks.cpp
1163 lines (931 loc) · 40 KB
/
egl_hooks.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2024 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "core/settings.h"
#include "hooks/hooks.h"
#include "strings/string_utils.h"
#include "egl_dispatch_table.h"
#include "gl_driver.h"
RDOC_CONFIG(bool, Android_AllowAllEGLExtensions, false,
"Normally certain extensions are removed from the EGL extension string for "
"compatibility, but with this option that behaviour can be overridden and all "
"extensions will be reported.");
#if ENABLED(RDOC_POSIX)
#include <dlfcn.h>
// default to RTLD_NEXT for EGL lookups if we haven't gotten a more specific library handle
#define DEFAULT_HANDLE RTLD_NEXT
#else
#define DEFAULT_HANDLE NULL
#endif
#if ENABLED(RDOC_LINUX)
namespace Keyboard
{
void UseXlibDisplay(Display *dpy);
WindowingSystem UseUnknownDisplay(void *disp);
void UseWaylandDisplay(wl_display *disp);
}
#endif
struct SurfaceConfig
{
WindowingSystem system;
void *wnd;
};
struct DisplayConfig
{
WindowingSystem system;
};
class EGLHook : LibraryHook
{
public:
EGLHook() : driver(GetEGLPlatform()) {}
~EGLHook()
{
for(auto it : extStrings)
SAFE_DELETE(it.second);
}
void RegisterHooks();
RDCDriver activeAPI = RDCDriver::OpenGLES;
void *handle = DEFAULT_HANDLE;
WrappedOpenGL driver;
std::set<EGLContext> contexts;
std::map<EGLContext, EGLConfig> configs;
std::map<EGLSurface, SurfaceConfig> windows;
std::map<EGLDisplay, DisplayConfig> displays;
std::map<EGLDisplay, rdcstr *> extStrings;
// indicates we're in a swap function, so don't process the swap any further if we recurse - could
// happen due to driver implementation of one function calling another
bool swapping = false;
bool IsYFlipped(EGLDisplay dpy, EGLSurface surface)
{
const char *extString = EGL.QueryString(dpy, EGL_EXTENSIONS);
if(extString && strstr(extString, "ANGLE_surface_orientation"))
{
// https://github.com/google/angle/blob/master/extensions/EGL_ANGLE_surface_orientation.txt
#define EGL_SURFACE_ORIENTATION_ANGLE 0x33A8
#define EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE 0x0002
int mask = 0;
EGL.QuerySurface(dpy, surface, EGL_SURFACE_ORIENTATION_ANGLE, &mask);
return (mask & EGL_SURFACE_ORIENTATION_INVERT_Y_ANGLE) != 0;
}
return false;
}
void RefreshWindowParameters(const GLWindowingData &data)
{
EGLDisplay display = data.egl_dpy;
EGLContext ctx = data.egl_ctx;
EGLSurface draw = data.egl_wnd;
EGLConfig config = data.egl_cfg;
if(ctx && draw)
{
GLInitParams ¶ms = driver.GetInitParams(data);
int height, width;
EGL.QuerySurface(display, draw, EGL_HEIGHT, &height);
EGL.QuerySurface(display, draw, EGL_WIDTH, &width);
int colorspace = 0;
EGL.QuerySurface(display, draw, EGL_GL_COLORSPACE, &colorspace);
// GL_SRGB8_ALPHA8 is specified as color-renderable, unlike GL_SRGB8.
bool isSRGB = params.colorBits == 32 && colorspace == EGL_GL_COLORSPACE_SRGB;
bool isYFlipped = IsYFlipped(display, draw);
int multiSamples;
EGL.GetConfigAttrib(display, config, EGL_SAMPLES, &multiSamples);
if(multiSamples != 1 && multiSamples != 2 && multiSamples != 4 && multiSamples != 8)
{
multiSamples = 1;
}
params.width = width;
params.height = height;
params.isSRGB = isSRGB;
params.isYFlipped = isYFlipped;
params.multiSamples = multiSamples;
}
}
} eglhook;
// On linux if a user doesn't link to libEGL or try to dlopen it, but just calls dlsym with
// RTLD_NEXT it might successfully one of our functions without anything ever loading libEGL. Then
// our attempts to call onwards will fail. When any of our functions are called we check to see if
// DEFAULT_HANDLE is RTLD_NEXT and if so we manually load the library. This will trigger our hook
// callback and we'll get a specific library handle.
//
// On other platforms this is not needed because we know the real library will be loaded before any
// of our hooks can be called
static void EnsureRealLibraryLoaded()
{
#if ENABLED(RDOC_LINUX)
if(eglhook.handle == DEFAULT_HANDLE)
{
if(!RenderDoc::Inst().IsReplayApp())
RDCLOG("Loading libEGL at the last second");
void *handle = Process::LoadModule("libEGL.so.1");
if(!handle)
handle = Process::LoadModule("libEGL.so");
if(RenderDoc::Inst().IsReplayApp())
eglhook.handle = handle;
}
#endif
}
HOOK_EXPORT EGLDisplay EGLAPIENTRY eglGetDisplay_renderdoc_hooked(EGLNativeDisplayType display)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.GetDisplay)
EGL.PopulateForReplay();
return EGL.GetDisplay(display);
}
EnsureRealLibraryLoaded();
#if ENABLED(RDOC_LINUX)
// display can be EGL_DEFAULT_DISPLAY which is NULL, and unfortunately we don't have anything then
if(display)
Keyboard::UseUnknownDisplay((void *)display);
// if xlib is compiled we can try to get the default display (which is what this will do)
#if ENABLED(RDOC_XLIB)
else
Keyboard::UseUnknownDisplay(XOpenDisplay(NULL));
#endif
#endif
return EGL.GetDisplay(display);
}
HOOK_EXPORT EGLDisplay EGLAPIENTRY eglGetPlatformDisplay_renderdoc_hooked(EGLenum platform,
void *native_display,
const EGLAttrib *attrib_list)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.GetDisplay)
EGL.PopulateForReplay();
return EGL.GetPlatformDisplay(platform, native_display, attrib_list);
}
EnsureRealLibraryLoaded();
#if ENABLED(RDOC_LINUX)
if(platform == EGL_PLATFORM_X11_KHR)
Keyboard::UseXlibDisplay((Display *)native_display);
else if(platform == EGL_PLATFORM_WAYLAND_KHR)
Keyboard::UseWaylandDisplay((wl_display *)native_display);
else
RDCWARN("Unknown platform %x in eglGetPlatformDisplay", platform);
#endif
return EGL.GetPlatformDisplay(platform, native_display, attrib_list);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglBindAPI_renderdoc_hooked(EGLenum api)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.GetDisplay)
EGL.PopulateForReplay();
return EGL.BindAPI(api);
}
EnsureRealLibraryLoaded();
EGLBoolean ret = EGL.BindAPI(api);
if(ret)
eglhook.activeAPI = api == EGL_OPENGL_API ? RDCDriver::OpenGL : RDCDriver::OpenGLES;
return ret;
}
HOOK_EXPORT EGLContext EGLAPIENTRY eglCreateContext_renderdoc_hooked(EGLDisplay display,
EGLConfig config,
EGLContext shareContext,
EGLint const *attribList)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.CreateContext)
EGL.PopulateForReplay();
return EGL.CreateContext(display, config, shareContext, attribList);
}
EnsureRealLibraryLoaded();
LibraryHooks::Refresh();
rdcarray<EGLint> attribs;
// modify attribList to our liking
{
bool flagsFound = false;
if(attribList)
{
const EGLint *ptr = attribList;
for(;;)
{
EGLint name = *ptr++;
if(name == EGL_NONE)
{
break;
}
EGLint value = *ptr++;
if(name == EGL_CONTEXT_FLAGS_KHR)
{
if(RenderDoc::Inst().GetCaptureOptions().apiValidation)
value |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
else
value &= ~EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
// remove NO_ERROR bit
value &= ~GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR;
flagsFound = true;
}
if(name == EGL_CONTEXT_OPENGL_NO_ERROR_KHR)
{
// remove this attribute so that we can be more stable
continue;
}
// remove reset notification attributes, so that we don't have to carry this bit around to
// know how to safely create sharing contexts.
if(name == EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT)
{
continue;
}
attribs.push_back(name);
attribs.push_back(value);
}
}
if(!flagsFound && RenderDoc::Inst().GetCaptureOptions().apiValidation)
{
attribs.push_back(EGL_CONTEXT_FLAGS_KHR);
attribs.push_back(EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR);
}
attribs.push_back(EGL_NONE);
}
RDCDEBUG("eglCreateContext:");
if(attribList)
{
int *a = (int *)attribs.data();
while(*a != EGL_NONE)
{
RDCDEBUG("%x: %d", a[0], a[1]);
a += 2;
}
}
EGLContext ret = EGL.CreateContext(display, config, shareContext, attribs.data());
// don't continue if context creation failed
if(ret == EGL_NO_CONTEXT)
return ret;
GLInitParams init;
init.width = 0;
init.height = 0;
EGLint value;
EGL.GetConfigAttrib(display, config, EGL_BUFFER_SIZE, &value);
init.colorBits = value;
EGL.GetConfigAttrib(display, config, EGL_DEPTH_SIZE, &value);
init.depthBits = value;
EGL.GetConfigAttrib(display, config, EGL_STENCIL_SIZE, &value);
init.stencilBits = value;
// We will set isSRGB when we see the surface.
init.isSRGB = 0;
EGLint rgbSize[3] = {};
EGL.GetConfigAttrib(display, config, EGL_RED_SIZE, &rgbSize[0]);
EGL.GetConfigAttrib(display, config, EGL_GREEN_SIZE, &rgbSize[1]);
EGL.GetConfigAttrib(display, config, EGL_BLUE_SIZE, &rgbSize[2]);
if(rgbSize[0] == rgbSize[1] && rgbSize[1] == rgbSize[2] && rgbSize[2] == 10)
init.colorBits = 10;
GLWindowingData data;
data.egl_dpy = display;
data.wnd = 0;
data.egl_wnd = (EGLSurface)NULL;
data.egl_ctx = ret;
data.egl_cfg = config;
eglhook.configs[ret] = config;
EnableGLHooks();
eglhook.driver.SetDriverType(eglhook.activeAPI);
{
SCOPED_LOCK(glLock);
eglhook.driver.CreateContext(data, shareContext, init, true, true);
}
return ret;
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglDestroyContext_renderdoc_hooked(EGLDisplay dpy, EGLContext ctx)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.DestroyContext)
EGL.PopulateForReplay();
return EGL.DestroyContext(dpy, ctx);
}
EnsureRealLibraryLoaded();
eglhook.driver.SetDriverType(eglhook.activeAPI);
{
SCOPED_LOCK(glLock);
eglhook.driver.DeleteContext(ctx);
eglhook.contexts.erase(ctx);
}
return EGL.DestroyContext(dpy, ctx);
}
HOOK_EXPORT EGLSurface EGLAPIENTRY eglCreateWindowSurface_renderdoc_hooked(EGLDisplay dpy,
EGLConfig config,
EGLNativeWindowType win,
const EGLint *attrib_list)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.CreateWindowSurface)
EGL.PopulateForReplay();
return EGL.CreateWindowSurface(dpy, config, win, attrib_list);
}
EnsureRealLibraryLoaded();
EGLSurface ret = EGL.CreateWindowSurface(dpy, config, win, attrib_list);
if(ret)
{
SCOPED_LOCK(glLock);
// spec says it's implementation dependent what happens, so we assume that we're using the same
// window system as the display
eglhook.windows[ret] = {eglhook.displays[dpy].system, (void *)win};
}
return ret;
}
HOOK_EXPORT EGLSurface EGLAPIENTRY eglCreatePlatformWindowSurface_renderdoc_hooked(
EGLDisplay dpy, EGLConfig config, void *native_window, const EGLAttrib *attrib_list)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.CreatePlatformWindowSurface)
EGL.PopulateForReplay();
return EGL.CreatePlatformWindowSurface(dpy, config, native_window, attrib_list);
}
EnsureRealLibraryLoaded();
EGLSurface ret = EGL.CreatePlatformWindowSurface(dpy, config, native_window, attrib_list);
if(ret)
{
SCOPED_LOCK(glLock);
// spec guarantees that we're using the same window system as the display
eglhook.windows[ret] = {eglhook.displays[dpy].system, native_window};
}
return ret;
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglMakeCurrent_renderdoc_hooked(EGLDisplay display,
EGLSurface draw, EGLSurface read,
EGLContext ctx)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.MakeCurrent || !EGL.GetProcAddress)
EGL.PopulateForReplay();
// populate GL function pointers now in case linked functions are called
if(EGL.GetProcAddress)
{
GL.PopulateWithCallback(
[](const char *funcName) -> void * { return (void *)EGL.GetProcAddress(funcName); });
}
return EGL.MakeCurrent(display, draw, read, ctx);
}
EnsureRealLibraryLoaded();
EGLBoolean ret = EGL.MakeCurrent(display, draw, read, ctx);
if(ret)
{
SCOPED_LOCK(glLock);
SetDriverForHooks(&eglhook.driver);
if(ctx && eglhook.contexts.find(ctx) == eglhook.contexts.end())
{
eglhook.contexts.insert(ctx);
if(FetchEnabledExtensions())
{
// see gl_emulated.cpp
GL.EmulateUnsupportedFunctions();
GL.EmulateRequiredExtensions();
GL.DriverForEmulation(&eglhook.driver);
}
}
SurfaceConfig cfg = eglhook.windows[draw];
GLWindowingData data;
data.egl_dpy = display;
data.egl_wnd = draw;
data.egl_ctx = ctx;
data.wnd = (decltype(data.wnd))cfg.wnd;
if(!data.wnd)
{
// could be a pbuffer surface or other offscreen rendering. We want a valid wnd, so set it to
// a dummy value
data.wnd = (decltype(data.wnd))(void *)(uintptr_t(0xdeadbeef) + uintptr_t(draw));
cfg.system = WindowingSystem::Headless;
}
// we could query this out technically but it's easier to keep a map
data.egl_cfg = eglhook.configs[ctx];
eglhook.driver.SetDriverType(eglhook.activeAPI);
eglhook.RefreshWindowParameters(data);
eglhook.driver.ActivateContext(data);
}
return ret;
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffers_renderdoc_hooked(EGLDisplay dpy, EGLSurface surface)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.SwapBuffers)
EGL.PopulateForReplay();
return EGL.SwapBuffers(dpy, surface);
}
EnsureRealLibraryLoaded();
SCOPED_LOCK(glLock);
eglhook.driver.SetDriverType(eglhook.activeAPI);
if(!eglhook.driver.UsesVRFrameMarkers() && !eglhook.swapping)
{
GLWindowingData data;
data.egl_dpy = dpy;
data.egl_wnd = surface;
data.egl_ctx = EGL.GetCurrentContext();
eglhook.RefreshWindowParameters(data);
SurfaceConfig cfg = eglhook.windows[surface];
gl_CurChunk = GLChunk::eglSwapBuffers;
eglhook.driver.SwapBuffers(cfg.system, cfg.wnd);
}
{
eglhook.swapping = true;
EGLBoolean ret = EGL.SwapBuffers(dpy, surface);
eglhook.swapping = false;
return ret;
}
}
HOOK_EXPORT const char *EGLAPIENTRY eglQueryString_renderdoc_hooked(EGLDisplay dpy, EGLint name)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.QueryString)
EGL.PopulateForReplay();
return EGL.QueryString(dpy, name);
}
EnsureRealLibraryLoaded();
SCOPED_LOCK(glLock);
if(name == EGL_EXTENSIONS && !Android_AllowAllEGLExtensions())
{
rdcstr *extStr = eglhook.extStrings[dpy];
if(extStr == NULL)
extStr = eglhook.extStrings[dpy] = new rdcstr;
const rdcstr implExtStr = EGL.QueryString(dpy, name);
rdcarray<rdcstr> exts;
split(implExtStr, exts, ' ');
// We take the unusual approach here of explicitly _disallowing_ extensions only when we know
// they are unsupported. The main reason for this is because EGL is the android platform API and
// it may well be that undocumented internal or private extensions are important and should not
// be filtered out. Also since we have minimal interaction with the API as long as they don't
// affect the functions we care about for context management and swapping most extensions can be
// allowed silently.
exts.removeOne("EGL_KHR_no_config_context");
merge(exts, *extStr, ' ');
}
return EGL.QueryString(dpy, name);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglPostSubBufferNV_renderdoc_hooked(EGLDisplay dpy,
EGLSurface surface, EGLint x,
EGLint y, EGLint width,
EGLint height)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.PostSubBufferNV)
EGL.PopulateForReplay();
return EGL.PostSubBufferNV(dpy, surface, x, y, width, height);
}
EnsureRealLibraryLoaded();
SCOPED_LOCK(glLock);
eglhook.driver.SetDriverType(eglhook.activeAPI);
if(!eglhook.driver.UsesVRFrameMarkers() && !eglhook.swapping)
{
SurfaceConfig cfg = eglhook.windows[surface];
gl_CurChunk = GLChunk::eglPostSubBufferNV;
eglhook.driver.SwapBuffers(cfg.system, cfg.wnd);
}
{
eglhook.swapping = true;
EGLBoolean ret = EGL.PostSubBufferNV(dpy, surface, x, y, width, height);
eglhook.swapping = false;
return ret;
}
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffersWithDamageEXT_renderdoc_hooked(EGLDisplay dpy,
EGLSurface surface,
EGLint *rects,
EGLint n_rects)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.SwapBuffersWithDamageEXT)
EGL.PopulateForReplay();
return EGL.SwapBuffersWithDamageEXT(dpy, surface, rects, n_rects);
}
EnsureRealLibraryLoaded();
SCOPED_LOCK(glLock);
eglhook.driver.SetDriverType(eglhook.activeAPI);
if(!eglhook.driver.UsesVRFrameMarkers() && !eglhook.swapping)
{
SurfaceConfig cfg = eglhook.windows[surface];
gl_CurChunk = GLChunk::eglSwapBuffersWithDamageEXT;
eglhook.driver.SwapBuffers(cfg.system, cfg.wnd);
}
{
eglhook.swapping = true;
EGLBoolean ret = EGL.SwapBuffersWithDamageEXT(dpy, surface, rects, n_rects);
eglhook.swapping = false;
return ret;
}
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffersWithDamageKHR_renderdoc_hooked(EGLDisplay dpy,
EGLSurface surface,
EGLint *rects,
EGLint n_rects)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.SwapBuffersWithDamageKHR)
EGL.PopulateForReplay();
return EGL.SwapBuffersWithDamageKHR(dpy, surface, rects, n_rects);
}
EnsureRealLibraryLoaded();
SCOPED_LOCK(glLock);
eglhook.driver.SetDriverType(eglhook.activeAPI);
if(!eglhook.driver.UsesVRFrameMarkers() && !eglhook.swapping)
{
SurfaceConfig cfg = eglhook.windows[surface];
gl_CurChunk = GLChunk::eglSwapBuffersWithDamageKHR;
eglhook.driver.SwapBuffers(cfg.system, cfg.wnd);
}
{
eglhook.swapping = true;
EGLBoolean ret = EGL.SwapBuffersWithDamageKHR(dpy, surface, rects, n_rects);
eglhook.swapping = false;
return ret;
}
}
HOOK_EXPORT __eglMustCastToProperFunctionPointerType EGLAPIENTRY
eglGetProcAddress_renderdoc_hooked(const char *func)
{
if(RenderDoc::Inst().IsReplayApp())
{
if(!EGL.GetProcAddress)
EGL.PopulateForReplay();
return EGL.GetProcAddress(func);
}
EnsureRealLibraryLoaded();
__eglMustCastToProperFunctionPointerType realFunc = NULL;
{
ScopedSuppressHooking suppress;
realFunc = EGL.GetProcAddress(func);
}
// if the real context doesn't support this function, and we don't provide an implementation fully
// ourselves, return NULL
if(realFunc == NULL && !FullyImplementedFunction(func))
return realFunc;
// return our egl hooks
#define GPA_FUNCTION(name, isext, replayrequired) \
if(!strcmp(func, "egl" STRINGIZE(name))) \
return (__eglMustCastToProperFunctionPointerType)&CONCAT(egl, CONCAT(name, _renderdoc_hooked));
EGL_HOOKED_SYMBOLS(GPA_FUNCTION)
#undef GPA_FUNCTION
// any other egl functions are safe to pass through unchanged
if(!strncmp(func, "egl", 3))
return realFunc;
// otherwise, consult our database of hooks
return (__eglMustCastToProperFunctionPointerType)HookedGetProcAddress(func, (void *)realFunc);
}
// on posix systems, someone might declare a global variable with the same name as a function. When
// doing this, it might mean that our code for "&eglSwapBuffers" looking up that global symbol will
// instead find the location fo the function pointer instead of our hook function. For this reason
// we always refer to the _renderdoc_hooked name, but we still must export the functions under their
// real names and just forward to the hook implementation.
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api)
{
return eglBindAPI_renderdoc_hooked(api);
}
HOOK_EXPORT EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display)
{
return eglGetDisplay_renderdoc_hooked(display);
}
HOOK_EXPORT EGLDisplay EGLAPIENTRY eglGetPlatformDisplay(EGLenum platform, void *native_display,
const EGLAttrib *attrib_list)
{
return eglGetPlatformDisplay_renderdoc_hooked(platform, native_display, attrib_list);
}
HOOK_EXPORT EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay display, EGLConfig config,
EGLContext shareContext, EGLint const *attribList)
{
return eglCreateContext_renderdoc_hooked(display, config, shareContext, attribList);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
{
return eglDestroyContext_renderdoc_hooked(dpy, ctx);
}
HOOK_EXPORT EGLSurface EGLAPIENTRY eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
EGLNativeWindowType win,
const EGLint *attrib_list)
{
return eglCreateWindowSurface_renderdoc_hooked(dpy, config, win, attrib_list);
}
HOOK_EXPORT EGLSurface EGLAPIENTRY eglCreatePlatformWindowSurface(EGLDisplay dpy, EGLConfig config,
void *native_window,
const EGLAttrib *attrib_list)
{
return eglCreatePlatformWindowSurface_renderdoc_hooked(dpy, config, native_window, attrib_list);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay display, EGLSurface draw,
EGLSurface read, EGLContext ctx)
{
return eglMakeCurrent_renderdoc_hooked(display, draw, read, ctx);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
return eglSwapBuffers_renderdoc_hooked(dpy, surface);
}
HOOK_EXPORT const char *EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name)
{
return eglQueryString_renderdoc_hooked(dpy, name);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglPostSubBufferNV(EGLDisplay dpy, EGLSurface surface, EGLint x,
EGLint y, EGLint width, EGLint height)
{
return eglPostSubBufferNV_renderdoc_hooked(dpy, surface, x, y, width, height);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffersWithDamageEXT(EGLDisplay dpy, EGLSurface surface,
EGLint *rects, EGLint n_rects)
{
return eglSwapBuffersWithDamageEXT_renderdoc_hooked(dpy, surface, rects, n_rects);
}
HOOK_EXPORT EGLBoolean EGLAPIENTRY eglSwapBuffersWithDamageKHR(EGLDisplay dpy, EGLSurface surface,
EGLint *rects, EGLint n_rects)
{
return eglSwapBuffersWithDamageKHR_renderdoc_hooked(dpy, surface, rects, n_rects);
}
HOOK_EXPORT __eglMustCastToProperFunctionPointerType EGLAPIENTRY eglGetProcAddress(const char *func)
{
return eglGetProcAddress_renderdoc_hooked(func);
}
// on posix systems we need to export the whole of the EGL API, since we will have redirected any
// dlopen() for libEGL.so to ourselves, and dlsym() for any of these entry points must return a
// valid function. We don't need to intercept them, so we just pass it along
#define EGL_PASSTHRU_0(ret, function) \
typedef ret (*CONCAT(function, _hooktype))(); \
HOOK_EXPORT ret EGLAPIENTRY function() \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(); \
}
#define EGL_PASSTHRU_1(ret, function, t1, p1) \
typedef ret (*CONCAT(function, _hooktype))(t1); \
HOOK_EXPORT ret EGLAPIENTRY function(t1 p1) \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(p1); \
}
#define EGL_PASSTHRU_2(ret, function, t1, p1, t2, p2) \
typedef ret (*CONCAT(function, _hooktype))(t1, t2); \
HOOK_EXPORT ret EGLAPIENTRY function(t1 p1, t2 p2) \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(p1, p2); \
}
#define EGL_PASSTHRU_3(ret, function, t1, p1, t2, p2, t3, p3) \
typedef ret (*CONCAT(function, _hooktype))(t1, t2, t3); \
HOOK_EXPORT ret EGLAPIENTRY function(t1 p1, t2 p2, t3 p3) \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(p1, p2, p3); \
}
#define EGL_PASSTHRU_4(ret, function, t1, p1, t2, p2, t3, p3, t4, p4) \
typedef ret (*CONCAT(function, _hooktype))(t1, t2, t3, t4); \
HOOK_EXPORT ret EGLAPIENTRY function(t1 p1, t2 p2, t3 p3, t4 p4) \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(p1, p2, p3, p4); \
}
#define EGL_PASSTHRU_5(ret, function, t1, p1, t2, p2, t3, p3, t4, p4, t5, p5) \
typedef ret (*CONCAT(function, _hooktype))(t1, t2, t3, t4, t5); \
HOOK_EXPORT ret EGLAPIENTRY function(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) \
{ \
EnsureRealLibraryLoaded(); \
CONCAT(function, _hooktype) \
real = (CONCAT(function, _hooktype))Process::GetFunctionAddress(eglhook.handle, \
STRINGIZE(function)); \
return real(p1, p2, p3, p4, p5); \
}
/* EGL 1.0 */
EGL_PASSTHRU_5(EGLBoolean, eglChooseConfig, EGLDisplay, dpy, const EGLint *, attrib_list,
EGLConfig *, configs, EGLint, config_size, EGLint *, num_config)
EGL_PASSTHRU_3(EGLBoolean, eglCopyBuffers, EGLDisplay, dpy, EGLSurface, surface,
EGLNativePixmapType, target)
EGL_PASSTHRU_3(EGLSurface, eglCreatePbufferSurface, EGLDisplay, dpy, EGLConfig, config,
const EGLint *, attrib_list)
EGL_PASSTHRU_4(EGLSurface, eglCreatePixmapSurface, EGLDisplay, dpy, EGLConfig, config,
EGLNativePixmapType, pixmap, const EGLint *, attrib_list)
EGL_PASSTHRU_2(EGLBoolean, eglDestroySurface, EGLDisplay, dpy, EGLSurface, surface)
EGL_PASSTHRU_4(EGLBoolean, eglGetConfigAttrib, EGLDisplay, dpy, EGLConfig, config, EGLint,
attribute, EGLint *, value)
EGL_PASSTHRU_4(EGLBoolean, eglGetConfigs, EGLDisplay, dpy, EGLConfig *, configs, EGLint,
config_size, EGLint *, num_config)
EGL_PASSTHRU_0(EGLDisplay, eglGetCurrentDisplay)
EGL_PASSTHRU_1(EGLSurface, eglGetCurrentSurface, EGLint, readdraw)
EGL_PASSTHRU_0(EGLint, eglGetError)
EGL_PASSTHRU_3(EGLBoolean, eglInitialize, EGLDisplay, dpy, EGLint *, major, EGLint *, minor)
EGL_PASSTHRU_4(EGLBoolean, eglQueryContext, EGLDisplay, dpy, EGLContext, ctx, EGLint, attribute,
EGLint *, value)
EGL_PASSTHRU_4(EGLBoolean, eglQuerySurface, EGLDisplay, dpy, EGLSurface, surface, EGLint, attribute,
EGLint *, value)
EGL_PASSTHRU_1(EGLBoolean, eglTerminate, EGLDisplay, dpy)
EGL_PASSTHRU_0(EGLBoolean, eglWaitGL)
EGL_PASSTHRU_1(EGLBoolean, eglWaitNative, EGLint, engine)
/* EGL 1.1 */
EGL_PASSTHRU_3(EGLBoolean, eglBindTexImage, EGLDisplay, dpy, EGLSurface, surface, EGLint, buffer)
EGL_PASSTHRU_3(EGLBoolean, eglReleaseTexImage, EGLDisplay, dpy, EGLSurface, surface, EGLint, buffer)
EGL_PASSTHRU_4(EGLBoolean, eglSurfaceAttrib, EGLDisplay, dpy, EGLSurface, surface, EGLint,
attribute, EGLint, value)
EGL_PASSTHRU_2(EGLBoolean, eglSwapInterval, EGLDisplay, dpy, EGLint, interval)
/* EGL 1.2 */
EGL_PASSTHRU_0(EGLenum, eglQueryAPI)
EGL_PASSTHRU_5(EGLSurface, eglCreatePbufferFromClientBuffer, EGLDisplay, dpy, EGLenum, buftype,
EGLClientBuffer, buffer, EGLConfig, config, const EGLint *, attrib_list)
EGL_PASSTHRU_0(EGLBoolean, eglReleaseThread)
EGL_PASSTHRU_0(EGLBoolean, eglWaitClient)
/* EGL 1.4 */
EGL_PASSTHRU_0(EGLContext, eglGetCurrentContext)
/* EGL 1.5 */
EGL_PASSTHRU_3(EGLSync, eglCreateSync, EGLDisplay, dpy, EGLenum, type, const EGLAttrib *, attrib_list)
EGL_PASSTHRU_2(EGLBoolean, eglDestroySync, EGLDisplay, dpy, EGLSync, sync)
EGL_PASSTHRU_4(EGLint, eglClientWaitSync, EGLDisplay, dpy, EGLSync, sync, EGLint, flags, EGLTime,
timeout)
EGL_PASSTHRU_4(EGLBoolean, eglGetSyncAttrib, EGLDisplay, dpy, EGLSync, sync, EGLint, attribute,
EGLAttrib *, value)
EGL_PASSTHRU_5(EGLImage, eglCreateImage, EGLDisplay, dpy, EGLContext, ctx, EGLenum, target,
EGLClientBuffer, buffer, const EGLAttrib *, attrib_list)
EGL_PASSTHRU_2(EGLBoolean, eglDestroyImage, EGLDisplay, dpy, EGLImage, image)
EGL_PASSTHRU_4(EGLSurface, eglCreatePlatformPixmapSurface, EGLDisplay, dpy, EGLConfig, config,
void *, native_pixmap, const EGLAttrib *, attrib_list)
EGL_PASSTHRU_3(EGLBoolean, eglWaitSync, EGLDisplay, dpy, EGLSync, sync, EGLint, flags)
static void EGLHooked(void *handle)
{
RDCDEBUG("EGL library hooked");
DisableWGLHooksForEGL();
// store the handle for any pass-through implementations that need to look up their onward
// pointers
eglhook.handle = handle;
// as a hook callback this is only called while capturing
RDCASSERT(!RenderDoc::Inst().IsReplayApp());
// fetch non-hooked functions into our dispatch table
#define EGL_FETCH(func, isext, replayrequired) \
EGL.func = (CONCAT(PFN_egl, func))Process::GetFunctionAddress(handle, "egl" STRINGIZE(func)); \
if(!EGL.func && CheckConstParam(isext)) \
EGL.func = (CONCAT(PFN_egl, func))EGL.GetProcAddress("egl" STRINGIZE(func));
EGL_NONHOOKED_SYMBOLS(EGL_FETCH)
#undef EGL_FETCH
// fetch any hooked extension functions into our dispatch table since they're not necessarily
// exported
#define EGL_FETCH(func, isext, replayrequired) \
if(!EGL.func) \
EGL.func = (CONCAT(PFN_egl, func))EGL.GetProcAddress("egl" STRINGIZE(func));
EGL_HOOKED_SYMBOLS(EGL_FETCH)
#undef EGL_FETCH