blob: d17f71a5a37dc34ac208b5a208c4f70cc681e012 [file] [log] [blame]
daniel@transgaming.come979ead2010-09-23 18:03:14 +00001//
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.come979ead2010-09-23 18:03:14 +00003// 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
11#include "libEGL/Display.h"
12
13#include <algorithm>
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000014#include <map>
daniel@transgaming.come979ead2010-09-23 18:03:14 +000015#include <vector>
16
17#include "common/debug.h"
jbauman@chromium.org06d7a752011-04-30 01:02:52 +000018#include "libGLESv2/mathutil.h"
daniel@transgaming.come979ead2010-09-23 18:03:14 +000019
20#include "libEGL/main.h"
21
baustin@google.com250f06c2011-06-06 16:59:53 +000022// Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
23#define REF_RAST 0
24
25// The "Debug This Pixel..." feature in PIX often fails when using the
26// D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7
27// machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file.
28#if !defined(ANGLE_ENABLE_D3D9EX)
29// Enables use of the IDirect3D9Ex interface, when available
30#define ANGLE_ENABLE_D3D9EX 1
31#endif // !defined(ANGLE_ENABLE_D3D9EX)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000032
33namespace egl
34{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000035namespace
36{
37 typedef std::map<EGLNativeDisplayType, Display*> DisplayMap;
38 DisplayMap displays;
39}
40
41egl::Display *Display::getDisplay(EGLNativeDisplayType displayId)
42{
43 if (displays.find(displayId) != displays.end())
44 {
45 return displays[displayId];
46 }
47
48 egl::Display *display = NULL;
49
50 if (displayId == EGL_DEFAULT_DISPLAY)
51 {
52 display = new egl::Display(displayId, (HDC)NULL, false);
53 }
54 else if (displayId == EGL_SOFTWARE_DISPLAY_ANGLE)
55 {
56 display = new egl::Display(displayId, (HDC)NULL, true);
57 }
58 else
59 {
60 // FIXME: Check if displayId is a valid display device context
61
62 display = new egl::Display(displayId, (HDC)displayId, false);
63 }
64
65 displays[displayId] = display;
66 return display;
67}
68
69Display::Display(EGLNativeDisplayType displayId, HDC deviceContext, bool software) : mDc(deviceContext)
daniel@transgaming.come979ead2010-09-23 18:03:14 +000070{
71 mD3d9Module = NULL;
72
73 mD3d9 = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000074 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000075 mDevice = NULL;
apatrick@chromium.org9e83b592011-02-10 22:04:34 +000076 mDeviceEx = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000077 mDeviceWindow = NULL;
78
79 mAdapter = D3DADAPTER_DEFAULT;
80
81 #if REF_RAST == 1 || defined(FORCE_REF_RAST)
82 mDeviceType = D3DDEVTYPE_REF;
83 #else
84 mDeviceType = D3DDEVTYPE_HAL;
85 #endif
86
87 mMinSwapInterval = 1;
88 mMaxSwapInterval = 1;
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000089 mSoftwareDevice = software;
90 mDisplayId = displayId;
daniel@transgaming.come979ead2010-09-23 18:03:14 +000091}
92
93Display::~Display()
94{
95 terminate();
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +000096
97 DisplayMap::iterator thisDisplay = displays.find(mDisplayId);
98
99 if (thisDisplay != displays.end())
100 {
101 displays.erase(thisDisplay);
102 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000103}
104
105bool Display::initialize()
106{
107 if (isInitialized())
108 {
109 return true;
110 }
111
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000112 if (mSoftwareDevice)
113 {
114 mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
115 }
116 else
117 {
118 mD3d9Module = GetModuleHandle(TEXT("d3d9.dll"));
119 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000120 if (mD3d9Module == NULL)
121 {
122 terminate();
123 return false;
124 }
125
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000126 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
127 Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
128
129 // Use Direct3D9Ex if available. Among other things, this version is less
130 // inclined to report a lost context, for example when the user switches
131 // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
baustin@google.com250f06c2011-06-06 16:59:53 +0000132 if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex)))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000133 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000134 ASSERT(mD3d9Ex);
135 mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000136 ASSERT(mD3d9);
137 }
138 else
139 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000140 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000141 }
142
143 if (mD3d9)
144 {
145 if (mDc != NULL)
146 {
147 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
148 }
149
150 HRESULT result;
151
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000152 // Give up on getting device caps after about one second.
153 for (int i = 0; i < 10; ++i)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000154 {
155 result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000156
157 if (SUCCEEDED(result))
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000158 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000159 break;
160 }
161 else if (result == D3DERR_NOTAVAILABLE)
162 {
163 Sleep(100); // Give the driver some time to initialize/recover
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000164 }
165 else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from
166 {
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000167 terminate();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000168 return error(EGL_BAD_ALLOC, false);
169 }
170 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000171
172 if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
173 {
174 terminate();
175 return error(EGL_NOT_INITIALIZED, false);
176 }
177
apatrick@chromium.org1c768012010-10-07 00:35:24 +0000178 // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported.
179 // This is required by Texture2D::convertToRenderTarget.
180 if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0)
181 {
182 terminate();
183 return error(EGL_NOT_INITIALIZED, false);
184 }
185
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000186 mMinSwapInterval = 4;
187 mMaxSwapInterval = 0;
188
189 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
190 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
191 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
192 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
193 if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
194
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000195 mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier);
196
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000197 const D3DFORMAT renderTargetFormats[] =
198 {
199 D3DFMT_A1R5G5B5,
200 // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
201 D3DFMT_A8R8G8B8,
202 D3DFMT_R5G6B5,
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000203 // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000204 D3DFMT_X8R8G8B8
205 };
206
207 const D3DFORMAT depthStencilFormats[] =
208 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000209 D3DFMT_UNKNOWN,
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000210 // D3DFMT_D16_LOCKABLE,
211 D3DFMT_D32,
212 // D3DFMT_D15S1,
213 D3DFMT_D24S8,
214 D3DFMT_D24X8,
215 // D3DFMT_D24X4S4,
216 D3DFMT_D16,
217 // D3DFMT_D32F_LOCKABLE,
218 // D3DFMT_D24FS8
219 };
220
221 D3DDISPLAYMODE currentDisplayMode;
222 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
223
224 ConfigSet configSet;
225
226 for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
227 {
228 D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
229
230 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
231
232 if (SUCCEEDED(result))
233 {
234 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
235 {
236 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000237 HRESULT result = D3D_OK;
238
239 if(depthStencilFormat != D3DFMT_UNKNOWN)
240 {
241 result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
242 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000243
244 if (SUCCEEDED(result))
245 {
daniel@transgaming.coma114c272011-04-22 04:18:50 +0000246 if(depthStencilFormat != D3DFMT_UNKNOWN)
247 {
248 result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
249 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000250
251 if (SUCCEEDED(result))
252 {
253 // FIXME: enumerate multi-sampling
254
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000255 configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0,
256 mDeviceCaps.MaxTextureWidth, mDeviceCaps.MaxTextureHeight);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000257 }
258 }
259 }
260 }
261 }
262
263 // Give the sorted configs a unique ID and store them internally
264 EGLint index = 1;
265 for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
266 {
267 Config configuration = *config;
268 configuration.mConfigID = index;
269 index++;
270
271 mConfigSet.mSet.insert(configuration);
272 }
273 }
274
275 if (!isInitialized())
276 {
277 terminate();
278
279 return false;
280 }
281
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000282 initExtensionString();
283
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000284 static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
285 static const TCHAR className[] = TEXT("STATIC");
286
287 mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
288
289 return true;
290}
291
292void Display::terminate()
293{
294 while (!mSurfaceSet.empty())
295 {
296 destroySurface(*mSurfaceSet.begin());
297 }
298
299 while (!mContextSet.empty())
300 {
301 destroyContext(*mContextSet.begin());
302 }
303
304 if (mDevice)
305 {
306 // If the device is lost, reset it first to prevent leaving the driver in an unstable state
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000307 if (isDeviceLost())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000308 {
309 resetDevice();
310 }
311
312 mDevice->Release();
313 mDevice = NULL;
314 }
315
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000316 if (mDeviceEx)
317 {
318 mDeviceEx->Release();
319 mDeviceEx = NULL;
320 }
321
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000322 if (mD3d9)
323 {
324 mD3d9->Release();
325 mD3d9 = NULL;
326 }
327
328 if (mDeviceWindow)
329 {
330 DestroyWindow(mDeviceWindow);
331 mDeviceWindow = NULL;
332 }
333
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000334 if (mD3d9Ex)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000335 {
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000336 mD3d9Ex->Release();
337 mD3d9Ex = NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000338 }
339
340 if (mD3d9Module)
341 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000342 mD3d9Module = NULL;
343 }
344}
345
346void Display::startScene()
347{
348 if (!mSceneStarted)
349 {
350 long result = mDevice->BeginScene();
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000351 if (SUCCEEDED(result)) {
352 // This is defensive checking against the device being
353 // lost at unexpected times.
354 mSceneStarted = true;
355 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000356 }
357}
358
359void Display::endScene()
360{
361 if (mSceneStarted)
362 {
kbr@chromium.org1a2cd262011-07-08 17:30:18 +0000363 // EndScene can fail if the device was lost, for example due
364 // to a TDR during a draw call.
365 mDevice->EndScene();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000366 mSceneStarted = false;
367 }
368}
369
370bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
371{
372 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
373}
374
375bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
376{
377 const egl::Config *configuration = mConfigSet.get(config);
378
379 switch (attribute)
380 {
381 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
382 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
383 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
384 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
385 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
386 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
387 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
388 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
389 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
390 case EGL_LEVEL: *value = configuration->mLevel; break;
391 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
392 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
393 case EGL_SAMPLES: *value = configuration->mSamples; break;
394 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
395 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
396 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
397 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
398 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
399 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
400 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
401 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
402 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
403 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
404 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
405 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
406 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
407 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
408 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
409 case EGL_CONFORMANT: *value = configuration->mConformant; break;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000410 case EGL_MAX_PBUFFER_WIDTH: *value = configuration->mMaxPBufferWidth; break;
411 case EGL_MAX_PBUFFER_HEIGHT: *value = configuration->mMaxPBufferHeight; break;
412 case EGL_MAX_PBUFFER_PIXELS: *value = configuration->mMaxPBufferPixels; break;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000413 default:
414 return false;
415 }
416
417 return true;
418}
419
420bool Display::createDevice()
421{
422 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
423 DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
424
425 HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
426
427 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
428 {
429 return error(EGL_BAD_ALLOC, false);
430 }
431
432 if (FAILED(result))
433 {
434 result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
435
436 if (FAILED(result))
437 {
438 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
439 return error(EGL_BAD_ALLOC, false);
440 }
441 }
442
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000443 if (mD3d9Ex)
444 {
445 result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx);
446 ASSERT(SUCCEEDED(result));
447 }
448
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000449 // Permanent non-default states
450 mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
451
452 mSceneStarted = false;
453
454 return true;
455}
456
457bool Display::resetDevice()
458{
459 D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters();
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000460 HRESULT result = mDevice->TestCooperativeLevel();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000461
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000462 while (result == D3DERR_DEVICELOST)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000463 {
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000464 Sleep(100); // Give the graphics driver some CPU time
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000465
daniel@transgaming.com94910c92011-11-09 17:44:15 +0000466 result = mDevice->TestCooperativeLevel();
467 }
468
469 if (result == D3DERR_DEVICENOTRESET)
470 {
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000471 result = mDevice->Reset(&presentParameters);
472 }
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000473
474 if (FAILED(result))
475 {
476 return error(EGL_BAD_ALLOC, false);
477 }
478
479 ASSERT(SUCCEEDED(result));
480
481 return true;
482}
483
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000484EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000485{
486 const Config *configuration = mConfigSet.get(config);
487
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000488 if (attribList)
489 {
490 while (*attribList != EGL_NONE)
491 {
492 switch (attribList[0])
493 {
494 case EGL_RENDER_BUFFER:
495 switch (attribList[1])
496 {
497 case EGL_BACK_BUFFER:
498 break;
499 case EGL_SINGLE_BUFFER:
500 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
501 default:
502 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
503 }
504 break;
505 case EGL_VG_COLORSPACE:
506 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
507 case EGL_VG_ALPHA_FORMAT:
508 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
509 default:
510 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
511 }
512
513 attribList += 2;
514 }
515 }
516
517 if (hasExistingWindowSurface(window))
518 {
519 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
520 }
521
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000522 if (isDeviceLost()) {
523 if (!restoreLostDevice())
524 return EGL_NO_SURFACE;
525 }
526
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000527 Surface *surface = new Surface(this, configuration, window);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000528
529 if (!surface->initialize())
530 {
531 delete surface;
532 return EGL_NO_SURFACE;
533 }
534
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000535 mSurfaceSet.insert(surface);
536
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000537 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000538}
539
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000540EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000541{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000542 EGLint width = 0, height = 0;
543 EGLenum textureFormat = EGL_NO_TEXTURE;
544 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000545 const Config *configuration = mConfigSet.get(config);
546
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000547 if (attribList)
548 {
549 while (*attribList != EGL_NONE)
550 {
551 switch (attribList[0])
552 {
553 case EGL_WIDTH:
554 width = attribList[1];
555 break;
556 case EGL_HEIGHT:
557 height = attribList[1];
558 break;
559 case EGL_LARGEST_PBUFFER:
560 if (attribList[1] != EGL_FALSE)
561 UNIMPLEMENTED(); // FIXME
562 break;
563 case EGL_TEXTURE_FORMAT:
564 switch (attribList[1])
565 {
566 case EGL_NO_TEXTURE:
567 case EGL_TEXTURE_RGB:
568 case EGL_TEXTURE_RGBA:
569 textureFormat = attribList[1];
570 break;
571 default:
572 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
573 }
574 break;
575 case EGL_TEXTURE_TARGET:
576 switch (attribList[1])
577 {
578 case EGL_NO_TEXTURE:
579 case EGL_TEXTURE_2D:
580 textureTarget = attribList[1];
581 break;
582 default:
583 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
584 }
585 break;
586 case EGL_MIPMAP_TEXTURE:
587 if (attribList[1] != EGL_FALSE)
588 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
589 break;
590 case EGL_VG_COLORSPACE:
591 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
592 case EGL_VG_ALPHA_FORMAT:
593 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
594 default:
595 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
596 }
597
598 attribList += 2;
599 }
600 }
601
602 if (width < 0 || height < 0)
603 {
604 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
605 }
606
607 if (width == 0 || height == 0)
608 {
609 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
610 }
611
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000612 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000613 {
614 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
615 }
616
617 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
618 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
619 {
620 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
621 }
622
623 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
624 {
625 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
626 }
627
628 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
629 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
630 {
631 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
632 }
633
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000634 if (isDeviceLost()) {
635 if (!restoreLostDevice())
636 return EGL_NO_SURFACE;
637 }
638
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000639 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
640
641 if (!surface->initialize())
642 {
643 delete surface;
644 return EGL_NO_SURFACE;
645 }
646
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000647 mSurfaceSet.insert(surface);
648
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000649 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000650}
651
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000652EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
653{
654 if (!mDevice)
655 {
656 if (!createDevice())
657 {
658 return NULL;
659 }
660 }
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000661 else if (isDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000662 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000663 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000664 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000665 }
666
667 const egl::Config *config = mConfigSet.get(configHandle);
668
669 gl::Context *context = glCreateContext(config, shareContext);
670 mContextSet.insert(context);
671
672 return context;
673}
674
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000675bool Display::restoreLostDevice()
676{
677 // Release surface resources to make the Reset() succeed
678 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
679 {
680 (*surface)->release();
681 }
682
683 if (!resetDevice())
684 {
685 return false;
686 }
687
688 // Restore any surfaces that may have been lost
689 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
690 {
691 (*surface)->resetSwapChain();
692 }
693
694 return true;
695}
696
697
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000698void Display::destroySurface(egl::Surface *surface)
699{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000700 delete surface;
701 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000702}
703
704void Display::destroyContext(gl::Context *context)
705{
706 glDestroyContext(context);
707 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000708}
709
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000710bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000711{
712 return mD3d9 != NULL && mConfigSet.size() > 0;
713}
714
715bool Display::isValidConfig(EGLConfig config)
716{
717 return mConfigSet.get(config) != NULL;
718}
719
720bool Display::isValidContext(gl::Context *context)
721{
722 return mContextSet.find(context) != mContextSet.end();
723}
724
725bool Display::isValidSurface(egl::Surface *surface)
726{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000727 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000728}
729
730bool Display::hasExistingWindowSurface(HWND window)
731{
732 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
733 {
734 if ((*surface)->getWindowHandle() == window)
735 {
736 return true;
737 }
738 }
739
740 return false;
741}
742
743EGLint Display::getMinSwapInterval()
744{
745 return mMinSwapInterval;
746}
747
748EGLint Display::getMaxSwapInterval()
749{
750 return mMaxSwapInterval;
751}
752
753IDirect3DDevice9 *Display::getDevice()
754{
755 if (!mDevice)
756 {
757 if (!createDevice())
758 {
759 return NULL;
760 }
761 }
762
763 return mDevice;
764}
765
766D3DCAPS9 Display::getDeviceCaps()
767{
768 return mDeviceCaps;
769}
770
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000771D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
772{
773 return &mAdapterIdentifier;
774}
775
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000776bool Display::isDeviceLost()
777{
778 if (mDeviceEx)
779 {
780 return FAILED(mDeviceEx->CheckDeviceState(NULL));
781 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000782 else if(mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000783 {
784 return FAILED(mDevice->TestCooperativeLevel());
785 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000786
787 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000788}
789
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000790void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
791{
792 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
793 {
794 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
795 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
796
797 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
798 }
799}
800
gman@chromium.org50c526d2011-08-10 05:19:44 +0000801bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000802{
803 D3DDISPLAYMODE currentDisplayMode;
804 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
805
806 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
807}
808
gman@chromium.org50c526d2011-08-10 05:19:44 +0000809bool Display::getDXT3TextureSupport()
810{
811 D3DDISPLAYMODE currentDisplayMode;
812 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
813
814 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
815}
816
817bool Display::getDXT5TextureSupport()
818{
819 D3DDISPLAYMODE currentDisplayMode;
820 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
821
822 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
823}
824
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000825bool Display::getFloatTextureSupport(bool *filtering, bool *renderable)
826{
827 D3DDISPLAYMODE currentDisplayMode;
828 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
829
830 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
831 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
832 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
833 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
834
835 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
836 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
837 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
838 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
839
840 if (!filtering && !renderable)
841 {
842 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
843 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
844 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
845 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
846 }
847 else
848 {
849 return true;
850 }
851}
852
853bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable)
854{
855 D3DDISPLAYMODE currentDisplayMode;
856 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
857
858 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
859 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
860 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
861 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
862
863 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
864 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
865 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
866 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
867
868 if (!filtering && !renderable)
869 {
870 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
871 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
872 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
873 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
874 }
875 else
876 {
877 return true;
878 }
879}
880
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000881bool Display::getLuminanceTextureSupport()
882{
883 D3DDISPLAYMODE currentDisplayMode;
884 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
885
886 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
887}
888
889bool Display::getLuminanceAlphaTextureSupport()
890{
891 D3DDISPLAYMODE currentDisplayMode;
892 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
893
894 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
895}
896
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000897D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000898{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000899 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000900 {
901 return D3DPOOL_DEFAULT;
902 }
903 else
904 {
905 if (!(usage & D3DUSAGE_DYNAMIC))
906 {
907 return D3DPOOL_MANAGED;
908 }
909 }
910
911 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000912}
913
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000914bool Display::getEventQuerySupport()
915{
916 IDirect3DQuery9 *query;
917 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
918 if (SUCCEEDED(result))
919 {
920 query->Release();
921 }
922
923 return result != D3DERR_NOTAVAILABLE;
924}
925
926D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
927{
928 D3DPRESENT_PARAMETERS presentParameters = {0};
929
930 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
931 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
932 presentParameters.BackBufferCount = 1;
933 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
934 presentParameters.BackBufferWidth = 1;
935 presentParameters.BackBufferHeight = 1;
936 presentParameters.EnableAutoDepthStencil = FALSE;
937 presentParameters.Flags = 0;
938 presentParameters.hDeviceWindow = mDeviceWindow;
939 presentParameters.MultiSampleQuality = 0;
940 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
941 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
942 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
943 presentParameters.Windowed = TRUE;
944
945 return presentParameters;
946}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000947
948void Display::initExtensionString()
949{
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000950 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +0000951 bool isd3d9ex = isD3d9ExDevice();
952
953 mExtensionString = "";
954
955 if (isd3d9ex) {
956 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
957 }
958
959 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000960
961 if (swiftShader)
962 {
963 mExtensionString += "EGL_ANGLE_software_display ";
964 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000965
daniel@transgaming.com1a1fe242011-09-26 18:25:45 +0000966 if (isd3d9ex) {
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000967 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
968 }
969
970 std::string::size_type end = mExtensionString.find_last_not_of(' ');
971 if (end != std::string::npos)
972 {
973 mExtensionString.resize(end+1);
974 }
975}
976
977const char *Display::getExtensionString() const
978{
979 return mExtensionString.c_str();
980}
981
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000982// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
983// We test this using D3D9 by checking support for the R16F format.
984bool Display::getVertexTextureSupport() const
985{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000986 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
987 {
988 return false;
989 }
990
991 D3DDISPLAYMODE currentDisplayMode;
992 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
993
994 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
995
996 return SUCCEEDED(result);
997}
998
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000999bool Display::getNonPower2TextureSupport() const
1000{
1001 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
1002 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
1003 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
1004}
1005
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +00001006}