d3d9/tests: Use a separate device for test_vshader_input().
[wine/wine-gecko.git] / dlls / d3d9 / surface.c
blobbf8b41a197814d603a5452792c4e4dffa3fa7f56
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->resource.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->resource.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 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
147 iface, debugstr_guid(guid), data, data_size, flags);
149 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
152 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
153 void *data, DWORD *data_size)
155 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
156 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
157 iface, debugstr_guid(guid), data, data_size);
159 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
162 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
164 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
165 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
167 return d3d9_resource_free_private_data(&surface->resource, guid);
170 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
172 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
173 DWORD ret;
175 TRACE("iface %p, priority %u.\n", iface, priority);
177 wined3d_mutex_lock();
178 ret = wined3d_surface_set_priority(surface->wined3d_surface, priority);
179 wined3d_mutex_unlock();
181 return ret;
184 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
186 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
187 DWORD ret;
189 TRACE("iface %p.\n", iface);
191 wined3d_mutex_lock();
192 ret = wined3d_surface_get_priority(surface->wined3d_surface);
193 wined3d_mutex_unlock();
195 return ret;
198 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
200 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
202 TRACE("iface %p.\n", iface);
204 wined3d_mutex_lock();
205 wined3d_surface_preload(surface->wined3d_surface);
206 wined3d_mutex_unlock();
209 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
211 TRACE("iface %p.\n", iface);
213 return D3DRTYPE_SURFACE;
216 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
218 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
219 HRESULT hr;
221 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
223 if (!surface->container)
224 return E_NOINTERFACE;
226 hr = IUnknown_QueryInterface(surface->container, riid, container);
228 TRACE("Returning %p.\n", *container);
230 return hr;
233 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
235 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
236 struct wined3d_resource_desc wined3d_desc;
237 struct wined3d_resource *wined3d_resource;
239 TRACE("iface %p, desc %p.\n", iface, desc);
241 wined3d_mutex_lock();
242 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
243 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
244 wined3d_mutex_unlock();
246 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
247 desc->Type = wined3d_desc.resource_type;
248 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
249 desc->Pool = wined3d_desc.pool;
250 desc->MultiSampleType = wined3d_desc.multisample_type;
251 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
252 desc->Width = wined3d_desc.width;
253 desc->Height = wined3d_desc.height;
255 return D3D_OK;
258 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
259 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
261 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
262 struct wined3d_map_desc map_desc;
263 HRESULT hr;
265 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
266 iface, locked_rect, wine_dbgstr_rect(rect), flags);
268 wined3d_mutex_lock();
269 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
270 wined3d_mutex_unlock();
272 if (SUCCEEDED(hr))
274 locked_rect->Pitch = map_desc.row_pitch;
275 locked_rect->pBits = map_desc.data;
278 return hr;
281 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
283 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
284 HRESULT hr;
286 TRACE("iface %p.\n", iface);
288 wined3d_mutex_lock();
289 hr = wined3d_surface_unmap(surface->wined3d_surface);
290 wined3d_mutex_unlock();
292 switch(hr)
294 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
295 default: return hr;
299 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
301 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
302 HRESULT hr;
304 TRACE("iface %p, dc %p.\n", iface, dc);
306 if (!surface->getdc_supported)
308 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
309 /* Don't touch the DC */
310 return D3DERR_INVALIDCALL;
313 wined3d_mutex_lock();
314 hr = wined3d_surface_getdc(surface->wined3d_surface, dc);
315 wined3d_mutex_unlock();
317 return hr;
320 static HRESULT WINAPI d3d9_surface_ReleaseDC(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 wined3d_mutex_lock();
328 hr = wined3d_surface_releasedc(surface->wined3d_surface, dc);
329 wined3d_mutex_unlock();
331 switch (hr)
333 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
334 default: return hr;
338 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
340 /* IUnknown */
341 d3d9_surface_QueryInterface,
342 d3d9_surface_AddRef,
343 d3d9_surface_Release,
344 /* IDirect3DResource9 */
345 d3d9_surface_GetDevice,
346 d3d9_surface_SetPrivateData,
347 d3d9_surface_GetPrivateData,
348 d3d9_surface_FreePrivateData,
349 d3d9_surface_SetPriority,
350 d3d9_surface_GetPriority,
351 d3d9_surface_PreLoad,
352 d3d9_surface_GetType,
353 /* IDirect3DSurface9 */
354 d3d9_surface_GetContainer,
355 d3d9_surface_GetDesc,
356 d3d9_surface_LockRect,
357 d3d9_surface_UnlockRect,
358 d3d9_surface_GetDC,
359 d3d9_surface_ReleaseDC,
362 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
364 struct d3d9_surface *surface = parent;
365 d3d9_resource_cleanup(&surface->resource);
366 HeapFree(GetProcessHeap(), 0, surface);
369 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
371 surface_wined3d_object_destroyed,
374 void surface_init(struct d3d9_surface *surface, struct wined3d_surface *wined3d_surface,
375 struct d3d9_device *device, const struct wined3d_parent_ops **parent_ops)
377 struct wined3d_resource_desc desc;
379 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
380 d3d9_resource_init(&surface->resource);
382 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &desc);
383 switch (d3dformat_from_wined3dformat(desc.format))
385 case D3DFMT_A8R8G8B8:
386 case D3DFMT_X8R8G8B8:
387 case D3DFMT_R5G6B5:
388 case D3DFMT_X1R5G5B5:
389 case D3DFMT_A1R5G5B5:
390 case D3DFMT_R8G8B8:
391 surface->getdc_supported = TRUE;
392 break;
394 default:
395 surface->getdc_supported = FALSE;
396 break;
399 wined3d_surface_incref(wined3d_surface);
400 surface->wined3d_surface = wined3d_surface;
401 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
402 IDirect3DDevice9Ex_AddRef(surface->parent_device);
404 *parent_ops = &d3d9_surface_wined3d_parent_ops;
407 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
409 if (!iface)
410 return NULL;
411 assert(iface->lpVtbl == &d3d9_surface_vtbl);
413 return impl_from_IDirect3DSurface9(iface);