winex11.drv: Correct sizes in COMPOSITIONSTRING structure when updating fields.
[wine.git] / dlls / d3d9 / swapchain.c
blob91ceaf03f4d36e3a6ae5f71a44d10f1c990f2e3b
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 "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 static inline struct d3d9_swapchain *impl_from_IDirect3DSwapChain9Ex(IDirect3DSwapChain9Ex *iface)
30 return CONTAINING_RECORD(iface, struct d3d9_swapchain, IDirect3DSwapChain9Ex_iface);
33 static HRESULT WINAPI d3d9_swapchain_QueryInterface(IDirect3DSwapChain9Ex *iface, REFIID riid, void **out)
35 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
37 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain9)
38 || IsEqualGUID(riid, &IID_IUnknown))
40 IDirect3DSwapChain9Ex_AddRef(iface);
41 *out = iface;
42 return S_OK;
45 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain9Ex))
47 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
48 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(swapchain->parent_device);
50 /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
51 * It doesn't matter with which function the device was created. */
52 if (!device->d3d_parent->extended)
54 WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE.\n");
55 *out = NULL;
56 return E_NOINTERFACE;
59 IDirect3DSwapChain9Ex_AddRef(iface);
60 *out = iface;
61 return S_OK;
64 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
66 *out = NULL;
67 return E_NOINTERFACE;
70 static ULONG WINAPI d3d9_swapchain_AddRef(IDirect3DSwapChain9Ex *iface)
72 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
73 ULONG refcount = InterlockedIncrement(&swapchain->refcount);
75 TRACE("%p increasing refcount to %u.\n", iface, refcount);
77 if (refcount == 1)
79 if (swapchain->parent_device)
80 IDirect3DDevice9Ex_AddRef(swapchain->parent_device);
82 wined3d_mutex_lock();
83 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
84 wined3d_mutex_unlock();
87 return refcount;
90 static ULONG WINAPI d3d9_swapchain_Release(IDirect3DSwapChain9Ex *iface)
92 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
93 ULONG refcount = InterlockedDecrement(&swapchain->refcount);
95 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
97 if (!refcount)
99 IDirect3DDevice9Ex *parent_device = swapchain->parent_device;
101 wined3d_mutex_lock();
102 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
103 wined3d_mutex_unlock();
105 /* Release the device last, as it may cause the device to be destroyed. */
106 if (parent_device)
107 IDirect3DDevice9Ex_Release(parent_device);
110 return refcount;
113 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d9_swapchain_Present(IDirect3DSwapChain9Ex *iface,
114 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
115 const RGNDATA *dirty_region, DWORD flags)
117 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
118 struct d3d9_device *device = impl_from_IDirect3DDevice9Ex(swapchain->parent_device);
119 HRESULT hr;
121 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p, flags %#x.\n",
122 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect),
123 dst_window_override, dirty_region, flags);
125 if (device->device_state != D3D9_DEVICE_STATE_OK)
126 return device->d3d_parent->extended ? S_PRESENT_OCCLUDED : D3DERR_DEVICELOST;
128 if (dirty_region)
129 FIXME("Ignoring dirty_region %p.\n", dirty_region);
131 wined3d_mutex_lock();
132 hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
133 src_rect, dst_rect, dst_window_override, flags);
134 wined3d_mutex_unlock();
136 return hr;
139 static HRESULT WINAPI d3d9_swapchain_GetFrontBufferData(IDirect3DSwapChain9Ex *iface, IDirect3DSurface9 *surface)
141 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
142 struct d3d9_surface *dst = unsafe_impl_from_IDirect3DSurface9(surface);
143 HRESULT hr;
145 TRACE("iface %p, surface %p.\n", iface, surface);
147 wined3d_mutex_lock();
148 hr = wined3d_swapchain_get_front_buffer_data(swapchain->wined3d_swapchain, dst->wined3d_texture, dst->sub_resource_idx);
149 wined3d_mutex_unlock();
151 return hr;
154 static HRESULT WINAPI d3d9_swapchain_GetBackBuffer(IDirect3DSwapChain9Ex *iface,
155 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface9 **backbuffer)
157 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
158 struct wined3d_texture *wined3d_texture;
159 struct d3d9_surface *surface_impl;
160 HRESULT hr = D3D_OK;
162 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
163 iface, backbuffer_idx, backbuffer_type, backbuffer);
165 /* backbuffer_type is ignored by native. */
167 if (!backbuffer)
169 WARN("The output pointer is NULL, returning D3DERR_INVALIDCALL.\n");
170 return D3DERR_INVALIDCALL;
173 wined3d_mutex_lock();
174 if ((wined3d_texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, backbuffer_idx)))
176 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
177 *backbuffer = &surface_impl->IDirect3DSurface9_iface;
178 IDirect3DSurface9_AddRef(*backbuffer);
180 else
182 /* Do not set *backbuffer = NULL, see tests/device.c, test_swapchain(). */
183 hr = D3DERR_INVALIDCALL;
185 wined3d_mutex_unlock();
187 return hr;
190 static HRESULT WINAPI d3d9_swapchain_GetRasterStatus(IDirect3DSwapChain9Ex *iface, D3DRASTER_STATUS *raster_status)
192 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
193 HRESULT hr;
195 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
197 wined3d_mutex_lock();
198 hr = wined3d_swapchain_get_raster_status(swapchain->wined3d_swapchain,
199 (struct wined3d_raster_status *)raster_status);
200 wined3d_mutex_unlock();
202 return hr;
205 static HRESULT WINAPI d3d9_swapchain_GetDisplayMode(IDirect3DSwapChain9Ex *iface, D3DDISPLAYMODE *mode)
207 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
208 struct wined3d_display_mode wined3d_mode;
209 HRESULT hr;
211 TRACE("iface %p, mode %p.\n", iface, mode);
213 wined3d_mutex_lock();
214 hr = wined3d_swapchain_get_display_mode(swapchain->wined3d_swapchain, &wined3d_mode, NULL);
215 wined3d_mutex_unlock();
217 if (SUCCEEDED(hr))
219 mode->Width = wined3d_mode.width;
220 mode->Height = wined3d_mode.height;
221 mode->RefreshRate = wined3d_mode.refresh_rate;
222 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
225 return hr;
228 static HRESULT WINAPI d3d9_swapchain_GetDevice(IDirect3DSwapChain9Ex *iface, IDirect3DDevice9 **device)
230 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
232 TRACE("iface %p, device %p.\n", iface, device);
234 *device = (IDirect3DDevice9 *)swapchain->parent_device;
235 IDirect3DDevice9_AddRef(*device);
237 TRACE("Returning device %p.\n", *device);
239 return D3D_OK;
242 static HRESULT WINAPI d3d9_swapchain_GetPresentParameters(IDirect3DSwapChain9Ex *iface,
243 D3DPRESENT_PARAMETERS *parameters)
245 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
246 struct wined3d_swapchain_desc desc;
248 TRACE("iface %p, parameters %p.\n", iface, parameters);
250 wined3d_mutex_lock();
251 wined3d_swapchain_get_desc(swapchain->wined3d_swapchain, &desc);
252 wined3d_mutex_unlock();
253 present_parameters_from_wined3d_swapchain_desc(parameters, &desc);
255 return D3D_OK;
258 static HRESULT WINAPI d3d9_swapchain_GetLastPresentCount(IDirect3DSwapChain9Ex *iface,
259 UINT *last_present_count)
261 FIXME("iface %p, last_present_count %p, stub!\n", iface, last_present_count);
263 if (last_present_count)
264 *last_present_count = 0;
266 return D3D_OK;
269 static HRESULT WINAPI d3d9_swapchain_GetPresentStatistics(IDirect3DSwapChain9Ex *iface,
270 D3DPRESENTSTATS *present_stats)
272 FIXME("iface %p, present_stats %p, stub!\n", iface, present_stats);
274 if (present_stats)
275 memset(present_stats, 0, sizeof(*present_stats));
277 return D3D_OK;
280 static HRESULT WINAPI d3d9_swapchain_GetDisplayModeEx(IDirect3DSwapChain9Ex *iface,
281 D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
283 struct d3d9_swapchain *swapchain = impl_from_IDirect3DSwapChain9Ex(iface);
284 struct wined3d_display_mode wined3d_mode;
285 HRESULT hr;
287 TRACE("iface %p, mode %p, rotation %p.\n", iface, mode, rotation);
289 if (mode->Size != sizeof(*mode))
290 return D3DERR_INVALIDCALL;
292 wined3d_mutex_lock();
293 hr = wined3d_swapchain_get_display_mode(swapchain->wined3d_swapchain, &wined3d_mode,
294 (enum wined3d_display_rotation *)rotation);
295 wined3d_mutex_unlock();
297 if (SUCCEEDED(hr))
299 mode->Width = wined3d_mode.width;
300 mode->Height = wined3d_mode.height;
301 mode->RefreshRate = wined3d_mode.refresh_rate;
302 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
303 mode->ScanLineOrdering = wined3d_mode.scanline_ordering;
306 return hr;
309 static const struct IDirect3DSwapChain9ExVtbl d3d9_swapchain_vtbl =
311 /* IUnknown */
312 d3d9_swapchain_QueryInterface,
313 d3d9_swapchain_AddRef,
314 d3d9_swapchain_Release,
315 /* IDirect3DSwapChain9 */
316 d3d9_swapchain_Present,
317 d3d9_swapchain_GetFrontBufferData,
318 d3d9_swapchain_GetBackBuffer,
319 d3d9_swapchain_GetRasterStatus,
320 d3d9_swapchain_GetDisplayMode,
321 d3d9_swapchain_GetDevice,
322 d3d9_swapchain_GetPresentParameters,
323 /* IDirect3DSwapChain9Ex */
324 d3d9_swapchain_GetLastPresentCount,
325 d3d9_swapchain_GetPresentStatistics,
326 d3d9_swapchain_GetDisplayModeEx
329 static void STDMETHODCALLTYPE d3d9_swapchain_wined3d_object_released(void *parent)
331 HeapFree(GetProcessHeap(), 0, parent);
334 static const struct wined3d_parent_ops d3d9_swapchain_wined3d_parent_ops =
336 d3d9_swapchain_wined3d_object_released,
339 static HRESULT swapchain_init(struct d3d9_swapchain *swapchain, struct d3d9_device *device,
340 struct wined3d_swapchain_desc *desc)
342 HRESULT hr;
344 swapchain->refcount = 1;
345 swapchain->IDirect3DSwapChain9Ex_iface.lpVtbl = &d3d9_swapchain_vtbl;
347 wined3d_mutex_lock();
348 hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
349 &d3d9_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain);
350 wined3d_mutex_unlock();
352 if (FAILED(hr))
354 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
355 return hr;
358 swapchain->parent_device = &device->IDirect3DDevice9Ex_iface;
359 IDirect3DDevice9Ex_AddRef(swapchain->parent_device);
361 return D3D_OK;
364 HRESULT d3d9_swapchain_create(struct d3d9_device *device, struct wined3d_swapchain_desc *desc,
365 struct d3d9_swapchain **swapchain)
367 struct d3d9_swapchain *object;
368 HRESULT hr;
370 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
371 return E_OUTOFMEMORY;
373 if (FAILED(hr = swapchain_init(object, device, desc)))
375 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
376 HeapFree(GetProcessHeap(), 0, object);
377 return hr;
380 TRACE("Created swapchain %p.\n", object);
381 *swapchain = object;
383 return D3D_OK;