d3d10/effect: Add support for 'imul' instruction.
[wine.git] / dlls / d3d8 / surface.c
blob37d225ede70295f8a7182df48b243ad5dd44bdfe
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 %lu.\n", iface, refcount);
65 if (refcount == 1)
67 if (surface->parent_device)
68 IDirect3DDevice8_AddRef(surface->parent_device);
69 if (surface->wined3d_rtv)
70 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
71 wined3d_texture_incref(surface->wined3d_texture);
72 if (surface->swapchain)
73 wined3d_swapchain_incref(surface->swapchain);
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 %lu.\n", iface, refcount);
101 if (!refcount)
103 IDirect3DDevice8 *parent_device = surface->parent_device;
105 if (surface->wined3d_rtv)
106 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
107 if (surface->swapchain)
108 wined3d_swapchain_decref(surface->swapchain);
109 /* Releasing the texture may free the d3d8 object, so do not access it
110 * after releasing the texture. */
111 wined3d_texture_decref(surface->wined3d_texture);
113 if (parent_device)
114 IDirect3DDevice8_Release(parent_device);
117 return refcount;
120 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
122 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
124 TRACE("iface %p, device %p.\n", iface, device);
126 if (surface->texture)
127 return IDirect3DBaseTexture8_GetDevice(&surface->texture->IDirect3DBaseTexture8_iface, device);
129 *device = surface->parent_device;
130 IDirect3DDevice8_AddRef(*device);
132 TRACE("Returning device %p.\n", *device);
134 return D3D_OK;
137 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
138 const void *data, DWORD data_size, DWORD flags)
140 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
141 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
142 iface, debugstr_guid(guid), data, data_size, flags);
144 return d3d8_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
147 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
148 void *data, DWORD *data_size)
150 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
151 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
152 iface, debugstr_guid(guid), data, data_size);
154 return d3d8_resource_get_private_data(&surface->resource, guid, data, data_size);
157 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
159 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
160 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
162 return d3d8_resource_free_private_data(&surface->resource, guid);
165 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
167 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
168 HRESULT hr;
170 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
172 if (!surface->container)
173 return E_NOINTERFACE;
175 hr = IUnknown_QueryInterface(surface->container, riid, container);
177 TRACE("Returning %p.\n", *container);
179 return hr;
182 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
184 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
185 struct wined3d_sub_resource_desc wined3d_desc;
187 TRACE("iface %p, desc %p.\n", iface, desc);
189 wined3d_mutex_lock();
190 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
191 wined3d_mutex_unlock();
193 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
194 desc->Type = D3DRTYPE_SURFACE;
195 desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
196 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
197 desc->Size = wined3d_desc.size;
198 desc->MultiSampleType = d3dmultisample_type_from_wined3d(wined3d_desc.multisample_type);
199 desc->Width = wined3d_desc.width;
200 desc->Height = wined3d_desc.height;
202 return D3D_OK;
205 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
206 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
208 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
209 struct wined3d_box box;
210 struct wined3d_map_desc map_desc;
211 HRESULT hr;
212 D3DRESOURCETYPE type;
214 TRACE("iface %p, locked_rect %p, rect %s, flags %#lx.\n",
215 iface, locked_rect, wine_dbgstr_rect(rect), flags);
217 wined3d_mutex_lock();
219 if (surface->texture)
220 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
221 else
222 type = D3DRTYPE_SURFACE;
224 if (rect)
226 D3DSURFACE_DESC desc;
227 IDirect3DSurface8_GetDesc(iface, &desc);
229 if (type != D3DRTYPE_TEXTURE
230 && ((rect->left < 0)
231 || (rect->top < 0)
232 || (rect->left >= rect->right)
233 || (rect->top >= rect->bottom)
234 || (rect->right > desc.Width)
235 || (rect->bottom > desc.Height)))
237 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
238 wined3d_mutex_unlock();
240 locked_rect->Pitch = 0;
241 locked_rect->pBits = NULL;
242 return D3DERR_INVALIDCALL;
244 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
247 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
248 &map_desc, rect ? &box : NULL, wined3dmapflags_from_d3dmapflags(flags, 0));
249 wined3d_mutex_unlock();
251 if (SUCCEEDED(hr))
253 locked_rect->Pitch = map_desc.row_pitch;
254 locked_rect->pBits = map_desc.data;
256 else if (type != D3DRTYPE_TEXTURE)
258 locked_rect->Pitch = 0;
259 locked_rect->pBits = NULL;
262 if (hr == E_INVALIDARG)
263 return D3DERR_INVALIDCALL;
264 return hr;
267 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
269 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
270 HRESULT hr;
272 TRACE("iface %p.\n", iface);
274 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
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 struct d3d8_surface *d3d8_surface_create(struct wined3d_texture *wined3d_texture,
319 unsigned int sub_resource_idx, IUnknown *container)
321 IDirect3DBaseTexture8 *texture;
322 struct d3d8_surface *surface;
324 if (!(surface = heap_alloc_zero(sizeof(*surface))))
325 return NULL;
327 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
328 d3d8_resource_init(&surface->resource);
329 surface->resource.refcount = 0;
330 list_init(&surface->rtv_entry);
331 surface->container = container;
332 surface->wined3d_texture = wined3d_texture;
333 surface->sub_resource_idx = sub_resource_idx;
334 surface->swapchain = wined3d_texture_get_swapchain(wined3d_texture);
336 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
337 &IID_IDirect3DBaseTexture8, (void **)&texture)))
339 surface->texture = unsafe_impl_from_IDirect3DBaseTexture8(texture);
340 IDirect3DBaseTexture8_Release(texture);
343 wined3d_texture_set_sub_resource_parent(wined3d_texture, sub_resource_idx,
344 surface, &d3d8_surface_wined3d_parent_ops);
346 TRACE("Created surface %p.\n", surface);
347 return surface;
350 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
352 struct d3d8_surface *surface = parent;
354 /* If the surface reference count drops to zero, we release our reference
355 * to the view, but don't clear the pointer yet, in case e.g. a
356 * GetRenderTarget() call brings the surface back before the view is
357 * actually destroyed. When the view is destroyed, we need to clear the
358 * pointer, or a subsequent surface AddRef() would reference it again.
360 * This is safe because as long as the view still has a reference to the
361 * texture, the surface is also still alive, and we're called before the
362 * view releases that reference. */
363 surface->wined3d_rtv = NULL;
364 list_remove(&surface->rtv_entry);
367 static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops =
369 view_wined3d_object_destroyed,
372 struct d3d8_device *d3d8_surface_get_device(const struct d3d8_surface *surface)
374 IDirect3DDevice8 *device;
375 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
376 return impl_from_IDirect3DDevice8(device);
379 struct wined3d_rendertarget_view *d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface)
381 HRESULT hr;
383 /* The surface reference count can be equal to 0 when this function is
384 * called. In order to properly manage the render target view reference
385 * count, we temporarily increment the surface reference count. */
386 d3d8_surface_AddRef(&surface->IDirect3DSurface8_iface);
388 if (surface->wined3d_rtv)
389 return surface->wined3d_rtv;
391 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
392 surface->sub_resource_idx, surface, &d3d8_view_wined3d_parent_ops, &surface->wined3d_rtv)))
394 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
395 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
396 return NULL;
399 if (surface->texture)
400 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
402 return surface->wined3d_rtv;
405 void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface,
406 struct wined3d_rendertarget_view *rtv)
408 if (rtv)
409 d3d8_surface_Release(&surface->IDirect3DSurface8_iface);
412 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
414 if (!iface)
415 return NULL;
416 assert(iface->lpVtbl == &d3d8_surface_vtbl);
418 return impl_from_IDirect3DSurface8(iface);