OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "gl/GLContext.h" | 8 #include "gl/GLTestContext.h" |
9 | 9 |
| 10 #define GL_GLEXT_PROTOTYPES |
10 #include <GLES2/gl2.h> | 11 #include <GLES2/gl2.h> |
11 | 12 |
12 #define EGL_EGLEXT_PROTOTYPES | 13 #define EGL_EGLEXT_PROTOTYPES |
13 #include <EGL/egl.h> | 14 #include <EGL/egl.h> |
14 #include <EGL/eglext.h> | 15 #include <EGL/eglext.h> |
15 | 16 |
16 #include "gl/GrGLDefines.h" | 17 #include "gl/GrGLDefines.h" |
17 #include "gl/GrGLUtil.h" | 18 #include "gl/GrGLUtil.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // TODO: Share this class with ANGLE if/when it gets support for EGL_KHR_fence_s
ync. | 22 // TODO: Share this class with ANGLE if/when it gets support for EGL_KHR_fence_s
ync. |
22 class SkEGLFenceSync : public SkGpuFenceSync { | 23 class EGLFenceSync : public SkGpuFenceSync { |
23 public: | 24 public: |
24 static SkEGLFenceSync* CreateIfSupported(EGLDisplay); | 25 static EGLFenceSync* CreateIfSupported(EGLDisplay); |
25 | 26 |
26 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override; | 27 SkPlatformGpuFence SK_WARN_UNUSED_RESULT insertFence() const override; |
27 bool waitFence(SkPlatformGpuFence fence, bool flush) const override; | 28 bool waitFence(SkPlatformGpuFence fence, bool flush) const override; |
28 void deleteFence(SkPlatformGpuFence fence) const override; | 29 void deleteFence(SkPlatformGpuFence fence) const override; |
29 | 30 |
30 private: | 31 private: |
31 SkEGLFenceSync(EGLDisplay display) : fDisplay(display) {} | 32 EGLFenceSync(EGLDisplay display) : fDisplay(display) {} |
32 | 33 |
33 EGLDisplay fDisplay; | 34 EGLDisplay fDisplay; |
34 | 35 |
35 typedef SkGpuFenceSync INHERITED; | 36 typedef SkGpuFenceSync INHERITED; |
36 }; | 37 }; |
37 | 38 |
38 class EGLGLContext : public sk_gpu_test::GLContext { | 39 class EGLGLTestContext : public sk_gpu_test::GLTestContext { |
39 public: | 40 public: |
40 EGLGLContext(GrGLStandard forcedGpuAPI); | 41 EGLGLTestContext(GrGLStandard forcedGpuAPI); |
41 ~EGLGLContext() override; | 42 ~EGLGLTestContext() override; |
42 | 43 |
43 GrEGLImage texture2DToEGLImage(GrGLuint texID) const override; | 44 GrEGLImage texture2DToEGLImage(GrGLuint texID) const override; |
44 void destroyEGLImage(GrEGLImage) const override; | 45 void destroyEGLImage(GrEGLImage) const override; |
45 GrGLuint eglImageToExternalTexture(GrEGLImage) const override; | 46 GrGLuint eglImageToExternalTexture(GrEGLImage) const override; |
46 sk_gpu_test::GLContext* createNew() const override; | 47 sk_gpu_test::GLTestContext* createNew() const override; |
47 | 48 |
48 private: | 49 private: |
49 void destroyGLContext(); | 50 void destroyGLContext(); |
50 | 51 |
51 void onPlatformMakeCurrent() const override; | 52 void onPlatformMakeCurrent() const override; |
52 void onPlatformSwapBuffers() const override; | 53 void onPlatformSwapBuffers() const override; |
53 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; | 54 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
54 | 55 |
55 EGLContext fContext; | 56 EGLContext fContext; |
56 EGLDisplay fDisplay; | 57 EGLDisplay fDisplay; |
57 EGLSurface fSurface; | 58 EGLSurface fSurface; |
58 }; | 59 }; |
59 | 60 |
60 EGLGLContext::EGLGLContext(GrGLStandard forcedGpuAPI) | 61 EGLGLTestContext::EGLGLTestContext(GrGLStandard forcedGpuAPI) |
61 : fContext(EGL_NO_CONTEXT) | 62 : fContext(EGL_NO_CONTEXT) |
62 , fDisplay(EGL_NO_DISPLAY) | 63 , fDisplay(EGL_NO_DISPLAY) |
63 , fSurface(EGL_NO_SURFACE) { | 64 , fSurface(EGL_NO_SURFACE) { |
64 static const EGLint kEGLContextAttribsForOpenGL[] = { | 65 static const EGLint kEGLContextAttribsForOpenGL[] = { |
65 EGL_NONE | 66 EGL_NONE |
66 }; | 67 }; |
67 | 68 |
68 static const EGLint kEGLContextAttribsForOpenGLES[] = { | 69 static const EGLint kEGLContextAttribsForOpenGLES[] = { |
69 EGL_CONTEXT_CLIENT_VERSION, 2, | 70 EGL_CONTEXT_CLIENT_VERSION, 2, |
70 EGL_NONE | 71 EGL_NONE |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 this->destroyGLContext(); | 173 this->destroyGLContext(); |
173 continue; | 174 continue; |
174 } | 175 } |
175 | 176 |
176 if (!gl->validate()) { | 177 if (!gl->validate()) { |
177 SkDebugf("Failed to validate gl interface.\n"); | 178 SkDebugf("Failed to validate gl interface.\n"); |
178 this->destroyGLContext(); | 179 this->destroyGLContext(); |
179 continue; | 180 continue; |
180 } | 181 } |
181 | 182 |
182 this->init(gl.release(), SkEGLFenceSync::CreateIfSupported(fDisplay)); | 183 this->init(gl.release(), EGLFenceSync::CreateIfSupported(fDisplay)); |
183 break; | 184 break; |
184 } | 185 } |
185 } | 186 } |
186 | 187 |
187 EGLGLContext::~EGLGLContext() { | 188 EGLGLTestContext::~EGLGLTestContext() { |
188 this->teardown(); | 189 this->teardown(); |
189 this->destroyGLContext(); | 190 this->destroyGLContext(); |
190 } | 191 } |
191 | 192 |
192 void EGLGLContext::destroyGLContext() { | 193 void EGLGLTestContext::destroyGLContext() { |
193 if (fDisplay) { | 194 if (fDisplay) { |
194 eglMakeCurrent(fDisplay, 0, 0, 0); | 195 eglMakeCurrent(fDisplay, 0, 0, 0); |
195 | 196 |
196 if (fContext) { | 197 if (fContext) { |
197 eglDestroyContext(fDisplay, fContext); | 198 eglDestroyContext(fDisplay, fContext); |
198 fContext = EGL_NO_CONTEXT; | 199 fContext = EGL_NO_CONTEXT; |
199 } | 200 } |
200 | 201 |
201 if (fSurface) { | 202 if (fSurface) { |
202 eglDestroySurface(fDisplay, fSurface); | 203 eglDestroySurface(fDisplay, fSurface); |
203 fSurface = EGL_NO_SURFACE; | 204 fSurface = EGL_NO_SURFACE; |
204 } | 205 } |
205 | 206 |
206 //TODO should we close the display? | 207 //TODO should we close the display? |
207 fDisplay = EGL_NO_DISPLAY; | 208 fDisplay = EGL_NO_DISPLAY; |
208 } | 209 } |
209 } | 210 } |
210 | 211 |
211 GrEGLImage EGLGLContext::texture2DToEGLImage(GrGLuint texID) const { | 212 GrEGLImage EGLGLTestContext::texture2DToEGLImage(GrGLuint texID) const { |
212 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { | 213 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { |
213 return GR_EGL_NO_IMAGE; | 214 return GR_EGL_NO_IMAGE; |
214 } | 215 } |
215 GrEGLImage img; | 216 GrEGLImage img; |
216 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, GR_EGL_NONE }; | 217 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, GR_EGL_NONE }; |
217 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>(texID); | 218 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>(texID); |
218 GR_GL_CALL_RET(this->gl(), img, | 219 GR_GL_CALL_RET(this->gl(), img, |
219 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie
ntBuffer, attribs)); | 220 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie
ntBuffer, attribs)); |
220 return img; | 221 return img; |
221 } | 222 } |
222 | 223 |
223 void EGLGLContext::destroyEGLImage(GrEGLImage image) const { | 224 void EGLGLTestContext::destroyEGLImage(GrEGLImage image) const { |
224 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image)); | 225 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image)); |
225 } | 226 } |
226 | 227 |
227 GrGLuint EGLGLContext::eglImageToExternalTexture(GrEGLImage image) const { | 228 GrGLuint EGLGLTestContext::eglImageToExternalTexture(GrEGLImage image) const { |
228 GrGLClearErr(this->gl()); | 229 GrGLClearErr(this->gl()); |
229 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) { | 230 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) { |
230 return 0; | 231 return 0; |
231 } | 232 } |
232 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage); | 233 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage); |
233 | 234 |
234 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D = | 235 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D = |
235 (EGLImageTargetTexture2DProc) eglGetProcAddress("glEGLImageTargetTexture
2DOES"); | 236 (EGLImageTargetTexture2DProc) eglGetProcAddress("glEGLImageTargetTexture
2DOES"); |
236 if (!glEGLImageTargetTexture2D) { | 237 if (!glEGLImageTargetTexture2D) { |
237 return 0; | 238 return 0; |
238 } | 239 } |
239 GrGLuint texID; | 240 GrGLuint texID; |
240 glGenTextures(1, &texID); | 241 glGenTextures(1, &texID); |
241 if (!texID) { | 242 if (!texID) { |
242 return 0; | 243 return 0; |
243 } | 244 } |
244 glBindTexture(GR_GL_TEXTURE_EXTERNAL, texID); | 245 glBindTexture(GR_GL_TEXTURE_EXTERNAL, texID); |
245 if (glGetError() != GR_GL_NO_ERROR) { | 246 if (glGetError() != GR_GL_NO_ERROR) { |
246 glDeleteTextures(1, &texID); | 247 glDeleteTextures(1, &texID); |
247 return 0; | 248 return 0; |
248 } | 249 } |
249 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image); | 250 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image); |
250 if (glGetError() != GR_GL_NO_ERROR) { | 251 if (glGetError() != GR_GL_NO_ERROR) { |
251 glDeleteTextures(1, &texID); | 252 glDeleteTextures(1, &texID); |
252 return 0; | 253 return 0; |
253 } | 254 } |
254 return texID; | 255 return texID; |
255 } | 256 } |
256 | 257 |
257 sk_gpu_test::GLContext* EGLGLContext::createNew() const { | 258 sk_gpu_test::GLTestContext* EGLGLTestContext::createNew() const { |
258 sk_gpu_test::GLContext* ctx = new EGLGLContext(this->gl()->fStandard); | 259 sk_gpu_test::GLTestContext* ctx = new EGLGLTestContext(this->gl()->fStandard
); |
259 if (ctx) { | 260 if (ctx) { |
260 ctx->makeCurrent(); | 261 ctx->makeCurrent(); |
261 } | 262 } |
262 return ctx; | 263 return ctx; |
263 } | 264 } |
264 | 265 |
265 void EGLGLContext::onPlatformMakeCurrent() const { | 266 void EGLGLTestContext::onPlatformMakeCurrent() const { |
266 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | 267 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
267 SkDebugf("Could not set the context.\n"); | 268 SkDebugf("Could not set the context.\n"); |
268 } | 269 } |
269 } | 270 } |
270 | 271 |
271 void EGLGLContext::onPlatformSwapBuffers() const { | 272 void EGLGLTestContext::onPlatformSwapBuffers() const { |
272 if (!eglSwapBuffers(fDisplay, fSurface)) { | 273 if (!eglSwapBuffers(fDisplay, fSurface)) { |
273 SkDebugf("Could not complete eglSwapBuffers.\n"); | 274 SkDebugf("Could not complete eglSwapBuffers.\n"); |
274 } | 275 } |
275 } | 276 } |
276 | 277 |
277 GrGLFuncPtr EGLGLContext::onPlatformGetProcAddress(const char* procName) const { | 278 GrGLFuncPtr EGLGLTestContext::onPlatformGetProcAddress(const char* procName) con
st { |
278 return eglGetProcAddress(procName); | 279 return eglGetProcAddress(procName); |
279 } | 280 } |
280 | 281 |
281 static bool supports_egl_extension(EGLDisplay display, const char* extension) { | 282 static bool supports_egl_extension(EGLDisplay display, const char* extension) { |
282 size_t extensionLength = strlen(extension); | 283 size_t extensionLength = strlen(extension); |
283 const char* extensionsStr = eglQueryString(display, EGL_EXTENSIONS); | 284 const char* extensionsStr = eglQueryString(display, EGL_EXTENSIONS); |
284 while (const char* match = strstr(extensionsStr, extension)) { | 285 while (const char* match = strstr(extensionsStr, extension)) { |
285 // Ensure the string we found is its own extension, not a substring of a
larger extension | 286 // Ensure the string we found is its own extension, not a substring of a
larger extension |
286 // (e.g. GL_ARB_occlusion_query / GL_ARB_occlusion_query2). | 287 // (e.g. GL_ARB_occlusion_query / GL_ARB_occlusion_query2). |
287 if ((match == extensionsStr || match[-1] == ' ') && | 288 if ((match == extensionsStr || match[-1] == ' ') && |
288 (match[extensionLength] == ' ' || match[extensionLength] == '\0')) { | 289 (match[extensionLength] == ' ' || match[extensionLength] == '\0')) { |
289 return true; | 290 return true; |
290 } | 291 } |
291 extensionsStr = match + extensionLength; | 292 extensionsStr = match + extensionLength; |
292 } | 293 } |
293 return false; | 294 return false; |
294 } | 295 } |
295 | 296 |
296 SkEGLFenceSync* SkEGLFenceSync::CreateIfSupported(EGLDisplay display) { | 297 EGLFenceSync* EGLFenceSync::CreateIfSupported(EGLDisplay display) { |
297 if (!display || !supports_egl_extension(display, "EGL_KHR_fence_sync")) { | 298 if (!display || !supports_egl_extension(display, "EGL_KHR_fence_sync")) { |
298 return nullptr; | 299 return nullptr; |
299 } | 300 } |
300 return new SkEGLFenceSync(display); | 301 return new EGLFenceSync(display); |
301 } | 302 } |
302 | 303 |
303 SkPlatformGpuFence SkEGLFenceSync::insertFence() const { | 304 SkPlatformGpuFence EGLFenceSync::insertFence() const { |
304 return eglCreateSyncKHR(fDisplay, EGL_SYNC_FENCE_KHR, nullptr); | 305 return eglCreateSyncKHR(fDisplay, EGL_SYNC_FENCE_KHR, nullptr); |
305 } | 306 } |
306 | 307 |
307 bool SkEGLFenceSync::waitFence(SkPlatformGpuFence platformFence, bool flush) con
st { | 308 bool EGLFenceSync::waitFence(SkPlatformGpuFence platformFence, bool flush) const
{ |
308 EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence); | 309 EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence); |
309 return EGL_CONDITION_SATISFIED_KHR == | 310 return EGL_CONDITION_SATISFIED_KHR == |
310 eglClientWaitSyncKHR(fDisplay, | 311 eglClientWaitSyncKHR(fDisplay, |
311 eglsync, | 312 eglsync, |
312 flush ? EGL_SYNC_FLUSH_COMMANDS_BIT_KHR : 0, | 313 flush ? EGL_SYNC_FLUSH_COMMANDS_BIT_KHR : 0, |
313 EGL_FOREVER_KHR); | 314 EGL_FOREVER_KHR); |
314 } | 315 } |
315 | 316 |
316 void SkEGLFenceSync::deleteFence(SkPlatformGpuFence platformFence) const { | 317 void EGLFenceSync::deleteFence(SkPlatformGpuFence platformFence) const { |
317 EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence); | 318 EGLSyncKHR eglsync = static_cast<EGLSyncKHR>(platformFence); |
318 eglDestroySyncKHR(fDisplay, eglsync); | 319 eglDestroySyncKHR(fDisplay, eglsync); |
319 } | 320 } |
320 | 321 |
321 } // anonymous namespace | 322 } // anonymous namespace |
322 | 323 |
323 namespace sk_gpu_test { | 324 namespace sk_gpu_test { |
324 GLContext *CreatePlatformGLContext(GrGLStandard forcedGpuAPI, GLContext *shareCo
ntext) { | 325 GLTestContext *CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI, |
| 326 GLTestContext *shareContext) { |
325 SkASSERT(!shareContext); | 327 SkASSERT(!shareContext); |
326 if (shareContext) { | 328 if (shareContext) { |
327 return nullptr; | 329 return nullptr; |
328 } | 330 } |
329 EGLGLContext *ctx = new EGLGLContext(forcedGpuAPI); | 331 EGLGLTestContext *ctx = new EGLGLTestContext(forcedGpuAPI); |
330 if (!ctx->isValid()) { | 332 if (!ctx->isValid()) { |
331 delete ctx; | 333 delete ctx; |
332 return nullptr; | 334 return nullptr; |
333 } | 335 } |
334 return ctx; | 336 return ctx; |
335 } | 337 } |
336 } // namespace sk_gpu_test | 338 } // namespace sk_gpu_test |
337 | 339 |
OLD | NEW |