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