1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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.
22 #include "libEGL/Surface.hpp"
23 #include "libEGL/Context.hpp"
24 #include "common/Image.hpp"
25 #include "common/debug.h"
26 #include "Common/MutexLock.hpp"
29 #include <system/window.h>
30 #include <sys/ioctl.h>
33 #elif defined(__linux__) || defined(__DragonFly__)
34 #include "Main/libX11.hpp"
35 #elif defined(__APPLE__)
36 #include "OSXUtils.hpp"
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <IOSurface/IOSurface.h>
48 class DisplayImplementation : public Display
51 DisplayImplementation(EGLDisplay dpy, void *nativeDisplay) : Display(dpy, nativeDisplay) {}
52 ~DisplayImplementation() override {}
54 Image *getSharedImage(EGLImageKHR name) override
56 return Display::getSharedImage(name);
60 Display *Display::get(EGLDisplay dpy)
62 if(dpy != PRIMARY_DISPLAY && dpy != HEADLESS_DISPLAY) // We only support the default display
67 static void *nativeDisplay = nullptr;
69 #if (defined(__linux__) && !defined(__ANDROID__)) || \
70 defined(__DragonFly__)
71 // Even if the application provides a native display handle, we open (and close) our own connection
72 if(!nativeDisplay && dpy != HEADLESS_DISPLAY && libX11 && libX11->XOpenDisplay)
74 nativeDisplay = libX11->XOpenDisplay(NULL);
78 static DisplayImplementation display(dpy, nativeDisplay);
83 Display::Display(EGLDisplay eglDisplay, void *nativeDisplay) : eglDisplay(eglDisplay), nativeDisplay(nativeDisplay)
93 #if (defined(__linux__) && !defined(__ANDROID__)) || \
94 defined(__DragonFly__)
95 if(nativeDisplay && libX11->XCloseDisplay)
97 libX11->XCloseDisplay((::Display*)nativeDisplay);
102 #if !defined(__i386__) && defined(_M_IX86)
106 #if !defined(__x86_64__) && (defined(_M_AMD64) || defined (_M_X64))
110 static void cpuid(int registers[4], int info)
112 #if defined(__i386__) || defined(__x86_64__)
114 __cpuid(registers, info);
116 __asm volatile("cpuid": "=a" (registers[0]), "=b" (registers[1]), "=c" (registers[2]), "=d" (registers[3]): "a" (info));
126 static bool detectSSE()
130 return (registers[3] & 0x02000000) != 0;
133 bool Display::initialize()
140 #if defined(__i386__) || defined(__x86_64__)
147 mMinSwapInterval = 0;
148 mMaxSwapInterval = 4;
150 const int samples[] =
157 const sw::Format renderTargetFormats[] =
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.
164 // sw::FORMAT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
169 const sw::Format depthStencilFormats[] =
172 // sw::FORMAT_D16_LOCKABLE,
177 // sw::FORMAT_D24X4S4,
179 // sw::FORMAT_D32F_LOCKABLE,
183 sw::Format currentDisplayFormat = getDisplayFormat();
186 for(unsigned int samplesIndex = 0; samplesIndex < sizeof(samples) / sizeof(int); samplesIndex++)
188 for(unsigned int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(sw::Format); formatIndex++)
190 sw::Format renderTargetFormat = renderTargetFormats[formatIndex];
192 for(unsigned int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(sw::Format); depthStencilIndex++)
194 sw::Format depthStencilFormat = depthStencilFormats[depthStencilIndex];
196 configSet.add(currentDisplayFormat, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, samples[samplesIndex]);
201 // Give the sorted configs a unique ID and store them internally
203 for(ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
205 Config configuration = *config;
206 configuration.mConfigID = index;
209 mConfigSet.mSet.insert(configuration);
222 void Display::terminate()
224 while(!mSurfaceSet.empty())
226 destroySurface(*mSurfaceSet.begin());
229 while(!mContextSet.empty())
231 destroyContext(*mContextSet.begin());
234 while(!mSharedImageNameSpace.empty())
236 destroySharedImage(reinterpret_cast<EGLImageKHR>((intptr_t)mSharedImageNameSpace.firstName()));
240 bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
242 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
245 bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
247 const egl::Config *configuration = mConfigSet.get(config);
251 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
252 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
253 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
254 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
255 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
256 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
257 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
258 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
259 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
260 case EGL_LEVEL: *value = configuration->mLevel; break;
261 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
262 case EGL_NATIVE_VISUAL_ID: *value = configuration->mNativeVisualID; break;
263 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
264 case EGL_SAMPLES: *value = configuration->mSamples; break;
265 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
266 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
267 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
268 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
269 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
270 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
271 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
272 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
273 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
274 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
275 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
276 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
277 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
278 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
279 case EGL_MATCH_NATIVE_PIXMAP: *value = EGL_FALSE; UNIMPLEMENTED(); break;
280 case EGL_CONFORMANT: *value = configuration->mConformant; break;
281 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
282 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
283 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
284 case EGL_RECORDABLE_ANDROID: *value = configuration->mRecordableAndroid; break;
285 case EGL_FRAMEBUFFER_TARGET_ANDROID: *value = configuration->mFramebufferTargetAndroid; break;
293 EGLSurface Display::createWindowSurface(EGLNativeWindowType window, EGLConfig config, const EGLint *attribList)
295 const Config *configuration = mConfigSet.get(config);
299 while(*attribList != EGL_NONE)
301 switch(attribList[0])
303 case EGL_RENDER_BUFFER:
304 switch(attribList[1])
306 case EGL_BACK_BUFFER:
308 case EGL_SINGLE_BUFFER:
309 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
311 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
314 case EGL_VG_COLORSPACE:
315 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
316 case EGL_VG_ALPHA_FORMAT:
317 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
319 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
326 if(hasExistingWindowSurface(window))
328 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
331 Surface *surface = new WindowSurface(this, configuration, window);
333 if(!surface->initialize())
336 return EGL_NO_SURFACE;
340 mSurfaceSet.insert(surface);
342 return success(surface);
345 EGLSurface Display::createPBufferSurface(EGLConfig config, const EGLint *attribList, EGLClientBuffer clientBuffer)
347 EGLint width = -1, height = -1, ioSurfacePlane = -1;
348 EGLenum textureFormat = EGL_NO_TEXTURE;
349 EGLenum textureTarget = EGL_NO_TEXTURE;
350 EGLenum clientBufferFormat = EGL_NO_TEXTURE;
351 EGLenum clientBufferType = EGL_NO_TEXTURE;
352 EGLBoolean largestPBuffer = EGL_FALSE;
353 const Config *configuration = mConfigSet.get(config);
357 while(*attribList != EGL_NONE)
359 switch(attribList[0])
362 width = attribList[1];
365 height = attribList[1];
367 case EGL_LARGEST_PBUFFER:
368 largestPBuffer = attribList[1];
370 case EGL_TEXTURE_FORMAT:
371 switch(attribList[1])
374 case EGL_TEXTURE_RGB:
375 case EGL_TEXTURE_RGBA:
376 textureFormat = attribList[1];
379 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
382 case EGL_TEXTURE_INTERNAL_FORMAT_ANGLE:
383 switch(attribList[1])
390 clientBufferFormat = attribList[1];
393 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
396 case EGL_TEXTURE_TYPE_ANGLE:
397 switch(attribList[1])
399 case GL_UNSIGNED_BYTE:
400 case GL_UNSIGNED_SHORT:
402 clientBufferType = attribList[1];
405 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
408 case EGL_IOSURFACE_PLANE_ANGLE:
409 if(attribList[1] < 0)
411 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
413 ioSurfacePlane = attribList[1];
415 case EGL_TEXTURE_TARGET:
416 switch(attribList[1])
420 case EGL_TEXTURE_RECTANGLE_ANGLE:
421 textureTarget = attribList[1];
424 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
427 case EGL_MIPMAP_TEXTURE:
428 if(attribList[1] != EGL_FALSE)
430 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
433 case EGL_VG_COLORSPACE:
434 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
435 case EGL_VG_ALPHA_FORMAT:
436 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
438 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
445 if(width < 0 || height < 0)
447 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
450 if(width == 0 || height == 0)
452 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
455 if((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
456 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
458 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
461 if(!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
463 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
468 switch(clientBufferType)
470 case GL_UNSIGNED_BYTE:
471 switch(clientBufferFormat)
479 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
481 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
484 case GL_UNSIGNED_SHORT:
485 switch(clientBufferFormat)
493 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
495 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
499 switch(clientBufferFormat)
507 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
509 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
513 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
516 if(ioSurfacePlane < 0)
518 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
521 if(textureFormat != EGL_TEXTURE_RGBA)
523 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
526 if(textureTarget != EGL_TEXTURE_RECTANGLE_ANGLE)
528 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
531 #if defined(__APPLE__)
532 IOSurfaceRef ioSurface = reinterpret_cast<IOSurfaceRef>(clientBuffer);
533 size_t planeCount = IOSurfaceGetPlaneCount(ioSurface);
534 if((static_cast<size_t>(width) > IOSurfaceGetWidthOfPlane(ioSurface, ioSurfacePlane)) ||
535 (static_cast<size_t>(height) > IOSurfaceGetHeightOfPlane(ioSurface, ioSurfacePlane)) ||
536 ((planeCount != 0) && static_cast<size_t>(ioSurfacePlane) >= planeCount))
538 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
544 if((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
545 ((textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE)))
547 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
551 Surface *surface = new PBufferSurface(this, configuration, width, height, textureFormat, textureTarget, clientBufferFormat, clientBufferType, largestPBuffer, clientBuffer, ioSurfacePlane);
553 if(!surface->initialize())
556 return EGL_NO_SURFACE;
560 mSurfaceSet.insert(surface);
562 return success(surface);
565 EGLContext Display::createContext(EGLConfig configHandle, const egl::Context *shareContext, EGLint clientVersion)
567 const egl::Config *config = mConfigSet.get(configHandle);
568 egl::Context *context = nullptr;
570 if(clientVersion == 1 && config->mRenderableType & EGL_OPENGL_ES_BIT)
574 context = libGLES_CM->es1CreateContext(this, shareContext, config);
577 else if((clientVersion == 2 && config->mRenderableType & EGL_OPENGL_ES2_BIT) ||
578 (clientVersion == 3 && config->mRenderableType & EGL_OPENGL_ES3_BIT))
582 context = libGLESv2->es2CreateContext(this, shareContext, clientVersion, config);
587 return error(EGL_BAD_CONFIG, EGL_NO_CONTEXT);
592 return error(EGL_BAD_ALLOC, EGL_NO_CONTEXT);
596 mContextSet.insert(context);
598 return success(context);
601 EGLSyncKHR Display::createSync(Context *context)
603 FenceSync *fenceSync = new egl::FenceSync(context);
604 LockGuard lock(mSyncSetMutex);
605 mSyncSet.insert(fenceSync);
609 void Display::destroySurface(egl::Surface *surface)
612 mSurfaceSet.erase(surface);
614 if(surface == getCurrentDrawSurface())
616 setCurrentDrawSurface(nullptr);
619 if(surface == getCurrentReadSurface())
621 setCurrentReadSurface(nullptr);
625 void Display::destroyContext(egl::Context *context)
628 mContextSet.erase(context);
630 if(context == getCurrentContext())
632 setCurrentContext(nullptr);
633 setCurrentDrawSurface(nullptr);
634 setCurrentReadSurface(nullptr);
638 void Display::destroySync(FenceSync *sync)
641 LockGuard lock(mSyncSetMutex);
642 mSyncSet.erase(sync);
647 bool Display::isInitialized() const
649 return mConfigSet.size() > 0;
652 bool Display::isValidConfig(EGLConfig config)
654 return mConfigSet.get(config) != nullptr;
657 bool Display::isValidContext(egl::Context *context)
659 return mContextSet.find(context) != mContextSet.end();
662 bool Display::isValidSurface(egl::Surface *surface)
664 return mSurfaceSet.find(surface) != mSurfaceSet.end();
667 bool Display::isValidWindow(EGLNativeWindowType window)
670 return IsWindow(window) == TRUE;
671 #elif defined(__ANDROID__)
674 ALOGE("%s called with window==NULL %s:%d", __FUNCTION__, __FILE__, __LINE__);
677 if(static_cast<ANativeWindow*>(window)->common.magic != ANDROID_NATIVE_WINDOW_MAGIC)
679 ALOGE("%s called with window==%p bad magic %s:%d", __FUNCTION__, window, __FILE__, __LINE__);
683 #elif defined(__linux__) || defined(__DragonFly__)
686 XWindowAttributes windowAttributes;
687 Status status = libX11->XGetWindowAttributes((::Display*)nativeDisplay, window, &windowAttributes);
692 #elif defined(__APPLE__)
693 return sw::OSX::IsValidWindow(window);
694 #elif defined(__Fuchsia__)
695 // TODO(crbug.com/800951): Integrate with Mozart.
698 #error "Display::isValidWindow unimplemented for this platform"
703 bool Display::hasExistingWindowSurface(EGLNativeWindowType window)
705 for(const auto &surface : mSurfaceSet)
707 if(surface->isWindowSurface())
709 if(surface->getWindowHandle() == window)
719 bool Display::isValidSync(FenceSync *sync)
721 LockGuard lock(mSyncSetMutex);
722 return mSyncSet.find(sync) != mSyncSet.end();
725 EGLint Display::getMinSwapInterval() const
727 return mMinSwapInterval;
730 EGLint Display::getMaxSwapInterval() const
732 return mMaxSwapInterval;
735 EGLDisplay Display::getEGLDisplay() const
740 void *Display::getNativeDisplay() const
742 return nativeDisplay;
745 EGLImageKHR Display::createSharedImage(Image *image)
747 return reinterpret_cast<EGLImageKHR>((intptr_t)mSharedImageNameSpace.allocate(image));
750 bool Display::destroySharedImage(EGLImageKHR image)
752 GLuint name = (GLuint)reinterpret_cast<intptr_t>(image);
753 Image *eglImage = mSharedImageNameSpace.find(name);
760 eglImage->destroyShared();
761 mSharedImageNameSpace.remove(name);
766 Image *Display::getSharedImage(EGLImageKHR image)
768 GLuint name = (GLuint)reinterpret_cast<intptr_t>(image);
769 return mSharedImageNameSpace.find(name);
772 sw::Format Display::getDisplayFormat() const
775 HDC deviceContext = GetDC(0);
776 unsigned int bpp = ::GetDeviceCaps(deviceContext, BITSPIXEL);
777 ReleaseDC(0, deviceContext);
781 case 32: return sw::FORMAT_X8R8G8B8;
782 case 24: return sw::FORMAT_R8G8B8;
783 case 16: return sw::FORMAT_R5G6B5;
784 default: UNREACHABLE(bpp); // Unexpected display mode color depth
786 #elif defined(__ANDROID__)
787 static const char *const framebuffer[] =
794 for(int i = 0; framebuffer[i]; i++)
796 int fd = open(framebuffer[i], O_RDONLY, 0);
800 struct fb_var_screeninfo info;
801 int io = ioctl(fd, FBIOGET_VSCREENINFO, &info);
806 switch(info.bits_per_pixel)
809 return sw::FORMAT_R5G6B5;
811 if(info.red.length == 8 && info.red.offset == 16 &&
812 info.green.length == 8 && info.green.offset == 8 &&
813 info.blue.length == 8 && info.blue.offset == 0 &&
814 info.transp.length == 0)
816 return sw::FORMAT_X8R8G8B8;
818 if(info.red.length == 8 && info.red.offset == 0 &&
819 info.green.length == 8 && info.green.offset == 8 &&
820 info.blue.length == 8 && info.blue.offset == 16 &&
821 info.transp.length == 0)
823 return sw::FORMAT_X8B8G8R8;
825 if(info.red.length == 8 && info.red.offset == 16 &&
826 info.green.length == 8 && info.green.offset == 8 &&
827 info.blue.length == 8 && info.blue.offset == 0 &&
828 info.transp.length == 8 && info.transp.offset == 24)
830 return sw::FORMAT_A8R8G8B8;
832 if(info.red.length == 8 && info.red.offset == 0 &&
833 info.green.length == 8 && info.green.offset == 8 &&
834 info.blue.length == 8 && info.blue.offset == 16 &&
835 info.transp.length == 8 && info.transp.offset == 24)
837 return sw::FORMAT_A8B8G8R8;
839 else UNIMPLEMENTED();
847 // No framebuffer device found, or we're in user space
848 return sw::FORMAT_X8B8G8R8;
849 #elif defined(__linux__) || defined(__DragonFly__)
852 Screen *screen = libX11->XDefaultScreenOfDisplay((::Display*)nativeDisplay);
853 unsigned int bpp = libX11->XPlanesOfScreen(screen);
857 case 32: return sw::FORMAT_X8R8G8B8;
858 case 24: return sw::FORMAT_R8G8B8;
859 case 16: return sw::FORMAT_R5G6B5;
860 default: UNREACHABLE(bpp); // Unexpected display mode color depth
865 return sw::FORMAT_X8R8G8B8;
867 #elif defined(__APPLE__)
868 return sw::FORMAT_A8B8G8R8;
869 #elif defined(__Fuchsia__)
870 return sw::FORMAT_A8B8G8R8;
872 #error "Display::isValidWindow unimplemented for this platform"
875 return sw::FORMAT_X8R8G8B8;