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 static inline struct d3d9_surface
*impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
26 return CONTAINING_RECORD(iface
, struct d3d9_surface
, IDirect3DSurface9_iface
);
29 static HRESULT WINAPI
d3d9_surface_QueryInterface(IDirect3DSurface9
*iface
, REFIID riid
, void **out
)
31 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
33 if (IsEqualGUID(riid
, &IID_IDirect3DSurface9
)
34 || IsEqualGUID(riid
, &IID_IDirect3DResource9
)
35 || IsEqualGUID(riid
, &IID_IUnknown
))
37 IDirect3DSurface9_AddRef(iface
);
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
48 static ULONG WINAPI
d3d9_surface_AddRef(IDirect3DSurface9
*iface
)
50 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
53 TRACE("iface %p.\n", iface
);
57 TRACE("Forwarding to %p.\n", surface
->texture
);
58 return IDirect3DBaseTexture9_AddRef(&surface
->texture
->IDirect3DBaseTexture9_iface
);
61 refcount
= InterlockedIncrement(&surface
->resource
.refcount
);
62 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
66 if (surface
->parent_device
)
67 IDirect3DDevice9Ex_AddRef(surface
->parent_device
);
69 if (surface
->wined3d_rtv
)
70 wined3d_rendertarget_view_incref(surface
->wined3d_rtv
);
71 wined3d_texture_incref(surface
->wined3d_texture
);
72 wined3d_mutex_unlock();
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 ERR("Surface doesn't have any references.\n");
97 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
98 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
102 IDirect3DDevice9Ex
*parent_device
= surface
->parent_device
;
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();
110 /* Release the device last, as it may cause the device to be destroyed. */
112 IDirect3DDevice9Ex_Release(parent_device
);
118 static HRESULT WINAPI
d3d9_surface_GetDevice(IDirect3DSurface9
*iface
, IDirect3DDevice9
**device
)
120 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
122 TRACE("iface %p, device %p.\n", iface
, device
);
124 if (surface
->texture
)
125 return IDirect3DBaseTexture9_GetDevice(&surface
->texture
->IDirect3DBaseTexture9_iface
, device
);
127 *device
= (IDirect3DDevice9
*)surface
->parent_device
;
128 IDirect3DDevice9_AddRef(*device
);
130 TRACE("Returning device %p.\n", *device
);
135 static HRESULT WINAPI
d3d9_surface_SetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
136 const void *data
, DWORD data_size
, DWORD flags
)
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
);
142 return d3d9_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
145 static HRESULT WINAPI
d3d9_surface_GetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
146 void *data
, DWORD
*data_size
)
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
);
152 return d3d9_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
155 static HRESULT WINAPI
d3d9_surface_FreePrivateData(IDirect3DSurface9
*iface
, REFGUID guid
)
157 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
158 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
160 return d3d9_resource_free_private_data(&surface
->resource
, guid
);
163 static DWORD WINAPI
d3d9_surface_SetPriority(IDirect3DSurface9
*iface
, DWORD priority
)
165 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface
, priority
);
169 static DWORD WINAPI
d3d9_surface_GetPriority(IDirect3DSurface9
*iface
)
171 TRACE("iface %p. Ignored on surfaces.\n", iface
);
175 static void WINAPI
d3d9_surface_PreLoad(IDirect3DSurface9
*iface
)
177 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
179 TRACE("iface %p.\n", iface
);
181 wined3d_mutex_lock();
182 wined3d_texture_preload(surface
->wined3d_texture
);
183 wined3d_mutex_unlock();
186 static D3DRESOURCETYPE WINAPI
d3d9_surface_GetType(IDirect3DSurface9
*iface
)
188 TRACE("iface %p.\n", iface
);
190 return D3DRTYPE_SURFACE
;
193 static HRESULT WINAPI
d3d9_surface_GetContainer(IDirect3DSurface9
*iface
, REFIID riid
, void **container
)
195 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
198 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
200 if (!surface
->container
)
201 return E_NOINTERFACE
;
203 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
205 TRACE("Returning %p.\n", *container
);
210 static HRESULT WINAPI
d3d9_surface_GetDesc(IDirect3DSurface9
*iface
, D3DSURFACE_DESC
*desc
)
212 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
213 struct wined3d_resource_desc wined3d_desc
;
214 struct wined3d_resource
*sub_resource
;
216 TRACE("iface %p, desc %p.\n", iface
, desc
);
218 wined3d_mutex_lock();
219 sub_resource
= wined3d_texture_get_sub_resource(surface
->wined3d_texture
, surface
->sub_resource_idx
);
220 wined3d_resource_get_desc(sub_resource
, &wined3d_desc
);
221 wined3d_mutex_unlock();
223 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
224 desc
->Type
= wined3d_desc
.resource_type
;
225 desc
->Usage
= wined3d_desc
.usage
& WINED3DUSAGE_MASK
;
226 desc
->Pool
= wined3d_desc
.pool
;
227 desc
->MultiSampleType
= 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
);
248 box
.left
= rect
->left
;
250 box
.right
= rect
->right
;
251 box
.bottom
= rect
->bottom
;
256 wined3d_mutex_lock();
257 hr
= wined3d_resource_sub_resource_map(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
,
258 &map_desc
, rect
? &box
: NULL
, flags
);
259 wined3d_mutex_unlock();
263 locked_rect
->Pitch
= map_desc
.row_pitch
;
264 locked_rect
->pBits
= map_desc
.data
;
270 static HRESULT WINAPI
d3d9_surface_UnlockRect(IDirect3DSurface9
*iface
)
272 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
275 TRACE("iface %p.\n", iface
);
277 wined3d_mutex_lock();
278 hr
= wined3d_resource_sub_resource_unmap(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
);
279 wined3d_mutex_unlock();
283 case WINEDDERR_NOTLOCKED
: return D3DERR_INVALIDCALL
;
288 static HRESULT WINAPI
d3d9_surface_GetDC(IDirect3DSurface9
*iface
, HDC
*dc
)
290 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
293 TRACE("iface %p, dc %p.\n", iface
, dc
);
295 if (!surface
->getdc_supported
)
297 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
298 /* Don't touch the DC */
299 return D3DERR_INVALIDCALL
;
302 wined3d_mutex_lock();
303 hr
= wined3d_texture_get_dc(surface
->wined3d_texture
, surface
->sub_resource_idx
, dc
);
304 wined3d_mutex_unlock();
309 static HRESULT WINAPI
d3d9_surface_ReleaseDC(IDirect3DSurface9
*iface
, HDC dc
)
311 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
314 TRACE("iface %p, dc %p.\n", iface
, dc
);
316 wined3d_mutex_lock();
317 hr
= wined3d_texture_release_dc(surface
->wined3d_texture
, surface
->sub_resource_idx
, dc
);
318 wined3d_mutex_unlock();
322 case WINEDDERR_NODC
: return D3DERR_INVALIDCALL
;
327 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl
=
330 d3d9_surface_QueryInterface
,
332 d3d9_surface_Release
,
333 /* IDirect3DResource9 */
334 d3d9_surface_GetDevice
,
335 d3d9_surface_SetPrivateData
,
336 d3d9_surface_GetPrivateData
,
337 d3d9_surface_FreePrivateData
,
338 d3d9_surface_SetPriority
,
339 d3d9_surface_GetPriority
,
340 d3d9_surface_PreLoad
,
341 d3d9_surface_GetType
,
342 /* IDirect3DSurface9 */
343 d3d9_surface_GetContainer
,
344 d3d9_surface_GetDesc
,
345 d3d9_surface_LockRect
,
346 d3d9_surface_UnlockRect
,
348 d3d9_surface_ReleaseDC
,
351 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
353 struct d3d9_surface
*surface
= parent
;
354 d3d9_resource_cleanup(&surface
->resource
);
355 HeapFree(GetProcessHeap(), 0, surface
);
358 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops
=
360 surface_wined3d_object_destroyed
,
363 void surface_init(struct d3d9_surface
*surface
, struct wined3d_texture
*wined3d_texture
,
364 unsigned int sub_resource_idx
, const struct wined3d_parent_ops
**parent_ops
)
366 struct wined3d_resource_desc desc
;
367 IDirect3DBaseTexture9
*texture
;
369 surface
->IDirect3DSurface9_iface
.lpVtbl
= &d3d9_surface_vtbl
;
370 d3d9_resource_init(&surface
->resource
);
371 surface
->resource
.refcount
= 0;
372 list_init(&surface
->rtv_entry
);
373 surface
->container
= wined3d_texture_get_parent(wined3d_texture
);
374 surface
->wined3d_texture
= wined3d_texture
;
375 surface
->sub_resource_idx
= sub_resource_idx
;
377 if (surface
->container
&& SUCCEEDED(IUnknown_QueryInterface(surface
->container
,
378 &IID_IDirect3DBaseTexture9
, (void **)&texture
)))
380 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture9(texture
);
381 IDirect3DBaseTexture9_Release(texture
);
384 wined3d_resource_get_desc(wined3d_texture_get_resource(wined3d_texture
), &desc
);
385 switch (d3dformat_from_wined3dformat(desc
.format
))
387 case D3DFMT_A8R8G8B8
:
388 case D3DFMT_X8R8G8B8
:
390 case D3DFMT_X1R5G5B5
:
391 case D3DFMT_A1R5G5B5
:
393 surface
->getdc_supported
= TRUE
;
397 surface
->getdc_supported
= FALSE
;
401 *parent_ops
= &d3d9_surface_wined3d_parent_ops
;
404 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
406 struct d3d9_surface
*surface
= parent
;
408 /* If the surface reference count drops to zero, we release our reference
409 * to the view, but don't clear the pointer yet, in case e.g. a
410 * GetRenderTarget() call brings the surface back before the view is
411 * actually destroyed. When the view is destroyed, we need to clear the
412 * pointer, or a subsequent surface AddRef() would reference it again.
414 * This is safe because as long as the view still has a reference to the
415 * texture, the surface is also still alive, and we're called before the
416 * view releases that reference. */
417 surface
->wined3d_rtv
= NULL
;
418 list_remove(&surface
->rtv_entry
);
421 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops
=
423 view_wined3d_object_destroyed
,
426 struct wined3d_rendertarget_view
*d3d9_surface_get_rendertarget_view(struct d3d9_surface
*surface
)
430 if (surface
->wined3d_rtv
)
431 return surface
->wined3d_rtv
;
433 if (FAILED(hr
= wined3d_rendertarget_view_create_from_sub_resource(surface
->wined3d_texture
,
434 surface
->sub_resource_idx
, surface
, &d3d9_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
436 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
440 if (surface
->texture
)
441 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
443 return surface
->wined3d_rtv
;
446 struct d3d9_surface
*unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
450 assert(iface
->lpVtbl
== &d3d9_surface_vtbl
);
452 return impl_from_IDirect3DSurface9(iface
);