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 IDirect3DSurface8Impl *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
28 return CONTAINING_RECORD(iface, IDirect3DSurface8Impl, IDirect3DSurface8_iface);
31 /* IDirect3DSurface8 IUnknown parts follow: */
32 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(IDirect3DSurface8 *iface, REFIID riid,
35 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
37 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
38 || IsEqualGUID(riid, &IID_IDirect3DResource8)
39 || IsEqualGUID(riid, &IID_IUnknown))
41 IUnknown_AddRef(iface);
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
52 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(IDirect3DSurface8 *iface)
54 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
56 TRACE("iface %p.\n", iface);
58 if (This->forwardReference) {
59 /* Forward refcounting */
60 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
61 return IUnknown_AddRef(This->forwardReference);
63 /* No container, handle our own refcounting */
64 ULONG ref = InterlockedIncrement(&This->ref);
66 TRACE("%p increasing refcount to %u.\n", iface, ref);
70 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
72 wined3d_surface_incref(This->wined3d_surface);
73 wined3d_mutex_unlock();
80 static ULONG WINAPI IDirect3DSurface8Impl_Release(IDirect3DSurface8 *iface)
82 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
84 TRACE("iface %p.\n", iface);
86 if (This->forwardReference) {
87 /* Forward refcounting */
88 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
89 return IUnknown_Release(This->forwardReference);
91 /* No container, handle our own refcounting */
92 ULONG ref = InterlockedDecrement(&This->ref);
94 TRACE("%p decreasing refcount to %u.\n", iface, ref);
97 IDirect3DDevice8 *parentDevice = This->parentDevice;
99 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
100 wined3d_mutex_lock();
101 wined3d_surface_decref(This->wined3d_surface);
102 wined3d_mutex_unlock();
104 if (parentDevice) IDirect3DDevice8_Release(parentDevice);
111 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
112 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface,
113 IDirect3DDevice8 **device)
115 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
117 TRACE("iface %p, device %p.\n", iface, device);
119 if (This->forwardReference)
121 IDirect3DResource8 *resource;
124 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
127 hr = IDirect3DResource8_GetDevice(resource, device);
128 IDirect3DResource8_Release(resource);
130 TRACE("Returning device %p.\n", *device);
136 *device = This->parentDevice;
137 IDirect3DDevice8_AddRef(*device);
139 TRACE("Returning device %p.\n", *device);
144 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
145 const void *data, DWORD data_size, DWORD flags)
147 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
148 struct wined3d_resource *resource;
151 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
152 iface, debugstr_guid(guid), data, data_size, flags);
154 wined3d_mutex_lock();
155 resource = wined3d_surface_get_resource(surface->wined3d_surface);
156 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
157 wined3d_mutex_unlock();
162 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
163 void *data, DWORD *data_size)
165 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
166 struct wined3d_resource *resource;
169 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
170 iface, debugstr_guid(guid), data, data_size);
172 wined3d_mutex_lock();
173 resource = wined3d_surface_get_resource(surface->wined3d_surface);
174 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
175 wined3d_mutex_unlock();
180 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
182 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
183 struct wined3d_resource *resource;
186 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
188 wined3d_mutex_lock();
189 resource = wined3d_surface_get_resource(surface->wined3d_surface);
190 hr = wined3d_resource_free_private_data(resource, guid);
191 wined3d_mutex_unlock();
196 /* IDirect3DSurface8 Interface follow: */
197 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(IDirect3DSurface8 *iface, REFIID riid,
200 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
203 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
205 if (!This->container) return E_NOINTERFACE;
207 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
209 TRACE("(%p) : returning %p\n", This, *ppContainer);
213 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
215 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
216 struct wined3d_resource_desc wined3d_desc;
217 struct wined3d_resource *wined3d_resource;
219 TRACE("iface %p, desc %p.\n", iface, desc);
221 wined3d_mutex_lock();
222 wined3d_resource = wined3d_surface_get_resource(This->wined3d_surface);
223 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
224 wined3d_mutex_unlock();
226 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
227 desc->Type = wined3d_desc.resource_type;
228 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
229 desc->Pool = wined3d_desc.pool;
230 desc->Size = wined3d_desc.size;
231 desc->MultiSampleType = wined3d_desc.multisample_type;
232 desc->Width = wined3d_desc.width;
233 desc->Height = wined3d_desc.height;
238 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(IDirect3DSurface8 *iface,
239 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
241 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
242 struct wined3d_map_desc map_desc;
245 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
246 iface, locked_rect, wine_dbgstr_rect(rect), flags);
248 wined3d_mutex_lock();
251 D3DSURFACE_DESC desc;
252 IDirect3DSurface8_GetDesc(iface, &desc);
256 || (rect->left >= rect->right)
257 || (rect->top >= rect->bottom)
258 || (rect->right > desc.Width)
259 || (rect->bottom > desc.Height))
261 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
262 wined3d_mutex_unlock();
264 return D3DERR_INVALIDCALL;
268 hr = wined3d_surface_map(This->wined3d_surface, &map_desc, rect, flags);
269 wined3d_mutex_unlock();
271 locked_rect->Pitch = map_desc.row_pitch;
272 locked_rect->pBits = map_desc.data;
277 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(IDirect3DSurface8 *iface)
279 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
282 TRACE("iface %p.\n", iface);
284 wined3d_mutex_lock();
285 hr = wined3d_surface_unmap(This->wined3d_surface);
286 wined3d_mutex_unlock();
290 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
295 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
298 IDirect3DSurface8Impl_QueryInterface,
299 IDirect3DSurface8Impl_AddRef,
300 IDirect3DSurface8Impl_Release,
301 /* IDirect3DResource8 */
302 IDirect3DSurface8Impl_GetDevice,
303 IDirect3DSurface8Impl_SetPrivateData,
304 IDirect3DSurface8Impl_GetPrivateData,
305 IDirect3DSurface8Impl_FreePrivateData,
306 /* IDirect3DSurface8 */
307 IDirect3DSurface8Impl_GetContainer,
308 IDirect3DSurface8Impl_GetDesc,
309 IDirect3DSurface8Impl_LockRect,
310 IDirect3DSurface8Impl_UnlockRect
313 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
315 HeapFree(GetProcessHeap(), 0, parent);
318 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
320 surface_wined3d_object_destroyed,
323 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
324 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
325 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
330 surface->IDirect3DSurface8_iface.lpVtbl = &Direct3DSurface8_Vtbl;
333 /* FIXME: Check MAX bounds of MultisampleQuality. */
334 if (multisample_quality > 0)
336 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
337 multisample_quality = 0;
341 flags |= WINED3D_SURFACE_MAPPABLE;
343 flags |= WINED3D_SURFACE_DISCARD;
345 wined3d_mutex_lock();
346 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
347 level, usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
348 WINED3D_SURFACE_TYPE_OPENGL, flags, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
349 wined3d_mutex_unlock();
352 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
356 surface->parentDevice = &device->IDirect3DDevice8_iface;
357 IUnknown_AddRef(surface->parentDevice);
362 IDirect3DSurface8Impl *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
366 assert(iface->lpVtbl == &Direct3DSurface8_Vtbl);
368 return impl_from_IDirect3DSurface8(iface);