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 %u.\n", iface
, refcount
);
68 if (surface
->parent_device
)
69 IDirect3DDevice9Ex_AddRef(surface
->parent_device
);
71 if (surface
->wined3d_rtv
)
72 wined3d_rendertarget_view_incref(surface
->wined3d_rtv
);
73 wined3d_texture_incref(surface
->wined3d_texture
);
74 wined3d_mutex_unlock();
80 static ULONG WINAPI
d3d9_surface_Release(IDirect3DSurface9
*iface
)
82 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
85 TRACE("iface %p.\n", iface
);
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");
99 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
100 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
104 IDirect3DDevice9Ex
*parent_device
= surface
->parent_device
;
106 wined3d_mutex_lock();
107 if (surface
->wined3d_rtv
)
108 wined3d_rendertarget_view_decref(surface
->wined3d_rtv
);
109 wined3d_texture_decref(surface
->wined3d_texture
);
110 wined3d_mutex_unlock();
112 /* Release the device last, as it may cause the device to be destroyed. */
114 IDirect3DDevice9Ex_Release(parent_device
);
120 static HRESULT WINAPI
d3d9_surface_GetDevice(IDirect3DSurface9
*iface
, IDirect3DDevice9
**device
)
122 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
124 TRACE("iface %p, device %p.\n", iface
, device
);
126 if (surface
->texture
)
127 return IDirect3DBaseTexture9_GetDevice(&surface
->texture
->IDirect3DBaseTexture9_iface
, device
);
129 *device
= (IDirect3DDevice9
*)surface
->parent_device
;
130 IDirect3DDevice9_AddRef(*device
);
132 TRACE("Returning device %p.\n", *device
);
137 static HRESULT WINAPI
d3d9_surface_SetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
138 const void *data
, DWORD data_size
, DWORD flags
)
140 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
141 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
142 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
144 return d3d9_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
147 static HRESULT WINAPI
d3d9_surface_GetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
148 void *data
, DWORD
*data_size
)
150 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
151 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
152 iface
, debugstr_guid(guid
), data
, data_size
);
154 return d3d9_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
157 static HRESULT WINAPI
d3d9_surface_FreePrivateData(IDirect3DSurface9
*iface
, REFGUID guid
)
159 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
160 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
162 return d3d9_resource_free_private_data(&surface
->resource
, guid
);
165 static DWORD WINAPI
d3d9_surface_SetPriority(IDirect3DSurface9
*iface
, DWORD priority
)
167 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface
, priority
);
171 static DWORD WINAPI
d3d9_surface_GetPriority(IDirect3DSurface9
*iface
)
173 TRACE("iface %p. Ignored on surfaces.\n", iface
);
177 static void WINAPI
d3d9_surface_PreLoad(IDirect3DSurface9
*iface
)
179 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
181 TRACE("iface %p.\n", iface
);
183 wined3d_mutex_lock();
184 wined3d_resource_preload(wined3d_texture_get_resource(surface
->wined3d_texture
));
185 wined3d_mutex_unlock();
188 static D3DRESOURCETYPE WINAPI
d3d9_surface_GetType(IDirect3DSurface9
*iface
)
190 TRACE("iface %p.\n", iface
);
192 return D3DRTYPE_SURFACE
;
195 static HRESULT WINAPI
d3d9_surface_GetContainer(IDirect3DSurface9
*iface
, REFIID riid
, void **container
)
197 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
200 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
202 if (!surface
->container
)
203 return E_NOINTERFACE
;
205 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
207 TRACE("Returning %p.\n", *container
);
212 static HRESULT WINAPI
d3d9_surface_GetDesc(IDirect3DSurface9
*iface
, D3DSURFACE_DESC
*desc
)
214 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
215 struct wined3d_sub_resource_desc wined3d_desc
;
217 TRACE("iface %p, desc %p.\n", iface
, desc
);
219 wined3d_mutex_lock();
220 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &wined3d_desc
);
221 wined3d_mutex_unlock();
223 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
224 desc
->Type
= D3DRTYPE_SURFACE
;
225 desc
->Usage
= d3dusage_from_wined3dusage(wined3d_desc
.usage
, wined3d_desc
.bind_flags
);
226 desc
->Pool
= d3dpool_from_wined3daccess(wined3d_desc
.access
, wined3d_desc
.usage
);
227 desc
->MultiSampleType
= d3dmultisample_type_from_wined3d(wined3d_desc
.multisample_type
);
228 desc
->MultiSampleQuality
= wined3d_desc
.multisample_quality
;
229 desc
->Width
= wined3d_desc
.width
;
230 desc
->Height
= wined3d_desc
.height
;
235 static HRESULT WINAPI
d3d9_surface_LockRect(IDirect3DSurface9
*iface
,
236 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
238 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
239 struct wined3d_box box
;
240 struct wined3d_map_desc map_desc
;
243 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
244 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
247 wined3d_box_set(&box
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1);
249 wined3d_mutex_lock();
250 hr
= wined3d_resource_map(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
,
251 &map_desc
, rect
? &box
: NULL
, wined3dmapflags_from_d3dmapflags(flags
, 0));
252 wined3d_mutex_unlock();
256 locked_rect
->Pitch
= map_desc
.row_pitch
;
257 locked_rect
->pBits
= map_desc
.data
;
260 if (hr
== E_INVALIDARG
)
261 return D3DERR_INVALIDCALL
;
265 static HRESULT WINAPI
d3d9_surface_UnlockRect(IDirect3DSurface9
*iface
)
267 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
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
);
284 type
= D3DRTYPE_SURFACE
;
285 hr
= type
== D3DRTYPE_TEXTURE
? D3D_OK
: D3DERR_INVALIDCALL
;
290 static HRESULT WINAPI
d3d9_surface_GetDC(IDirect3DSurface9
*iface
, HDC
*dc
)
292 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
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();
304 static HRESULT WINAPI
d3d9_surface_ReleaseDC(IDirect3DSurface9
*iface
, HDC dc
)
306 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
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();
320 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl
=
323 d3d9_surface_QueryInterface
,
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
,
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
);
351 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops
=
353 surface_wined3d_object_destroyed
,
356 void surface_init(struct d3d9_surface
*surface
, struct wined3d_texture
*wined3d_texture
,
357 unsigned int sub_resource_idx
, const struct wined3d_parent_ops
**parent_ops
)
359 IDirect3DBaseTexture9
*texture
;
361 surface
->IDirect3DSurface9_iface
.lpVtbl
= &d3d9_surface_vtbl
;
362 d3d9_resource_init(&surface
->resource
);
363 surface
->resource
.refcount
= 0;
364 list_init(&surface
->rtv_entry
);
365 surface
->container
= wined3d_texture_get_parent(wined3d_texture
);
366 surface
->wined3d_texture
= wined3d_texture
;
367 surface
->sub_resource_idx
= sub_resource_idx
;
369 if (surface
->container
&& SUCCEEDED(IUnknown_QueryInterface(surface
->container
,
370 &IID_IDirect3DBaseTexture9
, (void **)&texture
)))
372 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture9(texture
);
373 IDirect3DBaseTexture9_Release(texture
);
376 *parent_ops
= &d3d9_surface_wined3d_parent_ops
;
379 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
381 struct d3d9_surface
*surface
= parent
;
383 /* If the surface reference count drops to zero, we release our reference
384 * to the view, but don't clear the pointer yet, in case e.g. a
385 * GetRenderTarget() call brings the surface back before the view is
386 * actually destroyed. When the view is destroyed, we need to clear the
387 * pointer, or a subsequent surface AddRef() would reference it again.
389 * This is safe because as long as the view still has a reference to the
390 * texture, the surface is also still alive, and we're called before the
391 * view releases that reference. */
392 surface
->wined3d_rtv
= NULL
;
393 list_remove(&surface
->rtv_entry
);
396 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops
=
398 view_wined3d_object_destroyed
,
401 struct d3d9_device
*d3d9_surface_get_device(const struct d3d9_surface
*surface
)
403 IDirect3DDevice9Ex
*device
;
404 device
= surface
->texture
? surface
->texture
->parent_device
: surface
->parent_device
;
405 return impl_from_IDirect3DDevice9Ex(device
);
408 struct wined3d_rendertarget_view
*d3d9_surface_acquire_rendertarget_view(struct d3d9_surface
*surface
)
412 /* The surface reference count can be equal to 0 when this function is
413 * called. In order to properly manage the render target view reference
414 * count, we temporarily increment the surface reference count. */
415 d3d9_surface_AddRef(&surface
->IDirect3DSurface9_iface
);
417 if (surface
->wined3d_rtv
)
418 return surface
->wined3d_rtv
;
420 if (FAILED(hr
= wined3d_rendertarget_view_create_from_sub_resource(surface
->wined3d_texture
,
421 surface
->sub_resource_idx
, surface
, &d3d9_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
423 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
424 d3d9_surface_Release(&surface
->IDirect3DSurface9_iface
);
428 if (surface
->texture
)
429 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
431 return surface
->wined3d_rtv
;
434 void d3d9_surface_release_rendertarget_view(struct d3d9_surface
*surface
,
435 struct wined3d_rendertarget_view
*rtv
)
438 d3d9_surface_Release(&surface
->IDirect3DSurface9_iface
);
441 struct d3d9_surface
*unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
445 assert(iface
->lpVtbl
== &d3d9_surface_vtbl
);
447 return impl_from_IDirect3DSurface9(iface
);