gdi32: Make gdi.exe into a stand-alone 16-bit module.
[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     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource8)
34         || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
35         IUnknown_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
46     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
47
48     TRACE("iface %p.\n", iface);
49
50     if (This->forwardReference) {
51         /* Forward refcounting */
52         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
53         return IUnknown_AddRef(This->forwardReference);
54     } else {
55         /* No container, handle our own refcounting */
56         ULONG ref = InterlockedIncrement(&This->ref);
57
58         TRACE("%p increasing refcount to %u.\n", iface, ref);
59
60         if (ref == 1)
61         {
62             if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
63             wined3d_mutex_lock();
64             IUnknown_AddRef(This->wineD3DSurface);
65             wined3d_mutex_unlock();
66         }
67
68         return ref;
69     }
70 }
71
72 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
73     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
74
75     TRACE("iface %p.\n", iface);
76
77     if (This->forwardReference) {
78         /* Forward refcounting */
79         TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
80         return IUnknown_Release(This->forwardReference);
81     } else {
82         /* No container, handle our own refcounting */
83         ULONG ref = InterlockedDecrement(&This->ref);
84
85         TRACE("%p decreasing refcount to %u.\n", iface, ref);
86
87         if (ref == 0) {
88             IDirect3DDevice8 *parentDevice = This->parentDevice;
89
90             /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
91             wined3d_mutex_lock();
92             IWineD3DSurface_Release(This->wineD3DSurface);
93             wined3d_mutex_unlock();
94
95             if (parentDevice) IDirect3DDevice8_Release(parentDevice);
96         }
97
98         return ref;
99     }
100 }
101
102 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
103 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
104 {
105     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
106
107     TRACE("iface %p, device %p.\n", iface, device);
108
109     *device = (IDirect3DDevice8 *)This->parentDevice;
110     IDirect3DDevice8_AddRef(*device);
111
112     TRACE("Returning device %p.\n", *device);
113
114     return D3D_OK;
115 }
116
117 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
118     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
119     HRESULT hr;
120
121     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
122             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
123
124     wined3d_mutex_lock();
125     hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
126     wined3d_mutex_unlock();
127
128     return hr;
129 }
130
131 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
132     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
133     HRESULT hr;
134
135     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
136             iface, debugstr_guid(refguid), pData, pSizeOfData);
137
138     wined3d_mutex_lock();
139     hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
140     wined3d_mutex_unlock();
141
142     return hr;
143 }
144
145 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
146     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
147     HRESULT hr;
148
149     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
150
151     wined3d_mutex_lock();
152     hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
153     wined3d_mutex_unlock();
154
155     return hr;
156 }
157
158 /* IDirect3DSurface8 Interface follow: */
159 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
160     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
161     HRESULT res;
162
163     TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
164
165     if (!This->container) return E_NOINTERFACE;
166
167     res = IUnknown_QueryInterface(This->container, riid, ppContainer);
168
169     TRACE("(%p) : returning %p\n", This, *ppContainer);
170     return res;
171 }
172
173 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
174     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
175     WINED3DSURFACE_DESC    wined3ddesc;
176     HRESULT hr;
177
178     TRACE("iface %p, desc %p.\n", iface, pDesc);
179
180     wined3d_mutex_lock();
181     hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
182     wined3d_mutex_unlock();
183
184     if (SUCCEEDED(hr))
185     {
186         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
187         pDesc->Type = wined3ddesc.resource_type;
188         pDesc->Usage = wined3ddesc.usage;
189         pDesc->Pool = wined3ddesc.pool;
190         pDesc->Size = wined3ddesc.size;
191         pDesc->MultiSampleType = wined3ddesc.multisample_type;
192         pDesc->Width = wined3ddesc.width;
193         pDesc->Height = wined3ddesc.height;
194     }
195
196     return hr;
197 }
198
199 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
200     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
201     HRESULT hr;
202
203     TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
204
205     wined3d_mutex_lock();
206     if (pRect) {
207         D3DSURFACE_DESC desc;
208         IDirect3DSurface8_GetDesc(iface, &desc);
209
210         if ((pRect->left < 0)
211                 || (pRect->top < 0)
212                 || (pRect->left >= pRect->right)
213                 || (pRect->top >= pRect->bottom)
214                 || (pRect->right > desc.Width)
215                 || (pRect->bottom > desc.Height)) {
216             WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
217             wined3d_mutex_unlock();
218
219             return D3DERR_INVALIDCALL;
220         }
221     }
222
223     hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
224     wined3d_mutex_unlock();
225
226     return hr;
227 }
228
229 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
230     IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
231     HRESULT hr;
232
233     TRACE("iface %p.\n", iface);
234
235     wined3d_mutex_lock();
236     hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
237     wined3d_mutex_unlock();
238
239     switch(hr)
240     {
241         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
242         default:                        return hr;
243     }
244 }
245
246 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
247 {
248     /* IUnknown */
249     IDirect3DSurface8Impl_QueryInterface,
250     IDirect3DSurface8Impl_AddRef,
251     IDirect3DSurface8Impl_Release,
252     /* IDirect3DResource8 */
253     IDirect3DSurface8Impl_GetDevice,
254     IDirect3DSurface8Impl_SetPrivateData,
255     IDirect3DSurface8Impl_GetPrivateData,
256     IDirect3DSurface8Impl_FreePrivateData,
257     /* IDirect3DSurface8 */
258     IDirect3DSurface8Impl_GetContainer,
259     IDirect3DSurface8Impl_GetDesc,
260     IDirect3DSurface8Impl_LockRect,
261     IDirect3DSurface8Impl_UnlockRect
262 };
263
264 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
265 {
266     HeapFree(GetProcessHeap(), 0, parent);
267 }
268
269 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
270 {
271     surface_wined3d_object_destroyed,
272 };
273
274 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
275         UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
276         DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
277 {
278     HRESULT hr;
279
280     surface->lpVtbl = &Direct3DSurface8_Vtbl;
281     surface->ref = 1;
282
283     /* FIXME: Check MAX bounds of MultisampleQuality. */
284     if (multisample_quality > 0)
285     {
286         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
287         multisample_quality = 0;
288     }
289
290     wined3d_mutex_lock();
291     hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
292             lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
293             multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
294             &d3d8_surface_wined3d_parent_ops);
295     wined3d_mutex_unlock();
296     if (FAILED(hr))
297     {
298         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
299         return hr;
300     }
301
302     surface->parentDevice = (IDirect3DDevice8 *)device;
303     IUnknown_AddRef(surface->parentDevice);
304
305     return D3D_OK;
306 }