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