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/core/SkTLS.h"
16 #include "src/ports/SkOSLibrary.h"
17 #include "tools/gpu/gl/command_buffer/GLTestContext_command_buffer.h"
18
19 namespace {
20
21 typedef void *EGLDisplay;
22 typedef unsigned int EGLBoolean;
23 typedef void *EGLConfig;
24 typedef void *EGLSurface;
25 typedef void *EGLContext;
26 typedef int32_t EGLint;
27 typedef void* EGLNativeDisplayType;
28 typedef void* EGLNativeWindowType;
29 typedef void (*__eglMustCastToProperFunctionPointerType)(void);
30 #define EGL_FALSE 0
31 #define EGL_TRUE 1
32 #define EGL_OPENGL_ES2_BIT 0x0004
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 = DynamicLoadLibrary(libName);
97 if (gLibrary) {
98 gfGetDisplay = (GetDisplayProc)GetProcedureAddress(gLibrary, "eglGetDisplay");
99 gfInitialize = (InitializeProc)GetProcedureAddress(gLibrary, "eglInitialize");
100 gfTerminate = (TerminateProc)GetProcedureAddress(gLibrary, "eglTerminate");
101 gfChooseConfig = (ChooseConfigProc)GetProcedureAddress(gLibrary, "eglChooseConfig");
102 gfGetConfigAttrib = (GetConfigAttrib)GetProcedureAddress(gLibrary, "eglGetConfigAttrib");
103 gfCreateWindowSurface = (CreateWindowSurfaceProc)GetProcedureAddress(gLibrary, "eglCreateWindowSurface");
104 gfCreatePbufferSurface = (CreatePbufferSurfaceProc)GetProcedureAddress(gLibrary, "eglCreatePbufferSurface");
105 gfDestroySurface = (DestroySurfaceProc)GetProcedureAddress(gLibrary, "eglDestroySurface");
106 gfCreateContext = (CreateContextProc)GetProcedureAddress(gLibrary, "eglCreateContext");
107 gfDestroyContext = (DestroyContextProc)GetProcedureAddress(gLibrary, "eglDestroyContext");
108 gfMakeCurrent = (MakeCurrentProc)GetProcedureAddress(gLibrary, "eglMakeCurrent");
109 gfSwapBuffers = (SwapBuffersProc)GetProcedureAddress(gLibrary, "eglSwapBuffers");
110 gfGetProcAddress = (GetProcAddressProc)GetProcedureAddress(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 return (TLSCurrentObjects*) SkTLS::Get(TLSCreate, TLSDelete);
197 }
TLSCreate()198 static void* TLSCreate() { return new TLSCurrentObjects(); }
TLSDelete(void * objs)199 static void TLSDelete(void* objs) { delete (TLSCurrentObjects*)objs; }
200 };
201
context_restorer()202 std::function<void()> context_restorer() {
203 if (!gfFunctionsLoadedSuccessfully) {
204 return nullptr;
205 }
206 auto display = TLSCurrentObjects::CurrentDisplay();
207 auto dsurface = TLSCurrentObjects::CurrentSurface(EGL_DRAW);
208 auto rsurface = TLSCurrentObjects::CurrentSurface(EGL_READ);
209 auto context = TLSCurrentObjects::CurrentContext();
210 return [display, dsurface, rsurface, context] {
211 TLSCurrentObjects::MakeCurrent(display, dsurface, rsurface, context);
212 };
213 }
214
215 } // anonymous namespace
216
217 namespace sk_gpu_test {
218
CommandBufferGLTestContext(CommandBufferGLTestContext * shareContext)219 CommandBufferGLTestContext::CommandBufferGLTestContext(CommandBufferGLTestContext* shareContext)
220 : fContext(EGL_NO_CONTEXT), fDisplay(EGL_NO_DISPLAY), fSurface(EGL_NO_SURFACE) {
221
222 static const EGLint configAttribs[] = {
223 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
224 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
225 EGL_RED_SIZE, 8,
226 EGL_GREEN_SIZE, 8,
227 EGL_BLUE_SIZE, 8,
228 EGL_ALPHA_SIZE, 8,
229 EGL_NONE
230 };
231
232 static const EGLint surfaceAttribs[] = {
233 EGL_WIDTH, 1,
234 EGL_HEIGHT, 1,
235 EGL_NONE
236 };
237
238 load_command_buffer_once();
239 if (!gfFunctionsLoadedSuccessfully) {
240 return;
241 }
242
243 fDisplay = gfGetDisplay(EGL_DEFAULT_DISPLAY);
244 if (EGL_NO_DISPLAY == fDisplay) {
245 SkDebugf("Command Buffer: Could not create EGL display.\n");
246 return;
247 }
248 if (!gfInitialize(fDisplay, nullptr, nullptr)) {
249 SkDebugf("Command Buffer: Could not initialize EGL display.\n");
250 this->destroyGLContext();
251 return;
252 }
253 EGLint numConfigs;
254 if (!gfChooseConfig(fDisplay, configAttribs, static_cast<EGLConfig *>(&fConfig), 1,
255 &numConfigs) || numConfigs != 1) {
256 SkDebugf("Command Buffer: Could not choose EGL config.\n");
257 this->destroyGLContext();
258 return;
259 }
260
261 fSurface = gfCreatePbufferSurface(fDisplay,
262 static_cast<EGLConfig>(fConfig),
263 surfaceAttribs);
264
265 if (EGL_NO_SURFACE == fSurface) {
266 SkDebugf("Command Buffer: Could not create EGL surface.\n");
267 this->destroyGLContext();
268 return;
269 }
270
271 static const EGLint contextAttribs[] = {
272 EGL_CONTEXT_CLIENT_VERSION, 2,
273 EGL_NONE
274 };
275 EGLContext eglShareContext = shareContext
276 ? reinterpret_cast<EGLContext>(shareContext->fContext) : nullptr;
277 fContext = gfCreateContext(fDisplay, static_cast<EGLConfig>(fConfig), eglShareContext,
278 contextAttribs);
279 if (EGL_NO_CONTEXT == fContext) {
280 SkDebugf("Command Buffer: Could not create EGL context.\n");
281 this->destroyGLContext();
282 return;
283 }
284
285 SkScopeExit restorer(context_restorer());
286 if (!TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
287 SkDebugf("Command Buffer: Could not make EGL context current.\n");
288 this->destroyGLContext();
289 return;
290 }
291
292 auto gl = create_command_buffer_interface();
293 if (!gl) {
294 SkDebugf("Command Buffer: Could not create CommandBuffer GL interface.\n");
295 this->destroyGLContext();
296 return;
297 }
298 if (!gl->validate()) {
299 SkDebugf("Command Buffer: Could not validate CommandBuffer GL interface.\n");
300 this->destroyGLContext();
301 return;
302 }
303
304 this->init(std::move(gl));
305 }
306
~CommandBufferGLTestContext()307 CommandBufferGLTestContext::~CommandBufferGLTestContext() {
308 this->teardown();
309 this->destroyGLContext();
310 }
311
destroyGLContext()312 void CommandBufferGLTestContext::destroyGLContext() {
313 if (!gfFunctionsLoadedSuccessfully) {
314 return;
315 }
316 if (EGL_NO_DISPLAY == fDisplay) {
317 return;
318 }
319 bool wasCurrent = false;
320 if (EGL_NO_CONTEXT != fContext) {
321 wasCurrent = (TLSCurrentObjects::CurrentContext() == fContext);
322 gfDestroyContext(fDisplay, fContext);
323 fContext = EGL_NO_CONTEXT;
324 }
325 if (wasCurrent) {
326 // Call MakeCurrent after destroying the context, so that the EGL implementation knows that
327 // the context is not used anymore after it is released from being current.This way the
328 // command buffer does not need to abandon the context before destruction, and no
329 // client-side errors are printed.
330 TLSCurrentObjects::MakeCurrent(fDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
331 }
332
333 if (EGL_NO_SURFACE != fSurface) {
334 gfDestroySurface(fDisplay, fSurface);
335 fSurface = EGL_NO_SURFACE;
336 }
337 fDisplay = EGL_NO_DISPLAY;
338 }
339
onPlatformMakeCurrent() const340 void CommandBufferGLTestContext::onPlatformMakeCurrent() const {
341 if (!gfFunctionsLoadedSuccessfully) {
342 return;
343 }
344 if (!TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
345 SkDebugf("Command Buffer: Could not make EGL context current.\n");
346 }
347 }
348
onPlatformGetAutoContextRestore() const349 std::function<void()> CommandBufferGLTestContext::onPlatformGetAutoContextRestore() const {
350 if (!gfFunctionsLoadedSuccessfully || TLSCurrentObjects::CurrentContext() == fContext) {
351 return nullptr;
352 }
353 return context_restorer();
354 }
355
onPlatformSwapBuffers() const356 void CommandBufferGLTestContext::onPlatformSwapBuffers() const {
357 if (!gfFunctionsLoadedSuccessfully) {
358 return;
359 }
360 if (!gfSwapBuffers(fDisplay, fSurface)) {
361 SkDebugf("Command Buffer: Could not complete gfSwapBuffers.\n");
362 }
363 }
364
onPlatformGetProcAddress(const char * name) const365 GrGLFuncPtr CommandBufferGLTestContext::onPlatformGetProcAddress(const char *name) const {
366 if (!gfFunctionsLoadedSuccessfully) {
367 return nullptr;
368 }
369 return gfGetProcAddress(name);
370 }
371
presentCommandBuffer()372 void CommandBufferGLTestContext::presentCommandBuffer() {
373 if (this->gl()) {
374 this->gl()->fFunctions.fFlush();
375 }
376
377 this->onPlatformSwapBuffers();
378 }
379
makeCurrent()380 bool CommandBufferGLTestContext::makeCurrent() {
381 return TLSCurrentObjects::MakeCurrent(fDisplay, fSurface, fSurface, fContext) != EGL_FALSE;
382 }
383
getStencilBits()384 int CommandBufferGLTestContext::getStencilBits() {
385 EGLint result = 0;
386 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_STENCIL_SIZE, &result);
387 return result;
388 }
389
getSampleCount()390 int CommandBufferGLTestContext::getSampleCount() {
391 EGLint result = 0;
392 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_SAMPLES, &result);
393 return result;
394 }
395
396 } // namespace sk_gpu_test
397 #endif // SK_NO_COMMAND_BUFFER
398