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 | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 106 | for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 107 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 108 | D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | |
| 110 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); |
| 111 | |
| 112 | if (SUCCEEDED(result)) |
| 113 | { |
| 114 | for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) |
| 115 | { |
| 116 | D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; |
| 117 | HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); |
| 118 | |
| 119 | if (SUCCEEDED(result)) |
| 120 | { |
daniel@transgaming.com | adf0284 | 2010-05-07 13:03:33 +0000 | [diff] [blame] | 121 | HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 122 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | if (SUCCEEDED(result)) |
| 124 | { |
| 125 | // FIXME: Enumerate multi-sampling |
| 126 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 127 | mConfigSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | mConfigSet.enumerate(); |
| 136 | } |
| 137 | |
| 138 | if (!isInitialized()) |
| 139 | { |
| 140 | terminate(); |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | void Display::terminate() |
| 149 | { |
| 150 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 151 | { |
| 152 | delete *surface; |
| 153 | } |
| 154 | |
| 155 | for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) |
| 156 | { |
| 157 | glDestroyContext(*context); |
| 158 | } |
| 159 | |
| 160 | if (mDevice) |
| 161 | { |
| 162 | mDevice->Release(); |
| 163 | mDevice = NULL; |
| 164 | } |
| 165 | |
| 166 | if (mD3d9) |
| 167 | { |
| 168 | mD3d9->Release(); |
| 169 | mD3d9 = NULL; |
| 170 | } |
| 171 | } |
| 172 | |
daniel@transgaming.com | ae072af | 2010-05-05 18:47:28 +0000 | [diff] [blame] | 173 | void Display::startScene() |
| 174 | { |
| 175 | if (!mSceneStarted) |
| 176 | { |
| 177 | long result = mDevice->BeginScene(); |
| 178 | ASSERT(SUCCEEDED(result)); |
| 179 | mSceneStarted = true; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void Display::endScene() |
| 184 | { |
| 185 | if (mSceneStarted) |
| 186 | { |
| 187 | long result = mDevice->EndScene(); |
| 188 | ASSERT(SUCCEEDED(result)); |
| 189 | mSceneStarted = false; |
| 190 | } |
| 191 | } |
| 192 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) |
| 194 | { |
| 195 | return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); |
| 196 | } |
| 197 | |
| 198 | bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) |
| 199 | { |
| 200 | const egl::Config *configuration = mConfigSet.get(config); |
| 201 | |
| 202 | switch (attribute) |
| 203 | { |
| 204 | case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; |
| 205 | case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; |
| 206 | case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; |
| 207 | case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; |
| 208 | case EGL_RED_SIZE: *value = configuration->mRedSize; break; |
| 209 | case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; |
| 210 | case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; |
| 211 | case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; |
| 212 | case EGL_CONFIG_ID: *value = configuration->mConfigID; break; |
| 213 | case EGL_LEVEL: *value = configuration->mLevel; break; |
| 214 | case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; |
| 215 | case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; |
| 216 | case EGL_SAMPLES: *value = configuration->mSamples; break; |
| 217 | case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; |
| 218 | case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; |
| 219 | case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; |
| 220 | case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; |
| 221 | case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; |
| 222 | case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; |
| 223 | case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; |
| 224 | case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; |
| 225 | case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; |
| 226 | case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; |
| 227 | case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; |
| 228 | case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; |
| 229 | case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; |
| 230 | case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 231 | case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 232 | case EGL_CONFORMANT: *value = configuration->mConformant; break; |
| 233 | default: |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 240 | Surface *Display::createWindowSurface(HWND window, EGLConfig config) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 241 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 242 | const Config *configuration = mConfigSet.get(config); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 244 | D3DPRESENT_PARAMETERS presentParameters = {0}; |
| 245 | |
| 246 | presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat; |
| 247 | presentParameters.BackBufferCount = 1; |
| 248 | presentParameters.BackBufferFormat = configuration->mRenderTargetFormat; |
| 249 | presentParameters.BackBufferWidth = 0; |
| 250 | presentParameters.BackBufferHeight = 0; |
| 251 | presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE; |
| 252 | presentParameters.Flags = 0; |
| 253 | presentParameters.hDeviceWindow = window; |
| 254 | presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented |
| 255 | presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 256 | presentParameters.PresentationInterval = convertInterval(mMinSwapInterval); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 257 | presentParameters.SwapEffect = D3DSWAPEFFECT_COPY; |
| 258 | presentParameters.Windowed = TRUE; // FIXME |
| 259 | |
| 260 | IDirect3DSwapChain9 *swapChain = NULL; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 261 | IDirect3DSurface9 *depthStencilSurface = NULL; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 262 | |
| 263 | if (!mDevice) |
| 264 | { |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 265 | DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; |
| 266 | |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 267 | 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] | 268 | |
| 269 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 270 | { |
| 271 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 272 | } |
| 273 | |
daniel@transgaming.com | 02bc159 | 2010-03-30 03:36:13 +0000 | [diff] [blame] | 274 | if (FAILED(result)) |
| 275 | { |
daniel@transgaming.com | c28e76b | 2010-05-05 18:47:16 +0000 | [diff] [blame] | 276 | 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] | 277 | |
| 278 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 279 | { |
| 280 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 281 | } |
| 282 | } |
| 283 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 284 | ASSERT(SUCCEEDED(result)); |
| 285 | |
| 286 | if (mDevice) |
| 287 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 288 | mSceneStarted = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 289 | mDevice->GetSwapChain(0, &swapChain); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 290 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | else |
| 294 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 295 | if (!mSurfaceSet.empty()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 296 | { |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 297 | // if the device already exists, and there are other surfaces/windows currently in use, we need to create |
| 298 | // a separate swap chain for the new draw surface. |
| 299 | HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 300 | |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 301 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 302 | { |
| 303 | ERR("Could not create additional swap chains. Out of memory."); |
| 304 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 305 | } |
| 306 | |
| 307 | ASSERT(SUCCEEDED(result)); |
| 308 | |
| 309 | // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike |
| 310 | // CreateDevice, so we must do so explicitly. |
| 311 | result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight, |
| 312 | presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType, |
| 313 | presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL); |
| 314 | |
| 315 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 316 | { |
| 317 | swapChain->Release(); |
| 318 | ERR("Could not create depthstencil surface for new swap chain. Out of memory."); |
| 319 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 320 | } |
| 321 | |
| 322 | ASSERT(SUCCEEDED(result)); |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | // if the device already exists, but there are no surfaces in use, then all the surfaces/windows |
| 327 | // have been destroyed, and we should repurpose the originally created depthstencil surface for |
| 328 | // use with the new surface we are creating. |
| 329 | HRESULT result = mDevice->Reset(&presentParameters); |
| 330 | |
| 331 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 332 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 333 | ERR("Could not reset presentation parameters for device. Out of memory."); |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 334 | return error(EGL_BAD_ALLOC, (egl::Surface*)NULL); |
| 335 | } |
| 336 | |
| 337 | ASSERT(SUCCEEDED(result)); |
| 338 | |
| 339 | if (mDevice) |
| 340 | { |
daniel@transgaming.com | c9def0b | 2010-05-05 18:48:06 +0000 | [diff] [blame] | 341 | mSceneStarted = false; |
daniel@transgaming.com | 0009d62 | 2010-03-16 06:23:31 +0000 | [diff] [blame] | 342 | mDevice->GetSwapChain(0, &swapChain); |
| 343 | mDevice->GetDepthStencilSurface(&depthStencilSurface); |
| 344 | } |
| 345 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | Surface *surface = NULL; |
| 349 | |
| 350 | if (swapChain) |
| 351 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 352 | surface = new Surface(this, swapChain, depthStencilSurface, configuration); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 353 | mSurfaceSet.insert(surface); |
| 354 | |
| 355 | swapChain->Release(); |
| 356 | } |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 357 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 358 | return surface; |
| 359 | } |
| 360 | |
| 361 | EGLContext Display::createContext(EGLConfig configHandle) |
| 362 | { |
| 363 | const egl::Config *config = mConfigSet.get(configHandle); |
| 364 | |
| 365 | gl::Context *context = glCreateContext(config); |
| 366 | mContextSet.insert(context); |
| 367 | |
| 368 | return context; |
| 369 | } |
| 370 | |
| 371 | void Display::destroySurface(egl::Surface *surface) |
| 372 | { |
| 373 | delete surface; |
| 374 | mSurfaceSet.erase(surface); |
| 375 | } |
| 376 | |
| 377 | void Display::destroyContext(gl::Context *context) |
| 378 | { |
| 379 | glDestroyContext(context); |
| 380 | mContextSet.erase(context); |
| 381 | } |
| 382 | |
| 383 | bool Display::isInitialized() |
| 384 | { |
| 385 | return mD3d9 != NULL && mConfigSet.size() > 0; |
| 386 | } |
| 387 | |
| 388 | bool Display::isValidConfig(EGLConfig config) |
| 389 | { |
| 390 | return mConfigSet.get(config) != NULL; |
| 391 | } |
| 392 | |
| 393 | bool Display::isValidContext(gl::Context *context) |
| 394 | { |
| 395 | return mContextSet.find(context) != mContextSet.end(); |
| 396 | } |
| 397 | |
| 398 | bool Display::isValidSurface(egl::Surface *surface) |
| 399 | { |
| 400 | return mSurfaceSet.find(surface) != mSurfaceSet.end(); |
| 401 | } |
| 402 | |
| 403 | bool Display::hasExistingWindowSurface(HWND window) |
| 404 | { |
| 405 | for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) |
| 406 | { |
| 407 | if ((*surface)->getWindowHandle() == window) |
| 408 | { |
| 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return false; |
| 414 | } |
| 415 | |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 416 | void Display::setSwapInterval(GLint interval) |
| 417 | { |
| 418 | mSwapInterval = interval; |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 419 | mSwapInterval = std::max(mSwapInterval, mMinSwapInterval); |
| 420 | mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval); |
| 421 | |
| 422 | mPresentInterval = convertInterval(mSwapInterval); |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 423 | } |
| 424 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 425 | DWORD Display::getPresentInterval() |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 426 | { |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 427 | return mPresentInterval; |
| 428 | } |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 429 | |
daniel@transgaming.com | 616ffcf | 2010-05-07 19:01:49 +0000 | [diff] [blame^] | 430 | DWORD Display::convertInterval(GLint interval) |
| 431 | { |
daniel@transgaming.com | b6bd727 | 2010-05-05 18:48:22 +0000 | [diff] [blame] | 432 | switch(interval) |
| 433 | { |
| 434 | case 0: return D3DPRESENT_INTERVAL_IMMEDIATE; |
| 435 | case 1: return D3DPRESENT_INTERVAL_ONE; |
| 436 | case 2: return D3DPRESENT_INTERVAL_TWO; |
| 437 | case 3: return D3DPRESENT_INTERVAL_THREE; |
| 438 | case 4: return D3DPRESENT_INTERVAL_FOUR; |
| 439 | default: UNREACHABLE(); |
| 440 | } |
| 441 | |
| 442 | return D3DPRESENT_INTERVAL_DEFAULT; |
| 443 | } |
| 444 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 445 | IDirect3DDevice9 *Display::getDevice() |
| 446 | { |
| 447 | return mDevice; |
| 448 | } |
| 449 | } |