daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 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. |
| 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "libEGL/Display.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 12 | |
daniel@transgaming.com | 1697302 | 2010-03-11 19:22:19 +0000 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <vector> |
| 15 | |
alokp@chromium.org | ea0e1af | 2010-03-22 19:33:14 +0000 | [diff] [blame] | 16 | #include "common/debug.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 18 | #include "libEGL/main.h" |
| 19 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | namespace egl |
| 21 | { |
| 22 | Display::Display(HDC deviceContext) : mDc(deviceContext) |
| 23 | { |
| 24 | mD3d9 = NULL; |
| 25 | mDevice = NULL; |
| 26 | |
| 27 | mAdapter = D3DADAPTER_DEFAULT; |
| 28 | mDeviceType = D3DDEVTYPE_HAL; |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 29 | |
| 30 | mMinSwapInterval = 1; |
| 31 | mMaxSwapInterval = 1; |
| 32 | setSwapInterval(1); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Display::~Display() |
| 36 | { |
| 37 | terminate(); |
| 38 | } |
| 39 | |
| 40 | bool Display::initialize() |
| 41 | { |
| 42 | if (isInitialized()) |
| 43 | { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); |
| 48 | |
| 49 | if (mD3d9) |
| 50 | { |
| 51 | if (mDc != NULL) |
| 52 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 53 | // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | D3DCAPS9 caps; |
| 57 | HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps); |
| 58 | |
| 59 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 60 | { |
| 61 | return error(EGL_BAD_ALLOC, false); |
| 62 | } |
| 63 | |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 64 | if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | { |
| 66 | mD3d9->Release(); |
| 67 | mD3d9 = NULL; |
| 68 | } |
| 69 | else |
| 70 | { |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 71 | mMinSwapInterval = 4; |
| 72 | mMaxSwapInterval = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 73 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 74 | if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} |
| 75 | if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} |
| 76 | if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} |
| 77 | if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} |
| 78 | if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 80 | const D3DFORMAT renderTargetFormats[] = |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 81 | { |
| 82 | D3DFMT_A1R5G5B5, |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 83 | // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 84 | D3DFMT_A8R8G8B8, |
| 85 | D3DFMT_R5G6B5, |
| 86 | D3DFMT_X1R5G5B5, |
| 87 | D3DFMT_X8R8G8B8 |
| 88 | }; |
| 89 | |
| 90 | const D3DFORMAT depthStencilFormats[] = |
| 91 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 92 | // D3DFMT_D16_LOCKABLE, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | D3DFMT_D32, |
daniel@transgaming.com | d1f6fde | 2010-05-07 19:01:43 +0000 | [diff] [blame] | 94 | // D3DFMT_D15S1, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | D3DFMT_D24S8, |
| 96 | D3DFMT_D24X8, |
daniel@transgaming.com | d1f6fde | 2010-05-07 19:01:43 +0000 | [diff] [blame] | 97 | // D3DFMT_D24X4S4, |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 98 | D3DFMT_D16, |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 99 | // D3DFMT_D32F_LOCKABLE, |
| 100 | // D3DFMT_D24FS8 |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | D3DDISPLAYMODE currentDisplayMode; |
| 104 | mD3d9->GetAdapterDisplayMode(mAdapter, ¤tDisplayMode); |
| 105 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 106 | ConfigSet configSet; |
| 107 | |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 108 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 110 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 111 | |
| 112 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 113 | |
| 114 | if (SUCCEEDED(result)) |
| 115 | { |
| 116 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
| 117 | { |
| 118 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
| 119 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 120 | |
| 121 | if (SUCCEEDED(result)) |
| 122 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 123 | HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 124 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 125 | if (SUCCEEDED(result)) |
| 126 | { |
| 127 | // FIXME: Enumerate multi-sampling |
| 128 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 129 | configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | |
daniel@transgaming.com | 178adff | 2010-05-18 18:52:04 +0000 | [diff] [blame] | 136 | // Give the sorted configs a unique ID and store them internally |
| 137 | EGLint index = 1; |
| 138 | for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) |
| 139 | { |
| 140 | Config configuration = *config; |
| 141 | configuration.mConfigID = index; |
| 142 | index++; |
| 143 | |
| 144 | mConfigSet.mSet.insert(configuration); |
| 145 | } |
| 146 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (!isInitialized()) |
| 150 | { |
| 151 | terminate(); |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | void Display::terminate() |
| 160 | { |
| 161 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 162 | { |
| 163 | delete *surface; |
| 164 | } |
| 165 | |
| 166 | for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) |
| 167 | { |
| 168 | glDestroyContext(*context); |
| 169 | } |
| 170 | |
| 171 | if (mDevice) |
| 172 | { |
| 173 | mDevice->Release(); |
| 174 | mDevice = NULL; |
| 175 | } |
| 176 | |
| 177 | if (mD3d9) |
| 178 | { |
| 179 | mD3d9->Release(); |
| 180 | mD3d9 = NULL; |
| 181 | } |
| 182 | } |
| 183 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 184 | void Display::startScene() |
| 185 | { |
| 186 | if (!mSceneStarted) |
| 187 | { |
| 188 | long result = mDevice->BeginScene(); |
| 189 | ASSERT(SUCCEEDED(result)); |
| 190 | mSceneStarted = true; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void Display::endScene() |
| 195 | { |
| 196 | if (mSceneStarted) |
| 197 | { |
| 198 | long result = mDevice->EndScene(); |
| 199 | ASSERT(SUCCEEDED(result)); |
| 200 | mSceneStarted = false; |
| 201 | } |
| 202 | } |
| 203 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 204 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 205 | { |
| 206 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 207 | } |
| 208 | |
| 209 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 210 | { |
| 211 | const egl::Config *configuration = mConfigSet.get(config); |
| 212 | |
| 213 | switch (attribute) |
| 214 | { |
| 215 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
| 216 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
| 217 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
| 218 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
| 219 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
| 220 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
| 221 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
| 222 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
| 223 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
| 224 | case EGL_LEVEL: *value = configuration->mLevel; break; |
| 225 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
| 226 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
| 227 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 228 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 229 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 230 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 231 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 232 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 233 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 234 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 235 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 236 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 237 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 238 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 239 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 240 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 241 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 242 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
| 244 | default: |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 251 | Surface *Display::createWindowSurface(HWND window, EGLConfig config) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 252 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 253 | const Config *configuration = mConfigSet.get(config); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 254 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 255 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 256 | |
| 257 | presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat; |
| 258 | presentParameters.BackBufferCount = 1; |
| 259 | presentParameters.BackBufferFormat = configuration->mRenderTargetFormat; |
| 260 | presentParameters.BackBufferWidth = 0; |
| 261 | presentParameters.BackBufferHeight = 0; |
| 262 | presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE; |
| 263 | presentParameters.Flags = 0; |
| 264 | presentParameters.hDeviceWindow = window; |
| 265 | presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented |
| 266 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 267 | presentParameters.PresentationInterval = convertInterval(mMinSwapInterval); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 268 | presentParameters.SwapEffect = D3DSWAPEFFECT_COPY; |
| 269 | presentParameters.Windowed = TRUE; // FIXME |
| 270 | |
| 271 | IDirect3DSwapChain9 *swapChain = NULL; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 272 | IDirect3DSurface9 *depthStencilSurface = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 273 | |
| 274 | if (!mDevice) |
| 275 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 276 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 277 | |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 278 | HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 279 | |
| 280 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 281 | { |
| 282 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 283 | } |
| 284 | |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 285 | if (FAILED(result)) |
| 286 | { |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 287 | result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 288 | |
| 289 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 290 | { |
| 291 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 292 | } |
| 293 | } |
| 294 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | ASSERT(SUCCEEDED(result)); |
| 296 | |
| 297 | if (mDevice) |
| 298 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 299 | mSceneStarted = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 300 | mDevice->GetSwapChain(0, &swapChain); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 301 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | else |
| 305 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 306 | if (!mSurfaceSet.empty()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 307 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 308 | // if the device already exists, and there are other surfaces/windows currently in use, we need to create |
| 309 | // a separate swap chain for the new draw surface. |
| 310 | HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 311 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 312 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 313 | { |
| 314 | ERR("Could not create additional swap chains. Out of memory."); |
| 315 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 316 | } |
| 317 | |
| 318 | ASSERT(SUCCEEDED(result)); |
| 319 | |
| 320 | // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike |
| 321 | // CreateDevice, so we must do so explicitly. |
| 322 | result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, |
| 323 | presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, |
| 324 | presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL); |
| 325 | |
| 326 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 327 | { |
| 328 | swapChain->Release(); |
| 329 | ERR("Could not create depthstencil surface for new swap chain. Out of memory."); |
| 330 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 331 | } |
| 332 | |
| 333 | ASSERT(SUCCEEDED(result)); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | // if the device already exists, but there are no surfaces in use, then all the surfaces/windows |
| 338 | // have been destroyed, and we should repurpose the originally created depthstencil surface for |
| 339 | // use with the new surface we are creating. |
| 340 | HRESULT result = mDevice->Reset(&presentParameters); |
| 341 | |
| 342 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 343 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 344 | ERR("Could not reset presentation parameters for device. Out of memory."); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 345 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 346 | } |
| 347 | |
| 348 | ASSERT(SUCCEEDED(result)); |
| 349 | |
| 350 | if (mDevice) |
| 351 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 352 | mSceneStarted = false; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 353 | mDevice->GetSwapChain(0, &swapChain); |
| 354 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
| 355 | } |
| 356 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | Surface *surface = NULL; |
| 360 | |
| 361 | if (swapChain) |
| 362 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 363 | surface = new Surface(this, swapChain, depthStencilSurface, configuration); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 364 | mSurfaceSet.insert(surface); |
| 365 | |
| 366 | swapChain->Release(); |
| 367 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 368 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 369 | return surface; |
| 370 | } |
| 371 | |
| 372 | EGLContext Display::createContext(EGLConfig configHandle) |
| 373 | { |
| 374 | const egl::Config *config = mConfigSet.get(configHandle); |
| 375 | |
| 376 | gl::Context *context = glCreateContext(config); |
| 377 | mContextSet.insert(context); |
| 378 | |
| 379 | return context; |
| 380 | } |
| 381 | |
| 382 | void Display::destroySurface(egl::Surface *surface) |
| 383 | { |
| 384 | delete surface; |
| 385 | mSurfaceSet.erase(surface); |
| 386 | } |
| 387 | |
| 388 | void Display::destroyContext(gl::Context *context) |
| 389 | { |
| 390 | glDestroyContext(context); |
| 391 | mContextSet.erase(context); |
| 392 | } |
| 393 | |
| 394 | bool Display::isInitialized() |
| 395 | { |
| 396 | return mD3d9 != NULL && mConfigSet.size() > 0; |
| 397 | } |
| 398 | |
| 399 | bool Display::isValidConfig(EGLConfig config) |
| 400 | { |
| 401 | return mConfigSet.get(config) != NULL; |
| 402 | } |
| 403 | |
| 404 | bool Display::isValidContext(gl::Context *context) |
| 405 | { |
| 406 | return mContextSet.find(context) != mContextSet.end(); |
| 407 | } |
| 408 | |
| 409 | bool Display::isValidSurface(egl::Surface *surface) |
| 410 | { |
| 411 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
| 412 | } |
| 413 | |
| 414 | bool Display::hasExistingWindowSurface(HWND window) |
| 415 | { |
| 416 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 417 | { |
| 418 | if ((*surface)->getWindowHandle() == window) |
| 419 | { |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 427 | void Display::setSwapInterval(GLint interval) |
| 428 | { |
| 429 | mSwapInterval = interval; |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 430 | mSwapInterval = std::max(mSwapInterval, mMinSwapInterval); |
| 431 | mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval); |
| 432 | |
| 433 | mPresentInterval = convertInterval(mSwapInterval); |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 434 | } |
| 435 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 436 | DWORD Display::getPresentInterval() |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 437 | { |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 438 | return mPresentInterval; |
| 439 | } |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 440 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame] | 441 | DWORD Display::convertInterval(GLint interval) |
| 442 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 443 | switch(interval) |
| 444 | { |
| 445 | case 0: return D3DPRESENT_INTERVAL_IMMEDIATE; |
| 446 | case 1: return D3DPRESENT_INTERVAL_ONE; |
| 447 | case 2: return D3DPRESENT_INTERVAL_TWO; |
| 448 | case 3: return D3DPRESENT_INTERVAL_THREE; |
| 449 | case 4: return D3DPRESENT_INTERVAL_FOUR; |
| 450 | default: UNREACHABLE(); |
| 451 | } |
| 452 | |
| 453 | return D3DPRESENT_INTERVAL_DEFAULT; |
| 454 | } |
| 455 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 456 | IDirect3DDevice9 *Display::getDevice() |
| 457 | { |
| 458 | return mDevice; |
| 459 | } |
| 460 | } |