1
2 /*
3 * Copyright 2015 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 #ifndef SK_NO_COMMAND_BUFFER
10
11 #include "include/gpu/gl/GrGLAssembleInterface.h"
12 #include "include/gpu/gl/GrGLInterface.h"
13 #include "include/private/SkMutex.h"
14 #include "include/private/SkOnce.h"
15 #include "src/ports/SkOSLibrary.h"
16 #include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
17
18 namespace {
19
20 typedef void *EGLDisplay;
21 typedef unsigned int EGLBoolean;
22 typedef void *EGLConfig;
23 typedef void *EGLSurface;
24 typedef void *EGLContext;
25 typedef int32_t EGLint;
26 typedef void* EGLNativeDisplayType;
27 typedef void* EGLNativeWindowType;
28 typedef void (*__eglMustCastToProperFunctionPointerType)(void);
29 #define EGL_FALSE 0
30 #define EGL_TRUE 1
31 #define EGL_OPENGL_ES2_BIT 0x0004
32 #define EGL_OPENGL_ES3_BIT 0x0040
33 #define EGL_CONTEXT_CLIENT_VERSION 0x3098
34 #define EGL_NO_SURFACE ((EGLSurface)0)
35 #define EGL_NO_DISPLAY ((EGLDisplay)0)
36 #define EGL_NO_CONTEXT ((EGLContext)0)
37 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0)
38 #define EGL_SURFACE_TYPE 0x3033
39 #define EGL_PBUFFER_BIT 0x0001
40 #define EGL_RENDERABLE_TYPE 0x3040
41 #define EGL_RED_SIZE 0x3024
42 #define EGL_GREEN_SIZE 0x3023
43 #define EGL_BLUE_SIZE 0x3022
44 #define EGL_ALPHA_SIZE 0x3021
45 #define EGL_DEPTH_SIZE 0x3025
46 #define EGL_STENCIL_SIZE 0x3025
47 #define EGL_SAMPLES 0x3031
48 #define EGL_SAMPLE_BUFFERS 0x3032
49 #define EGL_NONE 0x3038
50 #define EGL_WIDTH 0x3057
51 #define EGL_HEIGHT 0x3056
52 #define EGL_DRAW 0x3059
53 #define EGL_READ 0x305A
54
55 typedef EGLDisplay (*GetDisplayProc)(EGLNativeDisplayType display_id);
56 typedef EGLBoolean (*InitializeProc)(EGLDisplay dpy, EGLint *major, EGLint *minor);
57 typedef EGLBoolean (*TerminateProc)(EGLDisplay dpy);
58 typedef EGLBoolean (*ChooseConfigProc)(EGLDisplay dpy, const EGLint* attrib_list, EGLConfig* configs, EGLint config_size, EGLint* num_config);
59 typedef EGLBoolean (*GetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint* value);
60 typedef EGLSurface (*CreateWindowSurfaceProc)(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint* attrib_list);
61 typedef EGLSurface (*CreatePbufferSurfaceProc)(EGLDisplay dpy, EGLConfig config, const EGLint* attrib_list);
62 typedef EGLBoolean (*DestroySurfaceProc)(EGLDisplay dpy, EGLSurface surface);
63 typedef EGLContext (*CreateContextProc)(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint* attrib_list);
64 typedef EGLBoolean (*DestroyContextProc)(EGLDisplay dpy, EGLContext ctx);
65 typedef EGLBoolean (*MakeCurrentProc)(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx);
66 typedef EGLBoolean (*SwapBuffersProc)(EGLDisplay dpy, EGLSurface surface);
67 typedef __eglMustCastToProperFunctionPointerType (*GetProcAddressProc)(const char* procname);
68
69 static GetDisplayProc gfGetDisplay = nullptr;
70 static InitializeProc gfInitialize = nullptr;
71 static TerminateProc gfTerminate = nullptr;
72 static ChooseConfigProc gfChooseConfig = nullptr;
73 static GetConfigAttrib gfGetConfigAttrib = nullptr;
74 static CreateWindowSurfaceProc gfCreateWindowSurface = nullptr;
75 static CreatePbufferSurfaceProc gfCreatePbufferSurface = nullptr;
76 static DestroySurfaceProc gfDestroySurface = nullptr;
77 static CreateContextProc gfCreateContext = nullptr;
78 static DestroyContextProc gfDestroyContext = nullptr;
79 static MakeCurrentProc gfMakeCurrent = nullptr;
80 static SwapBuffersProc gfSwapBuffers = nullptr;
81 static GetProcAddressProc gfGetProcAddress = nullptr;
82
83 static void* gLibrary = nullptr;
84 static bool gfFunctionsLoadedSuccessfully = false;
85
load_command_buffer_functions()86 static void load_command_buffer_functions() {
87 if (!gLibrary) {
88 static constexpr const char* libName =
89 #if defined _WIN32
90 "command_buffer_gles2.dll";
91 #elif defined SK_BUILD_FOR_MAC
92 "libcommand_buffer_gles2.dylib";
93 #else
94 "libcommand_buffer_gles2.so";
95 #endif // defined _WIN32
96 gLibrary = SkLoadDynamicLibrary(libName);
97 if (gLibrary) {
98 gfGetDisplay = (GetDisplayProc)SkGetProcedureAddress(gLibrary, "eglGetDisplay");
99 gfInitialize = (InitializeProc)SkGetProcedureAddress(gLibrary, "eglInitialize");
100 gfTerminate = (TerminateProc)SkGetProcedureAddress(gLibrary, "eglTerminate");
101 gfChooseConfig = (ChooseConfigProc)SkGetProcedureAddress(gLibrary, "eglChooseConfig");
102 gfGetConfigAttrib = (GetConfigAttrib)SkGetProcedureAddress(gLibrary, "eglGetConfigAttrib");
103 gfCreateWindowSurface = (CreateWindowSurfaceProc)SkGetProcedureAddress(gLibrary, "eglCreateWindowSurface");
104 gfCreatePbufferSurface = (CreatePbufferSurfaceProc)SkGetProcedureAddress(gLibrary, "eglCreatePbufferSurface");
105 gfDestroySurface = (DestroySurfaceProc)SkGetProcedureAddress(gLibrary, "eglDestroySurface");
106 gfCreateContext = (CreateContextProc)SkGetProcedureAddress(gLibrary, "eglCreateContext");
107 gfDestroyContext = (DestroyContextProc)SkGetProcedureAddress(gLibrary, "eglDestroyContext");
108 gfMakeCurrent = (MakeCurrentProc)SkGetProcedureAddress(gLibrary, "eglMakeCurrent");
109 gfSwapBuffers = (SwapBuffersProc)SkGetProcedureAddress(gLibrary, "eglSwapBuffers");
110 gfGetProcAddress = (GetProcAddressProc)SkGetProcedureAddress(gLibrary, "eglGetProcAddress");
111
112 gfFunctionsLoadedSuccessfully =
113 gfGetDisplay && gfInitialize && gfTerminate && gfChooseConfig &&
114 gfCreateWindowSurface && gfCreatePbufferSurface && gfDestroySurface &&
115 gfCreateContext && gfDestroyContext && gfMakeCurrent && gfSwapBuffers &&
116 gfGetProcAddress;
117 }
118 }
119 }
120
command_buffer_get_gl_proc(void * ctx,const char name[])121 static GrGLFuncPtr command_buffer_get_gl_proc(void* ctx, const char name[]) {
122 if (!gfFunctionsLoadedSuccessfully) {
123 return nullptr;
124 }
125 return gfGetProcAddress(name);
126 }
127
load_command_buffer_once()128 static void load_command_buffer_once() {
129 static SkOnce once;
130 once(load_command_buffer_functions);
131 }
132
create_command_buffer_interface()133 static sk_sp<const GrGLInterface> create_command_buffer_interface() {
134 load_command_buffer_once();
135 if (!gfFunctionsLoadedSuccessfully) {
136 return nullptr;
137 }
138 return GrGLMakeAssembledGLESInterface(gLibrary, command_buffer_get_gl_proc);
139 }
140
141
142 // The command buffer does not correctly implement eglGetCurrent. It always returns EGL_NO_<foo>.
143 // So we implement them ourselves and hook eglMakeCurrent to store the current values in TLS.
144 class TLSCurrentObjects {
145 public:
CurrentDisplay()146 static EGLDisplay CurrentDisplay() {
147 if (auto objects = Get()) {
148 return objects->fDisplay;
149 }
150 return EGL_NO_DISPLAY;
151 }
152
CurrentSurface(EGLint readdraw)153 static EGLSurface CurrentSurface(EGLint readdraw) {
154 if (auto objects = Get()) {
155 switch (readdraw) {
156 case EGL_DRAW:
157 return objects->fDrawSurface;
158 case EGL_READ:
159 return objects->fReadSurface;
160 default:
161 return EGL_NO_SURFACE;
162 }
163 }
164 return EGL_NO_SURFACE;
165 }
166
CurrentContext()167 static EGLContext CurrentContext() {
168 if (auto objects = Get()) {
169 return objects->fContext;
170 }
171 return EGL_NO_CONTEXT;
172 }
173
MakeCurrent(EGLDisplay display,EGLSurface draw,EGLSurface read,EGLContext ctx)174 static EGLBoolean MakeCurrent(EGLDisplay display, EGLSurface draw, EGLSurface read,
175 EGLContext ctx) {
176 if (gfFunctionsLoadedSuccessfully && EGL_TRUE == gfMakeCurrent(display, draw, read, ctx)) {
177 if (auto objects = Get()) {
178 objects->fDisplay = display;
179 objects->fDrawSurface = draw;
180 objects->fReadSurface = read;
181 objects->fContext = ctx;
182 }
183 return EGL_TRUE;
184 }
185 return EGL_FALSE;
186
187 }
188
189 private:
190 EGLDisplay fDisplay = EGL_NO_DISPLAY;
191 EGLSurface fReadSurface = EGL_NO_SURFACE;
192 EGLSurface fDrawSurface = EGL_NO_SURFACE;
193 EGLContext fContext = EGL_NO_CONTEXT;
194
Get()195 static TLSCurrentObjects* Get() {
196 static thread_local TLSCurrentObjects objects;
197 return &objects;
198 }
199 };
200
context_restorer()201 std::function<void()> context_restorer() {
202 if (!gfFunctionsLoadedSuccessfully) {
203 return nullptr;
204 }
205 auto display = TLSCurrentObjects::CurrentDisplay();
206 auto dsurface = TLSCurrentObjects::CurrentSurface(EGL_DRAW);
207 auto rsurface = TLSCurrentObjects::CurrentSurface(EGL_READ);
208 auto context = TLSCurrentObjects::CurrentContext();
209 return [display, dsurface, rsurface, context] {
210 TLSCurrentObjects::MakeCurrent(display, dsurface, rsurface, context);
211 };
212 }
213
214 } // anonymous namespace
215
216 namespace sk_gpu_test {
217
CommandBufferGLTestContext(int version,CommandBufferGLTestContext * shareContext)218 CommandBufferGLTestContext::CommandBufferGLTestContext(int version,
219 CommandBufferGLTestContext* shareContext)
220 : fContext(EGL_NO_CONTEXT), fDisplay(EGL_NO_DISPLAY), fSurface(EGL_NO_SURFACE) {
221
222 EGLint renderableType;
223 switch (version) {
224 case 2:
225 renderableType = EGL_OPENGL_ES2_BIT;
226 break;
227 case 3:
228 renderableType = EGL_OPENGL_ES3_BIT;
229 break;
230 default:
231 SkDebugf("Command Buffer: Invalid version requested (%i). Must be either 2 or 3.\n",
232 version);
233 return;
234 }
235
236 EGLint configAttribs[] = {
237 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
238 EGL_RENDERABLE_TYPE, renderableType,
239 EGL_RED_SIZE, 8,
240 EGL_GREEN_SIZE, 8,
241 EGL_BLUE_SIZE, 8,
242 EGL_ALPHA_SIZE, 8,
243 EGL_NONE
244 };
245
246 static const EGLint surfaceAttribs[] = {
247 EGL_WIDTH, 1,
248 EGL_HEIGHT, 1,
249 EGL_NONE
250 };
251
252 load_command_buffer_once();
253 if (!gfFunctionsLoadedSuccessfully) {
254 return;
255 }
256
257 fDisplay = gfGetDisplay(EGL_DEFAULT_DISPLAY);
258 if (EGL_NO_DISPLAY == fDisplay) {
259 SkDebugf("Command Buffer: Could not create EGL display.\n");
260 return;
261 }
262 if (!gfInitialize(fDisplay, nullptr, nullptr)) {
263 SkDebugf("Command Buffer: Could not initialize EGL display.\n");
264 this->destroyGLContext();
265 return;
266 }
267 EGLint numConfigs;
268 if (!gfChooseConfig(fDisplay, configAttribs, static_cast<EGLConfig *>(&fConfig), 1,
269 &numConfigs) || numConfigs != 1) {
270 SkDebugf("Command Buffer: Could not choose EGL config.\n");
271 this->destroyGLContext();
272 return;
273 }
274
275 fSurface = gfCreatePbufferSurface(fDisplay,
276 static_cast<EGLConfig>(fConfig),
277 surfaceAttribs);
278
279 if (EGL_NO_SURFACE == fSurface) {
280 SkDebugf("Command Buffer: Could not create EGL surface.\n");
281 this->destroyGLContext();
282 return;
283 }
284
285 EGLint contextAttribs[] = {
286 EGL_CONTEXT_CLIENT_VERSION, version,
287 EGL_NONE
288 };
289 EGLContext eglShareContext = shareContext
290 ? reinterpret_cast<EGLContext>(shareContext->fContext) : nullptr;
291 fContext = gfCreateContext(fDisplay, static_cast<EGLConfig>(fConfig), eglShareContext,
292 contextAttribs);
293 if (EGL_NO_CONTEXT == fContext) {
294 SkDebugf("Command Buffer: Could not create EGL context.\n");
295 this->destroyGLContext();
296 return;
297 }
298
299 SkScopeExit restorer(context_restorer());
300 if (!TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
301 SkDebugf("Command Buffer: Could not make EGL context current.\n");
302 this->destroyGLContext();
303 return;
304 }
305
306 auto gl = create_command_buffer_interface();
307 if (!gl) {
308 SkDebugf("Command Buffer: Could not create CommandBuffer GL interface.\n");
309 this->destroyGLContext();
310 return;
311 }
312 if (!gl->validate()) {
313 SkDebugf("Command Buffer: Could not validate CommandBuffer GL interface.\n");
314 this->destroyGLContext();
315 return;
316 }
317
318 // libcommand_buffer_gles2 has not always respected the EGL_CONTEXT_CLIENT_VERSION attrib.
319 // Double check that we are not using an old library and we actually got the context version we
320 // asked for.
321 const char* versionString = (const char*)gl->fFunctions.fGetString(GR_GL_VERSION);
322 const char* expectedVersion = (version == 2) ? "OpenGL ES 2.0" : "OpenGL ES 3.0";
323 if (strstr(versionString, expectedVersion) != versionString) {
324 SkDebugf("Command Buffer: Unexpected version.\n"
325 " Got: \"%s\".\n"
326 " Expected: \"%s ...\".\n",
327 versionString, expectedVersion);
328 return;
329 }
330
331 this->init(std::move(gl));
332 }
333
~CommandBufferGLTestContext()334 CommandBufferGLTestContext::~CommandBufferGLTestContext() {
335 this->teardown();
336 this->destroyGLContext();
337 }
338
destroyGLContext()339 void CommandBufferGLTestContext::destroyGLContext() {
340 if (!gfFunctionsLoadedSuccessfully) {
341 return;
342 }
343 if (EGL_NO_DISPLAY == fDisplay) {
344 return;
345 }
346 bool wasCurrent = false;
347 if (EGL_NO_CONTEXT != fContext) {
348 wasCurrent = (TLSCurrentObjects::CurrentContext() == fContext);
349 gfDestroyContext(fDisplay, fContext);
350 fContext = EGL_NO_CONTEXT;
351 }
352 if (wasCurrent) {
353 // Call MakeCurrent after destroying the context, so that the EGL implementation knows that
354 // the context is not used anymore after it is released from being current.This way the
355 // command buffer does not need to abandon the context before destruction, and no
356 // client-side errors are printed.
357 TLSCurrentObjects::MakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
358 }
359
360 if (EGL_NO_SURFACE != fSurface) {
361 gfDestroySurface(fDisplay, fSurface);
362 fSurface = EGL_NO_SURFACE;
363 }
364 fDisplay = EGL_NO_DISPLAY;
365 }
366
onPlatformMakeNotCurrent() const367 void CommandBufferGLTestContext::onPlatformMakeNotCurrent() const {
368 if (!gfFunctionsLoadedSuccessfully) {
369 return;
370 }
371 if (!TLSCurrentObjects::MakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
372 SkDebugf("Command Buffer: Could not null out current EGL context.\n");
373 }
374 }
375
onPlatformMakeCurrent() const376 void CommandBufferGLTestContext::onPlatformMakeCurrent() const {
377 if (!gfFunctionsLoadedSuccessfully) {
378 return;
379 }
380 if (!TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
381 SkDebugf("Command Buffer: Could not make EGL context current.\n");
382 }
383 }
384
onPlatformGetAutoContextRestore() const385 std::function<void()> CommandBufferGLTestContext::onPlatformGetAutoContextRestore() const {
386 if (!gfFunctionsLoadedSuccessfully || TLSCurrentObjects::CurrentContext() == fContext) {
387 return nullptr;
388 }
389 return context_restorer();
390 }
391
onPlatformGetProcAddress(const char * name) const392 GrGLFuncPtr CommandBufferGLTestContext::onPlatformGetProcAddress(const char *name) const {
393 if (!gfFunctionsLoadedSuccessfully) {
394 return nullptr;
395 }
396 return gfGetProcAddress(name);
397 }
398
presentCommandBuffer()399 void CommandBufferGLTestContext::presentCommandBuffer() {
400 if (this->gl()) {
401 this->gl()->fFunctions.fFlush();
402 }
403
404 if (!gfFunctionsLoadedSuccessfully) {
405 return;
406 }
407 if (!gfSwapBuffers(fDisplay, fSurface)) {
408 SkDebugf("Command Buffer: Could not complete gfSwapBuffers.\n");
409 }
410 }
411
makeCurrent()412 bool CommandBufferGLTestContext::makeCurrent() {
413 return TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext) != EGL_FALSE;
414 }
415
getStencilBits()416 int CommandBufferGLTestContext::getStencilBits() {
417 EGLint result = 0;
418 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_STENCIL_SIZE, &result);
419 return result;
420 }
421
getSampleCount()422 int CommandBufferGLTestContext::getSampleCount() {
423 EGLint result = 0;
424 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_SAMPLES, &result);
425 return result;
426 }
427
428 } // namespace sk_gpu_test
429 #endif // SK_NO_COMMAND_BUFFER
430