Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: tools/gpu/gl/angle/GLContext_angle.cpp

Issue 1815823002: Move SkGLContext and some GrGLInterface implementations to skgputest module (Closed) Base URL: https://chromium.googlesource.com/skia.git@debugobject
Patch Set: fix windows and android? Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1
1 /* 2 /*
2 * Copyright 2012 Google Inc. 3 * Copyright 2012 Google Inc.
3 * 4 *
4 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 6 * found in the LICENSE file.
6 */ 7 */
7 8
8 #include "gl/angle/SkANGLEGLContext.h" 9 #include "GLContext_angle.h"
9 10
10 #include <EGL/egl.h> 11 #include <EGL/egl.h>
11 #include <EGL/eglext.h> 12 #include <EGL/eglext.h>
12 13
13 #include "gl/GrGLDefines.h" 14 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLUtil.h" 15 #include "gl/GrGLUtil.h"
15 16
17 #include "gl/GrGLInterface.h"
18 #include "gl/GrGLAssembleInterface.h"
19 #include "../ports/SkOSLibrary.h"
20
21 #include <EGL/egl.h>
22
16 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202 23 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
17 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203 24 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
18 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207 25 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207
19 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208 26 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
20 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D 27 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D
21 28
22 void* SkANGLEGLContext::GetD3DEGLDisplay(void* nativeDisplay, bool useGLBackend) { 29 namespace {
30 struct Libs {
31 void* fGLLib;
32 void* fEGLLib;
33 };
34
35 static GrGLFuncPtr angle_get_gl_proc(void* ctx, const char name[]) {
36 const Libs* libs = reinterpret_cast<const Libs*>(ctx);
37 GrGLFuncPtr proc = (GrGLFuncPtr) GetProcedureAddress(libs->fGLLib, name);
38 if (proc) {
39 return proc;
40 }
41 proc = (GrGLFuncPtr) GetProcedureAddress(libs->fEGLLib, name);
42 if (proc) {
43 return proc;
44 }
45 return eglGetProcAddress(name);
46 }
47
48 void* get_angle_egl_display(void* nativeDisplay, bool useGLBackend) {
23 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; 49 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT;
24 eglGetPlatformDisplayEXT = 50 eglGetPlatformDisplayEXT =
25 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDispla yEXT"); 51 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDispla yEXT");
26 52
53 // We expect ANGLE to support this extension
27 if (!eglGetPlatformDisplayEXT) { 54 if (!eglGetPlatformDisplayEXT) {
28 return eglGetDisplay(static_cast<EGLNativeDisplayType>(nativeDisplay)); 55 return EGL_NO_DISPLAY;
29 } 56 }
30 57
31 EGLDisplay display = EGL_NO_DISPLAY; 58 EGLDisplay display = EGL_NO_DISPLAY;
32 if (useGLBackend) { 59 if (useGLBackend) {
33 // Try for an ANGLE D3D11 context, fall back to D3D9.
34 EGLint attribs[3] = { 60 EGLint attribs[3] = {
35 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 61 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
36 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, 62 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE,
37 EGL_NONE 63 EGL_NONE
38 }; 64 };
39 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDispl ay, attribs); 65 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDispl ay, attribs);
40 } else { 66 } else {
41 // Try for an ANGLE D3D11 context, fall back to D3D9, and finally GL. 67 // Try for an ANGLE D3D11 context, fall back to D3D9.
42 EGLint attribs[3][3] = { 68 EGLint attribs[3][3] = {
43 { 69 {
44 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 70 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
45 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, 71 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
46 EGL_NONE 72 EGL_NONE
47 }, 73 },
48 { 74 {
49 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 75 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
50 EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE, 76 EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE,
51 EGL_NONE 77 EGL_NONE
52 }, 78 },
53 {
54 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
55 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE,
56 EGL_NONE
57 }
58 }; 79 };
59 for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) { 80 for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) {
60 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,nativeDi splay, attribs[i]); 81 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,nativeDi splay, attribs[i]);
61 } 82 }
62 } 83 }
63 return display; 84 return display;
64 } 85 }
65 86
66 SkANGLEGLContext::SkANGLEGLContext(bool useGLBackend) 87 class ANGLEGLContext : public sk_gpu_test::GLContext {
88 public:
89 ANGLEGLContext(bool preferGLBackend);
90 ~ANGLEGLContext() override;
91
92 GrEGLImage texture2DToEGLImage(GrGLuint texID) const override;
93 void destroyEGLImage(GrEGLImage) const override;
94 GrGLuint eglImageToExternalTexture(GrEGLImage) const override;
95 sk_gpu_test::GLContext* createNew() const override;
96
97 private:
98 void destroyGLContext();
99
100 void onPlatformMakeCurrent() const override;
101 void onPlatformSwapBuffers() const override;
102 GrGLFuncPtr onPlatformGetProcAddress(const char* name) const override;
103
104 void* fContext;
105 void* fDisplay;
106 void* fSurface;
107 bool fIsGLBackend;
108 };
109
110 ANGLEGLContext::ANGLEGLContext(bool useGLBackend)
67 : fContext(EGL_NO_CONTEXT) 111 : fContext(EGL_NO_CONTEXT)
68 , fDisplay(EGL_NO_DISPLAY) 112 , fDisplay(EGL_NO_DISPLAY)
69 , fSurface(EGL_NO_SURFACE) { 113 , fSurface(EGL_NO_SURFACE) {
70 114
71 EGLint numConfigs; 115 EGLint numConfigs;
72 static const EGLint configAttribs[] = { 116 static const EGLint configAttribs[] = {
73 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 117 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
74 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 118 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
75 EGL_RED_SIZE, 8, 119 EGL_RED_SIZE, 8,
76 EGL_GREEN_SIZE, 8, 120 EGL_GREEN_SIZE, 8,
77 EGL_BLUE_SIZE, 8, 121 EGL_BLUE_SIZE, 8,
78 EGL_ALPHA_SIZE, 8, 122 EGL_ALPHA_SIZE, 8,
79 EGL_NONE 123 EGL_NONE
80 }; 124 };
81 125
82 fIsGLBackend = useGLBackend; 126 fIsGLBackend = useGLBackend;
83 fDisplay = GetD3DEGLDisplay(EGL_DEFAULT_DISPLAY, useGLBackend); 127 fDisplay = get_angle_egl_display(EGL_DEFAULT_DISPLAY, useGLBackend);
84 if (EGL_NO_DISPLAY == fDisplay) { 128 if (EGL_NO_DISPLAY == fDisplay) {
85 SkDebugf("Could not create EGL display!"); 129 SkDebugf("Could not create EGL display!");
86 return; 130 return;
87 } 131 }
88 132
89 EGLint majorVersion; 133 EGLint majorVersion;
90 EGLint minorVersion; 134 EGLint minorVersion;
91 eglInitialize(fDisplay, &majorVersion, &minorVersion); 135 eglInitialize(fDisplay, &majorVersion, &minorVersion);
92 136
93 EGLConfig surfaceConfig; 137 EGLConfig surfaceConfig;
94 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); 138 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
95 139
96 static const EGLint contextAttribs[] = { 140 static const EGLint contextAttribs[] = {
97 EGL_CONTEXT_CLIENT_VERSION, 2, 141 EGL_CONTEXT_CLIENT_VERSION, 2,
98 EGL_NONE 142 EGL_NONE
99 }; 143 };
100 fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs ); 144 fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs );
101 145
102 146
103 static const EGLint surfaceAttribs[] = { 147 static const EGLint surfaceAttribs[] = {
104 EGL_WIDTH, 1, 148 EGL_WIDTH, 1,
105 EGL_HEIGHT, 1, 149 EGL_HEIGHT, 1,
106 EGL_NONE 150 EGL_NONE
107 }; 151 };
108 152
109 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); 153 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
110 154
111 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); 155 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
112 156
113 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateANGLEInterface()); 157 SkAutoTUnref<const GrGLInterface> gl(sk_gpu_test::CreateANGLEGLInterface());
114 if (nullptr == gl.get()) { 158 if (nullptr == gl.get()) {
115 SkDebugf("Could not create ANGLE GL interface!\n"); 159 SkDebugf("Could not create ANGLE GL interface!\n");
116 this->destroyGLContext(); 160 this->destroyGLContext();
117 return; 161 return;
118 } 162 }
119 if (!gl->validate()) { 163 if (!gl->validate()) {
120 SkDebugf("Could not validate ANGLE GL interface!\n"); 164 SkDebugf("Could not validate ANGLE GL interface!\n");
121 this->destroyGLContext(); 165 this->destroyGLContext();
122 return; 166 return;
123 } 167 }
124 168
125 this->init(gl.release()); 169 this->init(gl.release());
126 } 170 }
127 171
128 SkANGLEGLContext::~SkANGLEGLContext() { 172 ANGLEGLContext::~ANGLEGLContext() {
129 this->teardown(); 173 this->teardown();
130 this->destroyGLContext(); 174 this->destroyGLContext();
131 } 175 }
132 176
133 GrEGLImage SkANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const { 177 GrEGLImage ANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const {
134 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { 178 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
135 return GR_EGL_NO_IMAGE; 179 return GR_EGL_NO_IMAGE;
136 } 180 }
137 GrEGLImage img; 181 GrEGLImage img;
138 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, 182 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0,
139 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE, 183 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE,
140 GR_EGL_NONE }; 184 GR_EGL_NONE };
141 // 64 bit cast is to shut Visual C++ up about casting 32 bit value to a poin ter. 185 // 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); 186 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64 _t)texID);
143 GR_GL_CALL_RET(this->gl(), img, 187 GR_GL_CALL_RET(this->gl(), img,
144 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie ntBuffer, 188 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie ntBuffer,
145 attribs)); 189 attribs));
146 return img; 190 return img;
147 } 191 }
148 192
149 void SkANGLEGLContext::destroyEGLImage(GrEGLImage image) const { 193 void ANGLEGLContext::destroyEGLImage(GrEGLImage image) const {
150 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image)); 194 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image));
151 } 195 }
152 196
153 GrGLuint SkANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const { 197 GrGLuint ANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const {
154 GrGLClearErr(this->gl()); 198 GrGLClearErr(this->gl());
155 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) { 199 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
156 return 0; 200 return 0;
157 } 201 }
158 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage); 202 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
159
160 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D = 203 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
161 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2 DOES"); 204 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2 DOES");
162 if (!glEGLImageTargetTexture2D) { 205 if (!glEGLImageTargetTexture2D) {
163 return 0; 206 return 0;
164 } 207 }
165 GrGLuint texID; 208 GrGLuint texID;
166 GR_GL_CALL(this->gl(), GenTextures(1, &texID)); 209 GR_GL_CALL(this->gl(), GenTextures(1, &texID));
167 if (!texID) { 210 if (!texID) {
168 return 0; 211 return 0;
169 } 212 }
170 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID)); 213 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID));
171 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { 214 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
172 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); 215 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
173 return 0; 216 return 0;
174 } 217 }
175 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image); 218 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
176 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { 219 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
177 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); 220 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
178 return 0; 221 return 0;
179 } 222 }
180 return texID; 223 return texID;
181 } 224 }
182 225
183 SkGLContext* SkANGLEGLContext::createNew() const { 226 sk_gpu_test::GLContext* ANGLEGLContext::createNew() const {
184 #ifdef SK_BUILD_FOR_WIN 227 #ifdef SK_BUILD_FOR_WIN
185 SkGLContext* ctx = fIsGLBackend ? SkANGLEGLContext::CreateOpenGL() 228 sk_gpu_test::GLContext* ctx = fIsGLBackend ? sk_gpu_test::CreateANGLEOpenGLG LContext()
186 : SkANGLEGLContext::CreateDirectX(); 229 : sk_gpu_test::CreateANGLEDirect3 DGLContext();
187 #else 230 #else
188 SkGLContext* ctx = SkANGLEGLContext::CreateOpenGL(); 231 sk_gpu_test::GLContext* ctx = sk_gpu_test::CreateANGLEOpenGLGLContext();
189 #endif 232 #endif
190 if (ctx) { 233 if (ctx) {
191 ctx->makeCurrent(); 234 ctx->makeCurrent();
192 } 235 }
193 return ctx; 236 return ctx;
194 } 237 }
195 238
196 void SkANGLEGLContext::destroyGLContext() { 239 void ANGLEGLContext::destroyGLContext() {
197 if (fDisplay) { 240 if (fDisplay) {
198 eglMakeCurrent(fDisplay, 0, 0, 0); 241 eglMakeCurrent(fDisplay, 0, 0, 0);
199 242
200 if (fContext) { 243 if (fContext) {
201 eglDestroyContext(fDisplay, fContext); 244 eglDestroyContext(fDisplay, fContext);
202 fContext = EGL_NO_CONTEXT; 245 fContext = EGL_NO_CONTEXT;
203 } 246 }
204 247
205 if (fSurface) { 248 if (fSurface) {
206 eglDestroySurface(fDisplay, fSurface); 249 eglDestroySurface(fDisplay, fSurface);
207 fSurface = EGL_NO_SURFACE; 250 fSurface = EGL_NO_SURFACE;
208 } 251 }
209 252
210 //TODO should we close the display? 253 //TODO should we close the display?
211 fDisplay = EGL_NO_DISPLAY; 254 fDisplay = EGL_NO_DISPLAY;
212 } 255 }
213 } 256 }
214 257
215 void SkANGLEGLContext::onPlatformMakeCurrent() const { 258 void ANGLEGLContext::onPlatformMakeCurrent() const {
216 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { 259 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
217 SkDebugf("Could not set the context.\n"); 260 SkDebugf("Could not set the context.\n");
218 } 261 }
219 } 262 }
220 263
221 void SkANGLEGLContext::onPlatformSwapBuffers() const { 264 void ANGLEGLContext::onPlatformSwapBuffers() const {
222 if (!eglSwapBuffers(fDisplay, fSurface)) { 265 if (!eglSwapBuffers(fDisplay, fSurface)) {
223 SkDebugf("Could not complete eglSwapBuffers.\n"); 266 SkDebugf("Could not complete eglSwapBuffers.\n");
224 } 267 }
225 } 268 }
226 269
227 GrGLFuncPtr SkANGLEGLContext::onPlatformGetProcAddress(const char* name) const { 270 GrGLFuncPtr ANGLEGLContext::onPlatformGetProcAddress(const char* name) const {
228 return eglGetProcAddress(name); 271 return eglGetProcAddress(name);
229 } 272 }
273 } // anonymous namespace
274
275 namespace sk_gpu_test {
276 const GrGLInterface* CreateANGLEGLInterface() {
277 static Libs gLibs = { nullptr, nullptr };
278
279 if (nullptr == gLibs.fGLLib) {
280 // We load the ANGLE library and never let it go
281 #if defined _WIN32
282 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.dll");
283 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.dll");
284 #elif defined SK_BUILD_FOR_MAC
285 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.dylib");
286 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.dylib");
287 #else
288 gLibs.fGLLib = DynamicLoadLibrary("libGLESv2.so");
289 gLibs.fEGLLib = DynamicLoadLibrary("libEGL.so");
290 #endif
291 }
292
293 if (nullptr == gLibs.fGLLib || nullptr == gLibs.fEGLLib) {
294 // We can't setup the interface correctly w/o the so
295 return nullptr;
296 }
297
298 return GrGLAssembleGLESInterface(&gLibs, angle_get_gl_proc);
299 }
300
301 #ifdef SK_BUILD_FOR_WIN
302 GLContext* CreateANGLEDirect3DGLContext() {
303 ANGLEGLContext* ctx = new ANGLEGLContext(false);
304 if (!ctx->isValid()) {
305 delete ctx;
306 return NULL;
307 }
308 return ctx;
309 }
310 #endif
311
312 GLContext* CreateANGLEOpenGLGLContext() {
313 ANGLEGLContext* ctx = new ANGLEGLContext(true);
314 if (!ctx->isValid()) {
315 delete ctx;
316 return NULL;
317 }
318 return ctx;
319 }
320 } // namespace sk_gpu_test
OLDNEW
« no previous file with comments | « tools/gpu/gl/angle/GLContext_angle.h ('k') | tools/gpu/gl/command_buffer/GLContext_command_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698