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