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
21 #include "d3d8_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8
);
25 static inline struct d3d8_surface
*impl_from_IDirect3DSurface8(IDirect3DSurface8
*iface
)
27 return CONTAINING_RECORD(iface
, struct d3d8_surface
, IDirect3DSurface8_iface
);
30 static HRESULT WINAPI
d3d8_surface_QueryInterface(IDirect3DSurface8
*iface
, REFIID riid
, void **out
)
32 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
34 if (IsEqualGUID(riid
, &IID_IDirect3DSurface8
)
35 || IsEqualGUID(riid
, &IID_IDirect3DResource8
)
36 || IsEqualGUID(riid
, &IID_IUnknown
))
38 IDirect3DSurface8_AddRef(iface
);
43 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
49 static ULONG WINAPI
d3d8_surface_AddRef(IDirect3DSurface8
*iface
)
51 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
54 TRACE("iface %p.\n", iface
);
58 TRACE("Forwarding to %p.\n", surface
->texture
);
59 return IDirect3DBaseTexture8_AddRef(&surface
->texture
->IDirect3DBaseTexture8_iface
);
62 refcount
= InterlockedIncrement(&surface
->resource
.refcount
);
63 TRACE("%p increasing refcount to %lu.\n", iface
, refcount
);
67 if (surface
->parent_device
)
68 IDirect3DDevice8_AddRef(surface
->parent_device
);
69 if (surface
->wined3d_rtv
)
70 wined3d_rendertarget_view_incref(surface
->wined3d_rtv
);
71 wined3d_texture_incref(surface
->wined3d_texture
);
77 static ULONG WINAPI
d3d8_surface_Release(IDirect3DSurface8
*iface
)
79 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
82 TRACE("iface %p.\n", iface
);
86 TRACE("Forwarding to %p.\n", surface
->texture
);
87 return IDirect3DBaseTexture8_Release(&surface
->texture
->IDirect3DBaseTexture8_iface
);
90 if (!surface
->resource
.refcount
)
92 WARN("Surface does not have any references.\n");
96 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
97 TRACE("%p decreasing refcount to %lu.\n", iface
, refcount
);
101 IDirect3DDevice8
*parent_device
= surface
->parent_device
;
103 if (surface
->wined3d_rtv
)
104 wined3d_rendertarget_view_decref(surface
->wined3d_rtv
);
105 wined3d_texture_decref(surface
->wined3d_texture
);
108 IDirect3DDevice8_Release(parent_device
);
114 static HRESULT WINAPI
d3d8_surface_GetDevice(IDirect3DSurface8
*iface
, IDirect3DDevice8
**device
)
116 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
118 TRACE("iface %p, device %p.\n", iface
, device
);
120 if (surface
->texture
)
121 return IDirect3DBaseTexture8_GetDevice(&surface
->texture
->IDirect3DBaseTexture8_iface
, device
);
123 *device
= surface
->parent_device
;
124 IDirect3DDevice8_AddRef(*device
);
126 TRACE("Returning device %p.\n", *device
);
131 static HRESULT WINAPI
d3d8_surface_SetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
132 const void *data
, DWORD data_size
, DWORD flags
)
134 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
135 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
136 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
138 return d3d8_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
141 static HRESULT WINAPI
d3d8_surface_GetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
142 void *data
, DWORD
*data_size
)
144 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
145 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
146 iface
, debugstr_guid(guid
), data
, data_size
);
148 return d3d8_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
151 static HRESULT WINAPI
d3d8_surface_FreePrivateData(IDirect3DSurface8
*iface
, REFGUID guid
)
153 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
154 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
156 return d3d8_resource_free_private_data(&surface
->resource
, guid
);
159 static HRESULT WINAPI
d3d8_surface_GetContainer(IDirect3DSurface8
*iface
, REFIID riid
, void **container
)
161 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
164 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
166 if (!surface
->container
)
167 return E_NOINTERFACE
;
169 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
171 TRACE("Returning %p.\n", *container
);
176 static HRESULT WINAPI
d3d8_surface_GetDesc(IDirect3DSurface8
*iface
, D3DSURFACE_DESC
*desc
)
178 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
179 struct wined3d_sub_resource_desc wined3d_desc
;
181 TRACE("iface %p, desc %p.\n", iface
, desc
);
183 wined3d_mutex_lock();
184 wined3d_texture_get_sub_resource_desc(surface
->wined3d_texture
, surface
->sub_resource_idx
, &wined3d_desc
);
185 wined3d_mutex_unlock();
187 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
188 desc
->Type
= D3DRTYPE_SURFACE
;
189 desc
->Usage
= d3dusage_from_wined3dusage(wined3d_desc
.usage
, wined3d_desc
.bind_flags
);
190 desc
->Pool
= d3dpool_from_wined3daccess(wined3d_desc
.access
, wined3d_desc
.usage
);
191 desc
->Size
= wined3d_desc
.size
;
192 desc
->MultiSampleType
= d3dmultisample_type_from_wined3d(wined3d_desc
.multisample_type
);
193 desc
->Width
= wined3d_desc
.width
;
194 desc
->Height
= wined3d_desc
.height
;
199 static HRESULT WINAPI
d3d8_surface_LockRect(IDirect3DSurface8
*iface
,
200 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
202 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
203 struct wined3d_box box
;
204 struct wined3d_map_desc map_desc
;
206 D3DRESOURCETYPE type
;
208 TRACE("iface %p, locked_rect %p, rect %s, flags %#lx.\n",
209 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
211 wined3d_mutex_lock();
213 if (surface
->texture
)
214 type
= IDirect3DBaseTexture8_GetType(&surface
->texture
->IDirect3DBaseTexture8_iface
);
216 type
= D3DRTYPE_SURFACE
;
220 D3DSURFACE_DESC desc
;
221 IDirect3DSurface8_GetDesc(iface
, &desc
);
223 if (type
!= D3DRTYPE_TEXTURE
226 || (rect
->left
>= rect
->right
)
227 || (rect
->top
>= rect
->bottom
)
228 || (rect
->right
> desc
.Width
)
229 || (rect
->bottom
> desc
.Height
)))
231 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
232 wined3d_mutex_unlock();
234 locked_rect
->Pitch
= 0;
235 locked_rect
->pBits
= NULL
;
236 return D3DERR_INVALIDCALL
;
238 wined3d_box_set(&box
, rect
->left
, rect
->top
, rect
->right
, rect
->bottom
, 0, 1);
241 hr
= wined3d_resource_map(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
,
242 &map_desc
, rect
? &box
: NULL
, wined3dmapflags_from_d3dmapflags(flags
, 0));
243 wined3d_mutex_unlock();
247 locked_rect
->Pitch
= map_desc
.row_pitch
;
248 locked_rect
->pBits
= map_desc
.data
;
250 else if (type
!= D3DRTYPE_TEXTURE
)
252 locked_rect
->Pitch
= 0;
253 locked_rect
->pBits
= NULL
;
256 if (hr
== E_INVALIDARG
)
257 return D3DERR_INVALIDCALL
;
261 static HRESULT WINAPI
d3d8_surface_UnlockRect(IDirect3DSurface8
*iface
)
263 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
266 TRACE("iface %p.\n", iface
);
268 hr
= wined3d_resource_unmap(wined3d_texture_get_resource(surface
->wined3d_texture
), surface
->sub_resource_idx
);
270 if (hr
== WINEDDERR_NOTLOCKED
)
272 D3DRESOURCETYPE type
;
273 if (surface
->texture
)
274 type
= IDirect3DBaseTexture8_GetType(&surface
->texture
->IDirect3DBaseTexture8_iface
);
276 type
= D3DRTYPE_SURFACE
;
277 hr
= type
== D3DRTYPE_TEXTURE
? D3D_OK
: D3DERR_INVALIDCALL
;
282 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl
=
285 d3d8_surface_QueryInterface
,
287 d3d8_surface_Release
,
288 /* IDirect3DResource8 */
289 d3d8_surface_GetDevice
,
290 d3d8_surface_SetPrivateData
,
291 d3d8_surface_GetPrivateData
,
292 d3d8_surface_FreePrivateData
,
293 /* IDirect3DSurface8 */
294 d3d8_surface_GetContainer
,
295 d3d8_surface_GetDesc
,
296 d3d8_surface_LockRect
,
297 d3d8_surface_UnlockRect
,
300 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
302 struct d3d8_surface
*surface
= parent
;
303 d3d8_resource_cleanup(&surface
->resource
);
307 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops
=
309 surface_wined3d_object_destroyed
,
312 void surface_init(struct d3d8_surface
*surface
, struct wined3d_texture
*wined3d_texture
, unsigned int sub_resource_idx
,
313 const struct wined3d_parent_ops
**parent_ops
)
315 IDirect3DBaseTexture8
*texture
;
317 surface
->IDirect3DSurface8_iface
.lpVtbl
= &d3d8_surface_vtbl
;
318 d3d8_resource_init(&surface
->resource
);
319 surface
->resource
.refcount
= 0;
320 list_init(&surface
->rtv_entry
);
321 surface
->container
= wined3d_texture_get_parent(wined3d_texture
);
322 surface
->wined3d_texture
= wined3d_texture
;
323 surface
->sub_resource_idx
= sub_resource_idx
;
325 if (surface
->container
&& SUCCEEDED(IUnknown_QueryInterface(surface
->container
,
326 &IID_IDirect3DBaseTexture8
, (void **)&texture
)))
328 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture8(texture
);
329 IDirect3DBaseTexture8_Release(texture
);
332 *parent_ops
= &d3d8_surface_wined3d_parent_ops
;
335 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
337 struct d3d8_surface
*surface
= parent
;
339 /* If the surface reference count drops to zero, we release our reference
340 * to the view, but don't clear the pointer yet, in case e.g. a
341 * GetRenderTarget() call brings the surface back before the view is
342 * actually destroyed. When the view is destroyed, we need to clear the
343 * pointer, or a subsequent surface AddRef() would reference it again.
345 * This is safe because as long as the view still has a reference to the
346 * texture, the surface is also still alive, and we're called before the
347 * view releases that reference. */
348 surface
->wined3d_rtv
= NULL
;
349 list_remove(&surface
->rtv_entry
);
352 static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops
=
354 view_wined3d_object_destroyed
,
357 struct d3d8_device
*d3d8_surface_get_device(const struct d3d8_surface
*surface
)
359 IDirect3DDevice8
*device
;
360 device
= surface
->texture
? surface
->texture
->parent_device
: surface
->parent_device
;
361 return impl_from_IDirect3DDevice8(device
);
364 struct wined3d_rendertarget_view
*d3d8_surface_acquire_rendertarget_view(struct d3d8_surface
*surface
)
368 /* The surface reference count can be equal to 0 when this function is
369 * called. In order to properly manage the render target view reference
370 * count, we temporarily increment the surface reference count. */
371 d3d8_surface_AddRef(&surface
->IDirect3DSurface8_iface
);
373 if (surface
->wined3d_rtv
)
374 return surface
->wined3d_rtv
;
376 if (FAILED(hr
= wined3d_rendertarget_view_create_from_sub_resource(surface
->wined3d_texture
,
377 surface
->sub_resource_idx
, surface
, &d3d8_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
379 ERR("Failed to create rendertarget view, hr %#lx.\n", hr
);
380 d3d8_surface_Release(&surface
->IDirect3DSurface8_iface
);
384 if (surface
->texture
)
385 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
387 return surface
->wined3d_rtv
;
390 void d3d8_surface_release_rendertarget_view(struct d3d8_surface
*surface
,
391 struct wined3d_rendertarget_view
*rtv
)
394 d3d8_surface_Release(&surface
->IDirect3DSurface8_iface
);
397 struct d3d8_surface
*unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8
*iface
)
401 assert(iface
->lpVtbl
== &d3d8_surface_vtbl
);
403 return impl_from_IDirect3DSurface8(iface
);