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 /* IDirect3DSurface9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
29 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
31 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDirect3DResource9)
35 || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
36 IDirect3DSurface9_AddRef(iface);
41 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
46 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
47 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
49 TRACE("iface %p.\n", iface);
51 if (This->forwardReference) {
52 /* Forward refcounting */
53 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
54 return IUnknown_AddRef(This->forwardReference);
56 /* No container, handle our own refcounting */
57 ULONG ref = InterlockedIncrement(&This->ref);
59 TRACE("%p increasing refcount to %u.\n", iface, ref);
63 if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
65 IWineD3DSurface_AddRef(This->wineD3DSurface);
66 wined3d_mutex_unlock();
74 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
75 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
77 TRACE("iface %p.\n", iface);
79 if (This->forwardReference) {
80 /* Forward to the containerParent */
81 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
82 return IUnknown_Release(This->forwardReference);
84 /* No container, handle our own refcounting */
85 ULONG ref = InterlockedDecrement(&This->ref);
87 TRACE("%p decreasing refcount to %u.\n", iface, ref);
90 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
93 IWineD3DSurface_Release(This->wineD3DSurface);
94 wined3d_mutex_unlock();
96 /* Release the device last, as it may cause the device to be destroyed. */
97 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
104 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
105 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
107 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
109 TRACE("iface %p, device %p.\n", iface, device);
111 if (This->forwardReference)
113 IDirect3DResource9 *resource;
116 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
119 hr = IDirect3DResource9_GetDevice(resource, device);
120 IDirect3DResource9_Release(resource);
122 TRACE("Returning device %p.\n", *device);
128 *device = (IDirect3DDevice9 *)This->parentDevice;
129 IDirect3DDevice9_AddRef(*device);
131 TRACE("Returning device %p.\n", *device);
136 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
137 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
140 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
141 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
143 wined3d_mutex_lock();
144 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
145 wined3d_mutex_unlock();
150 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
151 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
154 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
155 iface, debugstr_guid(refguid), pData, pSizeOfData);
157 wined3d_mutex_lock();
158 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
159 wined3d_mutex_unlock();
164 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
165 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
168 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
170 wined3d_mutex_lock();
171 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
172 wined3d_mutex_unlock();
177 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
178 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
181 TRACE("iface %p, priority %u.\n", iface, PriorityNew);
183 wined3d_mutex_lock();
184 hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
185 wined3d_mutex_unlock();
190 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
191 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
194 TRACE("iface %p.\n", iface);
196 wined3d_mutex_lock();
197 hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
198 wined3d_mutex_unlock();
203 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
204 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
206 TRACE("iface %p.\n", iface);
208 wined3d_mutex_lock();
209 IWineD3DSurface_PreLoad(This->wineD3DSurface);
210 wined3d_mutex_unlock();
213 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
214 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
217 TRACE("iface %p.\n", iface);
219 wined3d_mutex_lock();
220 ret = IWineD3DSurface_GetType(This->wineD3DSurface);
221 wined3d_mutex_unlock();
226 /* IDirect3DSurface9 Interface follow: */
227 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
228 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
231 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
233 if (!This->container) return E_NOINTERFACE;
235 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
237 TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
242 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
244 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
245 struct wined3d_resource_desc wined3d_desc;
246 struct wined3d_resource *wined3d_resource;
248 TRACE("iface %p, desc %p.\n", iface, desc);
250 wined3d_mutex_lock();
251 wined3d_resource = IWineD3DSurface_GetResource(This->wineD3DSurface);
252 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
253 wined3d_mutex_unlock();
255 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
256 desc->Type = wined3d_desc.resource_type;
257 desc->Usage = wined3d_desc.usage;
258 desc->Pool = wined3d_desc.pool;
259 desc->MultiSampleType = wined3d_desc.multisample_type;
260 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
261 desc->Width = wined3d_desc.width;
262 desc->Height = wined3d_desc.height;
267 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
268 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
271 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
273 wined3d_mutex_lock();
274 hr = IWineD3DSurface_Map(This->wineD3DSurface, (WINED3DLOCKED_RECT *)pLockedRect, pRect, Flags);
275 wined3d_mutex_unlock();
280 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
281 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
284 TRACE("iface %p.\n", iface);
286 wined3d_mutex_lock();
287 hr = IWineD3DSurface_Unmap(This->wineD3DSurface);
288 wined3d_mutex_unlock();
292 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
297 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
298 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
301 TRACE("iface %p, hdc %p.\n", iface, phdc);
303 if(!This->getdc_supported)
305 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
306 /* Don't touch the DC */
307 return D3DERR_INVALIDCALL;
310 wined3d_mutex_lock();
311 hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
312 wined3d_mutex_unlock();
317 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
318 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
321 TRACE("iface %p, hdc %p.\n", iface, hdc);
323 wined3d_mutex_lock();
324 hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
325 wined3d_mutex_unlock();
329 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
334 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
337 IDirect3DSurface9Impl_QueryInterface,
338 IDirect3DSurface9Impl_AddRef,
339 IDirect3DSurface9Impl_Release,
340 /* IDirect3DResource9 */
341 IDirect3DSurface9Impl_GetDevice,
342 IDirect3DSurface9Impl_SetPrivateData,
343 IDirect3DSurface9Impl_GetPrivateData,
344 IDirect3DSurface9Impl_FreePrivateData,
345 IDirect3DSurface9Impl_SetPriority,
346 IDirect3DSurface9Impl_GetPriority,
347 IDirect3DSurface9Impl_PreLoad,
348 IDirect3DSurface9Impl_GetType,
349 /* IDirect3DSurface9 */
350 IDirect3DSurface9Impl_GetContainer,
351 IDirect3DSurface9Impl_GetDesc,
352 IDirect3DSurface9Impl_LockRect,
353 IDirect3DSurface9Impl_UnlockRect,
354 IDirect3DSurface9Impl_GetDC,
355 IDirect3DSurface9Impl_ReleaseDC
358 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
360 HeapFree(GetProcessHeap(), 0, parent);
363 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
365 surface_wined3d_object_destroyed,
368 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
369 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
370 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
374 surface->lpVtbl = &Direct3DSurface9_Vtbl;
379 case D3DFMT_A8R8G8B8:
380 case D3DFMT_X8R8G8B8:
382 case D3DFMT_X1R5G5B5:
383 case D3DFMT_A1R5G5B5:
385 surface->getdc_supported = TRUE;
389 surface->getdc_supported = FALSE;
393 /* FIXME: Check MAX bounds of MultisampleQuality. */
394 if (multisample_quality > 0)
396 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
397 multisample_quality = 0;
400 wined3d_mutex_lock();
401 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
402 lockable, discard, level, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, multisample_type,
403 multisample_quality, SURFACE_OPENGL, surface, &d3d9_surface_wined3d_parent_ops, &surface->wineD3DSurface);
404 wined3d_mutex_unlock();
407 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
411 surface->parentDevice = (IDirect3DDevice9Ex *)device;
412 IDirect3DDevice9Ex_AddRef(surface->parentDevice);