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 | |
djsollen | e454521 | 2014-11-13 11:12:41 -0800 | [diff] [blame] | 9 | #include "gl/angle/SkANGLEGLContext.h" |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 10 | |
bsalomon | 93276c6 | 2015-02-23 10:51:13 -0800 | [diff] [blame] | 11 | #include <EGL/egl.h> |
hendrikw | 266c232 | 2015-07-24 06:52:40 -0700 | [diff] [blame] | 12 | #include <EGL/eglext.h> |
bsalomon | 93276c6 | 2015-02-23 10:51:13 -0800 | [diff] [blame] | 13 | |
hendrikw | 10b371c | 2015-07-21 14:50:07 -0700 | [diff] [blame] | 14 | #define EGL_PLATFORM_ANGLE_ANGLE 0x3202 |
| 15 | #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203 |
| 16 | #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207 |
| 17 | #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208 |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 18 | #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 19 | |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 20 | void* SkANGLEGLContext::GetD3DEGLDisplay(void* nativeDisplay, bool useGLBackend) { |
hendrikw | 266c232 | 2015-07-24 06:52:40 -0700 | [diff] [blame] | 21 | PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 22 | eglGetPlatformDisplayEXT = |
hendrikw | 266c232 | 2015-07-24 06:52:40 -0700 | [diff] [blame] | 23 | (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT"); |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 24 | |
| 25 | if (!eglGetPlatformDisplayEXT) { |
bsalomon | 93276c6 | 2015-02-23 10:51:13 -0800 | [diff] [blame] | 26 | return eglGetDisplay(static_cast<EGLNativeDisplayType>(nativeDisplay)); |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 27 | } |
| 28 | |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 29 | EGLDisplay display = EGL_NO_DISPLAY; |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 30 | if (useGLBackend) { |
| 31 | // Try for an ANGLE D3D11 context, fall back to D3D9. |
| 32 | EGLint attribs[3] = { |
| 33 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, |
| 34 | EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, |
| 35 | EGL_NONE |
| 36 | }; |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 37 | display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 38 | nativeDisplay, attribs); |
| 39 | } else { |
| 40 | // Try for an ANGLE D3D11 context, fall back to D3D9, and finally GL. |
| 41 | EGLint attribs[3][3] = { |
| 42 | { |
| 43 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, |
| 44 | EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, |
| 45 | EGL_NONE |
| 46 | }, |
| 47 | { |
| 48 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, |
| 49 | EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE, |
| 50 | EGL_NONE |
| 51 | }, |
| 52 | { |
| 53 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, |
| 54 | EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, |
| 55 | EGL_NONE |
| 56 | } |
| 57 | }; |
| 58 | for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) { |
| 59 | display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, |
| 60 | nativeDisplay, attribs[i]); |
| 61 | } |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 62 | } |
| 63 | return display; |
| 64 | } |
| 65 | |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 66 | SkANGLEGLContext::SkANGLEGLContext(bool useGLBackend) |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 67 | : fContext(EGL_NO_CONTEXT) |
| 68 | , fDisplay(EGL_NO_DISPLAY) |
| 69 | , fSurface(EGL_NO_SURFACE) { |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 70 | |
| 71 | EGLint numConfigs; |
| 72 | static const EGLint configAttribs[] = { |
| 73 | EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, |
| 74 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 75 | EGL_RED_SIZE, 8, |
| 76 | EGL_GREEN_SIZE, 8, |
| 77 | EGL_BLUE_SIZE, 8, |
| 78 | EGL_ALPHA_SIZE, 8, |
| 79 | EGL_NONE |
| 80 | }; |
| 81 | |
hendrikw | eddbefb | 2015-09-11 13:07:29 -0700 | [diff] [blame^] | 82 | fDisplay = GetD3DEGLDisplay(EGL_DEFAULT_DISPLAY, useGLBackend); |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 83 | if (EGL_NO_DISPLAY == fDisplay) { |
| 84 | SkDebugf("Could not create EGL display!"); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | EGLint majorVersion; |
| 89 | EGLint minorVersion; |
| 90 | eglInitialize(fDisplay, &majorVersion, &minorVersion); |
| 91 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 92 | EGLConfig surfaceConfig; |
| 93 | eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 94 | |
| 95 | static const EGLint contextAttribs[] = { |
| 96 | EGL_CONTEXT_CLIENT_VERSION, 2, |
| 97 | EGL_NONE |
| 98 | }; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 100 | |
| 101 | |
| 102 | static const EGLint surfaceAttribs[] = { |
bsalomon | 4238017 | 2015-02-23 08:57:23 -0800 | [diff] [blame] | 103 | EGL_WIDTH, 1, |
| 104 | EGL_HEIGHT, 1, |
| 105 | EGL_NONE |
| 106 | }; |
| 107 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 108 | fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 109 | |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 110 | eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 111 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 112 | SkAutoTUnref<const GrGLInterface> gl(GrGLCreateANGLEInterface()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | if (nullptr == gl.get()) { |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 114 | SkDebugf("Could not create ANGLE GL interface!\n"); |
| 115 | this->destroyGLContext(); |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 116 | return; |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 117 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 118 | if (!gl->validate()) { |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 119 | SkDebugf("Could not validate ANGLE GL interface!\n"); |
| 120 | this->destroyGLContext(); |
| 121 | return; |
| 122 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 123 | |
| 124 | this->init(gl.detach()); |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 125 | } |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 126 | |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 127 | SkANGLEGLContext::~SkANGLEGLContext() { |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 128 | this->teardown(); |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 129 | this->destroyGLContext(); |
| 130 | } |
| 131 | |
| 132 | void SkANGLEGLContext::destroyGLContext() { |
kkinnunen | 30bc88c | 2014-10-15 23:03:54 -0700 | [diff] [blame] | 133 | if (fDisplay) { |
| 134 | eglMakeCurrent(fDisplay, 0, 0, 0); |
| 135 | |
| 136 | if (fContext) { |
| 137 | eglDestroyContext(fDisplay, fContext); |
| 138 | fContext = EGL_NO_CONTEXT; |
| 139 | } |
| 140 | |
| 141 | if (fSurface) { |
| 142 | eglDestroySurface(fDisplay, fSurface); |
| 143 | fSurface = EGL_NO_SURFACE; |
| 144 | } |
| 145 | |
| 146 | //TODO should we close the display? |
| 147 | fDisplay = EGL_NO_DISPLAY; |
| 148 | } |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 149 | } |
| 150 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 151 | void SkANGLEGLContext::onPlatformMakeCurrent() const { |
robertphillips@google.com | d5c8fe6 | 2012-04-02 15:04:16 +0000 | [diff] [blame] | 152 | if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { |
robertphillips@google.com | d3b9fbb | 2012-03-28 16:19:11 +0000 | [diff] [blame] | 153 | SkDebugf("Could not set the context.\n"); |
| 154 | } |
| 155 | } |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 156 | |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 157 | void SkANGLEGLContext::onPlatformSwapBuffers() const { |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 158 | if (!eglSwapBuffers(fDisplay, fSurface)) { |
| 159 | SkDebugf("Could not complete eglSwapBuffers.\n"); |
| 160 | } |
| 161 | } |
cdalton | d416a5b | 2015-06-23 13:23:44 -0700 | [diff] [blame] | 162 | |
| 163 | GrGLFuncPtr SkANGLEGLContext::onPlatformGetProcAddress(const char* name) const { |
| 164 | return eglGetProcAddress(name); |
| 165 | } |