[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / d3d9 / surface.c
1 /*
2 * IDirect3DSurface9 implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "d3d9_private.h"
23
24 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
25 {
26 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
27 }
28
29 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
30 {
31 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
32
33 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
34 || IsEqualGUID(riid, &IID_IDirect3DResource9)
35 || IsEqualGUID(riid, &IID_IUnknown))
36 {
37 IDirect3DSurface9_AddRef(iface);
38 *out = iface;
39 return S_OK;
40 }
41
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43
44 *out = NULL;
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
49 {
50 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
51 ULONG refcount;
52
53 TRACE("iface %p.\n", iface);
54
55 if (surface->texture)
56 {
57 TRACE("Forwarding to %p.\n", surface->texture);
58 return IDirect3DBaseTexture9_AddRef(&surface->texture->IDirect3DBaseTexture9_iface);
59 }
60
61 refcount = InterlockedIncrement(&surface->resource.refcount);
62 TRACE("%p increasing refcount to %u.\n", iface, refcount);
63
64 if (refcount == 1)
65 {
66 if (surface->parent_device)
67 IDirect3DDevice9Ex_AddRef(surface->parent_device);
68 wined3d_mutex_lock();
69 if (surface->wined3d_rtv)
70 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
71 wined3d_texture_incref(surface->wined3d_texture);
72 wined3d_mutex_unlock();
73 }
74
75 return refcount;
76 }
77
78 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
79 {
80 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
81 ULONG refcount;
82
83 TRACE("iface %p.\n", iface);
84
85 if (surface->texture)
86 {
87 TRACE("Forwarding to %p.\n", surface->texture);
88 return IDirect3DBaseTexture9_Release(&surface->texture->IDirect3DBaseTexture9_iface);
89 }
90
91 if (!surface->resource.refcount)
92 {
93 WARN("Surface does not have any references.\n");
94 return 0;
95 }
96
97 refcount = InterlockedDecrement(&surface->resource.refcount);
98 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
99
100 if (!refcount)
101 {
102 IDirect3DDevice9Ex *parent_device = surface->parent_device;
103
104 wined3d_mutex_lock();
105 if (surface->wined3d_rtv)
106 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
107 wined3d_texture_decref(surface->wined3d_texture);
108 wined3d_mutex_unlock();
109
110 /* Release the device last, as it may cause the device to be destroyed. */
111 if (parent_device)
112 IDirect3DDevice9Ex_Release(parent_device);
113 }
114
115 return refcount;
116 }
117
118 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
119 {
120 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
121
122 TRACE("iface %p, device %p.\n", iface, device);
123
124 if (surface->texture)
125 return IDirect3DBaseTexture9_GetDevice(&surface->texture->IDirect3DBaseTexture9_iface, device);
126
127 *device = (IDirect3DDevice9 *)surface->parent_device;
128 IDirect3DDevice9_AddRef(*device);
129
130 TRACE("Returning device %p.\n", *device);
131
132 return D3D_OK;
133 }
134
135 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
136 const void *data, DWORD data_size, DWORD flags)
137 {
138 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
139 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
140 iface, debugstr_guid(guid), data, data_size, flags);
141
142 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
143 }
144
145 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
146 void *data, DWORD *data_size)
147 {
148 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
149 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
150 iface, debugstr_guid(guid), data, data_size);
151
152 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
153 }
154
155 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
156 {
157 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
158 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
159
160 return d3d9_resource_free_private_data(&surface->resource, guid);
161 }
162
163 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
164 {
165 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface, priority);
166 return 0;
167 }
168
169 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
170 {
171 TRACE("iface %p. Ignored on surfaces.\n", iface);
172 return 0;
173 }
174
175 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
176 {
177 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
178
179 TRACE("iface %p.\n", iface);
180
181 wined3d_mutex_lock();
182 wined3d_resource_preload(wined3d_texture_get_resource(surface->wined3d_texture));
183 wined3d_mutex_unlock();
184 }
185
186 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
187 {
188 TRACE("iface %p.\n", iface);
189
190 return D3DRTYPE_SURFACE;
191 }
192
193 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
194 {
195 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
196 HRESULT hr;
197
198 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
199
200 if (!surface->container)
201 return E_NOINTERFACE;
202
203 hr = IUnknown_QueryInterface(surface->container, riid, container);
204
205 TRACE("Returning %p.\n", *container);
206
207 return hr;
208 }
209
210 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
211 {
212 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
213 struct wined3d_sub_resource_desc wined3d_desc;
214
215 TRACE("iface %p, desc %p.\n", iface, desc);
216
217 wined3d_mutex_lock();
218 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
219 wined3d_mutex_unlock();
220
221 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
222 desc->Type = D3DRTYPE_SURFACE;
223 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
224 desc->Pool = wined3d_desc.pool;
225 desc->MultiSampleType = wined3d_desc.multisample_type;
226 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
227 desc->Width = wined3d_desc.width;
228 desc->Height = wined3d_desc.height;
229
230 return D3D_OK;
231 }
232
233 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
234 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
235 {
236 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
237 struct wined3d_box box;
238 struct wined3d_map_desc map_desc;
239 HRESULT hr;
240
241 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
242 iface, locked_rect, wine_dbgstr_rect(rect), flags);
243
244 if (rect)
245 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
246
247 wined3d_mutex_lock();
248 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
249 &map_desc, rect ? &box : NULL, flags);
250 wined3d_mutex_unlock();
251
252 if (SUCCEEDED(hr))
253 {
254 locked_rect->Pitch = map_desc.row_pitch;
255 locked_rect->pBits = map_desc.data;
256 }
257
258 return hr;
259 }
260
261 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
262 {
263 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(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 = IDirect3DBaseTexture9_GetType(&surface->texture->IDirect3DBaseTexture9_iface);
277 else
278 type = D3DRTYPE_SURFACE;
279 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
280 }
281 return hr;
282 }
283
284 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
285 {
286 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
287 HRESULT hr;
288
289 TRACE("iface %p, dc %p.\n", iface, dc);
290
291 wined3d_mutex_lock();
292 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
293 wined3d_mutex_unlock();
294
295 return hr;
296 }
297
298 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
299 {
300 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
301 HRESULT hr;
302
303 TRACE("iface %p, dc %p.\n", iface, dc);
304
305 wined3d_mutex_lock();
306 hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
307 wined3d_mutex_unlock();
308
309 return hr;
310 }
311
312 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
313 {
314 /* IUnknown */
315 d3d9_surface_QueryInterface,
316 d3d9_surface_AddRef,
317 d3d9_surface_Release,
318 /* IDirect3DResource9 */
319 d3d9_surface_GetDevice,
320 d3d9_surface_SetPrivateData,
321 d3d9_surface_GetPrivateData,
322 d3d9_surface_FreePrivateData,
323 d3d9_surface_SetPriority,
324 d3d9_surface_GetPriority,
325 d3d9_surface_PreLoad,
326 d3d9_surface_GetType,
327 /* IDirect3DSurface9 */
328 d3d9_surface_GetContainer,
329 d3d9_surface_GetDesc,
330 d3d9_surface_LockRect,
331 d3d9_surface_UnlockRect,
332 d3d9_surface_GetDC,
333 d3d9_surface_ReleaseDC,
334 };
335
336 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
337 {
338 struct d3d9_surface *surface = parent;
339 d3d9_resource_cleanup(&surface->resource);
340 HeapFree(GetProcessHeap(), 0, surface);
341 }
342
343 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
344 {
345 surface_wined3d_object_destroyed,
346 };
347
348 void surface_init(struct d3d9_surface *surface, struct wined3d_texture *wined3d_texture,
349 unsigned int sub_resource_idx, const struct wined3d_parent_ops **parent_ops)
350 {
351 IDirect3DBaseTexture9 *texture;
352
353 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
354 d3d9_resource_init(&surface->resource);
355 surface->resource.refcount = 0;
356 list_init(&surface->rtv_entry);
357 surface->container = wined3d_texture_get_parent(wined3d_texture);
358 surface->wined3d_texture = wined3d_texture;
359 surface->sub_resource_idx = sub_resource_idx;
360
361 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
362 &IID_IDirect3DBaseTexture9, (void **)&texture)))
363 {
364 surface->texture = unsafe_impl_from_IDirect3DBaseTexture9(texture);
365 IDirect3DBaseTexture9_Release(texture);
366 }
367
368 *parent_ops = &d3d9_surface_wined3d_parent_ops;
369 }
370
371 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
372 {
373 struct d3d9_surface *surface = parent;
374
375 /* If the surface reference count drops to zero, we release our reference
376 * to the view, but don't clear the pointer yet, in case e.g. a
377 * GetRenderTarget() call brings the surface back before the view is
378 * actually destroyed. When the view is destroyed, we need to clear the
379 * pointer, or a subsequent surface AddRef() would reference it again.
380 *
381 * This is safe because as long as the view still has a reference to the
382 * texture, the surface is also still alive, and we're called before the
383 * view releases that reference. */
384 surface->wined3d_rtv = NULL;
385 list_remove(&surface->rtv_entry);
386 }
387
388 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops =
389 {
390 view_wined3d_object_destroyed,
391 };
392
393 struct d3d9_device *d3d9_surface_get_device(const struct d3d9_surface *surface)
394 {
395 IDirect3DDevice9Ex *device;
396 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
397 return impl_from_IDirect3DDevice9Ex(device);
398 }
399
400 struct wined3d_rendertarget_view *d3d9_surface_acquire_rendertarget_view(struct d3d9_surface *surface)
401 {
402 HRESULT hr;
403
404 /* The surface reference count can be equal to 0 when this function is
405 * called. In order to properly manage the render target view reference
406 * count, we temporarily increment the surface reference count. */
407 d3d9_surface_AddRef(&surface->IDirect3DSurface9_iface);
408
409 if (surface->wined3d_rtv)
410 return surface->wined3d_rtv;
411
412 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
413 surface->sub_resource_idx, surface, &d3d9_view_wined3d_parent_ops, &surface->wined3d_rtv)))
414 {
415 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
416 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
417 return NULL;
418 }
419
420 if (surface->texture)
421 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
422
423 return surface->wined3d_rtv;
424 }
425
426 void d3d9_surface_release_rendertarget_view(struct d3d9_surface *surface,
427 struct wined3d_rendertarget_view *rtv)
428 {
429 if (rtv)
430 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
431 }
432
433 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
434 {
435 if (!iface)
436 return NULL;
437 assert(iface->lpVtbl == &d3d9_surface_vtbl);
438
439 return impl_from_IDirect3DSurface9(iface);
440 }