blob: 2785e590915b540bca8892010ff6fe979eb13f78 [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.com741a5882010-05-20 19:17:29 +000020#define REF_RAST 0 // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
21
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace egl
23{
24Display::Display(HDC deviceContext) : mDc(deviceContext)
25{
26 mD3d9 = NULL;
27 mDevice = NULL;
daniel@transgaming.comda6e2632010-07-28 19:21:18 +000028 mDeviceWindow = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029
30 mAdapter = D3DADAPTER_DEFAULT;
daniel@transgaming.com741a5882010-05-20 19:17:29 +000031
daniel@transgaming.com8b9f4cc2010-05-20 19:17:35 +000032 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
daniel@transgaming.com741a5882010-05-20 19:17:29 +000033 mDeviceType = D3DDEVTYPE_REF;
34 #else
35 mDeviceType = D3DDEVTYPE_HAL;
36 #endif
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +000037
38 mMinSwapInterval = 1;
39 mMaxSwapInterval = 1;
40 setSwapInterval(1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041}
42
43Display::~Display()
44{
45 terminate();
46}
47
48bool Display::initialize()
49{
50 if (isInitialized())
51 {
52 return true;
53 }
54
55 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
56
57 if (mD3d9)
58 {
59 if (mDc != NULL)
60 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000061 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062 }
63
daniel@transgaming.com353569a2010-06-24 13:02:12 +000064 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000065
66 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
67 {
68 return error(EGL_BAD_ALLOC, false);
69 }
70
daniel@transgaming.com353569a2010-06-24 13:02:12 +000071 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000072 {
73 mD3d9->Release();
74 mD3d9 = NULL;
75 }
76 else
77 {
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +000078 mMinSwapInterval = 4;
79 mMaxSwapInterval = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080
daniel@transgaming.com353569a2010-06-24 13:02:12 +000081 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
82 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
83 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
84 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
85 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086
daniel@transgaming.comadf02842010-05-07 13:03:33 +000087 const D3DFORMAT renderTargetFormats[] =
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088 {
89 D3DFMT_A1R5G5B5,
daniel@transgaming.comadf02842010-05-07 13:03:33 +000090 // 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 +000091 D3DFMT_A8R8G8B8,
92 D3DFMT_R5G6B5,
93 D3DFMT_X1R5G5B5,
94 D3DFMT_X8R8G8B8
95 };
96
97 const D3DFORMAT depthStencilFormats[] =
98 {
daniel@transgaming.com02bc1592010-03-30 03:36:13 +000099 // D3DFMT_D16_LOCKABLE,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100 D3DFMT_D32,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +0000101 // D3DFMT_D15S1,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 D3DFMT_D24S8,
103 D3DFMT_D24X8,
daniel@transgaming.comd1f6fde2010-05-07 19:01:43 +0000104 // D3DFMT_D24X4S4,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105 D3DFMT_D16,
daniel@transgaming.com02bc1592010-03-30 03:36:13 +0000106 // D3DFMT_D32F_LOCKABLE,
107 // D3DFMT_D24FS8
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108 };
109
110 D3DDISPLAYMODE currentDisplayMode;
111 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
112
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000113 ConfigSet configSet;
114
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000115 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000116 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000117 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118
119 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
120
121 if (SUCCEEDED(result))
122 {
123 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
124 {
125 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
126 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
127
128 if (SUCCEEDED(result))
129 {
daniel@transgaming.comadf02842010-05-07 13:03:33 +0000130 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000131
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132 if (SUCCEEDED(result))
133 {
134 // FIXME: Enumerate multi-sampling
135
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000136 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 }
138 }
139 }
140 }
141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000143 // Give the sorted configs a unique ID and store them internally
144 EGLint index = 1;
145 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
146 {
147 Config configuration = *config;
148 configuration.mConfigID = index;
149 index++;
150
151 mConfigSet.mSet.insert(configuration);
152 }
153 }
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000154
155 if (!createDevice())
156 {
157 terminate();
158
159 return false;
160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161 }
162
163 if (!isInitialized())
164 {
165 terminate();
166
167 return false;
168 }
169
170 return true;
171}
172
173void Display::terminate()
174{
175 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
176 {
177 delete *surface;
178 }
179
180 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
181 {
182 glDestroyContext(*context);
183 }
184
185 if (mDevice)
186 {
187 mDevice->Release();
188 mDevice = NULL;
189 }
190
191 if (mD3d9)
192 {
193 mD3d9->Release();
194 mD3d9 = NULL;
195 }
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000196
197 if (mDeviceWindow)
198 {
199 DestroyWindow(mDeviceWindow);
200 mDeviceWindow = NULL;
201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202}
203
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000204void Display::startScene()
205{
206 if (!mSceneStarted)
207 {
208 long result = mDevice->BeginScene();
209 ASSERT(SUCCEEDED(result));
210 mSceneStarted = true;
211 }
212}
213
214void Display::endScene()
215{
216 if (mSceneStarted)
217 {
218 long result = mDevice->EndScene();
219 ASSERT(SUCCEEDED(result));
220 mSceneStarted = false;
221 }
222}
223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
225{
226 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
227}
228
229bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
230{
231 const egl::Config *configuration = mConfigSet.get(config);
232
233 switch (attribute)
234 {
235 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
236 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
237 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
238 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
239 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
240 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
241 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
242 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
243 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
244 case EGL_LEVEL: *value = configuration->mLevel; break;
245 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
246 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
247 case EGL_SAMPLES: *value = configuration->mSamples; break;
248 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
249 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
250 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
251 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
252 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
253 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
254 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
255 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
256 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
257 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
258 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
259 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
260 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
261 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000262 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000263 case EGL_CONFORMANT: *value = configuration->mConformant; break;
264 default:
265 return false;
266 }
267
268 return true;
269}
270
daniel@transgaming.com37fa3c82010-07-28 19:21:21 +0000271bool Display::createDevice()
272{
273 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
274 static const TCHAR className[] = TEXT("STATIC");
275
276 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
277
278 D3DPRESENT_PARAMETERS presentParameters = {0};
279
280 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
281 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
282 presentParameters.BackBufferCount = 1;
283 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
284 presentParameters.BackBufferWidth = 1;
285 presentParameters.BackBufferHeight = 1;
286 presentParameters.EnableAutoDepthStencil = FALSE;
287 presentParameters.Flags = 0;
288 presentParameters.hDeviceWindow = mDeviceWindow;
289 presentParameters.MultiSampleQuality = 0;
290 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
291 presentParameters.PresentationInterval = convertInterval(mMinSwapInterval);
292 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
293 presentParameters.Windowed = TRUE;
294
295 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
296
297 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
298
299 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
300 {
301 return error(EGL_BAD_ALLOC, false);
302 }
303
304 if (FAILED(result))
305 {
306 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
307
308 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
309 {
310 return error(EGL_BAD_ALLOC, false);
311 }
312 }
313
314 ASSERT(SUCCEEDED(result));
315
316 // Permanent non-default states
317 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
318
319 mSceneStarted = false;
320
321 return true;
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000322}
323
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000324Surface *Display::createWindowSurface(HWND window, EGLConfig config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000326 const Config *configuration = mConfigSet.get(config);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000328 Surface *surface = new Surface(this, configuration, window);
329 mSurfaceSet.insert(surface);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331 return surface;
332}
333
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000334EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335{
336 const egl::Config *config = mConfigSet.get(configHandle);
337
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000338 gl::Context *context = glCreateContext(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339 mContextSet.insert(context);
340
341 return context;
342}
343
344void Display::destroySurface(egl::Surface *surface)
345{
346 delete surface;
347 mSurfaceSet.erase(surface);
348}
349
350void Display::destroyContext(gl::Context *context)
351{
352 glDestroyContext(context);
353 mContextSet.erase(context);
354}
355
356bool Display::isInitialized()
357{
358 return mD3d9 != NULL && mConfigSet.size() > 0;
359}
360
361bool Display::isValidConfig(EGLConfig config)
362{
363 return mConfigSet.get(config) != NULL;
364}
365
366bool Display::isValidContext(gl::Context *context)
367{
368 return mContextSet.find(context) != mContextSet.end();
369}
370
371bool Display::isValidSurface(egl::Surface *surface)
372{
373 return mSurfaceSet.find(surface) != mSurfaceSet.end();
374}
375
376bool Display::hasExistingWindowSurface(HWND window)
377{
378 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
379 {
380 if ((*surface)->getWindowHandle() == window)
381 {
382 return true;
383 }
384 }
385
386 return false;
387}
388
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000389void Display::setSwapInterval(GLint interval)
390{
391 mSwapInterval = interval;
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000392 mSwapInterval = std::max(mSwapInterval, mMinSwapInterval);
393 mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval);
394
395 mPresentInterval = convertInterval(mSwapInterval);
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000396}
397
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000398DWORD Display::getPresentInterval()
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000399{
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000400 return mPresentInterval;
401}
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000402
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000403DWORD Display::convertInterval(GLint interval)
404{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000405 switch(interval)
406 {
407 case 0: return D3DPRESENT_INTERVAL_IMMEDIATE;
408 case 1: return D3DPRESENT_INTERVAL_ONE;
409 case 2: return D3DPRESENT_INTERVAL_TWO;
410 case 3: return D3DPRESENT_INTERVAL_THREE;
411 case 4: return D3DPRESENT_INTERVAL_FOUR;
412 default: UNREACHABLE();
413 }
414
415 return D3DPRESENT_INTERVAL_DEFAULT;
416}
417
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418IDirect3DDevice9 *Display::getDevice()
419{
420 return mDevice;
421}
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000422
423D3DCAPS9 Display::getDeviceCaps()
424{
425 return mDeviceCaps;
426}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427}