Commit e860f079 by apatrick@chromium.org

Trying to fix bug crash when eglCreateWindowSurface (or any other code that…

Trying to fix bug crash when eglCreateWindowSurface (or any other code that causes createDevice to be invoked) is called and CreateDevice reports DEVICELOST or NOTAVAILABLE. This was a Windows XP service pack 3 box. 0x10002545 [libegl.dll - display.cpp:340] egl::Display::createDevice() 0x10002912 [libegl.dll - display.cpp:468] egl::Display::getDevice() 0x01d6a80f [chrome.dll - gpu_info_collector_win.cc:29] gpu_info_collector::CollectGraphicsInfo(GPUInfo *) 0x01d68eca [chrome.dll - gpu_thread.cc:104] GpuThread::OnEstablishChannel(int) Previously createDevice only checked for out of memory and the ASSERT(SUCCEEDED(result)) has no effect in release builds. I simulated the failure in a debugger and discovered a second place where a null dereference would occur in this case. I don't think this crash is technically correct but it prevents crashes. Review URL: http://codereview.appspot.com/2217043 git-svn-id: https://angleproject.googlecode.com/svn/trunk@428 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 4d5962cd
// //
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
// Display.cpp: Implements the egl::Display class, representing the abstract // Display.cpp: Implements the egl::Display class, representing the abstract
// display on which graphics are drawn. Implements EGLDisplay. // display on which graphics are drawn. Implements EGLDisplay.
// [EGL 1.4] section 2.1.2 page 3. // [EGL 1.4] section 2.1.2 page 3.
#include "libEGL/Display.h" #include "libEGL/Display.h"
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include "common/debug.h" #include "common/debug.h"
#include "libEGL/main.h" #include "libEGL/main.h"
#define REF_RAST 0 // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros #define REF_RAST 0 // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros
#define ENABLE_D3D9EX 1 // Enables use of the IDirect3D9Ex interface, when available #define ENABLE_D3D9EX 1 // Enables use of the IDirect3D9Ex interface, when available
namespace egl namespace egl
{ {
Display::Display(HDC deviceContext) : mDc(deviceContext) Display::Display(HDC deviceContext) : mDc(deviceContext)
{ {
mD3d9Module = NULL; mD3d9Module = NULL;
mD3d9 = NULL; mD3d9 = NULL;
mD3d9ex = NULL; mD3d9ex = NULL;
mDevice = NULL; mDevice = NULL;
mDeviceWindow = NULL; mDeviceWindow = NULL;
mAdapter = D3DADAPTER_DEFAULT; mAdapter = D3DADAPTER_DEFAULT;
#if REF_RAST == 1 || defined(FORCE_REF_RAST) #if REF_RAST == 1 || defined(FORCE_REF_RAST)
mDeviceType = D3DDEVTYPE_REF; mDeviceType = D3DDEVTYPE_REF;
#else #else
mDeviceType = D3DDEVTYPE_HAL; mDeviceType = D3DDEVTYPE_HAL;
#endif #endif
mMinSwapInterval = 1; mMinSwapInterval = 1;
mMaxSwapInterval = 1; mMaxSwapInterval = 1;
} }
Display::~Display() Display::~Display()
{ {
terminate(); terminate();
} }
bool Display::initialize() bool Display::initialize()
{ {
if (isInitialized()) if (isInitialized())
{ {
return true; return true;
} }
mD3d9Module = LoadLibrary(TEXT("d3d9.dll")); mD3d9Module = LoadLibrary(TEXT("d3d9.dll"));
if (mD3d9Module == NULL) if (mD3d9Module == NULL)
{ {
terminate(); terminate();
return false; return false;
} }
typedef IDirect3D9* (WINAPI *Direct3DCreate9Func)(UINT); typedef IDirect3D9* (WINAPI *Direct3DCreate9Func)(UINT);
Direct3DCreate9Func Direct3DCreate9Ptr = reinterpret_cast<Direct3DCreate9Func>(GetProcAddress(mD3d9Module, "Direct3DCreate9")); Direct3DCreate9Func Direct3DCreate9Ptr = reinterpret_cast<Direct3DCreate9Func>(GetProcAddress(mD3d9Module, "Direct3DCreate9"));
if (Direct3DCreate9Ptr == NULL) if (Direct3DCreate9Ptr == NULL)
{ {
terminate(); terminate();
return false; return false;
} }
typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**);
Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex"));
// Use Direct3D9Ex if available. Among other things, this version is less // Use Direct3D9Ex if available. Among other things, this version is less
// inclined to report a lost context, for example when the user switches // inclined to report a lost context, for example when the user switches
// desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available.
if (ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9ex))) if (ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9ex)))
{ {
ASSERT(mD3d9ex); ASSERT(mD3d9ex);
mD3d9ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); mD3d9ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9));
ASSERT(mD3d9); ASSERT(mD3d9);
} }
else else
{ {
mD3d9 = Direct3DCreate9Ptr(D3D_SDK_VERSION); mD3d9 = Direct3DCreate9Ptr(D3D_SDK_VERSION);
} }
if (mD3d9) if (mD3d9)
{ {
if (mDc != NULL) if (mDc != NULL)
{ {
// UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
} }
HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
{ {
return error(EGL_BAD_ALLOC, false); return error(EGL_BAD_ALLOC, false);
} }
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0))
{ {
terminate(); terminate();
return error(EGL_NOT_INITIALIZED, false); return error(EGL_NOT_INITIALIZED, false);
} }
mMinSwapInterval = 4; mMinSwapInterval = 4;
mMaxSwapInterval = 0; mMaxSwapInterval = 0;
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);} if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0);}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);} if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1);}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);} if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2);}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);} if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3);}
if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);} if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4);}
const D3DFORMAT renderTargetFormats[] = const D3DFORMAT renderTargetFormats[] =
{ {
D3DFMT_A1R5G5B5, D3DFMT_A1R5G5B5,
// D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value.
D3DFMT_A8R8G8B8, D3DFMT_A8R8G8B8,
D3DFMT_R5G6B5, D3DFMT_R5G6B5,
D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5,
D3DFMT_X8R8G8B8 D3DFMT_X8R8G8B8
}; };
const D3DFORMAT depthStencilFormats[] = const D3DFORMAT depthStencilFormats[] =
{ {
// D3DFMT_D16_LOCKABLE, // D3DFMT_D16_LOCKABLE,
D3DFMT_D32, D3DFMT_D32,
// D3DFMT_D15S1, // D3DFMT_D15S1,
D3DFMT_D24S8, D3DFMT_D24S8,
D3DFMT_D24X8, D3DFMT_D24X8,
// D3DFMT_D24X4S4, // D3DFMT_D24X4S4,
D3DFMT_D16, D3DFMT_D16,
// D3DFMT_D32F_LOCKABLE, // D3DFMT_D32F_LOCKABLE,
// D3DFMT_D24FS8 // D3DFMT_D24FS8
}; };
D3DDISPLAYMODE currentDisplayMode; D3DDISPLAYMODE currentDisplayMode;
mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode); mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
ConfigSet configSet; ConfigSet configSet;
for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++) for (int formatIndex = 0; formatIndex < sizeof(renderTargetFormats) / sizeof(D3DFORMAT); formatIndex++)
{ {
D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex]; D3DFORMAT renderTargetFormat = renderTargetFormats[formatIndex];
HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
if (SUCCEEDED(result)) if (SUCCEEDED(result))
{ {
for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++) for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
{ {
D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex]; D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
if (SUCCEEDED(result)) if (SUCCEEDED(result))
{ {
HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat);
if (SUCCEEDED(result)) if (SUCCEEDED(result))
{ {
// FIXME: enumerate multi-sampling // FIXME: enumerate multi-sampling
configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0); configSet.add(currentDisplayMode, mMinSwapInterval, mMaxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
} }
} }
} }
} }
} }
// Give the sorted configs a unique ID and store them internally // Give the sorted configs a unique ID and store them internally
EGLint index = 1; EGLint index = 1;
for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++) for (ConfigSet::Iterator config = configSet.mSet.begin(); config != configSet.mSet.end(); config++)
{ {
Config configuration = *config; Config configuration = *config;
configuration.mConfigID = index; configuration.mConfigID = index;
index++; index++;
mConfigSet.mSet.insert(configuration); mConfigSet.mSet.insert(configuration);
} }
} }
if (!isInitialized()) if (!isInitialized())
{ {
terminate(); terminate();
return false; return false;
} }
static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); static const TCHAR windowName[] = TEXT("AngleHiddenWindow");
static const TCHAR className[] = TEXT("STATIC"); static const TCHAR className[] = TEXT("STATIC");
mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
return true; return true;
} }
void Display::terminate() void Display::terminate()
{ {
for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
{ {
delete *surface; delete *surface;
} }
for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++) for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
{ {
glDestroyContext(*context); glDestroyContext(*context);
} }
if (mDevice) if (mDevice)
{ {
mDevice->Release(); mDevice->Release();
mDevice = NULL; mDevice = NULL;
} }
if (mD3d9) if (mD3d9)
{ {
mD3d9->Release(); mD3d9->Release();
mD3d9 = NULL; mD3d9 = NULL;
} }
if (mDeviceWindow) if (mDeviceWindow)
{ {
DestroyWindow(mDeviceWindow); DestroyWindow(mDeviceWindow);
mDeviceWindow = NULL; mDeviceWindow = NULL;
} }
if (mD3d9ex) if (mD3d9ex)
{ {
mD3d9ex->Release(); mD3d9ex->Release();
mD3d9ex = NULL; mD3d9ex = NULL;
} }
if (mD3d9Module) if (mD3d9Module)
{ {
FreeLibrary(mD3d9Module); FreeLibrary(mD3d9Module);
mD3d9Module = NULL; mD3d9Module = NULL;
} }
} }
void Display::startScene() void Display::startScene()
{ {
if (!mSceneStarted) if (!mSceneStarted)
{ {
long result = mDevice->BeginScene(); long result = mDevice->BeginScene();
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
mSceneStarted = true; mSceneStarted = true;
} }
} }
void Display::endScene() void Display::endScene()
{ {
if (mSceneStarted) if (mSceneStarted)
{ {
long result = mDevice->EndScene(); long result = mDevice->EndScene();
ASSERT(SUCCEEDED(result)); ASSERT(SUCCEEDED(result));
mSceneStarted = false; mSceneStarted = false;
} }
} }
bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig) bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
{ {
return mConfigSet.getConfigs(configs, attribList, configSize, numConfig); return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
} }
bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value) bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
{ {
const egl::Config *configuration = mConfigSet.get(config); const egl::Config *configuration = mConfigSet.get(config);
switch (attribute) switch (attribute)
{ {
case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break; case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break; case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break; case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break; case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
case EGL_RED_SIZE: *value = configuration->mRedSize; break; case EGL_RED_SIZE: *value = configuration->mRedSize; break;
case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break; case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break; case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break; case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
case EGL_CONFIG_ID: *value = configuration->mConfigID; break; case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
case EGL_LEVEL: *value = configuration->mLevel; break; case EGL_LEVEL: *value = configuration->mLevel; break;
case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break; case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break; case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
case EGL_SAMPLES: *value = configuration->mSamples; break; case EGL_SAMPLES: *value = configuration->mSamples; break;
case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break; case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break; case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break; case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break; case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break; case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break; case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break; case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break; case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break; case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break; case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break; case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break; case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break; case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break; case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break; case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
case EGL_CONFORMANT: *value = configuration->mConformant; break; case EGL_CONFORMANT: *value = configuration->mConformant; break;
default: default:
return false; return false;
} }
return true; return true;
} }
bool Display::createDevice() bool Display::createDevice()
{ {
D3DPRESENT_PARAMETERS presentParameters = getPresentParameters(); D3DPRESENT_PARAMETERS presentParameters = getPresentParameters();
DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES;
HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); HRESULT result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST)
{ {
return error(EGL_BAD_ALLOC, false); return error(EGL_BAD_ALLOC, false);
} }
if (FAILED(result)) if (FAILED(result))
{ {
result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) if (FAILED(result))
{ {
return error(EGL_BAD_ALLOC, false); ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST);
} return error(EGL_BAD_ALLOC, false);
} }
}
ASSERT(SUCCEEDED(result));
// Permanent non-default states
// Permanent non-default states mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE);
mSceneStarted = false;
mSceneStarted = false;
return true;
return true; }
}
Surface *Display::createWindowSurface(HWND window, EGLConfig config)
Surface *Display::createWindowSurface(HWND window, EGLConfig config) {
{ const Config *configuration = mConfigSet.get(config);
const Config *configuration = mConfigSet.get(config);
Surface *surface = new Surface(this, configuration, window);
Surface *surface = new Surface(this, configuration, window); mSurfaceSet.insert(surface);
mSurfaceSet.insert(surface);
return surface;
return surface; }
}
EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext)
EGLContext Display::createContext(EGLConfig configHandle, const gl::Context *shareContext) {
{ if (!mDevice)
if (!mDevice) {
{ if (!createDevice())
if (!createDevice()) {
{ return NULL;
return NULL; }
} }
} else if (FAILED(mDevice->TestCooperativeLevel())) // Lost device
else if (FAILED(mDevice->TestCooperativeLevel())) // Lost device {
{ D3DPRESENT_PARAMETERS presentParameters = getPresentParameters();
D3DPRESENT_PARAMETERS presentParameters = getPresentParameters(); HRESULT result;
HRESULT result;
do
do {
{ Sleep(0); // Give the graphics driver some CPU time
Sleep(0); // Give the graphics driver some CPU time
result = mDevice->Reset(&presentParameters);
result = mDevice->Reset(&presentParameters); }
} while (result == D3DERR_DEVICELOST);
while (result == D3DERR_DEVICELOST);
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICEREMOVED || result == D3DERR_DRIVERINTERNALERROR)
if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICEREMOVED || result == D3DERR_DRIVERINTERNALERROR) {
{ return error(EGL_BAD_ALLOC, (EGLContext)NULL);
return error(EGL_BAD_ALLOC, (EGLContext)NULL); }
}
ASSERT(SUCCEEDED(result));
ASSERT(SUCCEEDED(result));
// Restore any surfaces that may have been lost
// Restore any surfaces that may have been lost for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) {
{ (*surface)->resetSwapChain();
(*surface)->resetSwapChain(); }
} }
}
const egl::Config *config = mConfigSet.get(configHandle);
const egl::Config *config = mConfigSet.get(configHandle);
gl::Context *context = glCreateContext(config, shareContext);
gl::Context *context = glCreateContext(config, shareContext); mContextSet.insert(context);
mContextSet.insert(context);
return context;
return context; }
}
void Display::destroySurface(egl::Surface *surface)
void Display::destroySurface(egl::Surface *surface) {
{ delete surface;
delete surface; mSurfaceSet.erase(surface);
mSurfaceSet.erase(surface); }
}
void Display::destroyContext(gl::Context *context)
void Display::destroyContext(gl::Context *context) {
{ glDestroyContext(context);
glDestroyContext(context); mContextSet.erase(context);
mContextSet.erase(context);
if (mContextSet.empty() && mDevice && FAILED(mDevice->TestCooperativeLevel())) // Last context of a lost device
if (mContextSet.empty() && mDevice && FAILED(mDevice->TestCooperativeLevel())) // Last context of a lost device {
{ for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) {
{ (*surface)->release();
(*surface)->release(); }
} }
} }
}
bool Display::isInitialized()
bool Display::isInitialized() {
{ return mD3d9 != NULL && mConfigSet.size() > 0;
return mD3d9 != NULL && mConfigSet.size() > 0; }
}
bool Display::isValidConfig(EGLConfig config)
bool Display::isValidConfig(EGLConfig config) {
{ return mConfigSet.get(config) != NULL;
return mConfigSet.get(config) != NULL; }
}
bool Display::isValidContext(gl::Context *context)
bool Display::isValidContext(gl::Context *context) {
{ return mContextSet.find(context) != mContextSet.end();
return mContextSet.find(context) != mContextSet.end(); }
}
bool Display::isValidSurface(egl::Surface *surface)
bool Display::isValidSurface(egl::Surface *surface) {
{ return mSurfaceSet.find(surface) != mSurfaceSet.end();
return mSurfaceSet.find(surface) != mSurfaceSet.end(); }
}
bool Display::hasExistingWindowSurface(HWND window)
bool Display::hasExistingWindowSurface(HWND window) {
{ for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++) {
{ if ((*surface)->getWindowHandle() == window)
if ((*surface)->getWindowHandle() == window) {
{ return true;
return true; }
} }
}
return false;
return false; }
}
EGLint Display::getMinSwapInterval()
EGLint Display::getMinSwapInterval() {
{ return mMinSwapInterval;
return mMinSwapInterval; }
}
EGLint Display::getMaxSwapInterval()
EGLint Display::getMaxSwapInterval() {
{ return mMaxSwapInterval;
return mMaxSwapInterval; }
}
IDirect3DDevice9 *Display::getDevice()
IDirect3DDevice9 *Display::getDevice() {
{ if (!mDevice)
if (!mDevice) {
{ if (!createDevice())
if (!createDevice()) {
{ return NULL;
return NULL; }
} }
}
return mDevice;
return mDevice; }
}
D3DCAPS9 Display::getDeviceCaps()
D3DCAPS9 Display::getDeviceCaps() {
{ return mDeviceCaps;
return mDeviceCaps; }
}
void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray)
void Display::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) {
{ for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++)
for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) {
{ HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format,
HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL);
multiSampleArray[multiSampleIndex] = SUCCEEDED(result);
multiSampleArray[multiSampleIndex] = SUCCEEDED(result); }
} }
}
bool Display::getCompressedTextureSupport()
bool Display::getCompressedTextureSupport() {
{ D3DDISPLAYMODE currentDisplayMode;
D3DDISPLAYMODE currentDisplayMode; mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1));
return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); }
}
bool Display::getFloatTextureSupport(bool *filtering, bool *renderable)
bool Display::getFloatTextureSupport(bool *filtering, bool *renderable) {
{ D3DDISPLAYMODE currentDisplayMode;
D3DDISPLAYMODE currentDisplayMode; mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
*filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
*filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
*renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
*renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&&
D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F))&& SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
if (!filtering && !renderable)
if (!filtering && !renderable) {
{ return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) &&
D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); }
} else
else {
{ return true;
return true; }
} }
}
bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable)
bool Display::getHalfFloatTextureSupport(bool *filtering, bool *renderable) {
{ D3DDISPLAYMODE currentDisplayMode;
D3DDISPLAYMODE currentDisplayMode; mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
*filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
*filtering = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
*renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
*renderable = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
if (!filtering && !renderable)
if (!filtering && !renderable) {
{ return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
return SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) &&
D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0,
SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F));
D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); }
} else
else {
{ return true;
return true; }
} }
}
bool Display::getEventQuerySupport()
bool Display::getEventQuerySupport() {
{ IDirect3DQuery9 *query;
IDirect3DQuery9 *query; HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query);
HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); if (SUCCEEDED(result))
if (SUCCEEDED(result)) {
{ query->Release();
query->Release(); }
}
return result != D3DERR_NOTAVAILABLE;
return result != D3DERR_NOTAVAILABLE; }
}
D3DPRESENT_PARAMETERS Display::getPresentParameters()
D3DPRESENT_PARAMETERS Display::getPresentParameters() {
{ D3DPRESENT_PARAMETERS presentParameters = {0};
D3DPRESENT_PARAMETERS presentParameters = {0};
// The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters.
// The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN;
presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; presentParameters.BackBufferCount = 1;
presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = D3DFMT_UNKNOWN;
presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; presentParameters.BackBufferWidth = 1;
presentParameters.BackBufferWidth = 1; presentParameters.BackBufferHeight = 1;
presentParameters.BackBufferHeight = 1; presentParameters.EnableAutoDepthStencil = FALSE;
presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0;
presentParameters.Flags = 0; presentParameters.hDeviceWindow = mDeviceWindow;
presentParameters.hDeviceWindow = mDeviceWindow; presentParameters.MultiSampleQuality = 0;
presentParameters.MultiSampleQuality = 0; presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE;
presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE;
presentParameters.Windowed = TRUE;
return presentParameters;
return presentParameters; }
}
} }
\ No newline at end of file
...@@ -91,6 +91,11 @@ void Surface::resetSwapChain() ...@@ -91,6 +91,11 @@ void Surface::resetSwapChain()
{ {
IDirect3DDevice9 *device = mDisplay->getDevice(); IDirect3DDevice9 *device = mDisplay->getDevice();
if (device == NULL)
{
return;
}
D3DPRESENT_PARAMETERS presentParameters = {0}; D3DPRESENT_PARAMETERS presentParameters = {0};
presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat; presentParameters.AutoDepthStencilFormat = mConfig->mDepthStencilFormat;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment