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