blob: ea5fcc39fbeb23421e61afb973f1f70e8804f669 [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
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000518 Surface *surface = new Surface(this, configuration, window);
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000519
520 if (!surface->initialize())
521 {
522 delete surface;
523 return EGL_NO_SURFACE;
524 }
525
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000526 mSurfaceSet.insert(surface);
527
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000528 return success(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000529}
530
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000531EGLSurface Display::createOffscreenSurface(EGLConfig config, HANDLE shareHandle, const EGLint *attribList)
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000532{
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000533 EGLint width = 0, height = 0;
534 EGLenum textureFormat = EGL_NO_TEXTURE;
535 EGLenum textureTarget = EGL_NO_TEXTURE;
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000536 const Config *configuration = mConfigSet.get(config);
537
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000538 if (attribList)
539 {
540 while (*attribList != EGL_NONE)
541 {
542 switch (attribList[0])
543 {
544 case EGL_WIDTH:
545 width = attribList[1];
546 break;
547 case EGL_HEIGHT:
548 height = attribList[1];
549 break;
550 case EGL_LARGEST_PBUFFER:
551 if (attribList[1] != EGL_FALSE)
552 UNIMPLEMENTED(); // FIXME
553 break;
554 case EGL_TEXTURE_FORMAT:
555 switch (attribList[1])
556 {
557 case EGL_NO_TEXTURE:
558 case EGL_TEXTURE_RGB:
559 case EGL_TEXTURE_RGBA:
560 textureFormat = attribList[1];
561 break;
562 default:
563 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
564 }
565 break;
566 case EGL_TEXTURE_TARGET:
567 switch (attribList[1])
568 {
569 case EGL_NO_TEXTURE:
570 case EGL_TEXTURE_2D:
571 textureTarget = attribList[1];
572 break;
573 default:
574 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
575 }
576 break;
577 case EGL_MIPMAP_TEXTURE:
578 if (attribList[1] != EGL_FALSE)
579 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
580 break;
581 case EGL_VG_COLORSPACE:
582 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
583 case EGL_VG_ALPHA_FORMAT:
584 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
585 default:
586 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
587 }
588
589 attribList += 2;
590 }
591 }
592
593 if (width < 0 || height < 0)
594 {
595 return error(EGL_BAD_PARAMETER, EGL_NO_SURFACE);
596 }
597
598 if (width == 0 || height == 0)
599 {
600 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
601 }
602
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000603 if (textureFormat != EGL_NO_TEXTURE && !getNonPower2TextureSupport() && (!gl::isPow2(width) || !gl::isPow2(height)))
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000604 {
605 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
606 }
607
608 if ((textureFormat != EGL_NO_TEXTURE && textureTarget == EGL_NO_TEXTURE) ||
609 (textureFormat == EGL_NO_TEXTURE && textureTarget != EGL_NO_TEXTURE))
610 {
611 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
612 }
613
614 if (!(configuration->mSurfaceType & EGL_PBUFFER_BIT))
615 {
616 return error(EGL_BAD_MATCH, EGL_NO_SURFACE);
617 }
618
619 if ((textureFormat == EGL_TEXTURE_RGB && configuration->mBindToTextureRGB != EGL_TRUE) ||
620 (textureFormat == EGL_TEXTURE_RGBA && configuration->mBindToTextureRGBA != EGL_TRUE))
621 {
622 return error(EGL_BAD_ATTRIBUTE, EGL_NO_SURFACE);
623 }
624
jbauman@chromium.org4e297702011-05-12 23:04:07 +0000625 Surface *surface = new Surface(this, configuration, shareHandle, width, height, textureFormat, textureTarget);
626
627 if (!surface->initialize())
628 {
629 delete surface;
630 return EGL_NO_SURFACE;
631 }
632
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000633 mSurfaceSet.insert(surface);
634
jbauman@chromium.org06d7a752011-04-30 01:02:52 +0000635 return success(surface);
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000636}
637
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000638EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
639{
640 if (!mDevice)
641 {
642 if (!createDevice())
643 {
644 return NULL;
645 }
646 }
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000647 else if (isDeviceLost()) // Lost device
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000648 {
daniel@transgaming.com6c4dba02011-05-26 14:15:31 +0000649 // Release surface resources to make the Reset() succeed
650 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
651 {
652 (*surface)->release();
653 }
654
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000655 if (!resetDevice())
656 {
657 return NULL;
658 }
659
660 // Restore any surfaces that may have been lost
661 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
662 {
663 (*surface)->resetSwapChain();
664 }
665 }
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
675void Display::destroySurface(egl::Surface *surface)
676{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000677 delete surface;
678 mSurfaceSet.erase(surface);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000679}
680
681void Display::destroyContext(gl::Context *context)
682{
683 glDestroyContext(context);
684 mContextSet.erase(context);
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000685}
686
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000687bool Display::isInitialized() const
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000688{
689 return mD3d9 != NULL && mConfigSet.size() > 0;
690}
691
692bool Display::isValidConfig(EGLConfig config)
693{
694 return mConfigSet.get(config) != NULL;
695}
696
697bool Display::isValidContext(gl::Context *context)
698{
699 return mContextSet.find(context) != mContextSet.end();
700}
701
702bool Display::isValidSurface(egl::Surface *surface)
703{
daniel@transgaming.comc556fa52011-05-26 14:13:29 +0000704 return mSurfaceSet.find(surface) != mSurfaceSet.end();
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000705}
706
707bool Display::hasExistingWindowSurface(HWND window)
708{
709 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
710 {
711 if ((*surface)->getWindowHandle() == window)
712 {
713 return true;
714 }
715 }
716
717 return false;
718}
719
720EGLint Display::getMinSwapInterval()
721{
722 return mMinSwapInterval;
723}
724
725EGLint Display::getMaxSwapInterval()
726{
727 return mMaxSwapInterval;
728}
729
730IDirect3DDevice9 *Display::getDevice()
731{
732 if (!mDevice)
733 {
734 if (!createDevice())
735 {
736 return NULL;
737 }
738 }
739
740 return mDevice;
741}
742
743D3DCAPS9 Display::getDeviceCaps()
744{
745 return mDeviceCaps;
746}
747
jbauman@chromium.org03208d52011-06-15 01:15:24 +0000748D3DADAPTER_IDENTIFIER9 *Display::getAdapterIdentifier()
749{
750 return &mAdapterIdentifier;
751}
752
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000753bool Display::isDeviceLost()
754{
755 if (mDeviceEx)
756 {
757 return FAILED(mDeviceEx->CheckDeviceState(NULL));
758 }
759 else
760 {
761 return FAILED(mDevice->TestCooperativeLevel());
762 }
763}
764
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000765void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
766{
767 for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
768 {
769 HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
770 TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
771
772 multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
773 }
774}
775
776bool Display::getCompressedTextureSupport()
777{
778 D3DDISPLAYMODE currentDisplayMode;
779 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
780
781 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
782}
783
784bool Display::getFloatTextureSupport(bool *filtering, bool *renderable)
785{
786 D3DDISPLAYMODE currentDisplayMode;
787 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
788
789 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
790 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
791 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
792 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
793
794 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
795 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
796 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
797 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
798
799 if (!filtering && !renderable)
800 {
801 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
802 D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
803 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
804 D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
805 }
806 else
807 {
808 return true;
809 }
810}
811
812bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable)
813{
814 D3DDISPLAYMODE currentDisplayMode;
815 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
816
817 *filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
818 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
819 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
820 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
821
822 *renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
823 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
824 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
825 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
826
827 if (!filtering && !renderable)
828 {
829 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
830 D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
831 SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
832 D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
833 }
834 else
835 {
836 return true;
837 }
838}
839
daniel@transgaming.comed828e52010-10-15 17:57:30 +0000840bool Display::getLuminanceTextureSupport()
841{
842 D3DDISPLAYMODE currentDisplayMode;
843 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
844
845 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8));
846}
847
848bool Display::getLuminanceAlphaTextureSupport()
849{
850 D3DDISPLAYMODE currentDisplayMode;
851 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
852
853 return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8));
854}
855
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000856D3DPOOL Display::getBufferPool(DWORD usage) const
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000857{
apatrick@chromium.org9e83b592011-02-10 22:04:34 +0000858 if (mD3d9Ex != NULL)
daniel@transgaming.comee04e452011-01-08 05:46:27 +0000859 {
860 return D3DPOOL_DEFAULT;
861 }
862 else
863 {
864 if (!(usage & D3DUSAGE_DYNAMIC))
865 {
866 return D3DPOOL_MANAGED;
867 }
868 }
869
870 return D3DPOOL_DEFAULT;
daniel@transgaming.com37b141e2011-01-08 05:46:13 +0000871}
872
daniel@transgaming.come979ead2010-09-23 18:03:14 +0000873bool Display::getEventQuerySupport()
874{
875 IDirect3DQuery9 *query;
876 HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
877 if (SUCCEEDED(result))
878 {
879 query->Release();
880 }
881
882 return result != D3DERR_NOTAVAILABLE;
883}
884
885D3DPRESENT_PARAMETERS Display::getDefaultPresentParameters()
886{
887 D3DPRESENT_PARAMETERS presentParameters = {0};
888
889 // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
890 presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
891 presentParameters.BackBufferCount = 1;
892 presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
893 presentParameters.BackBufferWidth = 1;
894 presentParameters.BackBufferHeight = 1;
895 presentParameters.EnableAutoDepthStencil = FALSE;
896 presentParameters.Flags = 0;
897 presentParameters.hDeviceWindow = mDeviceWindow;
898 presentParameters.MultiSampleQuality = 0;
899 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
900 presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
901 presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
902 presentParameters.Windowed = TRUE;
903
904 return presentParameters;
905}
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000906
907void Display::initExtensionString()
908{
909 mExtensionString += "EGL_ANGLE_query_surface_pointer ";
jbauman@chromium.org84d7cbc2011-07-14 22:53:19 +0000910 HMODULE swiftShader = GetModuleHandle(TEXT("swiftshader_d3d9.dll"));
911
912 if (swiftShader)
913 {
914 mExtensionString += "EGL_ANGLE_software_display ";
915 }
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000916
917 if (isD3d9ExDevice()) {
918 mExtensionString += "EGL_ANGLE_surface_d3d_texture_2d_share_handle ";
daniel@transgaming.com2b720c92011-05-13 16:05:22 +0000919 mExtensionString += "EGL_ANGLE_d3d_share_handle_client_buffer ";
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000920 }
921
922 std::string::size_type end = mExtensionString.find_last_not_of(' ');
923 if (end != std::string::npos)
924 {
925 mExtensionString.resize(end+1);
926 }
927}
928
929const char *Display::getExtensionString() const
930{
931 return mExtensionString.c_str();
932}
933
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000934// Only Direct3D 10 ready devices support all the necessary vertex texture formats.
935// We test this using D3D9 by checking support for the R16F format.
936bool Display::getVertexTextureSupport() const
937{
daniel@transgaming.com3b1703f2011-05-11 15:36:26 +0000938 if (!isInitialized() || mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(3, 0))
939 {
940 return false;
941 }
942
943 D3DDISPLAYMODE currentDisplayMode;
944 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
945
946 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F);
947
948 return SUCCEEDED(result);
949}
950
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +0000951bool Display::getNonPower2TextureSupport() const
952{
953 return !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) &&
954 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) &&
955 !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL);
956}
957
vladimirv@gmail.com721b7f22011-02-11 00:54:47 +0000958}