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