ddraw/tests: Rewrite StructSizeTest().
[wine.git] / dlls / d3d8 / swapchain.c
blob6fd30e5ba47454856e915fe15d2b97b4fef97d55
1 /*
2 * IDirect3DSwapChain8 implementation
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 static inline struct d3d8_swapchain *impl_from_IDirect3DSwapChain8(IDirect3DSwapChain8 *iface)
28 return CONTAINING_RECORD(iface, struct d3d8_swapchain, IDirect3DSwapChain8_iface);
31 static HRESULT WINAPI d3d8_swapchain_QueryInterface(IDirect3DSwapChain8 *iface, REFIID riid, void **out)
33 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
35 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain8)
36 || IsEqualGUID(riid, &IID_IUnknown))
38 IDirect3DSwapChain8_AddRef(iface);
39 *out = iface;
40 return S_OK;
43 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
45 *out = NULL;
46 return E_NOINTERFACE;
49 static ULONG WINAPI d3d8_swapchain_AddRef(IDirect3DSwapChain8 *iface)
51 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
52 ULONG ref = InterlockedIncrement(&swapchain->refcount);
54 TRACE("%p increasing refcount to %u.\n", iface, ref);
56 if (ref == 1)
58 if (swapchain->parent_device)
59 IDirect3DDevice8_AddRef(swapchain->parent_device);
60 wined3d_mutex_lock();
61 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
62 wined3d_mutex_unlock();
65 return ref;
68 static ULONG WINAPI d3d8_swapchain_Release(IDirect3DSwapChain8 *iface)
70 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
71 ULONG ref = InterlockedDecrement(&swapchain->refcount);
73 TRACE("%p decreasing refcount to %u.\n", iface, ref);
75 if (!ref)
77 IDirect3DDevice8 *parent_device = swapchain->parent_device;
79 wined3d_mutex_lock();
80 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
81 wined3d_mutex_unlock();
83 if (parent_device)
84 IDirect3DDevice8_Release(parent_device);
86 return ref;
89 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_swapchain_Present(IDirect3DSwapChain8 *iface,
90 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
91 const RGNDATA *dirty_region)
93 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
94 struct d3d8_device *device = impl_from_IDirect3DDevice8(swapchain->parent_device);
95 HRESULT hr;
97 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
98 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
100 if (device->device_state != D3D8_DEVICE_STATE_OK)
101 return D3DERR_DEVICELOST;
103 if (dirty_region)
104 FIXME("Ignoring dirty_region %p.\n", dirty_region);
106 wined3d_mutex_lock();
107 hr = wined3d_swapchain_present(swapchain->wined3d_swapchain,
108 src_rect, dst_rect, dst_window_override, 0);
109 wined3d_mutex_unlock();
111 return hr;
114 static HRESULT WINAPI d3d8_swapchain_GetBackBuffer(IDirect3DSwapChain8 *iface,
115 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
117 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
118 struct wined3d_texture *wined3d_texture;
119 struct d3d8_surface *surface_impl;
120 HRESULT hr = D3D_OK;
122 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
123 iface, backbuffer_idx, backbuffer_type, backbuffer);
125 /* backbuffer_type is ignored by native. */
127 if (!backbuffer)
129 WARN("The output pointer is NULL, returning D3DERR_INVALIDCALL.\n");
130 return D3DERR_INVALIDCALL;
133 wined3d_mutex_lock();
134 if ((wined3d_texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, backbuffer_idx)))
136 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
137 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
138 IDirect3DSurface8_AddRef(*backbuffer);
140 else
142 /* Do not set *backbuffer = NULL, see tests/device.c, test_swapchain(). */
143 hr = D3DERR_INVALIDCALL;
145 wined3d_mutex_unlock();
147 return hr;
150 static const IDirect3DSwapChain8Vtbl d3d8_swapchain_vtbl =
152 d3d8_swapchain_QueryInterface,
153 d3d8_swapchain_AddRef,
154 d3d8_swapchain_Release,
155 d3d8_swapchain_Present,
156 d3d8_swapchain_GetBackBuffer
159 static void STDMETHODCALLTYPE d3d8_swapchain_wined3d_object_released(void *parent)
161 HeapFree(GetProcessHeap(), 0, parent);
164 static const struct wined3d_parent_ops d3d8_swapchain_wined3d_parent_ops =
166 d3d8_swapchain_wined3d_object_released,
169 static HRESULT swapchain_init(struct d3d8_swapchain *swapchain, struct d3d8_device *device,
170 struct wined3d_swapchain_desc *desc)
172 HRESULT hr;
174 swapchain->refcount = 1;
175 swapchain->IDirect3DSwapChain8_iface.lpVtbl = &d3d8_swapchain_vtbl;
177 wined3d_mutex_lock();
178 hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
179 &d3d8_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain);
180 wined3d_mutex_unlock();
182 if (FAILED(hr))
184 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
185 return hr;
188 swapchain->parent_device = &device->IDirect3DDevice8_iface;
189 IDirect3DDevice8_AddRef(swapchain->parent_device);
191 return D3D_OK;
194 HRESULT d3d8_swapchain_create(struct d3d8_device *device, struct wined3d_swapchain_desc *desc,
195 struct d3d8_swapchain **swapchain)
197 struct d3d8_swapchain *object;
198 HRESULT hr;
200 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
201 return E_OUTOFMEMORY;
203 if (FAILED(hr = swapchain_init(object, device, desc)))
205 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
206 HeapFree(GetProcessHeap(), 0, object);
207 return hr;
210 TRACE("Created swapchain %p.\n", object);
211 *swapchain = object;
213 return D3D_OK;