jscript: Added Object function invocation implementation.
[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             if (This->parentDevice) IUnknown_Release(This->parentDevice);
83             /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
84             wined3d_mutex_lock();
85             IWineD3DSurface_Release(This->wineD3DSurface);
86             wined3d_mutex_unlock();
87         }
88
89         return ref;
90     }
91 }
92
93 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
94 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
95     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
96     IWineD3DDevice *wined3d_device;
97     HRESULT hr;
98     TRACE("(%p)->(%p)\n", This, ppDevice);
99
100     wined3d_mutex_lock();
101     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
102     if (SUCCEEDED(hr))
103     {
104         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
105         IWineD3DDevice_Release(wined3d_device);
106     }
107     wined3d_mutex_unlock();
108
109     return hr;
110 }
111
112 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
113     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
114     HRESULT hr;
115     TRACE("(%p) Relay\n", This);
116
117     wined3d_mutex_lock();
118     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
119     wined3d_mutex_unlock();
120
121     return hr;
122 }
123
124 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
125     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
126     HRESULT hr;
127     TRACE("(%p) Relay\n", This);
128
129     wined3d_mutex_lock();
130     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
131     wined3d_mutex_unlock();
132
133     return hr;
134 }
135
136 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
137     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
138     HRESULT hr;
139     TRACE("(%p) Relay\n", This);
140
141     wined3d_mutex_lock();
142     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
143     wined3d_mutex_unlock();
144
145     return hr;
146 }
147
148 /* IDirect3DSurface8 Interface follow: */
149 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
150     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
151     HRESULT res;
152
153     TRACE("(%p) Relay\n", This);
154
155     if (!This->container) return E_NOINTERFACE;
156
157     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
158
159     TRACE("(%p) : returning %p\n", This, *ppContainer);
160     return res;
161 }
162
163 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
164     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
165     WINED3DSURFACE_DESC    wined3ddesc;
166     HRESULT hr;
167     TRACE("(%p) Relay\n", This);
168
169     wined3d_mutex_lock();
170     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
171     wined3d_mutex_unlock();
172
173     if (SUCCEEDED(hr))
174     {
175         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
176         pDesc->Type = wined3ddesc.resource_type;
177         pDesc->Usage = wined3ddesc.usage;
178         pDesc->Pool = wined3ddesc.pool;
179         pDesc->Size = wined3ddesc.size;
180         pDesc->MultiSampleType = wined3ddesc.multisample_type;
181         pDesc->Width = wined3ddesc.width;
182         pDesc->Height = wined3ddesc.height;
183     }
184
185     return hr;
186 }
187
188 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
189     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
190     HRESULT hr;
191     TRACE("(%p) Relay\n", This);
192     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
193
194     wined3d_mutex_lock();
195     if (pRect) {
196         D3DSURFACE_DESC desc;
197         IDirect3DSurface8_GetDesc(iface, &desc);
198
199         if ((pRect->left < 0)
200                 || (pRect->top < 0)
201                 || (pRect->left >= pRect->right)
202                 || (pRect->top >= pRect->bottom)
203                 || (pRect->right > desc.Width)
204                 || (pRect->bottom > desc.Height)) {
205             WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
206             wined3d_mutex_unlock();
207
208             return D3DERR_INVALIDCALL;
209         }
210     }
211
212     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
213     wined3d_mutex_unlock();
214
215     return hr;
216 }
217
218 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
219     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
220     HRESULT hr;
221     TRACE("(%p) Relay\n", This);
222
223     wined3d_mutex_lock();
224     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
225     wined3d_mutex_unlock();
226
227     switch(hr)
228     {
229         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
230         default:                        return hr;
231     }
232 }
233
234 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
235 {
236     /* IUnknown */
237     IDirect3DSurface8Impl_QueryInterface,
238     IDirect3DSurface8Impl_AddRef,
239     IDirect3DSurface8Impl_Release,
240     /* IDirect3DResource8 */
241     IDirect3DSurface8Impl_GetDevice,
242     IDirect3DSurface8Impl_SetPrivateData,
243     IDirect3DSurface8Impl_GetPrivateData,
244     IDirect3DSurface8Impl_FreePrivateData,
245     /* IDirect3DSurface8 */
246     IDirect3DSurface8Impl_GetContainer,
247     IDirect3DSurface8Impl_GetDesc,
248     IDirect3DSurface8Impl_LockRect,
249     IDirect3DSurface8Impl_UnlockRect
250 };
251
252 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
253 {
254     HeapFree(GetProcessHeap(), 0, parent);
255 }
256
257 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
258 {
259     surface_wined3d_object_destroyed,
260 };
261
262 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
263         UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
264         DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
265 {
266     HRESULT hr;
267
268     surface->lpVtbl = &Direct3DSurface8_Vtbl;
269     surface->ref = 1;
270
271     /* FIXME: Check MAX bounds of MultisampleQuality. */
272     if (multisample_quality > 0)
273     {
274         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
275         multisample_quality = 0;
276     }
277
278     wined3d_mutex_lock();
279     hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
280             lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
281             multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
282             &d3d8_surface_wined3d_parent_ops);
283     wined3d_mutex_unlock();
284     if (FAILED(hr))
285     {
286         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
287         return hr;
288     }
289
290     surface->parentDevice = (IDirect3DDevice8 *)device;
291     IUnknown_AddRef(surface->parentDevice);
292
293     return D3D_OK;
294 }