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