Sync with trunk r64222.
[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->resource.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->resource.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 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
144 iface, debugstr_guid(guid), data, data_size, flags);
145
146 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
147 }
148
149 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
150 void *data, DWORD *data_size)
151 {
152 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
153 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
154 iface, debugstr_guid(guid), data, data_size);
155
156 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
157 }
158
159 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
160 {
161 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
162 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
163
164 return d3d9_resource_free_private_data(&surface->resource, guid);
165 }
166
167 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
168 {
169 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
170 DWORD ret;
171
172 TRACE("iface %p, priority %u.\n", iface, priority);
173
174 wined3d_mutex_lock();
175 ret = wined3d_surface_set_priority(surface->wined3d_surface, priority);
176 wined3d_mutex_unlock();
177
178 return ret;
179 }
180
181 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
182 {
183 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
184 DWORD ret;
185
186 TRACE("iface %p.\n", iface);
187
188 wined3d_mutex_lock();
189 ret = wined3d_surface_get_priority(surface->wined3d_surface);
190 wined3d_mutex_unlock();
191
192 return ret;
193 }
194
195 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
196 {
197 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
198
199 TRACE("iface %p.\n", iface);
200
201 wined3d_mutex_lock();
202 wined3d_surface_preload(surface->wined3d_surface);
203 wined3d_mutex_unlock();
204 }
205
206 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
207 {
208 TRACE("iface %p.\n", iface);
209
210 return D3DRTYPE_SURFACE;
211 }
212
213 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
214 {
215 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
216 HRESULT hr;
217
218 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
219
220 if (!surface->container)
221 return E_NOINTERFACE;
222
223 hr = IUnknown_QueryInterface(surface->container, riid, container);
224
225 TRACE("Returning %p.\n", *container);
226
227 return hr;
228 }
229
230 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
231 {
232 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
233 struct wined3d_resource_desc wined3d_desc;
234 struct wined3d_resource *wined3d_resource;
235
236 TRACE("iface %p, desc %p.\n", iface, desc);
237
238 wined3d_mutex_lock();
239 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
240 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
241 wined3d_mutex_unlock();
242
243 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
244 desc->Type = wined3d_desc.resource_type;
245 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
246 desc->Pool = wined3d_desc.pool;
247 desc->MultiSampleType = wined3d_desc.multisample_type;
248 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
249 desc->Width = wined3d_desc.width;
250 desc->Height = wined3d_desc.height;
251
252 return D3D_OK;
253 }
254
255 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
256 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
257 {
258 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
259 struct wined3d_map_desc map_desc;
260 HRESULT hr;
261
262 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
263 iface, locked_rect, wine_dbgstr_rect(rect), flags);
264
265 wined3d_mutex_lock();
266 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
267 wined3d_mutex_unlock();
268
269 if (SUCCEEDED(hr))
270 {
271 locked_rect->Pitch = map_desc.row_pitch;
272 locked_rect->pBits = map_desc.data;
273 }
274
275 return hr;
276 }
277
278 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
279 {
280 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
281 HRESULT hr;
282
283 TRACE("iface %p.\n", iface);
284
285 wined3d_mutex_lock();
286 hr = wined3d_surface_unmap(surface->wined3d_surface);
287 wined3d_mutex_unlock();
288
289 switch(hr)
290 {
291 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
292 default: return hr;
293 }
294 }
295
296 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
297 {
298 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
299 HRESULT hr;
300
301 TRACE("iface %p, dc %p.\n", iface, dc);
302
303 if (!surface->getdc_supported)
304 {
305 WARN("Surface does not support GetDC, returning D3DERR_INVALIDCALL\n");
306 /* Don't touch the DC */
307 return D3DERR_INVALIDCALL;
308 }
309
310 wined3d_mutex_lock();
311 hr = wined3d_surface_getdc(surface->wined3d_surface, dc);
312 wined3d_mutex_unlock();
313
314 return hr;
315 }
316
317 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
318 {
319 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
320 HRESULT hr;
321
322 TRACE("iface %p, dc %p.\n", iface, dc);
323
324 wined3d_mutex_lock();
325 hr = wined3d_surface_releasedc(surface->wined3d_surface, dc);
326 wined3d_mutex_unlock();
327
328 switch (hr)
329 {
330 case WINEDDERR_NODC: return D3DERR_INVALIDCALL;
331 default: return hr;
332 }
333 }
334
335 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
336 {
337 /* IUnknown */
338 d3d9_surface_QueryInterface,
339 d3d9_surface_AddRef,
340 d3d9_surface_Release,
341 /* IDirect3DResource9 */
342 d3d9_surface_GetDevice,
343 d3d9_surface_SetPrivateData,
344 d3d9_surface_GetPrivateData,
345 d3d9_surface_FreePrivateData,
346 d3d9_surface_SetPriority,
347 d3d9_surface_GetPriority,
348 d3d9_surface_PreLoad,
349 d3d9_surface_GetType,
350 /* IDirect3DSurface9 */
351 d3d9_surface_GetContainer,
352 d3d9_surface_GetDesc,
353 d3d9_surface_LockRect,
354 d3d9_surface_UnlockRect,
355 d3d9_surface_GetDC,
356 d3d9_surface_ReleaseDC,
357 };
358
359 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
360 {
361 struct d3d9_surface *surface = parent;
362 d3d9_resource_cleanup(&surface->resource);
363 HeapFree(GetProcessHeap(), 0, surface);
364 }
365
366 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
367 {
368 surface_wined3d_object_destroyed,
369 };
370
371 void surface_init(struct d3d9_surface *surface, struct wined3d_surface *wined3d_surface,
372 struct d3d9_device *device, const struct wined3d_parent_ops **parent_ops)
373 {
374 struct wined3d_resource_desc desc;
375
376 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
377 d3d9_resource_init(&surface->resource);
378
379 wined3d_resource_get_desc(wined3d_surface_get_resource(wined3d_surface), &desc);
380 switch (d3dformat_from_wined3dformat(desc.format))
381 {
382 case D3DFMT_A8R8G8B8:
383 case D3DFMT_X8R8G8B8:
384 case D3DFMT_R5G6B5:
385 case D3DFMT_X1R5G5B5:
386 case D3DFMT_A1R5G5B5:
387 case D3DFMT_R8G8B8:
388 surface->getdc_supported = TRUE;
389 break;
390
391 default:
392 surface->getdc_supported = FALSE;
393 break;
394 }
395
396 wined3d_surface_incref(wined3d_surface);
397 surface->wined3d_surface = wined3d_surface;
398 surface->parent_device = &device->IDirect3DDevice9Ex_iface;
399 IDirect3DDevice9Ex_AddRef(surface->parent_device);
400
401 *parent_ops = &d3d9_surface_wined3d_parent_ops;
402 }
403
404 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
405 {
406 if (!iface)
407 return NULL;
408 assert(iface->lpVtbl == &d3d9_surface_vtbl);
409
410 return impl_from_IDirect3DSurface9(iface);
411 }