push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / d3d8 / surface.c
blob1d4b1dca0b6f6dc98a08848110fb34efd75582e5
1 /*
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
21 #include "config.h"
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 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
32 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3DResource8)
34 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
35 IUnknown_AddRef(iface);
36 *ppobj = This;
37 return S_OK;
40 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41 *ppobj = NULL;
42 return E_NOINTERFACE;
45 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
46 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
48 TRACE("iface %p.\n", iface);
50 if (This->forwardReference) {
51 /* Forward refcounting */
52 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
53 return IUnknown_AddRef(This->forwardReference);
54 } else {
55 /* No container, handle our own refcounting */
56 ULONG ref = InterlockedIncrement(&This->ref);
58 TRACE("%p increasing refcount to %u.\n", iface, ref);
60 if (ref == 1)
62 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
63 wined3d_mutex_lock();
64 IUnknown_AddRef(This->wineD3DSurface);
65 wined3d_mutex_unlock();
68 return ref;
72 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
73 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
75 TRACE("iface %p.\n", iface);
77 if (This->forwardReference) {
78 /* Forward refcounting */
79 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
80 return IUnknown_Release(This->forwardReference);
81 } else {
82 /* No container, handle our own refcounting */
83 ULONG ref = InterlockedDecrement(&This->ref);
85 TRACE("%p decreasing refcount to %u.\n", iface, ref);
87 if (ref == 0) {
88 IDirect3DDevice8 *parentDevice = This->parentDevice;
90 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
91 wined3d_mutex_lock();
92 IWineD3DSurface_Release(This->wineD3DSurface);
93 wined3d_mutex_unlock();
95 if (parentDevice) IDirect3DDevice8_Release(parentDevice);
98 return ref;
102 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
103 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
105 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
107 TRACE("iface %p, device %p.\n", iface, device);
109 *device = (IDirect3DDevice8 *)This->parentDevice;
110 IDirect3DDevice8_AddRef(*device);
112 TRACE("Returning device %p.\n", *device);
114 return D3D_OK;
117 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
118 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
119 HRESULT hr;
121 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
122 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
124 wined3d_mutex_lock();
125 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
126 wined3d_mutex_unlock();
128 return hr;
131 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
132 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
133 HRESULT hr;
135 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
136 iface, debugstr_guid(refguid), pData, pSizeOfData);
138 wined3d_mutex_lock();
139 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
140 wined3d_mutex_unlock();
142 return hr;
145 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
146 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
147 HRESULT hr;
149 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
151 wined3d_mutex_lock();
152 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
153 wined3d_mutex_unlock();
155 return hr;
158 /* IDirect3DSurface8 Interface follow: */
159 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
160 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
161 HRESULT res;
163 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
165 if (!This->container) return E_NOINTERFACE;
167 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
169 TRACE("(%p) : returning %p\n", This, *ppContainer);
170 return res;
173 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
174 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
175 WINED3DSURFACE_DESC wined3ddesc;
176 HRESULT hr;
178 TRACE("iface %p, desc %p.\n", iface, pDesc);
180 wined3d_mutex_lock();
181 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
182 wined3d_mutex_unlock();
184 if (SUCCEEDED(hr))
186 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
187 pDesc->Type = wined3ddesc.resource_type;
188 pDesc->Usage = wined3ddesc.usage;
189 pDesc->Pool = wined3ddesc.pool;
190 pDesc->Size = wined3ddesc.size;
191 pDesc->MultiSampleType = wined3ddesc.multisample_type;
192 pDesc->Width = wined3ddesc.width;
193 pDesc->Height = wined3ddesc.height;
196 return hr;
199 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
200 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
201 HRESULT hr;
203 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
205 wined3d_mutex_lock();
206 if (pRect) {
207 D3DSURFACE_DESC desc;
208 IDirect3DSurface8_GetDesc(iface, &desc);
210 if ((pRect->left < 0)
211 || (pRect->top < 0)
212 || (pRect->left >= pRect->right)
213 || (pRect->top >= pRect->bottom)
214 || (pRect->right > desc.Width)
215 || (pRect->bottom > desc.Height)) {
216 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
217 wined3d_mutex_unlock();
219 return D3DERR_INVALIDCALL;
223 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
224 wined3d_mutex_unlock();
226 return hr;
229 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
230 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
231 HRESULT hr;
233 TRACE("iface %p.\n", iface);
235 wined3d_mutex_lock();
236 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
237 wined3d_mutex_unlock();
239 switch(hr)
241 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
242 default: return hr;
246 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
248 /* IUnknown */
249 IDirect3DSurface8Impl_QueryInterface,
250 IDirect3DSurface8Impl_AddRef,
251 IDirect3DSurface8Impl_Release,
252 /* IDirect3DResource8 */
253 IDirect3DSurface8Impl_GetDevice,
254 IDirect3DSurface8Impl_SetPrivateData,
255 IDirect3DSurface8Impl_GetPrivateData,
256 IDirect3DSurface8Impl_FreePrivateData,
257 /* IDirect3DSurface8 */
258 IDirect3DSurface8Impl_GetContainer,
259 IDirect3DSurface8Impl_GetDesc,
260 IDirect3DSurface8Impl_LockRect,
261 IDirect3DSurface8Impl_UnlockRect
264 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
266 HeapFree(GetProcessHeap(), 0, parent);
269 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
271 surface_wined3d_object_destroyed,
274 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
275 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
276 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
278 HRESULT hr;
280 surface->lpVtbl = &Direct3DSurface8_Vtbl;
281 surface->ref = 1;
283 /* FIXME: Check MAX bounds of MultisampleQuality. */
284 if (multisample_quality > 0)
286 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
287 multisample_quality = 0;
290 wined3d_mutex_lock();
291 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
292 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
293 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
294 &d3d8_surface_wined3d_parent_ops);
295 wined3d_mutex_unlock();
296 if (FAILED(hr))
298 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
299 return hr;
302 surface->parentDevice = (IDirect3DDevice8 *)device;
303 IUnknown_AddRef(surface->parentDevice);
305 return D3D_OK;