4b382ecbc980984a4e1f620f150e6bdf9dfffbcc
[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 EnterCriticalSection(&d3d8_cs);
80 IWineD3DSurface_Release(This->wineD3DSurface);
81 LeaveCriticalSection(&d3d8_cs);
82 HeapFree(GetProcessHeap(), 0, This);
83 }
84 }
85
86 return ref;
87 }
88 }
89
90 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
91 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8 **ppDevice) {
92 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
93 IWineD3DDevice *wined3d_device;
94 HRESULT hr;
95 TRACE("(%p)->(%p)\n", This, ppDevice);
96
97 EnterCriticalSection(&d3d8_cs);
98 hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
99 if (SUCCEEDED(hr))
100 {
101 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
102 IWineD3DDevice_Release(wined3d_device);
103 }
104 LeaveCriticalSection(&d3d8_cs);
105 return hr;
106 }
107
108 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
109 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
110 HRESULT hr;
111 TRACE("(%p) Relay\n", This);
112
113 EnterCriticalSection(&d3d8_cs);
114 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
115 LeaveCriticalSection(&d3d8_cs);
116 return hr;
117 }
118
119 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
120 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
121 HRESULT hr;
122 TRACE("(%p) Relay\n", This);
123
124 EnterCriticalSection(&d3d8_cs);
125 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
126 LeaveCriticalSection(&d3d8_cs);
127 return hr;
128 }
129
130 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
131 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
132 HRESULT hr;
133 TRACE("(%p) Relay\n", This);
134
135 EnterCriticalSection(&d3d8_cs);
136 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
137 LeaveCriticalSection(&d3d8_cs);
138 return hr;
139 }
140
141 /* IDirect3DSurface8 Interface follow: */
142 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
143 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
144 HRESULT res;
145
146 TRACE("(%p) Relay\n", This);
147
148 if (!This->container) return E_NOINTERFACE;
149
150 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
151
152 TRACE("(%p) : returning %p\n", This, *ppContainer);
153 return res;
154 }
155
156 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
157 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
158 WINED3DSURFACE_DESC wined3ddesc;
159 HRESULT hr;
160 TRACE("(%p) Relay\n", This);
161
162 /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
163 memset(&wined3ddesc, 0, sizeof(wined3ddesc));
164 wined3ddesc.Format = (WINED3DFORMAT *)&pDesc->Format;
165 wined3ddesc.Type = (WINED3DRESOURCETYPE *)&pDesc->Type;
166 wined3ddesc.Usage = &pDesc->Usage;
167 wined3ddesc.Pool = (WINED3DPOOL *) &pDesc->Pool;
168 wined3ddesc.Size = &pDesc->Size;
169 wined3ddesc.MultiSampleType = (WINED3DMULTISAMPLE_TYPE *) &pDesc->MultiSampleType;
170 wined3ddesc.Width = &pDesc->Width;
171 wined3ddesc.Height = &pDesc->Height;
172
173 EnterCriticalSection(&d3d8_cs);
174 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
175 LeaveCriticalSection(&d3d8_cs);
176
177 if (SUCCEEDED(hr)) pDesc->Format = d3dformat_from_wined3dformat(pDesc->Format);
178
179 return hr;
180 }
181
182 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
183 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
184 HRESULT hr;
185 TRACE("(%p) Relay\n", This);
186 TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
187
188 EnterCriticalSection(&d3d8_cs);
189 if (pRect) {
190 D3DSURFACE_DESC desc;
191 IDirect3DSurface8_GetDesc(iface, &desc);
192
193 if ((pRect->left < 0)
194 || (pRect->top < 0)
195 || (pRect->left >= pRect->right)
196 || (pRect->top >= pRect->bottom)
197 || (pRect->right > desc.Width)
198 || (pRect->bottom > desc.Height)) {
199 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
200 LeaveCriticalSection(&d3d8_cs);
201 return D3DERR_INVALIDCALL;
202 }
203 }
204
205 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
206 LeaveCriticalSection(&d3d8_cs);
207 return hr;
208 }
209
210 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
211 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
212 HRESULT hr;
213 TRACE("(%p) Relay\n", This);
214
215 EnterCriticalSection(&d3d8_cs);
216 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
217 LeaveCriticalSection(&d3d8_cs);
218 switch(hr)
219 {
220 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
221 default: return hr;
222 }
223 }
224
225 const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
226 {
227 /* IUnknown */
228 IDirect3DSurface8Impl_QueryInterface,
229 IDirect3DSurface8Impl_AddRef,
230 IDirect3DSurface8Impl_Release,
231 /* IDirect3DResource8 */
232 IDirect3DSurface8Impl_GetDevice,
233 IDirect3DSurface8Impl_SetPrivateData,
234 IDirect3DSurface8Impl_GetPrivateData,
235 IDirect3DSurface8Impl_FreePrivateData,
236 /* IDirect3DSurface8 */
237 IDirect3DSurface8Impl_GetContainer,
238 IDirect3DSurface8Impl_GetDesc,
239 IDirect3DSurface8Impl_LockRect,
240 IDirect3DSurface8Impl_UnlockRect
241 };