riched20: Use cell ptrs in the rtf parsing code.
[wine/zf.git] / dlls / d3d8 / surface.c
blobc3d28efba2041eb6ce98a92a66423787790ea2df
1 /*
2 * IDirect3DSurface8 implementation
4 * Copyright 2005 Oliver Stieber
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.
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.
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
21 #include "d3d8_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25 static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
27 return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
30 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
32 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
34 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
35 || IsEqualGUID(riid, &IID_IDirect3DResource8)
36 || IsEqualGUID(riid, &IID_IUnknown))
38 IDirect3DSurface8_AddRef(iface);
39 *out = iface;
40 return S_OK;
43 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
45 *out = NULL;
46 return E_NOINTERFACE;
49 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
51 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
52 ULONG refcount;
54 TRACE("iface %p.\n", iface);
56 if (surface->texture)
58 TRACE("Forwarding to %p.\n", surface->texture);
59 return IDirect3DBaseTexture8_AddRef(&surface->texture->IDirect3DBaseTexture8_iface);
62 refcount = InterlockedIncrement(&surface->resource.refcount);
63 TRACE("%p increasing refcount to %u.\n", iface, refcount);
65 if (refcount == 1)
67 if (surface->parent_device)
68 IDirect3DDevice8_AddRef(surface->parent_device);
69 wined3d_mutex_lock();
70 if (surface->wined3d_rtv)
71 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
72 wined3d_texture_incref(surface->wined3d_texture);
73 wined3d_mutex_unlock();
76 return refcount;
79 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
81 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
82 ULONG refcount;
84 TRACE("iface %p.\n", iface);
86 if (surface->texture)
88 TRACE("Forwarding to %p.\n", surface->texture);
89 return IDirect3DBaseTexture8_Release(&surface->texture->IDirect3DBaseTexture8_iface);
92 if (!surface->resource.refcount)
94 WARN("Surface does not have any references.\n");
95 return 0;
98 refcount = InterlockedDecrement(&surface->resource.refcount);
99 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
101 if (!refcount)
103 IDirect3DDevice8 *parent_device = surface->parent_device;
105 wined3d_mutex_lock();
106 if (surface->wined3d_rtv)
107 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
108 wined3d_texture_decref(surface->wined3d_texture);
109 wined3d_mutex_unlock();
111 if (parent_device)
112 IDirect3DDevice8_Release(parent_device);
115 return refcount;
118 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
120 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
122 TRACE("iface %p, device %p.\n", iface, device);
124 if (surface->texture)
125 return IDirect3DBaseTexture8_GetDevice(&surface->texture->IDirect3DBaseTexture8_iface, device);
127 *device = surface->parent_device;
128 IDirect3DDevice8_AddRef(*device);
130 TRACE("Returning device %p.\n", *device);
132 return D3D_OK;
135 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
136 const void *data, DWORD data_size, DWORD flags)
138 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
139 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
140 iface, debugstr_guid(guid), data, data_size, flags);
142 return d3d8_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
145 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
146 void *data, DWORD *data_size)
148 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
149 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
150 iface, debugstr_guid(guid), data, data_size);
152 return d3d8_resource_get_private_data(&surface->resource, guid, data, data_size);
155 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
157 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
158 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
160 return d3d8_resource_free_private_data(&surface->resource, guid);
163 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
165 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
166 HRESULT hr;
168 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
170 if (!surface->container)
171 return E_NOINTERFACE;
173 hr = IUnknown_QueryInterface(surface->container, riid, container);
175 TRACE("Returning %p.\n", *container);
177 return hr;
180 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
182 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
183 struct wined3d_sub_resource_desc wined3d_desc;
185 TRACE("iface %p, desc %p.\n", iface, desc);
187 wined3d_mutex_lock();
188 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
189 wined3d_mutex_unlock();
191 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
192 desc->Type = D3DRTYPE_SURFACE;
193 desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
194 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
195 desc->Size = wined3d_desc.size;
196 desc->MultiSampleType = d3dmultisample_type_from_wined3d(wined3d_desc.multisample_type);
197 desc->Width = wined3d_desc.width;
198 desc->Height = wined3d_desc.height;
200 return D3D_OK;
203 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
204 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
206 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
207 struct wined3d_box box;
208 struct wined3d_map_desc map_desc;
209 HRESULT hr;
210 D3DRESOURCETYPE type;
212 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
213 iface, locked_rect, wine_dbgstr_rect(rect), flags);
215 wined3d_mutex_lock();
217 if (surface->texture)
218 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
219 else
220 type = D3DRTYPE_SURFACE;
222 if (rect)
224 D3DSURFACE_DESC desc;
225 IDirect3DSurface8_GetDesc(iface, &desc);
227 if (type != D3DRTYPE_TEXTURE
228 && ((rect->left < 0)
229 || (rect->top < 0)
230 || (rect->left >= rect->right)
231 || (rect->top >= rect->bottom)
232 || (rect->right > desc.Width)
233 || (rect->bottom > desc.Height)))
235 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
236 wined3d_mutex_unlock();
238 locked_rect->Pitch = 0;
239 locked_rect->pBits = NULL;
240 return D3DERR_INVALIDCALL;
242 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
245 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
246 &map_desc, rect ? &box : NULL, wined3dmapflags_from_d3dmapflags(flags, 0));
247 wined3d_mutex_unlock();
249 if (SUCCEEDED(hr))
251 locked_rect->Pitch = map_desc.row_pitch;
252 locked_rect->pBits = map_desc.data;
254 else if (type != D3DRTYPE_TEXTURE)
256 locked_rect->Pitch = 0;
257 locked_rect->pBits = NULL;
260 if (hr == E_INVALIDARG)
261 return D3DERR_INVALIDCALL;
262 return hr;
265 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
267 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
268 HRESULT hr;
270 TRACE("iface %p.\n", iface);
272 wined3d_mutex_lock();
273 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
274 wined3d_mutex_unlock();
276 if (hr == WINEDDERR_NOTLOCKED)
278 D3DRESOURCETYPE type;
279 if (surface->texture)
280 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
281 else
282 type = D3DRTYPE_SURFACE;
283 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
285 return hr;
288 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
290 /* IUnknown */
291 d3d8_surface_QueryInterface,
292 d3d8_surface_AddRef,
293 d3d8_surface_Release,
294 /* IDirect3DResource8 */
295 d3d8_surface_GetDevice,
296 d3d8_surface_SetPrivateData,
297 d3d8_surface_GetPrivateData,
298 d3d8_surface_FreePrivateData,
299 /* IDirect3DSurface8 */
300 d3d8_surface_GetContainer,
301 d3d8_surface_GetDesc,
302 d3d8_surface_LockRect,
303 d3d8_surface_UnlockRect,
306 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
308 struct d3d8_surface *surface = parent;
309 d3d8_resource_cleanup(&surface->resource);
310 heap_free(surface);
313 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
315 surface_wined3d_object_destroyed,
318 void surface_init(struct d3d8_surface *surface, struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
319 const struct wined3d_parent_ops **parent_ops)
321 IDirect3DBaseTexture8 *texture;
323 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
324 d3d8_resource_init(&surface->resource);
325 surface->resource.refcount = 0;
326 list_init(&surface->rtv_entry);
327 surface->container = wined3d_texture_get_parent(wined3d_texture);
328 surface->wined3d_texture = wined3d_texture;
329 surface->sub_resource_idx = sub_resource_idx;
331 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
332 &IID_IDirect3DBaseTexture8, (void **)&texture)))
334 surface->texture = unsafe_impl_from_IDirect3DBaseTexture8(texture);
335 IDirect3DBaseTexture8_Release(texture);
338 *parent_ops = &d3d8_surface_wined3d_parent_ops;
341 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
343 struct d3d8_surface *surface = parent;
345 /* If the surface reference count drops to zero, we release our reference
346 * to the view, but don't clear the pointer yet, in case e.g. a
347 * GetRenderTarget() call brings the surface back before the view is
348 * actually destroyed. When the view is destroyed, we need to clear the
349 * pointer, or a subsequent surface AddRef() would reference it again.
351 * This is safe because as long as the view still has a reference to the
352 * texture, the surface is also still alive, and we're called before the
353 * view releases that reference. */
354 surface->wined3d_rtv = NULL;
355 list_remove(&surface->rtv_entry);
358 static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops =
360 view_wined3d_object_destroyed,
363 struct d3d8_device *d3d8_surface_get_device(const struct d3d8_surface *surface)
365 IDirect3DDevice8 *device;
366 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
367 return impl_from_IDirect3DDevice8(device);
370 struct wined3d_rendertarget_view *d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface)
372 HRESULT hr;
374 /* The surface reference count can be equal to 0 when this function is
375 * called. In order to properly manage the render target view reference
376 * count, we temporarily increment the surface reference count. */
377 d3d8_surface_AddRef(&surface->IDirect3DSurface8_iface);
379 if (surface->wined3d_rtv)
380 return surface->wined3d_rtv;
382 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
383 surface->sub_resource_idx, surface, &d3d8_view_wined3d_parent_ops, &surface->wined3d_rtv)))
385 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
386 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
387 return NULL;
390 if (surface->texture)
391 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
393 return surface->wined3d_rtv;
396 void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface,
397 struct wined3d_rendertarget_view *rtv)
399 if (rtv)
400 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
403 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
405 if (!iface)
406 return NULL;
407 assert(iface->lpVtbl == &d3d8_surface_vtbl);
409 return impl_from_IDirect3DSurface8(iface);