| 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/SkNativeGLContext.h" | 8 #include "gl/SkNativeGLContext.h" |
| 9 | 9 |
| 10 SkNativeGLContext::AutoContextRestore::AutoContextRestore() { | 10 SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 const GrGLInterface* SkNativeGLContext::createGLContext() { | 54 const GrGLInterface* SkNativeGLContext::createGLContext() { |
| 55 static const EGLint kEGLContextAttribsForOpenGL[] = { | 55 static const EGLint kEGLContextAttribsForOpenGL[] = { |
| 56 EGL_NONE | 56 EGL_NONE |
| 57 }; | 57 }; |
| 58 | 58 |
| 59 static const EGLint kEGLContextAttribsForOpenGLES[] = { | 59 static const EGLint kEGLContextAttribsForOpenGLES[] = { |
| 60 EGL_CONTEXT_CLIENT_VERSION, 2, | 60 EGL_CONTEXT_CLIENT_VERSION, 2, |
| 61 EGL_NONE | 61 EGL_NONE |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 // Try first for OpenGL, then fall back to OpenGL ES. | 64 static const struct { |
| 65 EGLint renderableTypeBit = EGL_OPENGL_BIT; | 65 const EGLint* fContextAttribs; |
| 66 const EGLint* contextAttribs = kEGLContextAttribsForOpenGL; | 66 EGLenum fAPI; |
| 67 EGLBoolean apiBound = eglBindAPI(EGL_OPENGL_API); | 67 EGLint fRenderableTypeBit; |
| 68 | 68 GrGLBinding fBinding; |
| 69 if (!apiBound) { | 69 } kAPIs[] = { |
| 70 apiBound = eglBindAPI(EGL_OPENGL_ES_API); | 70 { // OpenGL |
| 71 renderableTypeBit = EGL_OPENGL_ES2_BIT; | 71 kEGLContextAttribsForOpenGL, |
| 72 contextAttribs = kEGLContextAttribsForOpenGLES; | 72 EGL_OPENGL_API, |
| 73 } | 73 EGL_OPENGL_BIT, |
| 74 | 74 kDesktop_GrGLBinding |
| 75 if (!apiBound) { | 75 }, |
| 76 return NULL; | 76 { // OpenGL ES. This seems to work for both ES2 and 3 (when available)
. |
| 77 } | 77 kEGLContextAttribsForOpenGLES, |
| 78 | 78 EGL_OPENGL_ES_API, |
| 79 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 79 EGL_OPENGL_ES2_BIT, |
| 80 | 80 kES_GrGLBinding |
| 81 EGLint majorVersion; | 81 }, |
| 82 EGLint minorVersion; | |
| 83 eglInitialize(fDisplay, &majorVersion, &minorVersion); | |
| 84 | |
| 85 EGLint numConfigs; | |
| 86 const EGLint configAttribs[] = { | |
| 87 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, | |
| 88 EGL_RENDERABLE_TYPE, renderableTypeBit, | |
| 89 EGL_RED_SIZE, 8, | |
| 90 EGL_GREEN_SIZE, 8, | |
| 91 EGL_BLUE_SIZE, 8, | |
| 92 EGL_ALPHA_SIZE, 8, | |
| 93 EGL_NONE | |
| 94 }; | 82 }; |
| 95 | 83 |
| 96 EGLConfig surfaceConfig; | 84 const GrGLInterface* interface = NULL; |
| 97 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs
)) { | |
| 98 SkDebugf("eglChooseConfig failed.\n"); | |
| 99 return NULL; | |
| 100 } | |
| 101 | 85 |
| 102 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs); | 86 for (size_t api = 0; NULL == interface && api < SK_ARRAY_COUNT(kAPIs); ++api
) { |
| 103 if (EGL_NO_CONTEXT == fContext) { | 87 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 104 SkDebugf("eglCreateContext failed.\n"); | |
| 105 return NULL; | |
| 106 } | |
| 107 | 88 |
| 108 static const EGLint kSurfaceAttribs[] = { | 89 EGLint majorVersion; |
| 109 EGL_WIDTH, 1, | 90 EGLint minorVersion; |
| 110 EGL_HEIGHT, 1, | 91 eglInitialize(fDisplay, &majorVersion, &minorVersion); |
| 111 EGL_NONE | |
| 112 }; | |
| 113 | 92 |
| 114 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs)
; | 93 #if 0 |
| 115 if (EGL_NO_SURFACE == fSurface) { | 94 SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR)); |
| 116 SkDebugf("eglCreatePbufferSurface failed.\n"); | 95 SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS)); |
| 117 this->destroyGLContext(); | 96 SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION)); |
| 118 return NULL; | 97 SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS)); |
| 119 } | 98 #endif |
| 120 | 99 |
| 121 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | 100 if (!eglBindAPI(kAPIs[api].fAPI)) { |
| 122 SkDebugf("eglMakeCurrent failed.\n"); | 101 continue; |
| 123 this->destroyGLContext(); | 102 } |
| 124 return NULL; | |
| 125 } | |
| 126 | 103 |
| 127 const GrGLInterface* interface = GrGLCreateNativeInterface(); | 104 EGLint numConfigs; |
| 128 if (!interface) { | 105 const EGLint configAttribs[] = { |
| 129 SkDebugf("Failed to create gl interface.\n"); | 106 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 130 this->destroyGLContext(); | 107 EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit, |
| 131 return NULL; | 108 EGL_RED_SIZE, 8, |
| 109 EGL_GREEN_SIZE, 8, |
| 110 EGL_BLUE_SIZE, 8, |
| 111 EGL_ALPHA_SIZE, 8, |
| 112 EGL_NONE |
| 113 }; |
| 114 |
| 115 EGLConfig surfaceConfig; |
| 116 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numCon
figs)) { |
| 117 SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError(
)); |
| 118 continue; |
| 119 } |
| 120 |
| 121 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fC
ontextAttribs); |
| 122 if (EGL_NO_CONTEXT == fContext) { |
| 123 SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetErro
r()); |
| 124 continue; |
| 125 } |
| 126 |
| 127 static const EGLint kSurfaceAttribs[] = { |
| 128 EGL_WIDTH, 1, |
| 129 EGL_HEIGHT, 1, |
| 130 EGL_NONE |
| 131 }; |
| 132 |
| 133 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttr
ibs); |
| 134 if (EGL_NO_SURFACE == fSurface) { |
| 135 SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglG
etError()); |
| 136 this->destroyGLContext(); |
| 137 continue; |
| 138 } |
| 139 |
| 140 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
| 141 SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError(
)); |
| 142 this->destroyGLContext(); |
| 143 continue; |
| 144 } |
| 145 |
| 146 interface = GrGLCreateNativeInterface(); |
| 147 if (NULL == interface) { |
| 148 SkDebugf("Failed to create gl interface.\n"); |
| 149 this->destroyGLContext(); |
| 150 continue; |
| 151 } |
| 152 |
| 153 if (!interface->validate(kAPIs[api].fBinding)) { |
| 154 interface->unref(); |
| 155 interface = NULL; |
| 156 this->destroyGLContext(); |
| 157 } |
| 132 } | 158 } |
| 133 | 159 |
| 134 return interface; | 160 return interface; |
| 135 } | 161 } |
| 136 | 162 |
| 137 void SkNativeGLContext::makeCurrent() const { | 163 void SkNativeGLContext::makeCurrent() const { |
| 138 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | 164 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
| 139 SkDebugf("Could not set the context.\n"); | 165 SkDebugf("Could not set the context.\n"); |
| 140 } | 166 } |
| 141 } | 167 } |
| OLD | NEW |