Sync with trunk r62529.
[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 "d3d9_private.h"
23
24 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
25 {
26 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
27 }
28
29 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
30 {
31 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
32
33 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
34 || IsEqualGUID(riid, &IID_IDirect3DResource9)
35 || IsEqualGUID(riid, &IID_IUnknown))
36 {
37 IDirect3DSurface9_AddRef(iface);
38 *out = iface;
39 return S_OK;
40 }
41
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43
44 *out = NULL;
45 return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
49 {
50 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
51 ULONG refcount;
52
53 TRACE("iface %p.\n", iface);
54
55 if (surface->forwardReference)
56 {
57 TRACE("Forwarding to %p.\n", surface->forwardReference);
58 return IUnknown_AddRef(surface->forwardReference);
59 }
60
61 refcount = InterlockedIncrement(&surface->refcount);
62 TRACE("%p increasing refcount to %u.\n", iface, refcount);
63
64 if (refcount == 1)
65 {
66 if (surface->parent_device)
67 IDirect3DDevice9Ex_AddRef(surface->parent_device);
68 wined3d_mutex_lock();
69 wined3d_surface_incref(surface->wined3d_surface);
70 wined3d_mutex_unlock();
71 }
72
73 return refcount;
74 }
75
76 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
77 {
78 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
79 ULONG refcount;
80
81 TRACE("iface %p.\n", iface);
82
83 if (surface->forwardReference)
84 {
85 TRACE("Forwarding to %p.\n", surface->forwardReference);
86 return IUnknown_Release(surface->forwardReference);
87 }
88
89 refcount = InterlockedDecrement(&surface->refcount);
90 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
91
92 if (!refcount)
93 {
94 IDirect3DDevice9Ex *parent_device = surface->parent_device;
95
96 wined3d_mutex_lock();
97 wined3d_surface_decref(surface->wined3d_surface);
98 wined3d_mutex_unlock();
99
100 /* Release the device last, as it may cause the device to be destroyed. */
101 if (parent_device)
102 IDirect3DDevice9Ex_Release(parent_device);
103 }
104
105 return refcount;
106 }
107
108 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
109 {
110 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
111
112 TRACE("iface %p, device %p.\n", iface, device);
113
114 if (surface->forwardReference)
115 {
116 IDirect3DResource9 *resource;
117 HRESULT hr;
118
119 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource9, (void **)&resource);
120 if (SUCCEEDED(hr))
121 {
122 hr = IDirect3DResource9_GetDevice(resource, device);
123 IDirect3DResource9_Release(resource);
124
125 TRACE("Returning device %p.\n", *device);
126 }
127
128 return hr;
129 }
130
131 *device = (IDirect3DDevice9 *)surface->parent_device;
132 IDirect3DDevice9_AddRef(*device);
133
134 TRACE("Returning device %p.\n", *device);
135
136 return D3D_OK;
137 }
138
139 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
140 const void *data, DWORD data_size, DWORD flags)
141 {
142 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
143 struct wined3d_resource *resource;
144 HRESULT hr;
145
146 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
147 iface, debugstr_guid(guid), data, data_size, flags);
148
149 wined3d_mutex_lock();
150 resource = wined3d_surface_get_resource(surface->wined3d_surface);
151 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
152 wined3d_mutex_unlock();
153
154 return hr;
155 }
156
157 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
158 void *data, DWORD *data_size)
159 {
160 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
161 struct wined3d_resource *resource;
162 HRESULT hr;
163
164 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
165 iface, debugstr_guid(guid), data, data_size);
166
167 wined3d_mutex_lock();
168 resource = wined3d_surface_get_resource(surface->wined3d_surface);
169 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
170 wined3d_mutex_unlock();
171
172 return hr;
173 }
174
175 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
176 {
177 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
178 struct wined3d_resource *resource;
179 HRESULT hr;
180
181 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
182
183 wined3d_mutex_lock();
184 resource = wined3d_surface_get_resource(surface->wined3d_surface);
185 hr = wined3d_resource_free_private_data(resource, guid);
186 wined3d_mutex_unlock();
187
188 return hr;
189 }
190
191 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
192 {
193 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
194 DWORD ret;
195
196 TRACE("iface %p, priority %u.\n", iface, priority);
197
198 wined3d_mutex_lock();
199 ret = wined3d_surface_set_priority(surface->wined3d_surface, priority);
200 wined3d_mutex_unlock();
201
202 return ret;
203 }
204
205 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
206 {
207 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
208 DWORD ret;
209
210 TRACE("iface %p.\n", iface);
211
212 wined3d_mutex_lock();
213 ret = wined3d_surface_get_priority(surface->wined3d_surface);
214 wined3d_mutex_unlock();
215
216 return ret;
217 }
218
219 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
220 {
221 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
222
223 TRACE("iface %p.\n", iface);
224
225 wined3d_mutex_lock();
226 wined3d_surface_preload(surface->wined3d_surface);
227 wined3d_mutex_unlock();
228 }
229
230 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
231 {
232 TRACE("iface %p.\n", iface);
233
234 return D3DRTYPE_SURFACE;
235 }
236
237 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
238 {
239 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
240 HRESULT hr;
241
242 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
243
244 if (!surface->container)
245 return E_NOINTERFACE;
246
247 hr = IUnknown_QueryInterface(surface->container, riid, container);
248
249 TRACE("Returning %p.\n", *container);
250
251 return hr;
252 }
253
254 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
255 {
256 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
257 struct wined3d_resource_desc wined3d_desc;
258 struct wined3d_resource *wined3d_resource;
259
260 TRACE("iface %p, desc %p.\n", iface, desc);
261
262 wined3d_mutex_lock();
263 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
264 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
265 wined3d_mutex_unlock();
266
267 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
268 desc->Type = wined3d_desc.resource_type;
269 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
270 desc->Pool = wined3d_desc.pool;
271 desc->MultiSampleType = wined3d_desc.multisample_type;
272 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
273 desc->Width = wined3d_desc.width;
274 desc->Height = wined3d_desc.height;
275
276 return D3D_OK;
277 }
278
279 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
280 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
281 {
282 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
283 struct wined3d_map_desc map_desc;
284 HRESULT hr;
285
286 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
287 iface, locked_rect, wine_dbgstr_rect(rect), flags);
288
289 wined3d_mutex_lock();
290 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
291 wined3d_mutex_unlock();
292
293 if (SUCCEEDED(hr))
294 {
295 locked_rect->Pitch = map_desc.row_pitch;
296 locked_rect->pBits = map_desc.data;
297 }
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 void surface_init(struct d3d9_surface *surface, struct wined3d_surface *wined3d_surface,
394 struct d3d9_device *device, const struct wined3d_parent_ops **parent_ops)
395 {
396 struct wined3d_resource_desc desc;
397
398 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
399 surface->refcount = 1;
400
401 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &desc);
402 switch (d3dformat_from_wined3dformat(desc.format))
403 {
404 case D3DFMT_A8R8G8B8:
405 case D3DFMT_X8R8G8B8:
406 case D3DFMT_R5G6B5:
407 case D3DFMT_X1R5G5B5:
408 case D3DFMT_A1R5G5B5:
409 case D3DFMT_R8G8B8:
410 surface->getdc_supported = TRUE;
411 break;
412
413 default:
414 surface->getdc_supported = FALSE;
415 break;
416 }
417
418 wined3d_surface_incref(wined3d_surface);
419 surface->wined3d_surface = wined3d_surface;
420 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
421 IDirect3DDevice9Ex_AddRef(surface->parent_device);
422
423 *parent_ops = &d3d9_surface_wined3d_parent_ops;
424 }
425
426 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
427 {
428 if (!iface)
429 return NULL;
430 assert(iface->lpVtbl == &d3d9_surface_vtbl);
431
432 return impl_from_IDirect3DSurface9(iface);
433 }