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