blob: 89eb5850167232f8c8150b9126bdd1fa25985c74 [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,
79 D3DFMT_A2R10G10B10,
80 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 UINT adapter = D3DADAPTER_DEFAULT;
242 D3DDEVTYPE deviceType = D3DDEVTYPE_HAL;
243 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
244
245 HRESULT result = mD3d9->CreateDevice(adapter, deviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246
247 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
248 {
249 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
250 }
251
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000252 if (FAILED(result))
253 {
254 result = mD3d9->CreateDevice(adapter, deviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
255
256 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
257 {
258 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
259 }
260 }
261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262 ASSERT(SUCCEEDED(result));
263
264 if (mDevice)
265 {
266 mDevice->GetSwapChain(0, &swapChain);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000267 mDevice->GetDepthStencilSurface(&depthStencilSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 }
269 }
270 else
271 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000272 if (!mSurfaceSet.empty())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000274 // if the device already exists, and there are other surfaces/windows currently in use, we need to create
275 // a separate swap chain for the new draw surface.
276 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000278 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
279 {
280 ERR("Could not create additional swap chains. Out of memory.");
281 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
282 }
283
284 ASSERT(SUCCEEDED(result));
285
286 // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike
287 // CreateDevice, so we must do so explicitly.
288 result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight,
289 presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType,
290 presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL);
291
292 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
293 {
294 swapChain->Release();
295 ERR("Could not create depthstencil surface for new swap chain. Out of memory.");
296 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
297 }
298
299 ASSERT(SUCCEEDED(result));
300 }
301 else
302 {
303 // if the device already exists, but there are no surfaces in use, then all the surfaces/windows
304 // have been destroyed, and we should repurpose the originally created depthstencil surface for
305 // use with the new surface we are creating.
306 HRESULT result = mDevice->Reset(&presentParameters);
307
308 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
309 {
310 ERR("Could not resent presentation parameters for device. Out of memory.");
311 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
312 }
313
314 ASSERT(SUCCEEDED(result));
315
316 if (mDevice)
317 {
318 mDevice->GetSwapChain(0, &swapChain);
319 mDevice->GetDepthStencilSurface(&depthStencilSurface);
320 }
321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322 }
323
324 Surface *surface = NULL;
325
326 if (swapChain)
327 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000328 surface = new Surface(mDevice, swapChain, depthStencilSurface, configuration->mConfigID);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329 mSurfaceSet.insert(surface);
330
331 swapChain->Release();
332 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000333
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334 return surface;
335}
336
337EGLContext Display::createContext(EGLConfig configHandle)
338{
339 const egl::Config *config = mConfigSet.get(configHandle);
340
341 gl::Context *context = glCreateContext(config);
342 mContextSet.insert(context);
343
344 return context;
345}
346
347void Display::destroySurface(egl::Surface *surface)
348{
349 delete surface;
350 mSurfaceSet.erase(surface);
351}
352
353void Display::destroyContext(gl::Context *context)
354{
355 glDestroyContext(context);
356 mContextSet.erase(context);
357}
358
359bool Display::isInitialized()
360{
361 return mD3d9 != NULL && mConfigSet.size() > 0;
362}
363
364bool Display::isValidConfig(EGLConfig config)
365{
366 return mConfigSet.get(config) != NULL;
367}
368
369bool Display::isValidContext(gl::Context *context)
370{
371 return mContextSet.find(context) != mContextSet.end();
372}
373
374bool Display::isValidSurface(egl::Surface *surface)
375{
376 return mSurfaceSet.find(surface) != mSurfaceSet.end();
377}
378
379bool Display::hasExistingWindowSurface(HWND window)
380{
381 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
382 {
383 if ((*surface)->getWindowHandle() == window)
384 {
385 return true;
386 }
387 }
388
389 return false;
390}
391
392IDirect3DDevice9 *Display::getDevice()
393{
394 return mDevice;
395}
396}