dmsynth: Dump data passed to Download method.
[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 static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
27 {
28     return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
29 }
30
31 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
32 {
33     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), out);
34
35     if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
36             || IsEqualGUID(riid, &IID_IDirect3DResource8)
37             || IsEqualGUID(riid, &IID_IUnknown))
38     {
39         IDirect3DSurface8_AddRef(iface);
40         *out = iface;
41         return S_OK;
42     }
43
44     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
45
46     *out = NULL;
47     return E_NOINTERFACE;
48 }
49
50 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
51 {
52     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
53
54     TRACE("iface %p.\n", iface);
55
56     if (surface->forwardReference)
57     {
58         /* Forward refcounting */
59         TRACE("Forwarding to %p.\n", surface->forwardReference);
60         return IUnknown_AddRef(surface->forwardReference);
61     }
62     else
63     {
64         /* No container, handle our own refcounting */
65         ULONG ref = InterlockedIncrement(&surface->refcount);
66
67         TRACE("%p increasing refcount to %u.\n", iface, ref);
68
69         if (ref == 1)
70         {
71             if (surface->parent_device)
72                 IDirect3DDevice8_AddRef(surface->parent_device);
73             wined3d_mutex_lock();
74             wined3d_surface_incref(surface->wined3d_surface);
75             wined3d_mutex_unlock();
76         }
77
78         return ref;
79     }
80 }
81
82 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
83 {
84     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
85
86     TRACE("iface %p.\n", iface);
87
88     if (surface->forwardReference)
89     {
90         /* Forward refcounting */
91         TRACE("Forwarding to %p.\n", surface->forwardReference);
92         return IUnknown_Release(surface->forwardReference);
93     }
94     else
95     {
96         /* No container, handle our own refcounting */
97         ULONG ref = InterlockedDecrement(&surface->refcount);
98
99         TRACE("%p decreasing refcount to %u.\n", iface, ref);
100
101         if (!ref)
102         {
103             IDirect3DDevice8 *parent_device = surface->parent_device;
104
105             /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
106             wined3d_mutex_lock();
107             wined3d_surface_decref(surface->wined3d_surface);
108             wined3d_mutex_unlock();
109
110             if (parent_device)
111                 IDirect3DDevice8_Release(parent_device);
112         }
113
114         return ref;
115     }
116 }
117
118 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
119 {
120     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
121
122     TRACE("iface %p, device %p.\n", iface, device);
123
124     if (surface->forwardReference)
125     {
126         IDirect3DResource8 *resource;
127         HRESULT hr;
128
129         hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
130         if (SUCCEEDED(hr))
131         {
132             hr = IDirect3DResource8_GetDevice(resource, device);
133             IDirect3DResource8_Release(resource);
134
135             TRACE("Returning device %p.\n", *device);
136         }
137
138         return hr;
139     }
140
141     *device = surface->parent_device;
142     IDirect3DDevice8_AddRef(*device);
143
144     TRACE("Returning device %p.\n", *device);
145
146     return D3D_OK;
147 }
148
149 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
150         const void *data, DWORD data_size, DWORD flags)
151 {
152     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
153     struct wined3d_resource *resource;
154     HRESULT hr;
155
156     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
157             iface, debugstr_guid(guid), data, data_size, flags);
158
159     wined3d_mutex_lock();
160     resource = wined3d_surface_get_resource(surface->wined3d_surface);
161     hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
162     wined3d_mutex_unlock();
163
164     return hr;
165 }
166
167 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
168         void *data, DWORD *data_size)
169 {
170     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
171     struct wined3d_resource *resource;
172     HRESULT hr;
173
174     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
175             iface, debugstr_guid(guid), data, data_size);
176
177     wined3d_mutex_lock();
178     resource = wined3d_surface_get_resource(surface->wined3d_surface);
179     hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
180     wined3d_mutex_unlock();
181
182     return hr;
183 }
184
185 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
186 {
187     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
188     struct wined3d_resource *resource;
189     HRESULT hr;
190
191     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
192
193     wined3d_mutex_lock();
194     resource = wined3d_surface_get_resource(surface->wined3d_surface);
195     hr = wined3d_resource_free_private_data(resource, guid);
196     wined3d_mutex_unlock();
197
198     return hr;
199 }
200
201 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
202 {
203     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
204     HRESULT hr;
205
206     TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
207
208     if (!surface->container)
209         return E_NOINTERFACE;
210
211     hr = IUnknown_QueryInterface(surface->container, riid, container);
212
213     TRACE("Returning %p.\n", *container);
214
215     return hr;
216 }
217
218 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
219 {
220     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
221     struct wined3d_resource_desc wined3d_desc;
222     struct wined3d_resource *wined3d_resource;
223
224     TRACE("iface %p, desc %p.\n", iface, desc);
225
226     wined3d_mutex_lock();
227     wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
228     wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
229     wined3d_mutex_unlock();
230
231     desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
232     desc->Type = wined3d_desc.resource_type;
233     desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
234     desc->Pool = wined3d_desc.pool;
235     desc->Size = wined3d_desc.size;
236     desc->MultiSampleType = wined3d_desc.multisample_type;
237     desc->Width = wined3d_desc.width;
238     desc->Height = wined3d_desc.height;
239
240     return D3D_OK;
241 }
242
243 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
244         D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
245 {
246     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
247     struct wined3d_map_desc map_desc;
248     HRESULT hr;
249
250     TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
251             iface, locked_rect, wine_dbgstr_rect(rect), flags);
252
253     wined3d_mutex_lock();
254     if (rect)
255     {
256         D3DSURFACE_DESC desc;
257         IDirect3DSurface8_GetDesc(iface, &desc);
258
259         if ((rect->left < 0)
260                 || (rect->top < 0)
261                 || (rect->left >= rect->right)
262                 || (rect->top >= rect->bottom)
263                 || (rect->right > desc.Width)
264                 || (rect->bottom > desc.Height))
265         {
266             WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
267             wined3d_mutex_unlock();
268
269             return D3DERR_INVALIDCALL;
270         }
271     }
272
273     hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
274     wined3d_mutex_unlock();
275
276     locked_rect->Pitch = map_desc.row_pitch;
277     locked_rect->pBits = map_desc.data;
278
279     return hr;
280 }
281
282 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
283 {
284     struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
285     HRESULT hr;
286
287     TRACE("iface %p.\n", iface);
288
289     wined3d_mutex_lock();
290     hr = wined3d_surface_unmap(surface->wined3d_surface);
291     wined3d_mutex_unlock();
292
293     switch(hr)
294     {
295         case WINEDDERR_NOTLOCKED:       return D3DERR_INVALIDCALL;
296         default:                        return hr;
297     }
298 }
299
300 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
301 {
302     /* IUnknown */
303     d3d8_surface_QueryInterface,
304     d3d8_surface_AddRef,
305     d3d8_surface_Release,
306     /* IDirect3DResource8 */
307     d3d8_surface_GetDevice,
308     d3d8_surface_SetPrivateData,
309     d3d8_surface_GetPrivateData,
310     d3d8_surface_FreePrivateData,
311     /* IDirect3DSurface8 */
312     d3d8_surface_GetContainer,
313     d3d8_surface_GetDesc,
314     d3d8_surface_LockRect,
315     d3d8_surface_UnlockRect,
316 };
317
318 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
319 {
320     HeapFree(GetProcessHeap(), 0, parent);
321 }
322
323 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
324 {
325     surface_wined3d_object_destroyed,
326 };
327
328 HRESULT surface_init(struct d3d8_surface *surface, struct d3d8_device *device, UINT width, UINT height,
329         D3DFORMAT format, BOOL lockable, BOOL discard, DWORD usage, D3DPOOL pool,
330         D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
331 {
332     DWORD flags = 0;
333     HRESULT hr;
334
335     surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
336     surface->refcount = 1;
337
338     /* FIXME: Check MAX bounds of MultisampleQuality. */
339     if (multisample_quality > 0)
340     {
341         FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
342         multisample_quality = 0;
343     }
344
345     if (lockable)
346         flags |= WINED3D_SURFACE_MAPPABLE;
347     if (discard)
348         flags |= WINED3D_SURFACE_DISCARD;
349
350     wined3d_mutex_lock();
351     hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
352             usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
353             WINED3D_SURFACE_TYPE_OPENGL, flags, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
354     wined3d_mutex_unlock();
355     if (FAILED(hr))
356     {
357         WARN("Failed to create wined3d surface, hr %#x.\n", hr);
358         return hr;
359     }
360
361     surface->parent_device = &device->IDirect3DDevice8_iface;
362     IDirect3DDevice8_AddRef(surface->parent_device);
363
364     return D3D_OK;
365 }
366
367 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
368 {
369     if (!iface)
370         return NULL;
371     assert(iface->lpVtbl == &d3d8_surface_vtbl);
372
373     return impl_from_IDirect3DSurface8(iface);
374 }