2 // Copyright (c) 2002-2013 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"
17 #include "common/debug.h"
18 #include "libGLESv2/mathutil.h"
19 #include "libGLESv2/main.h"
20 #include "libGLESv2/Context.h"
21 #include "libGLESv2/renderer/SwapChain.h"
23 #include "libEGL/main.h"
24 #include "libEGL/Surface.h"
30 typedef std::map<EGLNativeDisplayType, Display*> DisplayMap;
34 egl::Display *Display::getDisplay(EGLNativeDisplayType displayId)
36 if (displays.find(displayId) != displays.end())
38 return displays[displayId];
41 // FIXME: Check if displayId is a valid display device context
43 egl::Display *display = new egl::Display(displayId, (HDC)displayId);
45 displays[displayId] = display;
49 Display::Display(EGLNativeDisplayType displayId, HDC deviceContext) : mDc(deviceContext)
51 mDisplayId = displayId;
59 DisplayMap::iterator thisDisplay = displays.find(mDisplayId);
61 if (thisDisplay != displays.end())
63 displays.erase(thisDisplay);
67 bool Display::initialize()
74 mRenderer = glCreateRenderer(this, mDc, mDisplayId);
79 return error(EGL_NOT_INITIALIZED, false);
82 EGLint minSwapInterval = mRenderer->getMinSwapInterval();
83 EGLint maxSwapInterval = mRenderer->getMaxSwapInterval();
84 EGLint maxTextureWidth = mRenderer->getMaxTextureWidth();
85 EGLint maxTextureHeight = mRenderer->getMaxTextureHeight();
87 rx::ConfigDesc *descList;
88 int numConfigs = mRenderer->generateConfigs(&descList);
91 for (int i = 0; i < numConfigs; ++i)
92 configSet.add(descList[i], minSwapInterval, maxSwapInterval,
93 maxTextureWidth, maxTextureHeight);
95 // Give the sorted configs a unique ID and store them internally
97 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
99 Config configuration = *config;
100 configuration.mConfigID = index;
103 mConfigSet.mSet.insert(configuration);
106 mRenderer->deleteConfigs(descList);
109 if (!isInitialized())
115 initExtensionString();
121 void Display::terminate()
123 while (!mSurfaceSet.empty())
125 destroySurface(*mSurfaceSet.begin());
128 while (!mContextSet.empty())
130 destroyContext(*mContextSet.begin());
133 glDestroyRenderer(mRenderer);
137 bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
139 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
142 bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
144 const egl::Config *configuration = mConfigSet.get(config);
148 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
149 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
150 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
151 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
152 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
153 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
154 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
155 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
156 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
157 case EGL_LEVEL: *value = configuration->mLevel; break;
158 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
159 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
160 case EGL_SAMPLES: *value = configuration->mSamples; break;
161 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
162 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
163 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
164 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
165 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
166 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
167 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
168 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
169 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
170 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
171 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
172 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
173 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
174 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
175 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
176 case EGL_CONFORMANT: *value = configuration->mConformant; break;
177 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
178 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
179 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
189 EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
191 const Config *configuration = mConfigSet.get(config);
192 EGLint postSubBufferSupported = EGL_FALSE;
196 EGLint fixedSize = EGL_FALSE;
200 while (*attribList != EGL_NONE)
202 switch (attribList[0])
204 case EGL_RENDER_BUFFER:
205 switch (attribList[1])
207 case EGL_BACK_BUFFER:
209 case EGL_SINGLE_BUFFER:
210 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
212 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
215 case EGL_POST_SUB_BUFFER_SUPPORTED_NV:
216 postSubBufferSupported = attribList[1];
219 width = attribList[1];
222 height = attribList[1];
224 case EGL_FIXED_SIZE_ANGLE:
225 fixedSize = attribList[1];
227 case EGL_VG_COLORSPACE:
228 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
229 case EGL_VG_ALPHA_FORMAT:
230 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
232 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
239 if (width < 0 || height < 0)
241 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
250 if (hasExistingWindowSurface(window))
252 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
255 if (mRenderer->testDeviceLost(false))
257 if (!restoreLostDevice())
258 return EGL_NO_SURFACE;
261 Surface *surface = new Surface(this, configuration, window, fixedSize, width, height, postSubBufferSupported);
263 if (!surface->initialize())
266 return EGL_NO_SURFACE;
269 mSurfaceSet.insert(surface);
271 return success(surface);
274 EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
276 EGLint width = 0, height = 0;
277 EGLenum textureFormat = EGL_NO_TEXTURE;
278 EGLenum textureTarget = EGL_NO_TEXTURE;
279 const Config *configuration = mConfigSet.get(config);
283 while (*attribList != EGL_NONE)
285 switch (attribList[0])
288 width = attribList[1];
291 height = attribList[1];
293 case EGL_LARGEST_PBUFFER:
294 if (attribList[1] != EGL_FALSE)
295 UNIMPLEMENTED(); // FIXME
297 case EGL_TEXTURE_FORMAT:
298 switch (attribList[1])
301 case EGL_TEXTURE_RGB:
302 case EGL_TEXTURE_RGBA:
303 textureFormat = attribList[1];
306 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
309 case EGL_TEXTURE_TARGET:
310 switch (attribList[1])
314 textureTarget = attribList[1];
317 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
320 case EGL_MIPMAP_TEXTURE:
321 if (attribList[1] != EGL_FALSE)
322 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
324 case EGL_VG_COLORSPACE:
325 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
326 case EGL_VG_ALPHA_FORMAT:
327 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
329 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
336 if (width < 0 || height < 0)
338 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
341 if (width == 0 || height == 0)
343 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
346 if (textureFormat != EGL_NO_TEXTURE && !mRenderer->getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
348 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
351 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
352 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
354 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
357 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
359 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
362 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
363 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
365 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
368 if (mRenderer->testDeviceLost(false))
370 if (!restoreLostDevice())
371 return EGL_NO_SURFACE;
374 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
376 if (!surface->initialize())
379 return EGL_NO_SURFACE;
382 mSurfaceSet.insert(surface);
384 return success(surface);
387 EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
393 else if (mRenderer->testDeviceLost(false)) // Lost device
395 if (!restoreLostDevice())
399 gl::Context *context = glCreateContext(shareContext, mRenderer, notifyResets, robustAccess);
400 mContextSet.insert(context);
405 bool Display::restoreLostDevice()
407 for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++)
409 if ((*ctx)->isResetNotificationEnabled())
410 return false; // If reset notifications have been requested, application must delete all contexts first
413 // Release surface resources to make the Reset() succeed
414 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
416 (*surface)->release();
419 if (!mRenderer->resetDevice())
421 return error(EGL_BAD_ALLOC, false);
424 // Restore any surfaces that may have been lost
425 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
427 (*surface)->resetSwapChain();
434 void Display::destroySurface(egl::Surface *surface)
437 mSurfaceSet.erase(surface);
440 void Display::destroyContext(gl::Context *context)
442 glDestroyContext(context);
443 mContextSet.erase(context);
446 void Display::notifyDeviceLost()
448 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
450 (*context)->markContextLost();
452 egl::error(EGL_CONTEXT_LOST);
455 void Display::recreateSwapChains()
457 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
459 (*surface)->getSwapChain()->recreate();
463 bool Display::isInitialized() const
465 return mRenderer != NULL && mConfigSet.size() > 0;
468 bool Display::isValidConfig(EGLConfig config)
470 return mConfigSet.get(config) != NULL;
473 bool Display::isValidContext(gl::Context *context)
475 return mContextSet.find(context) != mContextSet.end();
478 bool Display::isValidSurface(egl::Surface *surface)
480 return mSurfaceSet.find(surface) != mSurfaceSet.end();
483 bool Display::hasExistingWindowSurface(HWND window)
485 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
487 if ((*surface)->getWindowHandle() == window)
496 void Display::initExtensionString()
498 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
499 bool shareHandleSupported = mRenderer->getShareHandleSupport();
501 mExtensionString = "";
503 // Multi-vendor (EXT) extensions
504 mExtensionString += "EGL_EXT_create_context_robustness ";
506 // ANGLE-specific extensions
507 if (shareHandleSupported)
509 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
512 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
514 mExtensionString += "EGL_ANGLE_window_fixed_size ";
518 mExtensionString += "EGL_ANGLE_software_display ";
521 if (shareHandleSupported)
523 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
526 if (mRenderer->getPostSubBufferSupport())
528 mExtensionString += "EGL_NV_post_sub_buffer";
531 std::string::size_type end = mExtensionString.find_last_not_of(' ');
532 if (end != std::string::npos)
534 mExtensionString.resize(end+1);
538 const char *Display::getExtensionString() const
540 return mExtensionString.c_str();
543 void Display::initVendorString()
545 mVendorString = "Google Inc.";
547 LUID adapterLuid = {0};
549 if (mRenderer && mRenderer->getLUID(&adapterLuid))
551 char adapterLuidString[64];
552 sprintf_s(adapterLuidString, sizeof(adapterLuidString), " (adapter LUID: %08x%08x)", adapterLuid.HighPart, adapterLuid.LowPart);
554 mVendorString += adapterLuidString;
558 const char *Display::getVendorString() const
560 return mVendorString.c_str();