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