blob: 4d6916750834bc1ff8e91e64fc107cf33156928e [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.comae072af2010-05-05 18:47:28 +000029
30 mSceneStarted = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031}
32
33Display::~Display()
34{
35 terminate();
36}
37
38bool Display::initialize()
39{
40 if (isInitialized())
41 {
42 return true;
43 }
44
45 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
46
47 if (mD3d9)
48 {
49 if (mDc != NULL)
50 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000051 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052 }
53
54 D3DCAPS9 caps;
55 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps);
56
57 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
58 {
59 return error(EGL_BAD_ALLOC, false);
60 }
61
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000062 if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063 {
64 mD3d9->Release();
65 mD3d9 = NULL;
66 }
67 else
68 {
69 EGLint minSwapInterval = 4;
70 EGLint maxSwapInterval = 0;
71
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000072 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {minSwapInterval = std::min(minSwapInterval, 0); maxSwapInterval = std::max(maxSwapInterval, 0);}
73 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {minSwapInterval = std::min(minSwapInterval, 1); maxSwapInterval = std::max(maxSwapInterval, 1);}
74 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {minSwapInterval = std::min(minSwapInterval, 2); maxSwapInterval = std::max(maxSwapInterval, 2);}
75 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {minSwapInterval = std::min(minSwapInterval, 3); maxSwapInterval = std::max(maxSwapInterval, 3);}
76 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {minSwapInterval = std::min(minSwapInterval, 4); maxSwapInterval = std::max(maxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077
78 const D3DFORMAT adapterFormats[] =
79 {
80 D3DFMT_A1R5G5B5,
daniel@transgaming.come8c0ca22010-04-20 18:52:47 +000081 //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 +000082 D3DFMT_A8R8G8B8,
83 D3DFMT_R5G6B5,
84 D3DFMT_X1R5G5B5,
85 D3DFMT_X8R8G8B8
86 };
87
88 const D3DFORMAT depthStencilFormats[] =
89 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000090 // D3DFMT_D16_LOCKABLE,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000091 D3DFMT_D32,
92 D3DFMT_D15S1,
93 D3DFMT_D24S8,
94 D3DFMT_D24X8,
95 D3DFMT_D24X4S4,
96 D3DFMT_D16,
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000097 // D3DFMT_D32F_LOCKABLE,
98 // D3DFMT_D24FS8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099 };
100
101 D3DDISPLAYMODE currentDisplayMode;
102 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
103
104 for (int formatIndex = 0; formatIndex < sizeof(adapterFormats) / sizeof(D3DFORMAT); formatIndex++)
105 {
106 D3DFORMAT renderTargetFormat = adapterFormats[formatIndex];
107
108 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
109
110 if (SUCCEEDED(result))
111 {
112 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
113 {
114 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
115 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
116
117 if (SUCCEEDED(result))
118 {
119 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); // FIXME: Only accept color formats available both in fullscreen and windowed?
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121 if (SUCCEEDED(result))
122 {
123 // FIXME: Enumerate multi-sampling
124
125 mConfigSet.add(currentDisplayMode, minSwapInterval, maxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
126 }
127 }
128 }
129 }
130 }
131 }
132
133 mConfigSet.enumerate();
134 }
135
136 if (!isInitialized())
137 {
138 terminate();
139
140 return false;
141 }
142
143 return true;
144}
145
146void Display::terminate()
147{
148 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
149 {
150 delete *surface;
151 }
152
153 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
154 {
155 glDestroyContext(*context);
156 }
157
158 if (mDevice)
159 {
160 mDevice->Release();
161 mDevice = NULL;
162 }
163
164 if (mD3d9)
165 {
166 mD3d9->Release();
167 mD3d9 = NULL;
168 }
169}
170
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000171void Display::startScene()
172{
173 if (!mSceneStarted)
174 {
175 long result = mDevice->BeginScene();
176 ASSERT(SUCCEEDED(result));
177 mSceneStarted = true;
178 }
179}
180
181void Display::endScene()
182{
183 if (mSceneStarted)
184 {
185 long result = mDevice->EndScene();
186 ASSERT(SUCCEEDED(result));
187 mSceneStarted = false;
188 }
189}
190
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
192{
193 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
194}
195
196bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
197{
198 const egl::Config *configuration = mConfigSet.get(config);
199
200 switch (attribute)
201 {
202 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
203 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
204 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
205 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
206 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
207 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
208 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
209 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
210 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
211 case EGL_LEVEL: *value = configuration->mLevel; break;
212 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
213 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
214 case EGL_SAMPLES: *value = configuration->mSamples; break;
215 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
216 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
217 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
218 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
219 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
220 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
221 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
222 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
223 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
224 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
225 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
226 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
227 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
228 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
229 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
230 case EGL_CONFORMANT: *value = configuration->mConformant; break;
231 default:
232 return false;
233 }
234
235 return true;
236}
237
238egl::Surface *Display::createWindowSurface(HWND window, EGLConfig config)
239{
240 const egl::Config *configuration = mConfigSet.get(config);
241
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242 D3DPRESENT_PARAMETERS presentParameters = {0};
243
244 presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat;
245 presentParameters.BackBufferCount = 1;
246 presentParameters.BackBufferFormat = configuration->mRenderTargetFormat;
247 presentParameters.BackBufferWidth = 0;
248 presentParameters.BackBufferHeight = 0;
249 presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE;
250 presentParameters.Flags = 0;
251 presentParameters.hDeviceWindow = window;
252 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
253 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
254 presentParameters.PresentationInterval = configuration->mMinSwapInterval;
255 presentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
256 presentParameters.Windowed = TRUE; // FIXME
257
258 IDirect3DSwapChain9 *swapChain = NULL;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000259 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260
261 if (!mDevice)
262 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000263 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
264
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000265 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
267 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
268 {
269 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
270 }
271
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000272 if (FAILED(result))
273 {
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000274 result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000275
276 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
277 {
278 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
279 }
280 }
281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282 ASSERT(SUCCEEDED(result));
283
284 if (mDevice)
285 {
286 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 {
330 ERR("Could not resent presentation parameters for device. Out of memory.");
331 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
332 }
333
334 ASSERT(SUCCEEDED(result));
335
336 if (mDevice)
337 {
338 mDevice->GetSwapChain(0, &swapChain);
339 mDevice->GetDepthStencilSurface(&depthStencilSurface);
340 }
341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 }
343
344 Surface *surface = NULL;
345
346 if (swapChain)
347 {
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000348 surface = new Surface(this, swapChain, depthStencilSurface, configuration->mConfigID);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 mSurfaceSet.insert(surface);
350
351 swapChain->Release();
352 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 return surface;
355}
356
357EGLContext Display::createContext(EGLConfig configHandle)
358{
359 const egl::Config *config = mConfigSet.get(configHandle);
360
361 gl::Context *context = glCreateContext(config);
362 mContextSet.insert(context);
363
364 return context;
365}
366
367void Display::destroySurface(egl::Surface *surface)
368{
369 delete surface;
370 mSurfaceSet.erase(surface);
371}
372
373void Display::destroyContext(gl::Context *context)
374{
375 glDestroyContext(context);
376 mContextSet.erase(context);
377}
378
379bool Display::isInitialized()
380{
381 return mD3d9 != NULL && mConfigSet.size() > 0;
382}
383
384bool Display::isValidConfig(EGLConfig config)
385{
386 return mConfigSet.get(config) != NULL;
387}
388
389bool Display::isValidContext(gl::Context *context)
390{
391 return mContextSet.find(context) != mContextSet.end();
392}
393
394bool Display::isValidSurface(egl::Surface *surface)
395{
396 return mSurfaceSet.find(surface) != mSurfaceSet.end();
397}
398
399bool Display::hasExistingWindowSurface(HWND window)
400{
401 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
402 {
403 if ((*surface)->getWindowHandle() == window)
404 {
405 return true;
406 }
407 }
408
409 return false;
410}
411
412IDirect3DDevice9 *Display::getDevice()
413{
414 return mDevice;
415}
416}