2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
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
);
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
50 static ULONG WINAPI
d3d9_surface_AddRef(IDirect3DSurface9
*iface
)
52 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
55 TRACE("iface %p.\n", iface
);
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
);
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
);
78 static ULONG WINAPI
d3d9_surface_Release(IDirect3DSurface9
*iface
)
80 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
83 TRACE("iface %p.\n", iface
);
87 TRACE("Forwarding to %p.\n", surface
->texture
);
88 return IDirect3DBaseTexture9_Release(&surface
->texture
->IDirect3DBaseTexture9_iface
);
91 if (!surface
->resource
.refcount
)
93 WARN("Surface does not have any references.\n");
97 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
98 TRACE("%p decreasing refcount to %lu.\n", iface
, refcount
);
102 IDirect3DDevice9Ex
*parent_device
= surface
->parent_device
;
104 if (surface
->wined3d_rtv
)
105 wined3d_rendertarget_view_decref(surface
->wined3d_rtv
);
106 wined3d_texture_decref(surface
->wined3d_texture
);
108 /* Release the device last, as it may cause the device to be destroyed. */
110 IDirect3DDevice9Ex_Release(parent_device
);
116 static HRESULT WINAPI
d3d9_surface_GetDevice(IDirect3DSurface9
*iface
, IDirect3DDevice9
**device
)
118 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
120 TRACE("iface %p, device %p.\n", iface
, device
);
122 if (surface
->texture
)
123 return IDirect3DBaseTexture9_GetDevice(&surface
->texture
->IDirect3DBaseTexture9_iface
, device
);
125 *device
= (IDirect3DDevice9
*)surface
->parent_device
;
126 IDirect3DDevice9_AddRef(*device
);
128 TRACE("Returning device %p.\n", *device
);
133 static HRESULT WINAPI
d3d9_surface_SetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
134 const void *data
, DWORD data_size
, DWORD flags
)
136 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
137 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
138 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
140 return d3d9_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
143 static HRESULT WINAPI
d3d9_surface_GetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
144 void *data
, DWORD
*data_size
)
146 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
147 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
148 iface
, debugstr_guid(guid
), data
, data_size
);
150 return d3d9_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
153 static HRESULT WINAPI
d3d9_surface_FreePrivateData(IDirect3DSurface9
*iface
, REFGUID guid
)
155 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
156 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
158 return d3d9_resource_free_private_data(&surface
->resource
, guid
);
161 static DWORD WINAPI
d3d9_surface_SetPriority(IDirect3DSurface9
*iface
, DWORD priority
)
163 TRACE("iface %p, priority %lu. Ignored on surfaces.\n", iface
, priority
);
167 static DWORD WINAPI
d3d9_surface_GetPriority(IDirect3DSurface9
*iface
)
169 TRACE("iface %p. Ignored on surfaces.\n", iface
);
173 static void WINAPI
d3d9_surface_PreLoad(IDirect3DSurface9
*iface
)
175 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
177 TRACE("iface %p.\n", iface
);
179 wined3d_mutex_lock();
180 wined3d_resource_preload(wined3d_texture_get_resource(surface
->wined3d_texture
));
181 wined3d_mutex_unlock();
184 static D3DRESOURCETYPE WINAPI
d3d9_surface_GetType(IDirect3DSurface9
*iface
)
186 TRACE("iface %p.\n", iface
);
188 return D3DRTYPE_SURFACE
;
191 static HRESULT WINAPI
d3d9_surface_GetContainer(IDirect3DSurface9
*iface
, REFIID riid
, void **container
)
193 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
196 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
198 if (!surface
->container
)
199 return E_NOINTERFACE
;
201 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
203 TRACE("Returning %p.\n", *container
);
208 static HRESULT WINAPI
d3d9_surface_GetDesc(IDirect3DSurface9
*iface
, D3DSURFACE_DESC
*desc
)
210 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
211 struct wined3d_sub_resource_desc wined3d_desc
;
213 TRACE("iface %p, desc %p.\n", iface
, desc
);
215 wined3d_mutex_lock();
216 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &wined3d_desc
);
217 wined3d_mutex_unlock();
219 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
220 desc
->Type
= D3DRTYPE_SURFACE
;
221 desc
->Usage
= d3dusage_from_wined3dusage(wined3d_desc
.usage
, wined3d_desc
.bind_flags
);
222 desc
->Pool
= d3dpool_from_wined3daccess(wined3d_desc
.access
, wined3d_desc
.usage
);
223 desc
->MultiSampleType
= d3dmultisample_type_from_wined3d(wined3d_desc
.multisample_type
);
224 desc
->MultiSampleQuality
= wined3d_desc
.multisample_quality
;
225 desc
->Width
= wined3d_desc
.width
;
226 desc
->Height
= wined3d_desc
.height
;
231 static HRESULT WINAPI
d3d9_surface_LockRect(IDirect3DSurface9
*iface
,
232 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
234 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
235 struct wined3d_box box
;
236 struct wined3d_map_desc map_desc
;
239 TRACE("iface %p, locked_rect %p, rect %s, flags %#lx.\n",
240 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
243 wined3d_box_set(&box
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1);
245 hr
= wined3d_resource_map(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
,
246 &map_desc
, rect
? &box
: NULL
, wined3dmapflags_from_d3dmapflags(flags
, 0));
250 locked_rect
->Pitch
= map_desc
.row_pitch
;
251 locked_rect
->pBits
= map_desc
.data
;
254 if (hr
== E_INVALIDARG
)
255 return D3DERR_INVALIDCALL
;
259 static HRESULT WINAPI
d3d9_surface_UnlockRect(IDirect3DSurface9
*iface
)
261 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
264 TRACE("iface %p.\n", iface
);
266 wined3d_mutex_lock();
267 hr
= wined3d_resource_unmap(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
);
268 if (SUCCEEDED(hr
) && surface
->texture
)
269 d3d9_texture_flag_auto_gen_mipmap(surface
->texture
);
270 wined3d_mutex_unlock();
272 if (hr
== WINEDDERR_NOTLOCKED
)
274 D3DRESOURCETYPE type
;
275 if (surface
->texture
)
276 type
= IDirect3DBaseTexture9_GetType(&surface
->texture
->IDirect3DBaseTexture9_iface
);
278 type
= D3DRTYPE_SURFACE
;
279 hr
= type
== D3DRTYPE_TEXTURE
? D3D_OK
: D3DERR_INVALIDCALL
;
284 static HRESULT WINAPI
d3d9_surface_GetDC(IDirect3DSurface9
*iface
, HDC
*dc
)
286 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
289 TRACE("iface %p, dc %p.\n", iface
, dc
);
291 wined3d_mutex_lock();
292 hr
= wined3d_texture_get_dc(surface
->wined3d_texture
, surface
->sub_resource_idx
, dc
);
293 wined3d_mutex_unlock();
298 static HRESULT WINAPI
d3d9_surface_ReleaseDC(IDirect3DSurface9
*iface
, HDC dc
)
300 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
303 TRACE("iface %p, dc %p.\n", iface
, dc
);
305 wined3d_mutex_lock();
306 hr
= wined3d_texture_release_dc(surface
->wined3d_texture
, surface
->sub_resource_idx
, dc
);
307 if (SUCCEEDED(hr
) && surface
->texture
)
308 d3d9_texture_flag_auto_gen_mipmap(surface
->texture
);
309 wined3d_mutex_unlock();
314 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl
=
317 d3d9_surface_QueryInterface
,
319 d3d9_surface_Release
,
320 /* IDirect3DResource9 */
321 d3d9_surface_GetDevice
,
322 d3d9_surface_SetPrivateData
,
323 d3d9_surface_GetPrivateData
,
324 d3d9_surface_FreePrivateData
,
325 d3d9_surface_SetPriority
,
326 d3d9_surface_GetPriority
,
327 d3d9_surface_PreLoad
,
328 d3d9_surface_GetType
,
329 /* IDirect3DSurface9 */
330 d3d9_surface_GetContainer
,
331 d3d9_surface_GetDesc
,
332 d3d9_surface_LockRect
,
333 d3d9_surface_UnlockRect
,
335 d3d9_surface_ReleaseDC
,
338 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
340 struct d3d9_surface
*surface
= parent
;
341 d3d9_resource_cleanup(&surface
->resource
);
345 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops
=
347 surface_wined3d_object_destroyed
,
350 void surface_init(struct d3d9_surface
*surface
, struct wined3d_texture
*wined3d_texture
,
351 unsigned int sub_resource_idx
, const struct wined3d_parent_ops
**parent_ops
)
353 IDirect3DBaseTexture9
*texture
;
355 surface
->IDirect3DSurface9_iface
.lpVtbl
= &d3d9_surface_vtbl
;
356 d3d9_resource_init(&surface
->resource
);
357 surface
->resource
.refcount
= 0;
358 list_init(&surface
->rtv_entry
);
359 surface
->container
= wined3d_texture_get_parent(wined3d_texture
);
360 surface
->wined3d_texture
= wined3d_texture
;
361 surface
->sub_resource_idx
= sub_resource_idx
;
363 if (surface
->container
&& SUCCEEDED(IUnknown_QueryInterface(surface
->container
,
364 &IID_IDirect3DBaseTexture9
, (void **)&texture
)))
366 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture9(texture
);
367 IDirect3DBaseTexture9_Release(texture
);
370 *parent_ops
= &d3d9_surface_wined3d_parent_ops
;
373 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
375 struct d3d9_surface
*surface
= parent
;
377 /* If the surface reference count drops to zero, we release our reference
378 * to the view, but don't clear the pointer yet, in case e.g. a
379 * GetRenderTarget() call brings the surface back before the view is
380 * actually destroyed. When the view is destroyed, we need to clear the
381 * pointer, or a subsequent surface AddRef() would reference it again.
383 * This is safe because as long as the view still has a reference to the
384 * texture, the surface is also still alive, and we're called before the
385 * view releases that reference. */
386 surface
->wined3d_rtv
= NULL
;
387 list_remove(&surface
->rtv_entry
);
390 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops
=
392 view_wined3d_object_destroyed
,
395 struct d3d9_device
*d3d9_surface_get_device(const struct d3d9_surface
*surface
)
397 IDirect3DDevice9Ex
*device
;
398 device
= surface
->texture
? &surface
->texture
->parent_device
->IDirect3DDevice9Ex_iface
: surface
->parent_device
;
399 return impl_from_IDirect3DDevice9Ex(device
);
402 struct wined3d_rendertarget_view
*d3d9_surface_acquire_rendertarget_view(struct d3d9_surface
*surface
)
406 /* The surface reference count can be equal to 0 when this function is
407 * called. In order to properly manage the render target view reference
408 * count, we temporarily increment the surface reference count. */
409 d3d9_surface_AddRef(&surface
->IDirect3DSurface9_iface
);
411 if (surface
->wined3d_rtv
)
412 return surface
->wined3d_rtv
;
414 if (FAILED(hr
= wined3d_rendertarget_view_create_from_sub_resource(surface
->wined3d_texture
,
415 surface
->sub_resource_idx
, surface
, &d3d9_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
417 ERR("Failed to create rendertarget view, hr %#lx.\n", hr
);
418 d3d9_surface_Release(&surface
->IDirect3DSurface9_iface
);
422 if (surface
->texture
)
423 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
425 return surface
->wined3d_rtv
;
428 void d3d9_surface_release_rendertarget_view(struct d3d9_surface
*surface
,
429 struct wined3d_rendertarget_view
*rtv
)
432 d3d9_surface_Release(&surface
->IDirect3DSurface9_iface
);
435 struct d3d9_surface
*unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
439 assert(iface
->lpVtbl
== &d3d9_surface_vtbl
);
441 return impl_from_IDirect3DSurface9(iface
);