OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 #include "SkOnce.h" | |
8 #include "gl/GrGLInterface.h" | |
9 #include "gl/GrGLAssembleInterface.h" | |
10 #include "gl/command_buffer/SkCommandBufferGLContext.h" | |
11 #include "../ports/SkOSEnvironment.h" | |
12 #include "../ports/SkOSLibrary.h" | |
13 | |
14 #if defined SK_BUILD_FOR_MAC | |
15 | |
16 // EGL doesn't exist on the mac, so expose what we need to get the command buffe
r's EGL running. | |
17 typedef void *EGLDisplay; | |
18 typedef unsigned int EGLBoolean; | |
19 typedef void *EGLConfig; | |
20 typedef void *EGLSurface; | |
21 typedef void *EGLContext; | |
22 typedef int32_t EGLint; | |
23 typedef void* EGLNativeDisplayType; | |
24 typedef void* EGLNativeWindowType; | |
25 typedef void (*__eglMustCastToProperFunctionPointerType)(void); | |
26 #define EGL_FALSE 0 | |
27 #define EGL_OPENGL_ES2_BIT 0x0004 | |
28 #define EGL_CONTEXT_CLIENT_VERSION 0x3098 | |
29 #define EGL_NO_SURFACE ((EGLSurface)0) | |
30 #define EGL_NO_DISPLAY ((EGLDisplay)0) | |
31 #define EGL_NO_CONTEXT ((EGLContext)0) | |
32 #define EGL_DEFAULT_DISPLAY ((EGLNativeDisplayType)0) | |
33 #define EGL_SURFACE_TYPE 0x3033 | |
34 #define EGL_PBUFFER_BIT 0x0001 | |
35 #define EGL_RENDERABLE_TYPE 0x3040 | |
36 #define EGL_RED_SIZE 0x3024 | |
37 #define EGL_GREEN_SIZE 0x3023 | |
38 #define EGL_BLUE_SIZE 0x3022 | |
39 #define EGL_ALPHA_SIZE 0x3021 | |
40 #define EGL_DEPTH_SIZE 0x3025 | |
41 #define EGL_STENCIL_SIZE 0x3025 | |
42 #define EGL_SAMPLES 0x3031 | |
43 #define EGL_SAMPLE_BUFFERS 0x3032 | |
44 #define EGL_NONE 0x3038 | |
45 #define EGL_WIDTH 0x3057 | |
46 #define EGL_HEIGHT 0x3056 | |
47 | |
48 #else | |
49 | |
50 #include <EGL/egl.h> | |
51 | |
52 #endif | |
53 | |
54 typedef EGLDisplay (*GetDisplayProc)(EGLNativeDisplayType display_id); | |
55 typedef EGLBoolean (*InitializeProc)(EGLDisplay dpy, EGLint *major, EGLint *mino
r); | |
56 typedef EGLBoolean (*TerminateProc)(EGLDisplay dpy); | |
57 typedef EGLBoolean (*ChooseConfigProc)(EGLDisplay dpy, const EGLint* attrib_list
, EGLConfig* configs, EGLint config_size, EGLint* num_config); | |
58 typedef EGLBoolean (*GetConfigAttrib)(EGLDisplay dpy, EGLConfig config, EGLint a
ttribute, EGLint* value); | |
59 typedef EGLSurface (*CreateWindowSurfaceProc)(EGLDisplay dpy, EGLConfig config,
EGLNativeWindowType win, const EGLint* attrib_list); | |
60 typedef EGLSurface (*CreatePbufferSurfaceProc)(EGLDisplay dpy, EGLConfig config,
const EGLint* attrib_list); | |
61 typedef EGLBoolean (*DestroySurfaceProc)(EGLDisplay dpy, EGLSurface surface); | |
62 typedef EGLContext (*CreateContextProc)(EGLDisplay dpy, EGLConfig config, EGLCon
text share_context, const EGLint* attrib_list); | |
63 typedef EGLBoolean (*DestroyContextProc)(EGLDisplay dpy, EGLContext ctx); | |
64 typedef EGLBoolean (*MakeCurrentProc)(EGLDisplay dpy, EGLSurface draw, EGLSurfac
e read, EGLContext ctx); | |
65 typedef EGLBoolean (*SwapBuffersProc)(EGLDisplay dpy, EGLSurface surface); | |
66 typedef __eglMustCastToProperFunctionPointerType (*GetProcAddressProc)(const cha
r* procname); | |
67 | |
68 static GetDisplayProc gfGetDisplay = nullptr; | |
69 static InitializeProc gfInitialize = nullptr; | |
70 static TerminateProc gfTerminate = nullptr; | |
71 static ChooseConfigProc gfChooseConfig = nullptr; | |
72 static GetConfigAttrib gfGetConfigAttrib = nullptr; | |
73 static CreateWindowSurfaceProc gfCreateWindowSurface = nullptr; | |
74 static CreatePbufferSurfaceProc gfCreatePbufferSurface = nullptr; | |
75 static DestroySurfaceProc gfDestroySurface = nullptr; | |
76 static CreateContextProc gfCreateContext = nullptr; | |
77 static DestroyContextProc gfDestroyContext = nullptr; | |
78 static MakeCurrentProc gfMakeCurrent = nullptr; | |
79 static SwapBuffersProc gfSwapBuffers = nullptr; | |
80 static GetProcAddressProc gfGetProcAddress = nullptr; | |
81 | |
82 static void* gLibrary = nullptr; | |
83 static bool gfFunctionsLoadedSuccessfully = false; | |
84 | |
85 static void load_command_buffer_functions() { | |
86 if (!gLibrary) { | |
87 #if defined _WIN32 | |
88 gLibrary = DynamicLoadLibrary("command_buffer_gles2.dll"); | |
89 #elif defined SK_BUILD_FOR_MAC | |
90 gLibrary = DynamicLoadLibrary("libcommand_buffer_gles2.dylib"); | |
91 #else | |
92 gLibrary = DynamicLoadLibrary("libcommand_buffer_gles2.so"); | |
93 #endif // defined _WIN32 | |
94 if (gLibrary) { | |
95 gfGetDisplay = (GetDisplayProc)GetProcedureAddress(gLibrary, "eglGet
Display"); | |
96 gfInitialize = (InitializeProc)GetProcedureAddress(gLibrary, "eglIni
tialize"); | |
97 gfTerminate = (TerminateProc)GetProcedureAddress(gLibrary, "eglTermi
nate"); | |
98 gfChooseConfig = (ChooseConfigProc)GetProcedureAddress(gLibrary, "eg
lChooseConfig"); | |
99 gfGetConfigAttrib = (GetConfigAttrib)GetProcedureAddress(gLibrary, "
eglGetConfigAttrib"); | |
100 gfCreateWindowSurface = (CreateWindowSurfaceProc)GetProcedureAddress
(gLibrary, "eglCreateWindowSurface"); | |
101 gfCreatePbufferSurface = (CreatePbufferSurfaceProc)GetProcedureAddre
ss(gLibrary, "eglCreatePbufferSurface"); | |
102 gfDestroySurface = (DestroySurfaceProc)GetProcedureAddress(gLibrary,
"eglDestroySurface"); | |
103 gfCreateContext = (CreateContextProc)GetProcedureAddress(gLibrary, "
eglCreateContext"); | |
104 gfDestroyContext = (DestroyContextProc)GetProcedureAddress(gLibrary,
"eglDestroyContext"); | |
105 gfMakeCurrent = (MakeCurrentProc)GetProcedureAddress(gLibrary, "eglM
akeCurrent"); | |
106 gfSwapBuffers = (SwapBuffersProc)GetProcedureAddress(gLibrary, "eglS
wapBuffers"); | |
107 gfGetProcAddress = (GetProcAddressProc)GetProcedureAddress(gLibrary,
"eglGetProcAddress"); | |
108 | |
109 gfFunctionsLoadedSuccessfully = gfGetDisplay && gfInitialize && gfTe
rminate && | |
110 gfChooseConfig && gfCreateWindowSurf
ace && | |
111 gfCreatePbufferSurface && gfDestroyS
urface && | |
112 gfCreateContext && gfDestroyContext
&& gfMakeCurrent && | |
113 gfSwapBuffers && gfGetProcAddress; | |
114 | |
115 } | |
116 } | |
117 } | |
118 | |
119 static GrGLFuncPtr command_buffer_get_gl_proc(void* ctx, const char name[]) { | |
120 if (!gfFunctionsLoadedSuccessfully) { | |
121 return nullptr; | |
122 } | |
123 return gfGetProcAddress(name); | |
124 } | |
125 | |
126 SK_DECLARE_STATIC_ONCE(loadCommandBufferOnce); | |
127 void LoadCommandBufferOnce() { | |
128 SkOnce(&loadCommandBufferOnce, load_command_buffer_functions); | |
129 } | |
130 | |
131 const GrGLInterface* GrGLCreateCommandBufferInterface() { | |
132 LoadCommandBufferOnce(); | |
133 if (!gfFunctionsLoadedSuccessfully) { | |
134 return nullptr; | |
135 } | |
136 return GrGLAssembleGLESInterface(gLibrary, command_buffer_get_gl_proc); | |
137 } | |
138 | |
139 SkCommandBufferGLContext::SkCommandBufferGLContext() | |
140 : fContext(EGL_NO_CONTEXT) | |
141 , fDisplay(EGL_NO_DISPLAY) | |
142 , fSurface(EGL_NO_SURFACE) { | |
143 | |
144 static const EGLint configAttribs[] = { | |
145 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, | |
146 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
147 EGL_RED_SIZE, 8, | |
148 EGL_GREEN_SIZE, 8, | |
149 EGL_BLUE_SIZE, 8, | |
150 EGL_ALPHA_SIZE, 8, | |
151 EGL_NONE | |
152 }; | |
153 | |
154 static const EGLint surfaceAttribs[] = { | |
155 EGL_WIDTH, 1, | |
156 EGL_HEIGHT, 1, | |
157 EGL_NONE | |
158 }; | |
159 | |
160 initializeGLContext(nullptr, configAttribs, surfaceAttribs); | |
161 } | |
162 | |
163 SkCommandBufferGLContext::SkCommandBufferGLContext(void* nativeWindow, int msaaS
ampleCount) { | |
164 static const EGLint surfaceAttribs[] = { EGL_NONE }; | |
165 | |
166 EGLint configAttribs[] = { | |
167 EGL_RED_SIZE, 8, | |
168 EGL_GREEN_SIZE, 8, | |
169 EGL_BLUE_SIZE, 8, | |
170 EGL_ALPHA_SIZE, 8, | |
171 EGL_DEPTH_SIZE, 8, | |
172 EGL_STENCIL_SIZE, 8, | |
173 EGL_SAMPLE_BUFFERS, 1, | |
174 EGL_SAMPLES, msaaSampleCount, | |
175 EGL_NONE | |
176 }; | |
177 if (msaaSampleCount == 0) { | |
178 configAttribs[12] = EGL_NONE; | |
179 } | |
180 | |
181 initializeGLContext(nativeWindow, configAttribs, surfaceAttribs); | |
182 } | |
183 | |
184 void SkCommandBufferGLContext::initializeGLContext(void* nativeWindow, const int
* configAttribs, | |
185 const int* surfaceAttribs) { | |
186 LoadCommandBufferOnce(); | |
187 if (!gfFunctionsLoadedSuccessfully) { | |
188 SkDebugf("Command Buffer: Could not load EGL functions.\n"); | |
189 return; | |
190 } | |
191 | |
192 // Make sure CHROMIUM_path_rendering is enabled for NVPR support. | |
193 sk_setenv("CHROME_COMMAND_BUFFER_GLES2_ARGS", "--enable-gl-path-rendering"); | |
194 fDisplay = gfGetDisplay(EGL_DEFAULT_DISPLAY); | |
195 if (EGL_NO_DISPLAY == fDisplay) { | |
196 SkDebugf("Command Buffer: Could not create EGL display.\n"); | |
197 return; | |
198 } | |
199 | |
200 EGLint majorVersion; | |
201 EGLint minorVersion; | |
202 if (!gfInitialize(fDisplay, &majorVersion, &minorVersion)) { | |
203 SkDebugf("Command Buffer: Could not initialize EGL display.\n"); | |
204 this->destroyGLContext(); | |
205 return; | |
206 } | |
207 | |
208 EGLint numConfigs; | |
209 if (!gfChooseConfig(fDisplay, configAttribs, static_cast<EGLConfig*>(&fConfi
g), 1, | |
210 &numConfigs) || numConfigs != 1) { | |
211 SkDebugf("Command Buffer: Could not choose EGL config.\n"); | |
212 this->destroyGLContext(); | |
213 return; | |
214 } | |
215 | |
216 if (nativeWindow) { | |
217 fSurface = gfCreateWindowSurface(fDisplay, | |
218 static_cast<EGLConfig>(fConfig), | |
219 (EGLNativeWindowType)nativeWindow, | |
220 surfaceAttribs); | |
221 } else { | |
222 fSurface = gfCreatePbufferSurface(fDisplay, | |
223 static_cast<EGLConfig>(fConfig), | |
224 surfaceAttribs); | |
225 } | |
226 if (EGL_NO_SURFACE == fSurface) { | |
227 SkDebugf("Command Buffer: Could not create EGL surface.\n"); | |
228 this->destroyGLContext(); | |
229 return; | |
230 } | |
231 | |
232 static const EGLint contextAttribs[] = { | |
233 EGL_CONTEXT_CLIENT_VERSION, 2, | |
234 EGL_NONE | |
235 }; | |
236 fContext = gfCreateContext(fDisplay, static_cast<EGLConfig>(fConfig), nullpt
r, contextAttribs); | |
237 if (EGL_NO_CONTEXT == fContext) { | |
238 SkDebugf("Command Buffer: Could not create EGL context.\n"); | |
239 this->destroyGLContext(); | |
240 return; | |
241 } | |
242 | |
243 if (!gfMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | |
244 SkDebugf("Command Buffer: Could not make EGL context current.\n"); | |
245 this->destroyGLContext(); | |
246 return; | |
247 } | |
248 | |
249 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateCommandBufferInterface()); | |
250 if (nullptr == gl.get()) { | |
251 SkDebugf("Command Buffer: Could not create CommandBuffer GL interface.\n
"); | |
252 this->destroyGLContext(); | |
253 return; | |
254 } | |
255 if (!gl->validate()) { | |
256 SkDebugf("Command Buffer: Could not validate CommandBuffer GL interface.
\n"); | |
257 this->destroyGLContext(); | |
258 return; | |
259 } | |
260 | |
261 this->init(gl.release()); | |
262 } | |
263 | |
264 SkCommandBufferGLContext::~SkCommandBufferGLContext() { | |
265 this->teardown(); | |
266 this->destroyGLContext(); | |
267 } | |
268 | |
269 void SkCommandBufferGLContext::destroyGLContext() { | |
270 if (!gfFunctionsLoadedSuccessfully) { | |
271 return; | |
272 } | |
273 if (fDisplay) { | |
274 gfMakeCurrent(fDisplay, 0, 0, 0); | |
275 | |
276 if (fContext) { | |
277 gfDestroyContext(fDisplay, fContext); | |
278 fContext = EGL_NO_CONTEXT; | |
279 } | |
280 | |
281 if (fSurface) { | |
282 gfDestroySurface(fDisplay, fSurface); | |
283 fSurface = EGL_NO_SURFACE; | |
284 } | |
285 | |
286 gfTerminate(fDisplay); | |
287 fDisplay = EGL_NO_DISPLAY; | |
288 } | |
289 } | |
290 | |
291 void SkCommandBufferGLContext::onPlatformMakeCurrent() const { | |
292 if (!gfFunctionsLoadedSuccessfully) { | |
293 return; | |
294 } | |
295 if (!gfMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { | |
296 SkDebugf("Command Buffer: Could not make EGL context current.\n"); | |
297 } | |
298 } | |
299 | |
300 void SkCommandBufferGLContext::onPlatformSwapBuffers() const { | |
301 if (!gfFunctionsLoadedSuccessfully) { | |
302 return; | |
303 } | |
304 if (!gfSwapBuffers(fDisplay, fSurface)) { | |
305 SkDebugf("Command Buffer: Could not complete gfSwapBuffers.\n"); | |
306 } | |
307 } | |
308 | |
309 GrGLFuncPtr SkCommandBufferGLContext::onPlatformGetProcAddress(const char* name)
const { | |
310 if (!gfFunctionsLoadedSuccessfully) { | |
311 return nullptr; | |
312 } | |
313 return gfGetProcAddress(name); | |
314 } | |
315 | |
316 void SkCommandBufferGLContext::presentCommandBuffer() { | |
317 if (this->gl()) { | |
318 this->gl()->fFunctions.fFlush(); | |
319 } | |
320 | |
321 this->onPlatformSwapBuffers(); | |
322 } | |
323 | |
324 bool SkCommandBufferGLContext::makeCurrent() { | |
325 return gfMakeCurrent(fDisplay, fSurface, fSurface, fContext) != EGL_FALSE; | |
326 } | |
327 | |
328 int SkCommandBufferGLContext::getStencilBits() { | |
329 EGLint result = 0; | |
330 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_STENCIL_SIZ
E, &result); | |
331 return result; | |
332 } | |
333 | |
334 int SkCommandBufferGLContext::getSampleCount() { | |
335 EGLint result = 0; | |
336 gfGetConfigAttrib(fDisplay, static_cast<EGLConfig>(fConfig), EGL_SAMPLES, &r
esult); | |
337 return result; | |
338 } | |
OLD | NEW |