dsound: Merge the DirectSoundCapture create functions.
[wine/multimedia.git] / dlls / d3d9 / surface.c
blob7213a32dfaf21a48a4e10486166158daa95ce968
1 /*
2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
29 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
32 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
36 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
37 || IsEqualGUID(riid, &IID_IDirect3DResource9)
38 || IsEqualGUID(riid, &IID_IUnknown))
40 IDirect3DSurface9_AddRef(iface);
41 *out = iface;
42 return S_OK;
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
47 *out = NULL;
48 return E_NOINTERFACE;
51 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
53 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
54 ULONG refcount;
56 TRACE("iface %p.\n", iface);
58 if (surface->forwardReference)
60 TRACE("Forwarding to %p.\n", surface->forwardReference);
61 return IUnknown_AddRef(surface->forwardReference);
64 refcount = InterlockedIncrement(&surface->refcount);
65 TRACE("%p increasing refcount to %u.\n", iface, refcount);
67 if (refcount == 1)
69 if (surface->parent_device)
70 IDirect3DDevice9Ex_AddRef(surface->parent_device);
71 wined3d_mutex_lock();
72 wined3d_surface_incref(surface->wined3d_surface);
73 wined3d_mutex_unlock();
76 return refcount;
79 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
81 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
82 ULONG refcount;
84 TRACE("iface %p.\n", iface);
86 if (surface->forwardReference)
88 TRACE("Forwarding to %p.\n", surface->forwardReference);
89 return IUnknown_Release(surface->forwardReference);
92 refcount = InterlockedDecrement(&surface->refcount);
93 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
95 if (!refcount)
97 IDirect3DDevice9Ex *parent_device = surface->parent_device;
99 wined3d_mutex_lock();
100 wined3d_surface_decref(surface->wined3d_surface);
101 wined3d_mutex_unlock();
103 /* Release the device last, as it may cause the device to be destroyed. */
104 if (parent_device)
105 IDirect3DDevice9Ex_Release(parent_device);
108 return refcount;
111 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
113 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
115 TRACE("iface %p, device %p.\n", iface, device);
117 if (surface->forwardReference)
119 IDirect3DResource9 *resource;
120 HRESULT hr;
122 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
123 if (SUCCEEDED(hr))
125 hr = IDirect3DResource9_GetDevice(resource, device);
126 IDirect3DResource9_Release(resource);
128 TRACE("Returning device %p.\n", *device);
131 return hr;
134 *device = (IDirect3DDevice9 *)surface->parent_device;
135 IDirect3DDevice9_AddRef(*device);
137 TRACE("Returning device %p.\n", *device);
139 return D3D_OK;
142 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
143 const void *data, DWORD data_size, DWORD flags)
145 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
146 struct wined3d_resource *resource;
147 HRESULT hr;
149 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
150 iface, debugstr_guid(guid), data, data_size, flags);
152 wined3d_mutex_lock();
153 resource = wined3d_surface_get_resource(surface->wined3d_surface);
154 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
155 wined3d_mutex_unlock();
157 return hr;
160 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
161 void *data, DWORD *data_size)
163 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
164 struct wined3d_resource *resource;
165 HRESULT hr;
167 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
168 iface, debugstr_guid(guid), data, data_size);
170 wined3d_mutex_lock();
171 resource = wined3d_surface_get_resource(surface->wined3d_surface);
172 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
173 wined3d_mutex_unlock();
175 return hr;
178 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
180 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
181 struct wined3d_resource *resource;
182 HRESULT hr;
184 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
186 wined3d_mutex_lock();
187 resource = wined3d_surface_get_resource(surface->wined3d_surface);
188 hr = wined3d_resource_free_private_data(resource, guid);
189 wined3d_mutex_unlock();
191 return hr;
194 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
196 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
197 DWORD ret;
199 TRACE("iface %p, priority %u.\n", iface, priority);
201 wined3d_mutex_lock();
202 ret = wined3d_surface_set_priority(surface->wined3d_surface, priority);
203 wined3d_mutex_unlock();
205 return ret;
208 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
210 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
211 DWORD ret;
213 TRACE("iface %p.\n", iface);
215 wined3d_mutex_lock();
216 ret = wined3d_surface_get_priority(surface->wined3d_surface);
217 wined3d_mutex_unlock();
219 return ret;
222 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
224 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
226 TRACE("iface %p.\n", iface);
228 wined3d_mutex_lock();
229 wined3d_surface_preload(surface->wined3d_surface);
230 wined3d_mutex_unlock();
233 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
235 TRACE("iface %p.\n", iface);
237 return D3DRTYPE_SURFACE;
240 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
242 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
243 HRESULT hr;
245 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
247 if (!surface->container)
248 return E_NOINTERFACE;
250 hr = IUnknown_QueryInterface(surface->container, riid, container);
252 TRACE("Returning %p.\n", *container);
254 return hr;
257 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
259 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
260 struct wined3d_resource_desc wined3d_desc;
261 struct wined3d_resource *wined3d_resource;
263 TRACE("iface %p, desc %p.\n", iface, desc);
265 wined3d_mutex_lock();
266 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
267 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
268 wined3d_mutex_unlock();
270 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
271 desc->Type = wined3d_desc.resource_type;
272 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
273 desc->Pool = wined3d_desc.pool;
274 desc->MultiSampleType = wined3d_desc.multisample_type;
275 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
276 desc->Width = wined3d_desc.width;
277 desc->Height = wined3d_desc.height;
279 return D3D_OK;
282 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
283 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
285 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
286 struct wined3d_map_desc map_desc;
287 HRESULT hr;
289 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
290 iface, locked_rect, wine_dbgstr_rect(rect), flags);
292 wined3d_mutex_lock();
293 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
294 wined3d_mutex_unlock();
296 locked_rect->Pitch = map_desc.row_pitch;
297 locked_rect->pBits = map_desc.data;
299 return hr;
302 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
304 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
305 HRESULT hr;
307 TRACE("iface %p.\n", iface);
309 wined3d_mutex_lock();
310 hr = wined3d_surface_unmap(surface->wined3d_surface);
311 wined3d_mutex_unlock();
313 switch(hr)
315 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
316 default: return hr;
320 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
322 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
323 HRESULT hr;
325 TRACE("iface %p, dc %p.\n", iface, dc);
327 if (!surface->getdc_supported)
329 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
330 /* Don't touch the DC */
331 return D3DERR_INVALIDCALL;
334 wined3d_mutex_lock();
335 hr = wined3d_surface_getdc(surface->wined3d_surface, dc);
336 wined3d_mutex_unlock();
338 return hr;
341 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
343 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
344 HRESULT hr;
346 TRACE("iface %p, dc %p.\n", iface, dc);
348 wined3d_mutex_lock();
349 hr = wined3d_surface_releasedc(surface->wined3d_surface, dc);
350 wined3d_mutex_unlock();
352 switch (hr)
354 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
355 default: return hr;
359 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
361 /* IUnknown */
362 d3d9_surface_QueryInterface,
363 d3d9_surface_AddRef,
364 d3d9_surface_Release,
365 /* IDirect3DResource9 */
366 d3d9_surface_GetDevice,
367 d3d9_surface_SetPrivateData,
368 d3d9_surface_GetPrivateData,
369 d3d9_surface_FreePrivateData,
370 d3d9_surface_SetPriority,
371 d3d9_surface_GetPriority,
372 d3d9_surface_PreLoad,
373 d3d9_surface_GetType,
374 /* IDirect3DSurface9 */
375 d3d9_surface_GetContainer,
376 d3d9_surface_GetDesc,
377 d3d9_surface_LockRect,
378 d3d9_surface_UnlockRect,
379 d3d9_surface_GetDC,
380 d3d9_surface_ReleaseDC,
383 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
385 HeapFree(GetProcessHeap(), 0, parent);
388 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
390 surface_wined3d_object_destroyed,
393 HRESULT surface_init(struct d3d9_surface *surface, struct d3d9_device *device,
394 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
395 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
397 DWORD flags = 0;
398 HRESULT hr;
400 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
401 surface->refcount = 1;
403 switch (format)
405 case D3DFMT_A8R8G8B8:
406 case D3DFMT_X8R8G8B8:
407 case D3DFMT_R5G6B5:
408 case D3DFMT_X1R5G5B5:
409 case D3DFMT_A1R5G5B5:
410 case D3DFMT_R8G8B8:
411 surface->getdc_supported = TRUE;
412 break;
414 default:
415 surface->getdc_supported = FALSE;
416 break;
419 /* FIXME: Check MAX bounds of MultisampleQuality. */
420 if (multisample_quality > 0)
422 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
423 multisample_quality = 0;
426 if (lockable)
427 flags |= WINED3D_SURFACE_MAPPABLE;
428 if (discard)
429 flags |= WINED3D_SURFACE_DISCARD;
431 wined3d_mutex_lock();
432 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
433 level, usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
434 WINED3D_SURFACE_TYPE_OPENGL, flags, surface, &d3d9_surface_wined3d_parent_ops, &surface->wined3d_surface);
435 wined3d_mutex_unlock();
436 if (FAILED(hr))
438 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
439 return hr;
442 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
443 IDirect3DDevice9Ex_AddRef(surface->parent_device);
445 return D3D_OK;
448 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
450 if (!iface)
451 return NULL;
452 assert(iface->lpVtbl == &d3d9_surface_vtbl);
454 return impl_from_IDirect3DSurface9(iface);