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
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9
);
27 static inline struct d3d9_surface
*impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
29 return CONTAINING_RECORD(iface
, struct d3d9_surface
, IDirect3DSurface9_iface
);
32 static HRESULT WINAPI
d3d9_surface_QueryInterface(IDirect3DSurface9
*iface
, REFIID riid
, void **out
)
34 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
36 if (IsEqualGUID(riid
, &IID_IDirect3DSurface9
)
37 || IsEqualGUID(riid
, &IID_IDirect3DResource9
)
38 || IsEqualGUID(riid
, &IID_IUnknown
))
40 IDirect3DSurface9_AddRef(iface
);
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
51 static ULONG WINAPI
d3d9_surface_AddRef(IDirect3DSurface9
*iface
)
53 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
56 TRACE("iface %p.\n", iface
);
60 TRACE("Forwarding to %p.\n", surface
->texture
);
61 return IDirect3DBaseTexture9_AddRef(&surface
->texture
->IDirect3DBaseTexture9_iface
);
64 refcount
= InterlockedIncrement(&surface
->resource
.refcount
);
65 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
69 if (surface
->parent_device
)
70 IDirect3DDevice9Ex_AddRef(surface
->parent_device
);
72 if (surface
->wined3d_rtv
)
73 wined3d_rendertarget_view_incref(surface
->wined3d_rtv
);
74 wined3d_surface_incref(surface
->wined3d_surface
);
75 wined3d_mutex_unlock();
81 static ULONG WINAPI
d3d9_surface_Release(IDirect3DSurface9
*iface
)
83 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
86 TRACE("iface %p.\n", iface
);
90 TRACE("Forwarding to %p.\n", surface
->texture
);
91 return IDirect3DBaseTexture9_Release(&surface
->texture
->IDirect3DBaseTexture9_iface
);
94 refcount
= InterlockedDecrement(&surface
->resource
.refcount
);
95 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
99 IDirect3DDevice9Ex
*parent_device
= surface
->parent_device
;
101 wined3d_mutex_lock();
102 if (surface
->wined3d_rtv
)
103 wined3d_rendertarget_view_decref(surface
->wined3d_rtv
);
104 wined3d_surface_decref(surface
->wined3d_surface
);
105 wined3d_mutex_unlock();
107 /* Release the device last, as it may cause the device to be destroyed. */
109 IDirect3DDevice9Ex_Release(parent_device
);
115 static HRESULT WINAPI
d3d9_surface_GetDevice(IDirect3DSurface9
*iface
, IDirect3DDevice9
**device
)
117 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
119 TRACE("iface %p, device %p.\n", iface
, device
);
121 if (surface
->texture
)
122 return IDirect3DBaseTexture9_GetDevice(&surface
->texture
->IDirect3DBaseTexture9_iface
, device
);
124 *device
= (IDirect3DDevice9
*)surface
->parent_device
;
125 IDirect3DDevice9_AddRef(*device
);
127 TRACE("Returning device %p.\n", *device
);
132 static HRESULT WINAPI
d3d9_surface_SetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
133 const void *data
, DWORD data_size
, DWORD flags
)
135 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
136 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
137 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
139 return d3d9_resource_set_private_data(&surface
->resource
, guid
, data
, data_size
, flags
);
142 static HRESULT WINAPI
d3d9_surface_GetPrivateData(IDirect3DSurface9
*iface
, REFGUID guid
,
143 void *data
, DWORD
*data_size
)
145 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
146 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
147 iface
, debugstr_guid(guid
), data
, data_size
);
149 return d3d9_resource_get_private_data(&surface
->resource
, guid
, data
, data_size
);
152 static HRESULT WINAPI
d3d9_surface_FreePrivateData(IDirect3DSurface9
*iface
, REFGUID guid
)
154 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
155 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
157 return d3d9_resource_free_private_data(&surface
->resource
, guid
);
160 static DWORD WINAPI
d3d9_surface_SetPriority(IDirect3DSurface9
*iface
, DWORD priority
)
162 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface
, priority
);
166 static DWORD WINAPI
d3d9_surface_GetPriority(IDirect3DSurface9
*iface
)
168 TRACE("iface %p. Ignored on surfaces.\n", iface
);
172 static void WINAPI
d3d9_surface_PreLoad(IDirect3DSurface9
*iface
)
174 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
176 TRACE("iface %p.\n", iface
);
178 wined3d_mutex_lock();
179 wined3d_surface_preload(surface
->wined3d_surface
);
180 wined3d_mutex_unlock();
183 static D3DRESOURCETYPE WINAPI
d3d9_surface_GetType(IDirect3DSurface9
*iface
)
185 TRACE("iface %p.\n", iface
);
187 return D3DRTYPE_SURFACE
;
190 static HRESULT WINAPI
d3d9_surface_GetContainer(IDirect3DSurface9
*iface
, REFIID riid
, void **container
)
192 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
195 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
197 if (!surface
->container
)
198 return E_NOINTERFACE
;
200 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
202 TRACE("Returning %p.\n", *container
);
207 static HRESULT WINAPI
d3d9_surface_GetDesc(IDirect3DSurface9
*iface
, D3DSURFACE_DESC
*desc
)
209 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
210 struct wined3d_resource_desc wined3d_desc
;
211 struct wined3d_resource
*wined3d_resource
;
213 TRACE("iface %p, desc %p.\n", iface
, desc
);
215 wined3d_mutex_lock();
216 wined3d_resource
= wined3d_surface_get_resource(surface
->wined3d_surface
);
217 wined3d_resource_get_desc(wined3d_resource
, &wined3d_desc
);
218 wined3d_mutex_unlock();
220 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
221 desc
->Type
= wined3d_desc
.resource_type
;
222 desc
->Usage
= wined3d_desc
.usage
& WINED3DUSAGE_MASK
;
223 desc
->Pool
= wined3d_desc
.pool
;
224 desc
->MultiSampleType
= wined3d_desc
.multisample_type
;
225 desc
->MultiSampleQuality
= wined3d_desc
.multisample_quality
;
226 desc
->Width
= wined3d_desc
.width
;
227 desc
->Height
= wined3d_desc
.height
;
232 static HRESULT WINAPI
d3d9_surface_LockRect(IDirect3DSurface9
*iface
,
233 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
235 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
236 struct wined3d_map_desc map_desc
;
239 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
240 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
242 wined3d_mutex_lock();
243 hr
= wined3d_surface_map(surface
->wined3d_surface
, &map_desc
, rect
, flags
);
244 wined3d_mutex_unlock();
248 locked_rect
->Pitch
= map_desc
.row_pitch
;
249 locked_rect
->pBits
= map_desc
.data
;
255 static HRESULT WINAPI
d3d9_surface_UnlockRect(IDirect3DSurface9
*iface
)
257 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
260 TRACE("iface %p.\n", iface
);
262 wined3d_mutex_lock();
263 hr
= wined3d_surface_unmap(surface
->wined3d_surface
);
264 wined3d_mutex_unlock();
268 case WINEDDERR_NOTLOCKED
: return D3DERR_INVALIDCALL
;
273 static HRESULT WINAPI
d3d9_surface_GetDC(IDirect3DSurface9
*iface
, HDC
*dc
)
275 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
278 TRACE("iface %p, dc %p.\n", iface
, dc
);
280 if (!surface
->getdc_supported
)
282 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
283 /* Don't touch the DC */
284 return D3DERR_INVALIDCALL
;
287 wined3d_mutex_lock();
288 hr
= wined3d_surface_getdc(surface
->wined3d_surface
, dc
);
289 wined3d_mutex_unlock();
294 static HRESULT WINAPI
d3d9_surface_ReleaseDC(IDirect3DSurface9
*iface
, HDC dc
)
296 struct d3d9_surface
*surface
= impl_from_IDirect3DSurface9(iface
);
299 TRACE("iface %p, dc %p.\n", iface
, dc
);
301 wined3d_mutex_lock();
302 hr
= wined3d_surface_releasedc(surface
->wined3d_surface
, dc
);
303 wined3d_mutex_unlock();
307 case WINEDDERR_NODC
: return D3DERR_INVALIDCALL
;
312 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl
=
315 d3d9_surface_QueryInterface
,
317 d3d9_surface_Release
,
318 /* IDirect3DResource9 */
319 d3d9_surface_GetDevice
,
320 d3d9_surface_SetPrivateData
,
321 d3d9_surface_GetPrivateData
,
322 d3d9_surface_FreePrivateData
,
323 d3d9_surface_SetPriority
,
324 d3d9_surface_GetPriority
,
325 d3d9_surface_PreLoad
,
326 d3d9_surface_GetType
,
327 /* IDirect3DSurface9 */
328 d3d9_surface_GetContainer
,
329 d3d9_surface_GetDesc
,
330 d3d9_surface_LockRect
,
331 d3d9_surface_UnlockRect
,
333 d3d9_surface_ReleaseDC
,
336 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
338 struct d3d9_surface
*surface
= parent
;
339 d3d9_resource_cleanup(&surface
->resource
);
340 HeapFree(GetProcessHeap(), 0, surface
);
343 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops
=
345 surface_wined3d_object_destroyed
,
348 void surface_init(struct d3d9_surface
*surface
, IUnknown
*container_parent
,
349 struct wined3d_surface
*wined3d_surface
, const struct wined3d_parent_ops
**parent_ops
)
351 struct wined3d_resource_desc desc
;
352 IDirect3DBaseTexture9
*texture
;
354 surface
->IDirect3DSurface9_iface
.lpVtbl
= &d3d9_surface_vtbl
;
355 d3d9_resource_init(&surface
->resource
);
356 surface
->resource
.refcount
= 0;
357 surface
->wined3d_surface
= wined3d_surface
;
358 list_init(&surface
->rtv_entry
);
359 surface
->container
= container_parent
;
361 if (container_parent
&& SUCCEEDED(IUnknown_QueryInterface(container_parent
,
362 &IID_IDirect3DBaseTexture9
, (void **)&texture
)))
364 surface
->texture
= unsafe_impl_from_IDirect3DBaseTexture9(texture
);
365 IDirect3DBaseTexture9_Release(texture
);
368 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface
), &desc
);
369 switch (d3dformat_from_wined3dformat(desc
.format
))
371 case D3DFMT_A8R8G8B8
:
372 case D3DFMT_X8R8G8B8
:
374 case D3DFMT_X1R5G5B5
:
375 case D3DFMT_A1R5G5B5
:
377 surface
->getdc_supported
= TRUE
;
381 surface
->getdc_supported
= FALSE
;
385 *parent_ops
= &d3d9_surface_wined3d_parent_ops
;
388 static void STDMETHODCALLTYPE
view_wined3d_object_destroyed(void *parent
)
390 struct d3d9_surface
*surface
= parent
;
392 /* If the surface reference count drops to zero, we release our reference
393 * to the view, but don't clear the pointer yet, in case e.g. a
394 * GetRenderTarget() call brings the surface back before the view is
395 * actually destroyed. When the view is destroyed, we need to clear the
396 * pointer, or a subsequent surface AddRef() would reference it again.
398 * This is safe because as long as the view still has a reference to the
399 * texture, the surface is also still alive, and we're called before the
400 * view releases that reference. */
401 surface
->wined3d_rtv
= NULL
;
402 list_remove(&surface
->rtv_entry
);
405 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops
=
407 view_wined3d_object_destroyed
,
410 struct wined3d_rendertarget_view
*d3d9_surface_get_rendertarget_view(struct d3d9_surface
*surface
)
414 if (surface
->wined3d_rtv
)
415 return surface
->wined3d_rtv
;
417 if (FAILED(hr
= wined3d_rendertarget_view_create_from_surface(surface
->wined3d_surface
,
418 surface
, &d3d9_view_wined3d_parent_ops
, &surface
->wined3d_rtv
)))
420 ERR("Failed to create rendertarget view, hr %#x.\n", hr
);
424 if (surface
->texture
)
425 list_add_head(&surface
->texture
->rtv_list
, &surface
->rtv_entry
);
427 return surface
->wined3d_rtv
;
430 struct d3d9_surface
*unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9
*iface
)
434 assert(iface
->lpVtbl
== &d3d9_surface_vtbl
);
436 return impl_from_IDirect3DSurface9(iface
);