widl: Always check the runtimeclass interfaces presence.
[wine/zf.git] / dlls / d3d9 / swapchain.c
blob0955b672c798524019a86466ffdefad7e5782db1
1 /*
2 * IDirect3DSwapChain9 implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static DWORD d3dpresentationinterval_from_wined3dswapinterval(enum wined3d_swap_interval interval)
29 switch (interval)
31 case WINED3D_SWAP_INTERVAL_IMMEDIATE:
32 return D3DPRESENT_INTERVAL_IMMEDIATE;
33 case WINED3D_SWAP_INTERVAL_ONE:
34 return D3DPRESENT_INTERVAL_ONE;
35 case WINED3D_SWAP_INTERVAL_TWO:
36 return D3DPRESENT_INTERVAL_TWO;
37 case WINED3D_SWAP_INTERVAL_THREE:
38 return D3DPRESENT_INTERVAL_THREE;
39 case WINED3D_SWAP_INTERVAL_FOUR:
40 return D3DPRESENT_INTERVAL_FOUR;
41 default:
42 ERR("Invalid swap interval %#x.\n", interval);
43 case WINED3D_SWAP_INTERVAL_DEFAULT:
44 return D3DPRESENT_INTERVAL_DEFAULT;
48 static inline struct d3d9_swapchain *impl_from_IDirect3DSwapChain9Ex(IDirect3DSwapChain9Ex *iface)
50 return CONTAINING_RECORD(iface, struct d3d9_swapchain, IDirect3DSwapChain9Ex_iface);
53 static HRESULT WINAPI d3d9_swapchain_QueryInterface(IDirect3DSwapChain9Ex *iface, REFIID riid, void **out)
55 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
57 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain9)
58 || IsEqualGUID(riid, &IID_IUnknown))
60 IDirect3DSwapChain9Ex_AddRef(iface);
61 *out = iface;
62 return S_OK;
65 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain9Ex))
67 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
68 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(swapchain->parent_device);
70 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
71 * It doesn't matter with which function the device was created. */
72 if (!device->d3d_parent->extended)
74 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
75 *out = NULL;
76 return E_NOINTERFACE;
79 IDirect3DSwapChain9Ex_AddRef(iface);
80 *out = iface;
81 return S_OK;
84 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
86 *out = NULL;
87 return E_NOINTERFACE;
90 static ULONG WINAPI d3d9_swapchain_AddRef(IDirect3DSwapChain9Ex *iface)
92 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
93 ULONG refcount = InterlockedIncrement(&swapchain->refcount);
95 TRACE("%p increasing refcount to %u.\n", iface, refcount);
97 if (refcount == 1)
99 if (swapchain->parent_device)
100 IDirect3DDevice9Ex_AddRef(swapchain->parent_device);
102 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
105 return refcount;
108 static ULONG WINAPI d3d9_swapchain_Release(IDirect3DSwapChain9Ex *iface)
110 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
111 ULONG refcount;
113 if (!swapchain->refcount)
115 WARN("Swapchain does not have any references.\n");
116 return 0;
119 refcount = InterlockedDecrement(&swapchain->refcount);
120 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
122 if (!refcount)
124 IDirect3DDevice9Ex *parent_device = swapchain->parent_device;
126 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
128 /* Release the device last, as it may cause the device to be destroyed. */
129 if (parent_device)
130 IDirect3DDevice9Ex_Release(parent_device);
133 return refcount;
136 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_swapchain_Present(IDirect3DSwapChain9Ex *iface,
137 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
138 const RGNDATA *dirty_region, DWORD flags)
140 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
141 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(swapchain->parent_device);
143 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
144 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
145 dst_window_override, dirty_region, flags);
147 if (device->device_state != D3D9_DEVICE_STATE_OK)
148 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
150 if (dirty_region)
151 FIXME("Ignoring dirty_region %p.\n", dirty_region);
153 return wined3d_swapchain_present(swapchain->wined3d_swapchain,
154 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, flags);
157 static HRESULT WINAPI d3d9_swapchain_GetFrontBufferData(IDirect3DSwapChain9Ex *iface, IDirect3DSurface9 *surface)
159 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
160 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(surface);
161 HRESULT hr;
163 TRACE("iface %p, surface %p.\n", iface, surface);
165 wined3d_mutex_lock();
166 hr = wined3d_swapchain_get_front_buffer_data(swapchain->wined3d_swapchain, dst->wined3d_texture, dst->sub_resource_idx);
167 wined3d_mutex_unlock();
169 return hr;
172 static HRESULT WINAPI d3d9_swapchain_GetBackBuffer(IDirect3DSwapChain9Ex *iface,
173 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
175 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
176 struct wined3d_texture *wined3d_texture;
177 struct d3d9_surface *surface_impl;
178 HRESULT hr = D3D_OK;
180 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
181 iface, backbuffer_idx, backbuffer_type, backbuffer);
183 /* backbuffer_type is ignored by native. */
185 if (!backbuffer)
187 WARN("The output pointer is NULL, returning D3DERR_INVALIDCALL.\n");
188 return D3DERR_INVALIDCALL;
191 wined3d_mutex_lock();
192 if ((wined3d_texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, backbuffer_idx)))
194 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
195 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
196 IDirect3DSurface9_AddRef(*backbuffer);
198 else
200 /* Do not set *backbuffer = NULL, see tests/device.c, test_swapchain(). */
201 hr = D3DERR_INVALIDCALL;
203 wined3d_mutex_unlock();
205 return hr;
208 static HRESULT WINAPI d3d9_swapchain_GetRasterStatus(IDirect3DSwapChain9Ex *iface, D3DRASTER_STATUS *raster_status)
210 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
211 HRESULT hr;
213 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
215 wined3d_mutex_lock();
216 hr = wined3d_swapchain_get_raster_status(swapchain->wined3d_swapchain,
217 (struct wined3d_raster_status *)raster_status);
218 wined3d_mutex_unlock();
220 return hr;
223 static HRESULT WINAPI d3d9_swapchain_GetDisplayMode(IDirect3DSwapChain9Ex *iface, D3DDISPLAYMODE *mode)
225 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
226 struct wined3d_display_mode wined3d_mode;
227 HRESULT hr;
229 TRACE("iface %p, mode %p.\n", iface, mode);
231 wined3d_mutex_lock();
232 hr = wined3d_swapchain_get_display_mode(swapchain->wined3d_swapchain, &wined3d_mode, NULL);
233 wined3d_mutex_unlock();
235 if (SUCCEEDED(hr))
237 mode->Width = wined3d_mode.width;
238 mode->Height = wined3d_mode.height;
239 mode->RefreshRate = wined3d_mode.refresh_rate;
240 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
243 return hr;
246 static HRESULT WINAPI d3d9_swapchain_GetDevice(IDirect3DSwapChain9Ex *iface, IDirect3DDevice9 **device)
248 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
250 TRACE("iface %p, device %p.\n", iface, device);
252 *device = (IDirect3DDevice9 *)swapchain->parent_device;
253 IDirect3DDevice9_AddRef(*device);
255 TRACE("Returning device %p.\n", *device);
257 return D3D_OK;
260 static HRESULT WINAPI d3d9_swapchain_GetPresentParameters(IDirect3DSwapChain9Ex *iface,
261 D3DPRESENT_PARAMETERS *parameters)
263 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
264 struct wined3d_swapchain_desc desc;
265 DWORD presentation_interval;
267 TRACE("iface %p, parameters %p.\n", iface, parameters);
269 wined3d_mutex_lock();
270 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &desc);
271 presentation_interval = d3dpresentationinterval_from_wined3dswapinterval(swapchain->swap_interval);
272 wined3d_mutex_unlock();
273 present_parameters_from_wined3d_swapchain_desc(parameters, &desc, presentation_interval);
275 return D3D_OK;
278 static HRESULT WINAPI d3d9_swapchain_GetLastPresentCount(IDirect3DSwapChain9Ex *iface,
279 UINT *last_present_count)
281 FIXME("iface %p, last_present_count %p, stub!\n", iface, last_present_count);
283 if (last_present_count)
284 *last_present_count = 0;
286 return D3D_OK;
289 static HRESULT WINAPI d3d9_swapchain_GetPresentStatistics(IDirect3DSwapChain9Ex *iface,
290 D3DPRESENTSTATS *present_stats)
292 FIXME("iface %p, present_stats %p, stub!\n", iface, present_stats);
294 if (present_stats)
295 memset(present_stats, 0, sizeof(*present_stats));
297 return D3D_OK;
300 static HRESULT WINAPI d3d9_swapchain_GetDisplayModeEx(IDirect3DSwapChain9Ex *iface,
301 D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
303 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
304 struct wined3d_display_mode wined3d_mode;
305 HRESULT hr;
307 TRACE("iface %p, mode %p, rotation %p.\n", iface, mode, rotation);
309 if (mode->Size != sizeof(*mode))
310 return D3DERR_INVALIDCALL;
312 wined3d_mutex_lock();
313 hr = wined3d_swapchain_get_display_mode(swapchain->wined3d_swapchain, &wined3d_mode,
314 (enum wined3d_display_rotation *)rotation);
315 wined3d_mutex_unlock();
317 if (SUCCEEDED(hr))
319 mode->Width = wined3d_mode.width;
320 mode->Height = wined3d_mode.height;
321 mode->RefreshRate = wined3d_mode.refresh_rate;
322 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
323 mode->ScanLineOrdering = d3dscanlineordering_from_wined3d(wined3d_mode.scanline_ordering);
326 return hr;
329 static const struct IDirect3DSwapChain9ExVtbl d3d9_swapchain_vtbl =
331 /* IUnknown */
332 d3d9_swapchain_QueryInterface,
333 d3d9_swapchain_AddRef,
334 d3d9_swapchain_Release,
335 /* IDirect3DSwapChain9 */
336 d3d9_swapchain_Present,
337 d3d9_swapchain_GetFrontBufferData,
338 d3d9_swapchain_GetBackBuffer,
339 d3d9_swapchain_GetRasterStatus,
340 d3d9_swapchain_GetDisplayMode,
341 d3d9_swapchain_GetDevice,
342 d3d9_swapchain_GetPresentParameters,
343 /* IDirect3DSwapChain9Ex */
344 d3d9_swapchain_GetLastPresentCount,
345 d3d9_swapchain_GetPresentStatistics,
346 d3d9_swapchain_GetDisplayModeEx
349 static void STDMETHODCALLTYPE d3d9_swapchain_wined3d_object_released(void *parent)
351 heap_free(parent);
354 static const struct wined3d_parent_ops d3d9_swapchain_wined3d_parent_ops =
356 d3d9_swapchain_wined3d_object_released,
359 static void CDECL d3d9_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
360 BOOL windowed)
362 TRACE("parent %p, windowed %d.\n", parent, windowed);
365 static const struct wined3d_swapchain_state_parent_ops d3d9_swapchain_state_parent_ops =
367 d3d9_swapchain_windowed_state_changed,
370 static HRESULT swapchain_init(struct d3d9_swapchain *swapchain, struct d3d9_device *device,
371 struct wined3d_swapchain_desc *desc, unsigned int swap_interval)
373 HRESULT hr;
375 swapchain->refcount = 1;
376 swapchain->IDirect3DSwapChain9Ex_iface.lpVtbl = &d3d9_swapchain_vtbl;
377 swapchain->state_parent.ops = &d3d9_swapchain_state_parent_ops;
378 swapchain->swap_interval = swap_interval;
380 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, &swapchain->state_parent,
381 swapchain, &d3d9_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
383 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
384 return hr;
387 swapchain->parent_device = &device->IDirect3DDevice9Ex_iface;
388 IDirect3DDevice9Ex_AddRef(swapchain->parent_device);
390 return D3D_OK;
393 HRESULT d3d9_swapchain_create(struct d3d9_device *device, struct wined3d_swapchain_desc *desc,
394 unsigned int swap_interval, struct d3d9_swapchain **swapchain)
396 struct d3d9_swapchain *object;
397 HRESULT hr;
399 if (!(object = heap_alloc_zero(sizeof(*object))))
400 return E_OUTOFMEMORY;
402 if (FAILED(hr = swapchain_init(object, device, desc, swap_interval)))
404 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
405 heap_free(object);
406 return hr;
409 TRACE("Created swapchain %p.\n", object);
410 *swapchain = object;
412 return D3D_OK;