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

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: Address comments 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 /*
3 * Copyright 2012 Google Inc. 3 * Copyright 2012 Google Inc.
4 * 4 *
5 * 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
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 #include "gl/angle/SkANGLEGLContext.h" 9 #include "GLContext_angle.h"
10 10
11 #include <EGL/egl.h> 11 #include <EGL/egl.h>
12 #include <EGL/eglext.h> 12 #include <EGL/eglext.h>
13 13
14 #include "gl/GrGLDefines.h" 14 #include "gl/GrGLDefines.h"
15 #include "gl/GrGLUtil.h" 15 #include "gl/GrGLUtil.h"
16 16
17 #include "gl/GrGLInterface.h"
18 #include "gl/GrGLAssembleInterface.h"
19 #include "../ports/SkOSLibrary.h"
20
21 #include <EGL/egl.h>
22
17 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202 23 #define EGL_PLATFORM_ANGLE_ANGLE 0x3202
18 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203 24 #define EGL_PLATFORM_ANGLE_TYPE_ANGLE 0x3203
19 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207 25 #define EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE 0x3207
20 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208 26 #define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
21 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D 27 #define EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE 0x320D
22 28
23 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) {
24 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT; 49 PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT;
25 eglGetPlatformDisplayEXT = 50 eglGetPlatformDisplayEXT =
26 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDispla yEXT"); 51 (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDispla yEXT");
27 52
53 // We expect ANGLE to support this extension
28 if (!eglGetPlatformDisplayEXT) { 54 if (!eglGetPlatformDisplayEXT) {
29 return eglGetDisplay(static_cast<EGLNativeDisplayType>(nativeDisplay)); 55 return EGL_NO_DISPLAY;
30 } 56 }
31 57
32 EGLDisplay display = EGL_NO_DISPLAY; 58 EGLDisplay display = EGL_NO_DISPLAY;
33 if (useGLBackend) { 59 if (useGLBackend) {
34 // Try for an ANGLE D3D11 context, fall back to D3D9.
35 EGLint attribs[3] = { 60 EGLint attribs[3] = {
36 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 61 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
37 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE, 62 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE,
38 EGL_NONE 63 EGL_NONE
39 }; 64 };
40 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDispl ay, attribs); 65 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, nativeDispl ay, attribs);
41 } else { 66 } else {
42 // Try for an ANGLE D3D11 context, fall back to D3D9, and finally GL. 67 // Try for an ANGLE D3D11 context, fall back to D3D9.
43 EGLint attribs[3][3] = { 68 EGLint attribs[3][3] = {
44 { 69 {
45 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 70 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
46 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, 71 EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
47 EGL_NONE 72 EGL_NONE
48 }, 73 },
49 { 74 {
50 EGL_PLATFORM_ANGLE_TYPE_ANGLE, 75 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
51 EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE, 76 EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE,
52 EGL_NONE 77 EGL_NONE
53 }, 78 },
54 {
55 EGL_PLATFORM_ANGLE_TYPE_ANGLE,
56 EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE,
57 EGL_NONE
58 }
59 }; 79 };
60 for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) { 80 for (int i = 0; i < 3 && display == EGL_NO_DISPLAY; ++i) {
61 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,nativeDi splay, attribs[i]); 81 display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,nativeDi splay, attribs[i]);
62 } 82 }
63 } 83 }
64 return display; 84 return display;
65 } 85 }
66 86
67 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)
68 : fContext(EGL_NO_CONTEXT) 111 : fContext(EGL_NO_CONTEXT)
69 , fDisplay(EGL_NO_DISPLAY) 112 , fDisplay(EGL_NO_DISPLAY)
70 , fSurface(EGL_NO_SURFACE) { 113 , fSurface(EGL_NO_SURFACE) {
71 114
72 EGLint numConfigs; 115 EGLint numConfigs;
73 static const EGLint configAttribs[] = { 116 static const EGLint configAttribs[] = {
74 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 117 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
75 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 118 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
76 EGL_RED_SIZE, 8, 119 EGL_RED_SIZE, 8,
77 EGL_GREEN_SIZE, 8, 120 EGL_GREEN_SIZE, 8,
78 EGL_BLUE_SIZE, 8, 121 EGL_BLUE_SIZE, 8,
79 EGL_ALPHA_SIZE, 8, 122 EGL_ALPHA_SIZE, 8,
80 EGL_NONE 123 EGL_NONE
81 }; 124 };
82 125
83 fIsGLBackend = useGLBackend; 126 fIsGLBackend = useGLBackend;
84 fDisplay = GetD3DEGLDisplay(EGL_DEFAULT_DISPLAY, useGLBackend); 127 fDisplay = get_angle_egl_display(EGL_DEFAULT_DISPLAY, useGLBackend);
85 if (EGL_NO_DISPLAY == fDisplay) { 128 if (EGL_NO_DISPLAY == fDisplay) {
86 SkDebugf("Could not create EGL display!"); 129 SkDebugf("Could not create EGL display!");
87 return; 130 return;
88 } 131 }
89 132
90 EGLint majorVersion; 133 EGLint majorVersion;
91 EGLint minorVersion; 134 EGLint minorVersion;
92 eglInitialize(fDisplay, &majorVersion, &minorVersion); 135 eglInitialize(fDisplay, &majorVersion, &minorVersion);
93 136
94 EGLConfig surfaceConfig; 137 EGLConfig surfaceConfig;
95 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); 138 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
96 139
97 static const EGLint contextAttribs[] = { 140 static const EGLint contextAttribs[] = {
98 EGL_CONTEXT_CLIENT_VERSION, 2, 141 EGL_CONTEXT_CLIENT_VERSION, 2,
99 EGL_NONE 142 EGL_NONE
100 }; 143 };
101 fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs ); 144 fContext = eglCreateContext(fDisplay, surfaceConfig, nullptr, contextAttribs );
102 145
103 146
104 static const EGLint surfaceAttribs[] = { 147 static const EGLint surfaceAttribs[] = {
105 EGL_WIDTH, 1, 148 EGL_WIDTH, 1,
106 EGL_HEIGHT, 1, 149 EGL_HEIGHT, 1,
107 EGL_NONE 150 EGL_NONE
108 }; 151 };
109 152
110 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); 153 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
111 154
112 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); 155 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
113 156
114 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateANGLEInterface()); 157 SkAutoTUnref<const GrGLInterface> gl(sk_gpu_test::CreateANGLEGLInterface());
115 if (nullptr == gl.get()) { 158 if (nullptr == gl.get()) {
116 SkDebugf("Could not create ANGLE GL interface!\n"); 159 SkDebugf("Could not create ANGLE GL interface!\n");
117 this->destroyGLContext(); 160 this->destroyGLContext();
118 return; 161 return;
119 } 162 }
120 if (!gl->validate()) { 163 if (!gl->validate()) {
121 SkDebugf("Could not validate ANGLE GL interface!\n"); 164 SkDebugf("Could not validate ANGLE GL interface!\n");
122 this->destroyGLContext(); 165 this->destroyGLContext();
123 return; 166 return;
124 } 167 }
125 168
126 this->init(gl.release()); 169 this->init(gl.release());
127 } 170 }
128 171
129 SkANGLEGLContext::~SkANGLEGLContext() { 172 ANGLEGLContext::~ANGLEGLContext() {
130 this->teardown(); 173 this->teardown();
131 this->destroyGLContext(); 174 this->destroyGLContext();
132 } 175 }
133 176
134 GrEGLImage SkANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const { 177 GrEGLImage ANGLEGLContext::texture2DToEGLImage(GrGLuint texID) const {
135 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) { 178 if (!this->gl()->hasExtension("EGL_KHR_gl_texture_2D_image")) {
136 return GR_EGL_NO_IMAGE; 179 return GR_EGL_NO_IMAGE;
137 } 180 }
138 GrEGLImage img; 181 GrEGLImage img;
139 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0, 182 GrEGLint attribs[] = { GR_EGL_GL_TEXTURE_LEVEL, 0,
140 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE, 183 GR_EGL_IMAGE_PRESERVED, GR_EGL_TRUE,
141 GR_EGL_NONE }; 184 GR_EGL_NONE };
142 // 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.
143 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64 _t)texID); 186 GrEGLClientBuffer clientBuffer = reinterpret_cast<GrEGLClientBuffer>((uint64 _t)texID);
144 GR_GL_CALL_RET(this->gl(), img, 187 GR_GL_CALL_RET(this->gl(), img,
145 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie ntBuffer, 188 EGLCreateImage(fDisplay, fContext, GR_EGL_GL_TEXTURE_2D, clie ntBuffer,
146 attribs)); 189 attribs));
147 return img; 190 return img;
148 } 191 }
149 192
150 void SkANGLEGLContext::destroyEGLImage(GrEGLImage image) const { 193 void ANGLEGLContext::destroyEGLImage(GrEGLImage image) const {
151 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image)); 194 GR_GL_CALL(this->gl(), EGLDestroyImage(fDisplay, image));
152 } 195 }
153 196
154 GrGLuint SkANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const { 197 GrGLuint ANGLEGLContext::eglImageToExternalTexture(GrEGLImage image) const {
155 GrGLClearErr(this->gl()); 198 GrGLClearErr(this->gl());
156 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) { 199 if (!this->gl()->hasExtension("GL_OES_EGL_image_external")) {
157 return 0; 200 return 0;
158 } 201 }
159 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage); 202 typedef GrGLvoid (*EGLImageTargetTexture2DProc)(GrGLenum, GrGLeglImage);
160
161 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D = 203 EGLImageTargetTexture2DProc glEGLImageTargetTexture2D =
162 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2 DOES"); 204 (EGLImageTargetTexture2DProc)eglGetProcAddress("glEGLImageTargetTexture2 DOES");
163 if (!glEGLImageTargetTexture2D) { 205 if (!glEGLImageTargetTexture2D) {
164 return 0; 206 return 0;
165 } 207 }
166 GrGLuint texID; 208 GrGLuint texID;
167 GR_GL_CALL(this->gl(), GenTextures(1, &texID)); 209 GR_GL_CALL(this->gl(), GenTextures(1, &texID));
168 if (!texID) { 210 if (!texID) {
169 return 0; 211 return 0;
170 } 212 }
171 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID)); 213 GR_GL_CALL(this->gl(), BindTexture(GR_GL_TEXTURE_EXTERNAL, texID));
172 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { 214 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
173 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); 215 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
174 return 0; 216 return 0;
175 } 217 }
176 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image); 218 glEGLImageTargetTexture2D(GR_GL_TEXTURE_EXTERNAL, image);
177 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) { 219 if (GR_GL_GET_ERROR(this->gl()) != GR_GL_NO_ERROR) {
178 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID)); 220 GR_GL_CALL(this->gl(), DeleteTextures(1, &texID));
179 return 0; 221 return 0;
180 } 222 }
181 return texID; 223 return texID;
182 } 224 }
183 225
184 SkGLContext* SkANGLEGLContext::createNew() const { 226 sk_gpu_test::GLContext* ANGLEGLContext::createNew() const {
185 #ifdef SK_BUILD_FOR_WIN 227 #ifdef SK_BUILD_FOR_WIN
186 SkGLContext* ctx = fIsGLBackend ? SkANGLEGLContext::CreateOpenGL() 228 sk_gpu_test::GLContext* ctx = fIsGLBackend ? sk_gpu_test::CreateANGLEOpenGLG LContext()
187 : SkANGLEGLContext::CreateDirectX(); 229 : sk_gpu_test::CreateANGLEDirect3 DGLContext();
188 #else 230 #else
189 SkGLContext* ctx = SkANGLEGLContext::CreateOpenGL(); 231 sk_gpu_test::GLContext* ctx = sk_gpu_test::CreateANGLEOpenGLGLContext();
190 #endif 232 #endif
191 if (ctx) { 233 if (ctx) {
192 ctx->makeCurrent(); 234 ctx->makeCurrent();
193 } 235 }
194 return ctx; 236 return ctx;
195 } 237 }
196 238
197 void SkANGLEGLContext::destroyGLContext() { 239 void ANGLEGLContext::destroyGLContext() {
198 if (fDisplay) { 240 if (fDisplay) {
199 eglMakeCurrent(fDisplay, 0, 0, 0); 241 eglMakeCurrent(fDisplay, 0, 0, 0);
200 242
201 if (fContext) { 243 if (fContext) {
202 eglDestroyContext(fDisplay, fContext); 244 eglDestroyContext(fDisplay, fContext);
203 fContext = EGL_NO_CONTEXT; 245 fContext = EGL_NO_CONTEXT;
204 } 246 }
205 247
206 if (fSurface) { 248 if (fSurface) {
207 eglDestroySurface(fDisplay, fSurface); 249 eglDestroySurface(fDisplay, fSurface);
208 fSurface = EGL_NO_SURFACE; 250 fSurface = EGL_NO_SURFACE;
209 } 251 }
210 252
211 //TODO should we close the display? 253 //TODO should we close the display?
212 fDisplay = EGL_NO_DISPLAY; 254 fDisplay = EGL_NO_DISPLAY;
213 } 255 }
214 } 256 }
215 257
216 void SkANGLEGLContext::onPlatformMakeCurrent() const { 258 void ANGLEGLContext::onPlatformMakeCurrent() const {
217 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { 259 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
218 SkDebugf("Could not set the context.\n"); 260 SkDebugf("Could not set the context.\n");
219 } 261 }
220 } 262 }
221 263
222 void SkANGLEGLContext::onPlatformSwapBuffers() const { 264 void ANGLEGLContext::onPlatformSwapBuffers() const {
223 if (!eglSwapBuffers(fDisplay, fSurface)) { 265 if (!eglSwapBuffers(fDisplay, fSurface)) {
224 SkDebugf("Could not complete eglSwapBuffers.\n"); 266 SkDebugf("Could not complete eglSwapBuffers.\n");
225 } 267 }
226 } 268 }
227 269
228 GrGLFuncPtr SkANGLEGLContext::onPlatformGetProcAddress(const char* name) const { 270 GrGLFuncPtr ANGLEGLContext::onPlatformGetProcAddress(const char* name) const {
229 return eglGetProcAddress(name); 271 return eglGetProcAddress(name);
230 } 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

Powered by Google App Engine
This is Rietveld 408576698