wined3d: Add a separate function for initializing the "contained_*" fields from the...
[wine/multimedia.git] / dlls / d3d9 / surface.c
blob0c277b9163438c34cd9f5f308eabb840a1b6020d
1 /*
2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
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
22 #include "config.h"
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 if (IsEqualGUID(riid, &IID_IUnknown)
32 || IsEqualGUID(riid, &IID_IDirect3DResource9)
33 || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
34 IDirect3DSurface9_AddRef(iface);
35 *ppobj = This;
36 return S_OK;
39 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40 *ppobj = NULL;
41 return E_NOINTERFACE;
44 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
45 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
47 TRACE("(%p)\n", This);
49 if (This->forwardReference) {
50 /* Forward refcounting */
51 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
52 return IUnknown_AddRef(This->forwardReference);
53 } else {
54 /* No container, handle our own refcounting */
55 ULONG ref = InterlockedIncrement(&This->ref);
56 if (ref == 1)
58 if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
59 wined3d_mutex_lock();
60 IWineD3DSurface_AddRef(This->wineD3DSurface);
61 wined3d_mutex_unlock();
63 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
65 return ref;
70 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
71 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
73 TRACE("(%p)\n", This);
75 if (This->forwardReference) {
76 /* Forward to the containerParent */
77 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
78 return IUnknown_Release(This->forwardReference);
79 } else {
80 /* No container, handle our own refcounting */
81 ULONG ref = InterlockedDecrement(&This->ref);
82 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
84 if (ref == 0) {
85 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
87 wined3d_mutex_lock();
88 IWineD3DSurface_Release(This->wineD3DSurface);
89 wined3d_mutex_unlock();
91 /* Release the device last, as it may cause the device to be destroyed. */
92 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
95 return ref;
99 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
100 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(LPDIRECT3DSURFACE9 iface, IDirect3DDevice9** ppDevice) {
101 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
102 IWineD3DDevice *wined3d_device;
103 HRESULT hr;
104 TRACE("(%p)->(%p)\n", This, ppDevice);
106 wined3d_mutex_lock();
107 hr = IWineD3DSurface_GetDevice(This->wineD3DSurface, &wined3d_device);
108 if (SUCCEEDED(hr))
110 IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
111 IWineD3DDevice_Release(wined3d_device);
113 wined3d_mutex_unlock();
115 return hr;
118 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
119 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
120 HRESULT hr;
121 TRACE("(%p) Relay\n", This);
123 wined3d_mutex_lock();
124 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
125 wined3d_mutex_unlock();
127 return hr;
130 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
131 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
132 HRESULT hr;
133 TRACE("(%p) Relay\n", This);
135 wined3d_mutex_lock();
136 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
137 wined3d_mutex_unlock();
139 return hr;
142 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
143 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
144 HRESULT hr;
145 TRACE("(%p) Relay\n", This);
147 wined3d_mutex_lock();
148 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
149 wined3d_mutex_unlock();
151 return hr;
154 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
155 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
156 HRESULT hr;
157 TRACE("(%p) Relay\n", This);
159 wined3d_mutex_lock();
160 hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
161 wined3d_mutex_unlock();
163 return hr;
166 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
167 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
168 HRESULT hr;
169 TRACE("(%p) Relay\n", This);
171 wined3d_mutex_lock();
172 hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
173 wined3d_mutex_unlock();
175 return hr;
178 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
179 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
180 TRACE("(%p) Relay\n", This);
182 wined3d_mutex_lock();
183 IWineD3DSurface_PreLoad(This->wineD3DSurface);
184 wined3d_mutex_unlock();
187 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
188 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
189 D3DRESOURCETYPE ret;
190 TRACE("(%p) Relay\n", This);
192 wined3d_mutex_lock();
193 ret = IWineD3DSurface_GetType(This->wineD3DSurface);
194 wined3d_mutex_unlock();
196 return ret;
199 /* IDirect3DSurface9 Interface follow: */
200 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
201 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
202 HRESULT res;
204 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
206 if (!This->container) return E_NOINTERFACE;
208 if (!ppContainer) {
209 ERR("Called without a valid ppContainer\n");
212 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
214 TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
216 return res;
219 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
220 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
221 WINED3DSURFACE_DESC wined3ddesc;
222 HRESULT hr;
223 TRACE("(%p) Relay\n", This);
225 wined3d_mutex_lock();
226 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
227 wined3d_mutex_unlock();
229 if (SUCCEEDED(hr))
231 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
232 pDesc->Type = wined3ddesc.resource_type;
233 pDesc->Usage = wined3ddesc.usage;
234 pDesc->Pool = wined3ddesc.pool;
235 pDesc->MultiSampleType = wined3ddesc.multisample_type;
236 pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
237 pDesc->Width = wined3ddesc.width;
238 pDesc->Height = wined3ddesc.height;
241 return hr;
244 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
245 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
246 HRESULT hr;
247 TRACE("(%p) Relay\n", This);
249 wined3d_mutex_lock();
250 TRACE("(%p) calling IWineD3DSurface_LockRect %p %p %p %d\n", This, This->wineD3DSurface, pLockedRect, pRect, Flags);
251 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
252 wined3d_mutex_unlock();
254 return hr;
257 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
258 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
259 HRESULT hr;
260 TRACE("(%p) Relay\n", This);
262 wined3d_mutex_lock();
263 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
264 wined3d_mutex_unlock();
266 switch(hr)
268 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
269 default: return hr;
273 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
274 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
275 HRESULT hr;
276 TRACE("(%p) Relay\n", This);
278 if(!This->getdc_supported)
280 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
281 /* Don't touch the DC */
282 return D3DERR_INVALIDCALL;
285 wined3d_mutex_lock();
286 hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
287 wined3d_mutex_unlock();
289 return hr;
292 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
293 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
294 HRESULT hr;
295 TRACE("(%p) Relay\n", This);
297 wined3d_mutex_lock();
298 hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
299 wined3d_mutex_unlock();
301 switch(hr) {
302 case WINEDDERR_NODC: return WINED3DERR_INVALIDCALL;
303 default: return hr;
307 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
309 /* IUnknown */
310 IDirect3DSurface9Impl_QueryInterface,
311 IDirect3DSurface9Impl_AddRef,
312 IDirect3DSurface9Impl_Release,
313 /* IDirect3DResource9 */
314 IDirect3DSurface9Impl_GetDevice,
315 IDirect3DSurface9Impl_SetPrivateData,
316 IDirect3DSurface9Impl_GetPrivateData,
317 IDirect3DSurface9Impl_FreePrivateData,
318 IDirect3DSurface9Impl_SetPriority,
319 IDirect3DSurface9Impl_GetPriority,
320 IDirect3DSurface9Impl_PreLoad,
321 IDirect3DSurface9Impl_GetType,
322 /* IDirect3DSurface9 */
323 IDirect3DSurface9Impl_GetContainer,
324 IDirect3DSurface9Impl_GetDesc,
325 IDirect3DSurface9Impl_LockRect,
326 IDirect3DSurface9Impl_UnlockRect,
327 IDirect3DSurface9Impl_GetDC,
328 IDirect3DSurface9Impl_ReleaseDC
331 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
333 HeapFree(GetProcessHeap(), 0, parent);
336 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
338 surface_wined3d_object_destroyed,
341 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
342 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
343 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
345 HRESULT hr;
347 surface->lpVtbl = &Direct3DSurface9_Vtbl;
348 surface->ref = 1;
350 switch (format)
352 case D3DFMT_A8R8G8B8:
353 case D3DFMT_X8R8G8B8:
354 case D3DFMT_R5G6B5:
355 case D3DFMT_X1R5G5B5:
356 case D3DFMT_A1R5G5B5:
357 case D3DFMT_R8G8B8:
358 surface->getdc_supported = TRUE;
359 break;
361 default:
362 surface->getdc_supported = FALSE;
363 break;
366 /* FIXME: Check MAX bounds of MultisampleQuality. */
367 if (multisample_quality > 0)
369 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
370 multisample_quality = 0;
373 wined3d_mutex_lock();
374 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
375 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
376 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
377 &d3d9_surface_wined3d_parent_ops);
378 wined3d_mutex_unlock();
379 if (FAILED(hr))
381 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
382 return hr;
385 surface->parentDevice = (IDirect3DDevice9Ex *)device;
386 IDirect3DDevice9Ex_AddRef(surface->parentDevice);
388 return D3D_OK;