daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 1 | // |
daniel@transgaming.com | c6f7f9d | 2012-01-27 15:40:00 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 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. |
| 10 | |
| 11 | #include "libEGL/Display.h" |
| 12 | |
| 13 | #include <algorithm> |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 14 | #include <map> |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
| 17 | #include "common/debug.h" |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 18 | #include "libGLESv2/mathutil.h" |
apatrick@chromium.org | a5ddde9 | 2012-01-10 23:00:07 +0000 | [diff] [blame] | 19 | #include "libGLESv2/utilities.h" |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 20 | |
| 21 | #include "libEGL/main.h" |
| 22 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 23 | |
| 24 | namespace egl |
| 25 | { |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 26 | namespace |
| 27 | { |
| 28 | typedef std::map<EGLNativeDisplayType, Display*> DisplayMap; |
| 29 | DisplayMap displays; |
| 30 | } |
| 31 | |
| 32 | egl::Display *Display::getDisplay(EGLNativeDisplayType displayId) |
| 33 | { |
| 34 | if (displays.find(displayId) != displays.end()) |
| 35 | { |
| 36 | return displays[displayId]; |
| 37 | } |
| 38 | |
| 39 | egl::Display *display = NULL; |
| 40 | |
| 41 | if (displayId == EGL_DEFAULT_DISPLAY) |
| 42 | { |
| 43 | display = new egl::Display(displayId, (HDC)NULL, false); |
| 44 | } |
| 45 | else if (displayId == EGL_SOFTWARE_DISPLAY_ANGLE) |
| 46 | { |
| 47 | display = new egl::Display(displayId, (HDC)NULL, true); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | // FIXME: Check if displayId is a valid display device context |
| 52 | |
| 53 | display = new egl::Display(displayId, (HDC)displayId, false); |
| 54 | } |
| 55 | |
| 56 | displays[displayId] = display; |
| 57 | return display; |
| 58 | } |
| 59 | |
| 60 | Display::Display(EGLNativeDisplayType displayId, HDC deviceContext, bool software) : mDc(deviceContext) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 61 | { |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 62 | |
| 63 | mMinSwapInterval = 1; |
| 64 | mMaxSwapInterval = 1; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 65 | mSoftwareDevice = software; |
| 66 | mDisplayId = displayId; |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 67 | mRenderer = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | Display::~Display() |
| 71 | { |
| 72 | terminate(); |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 73 | |
| 74 | DisplayMap::iterator thisDisplay = displays.find(mDisplayId); |
| 75 | |
| 76 | if (thisDisplay != displays.end()) |
| 77 | { |
| 78 | displays.erase(thisDisplay); |
| 79 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool Display::initialize() |
| 83 | { |
| 84 | if (isInitialized()) |
| 85 | { |
| 86 | return true; |
| 87 | } |
| 88 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 89 | HMODULE hModule = NULL; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 90 | if (mSoftwareDevice) |
| 91 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 92 | hModule = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 96 | hModule = GetModuleHandle(TEXT("d3d9.dll")); |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 97 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 98 | if (hModule == NULL) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 99 | { |
| 100 | terminate(); |
| 101 | return false; |
| 102 | } |
| 103 | |
daniel@transgaming.com | ef21ab2 | 2012-10-31 17:52:47 +0000 | [diff] [blame^] | 104 | mRenderer = glCreateRenderer(this, hModule, mDc); |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 105 | EGLint status = EGL_BAD_ALLOC; |
| 106 | if (mRenderer) |
| 107 | status = mRenderer->initialize(); |
| 108 | if (status != EGL_SUCCESS) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 109 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 110 | terminate(); |
| 111 | return error(status, false); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 112 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 113 | D3DCAPS9 deviceCaps = mRenderer->getDeviceCaps(); // D3D9_REPLACE |
| 114 | IDirect3D9 *d3d9 = mRenderer->getD3D(); // D3D9_REPLACE |
| 115 | UINT adapter = mRenderer->getAdapter(); // D3D9_REPLACE |
| 116 | D3DDEVTYPE deviceType = mRenderer->getDeviceType(); // D3D9_REPLACE |
| 117 | IDirect3DDevice9 *device = mRenderer->getDevice(); // D3D9_REPLACE |
| 118 | |
| 119 | mMinSwapInterval = 4; |
| 120 | mMaxSwapInterval = 0; |
| 121 | |
| 122 | // START D3D9_REPLACE |
| 123 | if (deviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} |
| 124 | if (deviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} |
| 125 | if (deviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} |
| 126 | if (deviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} |
| 127 | if (deviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} |
| 128 | |
| 129 | const D3DFORMAT renderTargetFormats[] = |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 130 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 131 | D3DFMT_A1R5G5B5, |
| 132 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
| 133 | D3DFMT_A8R8G8B8, |
| 134 | D3DFMT_R5G6B5, |
| 135 | // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format |
| 136 | D3DFMT_X8R8G8B8 |
| 137 | }; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 138 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 139 | const D3DFORMAT depthStencilFormats[] = |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 140 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 141 | D3DFMT_UNKNOWN, |
| 142 | // D3DFMT_D16_LOCKABLE, |
| 143 | D3DFMT_D32, |
| 144 | // D3DFMT_D15S1, |
| 145 | D3DFMT_D24S8, |
| 146 | D3DFMT_D24X8, |
| 147 | // D3DFMT_D24X4S4, |
| 148 | D3DFMT_D16, |
| 149 | // D3DFMT_D32F_LOCKABLE, |
| 150 | // D3DFMT_D24FS8 |
| 151 | }; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 152 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 153 | D3DDISPLAYMODE currentDisplayMode; |
| 154 | d3d9->GetAdapterDisplayMode(adapter, ¤tDisplayMode); |
| 155 | |
| 156 | ConfigSet configSet; |
| 157 | |
| 158 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
| 159 | { |
| 160 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
| 161 | |
| 162 | HRESULT result = d3d9->CheckDeviceFormat(adapter, deviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 163 | |
| 164 | if (SUCCEEDED(result)) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 165 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 166 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 167 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 168 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
| 169 | HRESULT result = D3D_OK; |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 170 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 171 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 172 | { |
| 173 | result = d3d9->CheckDeviceFormat(adapter, deviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 174 | } |
| 175 | |
| 176 | if (SUCCEEDED(result)) |
| 177 | { |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 178 | if(depthStencilFormat != D3DFMT_UNKNOWN) |
| 179 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 180 | result = d3d9->CheckDepthStencilMatch(adapter, deviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
daniel@transgaming.com | a114c27 | 2011-04-22 04:18:50 +0000 | [diff] [blame] | 181 | } |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 182 | |
| 183 | if (SUCCEEDED(result)) |
| 184 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 185 | // FIXME: enumerate multi-sampling |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 186 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 187 | configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0, |
| 188 | deviceCaps.MaxTextureWidth, deviceCaps.MaxTextureHeight); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 193 | } |
| 194 | // END D3D9_REPLACE |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 195 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 196 | // Give the sorted configs a unique ID and store them internally |
| 197 | EGLint index = 1; |
| 198 | for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
| 199 | { |
| 200 | Config configuration = *config; |
| 201 | configuration.mConfigID = index; |
| 202 | index++; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 203 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 204 | mConfigSet.mSet.insert(configuration); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (!isInitialized()) |
| 208 | { |
| 209 | terminate(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 213 | initExtensionString(); |
| 214 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 215 | mVertexShaderCache.initialize(device); |
| 216 | mPixelShaderCache.initialize(device); |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 217 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
| 221 | void Display::terminate() |
| 222 | { |
| 223 | while (!mSurfaceSet.empty()) |
| 224 | { |
| 225 | destroySurface(*mSurfaceSet.begin()); |
| 226 | } |
| 227 | |
| 228 | while (!mContextSet.empty()) |
| 229 | { |
| 230 | destroyContext(*mContextSet.begin()); |
| 231 | } |
| 232 | |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 233 | mVertexShaderCache.clear(); |
| 234 | mPixelShaderCache.clear(); |
| 235 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 236 | glDestroyRenderer(mRenderer); |
| 237 | mRenderer = NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 241 | { |
| 242 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 243 | } |
| 244 | |
| 245 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 246 | { |
| 247 | const egl::Config *configuration = mConfigSet.get(config); |
| 248 | |
| 249 | switch (attribute) |
| 250 | { |
| 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_TYPE: *value = configuration->mNativeVisualType; break; |
| 263 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 264 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 265 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 266 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 267 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 268 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 269 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 270 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 271 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 272 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 273 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 274 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 275 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 276 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 277 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
| 278 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
| 279 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 280 | case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break; |
| 281 | case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break; |
| 282 | case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 283 | default: |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | return true; |
| 288 | } |
| 289 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 290 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 291 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 292 | EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 293 | { |
| 294 | const Config *configuration = mConfigSet.get(config); |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 295 | EGLint postSubBufferSupported = EGL_FALSE; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 296 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 297 | if (attribList) |
| 298 | { |
| 299 | while (*attribList != EGL_NONE) |
| 300 | { |
| 301 | switch (attribList[0]) |
| 302 | { |
| 303 | case EGL_RENDER_BUFFER: |
| 304 | switch (attribList[1]) |
| 305 | { |
| 306 | case EGL_BACK_BUFFER: |
| 307 | break; |
| 308 | case EGL_SINGLE_BUFFER: |
| 309 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported |
| 310 | default: |
| 311 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 312 | } |
| 313 | break; |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 314 | case EGL_POST_SUB_BUFFER_SUPPORTED_NV: |
| 315 | postSubBufferSupported = attribList[1]; |
| 316 | break; |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 317 | case EGL_VG_COLORSPACE: |
| 318 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 319 | case EGL_VG_ALPHA_FORMAT: |
| 320 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 321 | default: |
| 322 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 323 | } |
| 324 | |
| 325 | attribList += 2; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (hasExistingWindowSurface(window)) |
| 330 | { |
| 331 | return error(EGL_BAD_ALLOC, EGL_NO_SURFACE); |
| 332 | } |
| 333 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 334 | if (mRenderer->testDeviceLost()) |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 335 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 336 | if (!restoreLostDevice()) |
| 337 | return EGL_NO_SURFACE; |
| 338 | } |
| 339 | |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 340 | Surface *surface = new Surface(this, configuration, window, postSubBufferSupported); |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 341 | |
| 342 | if (!surface->initialize()) |
| 343 | { |
| 344 | delete surface; |
| 345 | return EGL_NO_SURFACE; |
| 346 | } |
| 347 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 348 | mSurfaceSet.insert(surface); |
| 349 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 350 | return success(surface); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 351 | } |
| 352 | |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 353 | EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList) |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 354 | { |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 355 | EGLint width = 0, height = 0; |
| 356 | EGLenum textureFormat = EGL_NO_TEXTURE; |
| 357 | EGLenum textureTarget = EGL_NO_TEXTURE; |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 358 | const Config *configuration = mConfigSet.get(config); |
| 359 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 360 | if (attribList) |
| 361 | { |
| 362 | while (*attribList != EGL_NONE) |
| 363 | { |
| 364 | switch (attribList[0]) |
| 365 | { |
| 366 | case EGL_WIDTH: |
| 367 | width = attribList[1]; |
| 368 | break; |
| 369 | case EGL_HEIGHT: |
| 370 | height = attribList[1]; |
| 371 | break; |
| 372 | case EGL_LARGEST_PBUFFER: |
| 373 | if (attribList[1] != EGL_FALSE) |
| 374 | UNIMPLEMENTED(); // FIXME |
| 375 | break; |
| 376 | case EGL_TEXTURE_FORMAT: |
| 377 | switch (attribList[1]) |
| 378 | { |
| 379 | case EGL_NO_TEXTURE: |
| 380 | case EGL_TEXTURE_RGB: |
| 381 | case EGL_TEXTURE_RGBA: |
| 382 | textureFormat = attribList[1]; |
| 383 | break; |
| 384 | default: |
| 385 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 386 | } |
| 387 | break; |
| 388 | case EGL_TEXTURE_TARGET: |
| 389 | switch (attribList[1]) |
| 390 | { |
| 391 | case EGL_NO_TEXTURE: |
| 392 | case EGL_TEXTURE_2D: |
| 393 | textureTarget = attribList[1]; |
| 394 | break; |
| 395 | default: |
| 396 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 397 | } |
| 398 | break; |
| 399 | case EGL_MIPMAP_TEXTURE: |
| 400 | if (attribList[1] != EGL_FALSE) |
| 401 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 402 | break; |
| 403 | case EGL_VG_COLORSPACE: |
| 404 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 405 | case EGL_VG_ALPHA_FORMAT: |
| 406 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 407 | default: |
| 408 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 409 | } |
| 410 | |
| 411 | attribList += 2; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (width < 0 || height < 0) |
| 416 | { |
| 417 | return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 418 | } |
| 419 | |
| 420 | if (width == 0 || height == 0) |
| 421 | { |
| 422 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 423 | } |
| 424 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 425 | if (textureFormat != EGL_NO_TEXTURE && !mRenderer->getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height))) |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 426 | { |
| 427 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 428 | } |
| 429 | |
| 430 | if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) || |
| 431 | (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE)) |
| 432 | { |
| 433 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 434 | } |
| 435 | |
| 436 | if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT)) |
| 437 | { |
| 438 | return error(EGL_BAD_MATCH, EGL_NO_SURFACE); |
| 439 | } |
| 440 | |
| 441 | if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) || |
| 442 | (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE)) |
| 443 | { |
| 444 | return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE); |
| 445 | } |
| 446 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 447 | if (mRenderer->testDeviceLost()) |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 448 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 449 | if (!restoreLostDevice()) |
| 450 | return EGL_NO_SURFACE; |
| 451 | } |
| 452 | |
jbauman@chromium.org | 4e29770 | 2011-05-12 23:04:07 +0000 | [diff] [blame] | 453 | Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget); |
| 454 | |
| 455 | if (!surface->initialize()) |
| 456 | { |
| 457 | delete surface; |
| 458 | return EGL_NO_SURFACE; |
| 459 | } |
| 460 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 461 | mSurfaceSet.insert(surface); |
| 462 | |
jbauman@chromium.org | 06d7a75 | 2011-04-30 01:02:52 +0000 | [diff] [blame] | 463 | return success(surface); |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 464 | } |
| 465 | |
daniel@transgaming.com | 4ff960d | 2011-11-09 17:47:09 +0000 | [diff] [blame] | 466 | EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext, bool notifyResets, bool robustAccess) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 467 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 468 | if (!mRenderer) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 469 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 470 | return NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 471 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 472 | else if (mRenderer->testDeviceLost()) // Lost device |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 473 | { |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 474 | if (!restoreLostDevice()) |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 475 | return NULL; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | const egl::Config *config = mConfigSet.get(configHandle); |
| 479 | |
daniel@transgaming.com | 4ff960d | 2011-11-09 17:47:09 +0000 | [diff] [blame] | 480 | gl::Context *context = glCreateContext(config, shareContext, notifyResets, robustAccess); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 481 | mContextSet.insert(context); |
| 482 | |
| 483 | return context; |
| 484 | } |
| 485 | |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 486 | bool Display::restoreLostDevice() |
| 487 | { |
daniel@transgaming.com | cf23c45 | 2011-11-09 17:47:26 +0000 | [diff] [blame] | 488 | for (ContextSet::iterator ctx = mContextSet.begin(); ctx != mContextSet.end(); ctx++) |
| 489 | { |
| 490 | if ((*ctx)->isResetNotificationEnabled()) |
| 491 | return false; // If reset notifications have been requested, application must delete all contexts first |
| 492 | } |
| 493 | |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 494 | // Release surface resources to make the Reset() succeed |
| 495 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 496 | { |
| 497 | (*surface)->release(); |
| 498 | } |
| 499 | |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 500 | mVertexShaderCache.clear(); |
| 501 | mPixelShaderCache.clear(); |
| 502 | |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 503 | if (!mRenderer->resetDevice()) |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 504 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 505 | return error(EGL_BAD_ALLOC, false); |
vangelis@google.com | 8c9c452 | 2011-09-09 18:22:28 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // Restore any surfaces that may have been lost |
| 509 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 510 | { |
| 511 | (*surface)->resetSwapChain(); |
| 512 | } |
| 513 | |
| 514 | return true; |
| 515 | } |
| 516 | |
| 517 | |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 518 | void Display::destroySurface(egl::Surface *surface) |
| 519 | { |
daniel@transgaming.com | c556fa5 | 2011-05-26 14:13:29 +0000 | [diff] [blame] | 520 | delete surface; |
| 521 | mSurfaceSet.erase(surface); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | void Display::destroyContext(gl::Context *context) |
| 525 | { |
| 526 | glDestroyContext(context); |
| 527 | mContextSet.erase(context); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 528 | } |
| 529 | |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 530 | void Display::notifyDeviceLost() |
| 531 | { |
| 532 | for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) |
| 533 | { |
| 534 | (*context)->markContextLost(); |
| 535 | } |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 536 | mRenderer->markDeviceLost(); |
daniel@transgaming.com | 09fcc9f | 2011-11-09 17:46:47 +0000 | [diff] [blame] | 537 | error(EGL_CONTEXT_LOST); |
| 538 | } |
| 539 | |
daniel@transgaming.com | 3b1703f | 2011-05-11 15:36:26 +0000 | [diff] [blame] | 540 | bool Display::isInitialized() const |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 541 | { |
daniel@transgaming.com | 621ce05 | 2012-10-31 17:52:29 +0000 | [diff] [blame] | 542 | return mRenderer != NULL && mConfigSet.size() > 0; |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | bool Display::isValidConfig(EGLConfig config) |
| 546 | { |
| 547 | return mConfigSet.get(config) != NULL; |
| 548 | } |
| 549 | |
| 550 | bool Display::isValidContext(gl::Context *context) |
| 551 | { |
| 552 | return mContextSet.find(context) != mContextSet.end(); |
| 553 | } |
| 554 | |
| 555 | bool Display::isValidSurface(egl::Surface *surface) |
| 556 | { |
daniel@transgaming.com | c556fa5 | 2011-05-26 14:13:29 +0000 | [diff] [blame] | 557 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
daniel@transgaming.com | e979ead | 2010-09-23 18:03:14 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | bool Display::hasExistingWindowSurface(HWND window) |
| 561 | { |
| 562 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 563 | { |
| 564 | if ((*surface)->getWindowHandle() == window) |
| 565 | { |
| 566 | return true; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return false; |
| 571 | } |
| 572 | |
| 573 | EGLint Display::getMinSwapInterval() |
| 574 | { |
| 575 | return mMinSwapInterval; |
| 576 | } |
| 577 | |
| 578 | EGLint Display::getMaxSwapInterval() |
| 579 | { |
| 580 | return mMaxSwapInterval; |
| 581 | } |
| 582 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 583 | void Display::initExtensionString() |
| 584 | { |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 585 | HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 586 | bool shareHandleSupported = mRenderer->getShareHandleSupport(); |
daniel@transgaming.com | 1a1fe24 | 2011-09-26 18:25:45 +0000 | [diff] [blame] | 587 | |
| 588 | mExtensionString = ""; |
| 589 | |
daniel@transgaming.com | 8747f18 | 2011-11-09 17:50:38 +0000 | [diff] [blame] | 590 | // Multi-vendor (EXT) extensions |
| 591 | mExtensionString += "EGL_EXT_create_context_robustness "; |
| 592 | |
| 593 | // ANGLE-specific extensions |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 594 | if (shareHandleSupported) |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 595 | { |
daniel@transgaming.com | 1a1fe24 | 2011-09-26 18:25:45 +0000 | [diff] [blame] | 596 | mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer "; |
| 597 | } |
| 598 | |
| 599 | mExtensionString += "EGL_ANGLE_query_surface_pointer "; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 600 | |
| 601 | if (swiftShader) |
| 602 | { |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 603 | mExtensionString += "EGL_ANGLE_software_display "; |
jbauman@chromium.org | 84d7cbc | 2011-07-14 22:53:19 +0000 | [diff] [blame] | 604 | } |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 605 | |
daniel@transgaming.com | 313e392 | 2012-10-31 17:52:39 +0000 | [diff] [blame] | 606 | if (shareHandleSupported) |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 607 | { |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 608 | mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle "; |
| 609 | } |
| 610 | |
apatrick@chromium.org | f4490e2 | 2011-12-06 02:05:22 +0000 | [diff] [blame] | 611 | mExtensionString += "EGL_NV_post_sub_buffer"; |
| 612 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 613 | std::string::size_type end = mExtensionString.find_last_not_of(' '); |
| 614 | if (end != std::string::npos) |
| 615 | { |
| 616 | mExtensionString.resize(end+1); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | const char *Display::getExtensionString() const |
| 621 | { |
| 622 | return mExtensionString.c_str(); |
| 623 | } |
| 624 | |
apatrick@chromium.org | 3cfd722 | 2012-07-13 22:36:58 +0000 | [diff] [blame] | 625 | IDirect3DVertexShader9 *Display::createVertexShader(const DWORD *function, size_t length) |
| 626 | { |
| 627 | return mVertexShaderCache.create(function, length); |
| 628 | } |
| 629 | |
| 630 | IDirect3DPixelShader9 *Display::createPixelShader(const DWORD *function, size_t length) |
| 631 | { |
| 632 | return mPixelShaderCache.create(function, length); |
| 633 | } |
| 634 | |
daniel@transgaming.com | c6f7f9d | 2012-01-27 15:40:00 +0000 | [diff] [blame] | 635 | |
vladimirv@gmail.com | 721b7f2 | 2011-02-11 00:54:47 +0000 | [diff] [blame] | 636 | } |