blob: e031981dedce2ed1a9f413fe54795a180e8b408f [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();
460 HRESULT result;
461
462 do
463 {
464 Sleep(0); // Give the graphics driver some CPU time
465
466 result = mDevice->Reset(&presentParameters);
467 }
468 while (result == D3DERR_DEVICELOST);
469
470 if (FAILED(result))
471 {
472 return error(EGL_BAD_ALLOC, false);
473 }
474
475 ASSERT(SUCCEEDED(result));
476
477 return true;
478}
479
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000480EGLSurface Display::createWindowSurface(HWND window, EGLConfig config, const EGLint *attribList)
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000481{
482 const Config *configuration = mConfigSet.get(config);
483
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000484 if (attribList)
485 {
486 while (*attribList != EGL_NONE)
487 {
488 switch (attribList[0])
489 {
490 case EGL_RENDER_BUFFER:
491 switch (attribList[1])
492 {
493 case EGL_BACK_BUFFER:
494 break;
495 case EGL_SINGLE_BUFFER:
496 return error(EGL_BAD_MATCH, EGL_NO_SURFACE); // Rendering directly to front buffer not supported
497 default:
498 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
499 }
500 break;
501 case EGL_VG_COLORSPACE:
502 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
503 case EGL_VG_ALPHA_FORMAT:
504 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
505 default:
506 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
507 }
508
509 attribList += 2;
510 }
511 }
512
513 if (hasExistingWindowSurface(window))
514 {
515 return error(EGL_BAD_ALLOC, EGL_NO_SURFACE);
516 }
517
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000518 if (isDeviceLost()) {
519 if (!restoreLostDevice())
520 return EGL_NO_SURFACE;
521 }
522
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000523 Surface *surface = new Surface(this, configuration, window);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000524
525 if (!surface->initialize())
526 {
527 delete surface;
528 return EGL_NO_SURFACE;
529 }
530
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000531 mSurfaceSet.insert(surface);
532
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000533 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000534}
535
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000536EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000537{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000538 EGLint width = 0, height = 0;
539 EGLenum textureFormat = EGL_NO_TEXTURE;
540 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000541 const Config *configuration = mConfigSet.get(config);
542
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000543 if (attribList)
544 {
545 while (*attribList != EGL_NONE)
546 {
547 switch (attribList[0])
548 {
549 case EGL_WIDTH:
550 width = attribList[1];
551 break;
552 case EGL_HEIGHT:
553 height = attribList[1];
554 break;
555 case EGL_LARGEST_PBUFFER:
556 if (attribList[1] != EGL_FALSE)
557 UNIMPLEMENTED(); // FIXME
558 break;
559 case EGL_TEXTURE_FORMAT:
560 switch (attribList[1])
561 {
562 case EGL_NO_TEXTURE:
563 case EGL_TEXTURE_RGB:
564 case EGL_TEXTURE_RGBA:
565 textureFormat = attribList[1];
566 break;
567 default:
568 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
569 }
570 break;
571 case EGL_TEXTURE_TARGET:
572 switch (attribList[1])
573 {
574 case EGL_NO_TEXTURE:
575 case EGL_TEXTURE_2D:
576 textureTarget = attribList[1];
577 break;
578 default:
579 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
580 }
581 break;
582 case EGL_MIPMAP_TEXTURE:
583 if (attribList[1] != EGL_FALSE)
584 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
585 break;
586 case EGL_VG_COLORSPACE:
587 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
588 case EGL_VG_ALPHA_FORMAT:
589 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
590 default:
591 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
592 }
593
594 attribList += 2;
595 }
596 }
597
598 if (width < 0 || height < 0)
599 {
600 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
601 }
602
603 if (width == 0 || height == 0)
604 {
605 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
606 }
607
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000608 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000609 {
610 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
611 }
612
613 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
614 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
615 {
616 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
617 }
618
619 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
620 {
621 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
622 }
623
624 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
625 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
626 {
627 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
628 }
629
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000630 if (isDeviceLost()) {
631 if (!restoreLostDevice())
632 return EGL_NO_SURFACE;
633 }
634
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000635 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
636
637 if (!surface->initialize())
638 {
639 delete surface;
640 return EGL_NO_SURFACE;
641 }
642
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000643 mSurfaceSet.insert(surface);
644
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000645 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000646}
647
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000648EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
649{
650 if (!mDevice)
651 {
652 if (!createDevice())
653 {
654 return NULL;
655 }
656 }
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000657 else if (isDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000658 {
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000659 if (!restoreLostDevice())
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000660 return NULL;
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000661 }
662
663 const egl::Config *config = mConfigSet.get(configHandle);
664
665 gl::Context *context = glCreateContext(config, shareContext);
666 mContextSet.insert(context);
667
668 return context;
669}
670
vangelis@google.com8c9c4522011-09-09 18:22:28 +0000671bool Display::restoreLostDevice()
672{
673 // Release surface resources to make the Reset() succeed
674 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
675 {
676 (*surface)->release();
677 }
678
679 if (!resetDevice())
680 {
681 return false;
682 }
683
684 // Restore any surfaces that may have been lost
685 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
686 {
687 (*surface)->resetSwapChain();
688 }
689
690 return true;
691}
692
693
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000694void Display::destroySurface(egl::Surface *surface)
695{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000696 delete surface;
697 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000698}
699
700void Display::destroyContext(gl::Context *context)
701{
702 glDestroyContext(context);
703 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000704}
705
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000706bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000707{
708 return mD3d9 != NULL && mConfigSet.size() > 0;
709}
710
711bool Display::isValidConfig(EGLConfig config)
712{
713 return mConfigSet.get(config) != NULL;
714}
715
716bool Display::isValidContext(gl::Context *context)
717{
718 return mContextSet.find(context) != mContextSet.end();
719}
720
721bool Display::isValidSurface(egl::Surface *surface)
722{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000723 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000724}
725
726bool Display::hasExistingWindowSurface(HWND window)
727{
728 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
729 {
730 if ((*surface)->getWindowHandle() == window)
731 {
732 return true;
733 }
734 }
735
736 return false;
737}
738
739EGLint Display::getMinSwapInterval()
740{
741 return mMinSwapInterval;
742}
743
744EGLint Display::getMaxSwapInterval()
745{
746 return mMaxSwapInterval;
747}
748
749IDirect3DDevice9 *Display::getDevice()
750{
751 if (!mDevice)
752 {
753 if (!createDevice())
754 {
755 return NULL;
756 }
757 }
758
759 return mDevice;
760}
761
762D3DCAPS9 Display::getDeviceCaps()
763{
764 return mDeviceCaps;
765}
766
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000767D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
768{
769 return &mAdapterIdentifier;
770}
771
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000772bool Display::isDeviceLost()
773{
774 if (mDeviceEx)
775 {
776 return FAILED(mDeviceEx->CheckDeviceState(NULL));
777 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000778 else if(mDevice)
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000779 {
780 return FAILED(mDevice->TestCooperativeLevel());
781 }
daniel@transgaming.comfe4b0c92011-09-13 00:53:11 +0000782
783 return false; // No device yet, so no reset required
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000784}
785
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000786void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
787{
788 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
789 {
790 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
791 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
792
793 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
794 }
795}
796
gman@chromium.org50c526d2011-08-10 05:19:44 +0000797bool Display::getDXT1TextureSupport()
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000798{
799 D3DDISPLAYMODE currentDisplayMode;
800 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
801
802 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
803}
804
gman@chromium.org50c526d2011-08-10 05:19:44 +0000805bool Display::getDXT3TextureSupport()
806{
807 D3DDISPLAYMODE currentDisplayMode;
808 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
809
810 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3));
811}
812
813bool Display::getDXT5TextureSupport()
814{
815 D3DDISPLAYMODE currentDisplayMode;
816 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
817
818 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5));
819}
820
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000821bool Display::getFloatTextureSupport(bool *filtering, bool *renderable)
822{
823 D3DDISPLAYMODE currentDisplayMode;
824 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
825
826 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
827 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
828 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
829 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
830
831 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
832 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
833 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
834 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
835
836 if (!filtering && !renderable)
837 {
838 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
839 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
840 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
841 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
842 }
843 else
844 {
845 return true;
846 }
847}
848
849bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable)
850{
851 D3DDISPLAYMODE currentDisplayMode;
852 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
853
854 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
855 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
856 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
857 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
858
859 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
860 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
861 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
862 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
863
864 if (!filtering && !renderable)
865 {
866 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
867 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
868 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
869 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
870 }
871 else
872 {
873 return true;
874 }
875}
876
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000877bool Display::getLuminanceTextureSupport()
878{
879 D3DDISPLAYMODE currentDisplayMode;
880 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
881
882 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
883}
884
885bool Display::getLuminanceAlphaTextureSupport()
886{
887 D3DDISPLAYMODE currentDisplayMode;
888 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
889
890 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
891}
892
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000893D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000894{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000895 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000896 {
897 return D3DPOOL_DEFAULT;
898 }
899 else
900 {
901 if (!(usage & D3DUSAGE_DYNAMIC))
902 {
903 return D3DPOOL_MANAGED;
904 }
905 }
906
907 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000908}
909
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000910bool Display::getEventQuerySupport()
911{
912 IDirect3DQuery9 *query;
913 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
914 if (SUCCEEDED(result))
915 {
916 query->Release();
917 }
918
919 return result != D3DERR_NOTAVAILABLE;
920}
921
922D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
923{
924 D3DPRESENT_PARAMETERS presentParameters = {0};
925
926 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
927 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
928 presentParameters.BackBufferCount = 1;
929 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
930 presentParameters.BackBufferWidth = 1;
931 presentParameters.BackBufferHeight = 1;
932 presentParameters.EnableAutoDepthStencil = FALSE;
933 presentParameters.Flags = 0;
934 presentParameters.hDeviceWindow = mDeviceWindow;
935 presentParameters.MultiSampleQuality = 0;
936 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
937 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
938 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
939 presentParameters.Windowed = TRUE;
940
941 return presentParameters;
942}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000943
944void Display::initExtensionString()
945{
946 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000947 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
948
949 if (swiftShader)
950 {
951 mExtensionString += "EGL_ANGLE_software_display ";
952 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000953
954 if (isD3d9ExDevice()) {
955 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
daniel@transgaming.com2b720c92011-05-13 16:05:22 +0000956 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000957 }
958
959 std::string::size_type end = mExtensionString.find_last_not_of(' ');
960 if (end != std::string::npos)
961 {
962 mExtensionString.resize(end+1);
963 }
964}
965
966const char *Display::getExtensionString() const
967{
968 return mExtensionString.c_str();
969}
970
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000971// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
972// We test this using D3D9 by checking support for the R16F format.
973bool Display::getVertexTextureSupport() const
974{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000975 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
976 {
977 return false;
978 }
979
980 D3DDISPLAYMODE currentDisplayMode;
981 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
982
983 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
984
985 return SUCCEEDED(result);
986}
987
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000988bool Display::getNonPower2TextureSupport() const
989{
990 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
991 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
992 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
993}
994
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000995}