blob: d39a5bbabc2b974cd2bb42896aa6c6f08a9a5a4c [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.com616ffcf2010-05-07 19:01:49 +000029
30 mMinSwapInterval = 1;
31 mMaxSwapInterval = 1;
32 setSwapInterval(1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033}
34
35Display::~Display()
36{
37 terminate();
38}
39
40bool Display::initialize()
41{
42 if (isInitialized())
43 {
44 return true;
45 }
46
47 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
48
49 if (mD3d9)
50 {
51 if (mDc != NULL)
52 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000053 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054 }
55
56 D3DCAPS9 caps;
57 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps);
58
59 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
60 {
61 return error(EGL_BAD_ALLOC, false);
62 }
63
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000064 if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065 {
66 mD3d9->Release();
67 mD3d9 = NULL;
68 }
69 else
70 {
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +000071 mMinSwapInterval = 4;
72 mMaxSwapInterval = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000073
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +000074 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
75 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
76 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
77 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
78 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079
daniel@transgaming.comadf02842010-05-07 13:03:33 +000080 const D3DFORMAT renderTargetFormats[] =
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000081 {
82 D3DFMT_A1R5G5B5,
daniel@transgaming.comadf02842010-05-07 13:03:33 +000083 // 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 +000084 D3DFMT_A8R8G8B8,
85 D3DFMT_R5G6B5,
86 D3DFMT_X1R5G5B5,
87 D3DFMT_X8R8G8B8
88 };
89
90 const D3DFORMAT depthStencilFormats[] =
91 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000092 // D3DFMT_D16_LOCKABLE,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093 D3DFMT_D32,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +000094 // D3DFMT_D15S1,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 D3DFMT_D24S8,
96 D3DFMT_D24X8,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +000097 // D3DFMT_D24X4S4,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098 D3DFMT_D16,
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000099 // D3DFMT_D32F_LOCKABLE,
100 // D3DFMT_D24FS8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101 };
102
103 D3DDISPLAYMODE currentDisplayMode;
104 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
105
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000106 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000108 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
110 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
111
112 if (SUCCEEDED(result))
113 {
114 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
115 {
116 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
117 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
118
119 if (SUCCEEDED(result))
120 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000121 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 if (SUCCEEDED(result))
124 {
125 // FIXME: Enumerate multi-sampling
126
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000127 mConfigSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 }
129 }
130 }
131 }
132 }
133 }
134
135 mConfigSet.enumerate();
136 }
137
138 if (!isInitialized())
139 {
140 terminate();
141
142 return false;
143 }
144
145 return true;
146}
147
148void Display::terminate()
149{
150 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
151 {
152 delete *surface;
153 }
154
155 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
156 {
157 glDestroyContext(*context);
158 }
159
160 if (mDevice)
161 {
162 mDevice->Release();
163 mDevice = NULL;
164 }
165
166 if (mD3d9)
167 {
168 mD3d9->Release();
169 mD3d9 = NULL;
170 }
171}
172
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000173void Display::startScene()
174{
175 if (!mSceneStarted)
176 {
177 long result = mDevice->BeginScene();
178 ASSERT(SUCCEEDED(result));
179 mSceneStarted = true;
180 }
181}
182
183void Display::endScene()
184{
185 if (mSceneStarted)
186 {
187 long result = mDevice->EndScene();
188 ASSERT(SUCCEEDED(result));
189 mSceneStarted = false;
190 }
191}
192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
194{
195 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
196}
197
198bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
199{
200 const egl::Config *configuration = mConfigSet.get(config);
201
202 switch (attribute)
203 {
204 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
205 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
206 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
207 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
208 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
209 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
210 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
211 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
212 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
213 case EGL_LEVEL: *value = configuration->mLevel; break;
214 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
215 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
216 case EGL_SAMPLES: *value = configuration->mSamples; break;
217 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
218 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
219 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
220 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
221 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
222 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
223 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
224 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
225 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
226 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
227 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
228 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
229 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
230 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000231 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232 case EGL_CONFORMANT: *value = configuration->mConformant; break;
233 default:
234 return false;
235 }
236
237 return true;
238}
239
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000240Surface *Display::createWindowSurface(HWND window, EGLConfig config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000242 const Config *configuration = mConfigSet.get(config);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244 D3DPRESENT_PARAMETERS presentParameters = {0};
245
246 presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat;
247 presentParameters.BackBufferCount = 1;
248 presentParameters.BackBufferFormat = configuration->mRenderTargetFormat;
249 presentParameters.BackBufferWidth = 0;
250 presentParameters.BackBufferHeight = 0;
251 presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE;
252 presentParameters.Flags = 0;
253 presentParameters.hDeviceWindow = window;
254 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
255 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000256 presentParameters.PresentationInterval = convertInterval(mMinSwapInterval);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257 presentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
258 presentParameters.Windowed = TRUE; // FIXME
259
260 IDirect3DSwapChain9 *swapChain = NULL;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000261 IDirect3DSurface9 *depthStencilSurface = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
263 if (!mDevice)
264 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000265 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
266
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000267 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268
269 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
270 {
271 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
272 }
273
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000274 if (FAILED(result))
275 {
daniel@transgaming.comc28e76b2010-05-05 18:47:16 +0000276 result = mD3d9->CreateDevice(mAdapter, mDeviceType, window, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000277
278 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
279 {
280 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
281 }
282 }
283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284 ASSERT(SUCCEEDED(result));
285
286 if (mDevice)
287 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000288 mSceneStarted = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289 mDevice->GetSwapChain(0, &swapChain);
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000290 mDevice->GetDepthStencilSurface(&depthStencilSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 }
292 }
293 else
294 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000295 if (!mSurfaceSet.empty())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000297 // if the device already exists, and there are other surfaces/windows currently in use, we need to create
298 // a separate swap chain for the new draw surface.
299 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000301 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
302 {
303 ERR("Could not create additional swap chains. Out of memory.");
304 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
305 }
306
307 ASSERT(SUCCEEDED(result));
308
309 // CreateAdditionalSwapChain does not automatically generate a depthstencil surface, unlike
310 // CreateDevice, so we must do so explicitly.
311 result = mDevice->CreateDepthStencilSurface(presentParameters.BackBufferWidth, presentParameters.BackBufferHeight,
312 presentParameters.AutoDepthStencilFormat, presentParameters.MultiSampleType,
313 presentParameters.MultiSampleQuality, FALSE, &depthStencilSurface, NULL);
314
315 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
316 {
317 swapChain->Release();
318 ERR("Could not create depthstencil surface for new swap chain. Out of memory.");
319 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
320 }
321
322 ASSERT(SUCCEEDED(result));
323 }
324 else
325 {
326 // if the device already exists, but there are no surfaces in use, then all the surfaces/windows
327 // have been destroyed, and we should repurpose the originally created depthstencil surface for
328 // use with the new surface we are creating.
329 HRESULT result = mDevice->Reset(&presentParameters);
330
331 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
332 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000333 ERR("Could not reset presentation parameters for device. Out of memory.");
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000334 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
335 }
336
337 ASSERT(SUCCEEDED(result));
338
339 if (mDevice)
340 {
daniel@transgaming.comc9def0b2010-05-05 18:48:06 +0000341 mSceneStarted = false;
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000342 mDevice->GetSwapChain(0, &swapChain);
343 mDevice->GetDepthStencilSurface(&depthStencilSurface);
344 }
345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346 }
347
348 Surface *surface = NULL;
349
350 if (swapChain)
351 {
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000352 surface = new Surface(this, swapChain, depthStencilSurface, configuration);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353 mSurfaceSet.insert(surface);
354
355 swapChain->Release();
356 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000357
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358 return surface;
359}
360
361EGLContext Display::createContext(EGLConfig configHandle)
362{
363 const egl::Config *config = mConfigSet.get(configHandle);
364
365 gl::Context *context = glCreateContext(config);
366 mContextSet.insert(context);
367
368 return context;
369}
370
371void Display::destroySurface(egl::Surface *surface)
372{
373 delete surface;
374 mSurfaceSet.erase(surface);
375}
376
377void Display::destroyContext(gl::Context *context)
378{
379 glDestroyContext(context);
380 mContextSet.erase(context);
381}
382
383bool Display::isInitialized()
384{
385 return mD3d9 != NULL && mConfigSet.size() > 0;
386}
387
388bool Display::isValidConfig(EGLConfig config)
389{
390 return mConfigSet.get(config) != NULL;
391}
392
393bool Display::isValidContext(gl::Context *context)
394{
395 return mContextSet.find(context) != mContextSet.end();
396}
397
398bool Display::isValidSurface(egl::Surface *surface)
399{
400 return mSurfaceSet.find(surface) != mSurfaceSet.end();
401}
402
403bool Display::hasExistingWindowSurface(HWND window)
404{
405 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
406 {
407 if ((*surface)->getWindowHandle() == window)
408 {
409 return true;
410 }
411 }
412
413 return false;
414}
415
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000416void Display::setSwapInterval(GLint interval)
417{
418 mSwapInterval = interval;
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000419 mSwapInterval = std::max(mSwapInterval, mMinSwapInterval);
420 mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval);
421
422 mPresentInterval = convertInterval(mSwapInterval);
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000423}
424
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000425DWORD Display::getPresentInterval()
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000426{
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000427 return mPresentInterval;
428}
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000429
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000430DWORD Display::convertInterval(GLint interval)
431{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000432 switch(interval)
433 {
434 case 0: return D3DPRESENT_INTERVAL_IMMEDIATE;
435 case 1: return D3DPRESENT_INTERVAL_ONE;
436 case 2: return D3DPRESENT_INTERVAL_TWO;
437 case 3: return D3DPRESENT_INTERVAL_THREE;
438 case 4: return D3DPRESENT_INTERVAL_FOUR;
439 default: UNREACHABLE();
440 }
441
442 return D3DPRESENT_INTERVAL_DEFAULT;
443}
444
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445IDirect3DDevice9 *Display::getDevice()
446{
447 return mDevice;
448}
449}