winemenubuilder: Use CRT allocation functions.
[wine.git] / dlls / d3d9 / surface.c
blobf0cc36da3a19655b2a0a46bb5eea44a95120a7b2
1 /*
2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
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.
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.
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
22 #include "d3d9_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
28 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
31 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
33 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
35 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
36 || IsEqualGUID(riid, &IID_IDirect3DResource9)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DSurface9_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
52 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
53 ULONG refcount;
55 TRACE("iface %p.\n", iface);
57 if (surface->texture)
59 TRACE("Forwarding to %p.\n", surface->texture);
60 return IDirect3DBaseTexture9_AddRef(&surface->texture->IDirect3DBaseTexture9_iface);
63 refcount = InterlockedIncrement(&surface->resource.refcount);
64 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
66 if (refcount == 1)
68 if (surface->parent_device)
69 IDirect3DDevice9Ex_AddRef(surface->parent_device);
70 if (surface->wined3d_rtv)
71 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
72 wined3d_texture_incref(surface->wined3d_texture);
73 if (surface->swapchain)
74 wined3d_swapchain_incref(surface->swapchain);
77 return refcount;
80 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
82 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
83 ULONG refcount;
85 TRACE("iface %p.\n", iface);
87 if (surface->texture)
89 TRACE("Forwarding to %p.\n", surface->texture);
90 return IDirect3DBaseTexture9_Release(&surface->texture->IDirect3DBaseTexture9_iface);
93 if (!surface->resource.refcount)
95 WARN("Surface does not have any references.\n");
96 return 0;
99 refcount = InterlockedDecrement(&surface->resource.refcount);
100 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
102 if (!refcount)
104 IDirect3DDevice9Ex *parent_device = surface->parent_device;
106 if (surface->wined3d_rtv)
107 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
108 if (surface->swapchain)
109 wined3d_swapchain_decref(surface->swapchain);
110 /* Releasing the texture may free the d3d9 object, so do not access it
111 * after releasing the texture. */
112 wined3d_texture_decref(surface->wined3d_texture);
114 /* Release the device last, as it may cause the device to be destroyed. */
115 if (parent_device)
116 IDirect3DDevice9Ex_Release(parent_device);
119 return refcount;
122 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
124 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
126 TRACE("iface %p, device %p.\n", iface, device);
128 if (surface->texture)
129 return IDirect3DBaseTexture9_GetDevice(&surface->texture->IDirect3DBaseTexture9_iface, device);
131 *device = (IDirect3DDevice9 *)surface->parent_device;
132 IDirect3DDevice9_AddRef(*device);
134 TRACE("Returning device %p.\n", *device);
136 return D3D_OK;
139 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
140 const void *data, DWORD data_size, DWORD flags)
142 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
143 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
144 iface, debugstr_guid(guid), data, data_size, flags);
146 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
149 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
150 void *data, DWORD *data_size)
152 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
153 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
154 iface, debugstr_guid(guid), data, data_size);
156 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
159 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
161 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
162 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
164 return d3d9_resource_free_private_data(&surface->resource, guid);
167 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
169 TRACE("iface %p, priority %lu. Ignored on surfaces.\n", iface, priority);
170 return 0;
173 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
175 TRACE("iface %p. Ignored on surfaces.\n", iface);
176 return 0;
179 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
181 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
183 TRACE("iface %p.\n", iface);
185 wined3d_mutex_lock();
186 wined3d_resource_preload(wined3d_texture_get_resource(surface->wined3d_texture));
187 wined3d_mutex_unlock();
190 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
192 TRACE("iface %p.\n", iface);
194 return D3DRTYPE_SURFACE;
197 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
199 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
200 HRESULT hr;
202 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
204 if (!surface->container)
205 return E_NOINTERFACE;
207 hr = IUnknown_QueryInterface(surface->container, riid, container);
209 TRACE("Returning %p.\n", *container);
211 return hr;
214 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
216 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
217 struct wined3d_sub_resource_desc wined3d_desc;
219 TRACE("iface %p, desc %p.\n", iface, desc);
221 wined3d_mutex_lock();
222 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
223 wined3d_mutex_unlock();
225 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
226 desc->Type = D3DRTYPE_SURFACE;
227 desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
228 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
229 desc->MultiSampleType = d3dmultisample_type_from_wined3d(wined3d_desc.multisample_type);
230 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
231 desc->Width = wined3d_desc.width;
232 desc->Height = wined3d_desc.height;
234 return D3D_OK;
237 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
238 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
240 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
241 struct wined3d_box box;
242 struct wined3d_map_desc map_desc;
243 HRESULT hr;
245 TRACE("iface %p, locked_rect %p, rect %s, flags %#lx.\n",
246 iface, locked_rect, wine_dbgstr_rect(rect), flags);
248 if (rect)
249 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
251 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
252 &map_desc, rect ? &box : NULL, wined3dmapflags_from_d3dmapflags(flags, 0));
254 if (SUCCEEDED(hr))
256 locked_rect->Pitch = map_desc.row_pitch;
257 locked_rect->pBits = map_desc.data;
260 if (hr == E_INVALIDARG)
261 return D3DERR_INVALIDCALL;
262 return hr;
265 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
267 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
268 HRESULT hr;
270 TRACE("iface %p.\n", iface);
272 wined3d_mutex_lock();
273 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
274 if (SUCCEEDED(hr) && surface->texture)
275 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
276 wined3d_mutex_unlock();
278 if (hr == WINEDDERR_NOTLOCKED)
280 D3DRESOURCETYPE type;
281 if (surface->texture)
282 type = IDirect3DBaseTexture9_GetType(&surface->texture->IDirect3DBaseTexture9_iface);
283 else
284 type = D3DRTYPE_SURFACE;
285 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
287 return hr;
290 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
292 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
293 HRESULT hr;
295 TRACE("iface %p, dc %p.\n", iface, dc);
297 wined3d_mutex_lock();
298 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
299 wined3d_mutex_unlock();
301 return hr;
304 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
306 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
307 HRESULT hr;
309 TRACE("iface %p, dc %p.\n", iface, dc);
311 wined3d_mutex_lock();
312 hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
313 if (SUCCEEDED(hr) && surface->texture)
314 d3d9_texture_flag_auto_gen_mipmap(surface->texture);
315 wined3d_mutex_unlock();
317 return hr;
320 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
322 /* IUnknown */
323 d3d9_surface_QueryInterface,
324 d3d9_surface_AddRef,
325 d3d9_surface_Release,
326 /* IDirect3DResource9 */
327 d3d9_surface_GetDevice,
328 d3d9_surface_SetPrivateData,
329 d3d9_surface_GetPrivateData,
330 d3d9_surface_FreePrivateData,
331 d3d9_surface_SetPriority,
332 d3d9_surface_GetPriority,
333 d3d9_surface_PreLoad,
334 d3d9_surface_GetType,
335 /* IDirect3DSurface9 */
336 d3d9_surface_GetContainer,
337 d3d9_surface_GetDesc,
338 d3d9_surface_LockRect,
339 d3d9_surface_UnlockRect,
340 d3d9_surface_GetDC,
341 d3d9_surface_ReleaseDC,
344 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
346 struct d3d9_surface *surface = parent;
347 d3d9_resource_cleanup(&surface->resource);
348 heap_free(surface);
351 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
353 surface_wined3d_object_destroyed,
356 struct d3d9_surface *d3d9_surface_create(struct wined3d_texture *wined3d_texture,
357 unsigned int sub_resource_idx, IUnknown *container)
359 IDirect3DBaseTexture9 *texture;
360 struct d3d9_surface *surface;
362 if (!(surface = heap_alloc_zero(sizeof(*surface))))
363 return NULL;
365 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
366 d3d9_resource_init(&surface->resource);
367 surface->resource.refcount = 0;
368 list_init(&surface->rtv_entry);
369 surface->container = container;
370 surface->wined3d_texture = wined3d_texture;
371 surface->sub_resource_idx = sub_resource_idx;
372 surface->swapchain = wined3d_texture_get_swapchain(wined3d_texture);
374 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
375 &IID_IDirect3DBaseTexture9, (void **)&texture)))
377 surface->texture = unsafe_impl_from_IDirect3DBaseTexture9(texture);
378 IDirect3DBaseTexture9_Release(texture);
381 wined3d_texture_set_sub_resource_parent(wined3d_texture, sub_resource_idx,
382 surface, &d3d9_surface_wined3d_parent_ops);
384 TRACE("Created surface %p.\n", surface);
385 return surface;
388 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
390 struct d3d9_surface *surface = parent;
392 /* If the surface reference count drops to zero, we release our reference
393 * to the view, but don't clear the pointer yet, in case e.g. a
394 * GetRenderTarget() call brings the surface back before the view is
395 * actually destroyed. When the view is destroyed, we need to clear the
396 * pointer, or a subsequent surface AddRef() would reference it again.
398 * This is safe because as long as the view still has a reference to the
399 * texture, the surface is also still alive, and we're called before the
400 * view releases that reference. */
401 surface->wined3d_rtv = NULL;
402 list_remove(&surface->rtv_entry);
405 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops =
407 view_wined3d_object_destroyed,
410 struct d3d9_device *d3d9_surface_get_device(const struct d3d9_surface *surface)
412 IDirect3DDevice9Ex *device;
413 device = surface->texture ? &surface->texture->parent_device->IDirect3DDevice9Ex_iface : surface->parent_device;
414 return impl_from_IDirect3DDevice9Ex(device);
417 struct wined3d_rendertarget_view *d3d9_surface_acquire_rendertarget_view(struct d3d9_surface *surface)
419 HRESULT hr;
421 /* The surface reference count can be equal to 0 when this function is
422 * called. In order to properly manage the render target view reference
423 * count, we temporarily increment the surface reference count. */
424 d3d9_surface_AddRef(&surface->IDirect3DSurface9_iface);
426 if (surface->wined3d_rtv)
427 return surface->wined3d_rtv;
429 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
430 surface->sub_resource_idx, surface, &d3d9_view_wined3d_parent_ops, &surface->wined3d_rtv)))
432 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
433 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
434 return NULL;
437 if (surface->texture)
438 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
440 return surface->wined3d_rtv;
443 void d3d9_surface_release_rendertarget_view(struct d3d9_surface *surface,
444 struct wined3d_rendertarget_view *rtv)
446 if (rtv)
447 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
450 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
452 if (!iface)
453 return NULL;
454 assert(iface->lpVtbl == &d3d9_surface_vtbl);
456 return impl_from_IDirect3DSurface9(iface);