Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2011 Google Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | #include "gl/SkNativeGLContext.h" |
michael@0 | 9 | |
michael@0 | 10 | SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
michael@0 | 11 | fOldEGLContext = eglGetCurrentContext(); |
michael@0 | 12 | fOldDisplay = eglGetCurrentDisplay(); |
michael@0 | 13 | fOldSurface = eglGetCurrentSurface(EGL_DRAW); |
michael@0 | 14 | |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { |
michael@0 | 18 | if (NULL != fOldDisplay) { |
michael@0 | 19 | eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext); |
michael@0 | 20 | } |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 24 | |
michael@0 | 25 | SkNativeGLContext::SkNativeGLContext() |
michael@0 | 26 | : fContext(EGL_NO_CONTEXT) |
michael@0 | 27 | , fDisplay(EGL_NO_DISPLAY) |
michael@0 | 28 | , fSurface(EGL_NO_SURFACE) { |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | SkNativeGLContext::~SkNativeGLContext() { |
michael@0 | 32 | this->destroyGLContext(); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | void SkNativeGLContext::destroyGLContext() { |
michael@0 | 36 | if (fDisplay) { |
michael@0 | 37 | eglMakeCurrent(fDisplay, 0, 0, 0); |
michael@0 | 38 | |
michael@0 | 39 | if (fContext) { |
michael@0 | 40 | eglDestroyContext(fDisplay, fContext); |
michael@0 | 41 | fContext = EGL_NO_CONTEXT; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | if (fSurface) { |
michael@0 | 45 | eglDestroySurface(fDisplay, fSurface); |
michael@0 | 46 | fSurface = EGL_NO_SURFACE; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | //TODO should we close the display? |
michael@0 | 50 | fDisplay = EGL_NO_DISPLAY; |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | const GrGLInterface* SkNativeGLContext::createGLContext() { |
michael@0 | 55 | static const EGLint kEGLContextAttribsForOpenGL[] = { |
michael@0 | 56 | EGL_NONE |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | static const EGLint kEGLContextAttribsForOpenGLES[] = { |
michael@0 | 60 | EGL_CONTEXT_CLIENT_VERSION, 2, |
michael@0 | 61 | EGL_NONE |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | static const struct { |
michael@0 | 65 | const EGLint* fContextAttribs; |
michael@0 | 66 | EGLenum fAPI; |
michael@0 | 67 | EGLint fRenderableTypeBit; |
michael@0 | 68 | GrGLStandard fStandard; |
michael@0 | 69 | } kAPIs[] = { |
michael@0 | 70 | { // OpenGL |
michael@0 | 71 | kEGLContextAttribsForOpenGL, |
michael@0 | 72 | EGL_OPENGL_API, |
michael@0 | 73 | EGL_OPENGL_BIT, |
michael@0 | 74 | kGL_GrGLStandard |
michael@0 | 75 | }, |
michael@0 | 76 | { // OpenGL ES. This seems to work for both ES2 and 3 (when available). |
michael@0 | 77 | kEGLContextAttribsForOpenGLES, |
michael@0 | 78 | EGL_OPENGL_ES_API, |
michael@0 | 79 | EGL_OPENGL_ES2_BIT, |
michael@0 | 80 | kGLES_GrGLStandard |
michael@0 | 81 | }, |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | const GrGLInterface* interface = NULL; |
michael@0 | 85 | |
michael@0 | 86 | for (size_t api = 0; NULL == interface && api < SK_ARRAY_COUNT(kAPIs); ++api) { |
michael@0 | 87 | fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
michael@0 | 88 | |
michael@0 | 89 | EGLint majorVersion; |
michael@0 | 90 | EGLint minorVersion; |
michael@0 | 91 | eglInitialize(fDisplay, &majorVersion, &minorVersion); |
michael@0 | 92 | |
michael@0 | 93 | #if 0 |
michael@0 | 94 | SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR)); |
michael@0 | 95 | SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS)); |
michael@0 | 96 | SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION)); |
michael@0 | 97 | SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS)); |
michael@0 | 98 | #endif |
michael@0 | 99 | |
michael@0 | 100 | if (!eglBindAPI(kAPIs[api].fAPI)) { |
michael@0 | 101 | continue; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | EGLint numConfigs; |
michael@0 | 105 | const EGLint configAttribs[] = { |
michael@0 | 106 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
michael@0 | 107 | EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit, |
michael@0 | 108 | EGL_RED_SIZE, 8, |
michael@0 | 109 | EGL_GREEN_SIZE, 8, |
michael@0 | 110 | EGL_BLUE_SIZE, 8, |
michael@0 | 111 | EGL_ALPHA_SIZE, 8, |
michael@0 | 112 | EGL_NONE |
michael@0 | 113 | }; |
michael@0 | 114 | |
michael@0 | 115 | EGLConfig surfaceConfig; |
michael@0 | 116 | if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) { |
michael@0 | 117 | SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError()); |
michael@0 | 118 | continue; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs); |
michael@0 | 122 | if (EGL_NO_CONTEXT == fContext) { |
michael@0 | 123 | SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError()); |
michael@0 | 124 | continue; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | static const EGLint kSurfaceAttribs[] = { |
michael@0 | 128 | EGL_WIDTH, 1, |
michael@0 | 129 | EGL_HEIGHT, 1, |
michael@0 | 130 | EGL_NONE |
michael@0 | 131 | }; |
michael@0 | 132 | |
michael@0 | 133 | fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs); |
michael@0 | 134 | if (EGL_NO_SURFACE == fSurface) { |
michael@0 | 135 | SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError()); |
michael@0 | 136 | this->destroyGLContext(); |
michael@0 | 137 | continue; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
michael@0 | 141 | SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError()); |
michael@0 | 142 | this->destroyGLContext(); |
michael@0 | 143 | continue; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | interface = GrGLCreateNativeInterface(); |
michael@0 | 147 | if (NULL == interface) { |
michael@0 | 148 | SkDebugf("Failed to create gl interface.\n"); |
michael@0 | 149 | this->destroyGLContext(); |
michael@0 | 150 | continue; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | if (!interface->validate()) { |
michael@0 | 154 | interface->unref(); |
michael@0 | 155 | interface = NULL; |
michael@0 | 156 | this->destroyGLContext(); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | return interface; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void SkNativeGLContext::makeCurrent() const { |
michael@0 | 164 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
michael@0 | 165 | SkDebugf("Could not set the context.\n"); |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | void SkNativeGLContext::swapBuffers() const { |
michael@0 | 170 | if (!eglSwapBuffers(fDisplay, fSurface)) { |
michael@0 | 171 | SkDebugf("Could not complete eglSwapBuffers.\n"); |
michael@0 | 172 | } |
michael@0 | 173 | } |