80c7e7d199bbb79179e6e8c093dba3d59267c5d1
[reactos.git] / reactos / dll / directx / wine / d3d8 / swapchain.c
1 /*
2 * IDirect3DSwapChain8 implementation
3 *
4 * Copyright 2005 Oliver Stieber
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "d3d8_private.h"
22
23 static inline struct d3d8_swapchain *impl_from_IDirect3DSwapChain8(IDirect3DSwapChain8 *iface)
24 {
25 return CONTAINING_RECORD(iface, struct d3d8_swapchain, IDirect3DSwapChain8_iface);
26 }
27
28 static HRESULT WINAPI d3d8_swapchain_QueryInterface(IDirect3DSwapChain8 *iface, REFIID riid, void **out)
29 {
30 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
31
32 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain8)
33 || IsEqualGUID(riid, &IID_IUnknown))
34 {
35 IDirect3DSwapChain8_AddRef(iface);
36 *out = iface;
37 return S_OK;
38 }
39
40 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
41
42 *out = NULL;
43 return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI d3d8_swapchain_AddRef(IDirect3DSwapChain8 *iface)
47 {
48 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
49 ULONG ref = InterlockedIncrement(&swapchain->refcount);
50
51 TRACE("%p increasing refcount to %u.\n", iface, ref);
52
53 if (ref == 1)
54 {
55 if (swapchain->parent_device)
56 IDirect3DDevice8_AddRef(swapchain->parent_device);
57 wined3d_mutex_lock();
58 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
59 wined3d_mutex_unlock();
60 }
61
62 return ref;
63 }
64
65 static ULONG WINAPI d3d8_swapchain_Release(IDirect3DSwapChain8 *iface)
66 {
67 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
68 ULONG ref = InterlockedDecrement(&swapchain->refcount);
69
70 TRACE("%p decreasing refcount to %u.\n", iface, ref);
71
72 if (!ref)
73 {
74 IDirect3DDevice8 *parent_device = swapchain->parent_device;
75
76 wined3d_mutex_lock();
77 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
78 wined3d_mutex_unlock();
79
80 if (parent_device)
81 IDirect3DDevice8_Release(parent_device);
82 }
83 return ref;
84 }
85
86 static HRESULT WINAPI d3d8_swapchain_Present(IDirect3DSwapChain8 *iface,
87 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
88 const RGNDATA *dirty_region)
89 {
90 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
91 HRESULT hr;
92
93 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
94 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
95
96 wined3d_mutex_lock();
97 hr = wined3d_swapchain_present(swapchain->wined3d_swapchain, src_rect,
98 dst_rect, dst_window_override, dirty_region, 0);
99 wined3d_mutex_unlock();
100
101 return hr;
102 }
103
104 static HRESULT WINAPI d3d8_swapchain_GetBackBuffer(IDirect3DSwapChain8 *iface,
105 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
106 {
107 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
108 struct wined3d_surface *wined3d_surface = NULL;
109 struct d3d8_surface *surface_impl;
110 HRESULT hr = D3D_OK;
111
112 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
113 iface, backbuffer_idx, backbuffer_type, backbuffer);
114
115 wined3d_mutex_lock();
116 if ((wined3d_surface = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain,
117 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type)))
118 {
119 surface_impl = wined3d_surface_get_parent(wined3d_surface);
120 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
121 IDirect3DSurface8_AddRef(*backbuffer);
122 }
123 else
124 {
125 hr = D3DERR_INVALIDCALL;
126 }
127 wined3d_mutex_unlock();
128
129 return hr;
130 }
131
132 static const IDirect3DSwapChain8Vtbl d3d8_swapchain_vtbl =
133 {
134 d3d8_swapchain_QueryInterface,
135 d3d8_swapchain_AddRef,
136 d3d8_swapchain_Release,
137 d3d8_swapchain_Present,
138 d3d8_swapchain_GetBackBuffer
139 };
140
141 static void STDMETHODCALLTYPE d3d8_swapchain_wined3d_object_released(void *parent)
142 {
143 HeapFree(GetProcessHeap(), 0, parent);
144 }
145
146 static const struct wined3d_parent_ops d3d8_swapchain_wined3d_parent_ops =
147 {
148 d3d8_swapchain_wined3d_object_released,
149 };
150
151 static HRESULT swapchain_init(struct d3d8_swapchain *swapchain, struct d3d8_device *device,
152 struct wined3d_swapchain_desc *desc)
153 {
154 HRESULT hr;
155
156 swapchain->refcount = 1;
157 swapchain->IDirect3DSwapChain8_iface.lpVtbl = &d3d8_swapchain_vtbl;
158
159 wined3d_mutex_lock();
160 hr = wined3d_swapchain_create(device->wined3d_device, desc, swapchain,
161 &d3d8_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain);
162 wined3d_mutex_unlock();
163
164 if (FAILED(hr))
165 {
166 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
167 return hr;
168 }
169
170 swapchain->parent_device = &device->IDirect3DDevice8_iface;
171 IDirect3DDevice8_AddRef(swapchain->parent_device);
172
173 return D3D_OK;
174 }
175
176 HRESULT d3d8_swapchain_create(struct d3d8_device *device, struct wined3d_swapchain_desc *desc,
177 struct d3d8_swapchain **swapchain)
178 {
179 struct d3d8_swapchain *object;
180 HRESULT hr;
181
182 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
183 return E_OUTOFMEMORY;
184
185 if (FAILED(hr = swapchain_init(object, device, desc)))
186 {
187 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
188 HeapFree(GetProcessHeap(), 0, object);
189 return hr;
190 }
191
192 TRACE("Created swapchain %p.\n", object);
193 *swapchain = object;
194
195 return D3D_OK;
196 }