| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2012 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "gl/angle/SkANGLEGLContext.h" | |
| 9 | |
| 10 #include <EGL/egl.h> | |
| 11 #include <EGL/eglext.h> | |
| 12 | |
| 13 #include "gl/GrGLDefines.h" | |
| 14 #include "gl/GrGLUtil.h" | |
| 15 | |
| 16 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202 | |
| 17 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203 | |
| 18 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207 | |
| 19 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208 | |
| 20 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D | |
| 21 | |
| 22 void* SkANGLEGLContext::GetD3DEGLDisplay(void* nativeDisplay, bool useGLBackend)
{ | |
| 23 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; | |
| 24 eglGetPlatformDisplayEXT = | |
| 25 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDispla
yEXT"); | |
| 26 | |
| 27 if (!eglGetPlatformDisplayEXT) { | |
| 28 return eglGetDisplay(static_cast<EGLNativeDisplayType>(nativeDisplay)); | |
| 29 } | |
| 30 | |
| 31 EGLDisplay display = EGL_NO_DISPLAY; | |
| 32 if (useGLBackend) { | |
| 33 // Try for an ANGLE D3D11 context, fall back to D3D9. | |
| 34 EGLint attribs[3] = { | |
| 35 EGL_PLATFORM_ANGLE_TYPE_ANGLE, | |
| 36 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, | |
| 37 EGL_NONE | |
| 38 }; | |
| 39 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDispl
ay, attribs); | |
| 40 } else { | |
| 41 // Try for an ANGLE D3D11 context, fall back to D3D9, and finally GL. | |
| 42 EGLint attribs[3][3] = { | |
| 43 { | |
| 44 EGL_PLATFORM_ANGLE_TYPE_ANGLE, | |
| 45 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, | |
| 46 EGL_NONE | |
| 47 }, | |
| 48 { | |
| 49 EGL_PLATFORM_ANGLE_TYPE_ANGLE, | |
| 50 EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE, | |
| 51 EGL_NONE | |
| 52 }, | |
| 53 { | |
| 54 EGL_PLATFORM_ANGLE_TYPE_ANGLE, | |
| 55 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, | |
| 56 EGL_NONE | |
| 57 } | |
| 58 }; | |
| 59 for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) { | |
| 60 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,nativeDi
splay, attribs[i]); | |
| 61 } | |
| 62 } | |
| 63 return display; | |
| 64 } | |
| 65 | |
| 66 SkANGLEGLContext::SkANGLEGLContext(bool useGLBackend) | |
| 67 : fContext(EGL_NO_CONTEXT) | |
| 68 , fDisplay(EGL_NO_DISPLAY) | |
| 69 , fSurface(EGL_NO_SURFACE) { | |
| 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 | |
| 82 fIsGLBackend = useGLBackend; | |
| 83 fDisplay = GetD3DEGLDisplay(EGL_DEFAULT_DISPLAY, useGLBackend); | |
| 84 if (EGL_NO_DISPLAY == fDisplay) { | |
| 85 SkDebugf("Could not create EGL display!"); | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 EGLint majorVersion; | |
| 90 EGLint minorVersion; | |
| 91 eglInitialize(fDisplay, &majorVersion, &minorVersion); | |
| 92 | |
| 93 EGLConfig surfaceConfig; | |
| 94 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); | |
| 95 | |
| 96 static const EGLint contextAttribs[] = { | |
| 97 EGL_CONTEXT_CLIENT_VERSION, 2, | |
| 98 EGL_NONE | |
| 99 }; | |
| 100 fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs
); | |
| 101 | |
| 102 | |
| 103 static const EGLint surfaceAttribs[] = { | |
| 104 EGL_WIDTH, 1, | |
| 105 EGL_HEIGHT, 1, | |
| 106 EGL_NONE | |
| 107 }; | |
| 108 | |
| 109 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); | |
| 110 | |
| 111 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); | |
| 112 | |
| 113 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateANGLEInterface()); | |
| 114 if (nullptr == gl.get()) { | |
| 115 SkDebugf("Could not create ANGLE GL interface!\n"); | |
| 116 this->destroyGLContext(); | |
| 117 return; | |
| 118 } | |
| 119 if (!gl->validate()) { | |
| 120 SkDebugf("Could not validate ANGLE GL interface!\n"); | |
| 121 this->destroyGLContext(); | |
| 122 return; | |
| 123 } | |
| 124 | |
| 125 this->init(gl.release()); | |
| 126 } | |
| 127 | |
| 128 SkANGLEGLContext::~SkANGLEGLContext() { | |
| 129 this->teardown(); | |
| 130 this->destroyGLContext(); | |
| 131 } | |
| 132 | |
| 133 GrEGLImage SkANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const { | |
| 134 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { | |
| 135 return GR_EGL_NO_IMAGE; | |
| 136 } | |
| 137 GrEGLImage img; | |
| 138 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, | |
| 139 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE, | |
| 140 GR_EGL_NONE }; | |
| 141 // 64 bit cast is to shut Visual C++ up about casting 32 bit value to a poin
ter. | |
| 142 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64
_t)texID); | |
| 143 GR_GL_CALL_RET(this->gl(), img, | |
| 144 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie
ntBuffer, | |
| 145 attribs)); | |
| 146 return img; | |
| 147 } | |
| 148 | |
| 149 void SkANGLEGLContext::destroyEGLImage(GrEGLImage image) const { | |
| 150 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image)); | |
| 151 } | |
| 152 | |
| 153 GrGLuint SkANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const { | |
| 154 GrGLClearErr(this->gl()); | |
| 155 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) { | |
| 156 return 0; | |
| 157 } | |
| 158 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage); | |
| 159 | |
| 160 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D = | |
| 161 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2
DOES"); | |
| 162 if (!glEGLImageTargetTexture2D) { | |
| 163 return 0; | |
| 164 } | |
| 165 GrGLuint texID; | |
| 166 GR_GL_CALL(this->gl(), GenTextures(1, &texID)); | |
| 167 if (!texID) { | |
| 168 return 0; | |
| 169 } | |
| 170 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID)); | |
| 171 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { | |
| 172 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); | |
| 173 return 0; | |
| 174 } | |
| 175 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image); | |
| 176 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { | |
| 177 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); | |
| 178 return 0; | |
| 179 } | |
| 180 return texID; | |
| 181 } | |
| 182 | |
| 183 SkGLContext* SkANGLEGLContext::createNew() const { | |
| 184 #ifdef SK_BUILD_FOR_WIN | |
| 185 SkGLContext* ctx = fIsGLBackend ? SkANGLEGLContext::CreateOpenGL() | |
| 186 : SkANGLEGLContext::CreateDirectX(); | |
| 187 #else | |
| 188 SkGLContext* ctx = SkANGLEGLContext::CreateOpenGL(); | |
| 189 #endif | |
| 190 if (ctx) { | |
| 191 ctx->makeCurrent(); | |
| 192 } | |
| 193 return ctx; | |
| 194 } | |
| 195 | |
| 196 void SkANGLEGLContext::destroyGLContext() { | |
| 197 if (fDisplay) { | |
| 198 eglMakeCurrent(fDisplay, 0, 0, 0); | |
| 199 | |
| 200 if (fContext) { | |
| 201 eglDestroyContext(fDisplay, fContext); | |
| 202 fContext = EGL_NO_CONTEXT; | |
| 203 } | |
| 204 | |
| 205 if (fSurface) { | |
| 206 eglDestroySurface(fDisplay, fSurface); | |
| 207 fSurface = EGL_NO_SURFACE; | |
| 208 } | |
| 209 | |
| 210 //TODO should we close the display? | |
| 211 fDisplay = EGL_NO_DISPLAY; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void SkANGLEGLContext::onPlatformMakeCurrent() const { | |
| 216 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | |
| 217 SkDebugf("Could not set the context.\n"); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 void SkANGLEGLContext::onPlatformSwapBuffers() const { | |
| 222 if (!eglSwapBuffers(fDisplay, fSurface)) { | |
| 223 SkDebugf("Could not complete eglSwapBuffers.\n"); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 GrGLFuncPtr SkANGLEGLContext::onPlatformGetProcAddress(const char* name) const { | |
| 228 return eglGetProcAddress(name); | |
| 229 } | |
| OLD | NEW |