djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 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 | */ |
tomhudson@google.com | 6c8c34e | 2012-02-14 15:31:03 +0000 | [diff] [blame] | 8 | #include "gl/SkNativeGLContext.h" |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 9 | |
| 10 | SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
| 11 | fOldEGLContext = eglGetCurrentContext(); |
| 12 | fOldDisplay = eglGetCurrentDisplay(); |
| 13 | fOldSurface = eglGetCurrentSurface(EGL_DRAW); |
| 14 | |
| 15 | } |
| 16 | |
| 17 | SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 18 | if (fOldDisplay) { |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 19 | eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 25 | SkNativeGLContext::SkNativeGLContext() |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 26 | : fContext(EGL_NO_CONTEXT) |
| 27 | , fDisplay(EGL_NO_DISPLAY) |
| 28 | , fSurface(EGL_NO_SURFACE) { |
| 29 | } |
| 30 | |
| 31 | SkNativeGLContext::~SkNativeGLContext() { |
| 32 | this->destroyGLContext(); |
| 33 | } |
| 34 | |
| 35 | void SkNativeGLContext::destroyGLContext() { |
| 36 | if (fDisplay) { |
| 37 | eglMakeCurrent(fDisplay, 0, 0, 0); |
| 38 | |
| 39 | if (fContext) { |
| 40 | eglDestroyContext(fDisplay, fContext); |
| 41 | fContext = EGL_NO_CONTEXT; |
| 42 | } |
| 43 | |
| 44 | if (fSurface) { |
| 45 | eglDestroySurface(fDisplay, fSurface); |
| 46 | fSurface = EGL_NO_SURFACE; |
| 47 | } |
| 48 | |
| 49 | //TODO should we close the display? |
| 50 | fDisplay = EGL_NO_DISPLAY; |
| 51 | } |
| 52 | } |
| 53 | |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 54 | const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { |
commit-bot@chromium.org | f5897f8 | 2013-09-03 17:50:50 +0000 | [diff] [blame] | 55 | static const EGLint kEGLContextAttribsForOpenGL[] = { |
| 56 | EGL_NONE |
| 57 | }; |
| 58 | |
| 59 | static const EGLint kEGLContextAttribsForOpenGLES[] = { |
| 60 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 61 | EGL_NONE |
| 62 | }; |
| 63 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 64 | static const struct { |
| 65 | const EGLint* fContextAttribs; |
| 66 | EGLenum fAPI; |
| 67 | EGLint fRenderableTypeBit; |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 68 | GrGLStandard fStandard; |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 69 | } kAPIs[] = { |
| 70 | { // OpenGL |
| 71 | kEGLContextAttribsForOpenGL, |
| 72 | EGL_OPENGL_API, |
| 73 | EGL_OPENGL_BIT, |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 74 | kGL_GrGLStandard |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 75 | }, |
| 76 | { // OpenGL ES. This seems to work for both ES2 and 3 (when available). |
| 77 | kEGLContextAttribsForOpenGLES, |
| 78 | EGL_OPENGL_ES_API, |
| 79 | EGL_OPENGL_ES2_BIT, |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 80 | kGLES_GrGLStandard |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 81 | }, |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 84 | size_t apiLimit = SK_ARRAY_COUNT(kAPIs); |
| 85 | size_t api = 0; |
| 86 | if (forcedGpuAPI == kGL_GrGLStandard) { |
| 87 | apiLimit = 1; |
| 88 | } else if (forcedGpuAPI == kGLES_GrGLStandard) { |
| 89 | api = 1; |
| 90 | } |
| 91 | SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == forcedGpuAPI); |
| 92 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 93 | const GrGLInterface* interface = NULL; |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 94 | |
tomhudson | 579b4fb | 2014-09-10 10:45:42 -0700 | [diff] [blame^] | 95 | for (; NULL == interface && api < apiLimit; ++api) { |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 96 | fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
commit-bot@chromium.org | f5897f8 | 2013-09-03 17:50:50 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 98 | EGLint majorVersion; |
| 99 | EGLint minorVersion; |
| 100 | eglInitialize(fDisplay, &majorVersion, &minorVersion); |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 101 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 102 | #if 0 |
| 103 | SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR)); |
| 104 | SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS)); |
| 105 | SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION)); |
| 106 | SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS)); |
| 107 | #endif |
commit-bot@chromium.org | f5897f8 | 2013-09-03 17:50:50 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 109 | if (!eglBindAPI(kAPIs[api].fAPI)) { |
| 110 | continue; |
| 111 | } |
commit-bot@chromium.org | f5897f8 | 2013-09-03 17:50:50 +0000 | [diff] [blame] | 112 | |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 113 | EGLint numConfigs; |
| 114 | const EGLint configAttribs[] = { |
| 115 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 116 | EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit, |
| 117 | EGL_RED_SIZE, 8, |
| 118 | EGL_GREEN_SIZE, 8, |
| 119 | EGL_BLUE_SIZE, 8, |
| 120 | EGL_ALPHA_SIZE, 8, |
| 121 | EGL_NONE |
| 122 | }; |
| 123 | |
| 124 | EGLConfig surfaceConfig; |
| 125 | if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) { |
| 126 | SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError()); |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs); |
| 131 | if (EGL_NO_CONTEXT == fContext) { |
| 132 | SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError()); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | static const EGLint kSurfaceAttribs[] = { |
| 137 | EGL_WIDTH, 1, |
| 138 | EGL_HEIGHT, 1, |
| 139 | EGL_NONE |
| 140 | }; |
| 141 | |
| 142 | fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs); |
| 143 | if (EGL_NO_SURFACE == fSurface) { |
| 144 | SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError()); |
| 145 | this->destroyGLContext(); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
| 150 | SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError()); |
| 151 | this->destroyGLContext(); |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | interface = GrGLCreateNativeInterface(); |
| 156 | if (NULL == interface) { |
| 157 | SkDebugf("Failed to create gl interface.\n"); |
| 158 | this->destroyGLContext(); |
| 159 | continue; |
| 160 | } |
| 161 | |
commit-bot@chromium.org | 9e90aed | 2014-01-16 16:35:09 +0000 | [diff] [blame] | 162 | if (!interface->validate()) { |
commit-bot@chromium.org | 1e62727 | 2013-09-06 18:57:14 +0000 | [diff] [blame] | 163 | interface->unref(); |
| 164 | interface = NULL; |
| 165 | this->destroyGLContext(); |
| 166 | } |
commit-bot@chromium.org | f5897f8 | 2013-09-03 17:50:50 +0000 | [diff] [blame] | 167 | } |
| 168 | |
djsollen@google.com | 5862929 | 2011-11-03 13:08:29 +0000 | [diff] [blame] | 169 | return interface; |
| 170 | } |
| 171 | |
| 172 | void SkNativeGLContext::makeCurrent() const { |
| 173 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
| 174 | SkDebugf("Could not set the context.\n"); |
| 175 | } |
| 176 | } |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 177 | |
| 178 | void SkNativeGLContext::swapBuffers() const { |
| 179 | if (!eglSwapBuffers(fDisplay, fSurface)) { |
| 180 | SkDebugf("Could not complete eglSwapBuffers.\n"); |
| 181 | } |
| 182 | } |