[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / d3d8 / surface.c
1 /*
2 * IDirect3DSurface8 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_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
24 {
25 return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
26 }
27
28 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *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_IDirect3DSurface8)
33 || IsEqualGUID(riid, &IID_IDirect3DResource8)
34 || IsEqualGUID(riid, &IID_IUnknown))
35 {
36 IDirect3DSurface8_AddRef(iface);
37 *out = iface;
38 return S_OK;
39 }
40
41 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
42
43 *out = NULL;
44 return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
48 {
49 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
50 ULONG refcount;
51
52 TRACE("iface %p.\n", iface);
53
54 if (surface->texture)
55 {
56 TRACE("Forwarding to %p.\n", surface->texture);
57 return IDirect3DBaseTexture8_AddRef(&surface->texture->IDirect3DBaseTexture8_iface);
58 }
59
60 refcount = InterlockedIncrement(&surface->resource.refcount);
61 TRACE("%p increasing refcount to %u.\n", iface, refcount);
62
63 if (refcount == 1)
64 {
65 if (surface->parent_device)
66 IDirect3DDevice8_AddRef(surface->parent_device);
67 wined3d_mutex_lock();
68 if (surface->wined3d_rtv)
69 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
70 wined3d_texture_incref(surface->wined3d_texture);
71 wined3d_mutex_unlock();
72 }
73
74 return refcount;
75 }
76
77 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
78 {
79 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
80 ULONG refcount;
81
82 TRACE("iface %p.\n", iface);
83
84 if (surface->texture)
85 {
86 TRACE("Forwarding to %p.\n", surface->texture);
87 return IDirect3DBaseTexture8_Release(&surface->texture->IDirect3DBaseTexture8_iface);
88 }
89
90 if (!surface->resource.refcount)
91 {
92 WARN("Surface does not have any references.\n");
93 return 0;
94 }
95
96 refcount = InterlockedDecrement(&surface->resource.refcount);
97 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
98
99 if (!refcount)
100 {
101 IDirect3DDevice8 *parent_device = surface->parent_device;
102
103 wined3d_mutex_lock();
104 if (surface->wined3d_rtv)
105 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
106 wined3d_texture_decref(surface->wined3d_texture);
107 wined3d_mutex_unlock();
108
109 if (parent_device)
110 IDirect3DDevice8_Release(parent_device);
111 }
112
113 return refcount;
114 }
115
116 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
117 {
118 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
119
120 TRACE("iface %p, device %p.\n", iface, device);
121
122 if (surface->texture)
123 return IDirect3DBaseTexture8_GetDevice(&surface->texture->IDirect3DBaseTexture8_iface, device);
124
125 *device = surface->parent_device;
126 IDirect3DDevice8_AddRef(*device);
127
128 TRACE("Returning device %p.\n", *device);
129
130 return D3D_OK;
131 }
132
133 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
134 const void *data, DWORD data_size, DWORD flags)
135 {
136 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
137 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
138 iface, debugstr_guid(guid), data, data_size, flags);
139
140 return d3d8_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
141 }
142
143 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
144 void *data, DWORD *data_size)
145 {
146 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
147 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
148 iface, debugstr_guid(guid), data, data_size);
149
150 return d3d8_resource_get_private_data(&surface->resource, guid, data, data_size);
151 }
152
153 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
154 {
155 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
156 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
157
158 return d3d8_resource_free_private_data(&surface->resource, guid);
159 }
160
161 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
162 {
163 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
164 HRESULT hr;
165
166 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
167
168 if (!surface->container)
169 return E_NOINTERFACE;
170
171 hr = IUnknown_QueryInterface(surface->container, riid, container);
172
173 TRACE("Returning %p.\n", *container);
174
175 return hr;
176 }
177
178 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
179 {
180 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
181 struct wined3d_sub_resource_desc wined3d_desc;
182
183 TRACE("iface %p, desc %p.\n", iface, desc);
184
185 wined3d_mutex_lock();
186 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
187 wined3d_mutex_unlock();
188
189 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
190 desc->Type = D3DRTYPE_SURFACE;
191 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
192 desc->Pool = wined3d_desc.pool;
193 desc->Size = wined3d_desc.size;
194 desc->MultiSampleType = wined3d_desc.multisample_type;
195 desc->Width = wined3d_desc.width;
196 desc->Height = wined3d_desc.height;
197
198 return D3D_OK;
199 }
200
201 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
202 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
203 {
204 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
205 struct wined3d_box box;
206 struct wined3d_map_desc map_desc;
207 HRESULT hr;
208 D3DRESOURCETYPE type;
209
210 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
211 iface, locked_rect, wine_dbgstr_rect(rect), flags);
212
213 wined3d_mutex_lock();
214
215 if (surface->texture)
216 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
217 else
218 type = D3DRTYPE_SURFACE;
219
220 if (rect)
221 {
222 D3DSURFACE_DESC desc;
223 IDirect3DSurface8_GetDesc(iface, &desc);
224
225 if (type != D3DRTYPE_TEXTURE
226 && ((rect->left < 0)
227 || (rect->top < 0)
228 || (rect->left >= rect->right)
229 || (rect->top >= rect->bottom)
230 || (rect->right > desc.Width)
231 || (rect->bottom > desc.Height)))
232 {
233 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
234 wined3d_mutex_unlock();
235
236 locked_rect->Pitch = 0;
237 locked_rect->pBits = NULL;
238 return D3DERR_INVALIDCALL;
239 }
240 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
241 }
242
243 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
244 &map_desc, rect ? &box : NULL, flags);
245 wined3d_mutex_unlock();
246
247 if (SUCCEEDED(hr))
248 {
249 locked_rect->Pitch = map_desc.row_pitch;
250 locked_rect->pBits = map_desc.data;
251 }
252 else if (type != D3DRTYPE_TEXTURE)
253 {
254 locked_rect->Pitch = 0;
255 locked_rect->pBits = NULL;
256 }
257
258 return hr;
259 }
260
261 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
262 {
263 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
264 HRESULT hr;
265
266 TRACE("iface %p.\n", iface);
267
268 wined3d_mutex_lock();
269 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
270 wined3d_mutex_unlock();
271
272 if (hr == WINEDDERR_NOTLOCKED)
273 {
274 D3DRESOURCETYPE type;
275 if (surface->texture)
276 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
277 else
278 type = D3DRTYPE_SURFACE;
279 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
280 }
281 return hr;
282 }
283
284 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
285 {
286 /* IUnknown */
287 d3d8_surface_QueryInterface,
288 d3d8_surface_AddRef,
289 d3d8_surface_Release,
290 /* IDirect3DResource8 */
291 d3d8_surface_GetDevice,
292 d3d8_surface_SetPrivateData,
293 d3d8_surface_GetPrivateData,
294 d3d8_surface_FreePrivateData,
295 /* IDirect3DSurface8 */
296 d3d8_surface_GetContainer,
297 d3d8_surface_GetDesc,
298 d3d8_surface_LockRect,
299 d3d8_surface_UnlockRect,
300 };
301
302 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
303 {
304 struct d3d8_surface *surface = parent;
305 d3d8_resource_cleanup(&surface->resource);
306 HeapFree(GetProcessHeap(), 0, surface);
307 }
308
309 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
310 {
311 surface_wined3d_object_destroyed,
312 };
313
314 void surface_init(struct d3d8_surface *surface, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
315 const struct wined3d_parent_ops **parent_ops)
316 {
317 IDirect3DBaseTexture8 *texture;
318
319 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
320 d3d8_resource_init(&surface->resource);
321 surface->resource.refcount = 0;
322 list_init(&surface->rtv_entry);
323 surface->container = wined3d_texture_get_parent(wined3d_texture);
324 surface->wined3d_texture = wined3d_texture;
325 surface->sub_resource_idx = sub_resource_idx;
326
327 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
328 &IID_IDirect3DBaseTexture8, (void **)&texture)))
329 {
330 surface->texture = unsafe_impl_from_IDirect3DBaseTexture8(texture);
331 IDirect3DBaseTexture8_Release(texture);
332 }
333
334 *parent_ops = &d3d8_surface_wined3d_parent_ops;
335 }
336
337 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
338 {
339 struct d3d8_surface *surface = parent;
340
341 /* If the surface reference count drops to zero, we release our reference
342 * to the view, but don't clear the pointer yet, in case e.g. a
343 * GetRenderTarget() call brings the surface back before the view is
344 * actually destroyed. When the view is destroyed, we need to clear the
345 * pointer, or a subsequent surface AddRef() would reference it again.
346 *
347 * This is safe because as long as the view still has a reference to the
348 * texture, the surface is also still alive, and we're called before the
349 * view releases that reference. */
350 surface->wined3d_rtv = NULL;
351 list_remove(&surface->rtv_entry);
352 }
353
354 static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops =
355 {
356 view_wined3d_object_destroyed,
357 };
358
359 struct d3d8_device *d3d8_surface_get_device(const struct d3d8_surface *surface)
360 {
361 IDirect3DDevice8 *device;
362 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
363 return impl_from_IDirect3DDevice8(device);
364 }
365
366 struct wined3d_rendertarget_view *d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface)
367 {
368 HRESULT hr;
369
370 /* The surface reference count can be equal to 0 when this function is
371 * called. In order to properly manage the render target view reference
372 * count, we temporarily increment the surface reference count. */
373 d3d8_surface_AddRef(&surface->IDirect3DSurface8_iface);
374
375 if (surface->wined3d_rtv)
376 return surface->wined3d_rtv;
377
378 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
379 surface->sub_resource_idx, surface, &d3d8_view_wined3d_parent_ops, &surface->wined3d_rtv)))
380 {
381 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
382 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
383 return NULL;
384 }
385
386 if (surface->texture)
387 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
388
389 return surface->wined3d_rtv;
390 }
391
392 void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface,
393 struct wined3d_rendertarget_view *rtv)
394 {
395 if (rtv)
396 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
397 }
398
399 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
400 {
401 if (!iface)
402 return NULL;
403 assert(iface->lpVtbl == &d3d8_surface_vtbl);
404
405 return impl_from_IDirect3DSurface8(iface);
406 }