Synchronize with trunk's revision r57652.
[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 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
28 {
29 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
30 }
31
32 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
33 {
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
35
36 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
37 || IsEqualGUID(riid, &IID_IDirect3DResource9)
38 || IsEqualGUID(riid, &IID_IUnknown))
39 {
40 IDirect3DSurface9_AddRef(iface);
41 *out = iface;
42 return S_OK;
43 }
44
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46
47 *out = NULL;
48 return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
52 {
53 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
54 ULONG refcount;
55
56 TRACE("iface %p.\n", iface);
57
58 if (surface->forwardReference)
59 {
60 TRACE("Forwarding to %p.\n", surface->forwardReference);
61 return IUnknown_AddRef(surface->forwardReference);
62 }
63
64 refcount = InterlockedIncrement(&surface->refcount);
65 TRACE("%p increasing refcount to %u.\n", iface, refcount);
66
67 if (refcount == 1)
68 {
69 if (surface->parent_device)
70 IDirect3DDevice9Ex_AddRef(surface->parent_device);
71 wined3d_mutex_lock();
72 wined3d_surface_incref(surface->wined3d_surface);
73 wined3d_mutex_unlock();
74 }
75
76 return refcount;
77 }
78
79 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
80 {
81 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
82 ULONG refcount;
83
84 TRACE("iface %p.\n", iface);
85
86 if (surface->forwardReference)
87 {
88 TRACE("Forwarding to %p.\n", surface->forwardReference);
89 return IUnknown_Release(surface->forwardReference);
90 }
91
92 refcount = InterlockedDecrement(&surface->refcount);
93 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
94
95 if (!refcount)
96 {
97 IDirect3DDevice9Ex *parent_device = surface->parent_device;
98
99 wined3d_mutex_lock();
100 wined3d_surface_decref(surface->wined3d_surface);
101 wined3d_mutex_unlock();
102
103 /* Release the device last, as it may cause the device to be destroyed. */
104 if (parent_device)
105 IDirect3DDevice9Ex_Release(parent_device);
106 }
107
108 return refcount;
109 }
110
111 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
112 {
113 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
114
115 TRACE("iface %p, device %p.\n", iface, device);
116
117 if (surface->forwardReference)
118 {
119 IDirect3DResource9 *resource;
120 HRESULT hr;
121
122 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
123 if (SUCCEEDED(hr))
124 {
125 hr = IDirect3DResource9_GetDevice(resource, device);
126 IDirect3DResource9_Release(resource);
127
128 TRACE("Returning device %p.\n", *device);
129 }
130
131 return hr;
132 }
133
134 *device = (IDirect3DDevice9 *)surface->parent_device;
135 IDirect3DDevice9_AddRef(*device);
136
137 TRACE("Returning device %p.\n", *device);
138
139 return D3D_OK;
140 }
141
142 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
143 const void *data, DWORD data_size, DWORD flags)
144 {
145 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
146 struct wined3d_resource *resource;
147 HRESULT hr;
148
149 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
150 iface, debugstr_guid(guid), data, data_size, flags);
151
152 wined3d_mutex_lock();
153 resource = wined3d_surface_get_resource(surface->wined3d_surface);
154 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
155 wined3d_mutex_unlock();
156
157 return hr;
158 }
159
160 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
161 void *data, DWORD *data_size)
162 {
163 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
164 struct wined3d_resource *resource;
165 HRESULT hr;
166
167 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
168 iface, debugstr_guid(guid), data, data_size);
169
170 wined3d_mutex_lock();
171 resource = wined3d_surface_get_resource(surface->wined3d_surface);
172 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
173 wined3d_mutex_unlock();
174
175 return hr;
176 }
177
178 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
179 {
180 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
181 struct wined3d_resource *resource;
182 HRESULT hr;
183
184 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
185
186 wined3d_mutex_lock();
187 resource = wined3d_surface_get_resource(surface->wined3d_surface);
188 hr = wined3d_resource_free_private_data(resource, guid);
189 wined3d_mutex_unlock();
190
191 return hr;
192 }
193
194 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
195 {
196 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
197 DWORD ret;
198
199 TRACE("iface %p, priority %u.\n", iface, priority);
200
201 wined3d_mutex_lock();
202 ret = wined3d_surface_set_priority(surface->wined3d_surface, priority);
203 wined3d_mutex_unlock();
204
205 return ret;
206 }
207
208 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
209 {
210 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
211 DWORD ret;
212
213 TRACE("iface %p.\n", iface);
214
215 wined3d_mutex_lock();
216 ret = wined3d_surface_get_priority(surface->wined3d_surface);
217 wined3d_mutex_unlock();
218
219 return ret;
220 }
221
222 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
223 {
224 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
225
226 TRACE("iface %p.\n", iface);
227
228 wined3d_mutex_lock();
229 wined3d_surface_preload(surface->wined3d_surface);
230 wined3d_mutex_unlock();
231 }
232
233 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
234 {
235 TRACE("iface %p.\n", iface);
236
237 return D3DRTYPE_SURFACE;
238 }
239
240 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
241 {
242 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
243 HRESULT hr;
244
245 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
246
247 if (!surface->container)
248 return E_NOINTERFACE;
249
250 hr = IUnknown_QueryInterface(surface->container, riid, container);
251
252 TRACE("Returning %p.\n", *container);
253
254 return hr;
255 }
256
257 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
258 {
259 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
260 struct wined3d_resource_desc wined3d_desc;
261 struct wined3d_resource *wined3d_resource;
262
263 TRACE("iface %p, desc %p.\n", iface, desc);
264
265 wined3d_mutex_lock();
266 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
267 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
268 wined3d_mutex_unlock();
269
270 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
271 desc->Type = wined3d_desc.resource_type;
272 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
273 desc->Pool = wined3d_desc.pool;
274 desc->MultiSampleType = wined3d_desc.multisample_type;
275 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
276 desc->Width = wined3d_desc.width;
277 desc->Height = wined3d_desc.height;
278
279 return D3D_OK;
280 }
281
282 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
283 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
284 {
285 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
286 struct wined3d_map_desc map_desc;
287 HRESULT hr;
288
289 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
290 iface, locked_rect, wine_dbgstr_rect(rect), flags);
291
292 wined3d_mutex_lock();
293 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
294 wined3d_mutex_unlock();
295
296 locked_rect->Pitch = map_desc.row_pitch;
297 locked_rect->pBits = map_desc.data;
298
299 return hr;
300 }
301
302 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
303 {
304 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
305 HRESULT hr;
306
307 TRACE("iface %p.\n", iface);
308
309 wined3d_mutex_lock();
310 hr = wined3d_surface_unmap(surface->wined3d_surface);
311 wined3d_mutex_unlock();
312
313 switch(hr)
314 {
315 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
316 default: return hr;
317 }
318 }
319
320 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
321 {
322 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
323 HRESULT hr;
324
325 TRACE("iface %p, dc %p.\n", iface, dc);
326
327 if (!surface->getdc_supported)
328 {
329 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
330 /* Don't touch the DC */
331 return D3DERR_INVALIDCALL;
332 }
333
334 wined3d_mutex_lock();
335 hr = wined3d_surface_getdc(surface->wined3d_surface, dc);
336 wined3d_mutex_unlock();
337
338 return hr;
339 }
340
341 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
342 {
343 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
344 HRESULT hr;
345
346 TRACE("iface %p, dc %p.\n", iface, dc);
347
348 wined3d_mutex_lock();
349 hr = wined3d_surface_releasedc(surface->wined3d_surface, dc);
350 wined3d_mutex_unlock();
351
352 switch (hr)
353 {
354 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
355 default: return hr;
356 }
357 }
358
359 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
360 {
361 /* IUnknown */
362 d3d9_surface_QueryInterface,
363 d3d9_surface_AddRef,
364 d3d9_surface_Release,
365 /* IDirect3DResource9 */
366 d3d9_surface_GetDevice,
367 d3d9_surface_SetPrivateData,
368 d3d9_surface_GetPrivateData,
369 d3d9_surface_FreePrivateData,
370 d3d9_surface_SetPriority,
371 d3d9_surface_GetPriority,
372 d3d9_surface_PreLoad,
373 d3d9_surface_GetType,
374 /* IDirect3DSurface9 */
375 d3d9_surface_GetContainer,
376 d3d9_surface_GetDesc,
377 d3d9_surface_LockRect,
378 d3d9_surface_UnlockRect,
379 d3d9_surface_GetDC,
380 d3d9_surface_ReleaseDC,
381 };
382
383 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
384 {
385 HeapFree(GetProcessHeap(), 0, parent);
386 }
387
388 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
389 {
390 surface_wined3d_object_destroyed,
391 };
392
393 HRESULT surface_init(struct d3d9_surface *surface, struct d3d9_device *device,
394 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
395 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
396 {
397 DWORD flags = 0;
398 HRESULT hr;
399
400 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
401 surface->refcount = 1;
402
403 switch (format)
404 {
405 case D3DFMT_A8R8G8B8:
406 case D3DFMT_X8R8G8B8:
407 case D3DFMT_R5G6B5:
408 case D3DFMT_X1R5G5B5:
409 case D3DFMT_A1R5G5B5:
410 case D3DFMT_R8G8B8:
411 surface->getdc_supported = TRUE;
412 break;
413
414 default:
415 surface->getdc_supported = FALSE;
416 break;
417 }
418
419 /* FIXME: Check MAX bounds of MultisampleQuality. */
420 if (multisample_quality > 0)
421 {
422 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
423 multisample_quality = 0;
424 }
425
426 if (lockable)
427 flags |= WINED3D_SURFACE_MAPPABLE;
428 if (discard)
429 flags |= WINED3D_SURFACE_DISCARD;
430
431 wined3d_mutex_lock();
432 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
433 level, usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
434 WINED3D_SURFACE_TYPE_OPENGL, flags, surface, &d3d9_surface_wined3d_parent_ops, &surface->wined3d_surface);
435 wined3d_mutex_unlock();
436 if (FAILED(hr))
437 {
438 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
439 return hr;
440 }
441
442 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
443 IDirect3DDevice9Ex_AddRef(surface->parent_device);
444
445 return D3D_OK;
446 }
447
448 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
449 {
450 if (!iface)
451 return NULL;
452 assert(iface->lpVtbl == &d3d9_surface_vtbl);
453
454 return impl_from_IDirect3DSurface9(iface);
455 }