wbemprox: Add a partial implementation of Win32_NetworkAdapterConfiguration.
[wine.git] / dlls / d3d9 / surface.c
blobd475ae46979e1dd9a58f98eea1150970f8248380
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 if (SUCCEEDED(hr))
298 locked_rect->Pitch = map_desc.row_pitch;
299 locked_rect->pBits = map_desc.data;
302 return hr;
305 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
307 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
308 HRESULT hr;
310 TRACE("iface %p.\n", iface);
312 wined3d_mutex_lock();
313 hr = wined3d_surface_unmap(surface->wined3d_surface);
314 wined3d_mutex_unlock();
316 switch(hr)
318 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
319 default: return hr;
323 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
325 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
326 HRESULT hr;
328 TRACE("iface %p, dc %p.\n", iface, dc);
330 if (!surface->getdc_supported)
332 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
333 /* Don't touch the DC */
334 return D3DERR_INVALIDCALL;
337 wined3d_mutex_lock();
338 hr = wined3d_surface_getdc(surface->wined3d_surface, dc);
339 wined3d_mutex_unlock();
341 return hr;
344 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
346 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
347 HRESULT hr;
349 TRACE("iface %p, dc %p.\n", iface, dc);
351 wined3d_mutex_lock();
352 hr = wined3d_surface_releasedc(surface->wined3d_surface, dc);
353 wined3d_mutex_unlock();
355 switch (hr)
357 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
358 default: return hr;
362 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
364 /* IUnknown */
365 d3d9_surface_QueryInterface,
366 d3d9_surface_AddRef,
367 d3d9_surface_Release,
368 /* IDirect3DResource9 */
369 d3d9_surface_GetDevice,
370 d3d9_surface_SetPrivateData,
371 d3d9_surface_GetPrivateData,
372 d3d9_surface_FreePrivateData,
373 d3d9_surface_SetPriority,
374 d3d9_surface_GetPriority,
375 d3d9_surface_PreLoad,
376 d3d9_surface_GetType,
377 /* IDirect3DSurface9 */
378 d3d9_surface_GetContainer,
379 d3d9_surface_GetDesc,
380 d3d9_surface_LockRect,
381 d3d9_surface_UnlockRect,
382 d3d9_surface_GetDC,
383 d3d9_surface_ReleaseDC,
386 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
388 HeapFree(GetProcessHeap(), 0, parent);
391 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
393 surface_wined3d_object_destroyed,
396 HRESULT surface_init(struct d3d9_surface *surface, struct d3d9_device *device, UINT width, UINT height,
397 D3DFORMAT format, DWORD flags, DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type,
398 DWORD multisample_quality)
400 HRESULT hr;
402 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
403 surface->refcount = 1;
405 switch (format)
407 case D3DFMT_A8R8G8B8:
408 case D3DFMT_X8R8G8B8:
409 case D3DFMT_R5G6B5:
410 case D3DFMT_X1R5G5B5:
411 case D3DFMT_A1R5G5B5:
412 case D3DFMT_R8G8B8:
413 surface->getdc_supported = TRUE;
414 break;
416 default:
417 surface->getdc_supported = FALSE;
418 break;
421 /* FIXME: Check MAX bounds of MultisampleQuality. */
422 if (multisample_quality > 0)
424 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
425 multisample_quality = 0;
428 wined3d_mutex_lock();
429 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
430 usage, (enum wined3d_pool)pool, multisample_type, multisample_quality,
431 flags, surface, &d3d9_surface_wined3d_parent_ops, &surface->wined3d_surface);
432 wined3d_mutex_unlock();
433 if (FAILED(hr))
435 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
436 return hr;
439 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
440 IDirect3DDevice9Ex_AddRef(surface->parent_device);
442 return D3D_OK;
445 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
447 if (!iface)
448 return NULL;
449 assert(iface->lpVtbl == &d3d9_surface_vtbl);
451 return impl_from_IDirect3DSurface9(iface);