2 * IDirect3DSurface8 implementation
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
30 if (IsEqualGUID(riid, &IID_IUnknown)
31 || IsEqualGUID(riid, &IID_IDirect3DResource8)
32 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
33 IUnknown_AddRef(iface);
38 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
43 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
44 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
46 TRACE("(%p)\n", This);
48 if (This->forwardReference) {
49 /* Forward refcounting */
50 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
51 return IUnknown_AddRef(This->forwardReference);
53 /* No container, handle our own refcounting */
54 ULONG ref = InterlockedIncrement(&This->ref);
57 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
59 IUnknown_AddRef(This->wineD3DSurface);
60 wined3d_mutex_unlock();
62 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
67 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
68 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
70 TRACE("(%p)\n", This);
72 if (This->forwardReference) {
73 /* Forward refcounting */
74 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
75 return IUnknown_Release(This->forwardReference);
77 /* No container, handle our own refcounting */
78 ULONG ref = InterlockedDecrement(&This->ref);
79 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
82 if (This->parentDevice) IUnknown_Release(This->parentDevice);
83 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
85 IWineD3DSurface_Release(This->wineD3DSurface);
86 wined3d_mutex_unlock();
93 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
94 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
95 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
96 IWineD3DDevice *wined3d_device;
98 TRACE("(%p)->(%p)\n", This, ppDevice);
100 wined3d_mutex_lock();
101 hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
104 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
105 IWineD3DDevice_Release(wined3d_device);
107 wined3d_mutex_unlock();
112 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
113 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
115 TRACE("(%p) Relay\n", This);
117 wined3d_mutex_lock();
118 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
119 wined3d_mutex_unlock();
124 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
125 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
127 TRACE("(%p) Relay\n", This);
129 wined3d_mutex_lock();
130 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
131 wined3d_mutex_unlock();
136 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
137 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
139 TRACE("(%p) Relay\n", This);
141 wined3d_mutex_lock();
142 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
143 wined3d_mutex_unlock();
148 /* IDirect3DSurface8 Interface follow: */
149 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
150 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
153 TRACE("(%p) Relay\n", This);
155 if (!This->container) return E_NOINTERFACE;
157 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
159 TRACE("(%p) : returning %p\n", This, *ppContainer);
163 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
164 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
165 WINED3DSURFACE_DESC wined3ddesc;
167 TRACE("(%p) Relay\n", This);
169 wined3d_mutex_lock();
170 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
171 wined3d_mutex_unlock();
175 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
176 pDesc->Type = wined3ddesc.resource_type;
177 pDesc->Usage = wined3ddesc.usage;
178 pDesc->Pool = wined3ddesc.pool;
179 pDesc->Size = wined3ddesc.size;
180 pDesc->MultiSampleType = wined3ddesc.multisample_type;
181 pDesc->Width = wined3ddesc.width;
182 pDesc->Height = wined3ddesc.height;
188 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
189 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
191 TRACE("(%p) Relay\n", This);
192 TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
194 wined3d_mutex_lock();
196 D3DSURFACE_DESC desc;
197 IDirect3DSurface8_GetDesc(iface, &desc);
199 if ((pRect->left < 0)
201 || (pRect->left >= pRect->right)
202 || (pRect->top >= pRect->bottom)
203 || (pRect->right > desc.Width)
204 || (pRect->bottom > desc.Height)) {
205 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
206 wined3d_mutex_unlock();
208 return D3DERR_INVALIDCALL;
212 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
213 wined3d_mutex_unlock();
218 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
219 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
221 TRACE("(%p) Relay\n", This);
223 wined3d_mutex_lock();
224 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
225 wined3d_mutex_unlock();
229 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
234 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
237 IDirect3DSurface8Impl_QueryInterface,
238 IDirect3DSurface8Impl_AddRef,
239 IDirect3DSurface8Impl_Release,
240 /* IDirect3DResource8 */
241 IDirect3DSurface8Impl_GetDevice,
242 IDirect3DSurface8Impl_SetPrivateData,
243 IDirect3DSurface8Impl_GetPrivateData,
244 IDirect3DSurface8Impl_FreePrivateData,
245 /* IDirect3DSurface8 */
246 IDirect3DSurface8Impl_GetContainer,
247 IDirect3DSurface8Impl_GetDesc,
248 IDirect3DSurface8Impl_LockRect,
249 IDirect3DSurface8Impl_UnlockRect
252 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
254 HeapFree(GetProcessHeap(), 0, parent);
257 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
259 surface_wined3d_object_destroyed,
262 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
263 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
264 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
268 surface->lpVtbl = &Direct3DSurface8_Vtbl;
271 /* FIXME: Check MAX bounds of MultisampleQuality. */
272 if (multisample_quality > 0)
274 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
275 multisample_quality = 0;
278 wined3d_mutex_lock();
279 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
280 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
281 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
282 &d3d8_surface_wined3d_parent_ops);
283 wined3d_mutex_unlock();
286 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
290 surface->parentDevice = (IDirect3DDevice8 *)device;
291 IUnknown_AddRef(surface->parentDevice);