8295a36ceeee0f8497f56217177f0468d05240b6
[reactos.git] / reactos / dll / directx / wine / d3d8 / surface.c
1 /*
2 * IDirect3DSurface8 implementation
3 *
4 * Copyright 2005 Oliver Stieber
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "d3d8_private.h"
22
23 static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
24 {
25 return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
26 }
27
28 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
29 {
30 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), out);
31
32 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
33 || IsEqualGUID(riid, &IID_IDirect3DResource8)
34 || IsEqualGUID(riid, &IID_IUnknown))
35 {
36 IDirect3DSurface8_AddRef(iface);
37 *out = iface;
38 return S_OK;
39 }
40
41 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
42
43 *out = NULL;
44 return E_NOINTERFACE;
45 }
46
47 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
48 {
49 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
50
51 TRACE("iface %p.\n", iface);
52
53 if (surface->forwardReference)
54 {
55 /* Forward refcounting */
56 TRACE("Forwarding to %p.\n", surface->forwardReference);
57 return IUnknown_AddRef(surface->forwardReference);
58 }
59 else
60 {
61 /* No container, handle our own refcounting */
62 ULONG ref = InterlockedIncrement(&surface->refcount);
63
64 TRACE("%p increasing refcount to %u.\n", iface, ref);
65
66 if (ref == 1)
67 {
68 if (surface->parent_device)
69 IDirect3DDevice8_AddRef(surface->parent_device);
70 wined3d_mutex_lock();
71 wined3d_surface_incref(surface->wined3d_surface);
72 wined3d_mutex_unlock();
73 }
74
75 return ref;
76 }
77 }
78
79 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
80 {
81 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
82
83 TRACE("iface %p.\n", iface);
84
85 if (surface->forwardReference)
86 {
87 /* Forward refcounting */
88 TRACE("Forwarding to %p.\n", surface->forwardReference);
89 return IUnknown_Release(surface->forwardReference);
90 }
91 else
92 {
93 /* No container, handle our own refcounting */
94 ULONG ref = InterlockedDecrement(&surface->refcount);
95
96 TRACE("%p decreasing refcount to %u.\n", iface, ref);
97
98 if (!ref)
99 {
100 IDirect3DDevice8 *parent_device = surface->parent_device;
101
102 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
103 wined3d_mutex_lock();
104 wined3d_surface_decref(surface->wined3d_surface);
105 wined3d_mutex_unlock();
106
107 if (parent_device)
108 IDirect3DDevice8_Release(parent_device);
109 }
110
111 return ref;
112 }
113 }
114
115 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
116 {
117 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
118
119 TRACE("iface %p, device %p.\n", iface, device);
120
121 if (surface->forwardReference)
122 {
123 IDirect3DResource8 *resource;
124 HRESULT hr;
125
126 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
127 if (SUCCEEDED(hr))
128 {
129 hr = IDirect3DResource8_GetDevice(resource, device);
130 IDirect3DResource8_Release(resource);
131
132 TRACE("Returning device %p.\n", *device);
133 }
134
135 return hr;
136 }
137
138 *device = surface->parent_device;
139 IDirect3DDevice8_AddRef(*device);
140
141 TRACE("Returning device %p.\n", *device);
142
143 return D3D_OK;
144 }
145
146 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
147 const void *data, DWORD data_size, DWORD flags)
148 {
149 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
150 struct wined3d_resource *resource;
151 HRESULT hr;
152
153 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
154 iface, debugstr_guid(guid), data, data_size, flags);
155
156 wined3d_mutex_lock();
157 resource = wined3d_surface_get_resource(surface->wined3d_surface);
158 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
159 wined3d_mutex_unlock();
160
161 return hr;
162 }
163
164 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
165 void *data, DWORD *data_size)
166 {
167 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
168 struct wined3d_resource *resource;
169 HRESULT hr;
170
171 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
172 iface, debugstr_guid(guid), data, data_size);
173
174 wined3d_mutex_lock();
175 resource = wined3d_surface_get_resource(surface->wined3d_surface);
176 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
177 wined3d_mutex_unlock();
178
179 return hr;
180 }
181
182 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
183 {
184 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
185 struct wined3d_resource *resource;
186 HRESULT hr;
187
188 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
189
190 wined3d_mutex_lock();
191 resource = wined3d_surface_get_resource(surface->wined3d_surface);
192 hr = wined3d_resource_free_private_data(resource, guid);
193 wined3d_mutex_unlock();
194
195 return hr;
196 }
197
198 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
199 {
200 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
201 HRESULT hr;
202
203 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
204
205 if (!surface->container)
206 return E_NOINTERFACE;
207
208 hr = IUnknown_QueryInterface(surface->container, riid, container);
209
210 TRACE("Returning %p.\n", *container);
211
212 return hr;
213 }
214
215 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
216 {
217 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
218 struct wined3d_resource_desc wined3d_desc;
219 struct wined3d_resource *wined3d_resource;
220
221 TRACE("iface %p, desc %p.\n", iface, desc);
222
223 wined3d_mutex_lock();
224 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
225 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
226 wined3d_mutex_unlock();
227
228 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
229 desc->Type = wined3d_desc.resource_type;
230 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
231 desc->Pool = wined3d_desc.pool;
232 desc->Size = wined3d_desc.size;
233 desc->MultiSampleType = wined3d_desc.multisample_type;
234 desc->Width = wined3d_desc.width;
235 desc->Height = wined3d_desc.height;
236
237 return D3D_OK;
238 }
239
240 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
241 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
242 {
243 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
244 struct wined3d_map_desc map_desc;
245 HRESULT hr;
246
247 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
248 iface, locked_rect, wine_dbgstr_rect(rect), flags);
249
250 wined3d_mutex_lock();
251 if (rect)
252 {
253 D3DSURFACE_DESC desc;
254 IDirect3DSurface8_GetDesc(iface, &desc);
255
256 if ((rect->left < 0)
257 || (rect->top < 0)
258 || (rect->left >= rect->right)
259 || (rect->top >= rect->bottom)
260 || (rect->right > desc.Width)
261 || (rect->bottom > desc.Height))
262 {
263 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
264 wined3d_mutex_unlock();
265
266 return D3DERR_INVALIDCALL;
267 }
268 }
269
270 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
271 wined3d_mutex_unlock();
272
273 locked_rect->Pitch = map_desc.row_pitch;
274 locked_rect->pBits = map_desc.data;
275
276 return hr;
277 }
278
279 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
280 {
281 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
282 HRESULT hr;
283
284 TRACE("iface %p.\n", iface);
285
286 wined3d_mutex_lock();
287 hr = wined3d_surface_unmap(surface->wined3d_surface);
288 wined3d_mutex_unlock();
289
290 switch(hr)
291 {
292 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
293 default: return hr;
294 }
295 }
296
297 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
298 {
299 /* IUnknown */
300 d3d8_surface_QueryInterface,
301 d3d8_surface_AddRef,
302 d3d8_surface_Release,
303 /* IDirect3DResource8 */
304 d3d8_surface_GetDevice,
305 d3d8_surface_SetPrivateData,
306 d3d8_surface_GetPrivateData,
307 d3d8_surface_FreePrivateData,
308 /* IDirect3DSurface8 */
309 d3d8_surface_GetContainer,
310 d3d8_surface_GetDesc,
311 d3d8_surface_LockRect,
312 d3d8_surface_UnlockRect,
313 };
314
315 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
316 {
317 HeapFree(GetProcessHeap(), 0, parent);
318 }
319
320 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
321 {
322 surface_wined3d_object_destroyed,
323 };
324
325 HRESULT surface_init(struct d3d8_surface *surface, struct d3d8_device *device, UINT width, UINT height,
326 D3DFORMAT format, DWORD flags, DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type,
327 DWORD multisample_quality)
328 {
329 HRESULT hr;
330
331 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
332 surface->refcount = 1;
333
334 /* FIXME: Check MAX bounds of MultisampleQuality. */
335 if (multisample_quality > 0)
336 {
337 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
338 multisample_quality = 0;
339 }
340
341 wined3d_mutex_lock();
342 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
343 usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
344 flags, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
345 wined3d_mutex_unlock();
346 if (FAILED(hr))
347 {
348 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
349 return hr;
350 }
351
352 surface->parent_device = &device->IDirect3DDevice8_iface;
353 IDirect3DDevice8_AddRef(surface->parent_device);
354
355 return D3D_OK;
356 }
357
358 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
359 {
360 if (!iface)
361 return NULL;
362 assert(iface->lpVtbl == &d3d8_surface_vtbl);
363
364 return impl_from_IDirect3DSurface8(iface);
365 }