blob: 62d6830a27add93adf8f497c28811697d47261e8 [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 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +000073 terminate();
74 return error(EGL_NOT_INITIALIZED, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075 }
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +000076
77 mMinSwapInterval = 4;
78 mMaxSwapInterval = 0;
79
80 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
81 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
82 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
83 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
84 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
85
86 const D3DFORMAT renderTargetFormats[] =
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +000088 D3DFMT_A1R5G5B5,
89 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
90 D3DFMT_A8R8G8B8,
91 D3DFMT_R5G6B5,
92 D3DFMT_X1R5G5B5,
93 D3DFMT_X8R8G8B8
94 };
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +000096 const D3DFORMAT depthStencilFormats[] =
97 {
98 // D3DFMT_D16_LOCKABLE,
99 D3DFMT_D32,
100 // D3DFMT_D15S1,
101 D3DFMT_D24S8,
102 D3DFMT_D24X8,
103 // D3DFMT_D24X4S4,
104 D3DFMT_D16,
105 // D3DFMT_D32F_LOCKABLE,
106 // D3DFMT_D24FS8
107 };
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000109 D3DDISPLAYMODE currentDisplayMode;
110 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
111
112 ConfigSet configSet;
113
114 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
115 {
116 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
117
118 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
119
120 if (SUCCEEDED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000122 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000124 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
125 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
126
127 if (SUCCEEDED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000129 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
131 if (SUCCEEDED(result))
132 {
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000133 // FIXME: Enumerate multi-sampling
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000134
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000135 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 }
137 }
138 }
139 }
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000142 // Give the sorted configs a unique ID and store them internally
143 EGLint index = 1;
144 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
145 {
146 Config configuration = *config;
147 configuration.mConfigID = index;
148 index++;
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000149
apatrick@chromium.org6ad6ce82010-08-12 00:16:16 +0000150 mConfigSet.mSet.insert(configuration);
daniel@transgaming.com178adff2010-05-18 18:52:04 +0000151 }
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000152
153 if (!createDevice())
154 {
155 terminate();
156
157 return false;
158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159 }
160
161 if (!isInitialized())
162 {
163 terminate();
164
165 return false;
166 }
167
168 return true;
169}
170
171void Display::terminate()
172{
173 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
174 {
175 delete *surface;
176 }
177
178 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
179 {
180 glDestroyContext(*context);
181 }
182
183 if (mDevice)
184 {
185 mDevice->Release();
186 mDevice = NULL;
187 }
188
189 if (mD3d9)
190 {
191 mD3d9->Release();
192 mD3d9 = NULL;
193 }
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000194
195 if (mDeviceWindow)
196 {
197 DestroyWindow(mDeviceWindow);
198 mDeviceWindow = NULL;
199 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200}
201
daniel@transgaming.comae072af2010-05-05 18:47:28 +0000202void Display::startScene()
203{
204 if (!mSceneStarted)
205 {
206 long result = mDevice->BeginScene();
207 ASSERT(SUCCEEDED(result));
208 mSceneStarted = true;
209 }
210}
211
212void Display::endScene()
213{
214 if (mSceneStarted)
215 {
216 long result = mDevice->EndScene();
217 ASSERT(SUCCEEDED(result));
218 mSceneStarted = false;
219 }
220}
221
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
223{
224 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
225}
226
227bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
228{
229 const egl::Config *configuration = mConfigSet.get(config);
230
231 switch (attribute)
232 {
233 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
234 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
235 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
236 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
237 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
238 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
239 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
240 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
241 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
242 case EGL_LEVEL: *value = configuration->mLevel; break;
243 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
244 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
245 case EGL_SAMPLES: *value = configuration->mSamples; break;
246 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
247 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
248 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
249 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
250 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
251 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
252 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
253 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
254 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
255 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
256 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
257 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
258 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
259 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000260 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261 case EGL_CONFORMANT: *value = configuration->mConformant; break;
262 default:
263 return false;
264 }
265
266 return true;
267}
268
daniel@transgaming.com37fa3c82010-07-28 19:21:21 +0000269bool Display::createDevice()
270{
271 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
272 static const TCHAR className[] = TEXT("STATIC");
273
274 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
275
276 D3DPRESENT_PARAMETERS presentParameters = {0};
277
278 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
279 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
280 presentParameters.BackBufferCount = 1;
281 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
282 presentParameters.BackBufferWidth = 1;
283 presentParameters.BackBufferHeight = 1;
284 presentParameters.EnableAutoDepthStencil = FALSE;
285 presentParameters.Flags = 0;
286 presentParameters.hDeviceWindow = mDeviceWindow;
287 presentParameters.MultiSampleQuality = 0;
288 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
289 presentParameters.PresentationInterval = convertInterval(mMinSwapInterval);
290 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
291 presentParameters.Windowed = TRUE;
292
293 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
294
295 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
296
297 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
298 {
299 return error(EGL_BAD_ALLOC, false);
300 }
301
302 if (FAILED(result))
303 {
304 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
305
306 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
307 {
308 return error(EGL_BAD_ALLOC, false);
309 }
310 }
311
312 ASSERT(SUCCEEDED(result));
313
314 // Permanent non-default states
315 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
316
317 mSceneStarted = false;
318
319 return true;
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000320}
321
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000322Surface *Display::createWindowSurface(HWND window, EGLConfig config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000324 const Config *configuration = mConfigSet.get(config);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
daniel@transgaming.comda6e2632010-07-28 19:21:18 +0000326 Surface *surface = new Surface(this, configuration, window);
327 mSurfaceSet.insert(surface);
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000328
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329 return surface;
330}
331
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000332EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333{
334 const egl::Config *config = mConfigSet.get(configHandle);
335
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000336 gl::Context *context = glCreateContext(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337 mContextSet.insert(context);
338
339 return context;
340}
341
342void Display::destroySurface(egl::Surface *surface)
343{
344 delete surface;
345 mSurfaceSet.erase(surface);
346}
347
348void Display::destroyContext(gl::Context *context)
349{
350 glDestroyContext(context);
351 mContextSet.erase(context);
352}
353
354bool Display::isInitialized()
355{
356 return mD3d9 != NULL && mConfigSet.size() > 0;
357}
358
359bool Display::isValidConfig(EGLConfig config)
360{
361 return mConfigSet.get(config) != NULL;
362}
363
364bool Display::isValidContext(gl::Context *context)
365{
366 return mContextSet.find(context) != mContextSet.end();
367}
368
369bool Display::isValidSurface(egl::Surface *surface)
370{
371 return mSurfaceSet.find(surface) != mSurfaceSet.end();
372}
373
374bool Display::hasExistingWindowSurface(HWND window)
375{
376 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
377 {
378 if ((*surface)->getWindowHandle() == window)
379 {
380 return true;
381 }
382 }
383
384 return false;
385}
386
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000387void Display::setSwapInterval(GLint interval)
388{
389 mSwapInterval = interval;
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000390 mSwapInterval = std::max(mSwapInterval, mMinSwapInterval);
391 mSwapInterval = std::min(mSwapInterval, mMaxSwapInterval);
392
393 mPresentInterval = convertInterval(mSwapInterval);
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000394}
395
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000396DWORD Display::getPresentInterval()
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000397{
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000398 return mPresentInterval;
399}
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000400
daniel@transgaming.com616ffcf2010-05-07 19:01:49 +0000401DWORD Display::convertInterval(GLint interval)
402{
daniel@transgaming.comb6bd7272010-05-05 18:48:22 +0000403 switch(interval)
404 {
405 case 0: return D3DPRESENT_INTERVAL_IMMEDIATE;
406 case 1: return D3DPRESENT_INTERVAL_ONE;
407 case 2: return D3DPRESENT_INTERVAL_TWO;
408 case 3: return D3DPRESENT_INTERVAL_THREE;
409 case 4: return D3DPRESENT_INTERVAL_FOUR;
410 default: UNREACHABLE();
411 }
412
413 return D3DPRESENT_INTERVAL_DEFAULT;
414}
415
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416IDirect3DDevice9 *Display::getDevice()
417{
418 return mDevice;
419}
daniel@transgaming.com353569a2010-06-24 13:02:12 +0000420
421D3DCAPS9 Display::getDeviceCaps()
422{
423 return mDeviceCaps;
424}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425}