| OLD | NEW |
| (Empty) |
| 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/SkGLContext.h" | |
| 9 | |
| 10 #include <GLES2/gl2.h> | |
| 11 #include <EGL/egl.h> | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 class EGLGLContext : public SkGLContext { | |
| 16 public: | |
| 17 EGLGLContext(); | |
| 18 | |
| 19 virtual ~EGLGLContext(); | |
| 20 | |
| 21 virtual void makeCurrent() const SK_OVERRIDE; | |
| 22 virtual void swapBuffers() const SK_OVERRIDE; | |
| 23 protected: | |
| 24 virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_O
VERRIDE; | |
| 25 virtual void destroyGLContext() SK_OVERRIDE; | |
| 26 | |
| 27 private: | |
| 28 EGLContext fContext; | |
| 29 EGLDisplay fDisplay; | |
| 30 EGLSurface fSurface; | |
| 31 }; | |
| 32 | |
| 33 EGLGLContext::EGLGLContext() | |
| 34 : fContext(EGL_NO_CONTEXT) | |
| 35 , fDisplay(EGL_NO_DISPLAY) | |
| 36 , fSurface(EGL_NO_SURFACE) { | |
| 37 } | |
| 38 | |
| 39 EGLGLContext::~EGLGLContext() { | |
| 40 this->destroyGLContext(); | |
| 41 } | |
| 42 | |
| 43 void EGLGLContext::destroyGLContext() { | |
| 44 if (fDisplay) { | |
| 45 eglMakeCurrent(fDisplay, 0, 0, 0); | |
| 46 | |
| 47 if (fContext) { | |
| 48 eglDestroyContext(fDisplay, fContext); | |
| 49 fContext = EGL_NO_CONTEXT; | |
| 50 } | |
| 51 | |
| 52 if (fSurface) { | |
| 53 eglDestroySurface(fDisplay, fSurface); | |
| 54 fSurface = EGL_NO_SURFACE; | |
| 55 } | |
| 56 | |
| 57 //TODO should we close the display? | |
| 58 fDisplay = EGL_NO_DISPLAY; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 const GrGLInterface* EGLGLContext::createGLContext(GrGLStandard forcedGpuAPI) { | |
| 63 static const EGLint kEGLContextAttribsForOpenGL[] = { | |
| 64 EGL_NONE | |
| 65 }; | |
| 66 | |
| 67 static const EGLint kEGLContextAttribsForOpenGLES[] = { | |
| 68 EGL_CONTEXT_CLIENT_VERSION, 2, | |
| 69 EGL_NONE | |
| 70 }; | |
| 71 | |
| 72 static const struct { | |
| 73 const EGLint* fContextAttribs; | |
| 74 EGLenum fAPI; | |
| 75 EGLint fRenderableTypeBit; | |
| 76 GrGLStandard fStandard; | |
| 77 } kAPIs[] = { | |
| 78 { // OpenGL | |
| 79 kEGLContextAttribsForOpenGL, | |
| 80 EGL_OPENGL_API, | |
| 81 EGL_OPENGL_BIT, | |
| 82 kGL_GrGLStandard | |
| 83 }, | |
| 84 { // OpenGL ES. This seems to work for both ES2 and 3 (when available)
. | |
| 85 kEGLContextAttribsForOpenGLES, | |
| 86 EGL_OPENGL_ES_API, | |
| 87 EGL_OPENGL_ES2_BIT, | |
| 88 kGLES_GrGLStandard | |
| 89 }, | |
| 90 }; | |
| 91 | |
| 92 size_t apiLimit = SK_ARRAY_COUNT(kAPIs); | |
| 93 size_t api = 0; | |
| 94 if (forcedGpuAPI == kGL_GrGLStandard) { | |
| 95 apiLimit = 1; | |
| 96 } else if (forcedGpuAPI == kGLES_GrGLStandard) { | |
| 97 api = 1; | |
| 98 } | |
| 99 SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == force
dGpuAPI); | |
| 100 | |
| 101 const GrGLInterface* interface = NULL; | |
| 102 | |
| 103 for (; NULL == interface && api < apiLimit; ++api) { | |
| 104 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
| 105 | |
| 106 EGLint majorVersion; | |
| 107 EGLint minorVersion; | |
| 108 eglInitialize(fDisplay, &majorVersion, &minorVersion); | |
| 109 | |
| 110 #if 0 | |
| 111 SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR)); | |
| 112 SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS)); | |
| 113 SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION)); | |
| 114 SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS)); | |
| 115 #endif | |
| 116 | |
| 117 if (!eglBindAPI(kAPIs[api].fAPI)) { | |
| 118 continue; | |
| 119 } | |
| 120 | |
| 121 EGLint numConfigs; | |
| 122 const EGLint configAttribs[] = { | |
| 123 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, | |
| 124 EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit, | |
| 125 EGL_RED_SIZE, 8, | |
| 126 EGL_GREEN_SIZE, 8, | |
| 127 EGL_BLUE_SIZE, 8, | |
| 128 EGL_ALPHA_SIZE, 8, | |
| 129 EGL_NONE | |
| 130 }; | |
| 131 | |
| 132 EGLConfig surfaceConfig; | |
| 133 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numCon
figs)) { | |
| 134 SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError(
)); | |
| 135 continue; | |
| 136 } | |
| 137 | |
| 138 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fC
ontextAttribs); | |
| 139 if (EGL_NO_CONTEXT == fContext) { | |
| 140 SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetErro
r()); | |
| 141 continue; | |
| 142 } | |
| 143 | |
| 144 static const EGLint kSurfaceAttribs[] = { | |
| 145 EGL_WIDTH, 1, | |
| 146 EGL_HEIGHT, 1, | |
| 147 EGL_NONE | |
| 148 }; | |
| 149 | |
| 150 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttr
ibs); | |
| 151 if (EGL_NO_SURFACE == fSurface) { | |
| 152 SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglG
etError()); | |
| 153 this->destroyGLContext(); | |
| 154 continue; | |
| 155 } | |
| 156 | |
| 157 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | |
| 158 SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError(
)); | |
| 159 this->destroyGLContext(); | |
| 160 continue; | |
| 161 } | |
| 162 | |
| 163 interface = GrGLCreateNativeInterface(); | |
| 164 if (NULL == interface) { | |
| 165 SkDebugf("Failed to create gl interface.\n"); | |
| 166 this->destroyGLContext(); | |
| 167 continue; | |
| 168 } | |
| 169 | |
| 170 if (!interface->validate()) { | |
| 171 interface->unref(); | |
| 172 interface = NULL; | |
| 173 this->destroyGLContext(); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 return interface; | |
| 178 } | |
| 179 | |
| 180 void EGLGLContext::makeCurrent() const { | |
| 181 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | |
| 182 SkDebugf("Could not set the context.\n"); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void EGLGLContext::swapBuffers() const { | |
| 187 if (!eglSwapBuffers(fDisplay, fSurface)) { | |
| 188 SkDebugf("Could not complete eglSwapBuffers.\n"); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 } // anonymous namespace | |
| 193 | |
| 194 SkGLContext* CreatePlatformGLContext() { | |
| 195 return SkNEW(EGLGLContext); | |
| 196 } | |
| 197 | |
| OLD | NEW |