1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | // Display.cpp: Implements the egl::Display class, representing the abstract |
16 | // display on which graphics are drawn. Implements EGLDisplay. |
17 | // [EGL 1.4] section 2.1.2 page 3. |
18 | |
19 | #include "Display.h" |
20 | |
21 | #include "main.h" |
22 | #include "libEGL/Surface.hpp" |
23 | #include "libEGL/Context.hpp" |
24 | #include "common/Image.hpp" |
25 | #include "common/debug.h" |
26 | #include "Common/RecursiveLock.hpp" |
27 | |
28 | #if defined(__ANDROID__) && !defined(ANDROID_NDK_BUILD) |
29 | #include <system/window.h> |
30 | #include <sys/ioctl.h> |
31 | #include <linux/fb.h> |
32 | #include <fcntl.h> |
33 | #elif defined(USE_X11) |
34 | #include "Main/libX11.hpp" |
35 | #elif defined(__APPLE__) |
36 | #include "OSXUtils.hpp" |
37 | #include <CoreFoundation/CoreFoundation.h> |
38 | #include <IOSurface/IOSurface.h> |
39 | #endif |
40 | |
41 | #include <algorithm> |
42 | #include <vector> |
43 | #include <map> |
44 | |
45 | namespace egl |
46 | { |
47 | |
48 | class DisplayImplementation : public Display |
49 | { |
50 | public: |
51 | DisplayImplementation(EGLDisplay dpy, void *nativeDisplay) : Display(dpy, nativeDisplay) {} |
52 | ~DisplayImplementation() override {} |
53 | |
54 | Image *getSharedImage(EGLImageKHR name) override |
55 | { |
56 | return Display::getSharedImage(name); |
57 | } |
58 | }; |
59 | |
60 | Display *Display::get(EGLDisplay dpy) |
61 | { |
62 | if(dpy != PRIMARY_DISPLAY && dpy != HEADLESS_DISPLAY) // We only support the default display |
63 | { |
64 | return nullptr; |
65 | } |
66 | |
67 | static void *nativeDisplay = nullptr; |
68 | |
69 | #if defined(USE_X11) |
70 | // Even if the application provides a native display handle, we open (and close) our own connection |
71 | if(!nativeDisplay && dpy != HEADLESS_DISPLAY && libX11 && libX11->XOpenDisplay) |
72 | { |
73 | nativeDisplay = libX11->XOpenDisplay(NULL); |
74 | } |
75 | #endif |
76 | |
77 | static DisplayImplementation display(dpy, nativeDisplay); |
78 | |
79 | return &display; |
80 | } |
81 | |
82 | Display::Display(EGLDisplay eglDisplay, void *nativeDisplay) : eglDisplay(eglDisplay), nativeDisplay(nativeDisplay) |
83 | { |
84 | mMinSwapInterval = 1; |
85 | mMaxSwapInterval = 1; |
86 | } |
87 | |
88 | Display::~Display() |
89 | { |
90 | terminate(); |
91 | |
92 | #if defined(USE_X11) |
93 | if(nativeDisplay && libX11->XCloseDisplay) |
94 | { |
95 | libX11->XCloseDisplay((::Display*)nativeDisplay); |
96 | } |
97 | #endif |
98 | } |
99 | |
100 | #if !defined(__i386__) && defined(_M_IX86) |
101 | #define __i386__ 1 |
102 | #endif |
103 | |
104 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64)) |
105 | #define __x86_64__ 1 |
106 | #endif |
107 | |
108 | #if defined(__i386__) || defined(__x86_64__) |
109 | static void cpuid(int registers[4], int info) |
110 | { |
111 | #if defined(__i386__) || defined(__x86_64__) |
112 | #if defined(_WIN32) |
113 | __cpuid(registers, info); |
114 | #else |
115 | __asm volatile("cpuid" : "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info)); |
116 | #endif |
117 | #else |
118 | registers[0] = 0; |
119 | registers[1] = 0; |
120 | registers[2] = 0; |
121 | registers[3] = 0; |
122 | #endif |
123 | } |
124 | |
125 | static bool detectSSE() |
126 | { |
127 | int registers[4]; |
128 | cpuid(registers, 1); |
129 | return (registers[3] & 0x02000000) != 0; |
130 | } |
131 | #endif |
132 | |
133 | bool Display::initialize() |
134 | { |
135 | if(isInitialized()) |
136 | { |
137 | return true; |
138 | } |
139 | |
140 | #if defined(__i386__) || defined(__x86_64__) |
141 | if(!detectSSE()) |
142 | { |
143 | return false; |
144 | } |
145 | #endif |
146 | |
147 | mMinSwapInterval = 0; |
148 | mMaxSwapInterval = 4; |
149 | |
150 | const int samples[] = |
151 | { |
152 | 0, |
153 | 2, |
154 | 4 |
155 | }; |
156 | |
157 | const sw::Format renderTargetFormats[] = |
158 | { |
159 | // sw::FORMAT_A1R5G5B5, |
160 | // sw::FORMAT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
161 | sw::FORMAT_A8R8G8B8, |
162 | sw::FORMAT_A8B8G8R8, |
163 | sw::FORMAT_R5G6B5, |
164 | // sw::FORMAT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
165 | sw::FORMAT_X8R8G8B8, |
166 | sw::FORMAT_X8B8G8R8 |
167 | }; |
168 | |
169 | const sw::Format depthStencilFormats[] = |
170 | { |
171 | sw::FORMAT_NULL, |
172 | // sw::FORMAT_D16_LOCKABLE, |
173 | sw::FORMAT_D32, |
174 | // sw::FORMAT_D15S1, |
175 | sw::FORMAT_D24S8, |
176 | sw::FORMAT_D24X8, |
177 | // sw::FORMAT_D24X4S4, |
178 | sw::FORMAT_D16, |
179 | // sw::FORMAT_D32F_LOCKABLE, |
180 | // sw::FORMAT_D24FS8 |
181 | }; |
182 | |
183 | sw::Format currentDisplayFormat = getDisplayFormat(); |
184 | ConfigSet configSet; |
185 | |
186 | for(unsigned int samplesIndex = 0; samplesIndex < sizeof(samples) / sizeof(int); samplesIndex++) |
187 | { |
188 | for(sw::Format renderTargetFormat : renderTargetFormats) |
189 | { |
190 | for(sw::Format depthStencilFormat : depthStencilFormats) |
191 | { |
192 | configSet.add(currentDisplayFormat, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, samples[samplesIndex]); |
193 | } |
194 | } |
195 | } |
196 | |
197 | // Give the sorted configs a unique ID and store them internally |
198 | EGLint index = 1; |
199 | for(ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
200 | { |
201 | Config configuration = *config; |
202 | configuration.mConfigID = index; |
203 | index++; |
204 | |
205 | mConfigSet.mSet.insert(configuration); |
206 | } |
207 | |
208 | if(!isInitialized()) |
209 | { |
210 | terminate(); |
211 | |
212 | return false; |
213 | } |
214 | |
215 | return true; |
216 | } |
217 | |
218 | void Display::terminate() |
219 | { |
220 | while(!mSurfaceSet.empty()) |
221 | { |
222 | destroySurface(*mSurfaceSet.begin()); |
223 | } |
224 | |
225 | while(!mContextSet.empty()) |
226 | { |
227 | destroyContext(*mContextSet.begin()); |
228 | } |
229 | |
230 | while(!mSharedImageNameSpace.empty()) |
231 | { |
232 | destroySharedImage(reinterpret_cast<EGLImageKHR>((intptr_t)mSharedImageNameSpace.firstName())); |
233 | } |
234 | } |
235 | |
236 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
237 | { |
238 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
239 | } |
240 | |
241 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
242 | { |
243 | const egl::Config *configuration = mConfigSet.get(config); |
244 | |
245 | switch(attribute) |
246 | { |
247 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
248 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
249 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
250 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
251 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
252 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
253 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
254 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
255 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
256 | case EGL_LEVEL: *value = configuration->mLevel; break; |
257 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
258 | case EGL_NATIVE_VISUAL_ID: *value = configuration->mNativeVisualID; break; |
259 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
260 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
261 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
262 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
263 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
264 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
265 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
266 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
267 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
268 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
269 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
270 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
271 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
272 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
273 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
274 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
275 | case EGL_MATCH_NATIVE_PIXMAP: *value = EGL_FALSE; UNIMPLEMENTED(); break; |
276 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
277 | case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break; |
278 | case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break; |
279 | case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break; |
280 | case EGL_RECORDABLE_ANDROID: *value = configuration->mRecordableAndroid; break; |
281 | case EGL_FRAMEBUFFER_TARGET_ANDROID: *value = configuration->mFramebufferTargetAndroid; break; |
282 | default: |
283 | return false; |
284 | } |
285 | |
286 | return true; |
287 | } |
288 | |
289 | EGLSurface Display::createWindowSurface(EGLNativeWindowType window, EGLConfig config, const EGLAttrib *attribList) |
290 | { |
291 | const Config *configuration = mConfigSet.get(config); |
292 | |
293 | if(attribList) |
294 | { |
295 | while(*attribList != EGL_NONE) |
296 | { |
297 | switch(attribList[0]) |
298 | { |
299 | case EGL_RENDER_BUFFER: |
300 | switch(attribList[1]) |
301 | { |
302 | case EGL_BACK_BUFFER: |
303 | break; |
304 | case EGL_SINGLE_BUFFER: |
305 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported |
306 | default: |
307 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
308 | } |
309 | break; |
310 | case EGL_VG_COLORSPACE: |
311 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
312 | case EGL_VG_ALPHA_FORMAT: |
313 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
314 | default: |
315 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
316 | } |
317 | |
318 | attribList += 2; |
319 | } |
320 | } |
321 | |
322 | if(hasExistingWindowSurface(window)) |
323 | { |
324 | return error(EGL_BAD_ALLOC, EGL_NO_SURFACE); |
325 | } |
326 | |
327 | Surface *surface = new WindowSurface(this, configuration, window); |
328 | |
329 | if(!surface->initialize()) |
330 | { |
331 | surface->release(); |
332 | return EGL_NO_SURFACE; |
333 | } |
334 | |
335 | surface->addRef(); |
336 | mSurfaceSet.insert(surface); |
337 | |
338 | return success(surface); |
339 | } |
340 | |
341 | EGLSurface Display::createPBufferSurface(EGLConfig config, const EGLint *attribList, EGLClientBuffer clientBuffer) |
342 | { |
343 | EGLint width = -1, height = -1, ioSurfacePlane = -1; |
344 | EGLenum textureFormat = EGL_NO_TEXTURE; |
345 | EGLenum textureTarget = EGL_NO_TEXTURE; |
346 | EGLenum clientBufferFormat = EGL_NO_TEXTURE; |
347 | EGLenum clientBufferType = EGL_NO_TEXTURE; |
348 | EGLBoolean largestPBuffer = EGL_FALSE; |
349 | const Config *configuration = mConfigSet.get(config); |
350 | |
351 | if(attribList) |
352 | { |
353 | while(*attribList != EGL_NONE) |
354 | { |
355 | switch(attribList[0]) |
356 | { |
357 | case EGL_WIDTH: |
358 | width = attribList[1]; |
359 | break; |
360 | case EGL_HEIGHT: |
361 | height = attribList[1]; |
362 | break; |
363 | case EGL_LARGEST_PBUFFER: |
364 | largestPBuffer = attribList[1]; |
365 | break; |
366 | case EGL_TEXTURE_FORMAT: |
367 | switch(attribList[1]) |
368 | { |
369 | case EGL_NO_TEXTURE: |
370 | case EGL_TEXTURE_RGB: |
371 | case EGL_TEXTURE_RGBA: |
372 | textureFormat = attribList[1]; |
373 | break; |
374 | default: |
375 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
376 | } |
377 | break; |
378 | case EGL_TEXTURE_INTERNAL_FORMAT_ANGLE: |
379 | switch(attribList[1]) |
380 | { |
381 | case GL_RED: |
382 | case GL_R16UI: |
383 | case GL_RG: |
384 | case GL_RGB: |
385 | case GL_BGRA_EXT: |
386 | case GL_RGBA: |
387 | clientBufferFormat = attribList[1]; |
388 | break; |
389 | default: |
390 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
391 | } |
392 | break; |
393 | case EGL_TEXTURE_TYPE_ANGLE: |
394 | switch(attribList[1]) |
395 | { |
396 | case GL_UNSIGNED_BYTE: |
397 | case GL_UNSIGNED_SHORT: |
398 | case GL_HALF_FLOAT_OES: |
399 | case GL_HALF_FLOAT: |
400 | clientBufferType = attribList[1]; |
401 | break; |
402 | default: |
403 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
404 | } |
405 | break; |
406 | case EGL_IOSURFACE_PLANE_ANGLE: |
407 | if(attribList[1] < 0) |
408 | { |
409 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
410 | } |
411 | ioSurfacePlane = attribList[1]; |
412 | break; |
413 | case EGL_TEXTURE_TARGET: |
414 | switch(attribList[1]) |
415 | { |
416 | case EGL_NO_TEXTURE: |
417 | case EGL_TEXTURE_2D: |
418 | case EGL_TEXTURE_RECTANGLE_ANGLE: |
419 | textureTarget = attribList[1]; |
420 | break; |
421 | default: |
422 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
423 | } |
424 | break; |
425 | case EGL_MIPMAP_TEXTURE: |
426 | if(attribList[1] != EGL_FALSE) |
427 | { |
428 | UNIMPLEMENTED(); |
429 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
430 | } |
431 | break; |
432 | case EGL_VG_COLORSPACE: |
433 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
434 | case EGL_VG_ALPHA_FORMAT: |
435 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
436 | default: |
437 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
438 | } |
439 | |
440 | attribList += 2; |
441 | } |
442 | } |
443 | |
444 | if(width < 0 || height < 0) |
445 | { |
446 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
447 | } |
448 | |
449 | if(width == 0 || height == 0) |
450 | { |
451 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
452 | } |
453 | |
454 | if((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) || |
455 | (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE)) |
456 | { |
457 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
458 | } |
459 | |
460 | if(!(configuration->mSurfaceType & EGL_PBUFFER_BIT)) |
461 | { |
462 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
463 | } |
464 | |
465 | if(clientBuffer) |
466 | { |
467 | switch(clientBufferType) |
468 | { |
469 | case GL_UNSIGNED_BYTE: |
470 | switch(clientBufferFormat) |
471 | { |
472 | case GL_RED: |
473 | case GL_RG: |
474 | case GL_RGB: |
475 | case GL_BGRA_EXT: |
476 | break; |
477 | case GL_R16UI: |
478 | case GL_RGBA: |
479 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
480 | default: |
481 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
482 | } |
483 | break; |
484 | case GL_UNSIGNED_SHORT: |
485 | switch(clientBufferFormat) |
486 | { |
487 | case GL_R16UI: |
488 | break; |
489 | case GL_RED: |
490 | case GL_RG: |
491 | case GL_BGRA_EXT: |
492 | case GL_RGBA: |
493 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
494 | default: |
495 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
496 | } |
497 | break; |
498 | case GL_HALF_FLOAT_OES: |
499 | case GL_HALF_FLOAT: |
500 | switch(clientBufferFormat) |
501 | { |
502 | case GL_RGBA: |
503 | break; |
504 | case GL_RED: |
505 | case GL_R16UI: |
506 | case GL_RG: |
507 | case GL_BGRA_EXT: |
508 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
509 | default: |
510 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
511 | } |
512 | break; |
513 | default: |
514 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
515 | } |
516 | |
517 | if(ioSurfacePlane < 0) |
518 | { |
519 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
520 | } |
521 | |
522 | if(textureFormat != EGL_TEXTURE_RGBA) |
523 | { |
524 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
525 | } |
526 | |
527 | if(textureTarget != EGL_TEXTURE_RECTANGLE_ANGLE) |
528 | { |
529 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
530 | } |
531 | |
532 | #if defined(__APPLE__) |
533 | IOSurfaceRef ioSurface = reinterpret_cast<IOSurfaceRef>(clientBuffer); |
534 | size_t planeCount = IOSurfaceGetPlaneCount(ioSurface); |
535 | if((static_cast<size_t>(width) > IOSurfaceGetWidthOfPlane(ioSurface, ioSurfacePlane)) || |
536 | (static_cast<size_t>(height) > IOSurfaceGetHeightOfPlane(ioSurface, ioSurfacePlane)) || |
537 | ((planeCount != 0) && static_cast<size_t>(ioSurfacePlane) >= planeCount)) |
538 | { |
539 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
540 | } |
541 | #endif |
542 | } |
543 | else |
544 | { |
545 | if((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) || |
546 | ((textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))) |
547 | { |
548 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
549 | } |
550 | } |
551 | |
552 | Surface *surface = new PBufferSurface(this, configuration, width, height, textureFormat, textureTarget, clientBufferFormat, clientBufferType, largestPBuffer, clientBuffer, ioSurfacePlane); |
553 | |
554 | if(!surface->initialize()) |
555 | { |
556 | surface->release(); |
557 | return EGL_NO_SURFACE; |
558 | } |
559 | |
560 | surface->addRef(); |
561 | mSurfaceSet.insert(surface); |
562 | |
563 | return success(surface); |
564 | } |
565 | |
566 | EGLContext Display::createContext(EGLConfig configHandle, const egl::Context *shareContext, EGLint clientVersion) |
567 | { |
568 | const egl::Config *config = mConfigSet.get(configHandle); |
569 | egl::Context *context = nullptr; |
570 | |
571 | if(clientVersion == 1 && config->mRenderableType & EGL_OPENGL_ES_BIT) |
572 | { |
573 | if(libGLES_CM) |
574 | { |
575 | context = libGLES_CM->es1CreateContext(this, shareContext, config); |
576 | } |
577 | } |
578 | else if((clientVersion == 2 && config->mRenderableType & EGL_OPENGL_ES2_BIT) || |
579 | (clientVersion == 3 && config->mRenderableType & EGL_OPENGL_ES3_BIT)) |
580 | { |
581 | if(libGLESv2) |
582 | { |
583 | context = libGLESv2->es2CreateContext(this, shareContext, config); |
584 | } |
585 | } |
586 | else |
587 | { |
588 | return error(EGL_BAD_CONFIG, EGL_NO_CONTEXT); |
589 | } |
590 | |
591 | if(!context) |
592 | { |
593 | return error(EGL_BAD_ALLOC, EGL_NO_CONTEXT); |
594 | } |
595 | |
596 | context->addRef(); |
597 | mContextSet.insert(context); |
598 | |
599 | return success(context); |
600 | } |
601 | |
602 | EGLSyncKHR Display::createSync(Context *context) |
603 | { |
604 | FenceSync *fenceSync = new egl::FenceSync(context); |
605 | mSyncSet.insert(fenceSync); |
606 | return fenceSync; |
607 | } |
608 | |
609 | void Display::destroySurface(egl::Surface *surface) |
610 | { |
611 | surface->release(); |
612 | mSurfaceSet.erase(surface); |
613 | |
614 | if(surface == getCurrentDrawSurface()) |
615 | { |
616 | setCurrentDrawSurface(nullptr); |
617 | } |
618 | |
619 | if(surface == getCurrentReadSurface()) |
620 | { |
621 | setCurrentReadSurface(nullptr); |
622 | } |
623 | } |
624 | |
625 | void Display::destroyContext(egl::Context *context) |
626 | { |
627 | context->release(); |
628 | mContextSet.erase(context); |
629 | |
630 | if(context == getCurrentContext()) |
631 | { |
632 | setCurrentContext(nullptr); |
633 | setCurrentDrawSurface(nullptr); |
634 | setCurrentReadSurface(nullptr); |
635 | } |
636 | } |
637 | |
638 | void Display::destroySync(FenceSync *sync) |
639 | { |
640 | { |
641 | mSyncSet.erase(sync); |
642 | } |
643 | delete sync; |
644 | } |
645 | |
646 | bool Display::isInitialized() const |
647 | { |
648 | return mConfigSet.size() > 0; |
649 | } |
650 | |
651 | bool Display::isValidConfig(EGLConfig config) |
652 | { |
653 | return mConfigSet.get(config) != nullptr; |
654 | } |
655 | |
656 | bool Display::isValidContext(egl::Context *context) |
657 | { |
658 | return mContextSet.find(context) != mContextSet.end(); |
659 | } |
660 | |
661 | bool Display::isValidSurface(egl::Surface *surface) |
662 | { |
663 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
664 | } |
665 | |
666 | bool Display::isValidWindow(EGLNativeWindowType window) |
667 | { |
668 | #if defined(_WIN32) |
669 | return IsWindow(window) == TRUE; |
670 | #elif defined(__ANDROID__) |
671 | if(!window) |
672 | { |
673 | ERR("%s called with window==NULL %s:%d" , __FUNCTION__, __FILE__, __LINE__); |
674 | return false; |
675 | } |
676 | #if !defined(ANDROID_NDK_BUILD) |
677 | if(static_cast<ANativeWindow*>(window)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) |
678 | { |
679 | ERR("%s called with window==%p bad magic %s:%d" , __FUNCTION__, window, __FILE__, __LINE__); |
680 | return false; |
681 | } |
682 | #endif // !defined(ANDROID_NDK_BUILD) |
683 | return true; |
684 | #elif defined(USE_X11) |
685 | if(nativeDisplay) |
686 | { |
687 | XWindowAttributes windowAttributes; |
688 | Status status = libX11->XGetWindowAttributes((::Display*)nativeDisplay, window, &windowAttributes); |
689 | |
690 | return status != 0; |
691 | } |
692 | return false; |
693 | #elif defined(__linux__) |
694 | return false; // Non X11 linux is headless only |
695 | #elif defined(__APPLE__) |
696 | return sw::OSX::IsValidWindow(window); |
697 | #elif defined(__Fuchsia__) |
698 | // TODO(crbug.com/800951): Integrate with Mozart. |
699 | return true; |
700 | #else |
701 | #error "Display::isValidWindow unimplemented for this platform" |
702 | return false; |
703 | #endif |
704 | } |
705 | |
706 | bool Display::hasExistingWindowSurface(EGLNativeWindowType window) |
707 | { |
708 | for(const auto &surface : mSurfaceSet) |
709 | { |
710 | if(surface->isWindowSurface()) |
711 | { |
712 | if(surface->getWindowHandle() == window) |
713 | { |
714 | return true; |
715 | } |
716 | } |
717 | } |
718 | |
719 | return false; |
720 | } |
721 | |
722 | bool Display::isValidSync(FenceSync *sync) |
723 | { |
724 | return mSyncSet.find(sync) != mSyncSet.end(); |
725 | } |
726 | |
727 | EGLint Display::getMinSwapInterval() const |
728 | { |
729 | return mMinSwapInterval; |
730 | } |
731 | |
732 | EGLint Display::getMaxSwapInterval() const |
733 | { |
734 | return mMaxSwapInterval; |
735 | } |
736 | |
737 | EGLDisplay Display::getEGLDisplay() const |
738 | { |
739 | return eglDisplay; |
740 | } |
741 | |
742 | void *Display::getNativeDisplay() const |
743 | { |
744 | return nativeDisplay; |
745 | } |
746 | |
747 | EGLImageKHR Display::createSharedImage(Image *image) |
748 | { |
749 | return reinterpret_cast<EGLImageKHR>((intptr_t)mSharedImageNameSpace.allocate(image)); |
750 | } |
751 | |
752 | bool Display::destroySharedImage(EGLImageKHR image) |
753 | { |
754 | GLuint name = (GLuint)reinterpret_cast<intptr_t>(image); |
755 | Image *eglImage = mSharedImageNameSpace.find(name); |
756 | |
757 | if(!eglImage) |
758 | { |
759 | return false; |
760 | } |
761 | |
762 | eglImage->destroyShared(); |
763 | mSharedImageNameSpace.remove(name); |
764 | |
765 | return true; |
766 | } |
767 | |
768 | Image *Display::getSharedImage(EGLImageKHR image) |
769 | { |
770 | GLuint name = (GLuint)reinterpret_cast<intptr_t>(image); |
771 | return mSharedImageNameSpace.find(name); |
772 | } |
773 | |
774 | sw::Format Display::getDisplayFormat() const |
775 | { |
776 | #if defined(_WIN32) |
777 | HDC deviceContext = GetDC(0); |
778 | unsigned int bpp = ::GetDeviceCaps(deviceContext, BITSPIXEL); |
779 | ReleaseDC(0, deviceContext); |
780 | |
781 | switch(bpp) |
782 | { |
783 | case 32: return sw::FORMAT_X8R8G8B8; |
784 | case 24: return sw::FORMAT_R8G8B8; |
785 | case 16: return sw::FORMAT_R5G6B5; |
786 | default: UNREACHABLE(bpp); // Unexpected display mode color depth |
787 | } |
788 | #elif defined(__ANDROID__) |
789 | #if !defined(ANDROID_NDK_BUILD) |
790 | static const char *const framebuffer[] = |
791 | { |
792 | "/dev/graphics/fb0" , |
793 | "/dev/fb0" , |
794 | 0 |
795 | }; |
796 | |
797 | for(int i = 0; framebuffer[i]; i++) |
798 | { |
799 | int fd = open(framebuffer[i], O_RDONLY, 0); |
800 | |
801 | if(fd != -1) |
802 | { |
803 | struct fb_var_screeninfo info; |
804 | int io = ioctl(fd, FBIOGET_VSCREENINFO, &info); |
805 | close(fd); |
806 | |
807 | if(io >= 0) |
808 | { |
809 | switch(info.bits_per_pixel) |
810 | { |
811 | case 16: |
812 | return sw::FORMAT_R5G6B5; |
813 | case 32: |
814 | if(info.red.length == 8 && info.red.offset == 16 && |
815 | info.green.length == 8 && info.green.offset == 8 && |
816 | info.blue.length == 8 && info.blue.offset == 0 && |
817 | info.transp.length == 0) |
818 | { |
819 | return sw::FORMAT_X8R8G8B8; |
820 | } |
821 | if(info.red.length == 8 && info.red.offset == 0 && |
822 | info.green.length == 8 && info.green.offset == 8 && |
823 | info.blue.length == 8 && info.blue.offset == 16 && |
824 | info.transp.length == 0) |
825 | { |
826 | return sw::FORMAT_X8B8G8R8; |
827 | } |
828 | if(info.red.length == 8 && info.red.offset == 16 && |
829 | info.green.length == 8 && info.green.offset == 8 && |
830 | info.blue.length == 8 && info.blue.offset == 0 && |
831 | info.transp.length == 8 && info.transp.offset == 24) |
832 | { |
833 | return sw::FORMAT_A8R8G8B8; |
834 | } |
835 | if(info.red.length == 8 && info.red.offset == 0 && |
836 | info.green.length == 8 && info.green.offset == 8 && |
837 | info.blue.length == 8 && info.blue.offset == 16 && |
838 | info.transp.length == 8 && info.transp.offset == 24) |
839 | { |
840 | return sw::FORMAT_A8B8G8R8; |
841 | } |
842 | else UNIMPLEMENTED(); |
843 | default: |
844 | UNIMPLEMENTED(); |
845 | } |
846 | } |
847 | } |
848 | } |
849 | #endif // !defined_ANDROID_NDK_BUILD) |
850 | |
851 | // No framebuffer device found, or we're in user space |
852 | return sw::FORMAT_X8B8G8R8; |
853 | #elif defined(USE_X11) |
854 | if(nativeDisplay) |
855 | { |
856 | Screen *screen = libX11->XDefaultScreenOfDisplay((::Display*)nativeDisplay); |
857 | unsigned int bpp = libX11->XPlanesOfScreen(screen); |
858 | |
859 | switch(bpp) |
860 | { |
861 | case 32: return sw::FORMAT_X8R8G8B8; |
862 | case 24: return sw::FORMAT_R8G8B8; |
863 | case 16: return sw::FORMAT_R5G6B5; |
864 | default: UNREACHABLE(bpp); // Unexpected display mode color depth |
865 | } |
866 | } |
867 | else |
868 | { |
869 | return sw::FORMAT_X8R8G8B8; |
870 | } |
871 | #elif defined(__linux__) // Non X11 linux is headless only |
872 | return sw::FORMAT_A8B8G8R8; |
873 | #elif defined(__APPLE__) |
874 | return sw::FORMAT_A8B8G8R8; |
875 | #elif defined(__Fuchsia__) |
876 | return sw::FORMAT_A8B8G8R8; |
877 | #else |
878 | #error "Display::isValidWindow unimplemented for this platform" |
879 | #endif |
880 | |
881 | return sw::FORMAT_X8R8G8B8; |
882 | } |
883 | |
884 | } |
885 | |