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 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
30 if (IsEqualGUID(riid, &IID_IUnknown)
31 || IsEqualGUID(riid, &IID_IDirect3DResource8)
32 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
33 IUnknown_AddRef(iface);
38 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
43 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
44 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
46 TRACE("(%p)\n", This);
48 if (This->forwardReference) {
49 /* Forward refcounting */
50 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
51 return IUnknown_AddRef(This->forwardReference);
53 /* No container, handle our own refcounting */
54 ULONG ref = InterlockedIncrement(&This->ref);
57 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
59 IUnknown_AddRef(This->wineD3DSurface);
60 wined3d_mutex_unlock();
62 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
67 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
68 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
70 TRACE("(%p)\n", This);
72 if (This->forwardReference) {
73 /* Forward refcounting */
74 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
75 return IUnknown_Release(This->forwardReference);
77 /* No container, handle our own refcounting */
78 ULONG ref = InterlockedDecrement(&This->ref);
79 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
82 IDirect3DDevice8 *parentDevice = This->parentDevice;
84 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
86 IWineD3DSurface_Release(This->wineD3DSurface);
87 wined3d_mutex_unlock();
89 if (parentDevice) IDirect3DDevice8_Release(parentDevice);
96 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
97 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
98 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
99 IWineD3DDevice *wined3d_device;
101 TRACE("(%p)->(%p)\n", This, ppDevice);
103 wined3d_mutex_lock();
104 hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
107 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
108 IWineD3DDevice_Release(wined3d_device);
110 wined3d_mutex_unlock();
115 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
116 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
118 TRACE("(%p) Relay\n", This);
120 wined3d_mutex_lock();
121 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
122 wined3d_mutex_unlock();
127 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
128 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
130 TRACE("(%p) Relay\n", This);
132 wined3d_mutex_lock();
133 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
134 wined3d_mutex_unlock();
139 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
140 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
142 TRACE("(%p) Relay\n", This);
144 wined3d_mutex_lock();
145 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
146 wined3d_mutex_unlock();
151 /* IDirect3DSurface8 Interface follow: */
152 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
153 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
156 TRACE("(%p) Relay\n", This);
158 if (!This->container) return E_NOINTERFACE;
160 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
162 TRACE("(%p) : returning %p\n", This, *ppContainer);
166 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
167 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
168 WINED3DSURFACE_DESC wined3ddesc;
170 TRACE("(%p) Relay\n", This);
172 wined3d_mutex_lock();
173 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
174 wined3d_mutex_unlock();
178 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
179 pDesc->Type = wined3ddesc.resource_type;
180 pDesc->Usage = wined3ddesc.usage;
181 pDesc->Pool = wined3ddesc.pool;
182 pDesc->Size = wined3ddesc.size;
183 pDesc->MultiSampleType = wined3ddesc.multisample_type;
184 pDesc->Width = wined3ddesc.width;
185 pDesc->Height = wined3ddesc.height;
191 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
192 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
194 TRACE("(%p) Relay\n", This);
195 TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
197 wined3d_mutex_lock();
199 D3DSURFACE_DESC desc;
200 IDirect3DSurface8_GetDesc(iface, &desc);
202 if ((pRect->left < 0)
204 || (pRect->left >= pRect->right)
205 || (pRect->top >= pRect->bottom)
206 || (pRect->right > desc.Width)
207 || (pRect->bottom > desc.Height)) {
208 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
209 wined3d_mutex_unlock();
211 return D3DERR_INVALIDCALL;
215 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
216 wined3d_mutex_unlock();
221 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
222 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
224 TRACE("(%p) Relay\n", This);
226 wined3d_mutex_lock();
227 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
228 wined3d_mutex_unlock();
232 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
237 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
240 IDirect3DSurface8Impl_QueryInterface,
241 IDirect3DSurface8Impl_AddRef,
242 IDirect3DSurface8Impl_Release,
243 /* IDirect3DResource8 */
244 IDirect3DSurface8Impl_GetDevice,
245 IDirect3DSurface8Impl_SetPrivateData,
246 IDirect3DSurface8Impl_GetPrivateData,
247 IDirect3DSurface8Impl_FreePrivateData,
248 /* IDirect3DSurface8 */
249 IDirect3DSurface8Impl_GetContainer,
250 IDirect3DSurface8Impl_GetDesc,
251 IDirect3DSurface8Impl_LockRect,
252 IDirect3DSurface8Impl_UnlockRect
255 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
257 HeapFree(GetProcessHeap(), 0, parent);
260 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
262 surface_wined3d_object_destroyed,
265 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
266 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
267 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
271 surface->lpVtbl = &Direct3DSurface8_Vtbl;
274 /* FIXME: Check MAX bounds of MultisampleQuality. */
275 if (multisample_quality > 0)
277 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
278 multisample_quality = 0;
281 wined3d_mutex_lock();
282 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
283 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
284 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
285 &d3d8_surface_wined3d_parent_ops);
286 wined3d_mutex_unlock();
289 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
293 surface->parentDevice = (IDirect3DDevice8 *)device;
294 IUnknown_AddRef(surface->parentDevice);