fltlib: Add a stub dll.
[wine] / dlls / d3d8 / surface.c
1 /*
2  * IDirect3DSurface8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
29
30     if (IsEqualGUID(riid, &IID_IUnknown)
31         || IsEqualGUID(riid, &IID_IDirect3DResource8)
32         || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
33         IUnknown_AddRef(iface);
34         *ppobj = This;
35         return S_OK;
36     }
37
38     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39     *ppobj = NULL;
40     return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
44     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
45
46     TRACE("(%p)\n", This);
47
48     if (This->forwardReference) {
49         /* Forward refcounting */
50         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
51         return IUnknown_AddRef(This->forwardReference);
52     } else {
53         /* No container, handle our own refcounting */
54         ULONG ref = InterlockedIncrement(&This->ref);
55         if (ref == 1)
56         {
57             if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
58             wined3d_mutex_lock();
59             IUnknown_AddRef(This->wineD3DSurface);
60             wined3d_mutex_unlock();
61         }
62         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
63         return ref;
64     }
65 }
66
67 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
68     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
69
70     TRACE("(%p)\n", This);
71
72     if (This->forwardReference) {
73         /* Forward refcounting */
74         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
75         return IUnknown_Release(This->forwardReference);
76     } else {
77         /* No container, handle our own refcounting */
78         ULONG ref = InterlockedDecrement(&This->ref);
79         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
80
81         if (ref == 0) {
82             IDirect3DDevice8 *parentDevice = This->parentDevice;
83
84             /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
85             wined3d_mutex_lock();
86             IWineD3DSurface_Release(This->wineD3DSurface);
87             wined3d_mutex_unlock();
88
89             if (parentDevice) IDirect3DDevice8_Release(parentDevice);
90         }
91
92         return ref;
93     }
94 }
95
96 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
97 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
98     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
99     IWineD3DDevice *wined3d_device;
100     HRESULT hr;
101     TRACE("(%p)->(%p)\n", This, ppDevice);
102
103     wined3d_mutex_lock();
104     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
105     if (SUCCEEDED(hr))
106     {
107         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
108         IWineD3DDevice_Release(wined3d_device);
109     }
110     wined3d_mutex_unlock();
111
112     return hr;
113 }
114
115 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
116     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
117     HRESULT hr;
118     TRACE("(%p) Relay\n", This);
119
120     wined3d_mutex_lock();
121     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
122     wined3d_mutex_unlock();
123
124     return hr;
125 }
126
127 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
128     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
129     HRESULT hr;
130     TRACE("(%p) Relay\n", This);
131
132     wined3d_mutex_lock();
133     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
134     wined3d_mutex_unlock();
135
136     return hr;
137 }
138
139 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
140     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
141     HRESULT hr;
142     TRACE("(%p) Relay\n", This);
143
144     wined3d_mutex_lock();
145     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
146     wined3d_mutex_unlock();
147
148     return hr;
149 }
150
151 /* IDirect3DSurface8 Interface follow: */
152 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
153     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
154     HRESULT res;
155
156     TRACE("(%p) Relay\n", This);
157
158     if (!This->container) return E_NOINTERFACE;
159
160     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
161
162     TRACE("(%p) : returning %p\n", This, *ppContainer);
163     return res;
164 }
165
166 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
167     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
168     WINED3DSURFACE_DESC    wined3ddesc;
169     HRESULT hr;
170     TRACE("(%p) Relay\n", This);
171
172     wined3d_mutex_lock();
173     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
174     wined3d_mutex_unlock();
175
176     if (SUCCEEDED(hr))
177     {
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;
186     }
187
188     return hr;
189 }
190
191 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
192     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
193     HRESULT hr;
194     TRACE("(%p) Relay\n", This);
195     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
196
197     wined3d_mutex_lock();
198     if (pRect) {
199         D3DSURFACE_DESC desc;
200         IDirect3DSurface8_GetDesc(iface, &desc);
201
202         if ((pRect->left < 0)
203                 || (pRect->top < 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();
210
211             return D3DERR_INVALIDCALL;
212         }
213     }
214
215     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
216     wined3d_mutex_unlock();
217
218     return hr;
219 }
220
221 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
222     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
223     HRESULT hr;
224     TRACE("(%p) Relay\n", This);
225
226     wined3d_mutex_lock();
227     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
228     wined3d_mutex_unlock();
229
230     switch(hr)
231     {
232         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
233         default:                        return hr;
234     }
235 }
236
237 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
238 {
239     /* IUnknown */
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
253 };
254
255 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
256 {
257     HeapFree(GetProcessHeap(), 0, parent);
258 }
259
260 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
261 {
262     surface_wined3d_object_destroyed,
263 };
264
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)
268 {
269     HRESULT hr;
270
271     surface->lpVtbl = &Direct3DSurface8_Vtbl;
272     surface->ref = 1;
273
274     /* FIXME: Check MAX bounds of MultisampleQuality. */
275     if (multisample_quality > 0)
276     {
277         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
278         multisample_quality = 0;
279     }
280
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();
287     if (FAILED(hr))
288     {
289         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
290         return hr;
291     }
292
293     surface->parentDevice = (IDirect3DDevice8 *)device;
294     IUnknown_AddRef(surface->parentDevice);
295
296     return D3D_OK;
297 }