blob: 2eedcde32e74698dab84dd284864f959eedea219 [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;
29}
30
31Display::~Display()
32{
33 terminate();
34}
35
36bool Display::initialize()
37{
38 if (isInitialized())
39 {
40 return true;
41 }
42
43 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
44
45 if (mD3d9)
46 {
47 if (mDc != NULL)
48 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000049 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 }
51
52 D3DCAPS9 caps;
53 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps);
54
55 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
56 {
57 return error(EGL_BAD_ALLOC, false);
58 }
59
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000060 if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 {
62 mD3d9->Release();
63 mD3d9 = NULL;
64 }
65 else
66 {
67 EGLint minSwapInterval = 4;
68 EGLint maxSwapInterval = 0;
69
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000070 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {minSwapInterval = std::min(minSwapInterval, 0); maxSwapInterval = std::max(maxSwapInterval, 0);}
71 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {minSwapInterval = std::min(minSwapInterval, 1); maxSwapInterval = std::max(maxSwapInterval, 1);}
72 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {minSwapInterval = std::min(minSwapInterval, 2); maxSwapInterval = std::max(maxSwapInterval, 2);}
73 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {minSwapInterval = std::min(minSwapInterval, 3); maxSwapInterval = std::max(maxSwapInterval, 3);}
74 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {minSwapInterval = std::min(minSwapInterval, 4); maxSwapInterval = std::max(maxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075
76 const D3DFORMAT adapterFormats[] =
77 {
78 D3DFMT_A1R5G5B5,
daniel@transgaming.come8c0ca22010-04-20 18:52:47 +000079 //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 +000080 D3DFMT_A8R8G8B8,
81 D3DFMT_R5G6B5,
82 D3DFMT_X1R5G5B5,
83 D3DFMT_X8R8G8B8
84 };
85
86 const D3DFORMAT depthStencilFormats[] =
87 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000088 // D3DFMT_D16_LOCKABLE,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 D3DFMT_D32,
90 D3DFMT_D15S1,
91 D3DFMT_D24S8,
92 D3DFMT_D24X8,
93 D3DFMT_D24X4S4,
94 D3DFMT_D16,
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000095 // D3DFMT_D32F_LOCKABLE,
96 // D3DFMT_D24FS8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097 };
98
99 D3DDISPLAYMODE currentDisplayMode;
100 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
101
102 for (int formatIndex = 0; formatIndex < sizeof(adapterFormats) / sizeof(D3DFORMAT); formatIndex++)
103 {
104 D3DFORMAT renderTargetFormat = adapterFormats[formatIndex];
105
106 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
107
108 if (SUCCEEDED(result))
109 {
110 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
111 {
112 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
113 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
114
115 if (SUCCEEDED(result))
116 {
117 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 +0000118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000119 if (SUCCEEDED(result))
120 {
121 // FIXME: Enumerate multi-sampling
122
123 mConfigSet.add(currentDisplayMode, minSwapInterval, maxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
124 }
125 }
126 }
127 }
128 }
129 }
130
131 mConfigSet.enumerate();
132 }
133
134 if (!isInitialized())
135 {
136 terminate();
137
138 return false;
139 }
140
141 return true;
142}
143
144void Display::terminate()
145{
146 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
147 {
148 delete *surface;
149 }
150
151 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
152 {
153 glDestroyContext(*context);
154 }
155
156 if (mDevice)
157 {
158 mDevice->Release();
159 mDevice = NULL;
160 }
161
162 if (mD3d9)
163 {
164 mD3d9->Release();
165 mD3d9 = NULL;
166 }
167}
168
169bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
170{
171 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
172}
173
174bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
175{
176 const egl::Config *configuration = mConfigSet.get(config);
177
178 switch (attribute)
179 {
180 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
181 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
182 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
183 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
184 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
185 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
186 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
187 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
188 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
189 case EGL_LEVEL: *value = configuration->mLevel; break;
190 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
191 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
192 case EGL_SAMPLES: *value = configuration->mSamples; break;
193 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
194 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
195 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
196 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
197 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
198 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
199 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
200 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
201 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
202 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
203 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
204 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
205 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
206 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
207 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
208 case EGL_CONFORMANT: *value = configuration->mConformant; break;
209 default:
210 return false;
211 }
212
213 return true;
214}
215
216egl::Surface *Display::createWindowSurface(HWND window, EGLConfig config)
217{
218 const egl::Config *configuration = mConfigSet.get(config);
219
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220 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;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000237 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
239 if (!mDevice)
240 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000241 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
242
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000243 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
245 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
246 {
247 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
248 }
249
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000250 if (FAILED(result))
251 {
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000252 result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000253
254 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
255 {
256 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
257 }
258 }
259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260 ASSERT(SUCCEEDED(result));
261
262 if (mDevice)
263 {
264 mDevice->GetSwapChain(0, &swapChain);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000265 mDevice->GetDepthStencilSurface(&depthStencilSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266 }
267 }
268 else
269 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000270 if (!mSurfaceSet.empty())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000272 // if the device already exists, and there are other surfaces/windows currently in use, we need to create
273 // a separate swap chain for the new draw surface.
274 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000276 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
277 {
278 ERR("Could not create additional swap chains. Out of memory.");
279 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
280 }
281
282 ASSERT(SUCCEEDED(result));
283
284 // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike
285 // CreateDevice, so we must do so explicitly.
286 result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight,
287 presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType,
288 presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL);
289
290 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
291 {
292 swapChain->Release();
293 ERR("Could not create depthstencil surface for new swap chain. Out of memory.");
294 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
295 }
296
297 ASSERT(SUCCEEDED(result));
298 }
299 else
300 {
301 // if the device already exists, but there are no surfaces in use, then all the surfaces/windows
302 // have been destroyed, and we should repurpose the originally created depthstencil surface for
303 // use with the new surface we are creating.
304 HRESULT result = mDevice->Reset(&presentParameters);
305
306 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
307 {
308 ERR("Could not resent presentation parameters for device. Out of memory.");
309 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
310 }
311
312 ASSERT(SUCCEEDED(result));
313
314 if (mDevice)
315 {
316 mDevice->GetSwapChain(0, &swapChain);
317 mDevice->GetDepthStencilSurface(&depthStencilSurface);
318 }
319 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320 }
321
322 Surface *surface = NULL;
323
324 if (swapChain)
325 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000326 surface = new Surface(mDevice, swapChain, depthStencilSurface, configuration->mConfigID);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327 mSurfaceSet.insert(surface);
328
329 swapChain->Release();
330 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332 return surface;
333}
334
335EGLContext Display::createContext(EGLConfig configHandle)
336{
337 const egl::Config *config = mConfigSet.get(configHandle);
338
339 gl::Context *context = glCreateContext(config);
340 mContextSet.insert(context);
341
342 return context;
343}
344
345void Display::destroySurface(egl::Surface *surface)
346{
347 delete surface;
348 mSurfaceSet.erase(surface);
349}
350
351void Display::destroyContext(gl::Context *context)
352{
353 glDestroyContext(context);
354 mContextSet.erase(context);
355}
356
357bool Display::isInitialized()
358{
359 return mD3d9 != NULL && mConfigSet.size() > 0;
360}
361
362bool Display::isValidConfig(EGLConfig config)
363{
364 return mConfigSet.get(config) != NULL;
365}
366
367bool Display::isValidContext(gl::Context *context)
368{
369 return mContextSet.find(context) != mContextSet.end();
370}
371
372bool Display::isValidSurface(egl::Surface *surface)
373{
374 return mSurfaceSet.find(surface) != mSurfaceSet.end();
375}
376
377bool Display::hasExistingWindowSurface(HWND window)
378{
379 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
380 {
381 if ((*surface)->getWindowHandle() == window)
382 {
383 return true;
384 }
385 }
386
387 return false;
388}
389
390IDirect3DDevice9 *Display::getDevice()
391{
392 return mDevice;
393}
394}