msi/tests: Avoid #if 0 to make sure code still compiles.
[wine/multimedia.git] / dlls / d3d8 / surface.c
blob23e50369cbec9c1fe4522f3291a9a713242fe233
1 /*
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 "config.h"
22 #include <assert.h>
23 #include "d3d8_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
27 static inline IDirect3DSurface8Impl *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
29 return CONTAINING_RECORD(iface, IDirect3DSurface8Impl, IDirect3DSurface8_iface);
32 /* IDirect3DSurface8 IUnknown parts follow: */
33 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(IDirect3DSurface8 *iface, REFIID riid,
34 void **ppobj)
36 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
38 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
40 if (IsEqualGUID(riid, &IID_IUnknown)
41 || IsEqualGUID(riid, &IID_IDirect3DResource8)
42 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
43 IUnknown_AddRef(iface);
44 *ppobj = This;
45 return S_OK;
48 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
49 *ppobj = NULL;
50 return E_NOINTERFACE;
53 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(IDirect3DSurface8 *iface)
55 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
57 TRACE("iface %p.\n", iface);
59 if (This->forwardReference) {
60 /* Forward refcounting */
61 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
62 return IUnknown_AddRef(This->forwardReference);
63 } else {
64 /* No container, handle our own refcounting */
65 ULONG ref = InterlockedIncrement(&This->ref);
67 TRACE("%p increasing refcount to %u.\n", iface, ref);
69 if (ref == 1)
71 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
72 wined3d_mutex_lock();
73 wined3d_surface_incref(This->wined3d_surface);
74 wined3d_mutex_unlock();
77 return ref;
81 static ULONG WINAPI IDirect3DSurface8Impl_Release(IDirect3DSurface8 *iface)
83 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
85 TRACE("iface %p.\n", iface);
87 if (This->forwardReference) {
88 /* Forward refcounting */
89 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
90 return IUnknown_Release(This->forwardReference);
91 } else {
92 /* No container, handle our own refcounting */
93 ULONG ref = InterlockedDecrement(&This->ref);
95 TRACE("%p decreasing refcount to %u.\n", iface, ref);
97 if (ref == 0) {
98 IDirect3DDevice8 *parentDevice = This->parentDevice;
100 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
101 wined3d_mutex_lock();
102 wined3d_surface_decref(This->wined3d_surface);
103 wined3d_mutex_unlock();
105 if (parentDevice) IDirect3DDevice8_Release(parentDevice);
108 return ref;
112 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
113 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface,
114 IDirect3DDevice8 **device)
116 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
118 TRACE("iface %p, device %p.\n", iface, device);
120 if (This->forwardReference)
122 IDirect3DResource8 *resource;
123 HRESULT hr;
125 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
126 if (SUCCEEDED(hr))
128 hr = IDirect3DResource8_GetDevice(resource, device);
129 IDirect3DResource8_Release(resource);
131 TRACE("Returning device %p.\n", *device);
134 return hr;
137 *device = This->parentDevice;
138 IDirect3DDevice8_AddRef(*device);
140 TRACE("Returning device %p.\n", *device);
142 return D3D_OK;
145 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
146 const void *data, DWORD data_size, DWORD flags)
148 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
149 struct wined3d_resource *resource;
150 HRESULT hr;
152 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
153 iface, debugstr_guid(guid), data, data_size, flags);
155 wined3d_mutex_lock();
156 resource = wined3d_surface_get_resource(surface->wined3d_surface);
157 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
158 wined3d_mutex_unlock();
160 return hr;
163 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
164 void *data, DWORD *data_size)
166 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
167 struct wined3d_resource *resource;
168 HRESULT hr;
170 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
171 iface, debugstr_guid(guid), data, data_size);
173 wined3d_mutex_lock();
174 resource = wined3d_surface_get_resource(surface->wined3d_surface);
175 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
176 wined3d_mutex_unlock();
178 return hr;
181 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
183 IDirect3DSurface8Impl *surface = impl_from_IDirect3DSurface8(iface);
184 struct wined3d_resource *resource;
185 HRESULT hr;
187 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
189 wined3d_mutex_lock();
190 resource = wined3d_surface_get_resource(surface->wined3d_surface);
191 hr = wined3d_resource_free_private_data(resource, guid);
192 wined3d_mutex_unlock();
194 return hr;
197 /* IDirect3DSurface8 Interface follow: */
198 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(IDirect3DSurface8 *iface, REFIID riid,
199 void **ppContainer)
201 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
202 HRESULT res;
204 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
206 if (!This->container) return E_NOINTERFACE;
208 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
210 TRACE("(%p) : returning %p\n", This, *ppContainer);
211 return res;
214 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
216 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
217 struct wined3d_resource_desc wined3d_desc;
218 struct wined3d_resource *wined3d_resource;
220 TRACE("iface %p, desc %p.\n", iface, desc);
222 wined3d_mutex_lock();
223 wined3d_resource = wined3d_surface_get_resource(This->wined3d_surface);
224 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
225 wined3d_mutex_unlock();
227 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
228 desc->Type = wined3d_desc.resource_type;
229 desc->Usage = wined3d_desc.usage;
230 desc->Pool = wined3d_desc.pool;
231 desc->Size = wined3d_desc.size;
232 desc->MultiSampleType = wined3d_desc.multisample_type;
233 desc->Width = wined3d_desc.width;
234 desc->Height = wined3d_desc.height;
236 return D3D_OK;
239 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(IDirect3DSurface8 *iface,
240 D3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD Flags)
242 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
243 HRESULT hr;
245 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
247 wined3d_mutex_lock();
248 if (pRect) {
249 D3DSURFACE_DESC desc;
250 IDirect3DSurface8_GetDesc(iface, &desc);
252 if ((pRect->left < 0)
253 || (pRect->top < 0)
254 || (pRect->left >= pRect->right)
255 || (pRect->top >= pRect->bottom)
256 || (pRect->right > desc.Width)
257 || (pRect->bottom > desc.Height)) {
258 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
259 wined3d_mutex_unlock();
261 return D3DERR_INVALIDCALL;
265 hr = wined3d_surface_map(This->wined3d_surface, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
266 wined3d_mutex_unlock();
268 return hr;
271 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(IDirect3DSurface8 *iface)
273 IDirect3DSurface8Impl *This = impl_from_IDirect3DSurface8(iface);
274 HRESULT hr;
276 TRACE("iface %p.\n", iface);
278 wined3d_mutex_lock();
279 hr = wined3d_surface_unmap(This->wined3d_surface);
280 wined3d_mutex_unlock();
282 switch(hr)
284 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
285 default: return hr;
289 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
291 /* IUnknown */
292 IDirect3DSurface8Impl_QueryInterface,
293 IDirect3DSurface8Impl_AddRef,
294 IDirect3DSurface8Impl_Release,
295 /* IDirect3DResource8 */
296 IDirect3DSurface8Impl_GetDevice,
297 IDirect3DSurface8Impl_SetPrivateData,
298 IDirect3DSurface8Impl_GetPrivateData,
299 IDirect3DSurface8Impl_FreePrivateData,
300 /* IDirect3DSurface8 */
301 IDirect3DSurface8Impl_GetContainer,
302 IDirect3DSurface8Impl_GetDesc,
303 IDirect3DSurface8Impl_LockRect,
304 IDirect3DSurface8Impl_UnlockRect
307 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
309 HeapFree(GetProcessHeap(), 0, parent);
312 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
314 surface_wined3d_object_destroyed,
317 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
318 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
319 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
321 HRESULT hr;
323 surface->IDirect3DSurface8_iface.lpVtbl = &Direct3DSurface8_Vtbl;
324 surface->ref = 1;
326 /* FIXME: Check MAX bounds of MultisampleQuality. */
327 if (multisample_quality > 0)
329 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
330 multisample_quality = 0;
333 wined3d_mutex_lock();
334 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
335 lockable, discard, level, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, multisample_type,
336 multisample_quality, SURFACE_OPENGL, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
337 wined3d_mutex_unlock();
338 if (FAILED(hr))
340 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
341 return hr;
344 surface->parentDevice = &device->IDirect3DDevice8_iface;
345 IUnknown_AddRef(surface->parentDevice);
347 return D3D_OK;
350 IDirect3DSurface8Impl *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
352 if (!iface)
353 return NULL;
354 assert(iface->lpVtbl == &Direct3DSurface8_Vtbl);
356 return impl_from_IDirect3DSurface8(iface);