2 * IDirect3DSurface8 implementation
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8
);
26 static inline struct d3d8_surface
*impl_from_IDirect3DSurface8(IDirect3DSurface8
*iface
)
28 return CONTAINING_RECORD(iface
, struct d3d8_surface
, IDirect3DSurface8_iface
);
31 static HRESULT WINAPI
d3d8_surface_QueryInterface(IDirect3DSurface8
*iface
, REFIID riid
, void **out
)
33 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
35 if (IsEqualGUID(riid
, &IID_IDirect3DSurface8
)
36 || IsEqualGUID(riid
, &IID_IDirect3DResource8
)
37 || IsEqualGUID(riid
, &IID_IUnknown
))
39 IDirect3DSurface8_AddRef(iface
);
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
50 static ULONG WINAPI
d3d8_surface_AddRef(IDirect3DSurface8
*iface
)
52 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
55 TRACE("iface %p.\n", iface
);
59 TRACE("Forwarding to %p.\n", surface
->texture
);
60 return IDirect3DBaseTexture8_AddRef(&surface
->texture
->IDirect3DBaseTexture8_iface
);
63 refcount
= InterlockedIncrement(&surface
->resource
.refcount
);
64 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
68 if (surface
->parent_device
)
69 IDirect3DDevice8_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
d3d8_surface_Release(IDirect3DSurface8
*iface
)
82 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
85 TRACE("iface %p.\n", iface
);
89 TRACE("Forwarding to %p.\n", surface
->texture
);
90 return IDirect3DBaseTexture8_Release(&surface
->texture
->IDirect3DBaseTexture8_iface
);
93 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
94 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
98 IDirect3DDevice8
*parent_device
= surface
->parent_device
;
100 wined3d_mutex_lock();
101 if (surface
->wined3d_rtv
)
102 wined3d_rendertarget_view_decref(surface
->wined3d_rtv
);
103 wined3d_texture_decref(surface
->wined3d_texture
);
104 wined3d_mutex_unlock();
107 IDirect3DDevice8_Release(parent_device
);
113 static HRESULT WINAPI
d3d8_surface_GetDevice(IDirect3DSurface8
*iface
, IDirect3DDevice8
**device
)
115 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
117 TRACE("iface %p, device %p.\n", iface
, device
);
119 if (surface
->texture
)
120 return IDirect3DBaseTexture8_GetDevice(&surface
->texture
->IDirect3DBaseTexture8_iface
, device
);
122 *device
= surface
->parent_device
;
123 IDirect3DDevice8_AddRef(*device
);
125 TRACE("Returning device %p.\n", *device
);
130 static HRESULT WINAPI
d3d8_surface_SetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
131 const void *data
, DWORD data_size
, DWORD flags
)
133 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
134 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
135 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
137 return d3d8_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
140 static HRESULT WINAPI
d3d8_surface_GetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
141 void *data
, DWORD
*data_size
)
143 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
144 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
145 iface
, debugstr_guid(guid
), data
, data_size
);
147 return d3d8_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
150 static HRESULT WINAPI
d3d8_surface_FreePrivateData(IDirect3DSurface8
*iface
, REFGUID guid
)
152 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
153 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
155 return d3d8_resource_free_private_data(&surface
->resource
, guid
);
158 static HRESULT WINAPI
d3d8_surface_GetContainer(IDirect3DSurface8
*iface
, REFIID riid
, void **container
)
160 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
163 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
165 if (!surface
->container
)
166 return E_NOINTERFACE
;
168 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
170 TRACE("Returning %p.\n", *container
);
175 static HRESULT WINAPI
d3d8_surface_GetDesc(IDirect3DSurface8
*iface
, D3DSURFACE_DESC
*desc
)
177 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
178 struct wined3d_resource_desc wined3d_desc
;
179 struct wined3d_resource
*sub_resource
;
181 TRACE("iface %p, desc %p.\n", iface
, desc
);
183 wined3d_mutex_lock();
184 sub_resource
= wined3d_texture_get_sub_resource(surface
->wined3d_texture
, surface
->sub_resource_idx
);
185 wined3d_resource_get_desc(sub_resource
, &wined3d_desc
);
186 wined3d_mutex_unlock();
188 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
189 desc
->Type
= wined3d_desc
.resource_type
;
190 desc
->Usage
= wined3d_desc
.usage
& WINED3DUSAGE_MASK
;
191 desc
->Pool
= wined3d_desc
.pool
;
192 desc
->Size
= wined3d_desc
.size
;
193 desc
->MultiSampleType
= wined3d_desc
.multisample_type
;
194 desc
->Width
= wined3d_desc
.width
;
195 desc
->Height
= wined3d_desc
.height
;
200 static HRESULT WINAPI
d3d8_surface_LockRect(IDirect3DSurface8
*iface
,
201 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
203 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
204 struct wined3d_box box
;
205 struct wined3d_map_desc map_desc
;
208 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
209 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
211 wined3d_mutex_lock();
214 D3DSURFACE_DESC desc
;
215 IDirect3DSurface8_GetDesc(iface
, &desc
);
219 || (rect
->left
>= rect
->right
)
220 || (rect
->top
>= rect
->bottom
)
221 || (rect
->right
> desc
.Width
)
222 || (rect
->bottom
> desc
.Height
))
224 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
225 wined3d_mutex_unlock();
227 return D3DERR_INVALIDCALL
;
229 box
.left
= rect
->left
;
231 box
.right
= rect
->right
;
232 box
.bottom
= rect
->bottom
;
237 hr
= wined3d_resource_map(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
,
238 &map_desc
, rect
? &box
: NULL
, flags
);
239 wined3d_mutex_unlock();
243 locked_rect
->Pitch
= map_desc
.row_pitch
;
244 locked_rect
->pBits
= map_desc
.data
;
248 locked_rect
->Pitch
= 0;
249 locked_rect
->pBits
= NULL
;
255 static HRESULT WINAPI
d3d8_surface_UnlockRect(IDirect3DSurface8
*iface
)
257 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
260 TRACE("iface %p.\n", iface
);
262 wined3d_mutex_lock();
263 hr
= wined3d_resource_unmap(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
);
264 wined3d_mutex_unlock();
268 case WINEDDERR_NOTLOCKED
: return D3DERR_INVALIDCALL
;
273 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl
=
276 d3d8_surface_QueryInterface
,
278 d3d8_surface_Release
,
279 /* IDirect3DResource8 */
280 d3d8_surface_GetDevice
,
281 d3d8_surface_SetPrivateData
,
282 d3d8_surface_GetPrivateData
,
283 d3d8_surface_FreePrivateData
,
284 /* IDirect3DSurface8 */
285 d3d8_surface_GetContainer
,
286 d3d8_surface_GetDesc
,
287 d3d8_surface_LockRect
,
288 d3d8_surface_UnlockRect
,
291 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
293 struct d3d8_surface
*surface
= parent
;
294 d3d8_resource_cleanup(&surface
->resource
);
295 HeapFree(GetProcessHeap(), 0, surface
);
298 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops
=
300 surface_wined3d_object_destroyed
,
303 void surface_init(struct d3d8_surface
*surface
, struct wined3d_texture
*wined3d_texture
, unsigned int sub_resource_idx
,
304 const struct wined3d_parent_ops
**parent_ops
)
306 IDirect3DBaseTexture8
*texture
;
308 surface
->IDirect3DSurface8_iface
.lpVtbl
= &d3d8_surface_vtbl
;
309 d3d8_resource_init(&surface
->resource
);
310 surface
->resource
.refcount
= 0;
311 list_init(&surface
->rtv_entry
);
312 surface
->container
= wined3d_texture_get_parent(wined3d_texture
);
313 surface
->wined3d_texture
= wined3d_texture
;
314 surface
->sub_resource_idx
= sub_resource_idx
;
316 if (surface
->container
&& SUCCEEDED(IUnknown_QueryInterface(surface
->container
,
317 &IID_IDirect3DBaseTexture8
, (void **)&texture
)))
319 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture8(texture
);
320 IDirect3DBaseTexture8_Release(texture
);
323 *parent_ops
= &d3d8_surface_wined3d_parent_ops
;
326 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
328 struct d3d8_surface
*surface
= parent
;
330 /* If the surface reference count drops to zero, we release our reference
331 * to the view, but don't clear the pointer yet, in case e.g. a
332 * GetRenderTarget() call brings the surface back before the view is
333 * actually destroyed. When the view is destroyed, we need to clear the
334 * pointer, or a subsequent surface AddRef() would reference it again.
336 * This is safe because as long as the view still has a reference to the
337 * texture, the surface is also still alive, and we're called before the
338 * view releases that reference. */
339 surface
->wined3d_rtv
= NULL
;
340 list_remove(&surface
->rtv_entry
);
343 static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops
=
345 view_wined3d_object_destroyed
,
348 struct wined3d_rendertarget_view
*d3d8_surface_get_rendertarget_view(struct d3d8_surface
*surface
)
352 if (surface
->wined3d_rtv
)
353 return surface
->wined3d_rtv
;
355 if (FAILED(hr
= wined3d_rendertarget_view_create_from_sub_resource(surface
->wined3d_texture
,
356 surface
->sub_resource_idx
, surface
, &d3d8_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
358 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
362 if (surface
->texture
)
363 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
365 return surface
->wined3d_rtv
;
368 struct d3d8_surface
*unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8
*iface
)
372 assert(iface
->lpVtbl
== &d3d8_surface_vtbl
);
374 return impl_from_IDirect3DSurface8(iface
);