2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
7 // Display.cpp: Implements the egl::Display class, representing the abstract
8 // display on which graphics are drawn. Implements EGLDisplay.
9 // [EGL 1.4] section 2.1.2 page 3.
11 #include "libEGL/Display.h"
18 #include "common/debug.h"
19 #include "common/mathutil.h"
20 #include "libGLESv2/main.h"
21 #include "libGLESv2/Context.h"
22 #include "libGLESv2/renderer/SwapChain.h"
24 #include "libEGL/main.h"
25 #include "libEGL/Surface.h"
30 typedef std::map<EGLNativeDisplayType, Display*> DisplayMap;
31 static DisplayMap *GetDisplayMap()
33 static DisplayMap displays;
37 egl::Display *Display::getDisplay(EGLNativeDisplayType displayId, EGLint displayType)
39 DisplayMap *displays = GetDisplayMap();
40 DisplayMap::const_iterator iter = displays->find(displayId);
41 if (iter != displays->end())
46 // FIXME: Check if displayId is a valid display device context
48 egl::Display *display = new egl::Display(displayId, displayType);
49 displays->insert(std::make_pair(displayId, display));
54 Display::Display(EGLNativeDisplayType displayId, EGLint displayType)
55 : mDisplayId(displayId),
56 mRequestedDisplayType(displayType),
65 DisplayMap *displays = GetDisplayMap();
66 DisplayMap::iterator iter = displays->find(mDisplayId);
67 if (iter != displays->end())
69 displays->erase(iter);
73 bool Display::initialize()
80 mRenderer = glCreateRenderer(this, mDisplayId, mRequestedDisplayType);
85 return error(EGL_NOT_INITIALIZED, false);
88 EGLint minSwapInterval = mRenderer->getMinSwapInterval();
89 EGLint maxSwapInterval = mRenderer->getMaxSwapInterval();
90 EGLint maxTextureSize = mRenderer->getRendererCaps().max2DTextureSize;
92 rx::ConfigDesc *descList;
93 int numConfigs = mRenderer->generateConfigs(&descList);
96 for (int i = 0; i < numConfigs; ++i)
98 configSet.add(descList[i], minSwapInterval, maxSwapInterval, maxTextureSize, maxTextureSize);
101 // Give the sorted configs a unique ID and store them internally
103 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
105 Config configuration = *config;
106 configuration.mConfigID = index;
109 mConfigSet.mSet.insert(configuration);
112 mRenderer->deleteConfigs(descList);
115 if (!isInitialized())
121 initDisplayExtensionString();
127 void Display::terminate()
129 while (!mSurfaceSet.empty())
131 destroySurface(*mSurfaceSet.begin());
134 while (!mContextSet.empty())
136 destroyContext(*mContextSet.begin());
139 glDestroyRenderer(mRenderer);
143 bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
145 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
148 bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
150 const egl::Config *configuration = mConfigSet.get(config);
154 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
155 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
156 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
157 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
158 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
159 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
160 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
161 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
162 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
163 case EGL_LEVEL: *value = configuration->mLevel; break;
164 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
165 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
166 case EGL_SAMPLES: *value = configuration->mSamples; break;
167 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
168 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
169 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
170 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
171 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
172 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
173 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
174 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
175 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
176 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
177 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
178 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
179 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
180 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
181 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
182 case EGL_CONFORMANT: *value = configuration->mConformant; break;
183 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
184 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
185 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
195 EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
197 const Config *configuration = mConfigSet.get(config);
198 EGLint postSubBufferSupported = EGL_FALSE;
202 EGLint fixedSize = EGL_FALSE;
206 while (*attribList != EGL_NONE)
208 switch (attribList[0])
210 case EGL_RENDER_BUFFER:
211 switch (attribList[1])
213 case EGL_BACK_BUFFER:
215 case EGL_SINGLE_BUFFER:
216 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
218 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
221 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
222 postSubBufferSupported = attribList[1];
225 width = attribList[1];
228 height = attribList[1];
230 case EGL_FIXED_SIZE_ANGLE:
231 fixedSize = attribList[1];
233 case EGL_VG_COLORSPACE:
234 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
235 case EGL_VG_ALPHA_FORMAT:
236 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
238 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
245 if (width < 0 || height < 0)
247 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
256 if (hasExistingWindowSurface(window))
258 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
261 if (mRenderer->testDeviceLost(false))
263 if (!restoreLostDevice())
264 return EGL_NO_SURFACE;
267 Surface *surface = new Surface(this, configuration, window, fixedSize, width, height, postSubBufferSupported);
269 if (!surface->initialize())
272 return EGL_NO_SURFACE;
275 mSurfaceSet.insert(surface);
277 return success(surface);
280 EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
282 EGLint width = 0, height = 0;
283 EGLenum textureFormat = EGL_NO_TEXTURE;
284 EGLenum textureTarget = EGL_NO_TEXTURE;
285 const Config *configuration = mConfigSet.get(config);
289 while (*attribList != EGL_NONE)
291 switch (attribList[0])
294 width = attribList[1];
297 height = attribList[1];
299 case EGL_LARGEST_PBUFFER:
300 if (attribList[1] != EGL_FALSE)
301 UNIMPLEMENTED(); // FIXME
303 case EGL_TEXTURE_FORMAT:
304 switch (attribList[1])
307 case EGL_TEXTURE_RGB:
308 case EGL_TEXTURE_RGBA:
309 textureFormat = attribList[1];
312 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
315 case EGL_TEXTURE_TARGET:
316 switch (attribList[1])
320 textureTarget = attribList[1];
323 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
326 case EGL_MIPMAP_TEXTURE:
327 if (attribList[1] != EGL_FALSE)
328 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
330 case EGL_VG_COLORSPACE:
331 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
332 case EGL_VG_ALPHA_FORMAT:
333 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
335 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
342 if (width < 0 || height < 0)
344 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
347 if (width == 0 || height == 0)
349 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
352 if (textureFormat != EGL_NO_TEXTURE && !mRenderer->getRendererExtensions().textureNPOT && (!gl::isPow2(width) || !gl::isPow2(height)))
354 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
357 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
358 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
360 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
363 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
365 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
368 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
369 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
371 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
374 if (mRenderer->testDeviceLost(false))
376 if (!restoreLostDevice())
377 return EGL_NO_SURFACE;
380 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
382 if (!surface->initialize())
385 return EGL_NO_SURFACE;
388 mSurfaceSet.insert(surface);
390 return success(surface);
393 EGLContext Display::createContext(EGLConfig configHandle, EGLint clientVersion, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
397 return EGL_NO_CONTEXT;
399 else if (mRenderer->testDeviceLost(false)) // Lost device
401 if (!restoreLostDevice())
403 return error(EGL_CONTEXT_LOST, EGL_NO_CONTEXT);
407 if (clientVersion > 2 && mRenderer->getMajorShaderModel() < 4)
409 return error(EGL_BAD_CONFIG, EGL_NO_CONTEXT);
412 gl::Context *context = glCreateContext(clientVersion, shareContext, mRenderer, notifyResets, robustAccess);
413 mContextSet.insert(context);
415 return success(context);
418 bool Display::restoreLostDevice()
420 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
422 if ((*ctx)->isResetNotificationEnabled())
423 return false; // If reset notifications have been requested, application must delete all contexts first
426 // Release surface resources to make the Reset() succeed
427 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
429 (*surface)->release();
432 if (!mRenderer->resetDevice())
434 return error(EGL_BAD_ALLOC, false);
437 // Restore any surfaces that may have been lost
438 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
440 (*surface)->resetSwapChain();
447 void Display::destroySurface(egl::Surface *surface)
450 mSurfaceSet.erase(surface);
453 void Display::destroyContext(gl::Context *context)
455 glDestroyContext(context);
456 mContextSet.erase(context);
459 void Display::notifyDeviceLost()
461 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
463 (*context)->markContextLost();
465 egl::error(EGL_CONTEXT_LOST);
468 void Display::recreateSwapChains()
470 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
472 (*surface)->getSwapChain()->recreate();
476 bool Display::isInitialized() const
478 return mRenderer != NULL && mConfigSet.size() > 0;
481 bool Display::isValidConfig(EGLConfig config)
483 return mConfigSet.get(config) != NULL;
486 bool Display::isValidContext(gl::Context *context)
488 return mContextSet.find(context) != mContextSet.end();
491 bool Display::isValidSurface(egl::Surface *surface)
493 return mSurfaceSet.find(surface) != mSurfaceSet.end();
496 bool Display::hasExistingWindowSurface(HWND window)
498 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
500 if ((*surface)->getWindowHandle() == window)
509 std::string Display::generateClientExtensionString()
511 std::vector<std::string> extensions;
513 extensions.push_back("EGL_EXT_client_extensions");
515 extensions.push_back("ANGLE_platform_angle");
517 if (supportsPlatformD3D())
519 extensions.push_back("ANGLE_platform_angle_d3d");
522 if (supportsPlatformOpenGL())
524 extensions.push_back("ANGLE_platform_angle_opengl");
527 std::ostringstream stream;
528 std::copy(extensions.begin(), extensions.end(), std::ostream_iterator<std::string>(stream, " "));
532 void Display::initDisplayExtensionString()
534 std::vector<std::string> extensions;
536 // Multi-vendor (EXT) extensions
537 extensions.push_back("EGL_EXT_create_context_robustness");
539 // ANGLE-specific extensions
540 if (mRenderer->getShareHandleSupport())
542 extensions.push_back("EGL_ANGLE_d3d_share_handle_client_buffer");
543 extensions.push_back("EGL_ANGLE_surface_d3d_texture_2d_share_handle");
546 extensions.push_back("EGL_ANGLE_query_surface_pointer");
547 extensions.push_back("EGL_ANGLE_window_fixed_size");
549 if (mRenderer->getPostSubBufferSupport())
551 extensions.push_back("EGL_NV_post_sub_buffer");
554 // TODO: complete support for the EGL_KHR_create_context extension
555 extensions.push_back("EGL_KHR_create_context");
557 std::ostringstream stream;
558 std::copy(extensions.begin(), extensions.end(), std::ostream_iterator<std::string>(stream, " "));
559 mDisplayExtensionString = stream.str();
562 const char *Display::getExtensionString(egl::Display *display)
564 if (display != EGL_NO_DISPLAY)
566 return display->mDisplayExtensionString.c_str();
570 static std::string clientExtensions = generateClientExtensionString();
571 return clientExtensions.c_str();
575 bool Display::supportsPlatformD3D()
577 #if defined(ANGLE_ENABLE_D3D9) || defined(ANGLE_ENABLE_D3D11)
584 bool Display::supportsPlatformOpenGL()
589 void Display::initVendorString()
591 mVendorString = "Google Inc.";
593 LUID adapterLuid = {0};
595 if (mRenderer && mRenderer->getLUID(&adapterLuid))
597 char adapterLuidString[64];
598 sprintf_s(adapterLuidString, sizeof(adapterLuidString), " (adapter LUID: %08x%08x)", adapterLuid.HighPart, adapterLuid.LowPart);
600 mVendorString += adapterLuidString;
604 const char *Display::getVendorString() const
606 return mVendorString.c_str();