blob: 6276452ba5734d35c238e6231fcfbffff4c200bb [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
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000169void Display::startScene()
170{
171 if (!mSceneStarted)
172 {
173 long result = mDevice->BeginScene();
174 ASSERT(SUCCEEDED(result));
175 mSceneStarted = true;
176 }
177}
178
179void Display::endScene()
180{
181 if (mSceneStarted)
182 {
183 long result = mDevice->EndScene();
184 ASSERT(SUCCEEDED(result));
185 mSceneStarted = false;
186 }
187}
188
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
190{
191 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
192}
193
194bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
195{
196 const egl::Config *configuration = mConfigSet.get(config);
197
198 switch (attribute)
199 {
200 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
201 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
202 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
203 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
204 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
205 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
206 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
207 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
208 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
209 case EGL_LEVEL: *value = configuration->mLevel; break;
210 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
211 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
212 case EGL_SAMPLES: *value = configuration->mSamples; break;
213 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
214 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
215 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
216 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
217 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
218 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
219 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
220 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
221 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
222 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
223 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
224 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
225 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
226 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
227 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
228 case EGL_CONFORMANT: *value = configuration->mConformant; break;
229 default:
230 return false;
231 }
232
233 return true;
234}
235
236egl::Surface *Display::createWindowSurface(HWND window, EGLConfig config)
237{
238 const egl::Config *configuration = mConfigSet.get(config);
239
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240 D3DPRESENT_PARAMETERS presentParameters = {0};
241
242 presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat;
243 presentParameters.BackBufferCount = 1;
244 presentParameters.BackBufferFormat = configuration->mRenderTargetFormat;
245 presentParameters.BackBufferWidth = 0;
246 presentParameters.BackBufferHeight = 0;
247 presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE;
248 presentParameters.Flags = 0;
249 presentParameters.hDeviceWindow = window;
250 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
251 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
252 presentParameters.PresentationInterval = configuration->mMinSwapInterval;
253 presentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
254 presentParameters.Windowed = TRUE; // FIXME
255
256 IDirect3DSwapChain9 *swapChain = NULL;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000257 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
259 if (!mDevice)
260 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000261 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
262
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000263 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264
265 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
266 {
267 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
268 }
269
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000270 if (FAILED(result))
271 {
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000272 result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000273
274 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
275 {
276 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
277 }
278 }
279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280 ASSERT(SUCCEEDED(result));
281
282 if (mDevice)
283 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000284 mSceneStarted = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285 mDevice->GetSwapChain(0, &swapChain);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000286 mDevice->GetDepthStencilSurface(&depthStencilSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 }
288 }
289 else
290 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000291 if (!mSurfaceSet.empty())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000293 // if the device already exists, and there are other surfaces/windows currently in use, we need to create
294 // a separate swap chain for the new draw surface.
295 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000297 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
298 {
299 ERR("Could not create additional swap chains. Out of memory.");
300 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
301 }
302
303 ASSERT(SUCCEEDED(result));
304
305 // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike
306 // CreateDevice, so we must do so explicitly.
307 result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight,
308 presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType,
309 presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL);
310
311 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
312 {
313 swapChain->Release();
314 ERR("Could not create depthstencil surface for new swap chain. Out of memory.");
315 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
316 }
317
318 ASSERT(SUCCEEDED(result));
319 }
320 else
321 {
322 // if the device already exists, but there are no surfaces in use, then all the surfaces/windows
323 // have been destroyed, and we should repurpose the originally created depthstencil surface for
324 // use with the new surface we are creating.
325 HRESULT result = mDevice->Reset(&presentParameters);
326
327 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
328 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000329 ERR("Could not reset presentation parameters for device. Out of memory.");
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000330 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
331 }
332
333 ASSERT(SUCCEEDED(result));
334
335 if (mDevice)
336 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000337 mSceneStarted = false;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000338 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}