• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2011 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 #include "gl/GLTestContext.h"
9 
10 #define GL_GLEXT_PROTOTYPES
11 #include <GLES2/gl2.h>
12 
13 #define EGL_EGLEXT_PROTOTYPES
14 #include <EGL/egl.h>
15 #include <EGL/eglext.h>
16 
17 #include "gl/GrGLDefines.h"
18 #include "gl/GrGLUtil.h"
19 
20 namespace {
21 
22 // TODO: Share this class with ANGLE if/when it gets support for EGL_KHR_fence_sync.
23 class EGLFenceSync : public sk_gpu_test::FenceSync {
24 public:
25     static std::unique_ptr<EGLFenceSync> MakeIfSupported(EGLDisplay);
26 
27     sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override;
28     bool waitFence(sk_gpu_test::PlatformFence fence) const override;
29     void deleteFence(sk_gpu_test::PlatformFence fence) const override;
30 
31 private:
EGLFenceSync(EGLDisplay display)32     EGLFenceSync(EGLDisplay display) : fDisplay(display) {}
33 
34     EGLDisplay                    fDisplay;
35 
36     typedef sk_gpu_test::FenceSync INHERITED;
37 };
38 
39 class EGLGLTestContext : public sk_gpu_test::GLTestContext {
40 public:
41     EGLGLTestContext(GrGLStandard forcedGpuAPI, EGLGLTestContext* shareContext);
42     ~EGLGLTestContext() override;
43 
44     GrEGLImage texture2DToEGLImage(GrGLuint texID) const override;
45     void destroyEGLImage(GrEGLImage) const override;
46     GrGLuint eglImageToExternalTexture(GrEGLImage) const override;
47     std::unique_ptr<sk_gpu_test::GLTestContext> makeNew() const override;
48 
49 private:
50     void destroyGLContext();
51 
52     void onPlatformMakeCurrent() const override;
53     void onPlatformSwapBuffers() const override;
54     GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
55 
56     EGLContext fContext;
57     EGLDisplay fDisplay;
58     EGLSurface fSurface;
59 };
60 
EGLGLTestContext(GrGLStandard forcedGpuAPI,EGLGLTestContext * shareContext)61 EGLGLTestContext::EGLGLTestContext(GrGLStandard forcedGpuAPI, EGLGLTestContext* shareContext)
62     : fContext(EGL_NO_CONTEXT)
63     , fDisplay(EGL_NO_DISPLAY)
64     , fSurface(EGL_NO_SURFACE) {
65 
66     EGLContext eglShareContext = shareContext ? shareContext->fContext : nullptr;
67 
68     static const EGLint kEGLContextAttribsForOpenGL[] = {
69         EGL_NONE
70     };
71 
72     static const EGLint kEGLContextAttribsForOpenGLES[] = {
73         EGL_CONTEXT_CLIENT_VERSION, 2,
74         EGL_NONE
75     };
76 
77     static const struct {
78         const EGLint* fContextAttribs;
79         EGLenum fAPI;
80         EGLint  fRenderableTypeBit;
81         GrGLStandard fStandard;
82     } kAPIs[] = {
83         {   // OpenGL
84             kEGLContextAttribsForOpenGL,
85             EGL_OPENGL_API,
86             EGL_OPENGL_BIT,
87             kGL_GrGLStandard
88         },
89         {   // OpenGL ES. This seems to work for both ES2 and 3 (when available).
90             kEGLContextAttribsForOpenGLES,
91             EGL_OPENGL_ES_API,
92             EGL_OPENGL_ES2_BIT,
93             kGLES_GrGLStandard
94         },
95     };
96 
97     size_t apiLimit = SK_ARRAY_COUNT(kAPIs);
98     size_t api = 0;
99     if (forcedGpuAPI == kGL_GrGLStandard) {
100         apiLimit = 1;
101     } else if (forcedGpuAPI == kGLES_GrGLStandard) {
102         api = 1;
103     }
104     SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == forcedGpuAPI);
105 
106     sk_sp<const GrGLInterface> gl;
107 
108     for (; nullptr == gl.get() && api < apiLimit; ++api) {
109         fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
110 
111         EGLint majorVersion;
112         EGLint minorVersion;
113         eglInitialize(fDisplay, &majorVersion, &minorVersion);
114 
115 #if 0
116         SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
117         SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
118         SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
119         SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
120 #endif
121 
122         if (!eglBindAPI(kAPIs[api].fAPI)) {
123             continue;
124         }
125 
126         EGLint numConfigs = 0;
127         const EGLint configAttribs[] = {
128             EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
129             EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
130             EGL_RED_SIZE, 8,
131             EGL_GREEN_SIZE, 8,
132             EGL_BLUE_SIZE, 8,
133             EGL_ALPHA_SIZE, 8,
134             EGL_NONE
135         };
136 
137         EGLConfig surfaceConfig;
138         if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
139             SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
140             continue;
141         }
142 
143         if (0 == numConfigs) {
144             SkDebugf("No suitable EGL config found.\n");
145             continue;
146         }
147 
148         fContext = eglCreateContext(fDisplay, surfaceConfig, eglShareContext,
149                                     kAPIs[api].fContextAttribs);
150         if (EGL_NO_CONTEXT == fContext) {
151             SkDebugf("eglCreateContext failed.  EGL Error: 0x%08x\n", eglGetError());
152             continue;
153         }
154 
155         static const EGLint kSurfaceAttribs[] = {
156             EGL_WIDTH, 1,
157             EGL_HEIGHT, 1,
158             EGL_NONE
159         };
160 
161         fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
162         if (EGL_NO_SURFACE == fSurface) {
163             SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
164             this->destroyGLContext();
165             continue;
166         }
167 
168         if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
169             SkDebugf("eglMakeCurrent failed.  EGL Error: 0x%08x\n", eglGetError());
170             this->destroyGLContext();
171             continue;
172         }
173 
174         gl.reset(GrGLCreateNativeInterface());
175         if (nullptr == gl.get()) {
176             SkDebugf("Failed to create gl interface.\n");
177             this->destroyGLContext();
178             continue;
179         }
180 
181         if (!gl->validate()) {
182             SkDebugf("Failed to validate gl interface.\n");
183             this->destroyGLContext();
184             continue;
185         }
186 
187         this->init(gl.release(), EGLFenceSync::MakeIfSupported(fDisplay));
188         break;
189     }
190 }
191 
~EGLGLTestContext()192 EGLGLTestContext::~EGLGLTestContext() {
193     this->teardown();
194     this->destroyGLContext();
195 }
196 
destroyGLContext()197 void EGLGLTestContext::destroyGLContext() {
198     if (fDisplay) {
199         eglMakeCurrent(fDisplay, 0, 0, 0);
200 
201         if (fContext) {
202             eglDestroyContext(fDisplay, fContext);
203             fContext = EGL_NO_CONTEXT;
204         }
205 
206         if (fSurface) {
207             eglDestroySurface(fDisplay, fSurface);
208             fSurface = EGL_NO_SURFACE;
209         }
210 
211         //TODO should we close the display?
212         fDisplay = EGL_NO_DISPLAY;
213     }
214 }
215 
texture2DToEGLImage(GrGLuint texID) const216 GrEGLImage EGLGLTestContext::texture2DToEGLImage(GrGLuint texID) const {
217     if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
218         return GR_EGL_NO_IMAGE;
219     }
220     GrEGLImage img;
221     GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, GR_EGL_NONE };
222     GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>(texID);
223     GR_GL_CALL_RET(this->gl(), img,
224                    EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clientBuffer, attribs));
225     return img;
226 }
227 
destroyEGLImage(GrEGLImage image) const228 void EGLGLTestContext::destroyEGLImage(GrEGLImage image) const {
229     GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image));
230 }
231 
eglImageToExternalTexture(GrEGLImage image) const232 GrGLuint EGLGLTestContext::eglImageToExternalTexture(GrEGLImage image) const {
233     GrGLClearErr(this->gl());
234     if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
235         return 0;
236     }
237     typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
238 
239     EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
240         (EGLImageTargetTexture2DProc) eglGetProcAddress("glEGLImageTargetTexture2DOES");
241     if (!glEGLImageTargetTexture2D) {
242         return 0;
243     }
244     GrGLuint texID;
245     glGenTextures(1, &texID);
246     if (!texID) {
247         return 0;
248     }
249     glBindTexture(GR_GL_TEXTURE_EXTERNAL, texID);
250     if (glGetError() != GR_GL_NO_ERROR) {
251         glDeleteTextures(1, &texID);
252         return 0;
253     }
254     glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
255     if (glGetError() != GR_GL_NO_ERROR) {
256         glDeleteTextures(1, &texID);
257         return 0;
258     }
259     return texID;
260 }
261 
makeNew() const262 std::unique_ptr<sk_gpu_test::GLTestContext> EGLGLTestContext::makeNew() const {
263     std::unique_ptr<sk_gpu_test::GLTestContext> ctx(new EGLGLTestContext(this->gl()->fStandard,
264                                                                          nullptr));
265     if (ctx) {
266         ctx->makeCurrent();
267     }
268     return ctx;
269 }
270 
onPlatformMakeCurrent() const271 void EGLGLTestContext::onPlatformMakeCurrent() const {
272     if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
273         SkDebugf("Could not set the context.\n");
274     }
275 }
276 
onPlatformSwapBuffers() const277 void EGLGLTestContext::onPlatformSwapBuffers() const {
278     if (!eglSwapBuffers(fDisplay, fSurface)) {
279         SkDebugf("Could not complete eglSwapBuffers.\n");
280     }
281 }
282 
onPlatformGetProcAddress(const char * procName) const283 GrGLFuncPtr EGLGLTestContext::onPlatformGetProcAddress(const char* procName) const {
284     return eglGetProcAddress(procName);
285 }
286 
supports_egl_extension(EGLDisplay display,const char * extension)287 static bool supports_egl_extension(EGLDisplay display, const char* extension) {
288     size_t extensionLength = strlen(extension);
289     const char* extensionsStr = eglQueryString(display, EGL_EXTENSIONS);
290     while (const char* match = strstr(extensionsStr, extension)) {
291         // Ensure the string we found is its own extension, not a substring of a larger extension
292         // (e.g. GL_ARB_occlusion_query / GL_ARB_occlusion_query2).
293         if ((match == extensionsStr || match[-1] == ' ') &&
294             (match[extensionLength] == ' ' || match[extensionLength] == '\0')) {
295             return true;
296         }
297         extensionsStr = match + extensionLength;
298     }
299     return false;
300 }
301 
MakeIfSupported(EGLDisplay display)302 std::unique_ptr<EGLFenceSync> EGLFenceSync::MakeIfSupported(EGLDisplay display) {
303     if (!display || !supports_egl_extension(display, "EGL_KHR_fence_sync")) {
304         return nullptr;
305     }
306     return std::unique_ptr<EGLFenceSync>(new EGLFenceSync(display));
307 }
308 
insertFence() const309 sk_gpu_test::PlatformFence EGLFenceSync::insertFence() const {
310     EGLSyncKHR eglsync = eglCreateSyncKHR(fDisplay, EGL_SYNC_FENCE_KHR, nullptr);
311     return reinterpret_cast<sk_gpu_test::PlatformFence>(eglsync);
312 }
313 
waitFence(sk_gpu_test::PlatformFence platformFence) const314 bool EGLFenceSync::waitFence(sk_gpu_test::PlatformFence platformFence) const {
315     EGLSyncKHR eglsync = reinterpret_cast<EGLSyncKHR>(platformFence);
316     return EGL_CONDITION_SATISFIED_KHR ==
317             eglClientWaitSyncKHR(fDisplay,
318                                  eglsync,
319                                  EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
320                                  EGL_FOREVER_KHR);
321 }
322 
deleteFence(sk_gpu_test::PlatformFence platformFence) const323 void EGLFenceSync::deleteFence(sk_gpu_test::PlatformFence platformFence) const {
324     EGLSyncKHR eglsync = reinterpret_cast<EGLSyncKHR>(platformFence);
325     eglDestroySyncKHR(fDisplay, eglsync);
326 }
327 
328 GR_STATIC_ASSERT(sizeof(EGLSyncKHR) <= sizeof(sk_gpu_test::PlatformFence));
329 
330 }  // anonymous namespace
331 
332 namespace sk_gpu_test {
CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,GLTestContext * shareContext)333 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
334                                            GLTestContext *shareContext) {
335     EGLGLTestContext* eglShareContext = reinterpret_cast<EGLGLTestContext*>(shareContext);
336     EGLGLTestContext *ctx = new EGLGLTestContext(forcedGpuAPI, eglShareContext);
337     if (!ctx->isValid()) {
338         delete ctx;
339         return nullptr;
340     }
341     return ctx;
342 }
343 }  // namespace sk_gpu_test
344