Sync ddraw, d3d8 and d3d9 + wined3d to wine 1.1.28
[reactos.git] / reactos / dll / directx / wine / d3d8 / surface.c
1 /*
2 * IDirect3DSurface8 implementation
3 *
4 * Copyright 2005 Oliver Stieber
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
29
30 if (IsEqualGUID(riid, &IID_IUnknown)
31 || IsEqualGUID(riid, &IID_IDirect3DResource8)
32 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
33 IUnknown_AddRef(iface);
34 *ppobj = This;
35 return S_OK;
36 }
37
38 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
39 *ppobj = NULL;
40 return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
44 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
45
46 TRACE("(%p)\n", This);
47
48 if (This->forwardReference) {
49 /* Forward refcounting */
50 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
51 return IUnknown_AddRef(This->forwardReference);
52 } else {
53 /* No container, handle our own refcounting */
54 ULONG ref = InterlockedIncrement(&This->ref);
55 if(ref == 1 && This->parentDevice) IUnknown_AddRef(This->parentDevice);
56 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
57 return ref;
58 }
59 }
60
61 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
62 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
63
64 TRACE("(%p)\n", This);
65
66 if (This->forwardReference) {
67 /* Forward refcounting */
68 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
69 return IUnknown_Release(This->forwardReference);
70 } else {
71 /* No container, handle our own refcounting */
72 ULONG ref = InterlockedDecrement(&This->ref);
73 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
74
75 if (ref == 0) {
76 if (This->parentDevice) IUnknown_Release(This->parentDevice);
77 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
78 if (!This->isImplicit) {
79 wined3d_mutex_lock();
80 IWineD3DSurface_Release(This->wineD3DSurface);
81 wined3d_mutex_unlock();
82
83 HeapFree(GetProcessHeap(), 0, This);
84 }
85 }
86
87 return ref;
88 }
89 }
90
91 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
92 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
93 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
94 IWineD3DDevice *wined3d_device;
95 HRESULT hr;
96 TRACE("(%p)->(%p)\n", This, ppDevice);
97
98 wined3d_mutex_lock();
99 hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
100 if (SUCCEEDED(hr))
101 {
102 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
103 IWineD3DDevice_Release(wined3d_device);
104 }
105 wined3d_mutex_unlock();
106
107 return hr;
108 }
109
110 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
111 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
112 HRESULT hr;
113 TRACE("(%p) Relay\n", This);
114
115 wined3d_mutex_lock();
116 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
117 wined3d_mutex_unlock();
118
119 return hr;
120 }
121
122 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
123 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
124 HRESULT hr;
125 TRACE("(%p) Relay\n", This);
126
127 wined3d_mutex_lock();
128 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
129 wined3d_mutex_unlock();
130
131 return hr;
132 }
133
134 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
135 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
136 HRESULT hr;
137 TRACE("(%p) Relay\n", This);
138
139 wined3d_mutex_lock();
140 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
141 wined3d_mutex_unlock();
142
143 return hr;
144 }
145
146 /* IDirect3DSurface8 Interface follow: */
147 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
148 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
149 HRESULT res;
150
151 TRACE("(%p) Relay\n", This);
152
153 if (!This->container) return E_NOINTERFACE;
154
155 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
156
157 TRACE("(%p) : returning %p\n", This, *ppContainer);
158 return res;
159 }
160
161 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
162 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
163 WINED3DSURFACE_DESC wined3ddesc;
164 HRESULT hr;
165 TRACE("(%p) Relay\n", This);
166
167 wined3d_mutex_lock();
168 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
169 wined3d_mutex_unlock();
170
171 if (SUCCEEDED(hr))
172 {
173 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
174 pDesc->Type = wined3ddesc.resource_type;
175 pDesc->Usage = wined3ddesc.usage;
176 pDesc->Pool = wined3ddesc.pool;
177 pDesc->Size = wined3ddesc.size;
178 pDesc->MultiSampleType = wined3ddesc.multisample_type;
179 pDesc->Width = wined3ddesc.width;
180 pDesc->Height = wined3ddesc.height;
181 }
182
183 return hr;
184 }
185
186 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
187 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
188 HRESULT hr;
189 TRACE("(%p) Relay\n", This);
190 TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
191
192 wined3d_mutex_lock();
193 if (pRect) {
194 D3DSURFACE_DESC desc;
195 IDirect3DSurface8_GetDesc(iface, &desc);
196
197 if ((pRect->left < 0)
198 || (pRect->top < 0)
199 || (pRect->left >= pRect->right)
200 || (pRect->top >= pRect->bottom)
201 || (pRect->right > desc.Width)
202 || (pRect->bottom > desc.Height)) {
203 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
204 wined3d_mutex_unlock();
205
206 return D3DERR_INVALIDCALL;
207 }
208 }
209
210 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
211 wined3d_mutex_unlock();
212
213 return hr;
214 }
215
216 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
217 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
218 HRESULT hr;
219 TRACE("(%p) Relay\n", This);
220
221 wined3d_mutex_lock();
222 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
223 wined3d_mutex_unlock();
224
225 switch(hr)
226 {
227 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
228 default: return hr;
229 }
230 }
231
232 const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
233 {
234 /* IUnknown */
235 IDirect3DSurface8Impl_QueryInterface,
236 IDirect3DSurface8Impl_AddRef,
237 IDirect3DSurface8Impl_Release,
238 /* IDirect3DResource8 */
239 IDirect3DSurface8Impl_GetDevice,
240 IDirect3DSurface8Impl_SetPrivateData,
241 IDirect3DSurface8Impl_GetPrivateData,
242 IDirect3DSurface8Impl_FreePrivateData,
243 /* IDirect3DSurface8 */
244 IDirect3DSurface8Impl_GetContainer,
245 IDirect3DSurface8Impl_GetDesc,
246 IDirect3DSurface8Impl_LockRect,
247 IDirect3DSurface8Impl_UnlockRect
248 };