2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
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
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline IDirect3DSurface9Impl *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
29 return CONTAINING_RECORD(iface, IDirect3DSurface9Impl, lpVtbl);
32 /* IDirect3DSurface9 IUnknown parts follow: */
33 static HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
34 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
36 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
38 if (IsEqualGUID(riid, &IID_IUnknown)
39 || IsEqualGUID(riid, &IID_IDirect3DResource9)
40 || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
41 IDirect3DSurface9_AddRef(iface);
46 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
51 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
52 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
54 TRACE("iface %p.\n", iface);
56 if (This->forwardReference) {
57 /* Forward refcounting */
58 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
59 return IUnknown_AddRef(This->forwardReference);
61 /* No container, handle our own refcounting */
62 ULONG ref = InterlockedIncrement(&This->ref);
64 TRACE("%p increasing refcount to %u.\n", iface, ref);
68 if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
70 wined3d_surface_incref(This->wined3d_surface);
71 wined3d_mutex_unlock();
79 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
80 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
82 TRACE("iface %p.\n", iface);
84 if (This->forwardReference) {
85 /* Forward to the containerParent */
86 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
87 return IUnknown_Release(This->forwardReference);
89 /* No container, handle our own refcounting */
90 ULONG ref = InterlockedDecrement(&This->ref);
92 TRACE("%p decreasing refcount to %u.\n", iface, ref);
95 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
98 wined3d_surface_decref(This->wined3d_surface);
99 wined3d_mutex_unlock();
101 /* Release the device last, as it may cause the device to be destroyed. */
102 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
109 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
110 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
112 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
114 TRACE("iface %p, device %p.\n", iface, device);
116 if (This->forwardReference)
118 IDirect3DResource9 *resource;
121 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
124 hr = IDirect3DResource9_GetDevice(resource, device);
125 IDirect3DResource9_Release(resource);
127 TRACE("Returning device %p.\n", *device);
133 *device = (IDirect3DDevice9 *)This->parentDevice;
134 IDirect3DDevice9_AddRef(*device);
136 TRACE("Returning device %p.\n", *device);
141 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
142 const void *data, DWORD data_size, DWORD flags)
144 IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
145 struct wined3d_resource *resource;
148 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
149 iface, debugstr_guid(guid), data, data_size, flags);
151 wined3d_mutex_lock();
152 resource = wined3d_surface_get_resource(surface->wined3d_surface);
153 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
154 wined3d_mutex_unlock();
159 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
160 void *data, DWORD *data_size)
162 IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
163 struct wined3d_resource *resource;
166 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
167 iface, debugstr_guid(guid), data, data_size);
169 wined3d_mutex_lock();
170 resource = wined3d_surface_get_resource(surface->wined3d_surface);
171 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
172 wined3d_mutex_unlock();
177 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
179 IDirect3DSurface9Impl *surface = impl_from_IDirect3DSurface9(iface);
180 struct wined3d_resource *resource;
183 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
185 wined3d_mutex_lock();
186 resource = wined3d_surface_get_resource(surface->wined3d_surface);
187 hr = wined3d_resource_free_private_data(resource, guid);
188 wined3d_mutex_unlock();
193 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
194 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
197 TRACE("iface %p, priority %u.\n", iface, PriorityNew);
199 wined3d_mutex_lock();
200 hr = wined3d_surface_set_priority(This->wined3d_surface, PriorityNew);
201 wined3d_mutex_unlock();
206 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
207 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
210 TRACE("iface %p.\n", iface);
212 wined3d_mutex_lock();
213 hr = wined3d_surface_get_priority(This->wined3d_surface);
214 wined3d_mutex_unlock();
219 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
220 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
222 TRACE("iface %p.\n", iface);
224 wined3d_mutex_lock();
225 wined3d_surface_preload(This->wined3d_surface);
226 wined3d_mutex_unlock();
229 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(IDirect3DSurface9 *iface)
231 TRACE("iface %p.\n", iface);
233 return D3DRTYPE_SURFACE;
236 /* IDirect3DSurface9 Interface follow: */
237 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
238 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
241 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
243 if (!This->container) return E_NOINTERFACE;
245 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
247 TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
252 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
254 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
255 struct wined3d_resource_desc wined3d_desc;
256 struct wined3d_resource *wined3d_resource;
258 TRACE("iface %p, desc %p.\n", iface, desc);
260 wined3d_mutex_lock();
261 wined3d_resource = wined3d_surface_get_resource(This->wined3d_surface);
262 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
263 wined3d_mutex_unlock();
265 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
266 desc->Type = wined3d_desc.resource_type;
267 desc->Usage = wined3d_desc.usage;
268 desc->Pool = wined3d_desc.pool;
269 desc->MultiSampleType = wined3d_desc.multisample_type;
270 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
271 desc->Width = wined3d_desc.width;
272 desc->Height = wined3d_desc.height;
277 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
278 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
281 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
283 wined3d_mutex_lock();
284 hr = wined3d_surface_map(This->wined3d_surface, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
285 wined3d_mutex_unlock();
290 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
291 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
294 TRACE("iface %p.\n", iface);
296 wined3d_mutex_lock();
297 hr = wined3d_surface_unmap(This->wined3d_surface);
298 wined3d_mutex_unlock();
302 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
307 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
308 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
311 TRACE("iface %p, hdc %p.\n", iface, phdc);
313 if(!This->getdc_supported)
315 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
316 /* Don't touch the DC */
317 return D3DERR_INVALIDCALL;
320 wined3d_mutex_lock();
321 hr = wined3d_surface_getdc(This->wined3d_surface, phdc);
322 wined3d_mutex_unlock();
327 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
328 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
331 TRACE("iface %p, hdc %p.\n", iface, hdc);
333 wined3d_mutex_lock();
334 hr = wined3d_surface_releasedc(This->wined3d_surface, hdc);
335 wined3d_mutex_unlock();
339 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
344 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
347 IDirect3DSurface9Impl_QueryInterface,
348 IDirect3DSurface9Impl_AddRef,
349 IDirect3DSurface9Impl_Release,
350 /* IDirect3DResource9 */
351 IDirect3DSurface9Impl_GetDevice,
352 IDirect3DSurface9Impl_SetPrivateData,
353 IDirect3DSurface9Impl_GetPrivateData,
354 IDirect3DSurface9Impl_FreePrivateData,
355 IDirect3DSurface9Impl_SetPriority,
356 IDirect3DSurface9Impl_GetPriority,
357 IDirect3DSurface9Impl_PreLoad,
358 IDirect3DSurface9Impl_GetType,
359 /* IDirect3DSurface9 */
360 IDirect3DSurface9Impl_GetContainer,
361 IDirect3DSurface9Impl_GetDesc,
362 IDirect3DSurface9Impl_LockRect,
363 IDirect3DSurface9Impl_UnlockRect,
364 IDirect3DSurface9Impl_GetDC,
365 IDirect3DSurface9Impl_ReleaseDC
368 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
370 HeapFree(GetProcessHeap(), 0, parent);
373 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
375 surface_wined3d_object_destroyed,
378 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
379 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
380 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
384 surface->lpVtbl = &Direct3DSurface9_Vtbl;
389 case D3DFMT_A8R8G8B8:
390 case D3DFMT_X8R8G8B8:
392 case D3DFMT_X1R5G5B5:
393 case D3DFMT_A1R5G5B5:
395 surface->getdc_supported = TRUE;
399 surface->getdc_supported = FALSE;
403 /* FIXME: Check MAX bounds of MultisampleQuality. */
404 if (multisample_quality > 0)
406 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
407 multisample_quality = 0;
410 wined3d_mutex_lock();
411 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
412 lockable, discard, level, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, multisample_type,
413 multisample_quality, SURFACE_OPENGL, surface, &d3d9_surface_wined3d_parent_ops, &surface->wined3d_surface);
414 wined3d_mutex_unlock();
417 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
421 surface->parentDevice = &device->IDirect3DDevice9Ex_iface;
422 IDirect3DDevice9Ex_AddRef(surface->parentDevice);