wined3d: Revert 4ba16b84a9e60ffcc98fc4b4c303c975589b59e2.
[wine] / dlls / d3d9 / surface.c
1 /*
2  * IDirect3DSurface9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DSurface9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32         || IsEqualGUID(riid, &IID_IDirect3DResource9)
33         || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
34         IDirect3DSurface9_AddRef(iface);
35         *ppobj = This;
36         return S_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     *ppobj = NULL;
41     return E_NOINTERFACE;
42 }
43
44 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
45     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
46
47     TRACE("(%p)\n", This);
48
49     if (This->forwardReference) {
50         /* Forward refcounting */
51         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
52         return IUnknown_AddRef(This->forwardReference);
53     } else {
54         /* No container, handle our own refcounting */
55         ULONG ref = InterlockedIncrement(&This->ref);
56         if (ref == 1)
57         {
58             if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
59             wined3d_mutex_lock();
60             IWineD3DSurface_AddRef(This->wineD3DSurface);
61             wined3d_mutex_unlock();
62         }
63         TRACE("(%p) : AddRef from %d\n", This, ref - 1);
64
65         return ref;
66     }
67
68 }
69
70 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
71     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
72
73     TRACE("(%p)\n", This);
74
75     if (This->forwardReference) {
76         /* Forward to the containerParent */
77         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
78         return IUnknown_Release(This->forwardReference);
79     } else {
80         /* No container, handle our own refcounting */
81         ULONG ref = InterlockedDecrement(&This->ref);
82         TRACE("(%p) : ReleaseRef to %d\n", This, ref);
83
84         if (ref == 0) {
85             if (This->parentDevice) IDirect3DDevice9Ex_Release(This->parentDevice);
86             wined3d_mutex_lock();
87             IWineD3DSurface_Release(This->wineD3DSurface);
88             wined3d_mutex_unlock();
89         }
90
91         return ref;
92     }
93 }
94
95 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
96 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
97     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
98     IWineD3DDevice *wined3d_device;
99     HRESULT hr;
100     TRACE("(%p)->(%p)\n", This, ppDevice);
101
102     wined3d_mutex_lock();
103     hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
104     if (SUCCEEDED(hr))
105     {
106         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
107         IWineD3DDevice_Release(wined3d_device);
108     }
109     wined3d_mutex_unlock();
110
111     return hr;
112 }
113
114 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
115     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
116     HRESULT hr;
117     TRACE("(%p) Relay\n", This);
118
119     wined3d_mutex_lock();
120     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
121     wined3d_mutex_unlock();
122
123     return hr;
124 }
125
126 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
127     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
128     HRESULT hr;
129     TRACE("(%p) Relay\n", This);
130
131     wined3d_mutex_lock();
132     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
133     wined3d_mutex_unlock();
134
135     return hr;
136 }
137
138 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
139     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
140     HRESULT hr;
141     TRACE("(%p) Relay\n", This);
142
143     wined3d_mutex_lock();
144     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
145     wined3d_mutex_unlock();
146
147     return hr;
148 }
149
150 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
151     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
152     HRESULT hr;
153     TRACE("(%p) Relay\n", This);
154
155     wined3d_mutex_lock();
156     hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
157     wined3d_mutex_unlock();
158
159     return hr;
160 }
161
162 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
163     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
164     HRESULT hr;
165     TRACE("(%p) Relay\n", This);
166
167     wined3d_mutex_lock();
168     hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
169     wined3d_mutex_unlock();
170
171     return hr;
172 }
173
174 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
175     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
176     TRACE("(%p) Relay\n", This);
177
178     wined3d_mutex_lock();
179     IWineD3DSurface_PreLoad(This->wineD3DSurface);
180     wined3d_mutex_unlock();
181 }
182
183 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
184     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
185     D3DRESOURCETYPE ret;
186     TRACE("(%p) Relay\n", This);
187
188     wined3d_mutex_lock();
189     ret = IWineD3DSurface_GetType(This->wineD3DSurface);
190     wined3d_mutex_unlock();
191
192     return ret;
193 }
194
195 /* IDirect3DSurface9 Interface follow: */
196 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
197     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
198     HRESULT res;
199
200     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
201
202     if (!This->container) return E_NOINTERFACE;
203
204     if (!ppContainer) {
205         ERR("Called without a valid ppContainer\n");
206     }
207
208     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
209
210     TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
211
212     return res;
213 }
214
215 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
216     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
217     WINED3DSURFACE_DESC wined3ddesc;
218     HRESULT hr;
219     TRACE("(%p) Relay\n", This);
220
221     wined3d_mutex_lock();
222     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
223     wined3d_mutex_unlock();
224
225     if (SUCCEEDED(hr))
226     {
227         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
228         pDesc->Type = wined3ddesc.resource_type;
229         pDesc->Usage = wined3ddesc.usage;
230         pDesc->Pool = wined3ddesc.pool;
231         pDesc->MultiSampleType = wined3ddesc.multisample_type;
232         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
233         pDesc->Width = wined3ddesc.width;
234         pDesc->Height = wined3ddesc.height;
235     }
236
237     return hr;
238 }
239
240 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
241     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
242     HRESULT hr;
243     TRACE("(%p) Relay\n", This);
244
245     wined3d_mutex_lock();
246     TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
247     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
248     wined3d_mutex_unlock();
249
250     return hr;
251 }
252
253 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
254     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
255     HRESULT hr;
256     TRACE("(%p) Relay\n", This);
257
258     wined3d_mutex_lock();
259     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
260     wined3d_mutex_unlock();
261
262     switch(hr)
263     {
264         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
265         default:                        return hr;
266     }
267 }
268
269 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
270     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
271     HRESULT hr;
272     TRACE("(%p) Relay\n", This);
273
274     if(!This->getdc_supported)
275     {
276         WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
277         /* Don't touch the DC */
278         return D3DERR_INVALIDCALL;
279     }
280
281     wined3d_mutex_lock();
282     hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
283     wined3d_mutex_unlock();
284
285     return hr;
286 }
287
288 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
289     IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
290     HRESULT hr;
291     TRACE("(%p) Relay\n", This);
292
293     wined3d_mutex_lock();
294     hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
295     wined3d_mutex_unlock();
296
297     switch(hr) {
298         case WINEDDERR_NODC:    return WINED3DERR_INVALIDCALL;
299         default:                return hr;
300     }
301 }
302
303 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
304 {
305     /* IUnknown */
306     IDirect3DSurface9Impl_QueryInterface,
307     IDirect3DSurface9Impl_AddRef,
308     IDirect3DSurface9Impl_Release,
309     /* IDirect3DResource9 */
310     IDirect3DSurface9Impl_GetDevice,
311     IDirect3DSurface9Impl_SetPrivateData,
312     IDirect3DSurface9Impl_GetPrivateData,
313     IDirect3DSurface9Impl_FreePrivateData,
314     IDirect3DSurface9Impl_SetPriority,
315     IDirect3DSurface9Impl_GetPriority,
316     IDirect3DSurface9Impl_PreLoad,
317     IDirect3DSurface9Impl_GetType,
318     /* IDirect3DSurface9 */
319     IDirect3DSurface9Impl_GetContainer,
320     IDirect3DSurface9Impl_GetDesc,
321     IDirect3DSurface9Impl_LockRect,
322     IDirect3DSurface9Impl_UnlockRect,
323     IDirect3DSurface9Impl_GetDC,
324     IDirect3DSurface9Impl_ReleaseDC
325 };
326
327 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
328 {
329     HeapFree(GetProcessHeap(), 0, parent);
330 }
331
332 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
333 {
334     surface_wined3d_object_destroyed,
335 };
336
337 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
338         UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
339         DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
340 {
341     HRESULT hr;
342
343     surface->lpVtbl = &Direct3DSurface9_Vtbl;
344     surface->ref = 1;
345
346     switch (format)
347     {
348         case D3DFMT_A8R8G8B8:
349         case D3DFMT_X8R8G8B8:
350         case D3DFMT_R5G6B5:
351         case D3DFMT_X1R5G5B5:
352         case D3DFMT_A1R5G5B5:
353         case D3DFMT_R8G8B8:
354             surface->getdc_supported = TRUE;
355             break;
356
357         default:
358             surface->getdc_supported = FALSE;
359             break;
360     }
361
362     /* FIXME: Check MAX bounds of MultisampleQuality. */
363     if (multisample_quality > 0)
364     {
365         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
366         multisample_quality = 0;
367     }
368
369     wined3d_mutex_lock();
370     hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
371             lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
372             multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
373             &d3d9_surface_wined3d_parent_ops);
374     wined3d_mutex_unlock();
375     if (FAILED(hr))
376     {
377         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
378         return hr;
379     }
380
381     surface->parentDevice = (IDirect3DDevice9Ex *)device;
382     IDirect3DDevice9Ex_AddRef(surface->parentDevice);
383
384     return D3D_OK;
385 }