1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "tools/gpu/gl/angle/GLTestContext_angle.h"
9
10 #define EGL_EGL_PROTOTYPES 1
11
12 #include <EGL/egl.h>
13 #include <EGL/eglext.h>
14
15 #include "src/gpu/gl/GrGLDefines.h"
16 #include "src/gpu/gl/GrGLUtil.h"
17
18 #include "include/core/SkTime.h"
19 #include "include/gpu/gl/GrGLAssembleInterface.h"
20 #include "include/gpu/gl/GrGLInterface.h"
21 #include "src/core/SkTraceEvent.h"
22 #include "src/ports/SkOSLibrary.h"
23 #include "third_party/externals/angle2/include/platform/Platform.h"
24
25 #include <EGL/egl.h>
26
27 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
28 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
29 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207
30 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
31 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D
32
33 using sk_gpu_test::ANGLEBackend;
34 using sk_gpu_test::ANGLEContextVersion;
35
36 namespace {
37 struct Libs {
38 void* fGLLib;
39 void* fEGLLib;
40 };
41
context_restorer()42 std::function<void()> context_restorer() {
43 auto display = eglGetCurrentDisplay();
44 auto dsurface = eglGetCurrentSurface(EGL_DRAW);
45 auto rsurface = eglGetCurrentSurface(EGL_READ);
46 auto context = eglGetCurrentContext();
47 return [display, dsurface, rsurface, context] {
48 eglMakeCurrent(display, dsurface, rsurface, context);
49 };
50 }
51
angle_get_gl_proc(void * ctx,const char name[])52 static GrGLFuncPtr angle_get_gl_proc(void* ctx, const char name[]) {
53 const Libs* libs = reinterpret_cast<const Libs*>(ctx);
54 GrGLFuncPtr proc = (GrGLFuncPtr) GetProcedureAddress(libs->fGLLib, name);
55 if (proc) {
56 return proc;
57 }
58 proc = (GrGLFuncPtr) GetProcedureAddress(libs->fEGLLib, name);
59 if (proc) {
60 return proc;
61 }
62 return eglGetProcAddress(name);
63 }
64
get_angle_egl_display(void * nativeDisplay,ANGLEBackend type)65 void* get_angle_egl_display(void* nativeDisplay, ANGLEBackend type) {
66 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT;
67 eglGetPlatformDisplayEXT =
68 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
69
70 // We expect ANGLE to support this extension
71 if (!eglGetPlatformDisplayEXT) {
72 return EGL_NO_DISPLAY;
73 }
74
75 EGLint typeNum = 0;
76 switch (type) {
77 case ANGLEBackend::kD3D9:
78 typeNum = EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE;
79 break;
80 case ANGLEBackend::kD3D11:
81 typeNum = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
82 break;
83 case ANGLEBackend::kOpenGL:
84 typeNum = EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE;
85 break;
86 }
87 const EGLint attribs[] = { EGL_PLATFORM_ANGLE_TYPE_ANGLE, typeNum, EGL_NONE };
88 return eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDisplay, attribs);
89 }
90
91 class ANGLEGLContext : public sk_gpu_test::GLTestContext {
92 public:
93 ANGLEGLContext(ANGLEBackend, ANGLEContextVersion, ANGLEGLContext* shareContext, void* display);
94 ~ANGLEGLContext() override;
95
96 GrEGLImage texture2DToEGLImage(GrGLuint texID) const override;
97 void destroyEGLImage(GrEGLImage) const override;
98 GrGLuint eglImageToExternalTexture(GrEGLImage) const override;
99 std::unique_ptr<sk_gpu_test::GLTestContext> makeNew() const override;
100
101 private:
102 void destroyGLContext();
103
104 void onPlatformMakeNotCurrent() const override;
105 void onPlatformMakeCurrent() const override;
106 std::function<void()> onPlatformGetAutoContextRestore() const override;
107 void onPlatformSwapBuffers() const override;
108 GrGLFuncPtr onPlatformGetProcAddress(const char* name) const override;
109
110 void* fContext;
111 void* fDisplay;
112 void* fSurface;
113 ANGLEBackend fType;
114 ANGLEContextVersion fVersion;
115
116 angle::ResetDisplayPlatformFunc fResetPlatform = nullptr;
117
118 PFNEGLCREATEIMAGEKHRPROC fCreateImage = nullptr;
119 PFNEGLDESTROYIMAGEKHRPROC fDestroyImage = nullptr;
120
121 #ifdef SK_BUILD_FOR_WIN
122 HWND fWindow;
123 HDC fDeviceContext;
124 static ATOM gWC;
125 #endif
126 };
127
128 #ifdef SK_BUILD_FOR_WIN
129 ATOM ANGLEGLContext::gWC = 0;
130
131 enum class IsWine { kUnknown, kNo, kYes };
132
is_wine()133 static IsWine is_wine() {
134 HMODULE ntdll = GetModuleHandle("ntdll.dll");
135 if (!ntdll) {
136 SkDebugf("No ntdll.dll on Windows?!\n");
137 return IsWine::kUnknown;
138 }
139 return GetProcAddress(ntdll, "wine_get_version") == nullptr ? IsWine::kNo : IsWine::kYes;
140 }
141
142 #endif
143
ANGLE_getTraceCategoryEnabledFlag(angle::PlatformMethods * platform,const char * category_group)144 static const unsigned char* ANGLE_getTraceCategoryEnabledFlag(angle::PlatformMethods* platform,
145 const char* category_group) {
146 return SkEventTracer::GetInstance()->getCategoryGroupEnabled(category_group);
147 }
148
ANGLE_addTraceEvent(angle::PlatformMethods * platform,char phase,const unsigned char * category_group_enabled,const char * name,unsigned long long id,double timestamp,int num_args,const char ** arg_names,const unsigned char * arg_types,const unsigned long long * arg_values,unsigned char flags)149 static angle::TraceEventHandle ANGLE_addTraceEvent(angle::PlatformMethods* platform,
150 char phase,
151 const unsigned char* category_group_enabled,
152 const char* name,
153 unsigned long long id,
154 double timestamp,
155 int num_args,
156 const char** arg_names,
157 const unsigned char* arg_types,
158 const unsigned long long* arg_values,
159 unsigned char flags) {
160 static_assert(sizeof(unsigned long long) == sizeof(uint64_t), "Non-64-bit trace event args!");
161 return SkEventTracer::GetInstance()->addTraceEvent(
162 phase, category_group_enabled, name, id, num_args, arg_names, arg_types,
163 reinterpret_cast<const uint64_t*>(arg_values), flags);
164 }
165
ANGLE_updateTraceEventDuration(angle::PlatformMethods * platform,const unsigned char * category_group_enabled,const char * name,angle::TraceEventHandle handle)166 static void ANGLE_updateTraceEventDuration(angle::PlatformMethods* platform,
167 const unsigned char* category_group_enabled,
168 const char* name,
169 angle::TraceEventHandle handle) {
170 SkEventTracer::GetInstance()->updateTraceEventDuration(category_group_enabled, name, handle);
171 }
172
ANGLE_monotonicallyIncreasingTime(angle::PlatformMethods * platform)173 static double ANGLE_monotonicallyIncreasingTime(angle::PlatformMethods* platform) {
174 return SkTime::GetSecs();
175 }
176
ANGLEGLContext(ANGLEBackend type,ANGLEContextVersion version,ANGLEGLContext * shareContext,void * display)177 ANGLEGLContext::ANGLEGLContext(ANGLEBackend type, ANGLEContextVersion version,
178 ANGLEGLContext* shareContext, void* display)
179 : fContext(EGL_NO_CONTEXT)
180 , fDisplay(display)
181 , fSurface(EGL_NO_SURFACE)
182 , fType(type)
183 , fVersion(version) {
184 #ifdef SK_BUILD_FOR_WIN
185 fWindow = nullptr;
186 fDeviceContext = nullptr;
187
188 static IsWine gIsWine = is_wine();
189 if (gIsWine == IsWine::kYes && type != ANGLEBackend::kOpenGL) {
190 // D3D backends of ANGLE don't really work well under Wine with our tests and are likely to
191 // crash. This makes it easier to test using the GL ANGLE backend under Wine on Linux
192 // without lots of spurious Wine debug spew and crashes.
193 return;
194 }
195
196 if (EGL_NO_DISPLAY == fDisplay) {
197 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(nullptr);
198
199 if (!gWC) {
200 WNDCLASS wc;
201 wc.cbClsExtra = 0;
202 wc.cbWndExtra = 0;
203 wc.hbrBackground = nullptr;
204 wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
205 wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
206 wc.hInstance = hInstance;
207 wc.lpfnWndProc = (WNDPROC) DefWindowProc;
208 wc.lpszClassName = TEXT("ANGLE-win");
209 wc.lpszMenuName = nullptr;
210 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
211
212 gWC = RegisterClass(&wc);
213 if (!gWC) {
214 SkDebugf("Could not register window class.\n");
215 return;
216 }
217 }
218 if (!(fWindow = CreateWindow(TEXT("ANGLE-win"),
219 TEXT("The Invisible Man"),
220 WS_OVERLAPPEDWINDOW,
221 0, 0, 1, 1,
222 nullptr, nullptr,
223 hInstance, nullptr))) {
224 SkDebugf("Could not create window.\n");
225 return;
226 }
227
228 if (!(fDeviceContext = GetDC(fWindow))) {
229 SkDebugf("Could not get device context.\n");
230 this->destroyGLContext();
231 return;
232 }
233
234 fDisplay = get_angle_egl_display(fDeviceContext, type);
235 }
236 #else
237 SkASSERT(EGL_NO_DISPLAY == fDisplay);
238 fDisplay = get_angle_egl_display(EGL_DEFAULT_DISPLAY, type);
239 #endif
240 if (EGL_NO_DISPLAY == fDisplay) {
241 SkDebugf("Could not create EGL display!");
242 return;
243 }
244
245 // Add ANGLE platform hooks to connect to Skia's tracing implementation
246 angle::GetDisplayPlatformFunc getPlatform = reinterpret_cast<angle::GetDisplayPlatformFunc>(
247 eglGetProcAddress("ANGLEGetDisplayPlatform"));
248 if (getPlatform) {
249 fResetPlatform = reinterpret_cast<angle::ResetDisplayPlatformFunc>(
250 eglGetProcAddress("ANGLEResetDisplayPlatform"));
251 SkASSERT(fResetPlatform);
252
253 angle::PlatformMethods* platformMethods = nullptr;
254 if (getPlatform(fDisplay, angle::g_PlatformMethodNames, angle::g_NumPlatformMethods,
255 nullptr, &platformMethods)) {
256 platformMethods->addTraceEvent = ANGLE_addTraceEvent;
257 platformMethods->getTraceCategoryEnabledFlag = ANGLE_getTraceCategoryEnabledFlag;
258 platformMethods->updateTraceEventDuration = ANGLE_updateTraceEventDuration;
259 platformMethods->monotonicallyIncreasingTime = ANGLE_monotonicallyIncreasingTime;
260 }
261 }
262
263 EGLint majorVersion;
264 EGLint minorVersion;
265 if (!eglInitialize(fDisplay, &majorVersion, &minorVersion)) {
266 SkDebugf("Could not initialize display!");
267 this->destroyGLContext();
268 return;
269 }
270
271 EGLint numConfigs;
272 static const EGLint configAttribs[] = {
273 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
274 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
275 EGL_RED_SIZE, 8,
276 EGL_GREEN_SIZE, 8,
277 EGL_BLUE_SIZE, 8,
278 EGL_ALPHA_SIZE, 8,
279 EGL_NONE
280 };
281
282 EGLConfig surfaceConfig;
283 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
284 SkDebugf("Could not create choose config!");
285 this->destroyGLContext();
286 return;
287 }
288
289 int versionNum = ANGLEContextVersion::kES2 == version ? 2 : 3;
290 const EGLint contextAttribs[] = {
291 EGL_CONTEXT_CLIENT_VERSION, versionNum,
292 EGL_NONE
293 };
294 EGLContext eglShareContext = shareContext ? shareContext->fContext : nullptr;
295 fContext = eglCreateContext(fDisplay, surfaceConfig, eglShareContext, contextAttribs);
296 if (EGL_NO_CONTEXT == fContext) {
297 SkDebugf("Could not create context!");
298 this->destroyGLContext();
299 return;
300 }
301
302 static const EGLint surfaceAttribs[] = {
303 EGL_WIDTH, 1,
304 EGL_HEIGHT, 1,
305 EGL_NONE
306 };
307
308 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
309
310 SkScopeExit restorer(context_restorer());
311 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
312 SkDebugf("Could not set the context.");
313 this->destroyGLContext();
314 return;
315 }
316
317 sk_sp<const GrGLInterface> gl = sk_gpu_test::CreateANGLEGLInterface();
318 if (nullptr == gl.get()) {
319 SkDebugf("Could not create ANGLE GL interface!\n");
320 this->destroyGLContext();
321 return;
322 }
323 if (!gl->validate()) {
324 SkDebugf("Could not validate ANGLE GL interface!\n");
325 this->destroyGLContext();
326 return;
327 }
328
329 #ifdef SK_DEBUG
330 // Verify that the interface we requested was actually returned to us
331 const GrGLubyte* rendererUByte;
332 GR_GL_CALL_RET(gl.get(), rendererUByte, GetString(GR_GL_RENDERER));
333 const char* renderer = reinterpret_cast<const char*>(rendererUByte);
334 switch (type) {
335 case ANGLEBackend::kD3D9:
336 SkASSERT(strstr(renderer, "Direct3D9"));
337 break;
338 case ANGLEBackend::kD3D11:
339 SkASSERT(strstr(renderer, "Direct3D11"));
340 break;
341 case ANGLEBackend::kOpenGL:
342 SkASSERT(strstr(renderer, "OpenGL"));
343 break;
344 }
345 #endif
346 const char* extensions = eglQueryString(fDisplay, EGL_EXTENSIONS);
347 if (strstr(extensions, "EGL_KHR_image")) {
348 fCreateImage = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
349 fDestroyImage = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
350 }
351
352 this->init(std::move(gl));
353 }
354
~ANGLEGLContext()355 ANGLEGLContext::~ANGLEGLContext() {
356 this->teardown();
357 this->destroyGLContext();
358 }
359
texture2DToEGLImage(GrGLuint texID) const360 GrEGLImage ANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const {
361 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
362 return GR_EGL_NO_IMAGE;
363 }
364 EGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0,
365 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE,
366 GR_EGL_NONE };
367 // 64 bit cast is to shut Visual C++ up about casting 32 bit value to a pointer.
368 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64_t)texID);
369 return fCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clientBuffer, attribs);
370 }
371
destroyEGLImage(GrEGLImage image) const372 void ANGLEGLContext::destroyEGLImage(GrEGLImage image) const { fDestroyImage(fDisplay, image); }
373
eglImageToExternalTexture(GrEGLImage image) const374 GrGLuint ANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const {
375 GrGLClearErr(this->gl());
376 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
377 return 0;
378 }
379 typedef GrGLvoid (EGLAPIENTRY *EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
380 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
381 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2DOES");
382 if (!glEGLImageTargetTexture2D) {
383 return 0;
384 }
385 GrGLuint texID;
386 GR_GL_CALL(this->gl(), GenTextures(1, &texID));
387 if (!texID) {
388 return 0;
389 }
390 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID));
391 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
392 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
393 return 0;
394 }
395 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
396 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
397 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
398 return 0;
399 }
400 return texID;
401 }
402
makeNew() const403 std::unique_ptr<sk_gpu_test::GLTestContext> ANGLEGLContext::makeNew() const {
404 // For EGLImage sharing between contexts to work in ANGLE the two contexts
405 // need to share the same display
406 std::unique_ptr<sk_gpu_test::GLTestContext> ctx =
407 sk_gpu_test::MakeANGLETestContext(fType, fVersion, nullptr, fDisplay);
408 if (ctx) {
409 ctx->makeCurrent();
410 }
411 return ctx;
412 }
413
destroyGLContext()414 void ANGLEGLContext::destroyGLContext() {
415 if (EGL_NO_DISPLAY != fDisplay) {
416 if (eglGetCurrentContext() == fContext) {
417 // This will ensure that the context is immediately deleted.
418 eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
419 }
420
421 if (EGL_NO_CONTEXT != fContext) {
422 eglDestroyContext(fDisplay, fContext);
423 fContext = EGL_NO_CONTEXT;
424 }
425
426 if (EGL_NO_SURFACE != fSurface) {
427 eglDestroySurface(fDisplay, fSurface);
428 fSurface = EGL_NO_SURFACE;
429 }
430
431 if (fResetPlatform) {
432 fResetPlatform(fDisplay);
433 }
434
435 eglTerminate(fDisplay);
436 fDisplay = EGL_NO_DISPLAY;
437 }
438
439 #ifdef SK_BUILD_FOR_WIN
440 if (fWindow) {
441 if (fDeviceContext) {
442 ReleaseDC(fWindow, fDeviceContext);
443 fDeviceContext = 0;
444 }
445
446 DestroyWindow(fWindow);
447 fWindow = 0;
448 }
449 #endif
450 }
451
onPlatformMakeNotCurrent() const452 void ANGLEGLContext::onPlatformMakeNotCurrent() const {
453 if (!eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
454 SkDebugf("Could not reset the context 0x%x.\n", eglGetError());
455 }
456 }
457
onPlatformMakeCurrent() const458 void ANGLEGLContext::onPlatformMakeCurrent() const {
459 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
460 SkDebugf("Could not set the context 0x%x.\n", eglGetError());
461 }
462 }
463
onPlatformGetAutoContextRestore() const464 std::function<void()> ANGLEGLContext::onPlatformGetAutoContextRestore() const {
465 if (eglGetCurrentContext() == fContext) {
466 return nullptr;
467 }
468 return context_restorer();
469 }
470
onPlatformSwapBuffers() const471 void ANGLEGLContext::onPlatformSwapBuffers() const {
472 if (!eglSwapBuffers(fDisplay, fSurface)) {
473 SkDebugf("Could not complete eglSwapBuffers.\n");
474 }
475 }
476
onPlatformGetProcAddress(const char * name) const477 GrGLFuncPtr ANGLEGLContext::onPlatformGetProcAddress(const char* name) const {
478 return eglGetProcAddress(name);
479 }
480 } // anonymous namespace
481
482 namespace sk_gpu_test {
CreateANGLEGLInterface()483 sk_sp<const GrGLInterface> CreateANGLEGLInterface() {
484 static Libs gLibs = { nullptr, nullptr };
485
486 if (nullptr == gLibs.fGLLib) {
487 // We load the ANGLE library and never let it go
488 #if defined _WIN32
489 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.dll");
490 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.dll");
491 #elif defined SK_BUILD_FOR_MAC
492 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.dylib");
493 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.dylib");
494 #else
495 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.so");
496 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.so");
497 #endif
498 }
499
500 if (nullptr == gLibs.fGLLib || nullptr == gLibs.fEGLLib) {
501 // We can't setup the interface correctly w/o the so
502 return nullptr;
503 }
504
505 return GrGLMakeAssembledGLESInterface(&gLibs, angle_get_gl_proc);
506 }
507
MakeANGLETestContext(ANGLEBackend type,ANGLEContextVersion version,GLTestContext * shareContext,void * display)508 std::unique_ptr<GLTestContext> MakeANGLETestContext(ANGLEBackend type, ANGLEContextVersion version,
509 GLTestContext* shareContext, void* display) {
510 #if defined(SK_BUILD_FOR_WIN) && defined(_M_ARM64)
511 // Windows-on-ARM only has D3D11. This will fail correctly, but it produces huge amounts of
512 // debug output for every unit test from both ANGLE and our context factory.
513 if (ANGLEBackend::kD3D11 != type) {
514 return nullptr;
515 }
516 #endif
517
518 ANGLEGLContext* angleShareContext = reinterpret_cast<ANGLEGLContext*>(shareContext);
519 std::unique_ptr<GLTestContext> ctx(new ANGLEGLContext(type, version,
520 angleShareContext, display));
521 if (!ctx->isValid()) {
522 return nullptr;
523 }
524 return ctx;
525 }
526 } // namespace sk_gpu_test
527