210a0ee1d4548d64327fe9052807b0929396147e
[reactos.git] / dll / directx / wine / d3d9 / surface.c
1 /*
2 * IDirect3DSurface9 implementation
3 *
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DSurface9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DSurface9Impl_QueryInterface(LPDIRECT3DSURFACE9 iface, REFIID riid, LPVOID* ppobj) {
29 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
30
31 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
32
33 if (IsEqualGUID(riid, &IID_IUnknown)
34 || IsEqualGUID(riid, &IID_IDirect3DResource9)
35 || IsEqualGUID(riid, &IID_IDirect3DSurface9)) {
36 IDirect3DSurface9_AddRef(iface);
37 *ppobj = This;
38 return S_OK;
39 }
40
41 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
42 *ppobj = NULL;
43 return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DSurface9Impl_AddRef(LPDIRECT3DSURFACE9 iface) {
47 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
48
49 TRACE("iface %p.\n", iface);
50
51 if (This->forwardReference) {
52 /* Forward refcounting */
53 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
54 return IUnknown_AddRef(This->forwardReference);
55 } else {
56 /* No container, handle our own refcounting */
57 ULONG ref = InterlockedIncrement(&This->ref);
58
59 TRACE("%p increasing refcount to %u.\n", iface, ref);
60
61 if (ref == 1)
62 {
63 if (This->parentDevice) IDirect3DDevice9Ex_AddRef(This->parentDevice);
64 wined3d_mutex_lock();
65 IWineD3DSurface_AddRef(This->wineD3DSurface);
66 wined3d_mutex_unlock();
67 }
68
69 return ref;
70 }
71
72 }
73
74 static ULONG WINAPI IDirect3DSurface9Impl_Release(LPDIRECT3DSURFACE9 iface) {
75 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
76
77 TRACE("iface %p.\n", iface);
78
79 if (This->forwardReference) {
80 /* Forward to the containerParent */
81 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
82 return IUnknown_Release(This->forwardReference);
83 } else {
84 /* No container, handle our own refcounting */
85 ULONG ref = InterlockedDecrement(&This->ref);
86
87 TRACE("%p decreasing refcount to %u.\n", iface, ref);
88
89 if (ref == 0) {
90 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
91
92 wined3d_mutex_lock();
93 IWineD3DSurface_Release(This->wineD3DSurface);
94 wined3d_mutex_unlock();
95
96 /* Release the device last, as it may cause the device to be destroyed. */
97 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
98 }
99
100 return ref;
101 }
102 }
103
104 /* IDirect3DSurface9 IDirect3DResource9 Interface follow: */
105 static HRESULT WINAPI IDirect3DSurface9Impl_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
106 {
107 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
108
109 TRACE("iface %p, device %p.\n", iface, device);
110
111 if (This->forwardReference)
112 {
113 IDirect3DResource9 *resource;
114 HRESULT hr;
115
116 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
117 if (SUCCEEDED(hr))
118 {
119 hr = IDirect3DResource9_GetDevice(resource, device);
120 IDirect3DResource9_Release(resource);
121
122 TRACE("Returning device %p.\n", *device);
123 }
124
125 return hr;
126 }
127
128 *device = (IDirect3DDevice9 *)This->parentDevice;
129 IDirect3DDevice9_AddRef(*device);
130
131 TRACE("Returning device %p.\n", *device);
132
133 return D3D_OK;
134 }
135
136 static HRESULT WINAPI IDirect3DSurface9Impl_SetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
137 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
138 HRESULT hr;
139
140 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
141 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
142
143 wined3d_mutex_lock();
144 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
145 wined3d_mutex_unlock();
146
147 return hr;
148 }
149
150 static HRESULT WINAPI IDirect3DSurface9Impl_GetPrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
151 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
152 HRESULT hr;
153
154 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
155 iface, debugstr_guid(refguid), pData, pSizeOfData);
156
157 wined3d_mutex_lock();
158 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
159 wined3d_mutex_unlock();
160
161 return hr;
162 }
163
164 static HRESULT WINAPI IDirect3DSurface9Impl_FreePrivateData(LPDIRECT3DSURFACE9 iface, REFGUID refguid) {
165 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
166 HRESULT hr;
167
168 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
169
170 wined3d_mutex_lock();
171 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
172 wined3d_mutex_unlock();
173
174 return hr;
175 }
176
177 static DWORD WINAPI IDirect3DSurface9Impl_SetPriority(LPDIRECT3DSURFACE9 iface, DWORD PriorityNew) {
178 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
179 HRESULT hr;
180
181 TRACE("iface %p, priority %u.\n", iface, PriorityNew);
182
183 wined3d_mutex_lock();
184 hr = IWineD3DSurface_SetPriority(This->wineD3DSurface, PriorityNew);
185 wined3d_mutex_unlock();
186
187 return hr;
188 }
189
190 static DWORD WINAPI IDirect3DSurface9Impl_GetPriority(LPDIRECT3DSURFACE9 iface) {
191 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
192 HRESULT hr;
193
194 TRACE("iface %p.\n", iface);
195
196 wined3d_mutex_lock();
197 hr = IWineD3DSurface_GetPriority(This->wineD3DSurface);
198 wined3d_mutex_unlock();
199
200 return hr;
201 }
202
203 static void WINAPI IDirect3DSurface9Impl_PreLoad(LPDIRECT3DSURFACE9 iface) {
204 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
205
206 TRACE("iface %p.\n", iface);
207
208 wined3d_mutex_lock();
209 IWineD3DSurface_PreLoad(This->wineD3DSurface);
210 wined3d_mutex_unlock();
211 }
212
213 static D3DRESOURCETYPE WINAPI IDirect3DSurface9Impl_GetType(LPDIRECT3DSURFACE9 iface) {
214 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
215 D3DRESOURCETYPE ret;
216
217 TRACE("iface %p.\n", iface);
218
219 wined3d_mutex_lock();
220 ret = IWineD3DSurface_GetType(This->wineD3DSurface);
221 wined3d_mutex_unlock();
222
223 return ret;
224 }
225
226 /* IDirect3DSurface9 Interface follow: */
227 static HRESULT WINAPI IDirect3DSurface9Impl_GetContainer(LPDIRECT3DSURFACE9 iface, REFIID riid, void** ppContainer) {
228 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
229 HRESULT res;
230
231 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
232
233 if (!This->container) return E_NOINTERFACE;
234
235 if (!ppContainer) {
236 ERR("Called without a valid ppContainer\n");
237 }
238
239 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
240
241 TRACE("Returning ppContainer %p, *ppContainer %p\n", ppContainer, *ppContainer);
242
243 return res;
244 }
245
246 static HRESULT WINAPI IDirect3DSurface9Impl_GetDesc(LPDIRECT3DSURFACE9 iface, D3DSURFACE_DESC* pDesc) {
247 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
248 WINED3DSURFACE_DESC wined3ddesc;
249 HRESULT hr;
250
251 TRACE("iface %p, desc %p.\n", iface, pDesc);
252
253 wined3d_mutex_lock();
254 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
255 wined3d_mutex_unlock();
256
257 if (SUCCEEDED(hr))
258 {
259 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
260 pDesc->Type = wined3ddesc.resource_type;
261 pDesc->Usage = wined3ddesc.usage;
262 pDesc->Pool = wined3ddesc.pool;
263 pDesc->MultiSampleType = wined3ddesc.multisample_type;
264 pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
265 pDesc->Width = wined3ddesc.width;
266 pDesc->Height = wined3ddesc.height;
267 }
268
269 return hr;
270 }
271
272 static HRESULT WINAPI IDirect3DSurface9Impl_LockRect(LPDIRECT3DSURFACE9 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
273 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
274 HRESULT hr;
275
276 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
277
278 wined3d_mutex_lock();
279 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
280 wined3d_mutex_unlock();
281
282 return hr;
283 }
284
285 static HRESULT WINAPI IDirect3DSurface9Impl_UnlockRect(LPDIRECT3DSURFACE9 iface) {
286 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
287 HRESULT hr;
288
289 TRACE("iface %p.\n", iface);
290
291 wined3d_mutex_lock();
292 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
293 wined3d_mutex_unlock();
294
295 switch(hr)
296 {
297 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
298 default: return hr;
299 }
300 }
301
302 static HRESULT WINAPI IDirect3DSurface9Impl_GetDC(LPDIRECT3DSURFACE9 iface, HDC* phdc) {
303 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
304 HRESULT hr;
305
306 TRACE("iface %p, hdc %p.\n", iface, phdc);
307
308 if(!This->getdc_supported)
309 {
310 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
311 /* Don't touch the DC */
312 return D3DERR_INVALIDCALL;
313 }
314
315 wined3d_mutex_lock();
316 hr = IWineD3DSurface_GetDC(This->wineD3DSurface, phdc);
317 wined3d_mutex_unlock();
318
319 return hr;
320 }
321
322 static HRESULT WINAPI IDirect3DSurface9Impl_ReleaseDC(LPDIRECT3DSURFACE9 iface, HDC hdc) {
323 IDirect3DSurface9Impl *This = (IDirect3DSurface9Impl *)iface;
324 HRESULT hr;
325
326 TRACE("iface %p, hdc %p.\n", iface, hdc);
327
328 wined3d_mutex_lock();
329 hr = IWineD3DSurface_ReleaseDC(This->wineD3DSurface, hdc);
330 wined3d_mutex_unlock();
331
332 switch(hr) {
333 case WINEDDERR_NODC: return WINED3DERR_INVALIDCALL;
334 default: return hr;
335 }
336 }
337
338 static const IDirect3DSurface9Vtbl Direct3DSurface9_Vtbl =
339 {
340 /* IUnknown */
341 IDirect3DSurface9Impl_QueryInterface,
342 IDirect3DSurface9Impl_AddRef,
343 IDirect3DSurface9Impl_Release,
344 /* IDirect3DResource9 */
345 IDirect3DSurface9Impl_GetDevice,
346 IDirect3DSurface9Impl_SetPrivateData,
347 IDirect3DSurface9Impl_GetPrivateData,
348 IDirect3DSurface9Impl_FreePrivateData,
349 IDirect3DSurface9Impl_SetPriority,
350 IDirect3DSurface9Impl_GetPriority,
351 IDirect3DSurface9Impl_PreLoad,
352 IDirect3DSurface9Impl_GetType,
353 /* IDirect3DSurface9 */
354 IDirect3DSurface9Impl_GetContainer,
355 IDirect3DSurface9Impl_GetDesc,
356 IDirect3DSurface9Impl_LockRect,
357 IDirect3DSurface9Impl_UnlockRect,
358 IDirect3DSurface9Impl_GetDC,
359 IDirect3DSurface9Impl_ReleaseDC
360 };
361
362 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
363 {
364 HeapFree(GetProcessHeap(), 0, parent);
365 }
366
367 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
368 {
369 surface_wined3d_object_destroyed,
370 };
371
372 HRESULT surface_init(IDirect3DSurface9Impl *surface, IDirect3DDevice9Impl *device,
373 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
374 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
375 {
376 HRESULT hr;
377
378 surface->lpVtbl = &Direct3DSurface9_Vtbl;
379 surface->ref = 1;
380
381 switch (format)
382 {
383 case D3DFMT_A8R8G8B8:
384 case D3DFMT_X8R8G8B8:
385 case D3DFMT_R5G6B5:
386 case D3DFMT_X1R5G5B5:
387 case D3DFMT_A1R5G5B5:
388 case D3DFMT_R8G8B8:
389 surface->getdc_supported = TRUE;
390 break;
391
392 default:
393 surface->getdc_supported = FALSE;
394 break;
395 }
396
397 /* FIXME: Check MAX bounds of MultisampleQuality. */
398 if (multisample_quality > 0)
399 {
400 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
401 multisample_quality = 0;
402 }
403
404 wined3d_mutex_lock();
405 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
406 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
407 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
408 &d3d9_surface_wined3d_parent_ops);
409 wined3d_mutex_unlock();
410 if (FAILED(hr))
411 {
412 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
413 return hr;
414 }
415
416 surface->parentDevice = (IDirect3DDevice9Ex *)device;
417 IDirect3DDevice9Ex_AddRef(surface->parentDevice);
418
419 return D3D_OK;
420 }