robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 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 | |
| 9 | #include "gl/SkANGLEGLContext.h" |
| 10 | |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 11 | SkANGLEGLContext::SkANGLEGLContext() |
| 12 | : fContext(EGL_NO_CONTEXT) |
| 13 | , fDisplay(EGL_NO_DISPLAY) |
| 14 | , fSurface(EGL_NO_SURFACE) { |
| 15 | } |
| 16 | |
| 17 | SkANGLEGLContext::~SkANGLEGLContext() { |
| 18 | this->destroyGLContext(); |
| 19 | } |
| 20 | |
| 21 | void SkANGLEGLContext::destroyGLContext() { |
| 22 | if (fDisplay) { |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 23 | eglMakeCurrent(fDisplay, 0, 0, 0); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 24 | |
| 25 | if (fContext) { |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 26 | eglDestroyContext(fDisplay, fContext); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 27 | fContext = EGL_NO_CONTEXT; |
| 28 | } |
| 29 | |
| 30 | if (fSurface) { |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 31 | eglDestroySurface(fDisplay, fSurface); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 32 | fSurface = EGL_NO_SURFACE; |
| 33 | } |
| 34 | |
| 35 | //TODO should we close the display? |
| 36 | fDisplay = EGL_NO_DISPLAY; |
| 37 | } |
| 38 | } |
| 39 | |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 40 | const GrGLInterface* SkANGLEGLContext::createGLContext(GrGLStandard forcedGpuAPI) { |
| 41 | if (kGL_GrGLStandard == forcedGpuAPI) { |
| 42 | return NULL; |
| 43 | } |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 44 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 45 | fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 46 | |
| 47 | EGLint majorVersion; |
| 48 | EGLint minorVersion; |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 49 | eglInitialize(fDisplay, &majorVersion, &minorVersion); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 50 | |
| 51 | EGLint numConfigs; |
| 52 | static const EGLint configAttribs[] = { |
| 53 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 54 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 55 | EGL_RED_SIZE, 8, |
| 56 | EGL_GREEN_SIZE, 8, |
| 57 | EGL_BLUE_SIZE, 8, |
| 58 | EGL_ALPHA_SIZE, 8, |
| 59 | EGL_NONE |
| 60 | }; |
| 61 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 62 | EGLConfig surfaceConfig; |
| 63 | eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 64 | |
| 65 | static const EGLint contextAttribs[] = { |
| 66 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 67 | EGL_NONE |
| 68 | }; |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 69 | fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | static const EGLint surfaceAttribs[] = { |
| 73 | EGL_WIDTH, 1, |
| 74 | EGL_HEIGHT, 1, |
| 75 | EGL_NONE |
| 76 | }; |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 77 | fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 78 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 79 | eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 80 | |
| 81 | const GrGLInterface* interface = GrGLCreateANGLEInterface(); |
| 82 | if (NULL == interface) { |
| 83 | SkDebugf("Could not create ANGLE GL interface!\n"); |
| 84 | this->destroyGLContext(); |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | return interface; |
| 89 | } |
| 90 | |
| 91 | void SkANGLEGLContext::makeCurrent() const { |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 92 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 93 | SkDebugf("Could not set the context.\n"); |
| 94 | } |
| 95 | } |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 96 | |
| 97 | void SkANGLEGLContext::swapBuffers() const { |
| 98 | if (!eglSwapBuffers(fDisplay, fSurface)) { |
| 99 | SkDebugf("Could not complete eglSwapBuffers.\n"); |
| 100 | } |
| 101 | } |