blob: 31c25ef61c068c446397d01c0d1026dbdc3efa4b [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Display.cpp: Implements the egl::Display class, representing the abstract
8// display on which graphics are drawn. Implements EGLDisplay.
9// [EGL 1.4] section 2.1.2 page 3.
10
11#include "Display.h"
12
daniel@transgaming.com16973022010-03-11 19:22:19 +000013#include <algorithm>
14#include <vector>
15
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000016#include "main.h"
17#include "debug.h"
18
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000019namespace egl
20{
21Display::Display(HDC deviceContext) : mDc(deviceContext)
22{
23 mD3d9 = NULL;
24 mDevice = NULL;
25
26 mAdapter = D3DADAPTER_DEFAULT;
27 mDeviceType = D3DDEVTYPE_HAL;
28}
29
30Display::~Display()
31{
32 terminate();
33}
34
35bool Display::initialize()
36{
37 if (isInitialized())
38 {
39 return true;
40 }
41
42 mD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
43
44 if (mD3d9)
45 {
46 if (mDc != NULL)
47 {
48 // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to
49 }
50
51 D3DCAPS9 caps;
52 HRESULT result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &caps);
53
54 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
55 {
56 return error(EGL_BAD_ALLOC, false);
57 }
58
59 if (caps.PixelShaderVersion < D3DPS_VERSION(2, 0) || caps.VertexShaderVersion < D3DVS_VERSION(2, 0))
60 {
61 mD3d9->Release();
62 mD3d9 = NULL;
63 }
64 else
65 {
66 EGLint minSwapInterval = 4;
67 EGLint maxSwapInterval = 0;
68
daniel@transgaming.com16973022010-03-11 19:22:19 +000069 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) {minSwapInterval = std::min(minSwapInterval, 0); maxSwapInterval = std::max(maxSwapInterval, 0);}
70 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) {minSwapInterval = std::min(minSwapInterval, 1); maxSwapInterval = std::max(maxSwapInterval, 1);}
71 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) {minSwapInterval = std::min(minSwapInterval, 2); maxSwapInterval = std::max(maxSwapInterval, 2);}
72 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) {minSwapInterval = std::min(minSwapInterval, 3); maxSwapInterval = std::max(maxSwapInterval, 3);}
73 if (caps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) {minSwapInterval = std::min(minSwapInterval, 4); maxSwapInterval = std::max(maxSwapInterval, 4);}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074
75 const D3DFORMAT adapterFormats[] =
76 {
77 D3DFMT_A1R5G5B5,
78 D3DFMT_A2R10G10B10,
79 D3DFMT_A8R8G8B8,
80 D3DFMT_R5G6B5,
81 D3DFMT_X1R5G5B5,
82 D3DFMT_X8R8G8B8
83 };
84
85 const D3DFORMAT depthStencilFormats[] =
86 {
87 // D3DFMT_D16_LOCKABLE,
88 D3DFMT_D32,
89 D3DFMT_D15S1,
90 D3DFMT_D24S8,
91 D3DFMT_D24X8,
92 D3DFMT_D24X4S4,
93 D3DFMT_D16,
94 // D3DFMT_D32F_LOCKABLE,
95 // D3DFMT_D24FS8
96 };
97
98 D3DDISPLAYMODE currentDisplayMode;
99 mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode);
100
101 for (int formatIndex = 0; formatIndex < sizeof(adapterFormats) / sizeof(D3DFORMAT); formatIndex++)
102 {
103 D3DFORMAT renderTargetFormat = adapterFormats[formatIndex];
104
105 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat);
106
107 if (SUCCEEDED(result))
108 {
109 for (int depthStencilIndex = 0; depthStencilIndex < sizeof(depthStencilFormats) / sizeof(D3DFORMAT); depthStencilIndex++)
110 {
111 D3DFORMAT depthStencilFormat = depthStencilFormats[depthStencilIndex];
112 HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat);
113
114 if (SUCCEEDED(result))
115 {
116 HRESULT result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); // FIXME: Only accept color formats available both in fullscreen and windowed?
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000117
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118 if (SUCCEEDED(result))
119 {
120 // FIXME: Enumerate multi-sampling
121
122 mConfigSet.add(currentDisplayMode, minSwapInterval, maxSwapInterval, renderTargetFormat, depthStencilFormat, 0);
123 }
124 }
125 }
126 }
127 }
128 }
129
130 mConfigSet.enumerate();
131 }
132
133 if (!isInitialized())
134 {
135 terminate();
136
137 return false;
138 }
139
140 return true;
141}
142
143void Display::terminate()
144{
145 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
146 {
147 delete *surface;
148 }
149
150 for (ContextSet::iterator context = mContextSet.begin(); context != mContextSet.end(); context++)
151 {
152 glDestroyContext(*context);
153 }
154
155 if (mDevice)
156 {
157 mDevice->Release();
158 mDevice = NULL;
159 }
160
161 if (mD3d9)
162 {
163 mD3d9->Release();
164 mD3d9 = NULL;
165 }
166}
167
168bool Display::getConfigs(EGLConfig *configs, const EGLint *attribList, EGLint configSize, EGLint *numConfig)
169{
170 return mConfigSet.getConfigs(configs, attribList, configSize, numConfig);
171}
172
173bool Display::getConfigAttrib(EGLConfig config, EGLint attribute, EGLint *value)
174{
175 const egl::Config *configuration = mConfigSet.get(config);
176
177 switch (attribute)
178 {
179 case EGL_BUFFER_SIZE: *value = configuration->mBufferSize; break;
180 case EGL_ALPHA_SIZE: *value = configuration->mAlphaSize; break;
181 case EGL_BLUE_SIZE: *value = configuration->mBlueSize; break;
182 case EGL_GREEN_SIZE: *value = configuration->mGreenSize; break;
183 case EGL_RED_SIZE: *value = configuration->mRedSize; break;
184 case EGL_DEPTH_SIZE: *value = configuration->mDepthSize; break;
185 case EGL_STENCIL_SIZE: *value = configuration->mStencilSize; break;
186 case EGL_CONFIG_CAVEAT: *value = configuration->mConfigCaveat; break;
187 case EGL_CONFIG_ID: *value = configuration->mConfigID; break;
188 case EGL_LEVEL: *value = configuration->mLevel; break;
189 case EGL_NATIVE_RENDERABLE: *value = configuration->mNativeRenderable; break;
190 case EGL_NATIVE_VISUAL_TYPE: *value = configuration->mNativeVisualType; break;
191 case EGL_SAMPLES: *value = configuration->mSamples; break;
192 case EGL_SAMPLE_BUFFERS: *value = configuration->mSampleBuffers; break;
193 case EGL_SURFACE_TYPE: *value = configuration->mSurfaceType; break;
194 case EGL_TRANSPARENT_TYPE: *value = configuration->mTransparentType; break;
195 case EGL_TRANSPARENT_BLUE_VALUE: *value = configuration->mTransparentBlueValue; break;
196 case EGL_TRANSPARENT_GREEN_VALUE: *value = configuration->mTransparentGreenValue; break;
197 case EGL_TRANSPARENT_RED_VALUE: *value = configuration->mTransparentRedValue; break;
198 case EGL_BIND_TO_TEXTURE_RGB: *value = configuration->mBindToTextureRGB; break;
199 case EGL_BIND_TO_TEXTURE_RGBA: *value = configuration->mBindToTextureRGBA; break;
200 case EGL_MIN_SWAP_INTERVAL: *value = configuration->mMinSwapInterval; break;
201 case EGL_MAX_SWAP_INTERVAL: *value = configuration->mMaxSwapInterval; break;
202 case EGL_LUMINANCE_SIZE: *value = configuration->mLuminanceSize; break;
203 case EGL_ALPHA_MASK_SIZE: *value = configuration->mAlphaMaskSize; break;
204 case EGL_COLOR_BUFFER_TYPE: *value = configuration->mColorBufferType; break;
205 case EGL_RENDERABLE_TYPE: *value = configuration->mRenderableType; break;
206 case EGL_MATCH_NATIVE_PIXMAP: *value = false; UNIMPLEMENTED(); break;
207 case EGL_CONFORMANT: *value = configuration->mConformant; break;
208 default:
209 return false;
210 }
211
212 return true;
213}
214
215egl::Surface *Display::createWindowSurface(HWND window, EGLConfig config)
216{
217 const egl::Config *configuration = mConfigSet.get(config);
218
219 UINT adapter = D3DADAPTER_DEFAULT;
220 D3DDEVTYPE deviceType = D3DDEVTYPE_HAL;
221 D3DPRESENT_PARAMETERS presentParameters = {0};
222
223 presentParameters.AutoDepthStencilFormat = configuration->mDepthStencilFormat;
224 presentParameters.BackBufferCount = 1;
225 presentParameters.BackBufferFormat = configuration->mRenderTargetFormat;
226 presentParameters.BackBufferWidth = 0;
227 presentParameters.BackBufferHeight = 0;
228 presentParameters.EnableAutoDepthStencil = configuration->mDepthSize ? TRUE : FALSE;
229 presentParameters.Flags = 0;
230 presentParameters.hDeviceWindow = window;
231 presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented
232 presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented
233 presentParameters.PresentationInterval = configuration->mMinSwapInterval;
234 presentParameters.SwapEffect = D3DSWAPEFFECT_COPY;
235 presentParameters.Windowed = TRUE; // FIXME
236
237 IDirect3DSwapChain9 *swapChain = NULL;
238
239 if (!mDevice)
240 {
241 HRESULT result = mD3d9->CreateDevice(adapter, deviceType, window, D3DCREATE_FPU_PRESERVE | D3DCREATE_MIXED_VERTEXPROCESSING | D3DCREATE_NOWINDOWCHANGES, &presentParameters, &mDevice);
242
243 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
244 {
245 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
246 }
247
248 ASSERT(SUCCEEDED(result));
249
250 if (mDevice)
251 {
252 mDevice->GetSwapChain(0, &swapChain);
253 }
254 }
255 else
256 {
257 HRESULT result = mDevice->CreateAdditionalSwapChain(&presentParameters, &swapChain);
258
259 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
260 {
261 return error(EGL_BAD_ALLOC, (egl::Surface*)NULL);
262 }
263
264 ASSERT(SUCCEEDED(result));
265 }
266
267 Surface *surface = NULL;
268
269 if (swapChain)
270 {
271 surface = new Surface(mDevice, swapChain, configuration->mConfigID);
272 mSurfaceSet.insert(surface);
273
274 swapChain->Release();
275 }
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000276
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277 return surface;
278}
279
280EGLContext Display::createContext(EGLConfig configHandle)
281{
282 const egl::Config *config = mConfigSet.get(configHandle);
283
284 gl::Context *context = glCreateContext(config);
285 mContextSet.insert(context);
286
287 return context;
288}
289
290void Display::destroySurface(egl::Surface *surface)
291{
292 delete surface;
293 mSurfaceSet.erase(surface);
294}
295
296void Display::destroyContext(gl::Context *context)
297{
298 glDestroyContext(context);
299 mContextSet.erase(context);
300}
301
302bool Display::isInitialized()
303{
304 return mD3d9 != NULL && mConfigSet.size() > 0;
305}
306
307bool Display::isValidConfig(EGLConfig config)
308{
309 return mConfigSet.get(config) != NULL;
310}
311
312bool Display::isValidContext(gl::Context *context)
313{
314 return mContextSet.find(context) != mContextSet.end();
315}
316
317bool Display::isValidSurface(egl::Surface *surface)
318{
319 return mSurfaceSet.find(surface) != mSurfaceSet.end();
320}
321
322bool Display::hasExistingWindowSurface(HWND window)
323{
324 for (SurfaceSet::iterator surface = mSurfaceSet.begin(); surface != mSurfaceSet.end(); surface++)
325 {
326 if ((*surface)->getWindowHandle() == window)
327 {
328 return true;
329 }
330 }
331
332 return false;
333}
334
335IDirect3DDevice9 *Display::getDevice()
336{
337 return mDevice;
338}
339}