blob: 1f7a2b0d0ed1333daf424b0444377531be210054 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.combbf56f72010-04-20 18:52:13 +000011#include "libEGL/Display.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000012
daniel@transgaming.com16973022010-03-11 19:22:19 +000013#include <algorithm>
14#include <vector>
15
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000016#include "common/debug.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000017
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018#include "libEGL/main.h"
19
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020namespace egl
21{
22Display::Display(HDC deviceContext) : mDc(deviceContext)
23{
24 mD3d9 = NULL;
25 mDevice = NULL;
26
27 mAdapter = D3DADAPTER_DEFAULT;
28 mDeviceType = D3DDEVTYPE_HAL;
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +000029 mSwapInterval = 1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030}
31
32Display::~Display()
33{
34 terminate();
35}
36
37bool Display::initialize()
38{
39 if (isInitialized())
40 {
41 return true;
42 }
43
44 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
45
46 if (mD3d9)
47 {
48 if (mDc != NULL)
49 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000050 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051 }
52
53 D3DCAPS9 caps;
54 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps);
55
56 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
57 {
58 return error(EGL_BAD_ALLOC, false);
59 }
60
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000061 if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 {
63 mD3d9->Release();
64 mD3d9 = NULL;
65 }
66 else
67 {
68 EGLint minSwapInterval = 4;
69 EGLint maxSwapInterval = 0;
70
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000071 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {minSwapInterval = std::min(minSwapInterval, 0); maxSwapInterval = std::max(maxSwapInterval, 0);}
72 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {minSwapInterval = std::min(minSwapInterval, 1); maxSwapInterval = std::max(maxSwapInterval, 1);}
73 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {minSwapInterval = std::min(minSwapInterval, 2); maxSwapInterval = std::max(maxSwapInterval, 2);}
74 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {minSwapInterval = std::min(minSwapInterval, 3); maxSwapInterval = std::max(maxSwapInterval, 3);}
75 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {minSwapInterval = std::min(minSwapInterval, 4); maxSwapInterval = std::max(maxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076
daniel@transgaming.comadf02842010-05-07 13:03:33 +000077 const D3DFORMAT renderTargetFormats[] =
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000078 {
79 D3DFMT_A1R5G5B5,
daniel@transgaming.comadf02842010-05-07 13:03:33 +000080 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081 D3DFMT_A8R8G8B8,
82 D3DFMT_R5G6B5,
83 D3DFMT_X1R5G5B5,
84 D3DFMT_X8R8G8B8
85 };
86
87 const D3DFORMAT depthStencilFormats[] =
88 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000089 // D3DFMT_D16_LOCKABLE,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090 D3DFMT_D32,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +000091 // D3DFMT_D15S1,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092 D3DFMT_D24S8,
93 D3DFMT_D24X8,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +000094 // D3DFMT_D24X4S4,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 D3DFMT_D16,
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000096 // D3DFMT_D32F_LOCKABLE,
97 // D3DFMT_D24FS8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098 };
99
100 D3DDISPLAYMODE currentDisplayMode;
101 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
102
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000103 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000105 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
107 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
108
109 if (SUCCEEDED(result))
110 {
111 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
112 {
113 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
114 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
115
116 if (SUCCEEDED(result))
117 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000118 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000119
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120 if (SUCCEEDED(result))
121 {
122 // FIXME: Enumerate multi-sampling
123
124 mConfigSet.add(currentDisplayMode, minSwapInterval, maxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
125 }
126 }
127 }
128 }
129 }
130 }
131
132 mConfigSet.enumerate();
133 }
134
135 if (!isInitialized())
136 {
137 terminate();
138
139 return false;
140 }
141
142 return true;
143}
144
145void Display::terminate()
146{
147 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
148 {
149 delete *surface;
150 }
151
152 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
153 {
154 glDestroyContext(*context);
155 }
156
157 if (mDevice)
158 {
159 mDevice->Release();
160 mDevice = NULL;
161 }
162
163 if (mD3d9)
164 {
165 mD3d9->Release();
166 mD3d9 = NULL;
167 }
168}
169
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000170void Display::startScene()
171{
172 if (!mSceneStarted)
173 {
174 long result = mDevice->BeginScene();
175 ASSERT(SUCCEEDED(result));
176 mSceneStarted = true;
177 }
178}
179
180void Display::endScene()
181{
182 if (mSceneStarted)
183 {
184 long result = mDevice->EndScene();
185 ASSERT(SUCCEEDED(result));
186 mSceneStarted = false;
187 }
188}
189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
191{
192 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
193}
194
195bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
196{
197 const egl::Config *configuration = mConfigSet.get(config);
198
199 switch (attribute)
200 {
201 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
202 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
203 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
204 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
205 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
206 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
207 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
208 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
209 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
210 case EGL_LEVEL: *value = configuration->mLevel; break;
211 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
212 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
213 case EGL_SAMPLES: *value = configuration->mSamples; break;
214 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
215 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
216 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
217 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
218 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
219 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
220 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
221 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
222 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
223 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
224 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
225 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
226 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
227 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000228 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229 case EGL_CONFORMANT: *value = configuration->mConformant; break;
230 default:
231 return false;
232 }
233
234 return true;
235}
236
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000237Surface *Display::createWindowSurface(HWND window, EGLConfig config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000239 const Config *configuration = mConfigSet.get(config);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241 D3DPRESENT_PARAMETERS presentParameters = {0};
242
243 presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat;
244 presentParameters.BackBufferCount = 1;
245 presentParameters.BackBufferFormat = configuration->mRenderTargetFormat;
246 presentParameters.BackBufferWidth = 0;
247 presentParameters.BackBufferHeight = 0;
248 presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE;
249 presentParameters.Flags = 0;
250 presentParameters.hDeviceWindow = window;
251 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
252 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000253 presentParameters.PresentationInterval = getPresentInterval(configuration, true);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254 presentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
255 presentParameters.Windowed = TRUE; // FIXME
256
257 IDirect3DSwapChain9 *swapChain = NULL;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000258 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
260 if (!mDevice)
261 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000262 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
263
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000264 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
266 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
267 {
268 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
269 }
270
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000271 if (FAILED(result))
272 {
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000273 result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000274
275 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
276 {
277 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
278 }
279 }
280
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 ASSERT(SUCCEEDED(result));
282
283 if (mDevice)
284 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000285 mSceneStarted = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286 mDevice->GetSwapChain(0, &swapChain);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000287 mDevice->GetDepthStencilSurface(&depthStencilSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288 }
289 }
290 else
291 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000292 if (!mSurfaceSet.empty())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000294 // if the device already exists, and there are other surfaces/windows currently in use, we need to create
295 // a separate swap chain for the new draw surface.
296 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000298 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
299 {
300 ERR("Could not create additional swap chains. Out of memory.");
301 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
302 }
303
304 ASSERT(SUCCEEDED(result));
305
306 // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike
307 // CreateDevice, so we must do so explicitly.
308 result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight,
309 presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType,
310 presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL);
311
312 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
313 {
314 swapChain->Release();
315 ERR("Could not create depthstencil surface for new swap chain. Out of memory.");
316 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
317 }
318
319 ASSERT(SUCCEEDED(result));
320 }
321 else
322 {
323 // if the device already exists, but there are no surfaces in use, then all the surfaces/windows
324 // have been destroyed, and we should repurpose the originally created depthstencil surface for
325 // use with the new surface we are creating.
326 HRESULT result = mDevice->Reset(&presentParameters);
327
328 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
329 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000330 ERR("Could not reset presentation parameters for device. Out of memory.");
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000331 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
332 }
333
334 ASSERT(SUCCEEDED(result));
335
336 if (mDevice)
337 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000338 mSceneStarted = false;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000339 mDevice->GetSwapChain(0, &swapChain);
340 mDevice->GetDepthStencilSurface(&depthStencilSurface);
341 }
342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343 }
344
345 Surface *surface = NULL;
346
347 if (swapChain)
348 {
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000349 surface = new Surface(this, swapChain, depthStencilSurface, configuration);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350 mSurfaceSet.insert(surface);
351
352 swapChain->Release();
353 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000354
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355 return surface;
356}
357
358EGLContext Display::createContext(EGLConfig configHandle)
359{
360 const egl::Config *config = mConfigSet.get(configHandle);
361
362 gl::Context *context = glCreateContext(config);
363 mContextSet.insert(context);
364
365 return context;
366}
367
368void Display::destroySurface(egl::Surface *surface)
369{
370 delete surface;
371 mSurfaceSet.erase(surface);
372}
373
374void Display::destroyContext(gl::Context *context)
375{
376 glDestroyContext(context);
377 mContextSet.erase(context);
378}
379
380bool Display::isInitialized()
381{
382 return mD3d9 != NULL && mConfigSet.size() > 0;
383}
384
385bool Display::isValidConfig(EGLConfig config)
386{
387 return mConfigSet.get(config) != NULL;
388}
389
390bool Display::isValidContext(gl::Context *context)
391{
392 return mContextSet.find(context) != mContextSet.end();
393}
394
395bool Display::isValidSurface(egl::Surface *surface)
396{
397 return mSurfaceSet.find(surface) != mSurfaceSet.end();
398}
399
400bool Display::hasExistingWindowSurface(HWND window)
401{
402 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
403 {
404 if ((*surface)->getWindowHandle() == window)
405 {
406 return true;
407 }
408 }
409
410 return false;
411}
412
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000413void Display::setSwapInterval(GLint interval)
414{
415 mSwapInterval = interval;
416}
417
418DWORD Display::getPresentInterval(const egl::Config *config, bool maximumRate)
419{
420 GLint interval = maximumRate ? 0 : mSwapInterval;
421 interval = interval < config->mMinSwapInterval ? config->mMinSwapInterval : interval;
422 interval = interval > config->mMaxSwapInterval ? config->mMaxSwapInterval : interval;
423
424 switch(interval)
425 {
426 case 0: return D3DPRESENT_INTERVAL_IMMEDIATE;
427 case 1: return D3DPRESENT_INTERVAL_ONE;
428 case 2: return D3DPRESENT_INTERVAL_TWO;
429 case 3: return D3DPRESENT_INTERVAL_THREE;
430 case 4: return D3DPRESENT_INTERVAL_FOUR;
431 default: UNREACHABLE();
432 }
433
434 return D3DPRESENT_INTERVAL_DEFAULT;
435}
436
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437IDirect3DDevice9 *Display::getDevice()
438{
439 return mDevice;
440}
441}