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, object %p.\n", iface
, debugstr_guid(riid
), out
);
35 if (IsEqualGUID(riid
, &IID_IDirect3DSurface8
)
36 || IsEqualGUID(riid
, &IID_IDirect3DResource8
)
37 || IsEqualGUID(riid
, &IID_IUnknown
))
39 IUnknown_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
);
54 TRACE("iface %p.\n", iface
);
56 if (surface
->forwardReference
)
58 /* Forward refcounting */
59 TRACE("Forwarding to %p.\n", surface
->forwardReference
);
60 return IUnknown_AddRef(surface
->forwardReference
);
64 /* No container, handle our own refcounting */
65 ULONG ref
= InterlockedIncrement(&surface
->refcount
);
67 TRACE("%p increasing refcount to %u.\n", iface
, ref
);
71 if (surface
->parent_device
)
72 IUnknown_AddRef(surface
->parent_device
);
74 wined3d_surface_incref(surface
->wined3d_surface
);
75 wined3d_mutex_unlock();
82 static ULONG WINAPI
d3d8_surface_Release(IDirect3DSurface8
*iface
)
84 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
86 TRACE("iface %p.\n", iface
);
88 if (surface
->forwardReference
)
90 /* Forward refcounting */
91 TRACE("Forwarding to %p.\n", surface
->forwardReference
);
92 return IUnknown_Release(surface
->forwardReference
);
96 /* No container, handle our own refcounting */
97 ULONG ref
= InterlockedDecrement(&surface
->refcount
);
99 TRACE("%p decreasing refcount to %u.\n", iface
, ref
);
103 IDirect3DDevice8
*parent_device
= surface
->parent_device
;
105 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
106 wined3d_mutex_lock();
107 wined3d_surface_decref(surface
->wined3d_surface
);
108 wined3d_mutex_unlock();
111 IDirect3DDevice8_Release(parent_device
);
118 static HRESULT WINAPI
d3d8_surface_GetDevice(IDirect3DSurface8
*iface
, IDirect3DDevice8
**device
)
120 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
122 TRACE("iface %p, device %p.\n", iface
, device
);
124 if (surface
->forwardReference
)
126 IDirect3DResource8
*resource
;
129 hr
= IUnknown_QueryInterface(surface
->forwardReference
, &IID_IDirect3DResource8
, (void **)&resource
);
132 hr
= IDirect3DResource8_GetDevice(resource
, device
);
133 IDirect3DResource8_Release(resource
);
135 TRACE("Returning device %p.\n", *device
);
141 *device
= surface
->parent_device
;
142 IDirect3DDevice8_AddRef(*device
);
144 TRACE("Returning device %p.\n", *device
);
149 static HRESULT WINAPI
d3d8_surface_SetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
150 const void *data
, DWORD data_size
, DWORD flags
)
152 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
153 struct wined3d_resource
*resource
;
156 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
157 iface
, debugstr_guid(guid
), data
, data_size
, flags
);
159 wined3d_mutex_lock();
160 resource
= wined3d_surface_get_resource(surface
->wined3d_surface
);
161 hr
= wined3d_resource_set_private_data(resource
, guid
, data
, data_size
, flags
);
162 wined3d_mutex_unlock();
167 static HRESULT WINAPI
d3d8_surface_GetPrivateData(IDirect3DSurface8
*iface
, REFGUID guid
,
168 void *data
, DWORD
*data_size
)
170 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
171 struct wined3d_resource
*resource
;
174 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
175 iface
, debugstr_guid(guid
), data
, data_size
);
177 wined3d_mutex_lock();
178 resource
= wined3d_surface_get_resource(surface
->wined3d_surface
);
179 hr
= wined3d_resource_get_private_data(resource
, guid
, data
, data_size
);
180 wined3d_mutex_unlock();
185 static HRESULT WINAPI
d3d8_surface_FreePrivateData(IDirect3DSurface8
*iface
, REFGUID guid
)
187 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
188 struct wined3d_resource
*resource
;
191 TRACE("iface %p, guid %s.\n", iface
, debugstr_guid(guid
));
193 wined3d_mutex_lock();
194 resource
= wined3d_surface_get_resource(surface
->wined3d_surface
);
195 hr
= wined3d_resource_free_private_data(resource
, guid
);
196 wined3d_mutex_unlock();
201 static HRESULT WINAPI
d3d8_surface_GetContainer(IDirect3DSurface8
*iface
, REFIID riid
, void **container
)
203 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
206 TRACE("iface %p, riid %s, container %p.\n", iface
, debugstr_guid(riid
), container
);
208 if (!surface
->container
)
209 return E_NOINTERFACE
;
211 hr
= IUnknown_QueryInterface(surface
->container
, riid
, container
);
213 TRACE("Returning %p.\n", *container
);
218 static HRESULT WINAPI
d3d8_surface_GetDesc(IDirect3DSurface8
*iface
, D3DSURFACE_DESC
*desc
)
220 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
221 struct wined3d_resource_desc wined3d_desc
;
222 struct wined3d_resource
*wined3d_resource
;
224 TRACE("iface %p, desc %p.\n", iface
, desc
);
226 wined3d_mutex_lock();
227 wined3d_resource
= wined3d_surface_get_resource(surface
->wined3d_surface
);
228 wined3d_resource_get_desc(wined3d_resource
, &wined3d_desc
);
229 wined3d_mutex_unlock();
231 desc
->Format
= d3dformat_from_wined3dformat(wined3d_desc
.format
);
232 desc
->Type
= wined3d_desc
.resource_type
;
233 desc
->Usage
= wined3d_desc
.usage
& WINED3DUSAGE_MASK
;
234 desc
->Pool
= wined3d_desc
.pool
;
235 desc
->Size
= wined3d_desc
.size
;
236 desc
->MultiSampleType
= wined3d_desc
.multisample_type
;
237 desc
->Width
= wined3d_desc
.width
;
238 desc
->Height
= wined3d_desc
.height
;
243 static HRESULT WINAPI
d3d8_surface_LockRect(IDirect3DSurface8
*iface
,
244 D3DLOCKED_RECT
*locked_rect
, const RECT
*rect
, DWORD flags
)
246 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
247 struct wined3d_map_desc map_desc
;
250 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
251 iface
, locked_rect
, wine_dbgstr_rect(rect
), flags
);
253 wined3d_mutex_lock();
256 D3DSURFACE_DESC desc
;
257 IDirect3DSurface8_GetDesc(iface
, &desc
);
261 || (rect
->left
>= rect
->right
)
262 || (rect
->top
>= rect
->bottom
)
263 || (rect
->right
> desc
.Width
)
264 || (rect
->bottom
> desc
.Height
))
266 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
267 wined3d_mutex_unlock();
269 return D3DERR_INVALIDCALL
;
273 hr
= wined3d_surface_map(surface
->wined3d_surface
, &map_desc
, rect
, flags
);
274 wined3d_mutex_unlock();
276 locked_rect
->Pitch
= map_desc
.row_pitch
;
277 locked_rect
->pBits
= map_desc
.data
;
282 static HRESULT WINAPI
d3d8_surface_UnlockRect(IDirect3DSurface8
*iface
)
284 struct d3d8_surface
*surface
= impl_from_IDirect3DSurface8(iface
);
287 TRACE("iface %p.\n", iface
);
289 wined3d_mutex_lock();
290 hr
= wined3d_surface_unmap(surface
->wined3d_surface
);
291 wined3d_mutex_unlock();
295 case WINEDDERR_NOTLOCKED
: return D3DERR_INVALIDCALL
;
300 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl
=
303 d3d8_surface_QueryInterface
,
305 d3d8_surface_Release
,
306 /* IDirect3DResource8 */
307 d3d8_surface_GetDevice
,
308 d3d8_surface_SetPrivateData
,
309 d3d8_surface_GetPrivateData
,
310 d3d8_surface_FreePrivateData
,
311 /* IDirect3DSurface8 */
312 d3d8_surface_GetContainer
,
313 d3d8_surface_GetDesc
,
314 d3d8_surface_LockRect
,
315 d3d8_surface_UnlockRect
,
318 static void STDMETHODCALLTYPE
surface_wined3d_object_destroyed(void *parent
)
320 HeapFree(GetProcessHeap(), 0, parent
);
323 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops
=
325 surface_wined3d_object_destroyed
,
328 HRESULT
surface_init(struct d3d8_surface
*surface
, struct d3d8_device
*device
,
329 UINT width
, UINT height
, D3DFORMAT format
, BOOL lockable
, BOOL discard
, UINT level
,
330 DWORD usage
, D3DPOOL pool
, D3DMULTISAMPLE_TYPE multisample_type
, DWORD multisample_quality
)
335 surface
->IDirect3DSurface8_iface
.lpVtbl
= &d3d8_surface_vtbl
;
336 surface
->refcount
= 1;
338 /* FIXME: Check MAX bounds of MultisampleQuality. */
339 if (multisample_quality
> 0)
341 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality
);
342 multisample_quality
= 0;
346 flags
|= WINED3D_SURFACE_MAPPABLE
;
348 flags
|= WINED3D_SURFACE_DISCARD
;
350 wined3d_mutex_lock();
351 hr
= wined3d_surface_create(device
->wined3d_device
, width
, height
, wined3dformat_from_d3dformat(format
),
352 level
, usage
& WINED3DUSAGE_MASK
, (enum wined3d_pool
)pool
, multisample_type
, multisample_quality
,
353 WINED3D_SURFACE_TYPE_OPENGL
, flags
, surface
, &d3d8_surface_wined3d_parent_ops
, &surface
->wined3d_surface
);
354 wined3d_mutex_unlock();
357 WARN("Failed to create wined3d surface, hr %#x.\n", hr
);
361 surface
->parent_device
= &device
->IDirect3DDevice8_iface
;
362 IUnknown_AddRef(surface
->parent_device
);
367 struct d3d8_surface
*unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8
*iface
)
371 assert(iface
->lpVtbl
== &d3d8_surface_vtbl
);
373 return impl_from_IDirect3DSurface8(iface
);