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 #include "include/core/SkTime.h"
11 #include "include/gpu/gl/GrGLAssembleInterface.h"
12 #include "include/gpu/gl/GrGLInterface.h"
13 #include "src/core/SkTraceEvent.h"
14 #include "src/gpu/gl/GrGLDefines.h"
15 #include "src/gpu/gl/GrGLUtil.h"
16 #include "src/ports/SkOSLibrary.h"
17 #include "third_party/externals/angle2/include/platform/Platform.h"
18
19 #include <vector>
20
21 #define EGL_EGL_PROTOTYPES 1
22 #include <EGL/egl.h>
23 #include <EGL/eglext.h>
24
25 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
26 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
27 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207
28 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
29 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D
30
31 #define EGL_CONTEXT_OPENGL_BACKWARDS_COMPATIBLE_ANGLE 0x3483
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) SkGetProcedureAddress(libs->fGLLib, name);
55 if (proc) {
56 return proc;
57 }
58 proc = (GrGLFuncPtr) SkGetProcedureAddress(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 GrGLFuncPtr onPlatformGetProcAddress(const char* name) const override;
108
109 void* fContext;
110 void* fDisplay;
111 void* fSurface;
112 ANGLEBackend fType;
113 ANGLEContextVersion fVersion;
114
115 angle::ResetDisplayPlatformFunc fResetPlatform = nullptr;
116
117 PFNEGLCREATEIMAGEKHRPROC fCreateImage = nullptr;
118 PFNEGLDESTROYIMAGEKHRPROC fDestroyImage = nullptr;
119
120 #ifdef SK_BUILD_FOR_WIN
121 HWND fWindow;
122 HDC fDeviceContext;
123 static ATOM gWC;
124 #endif
125 };
126
127 #ifdef SK_BUILD_FOR_WIN
128 ATOM ANGLEGLContext::gWC = 0;
129
130 enum class IsWine { kUnknown, kNo, kYes };
131
is_wine()132 static IsWine is_wine() {
133 HMODULE ntdll = GetModuleHandle("ntdll.dll");
134 if (!ntdll) {
135 SkDebugf("No ntdll.dll on Windows?!\n");
136 return IsWine::kUnknown;
137 }
138 return GetProcAddress(ntdll, "wine_get_version") == nullptr ? IsWine::kNo : IsWine::kYes;
139 }
140
141 #endif
142
ANGLE_getTraceCategoryEnabledFlag(angle::PlatformMethods * platform,const char * category_group)143 static const unsigned char* ANGLE_getTraceCategoryEnabledFlag(angle::PlatformMethods* platform,
144 const char* category_group) {
145 return SkEventTracer::GetInstance()->getCategoryGroupEnabled(category_group);
146 }
147
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)148 static angle::TraceEventHandle ANGLE_addTraceEvent(angle::PlatformMethods* platform,
149 char phase,
150 const unsigned char* category_group_enabled,
151 const char* name,
152 unsigned long long id,
153 double timestamp,
154 int num_args,
155 const char** arg_names,
156 const unsigned char* arg_types,
157 const unsigned long long* arg_values,
158 unsigned char flags) {
159 static_assert(sizeof(unsigned long long) == sizeof(uint64_t), "Non-64-bit trace event args!");
160 return SkEventTracer::GetInstance()->addTraceEvent(
161 phase, category_group_enabled, name, id, num_args, arg_names, arg_types,
162 reinterpret_cast<const uint64_t*>(arg_values), flags);
163 }
164
ANGLE_updateTraceEventDuration(angle::PlatformMethods * platform,const unsigned char * category_group_enabled,const char * name,angle::TraceEventHandle handle)165 static void ANGLE_updateTraceEventDuration(angle::PlatformMethods* platform,
166 const unsigned char* category_group_enabled,
167 const char* name,
168 angle::TraceEventHandle handle) {
169 SkEventTracer::GetInstance()->updateTraceEventDuration(category_group_enabled, name, handle);
170 }
171
ANGLE_monotonicallyIncreasingTime(angle::PlatformMethods * platform)172 static double ANGLE_monotonicallyIncreasingTime(angle::PlatformMethods* platform) {
173 return SkTime::GetSecs();
174 }
175
ANGLEGLContext(ANGLEBackend type,ANGLEContextVersion version,ANGLEGLContext * shareContext,void * display)176 ANGLEGLContext::ANGLEGLContext(ANGLEBackend type, ANGLEContextVersion version,
177 ANGLEGLContext* shareContext, void* display)
178 : fContext(EGL_NO_CONTEXT)
179 , fDisplay(display)
180 , fSurface(EGL_NO_SURFACE)
181 , fType(type)
182 , fVersion(version) {
183 #ifdef SK_BUILD_FOR_WIN
184 fWindow = nullptr;
185 fDeviceContext = nullptr;
186
187 static IsWine gIsWine = is_wine();
188 if (gIsWine == IsWine::kYes && type != ANGLEBackend::kOpenGL) {
189 // D3D backends of ANGLE don't really work well under Wine with our tests and are likely to
190 // crash. This makes it easier to test using the GL ANGLE backend under Wine on Linux
191 // without lots of spurious Wine debug spew and crashes.
192 return;
193 }
194
195 if (EGL_NO_DISPLAY == fDisplay) {
196 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(nullptr);
197
198 if (!gWC) {
199 WNDCLASS wc;
200 wc.cbClsExtra = 0;
201 wc.cbWndExtra = 0;
202 wc.hbrBackground = nullptr;
203 wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
204 wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
205 wc.hInstance = hInstance;
206 wc.lpfnWndProc = (WNDPROC) DefWindowProc;
207 wc.lpszClassName = TEXT("ANGLE-win");
208 wc.lpszMenuName = nullptr;
209 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
210
211 gWC = RegisterClass(&wc);
212 if (!gWC) {
213 SkDebugf("Could not register window class.\n");
214 return;
215 }
216 }
217 if (!(fWindow = CreateWindow(TEXT("ANGLE-win"),
218 TEXT("The Invisible Man"),
219 WS_OVERLAPPEDWINDOW,
220 0, 0, 1, 1,
221 nullptr, nullptr,
222 hInstance, nullptr))) {
223 SkDebugf("Could not create window.\n");
224 return;
225 }
226
227 if (!(fDeviceContext = GetDC(fWindow))) {
228 SkDebugf("Could not get device context.\n");
229 this->destroyGLContext();
230 return;
231 }
232
233 fDisplay = get_angle_egl_display(fDeviceContext, type);
234 }
235 #else
236 SkASSERT(EGL_NO_DISPLAY == fDisplay);
237 fDisplay = get_angle_egl_display(EGL_DEFAULT_DISPLAY, type);
238 #endif
239 if (EGL_NO_DISPLAY == fDisplay) {
240 SkDebugf("Could not create EGL display!");
241 return;
242 }
243
244 // Add ANGLE platform hooks to connect to Skia's tracing implementation
245 angle::GetDisplayPlatformFunc getPlatform = reinterpret_cast<angle::GetDisplayPlatformFunc>(
246 eglGetProcAddress("ANGLEGetDisplayPlatform"));
247 if (getPlatform) {
248 fResetPlatform = reinterpret_cast<angle::ResetDisplayPlatformFunc>(
249 eglGetProcAddress("ANGLEResetDisplayPlatform"));
250 SkASSERT(fResetPlatform);
251
252 angle::PlatformMethods* platformMethods = nullptr;
253 if (getPlatform(fDisplay, angle::g_PlatformMethodNames, angle::g_NumPlatformMethods,
254 nullptr, &platformMethods)) {
255 platformMethods->addTraceEvent = ANGLE_addTraceEvent;
256 platformMethods->getTraceCategoryEnabledFlag = ANGLE_getTraceCategoryEnabledFlag;
257 platformMethods->updateTraceEventDuration = ANGLE_updateTraceEventDuration;
258 platformMethods->monotonicallyIncreasingTime = ANGLE_monotonicallyIncreasingTime;
259 }
260 }
261
262 EGLint majorVersion;
263 EGLint minorVersion;
264 if (!eglInitialize(fDisplay, &majorVersion, &minorVersion)) {
265 SkDebugf("Could not initialize display!");
266 this->destroyGLContext();
267 return;
268 }
269
270 EGLint numConfigs;
271 static const EGLint configAttribs[] = {
272 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
273 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
274 EGL_RED_SIZE, 8,
275 EGL_GREEN_SIZE, 8,
276 EGL_BLUE_SIZE, 8,
277 EGL_ALPHA_SIZE, 8,
278 EGL_NONE
279 };
280
281 EGLConfig surfaceConfig;
282 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
283 SkDebugf("Could not create choose config!");
284 this->destroyGLContext();
285 return;
286 }
287
288 int versionNum = ANGLEContextVersion::kES2 == version ? 2 : 3;
289 std::vector<EGLint> contextAttribs = {
290 EGL_CONTEXT_CLIENT_VERSION, versionNum,
291 };
292
293 const char* extensions = eglQueryString(fDisplay, EGL_EXTENSIONS);
294 if (strstr(extensions, "EGL_ANGLE_create_context_backwards_compatible")) {
295 contextAttribs.push_back(EGL_CONTEXT_OPENGL_BACKWARDS_COMPATIBLE_ANGLE);
296 contextAttribs.push_back(EGL_FALSE);
297 }
298
299 contextAttribs.push_back(EGL_NONE);
300
301 EGLContext eglShareContext = shareContext ? shareContext->fContext : nullptr;
302 fContext = eglCreateContext(fDisplay, surfaceConfig, eglShareContext, contextAttribs.data());
303 if (EGL_NO_CONTEXT == fContext) {
304 SkDebugf("Could not create context!");
305 this->destroyGLContext();
306 return;
307 }
308
309 static const EGLint surfaceAttribs[] = {
310 EGL_WIDTH, 1,
311 EGL_HEIGHT, 1,
312 EGL_NONE
313 };
314
315 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
316
317 SkScopeExit restorer(context_restorer());
318 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
319 SkDebugf("Could not set the context.");
320 this->destroyGLContext();
321 return;
322 }
323
324 sk_sp<const GrGLInterface> gl = sk_gpu_test::CreateANGLEGLInterface();
325 if (nullptr == gl.get()) {
326 SkDebugf("Could not create ANGLE GL interface!\n");
327 this->destroyGLContext();
328 return;
329 }
330 if (!gl->validate()) {
331 SkDebugf("Could not validate ANGLE GL interface!\n");
332 this->destroyGLContext();
333 return;
334 }
335
336 #ifdef SK_DEBUG
337 // Verify that the interface we requested was actually returned to us
338 const GrGLubyte* rendererUByte;
339 GR_GL_CALL_RET(gl.get(), rendererUByte, GetString(GR_GL_RENDERER));
340 const char* renderer = reinterpret_cast<const char*>(rendererUByte);
341 switch (type) {
342 case ANGLEBackend::kD3D9:
343 SkASSERT(strstr(renderer, "Direct3D9"));
344 break;
345 case ANGLEBackend::kD3D11:
346 SkASSERT(strstr(renderer, "Direct3D11"));
347 break;
348 case ANGLEBackend::kOpenGL:
349 SkASSERT(strstr(renderer, "OpenGL"));
350 break;
351 }
352 #endif
353 if (strstr(extensions, "EGL_KHR_image")) {
354 fCreateImage = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
355 fDestroyImage = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");
356 }
357
358 this->init(std::move(gl));
359 }
360
~ANGLEGLContext()361 ANGLEGLContext::~ANGLEGLContext() {
362 this->teardown();
363 this->destroyGLContext();
364 }
365
texture2DToEGLImage(GrGLuint texID) const366 GrEGLImage ANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const {
367 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
368 return GR_EGL_NO_IMAGE;
369 }
370 EGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0,
371 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE,
372 GR_EGL_NONE };
373 // 64 bit cast is to shut Visual C++ up about casting 32 bit value to a pointer.
374 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64_t)texID);
375 return fCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clientBuffer, attribs);
376 }
377
destroyEGLImage(GrEGLImage image) const378 void ANGLEGLContext::destroyEGLImage(GrEGLImage image) const { fDestroyImage(fDisplay, image); }
379
eglImageToExternalTexture(GrEGLImage image) const380 GrGLuint ANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const {
381 while (this->gl()->fFunctions.fGetError() != GR_GL_NO_ERROR) {}
382 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
383 return 0;
384 }
385 typedef GrGLvoid (EGLAPIENTRY *EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
386 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
387 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2DOES");
388 if (!glEGLImageTargetTexture2D) {
389 return 0;
390 }
391 GrGLuint texID;
392 GR_GL_CALL(this->gl(), GenTextures(1, &texID));
393 if (!texID) {
394 return 0;
395 }
396 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID));
397 if (this->gl()->fFunctions.fGetError() != GR_GL_NO_ERROR) {
398 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
399 return 0;
400 }
401 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
402 if (this->gl()->fFunctions.fGetError() != GR_GL_NO_ERROR) {
403 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
404 return 0;
405 }
406 return texID;
407 }
408
makeNew() const409 std::unique_ptr<sk_gpu_test::GLTestContext> ANGLEGLContext::makeNew() const {
410 // For EGLImage sharing between contexts to work in ANGLE the two contexts
411 // need to share the same display
412 std::unique_ptr<sk_gpu_test::GLTestContext> ctx =
413 sk_gpu_test::MakeANGLETestContext(fType, fVersion, nullptr, fDisplay);
414 if (ctx) {
415 ctx->makeCurrent();
416 }
417 return ctx;
418 }
419
destroyGLContext()420 void ANGLEGLContext::destroyGLContext() {
421 if (EGL_NO_DISPLAY != fDisplay) {
422 if (eglGetCurrentContext() == fContext) {
423 // This will ensure that the context is immediately deleted.
424 eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
425 }
426
427 if (EGL_NO_CONTEXT != fContext) {
428 eglDestroyContext(fDisplay, fContext);
429 fContext = EGL_NO_CONTEXT;
430 }
431
432 if (EGL_NO_SURFACE != fSurface) {
433 eglDestroySurface(fDisplay, fSurface);
434 fSurface = EGL_NO_SURFACE;
435 }
436
437 if (fResetPlatform) {
438 fResetPlatform(fDisplay);
439 }
440
441 eglTerminate(fDisplay);
442 fDisplay = EGL_NO_DISPLAY;
443 }
444
445 #ifdef SK_BUILD_FOR_WIN
446 if (fWindow) {
447 if (fDeviceContext) {
448 ReleaseDC(fWindow, fDeviceContext);
449 fDeviceContext = 0;
450 }
451
452 DestroyWindow(fWindow);
453 fWindow = 0;
454 }
455 #endif
456 }
457
onPlatformMakeNotCurrent() const458 void ANGLEGLContext::onPlatformMakeNotCurrent() const {
459 if (!eglMakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
460 SkDebugf("Could not reset the context 0x%x.\n", eglGetError());
461 }
462 }
463
onPlatformMakeCurrent() const464 void ANGLEGLContext::onPlatformMakeCurrent() const {
465 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
466 SkDebugf("Could not set the context 0x%x.\n", eglGetError());
467 }
468 }
469
onPlatformGetAutoContextRestore() const470 std::function<void()> ANGLEGLContext::onPlatformGetAutoContextRestore() const {
471 if (eglGetCurrentContext() == fContext) {
472 return nullptr;
473 }
474 return context_restorer();
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 = SkLoadDynamicLibrary("libGLESv2.dll");
490 gLibs.fEGLLib = SkLoadDynamicLibrary("libEGL.dll");
491 #elif defined SK_BUILD_FOR_MAC
492 gLibs.fGLLib = SkLoadDynamicLibrary("libGLESv2.dylib");
493 gLibs.fEGLLib = SkLoadDynamicLibrary("libEGL.dylib");
494 #else
495 gLibs.fGLLib = SkLoadDynamicLibrary("libGLESv2.so");
496 gLibs.fEGLLib = SkLoadDynamicLibrary("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