michael@0: // michael@0: // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // Display.cpp: Implements the egl::Display class, representing the abstract michael@0: // display on which graphics are drawn. Implements EGLDisplay. michael@0: // [EGL 1.4] section 2.1.2 page 3. michael@0: michael@0: #include "libEGL/Display.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/debug.h" michael@0: #include "libGLESv2/mathutil.h" michael@0: #include "libGLESv2/main.h" michael@0: #include "libGLESv2/Context.h" michael@0: #include "libGLESv2/renderer/SwapChain.h" michael@0: michael@0: #include "libEGL/main.h" michael@0: #include "libEGL/Surface.h" michael@0: michael@0: namespace egl michael@0: { michael@0: namespace michael@0: { michael@0: typedef std::map DisplayMap; michael@0: DisplayMap displays; michael@0: } michael@0: michael@0: egl::Display *Display::getDisplay(EGLNativeDisplayType displayId) michael@0: { michael@0: if (displays.find(displayId) != displays.end()) michael@0: { michael@0: return displays[displayId]; michael@0: } michael@0: michael@0: // FIXME: Check if displayId is a valid display device context michael@0: michael@0: egl::Display *display = new egl::Display(displayId, (HDC)displayId); michael@0: michael@0: displays[displayId] = display; michael@0: return display; michael@0: } michael@0: michael@0: Display::Display(EGLNativeDisplayType displayId, HDC deviceContext) : mDc(deviceContext) michael@0: { michael@0: mDisplayId = displayId; michael@0: mRenderer = NULL; michael@0: } michael@0: michael@0: Display::~Display() michael@0: { michael@0: terminate(); michael@0: michael@0: DisplayMap::iterator thisDisplay = displays.find(mDisplayId); michael@0: michael@0: if (thisDisplay != displays.end()) michael@0: { michael@0: displays.erase(thisDisplay); michael@0: } michael@0: } michael@0: michael@0: bool Display::initialize() michael@0: { michael@0: if (isInitialized()) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: mRenderer = glCreateRenderer(this, mDc, mDisplayId); michael@0: michael@0: if (!mRenderer) michael@0: { michael@0: terminate(); michael@0: return error(EGL_NOT_INITIALIZED, false); michael@0: } michael@0: michael@0: EGLint minSwapInterval = mRenderer->getMinSwapInterval(); michael@0: EGLint maxSwapInterval = mRenderer->getMaxSwapInterval(); michael@0: EGLint maxTextureWidth = mRenderer->getMaxTextureWidth(); michael@0: EGLint maxTextureHeight = mRenderer->getMaxTextureHeight(); michael@0: michael@0: rx::ConfigDesc *descList; michael@0: int numConfigs = mRenderer->generateConfigs(&descList); michael@0: ConfigSet configSet; michael@0: michael@0: for (int i = 0; i < numConfigs; ++i) michael@0: configSet.add(descList[i], minSwapInterval, maxSwapInterval, michael@0: maxTextureWidth, maxTextureHeight); michael@0: michael@0: // Give the sorted configs a unique ID and store them internally michael@0: EGLint index = 1; michael@0: for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) michael@0: { michael@0: Config configuration = *config; michael@0: configuration.mConfigID = index; michael@0: index++; michael@0: michael@0: mConfigSet.mSet.insert(configuration); michael@0: } michael@0: michael@0: mRenderer->deleteConfigs(descList); michael@0: descList = NULL; michael@0: michael@0: if (!isInitialized()) michael@0: { michael@0: terminate(); michael@0: return false; michael@0: } michael@0: michael@0: initExtensionString(); michael@0: initVendorString(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void Display::terminate() michael@0: { michael@0: while (!mSurfaceSet.empty()) michael@0: { michael@0: destroySurface(*mSurfaceSet.begin()); michael@0: } michael@0: michael@0: while (!mContextSet.empty()) michael@0: { michael@0: destroyContext(*mContextSet.begin()); michael@0: } michael@0: michael@0: glDestroyRenderer(mRenderer); michael@0: mRenderer = NULL; michael@0: } michael@0: michael@0: bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) michael@0: { michael@0: return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); michael@0: } michael@0: michael@0: bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) michael@0: { michael@0: const egl::Config *configuration = mConfigSet.get(config); michael@0: michael@0: switch (attribute) michael@0: { michael@0: case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; michael@0: case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; michael@0: case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; michael@0: case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; michael@0: case EGL_RED_SIZE: *value = configuration->mRedSize; break; michael@0: case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; michael@0: case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; michael@0: case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; michael@0: case EGL_CONFIG_ID: *value = configuration->mConfigID; break; michael@0: case EGL_LEVEL: *value = configuration->mLevel; break; michael@0: case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; michael@0: case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; michael@0: case EGL_SAMPLES: *value = configuration->mSamples; break; michael@0: case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; michael@0: case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; michael@0: case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; michael@0: case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; michael@0: case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; michael@0: case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; michael@0: case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; michael@0: case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; michael@0: case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; michael@0: case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; michael@0: case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; michael@0: case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; michael@0: case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; michael@0: case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; michael@0: case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; michael@0: case EGL_CONFORMANT: *value = configuration->mConformant; break; michael@0: case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break; michael@0: case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break; michael@0: case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: michael@0: EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList) michael@0: { michael@0: const Config *configuration = mConfigSet.get(config); michael@0: EGLint postSubBufferSupported = EGL_FALSE; michael@0: michael@0: if (attribList) michael@0: { michael@0: while (*attribList != EGL_NONE) michael@0: { michael@0: switch (attribList[0]) michael@0: { michael@0: case EGL_RENDER_BUFFER: michael@0: switch (attribList[1]) michael@0: { michael@0: case EGL_BACK_BUFFER: michael@0: break; michael@0: case EGL_SINGLE_BUFFER: michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported michael@0: default: michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: break; michael@0: case EGL_POST_SUB_BUFFER_SUPPORTED_NV: michael@0: postSubBufferSupported = attribList[1]; michael@0: break; michael@0: case EGL_VG_COLORSPACE: michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: case EGL_VG_ALPHA_FORMAT: michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: default: michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: attribList += 2; michael@0: } michael@0: } michael@0: michael@0: if (hasExistingWindowSurface(window)) michael@0: { michael@0: return error(EGL_BAD_ALLOC, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if (mRenderer->testDeviceLost(false)) michael@0: { michael@0: if (!restoreLostDevice()) michael@0: return EGL_NO_SURFACE; michael@0: } michael@0: michael@0: Surface *surface = new Surface(this, configuration, window, postSubBufferSupported); michael@0: michael@0: if (!surface->initialize()) michael@0: { michael@0: delete surface; michael@0: return EGL_NO_SURFACE; michael@0: } michael@0: michael@0: mSurfaceSet.insert(surface); michael@0: michael@0: return success(surface); michael@0: } michael@0: michael@0: EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList) michael@0: { michael@0: EGLint width = 0, height = 0; michael@0: EGLenum textureFormat = EGL_NO_TEXTURE; michael@0: EGLenum textureTarget = EGL_NO_TEXTURE; michael@0: const Config *configuration = mConfigSet.get(config); michael@0: michael@0: if (attribList) michael@0: { michael@0: while (*attribList != EGL_NONE) michael@0: { michael@0: switch (attribList[0]) michael@0: { michael@0: case EGL_WIDTH: michael@0: width = attribList[1]; michael@0: break; michael@0: case EGL_HEIGHT: michael@0: height = attribList[1]; michael@0: break; michael@0: case EGL_LARGEST_PBUFFER: michael@0: if (attribList[1] != EGL_FALSE) michael@0: UNIMPLEMENTED(); // FIXME michael@0: break; michael@0: case EGL_TEXTURE_FORMAT: michael@0: switch (attribList[1]) michael@0: { michael@0: case EGL_NO_TEXTURE: michael@0: case EGL_TEXTURE_RGB: michael@0: case EGL_TEXTURE_RGBA: michael@0: textureFormat = attribList[1]; michael@0: break; michael@0: default: michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: break; michael@0: case EGL_TEXTURE_TARGET: michael@0: switch (attribList[1]) michael@0: { michael@0: case EGL_NO_TEXTURE: michael@0: case EGL_TEXTURE_2D: michael@0: textureTarget = attribList[1]; michael@0: break; michael@0: default: michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: break; michael@0: case EGL_MIPMAP_TEXTURE: michael@0: if (attribList[1] != EGL_FALSE) michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: break; michael@0: case EGL_VG_COLORSPACE: michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: case EGL_VG_ALPHA_FORMAT: michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: default: michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: attribList += 2; michael@0: } michael@0: } michael@0: michael@0: if (width < 0 || height < 0) michael@0: { michael@0: return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if (width == 0 || height == 0) michael@0: { michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if (textureFormat != EGL_NO_TEXTURE && !mRenderer->getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height))) michael@0: { michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) || michael@0: (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE)) michael@0: { michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT)) michael@0: { michael@0: return error(EGL_BAD_MATCH, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) || michael@0: (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE)) michael@0: { michael@0: return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); michael@0: } michael@0: michael@0: if (mRenderer->testDeviceLost(false)) michael@0: { michael@0: if (!restoreLostDevice()) michael@0: return EGL_NO_SURFACE; michael@0: } michael@0: michael@0: Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget); michael@0: michael@0: if (!surface->initialize()) michael@0: { michael@0: delete surface; michael@0: return EGL_NO_SURFACE; michael@0: } michael@0: michael@0: mSurfaceSet.insert(surface); michael@0: michael@0: return success(surface); michael@0: } michael@0: michael@0: EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess) michael@0: { michael@0: if (!mRenderer) michael@0: { michael@0: return NULL; michael@0: } michael@0: else if (mRenderer->testDeviceLost(false)) // Lost device michael@0: { michael@0: if (!restoreLostDevice()) michael@0: return NULL; michael@0: } michael@0: michael@0: gl::Context *context = glCreateContext(shareContext, mRenderer, notifyResets, robustAccess); michael@0: mContextSet.insert(context); michael@0: michael@0: return context; michael@0: } michael@0: michael@0: bool Display::restoreLostDevice() michael@0: { michael@0: for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++) michael@0: { michael@0: if ((*ctx)->isResetNotificationEnabled()) michael@0: return false; // If reset notifications have been requested, application must delete all contexts first michael@0: } michael@0: michael@0: // Release surface resources to make the Reset() succeed michael@0: for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) michael@0: { michael@0: (*surface)->release(); michael@0: } michael@0: michael@0: if (!mRenderer->resetDevice()) michael@0: { michael@0: return error(EGL_BAD_ALLOC, false); michael@0: } michael@0: michael@0: // Restore any surfaces that may have been lost michael@0: for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) michael@0: { michael@0: (*surface)->resetSwapChain(); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: void Display::destroySurface(egl::Surface *surface) michael@0: { michael@0: delete surface; michael@0: mSurfaceSet.erase(surface); michael@0: } michael@0: michael@0: void Display::destroyContext(gl::Context *context) michael@0: { michael@0: glDestroyContext(context); michael@0: mContextSet.erase(context); michael@0: } michael@0: michael@0: void Display::notifyDeviceLost() michael@0: { michael@0: for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) michael@0: { michael@0: (*context)->markContextLost(); michael@0: } michael@0: egl::error(EGL_CONTEXT_LOST); michael@0: } michael@0: michael@0: void Display::recreateSwapChains() michael@0: { michael@0: for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) michael@0: { michael@0: (*surface)->getSwapChain()->recreate(); michael@0: } michael@0: } michael@0: michael@0: bool Display::isInitialized() const michael@0: { michael@0: return mRenderer != NULL && mConfigSet.size() > 0; michael@0: } michael@0: michael@0: bool Display::isValidConfig(EGLConfig config) michael@0: { michael@0: return mConfigSet.get(config) != NULL; michael@0: } michael@0: michael@0: bool Display::isValidContext(gl::Context *context) michael@0: { michael@0: return mContextSet.find(context) != mContextSet.end(); michael@0: } michael@0: michael@0: bool Display::isValidSurface(egl::Surface *surface) michael@0: { michael@0: return mSurfaceSet.find(surface) != mSurfaceSet.end(); michael@0: } michael@0: michael@0: bool Display::hasExistingWindowSurface(HWND window) michael@0: { michael@0: for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) michael@0: { michael@0: if ((*surface)->getWindowHandle() == window) michael@0: { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void Display::initExtensionString() michael@0: { michael@0: HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); michael@0: bool shareHandleSupported = mRenderer->getShareHandleSupport(); michael@0: michael@0: mExtensionString = ""; michael@0: michael@0: // Multi-vendor (EXT) extensions michael@0: mExtensionString += "EGL_EXT_create_context_robustness "; michael@0: michael@0: // ANGLE-specific extensions michael@0: if (shareHandleSupported) michael@0: { michael@0: mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer "; michael@0: } michael@0: michael@0: mExtensionString += "EGL_ANGLE_query_surface_pointer "; michael@0: michael@0: if (swiftShader) michael@0: { michael@0: mExtensionString += "EGL_ANGLE_software_display "; michael@0: } michael@0: michael@0: if (shareHandleSupported) michael@0: { michael@0: mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle "; michael@0: } michael@0: michael@0: if (mRenderer->getPostSubBufferSupport()) michael@0: { michael@0: mExtensionString += "EGL_NV_post_sub_buffer"; michael@0: } michael@0: michael@0: std::string::size_type end = mExtensionString.find_last_not_of(' '); michael@0: if (end != std::string::npos) michael@0: { michael@0: mExtensionString.resize(end+1); michael@0: } michael@0: } michael@0: michael@0: const char *Display::getExtensionString() const michael@0: { michael@0: return mExtensionString.c_str(); michael@0: } michael@0: michael@0: void Display::initVendorString() michael@0: { michael@0: mVendorString = "Google Inc."; michael@0: michael@0: LUID adapterLuid = {0}; michael@0: michael@0: if (mRenderer && mRenderer->getLUID(&adapterLuid)) michael@0: { michael@0: char adapterLuidString[64]; michael@0: sprintf_s(adapterLuidString, sizeof(adapterLuidString), " (adapter LUID: %08x%08x)", adapterLuid.HighPart, adapterLuid.LowPart); michael@0: michael@0: mVendorString += adapterLuidString; michael@0: } michael@0: } michael@0: michael@0: const char *Display::getVendorString() const michael@0: { michael@0: return mVendorString.c_str(); michael@0: } michael@0: michael@0: }