usp10: Fall back to 'dflt' language if shaping language tag isn't found.
[wine.git] / dlls / d3d9 / surface.c
blob3c01efc5891bdfef67ddf0c622654838ace01e53
1 /*
2 * IDirect3DSurface9 implementation
4 * Copyright 2002-2005 Jason Edmeades
5 * Raphael Junqueira
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.
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.
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
22 #include "config.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 static inline struct d3d9_surface *impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
29 return CONTAINING_RECORD(iface, struct d3d9_surface, IDirect3DSurface9_iface);
32 static HRESULT WINAPI d3d9_surface_QueryInterface(IDirect3DSurface9 *iface, REFIID riid, void **out)
34 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
36 if (IsEqualGUID(riid, &IID_IDirect3DSurface9)
37 || IsEqualGUID(riid, &IID_IDirect3DResource9)
38 || IsEqualGUID(riid, &IID_IUnknown))
40 IDirect3DSurface9_AddRef(iface);
41 *out = iface;
42 return S_OK;
45 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
47 *out = NULL;
48 return E_NOINTERFACE;
51 static ULONG WINAPI d3d9_surface_AddRef(IDirect3DSurface9 *iface)
53 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
54 ULONG refcount;
56 TRACE("iface %p.\n", iface);
58 if (surface->texture)
60 TRACE("Forwarding to %p.\n", surface->texture);
61 return IDirect3DBaseTexture9_AddRef(&surface->texture->IDirect3DBaseTexture9_iface);
64 refcount = InterlockedIncrement(&surface->resource.refcount);
65 TRACE("%p increasing refcount to %u.\n", iface, refcount);
67 if (refcount == 1)
69 if (surface->parent_device)
70 IDirect3DDevice9Ex_AddRef(surface->parent_device);
71 wined3d_mutex_lock();
72 if (surface->wined3d_rtv)
73 wined3d_rendertarget_view_incref(surface->wined3d_rtv);
74 wined3d_texture_incref(surface->wined3d_texture);
75 wined3d_mutex_unlock();
78 return refcount;
81 static ULONG WINAPI d3d9_surface_Release(IDirect3DSurface9 *iface)
83 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
84 ULONG refcount;
86 TRACE("iface %p.\n", iface);
88 if (surface->texture)
90 TRACE("Forwarding to %p.\n", surface->texture);
91 return IDirect3DBaseTexture9_Release(&surface->texture->IDirect3DBaseTexture9_iface);
94 if (!surface->resource.refcount)
96 WARN("Surface does not have any references.\n");
97 return 0;
100 refcount = InterlockedDecrement(&surface->resource.refcount);
101 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
103 if (!refcount)
105 IDirect3DDevice9Ex *parent_device = surface->parent_device;
107 wined3d_mutex_lock();
108 if (surface->wined3d_rtv)
109 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
110 wined3d_texture_decref(surface->wined3d_texture);
111 wined3d_mutex_unlock();
113 /* Release the device last, as it may cause the device to be destroyed. */
114 if (parent_device)
115 IDirect3DDevice9Ex_Release(parent_device);
118 return refcount;
121 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
123 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
125 TRACE("iface %p, device %p.\n", iface, device);
127 if (surface->texture)
128 return IDirect3DBaseTexture9_GetDevice(&surface->texture->IDirect3DBaseTexture9_iface, device);
130 *device = (IDirect3DDevice9 *)surface->parent_device;
131 IDirect3DDevice9_AddRef(*device);
133 TRACE("Returning device %p.\n", *device);
135 return D3D_OK;
138 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
139 const void *data, DWORD data_size, DWORD flags)
141 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
142 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
143 iface, debugstr_guid(guid), data, data_size, flags);
145 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
148 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
149 void *data, DWORD *data_size)
151 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
152 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
153 iface, debugstr_guid(guid), data, data_size);
155 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
158 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
160 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
161 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
163 return d3d9_resource_free_private_data(&surface->resource, guid);
166 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
168 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface, priority);
169 return 0;
172 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
174 TRACE("iface %p. Ignored on surfaces.\n", iface);
175 return 0;
178 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
180 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
182 TRACE("iface %p.\n", iface);
184 wined3d_mutex_lock();
185 wined3d_resource_preload(wined3d_texture_get_resource(surface->wined3d_texture));
186 wined3d_mutex_unlock();
189 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
191 TRACE("iface %p.\n", iface);
193 return D3DRTYPE_SURFACE;
196 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
198 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
199 HRESULT hr;
201 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
203 if (!surface->container)
204 return E_NOINTERFACE;
206 hr = IUnknown_QueryInterface(surface->container, riid, container);
208 TRACE("Returning %p.\n", *container);
210 return hr;
213 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
215 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
216 struct wined3d_sub_resource_desc wined3d_desc;
218 TRACE("iface %p, desc %p.\n", iface, desc);
220 wined3d_mutex_lock();
221 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
222 wined3d_mutex_unlock();
224 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
225 desc->Type = D3DRTYPE_SURFACE;
226 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
227 desc->Pool = wined3d_desc.pool;
228 desc->MultiSampleType = wined3d_desc.multisample_type;
229 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
230 desc->Width = wined3d_desc.width;
231 desc->Height = wined3d_desc.height;
233 return D3D_OK;
236 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
237 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
239 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
240 struct wined3d_box box;
241 struct wined3d_map_desc map_desc;
242 HRESULT hr;
244 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
245 iface, locked_rect, wine_dbgstr_rect(rect), flags);
247 if (rect)
248 wined3d_box_set(&box, rect->left, rect->top, rect->right, rect->bottom, 0, 1);
250 wined3d_mutex_lock();
251 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
252 &map_desc, rect ? &box : NULL, flags);
253 wined3d_mutex_unlock();
255 if (SUCCEEDED(hr))
257 locked_rect->Pitch = map_desc.row_pitch;
258 locked_rect->pBits = map_desc.data;
261 return hr;
264 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
266 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
267 HRESULT hr;
269 TRACE("iface %p.\n", iface);
271 wined3d_mutex_lock();
272 hr = wined3d_resource_unmap(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx);
273 wined3d_mutex_unlock();
275 if (hr == WINEDDERR_NOTLOCKED)
277 D3DRESOURCETYPE type;
278 if (surface->texture)
279 type = IDirect3DBaseTexture9_GetType(&surface->texture->IDirect3DBaseTexture9_iface);
280 else
281 type = D3DRTYPE_SURFACE;
282 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
284 return hr;
287 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
289 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
290 HRESULT hr;
292 TRACE("iface %p, dc %p.\n", iface, dc);
294 wined3d_mutex_lock();
295 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
296 wined3d_mutex_unlock();
298 return hr;
301 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
303 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
304 HRESULT hr;
306 TRACE("iface %p, dc %p.\n", iface, dc);
308 wined3d_mutex_lock();
309 hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
310 wined3d_mutex_unlock();
312 return hr;
315 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
317 /* IUnknown */
318 d3d9_surface_QueryInterface,
319 d3d9_surface_AddRef,
320 d3d9_surface_Release,
321 /* IDirect3DResource9 */
322 d3d9_surface_GetDevice,
323 d3d9_surface_SetPrivateData,
324 d3d9_surface_GetPrivateData,
325 d3d9_surface_FreePrivateData,
326 d3d9_surface_SetPriority,
327 d3d9_surface_GetPriority,
328 d3d9_surface_PreLoad,
329 d3d9_surface_GetType,
330 /* IDirect3DSurface9 */
331 d3d9_surface_GetContainer,
332 d3d9_surface_GetDesc,
333 d3d9_surface_LockRect,
334 d3d9_surface_UnlockRect,
335 d3d9_surface_GetDC,
336 d3d9_surface_ReleaseDC,
339 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
341 struct d3d9_surface *surface = parent;
342 d3d9_resource_cleanup(&surface->resource);
343 HeapFree(GetProcessHeap(), 0, surface);
346 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
348 surface_wined3d_object_destroyed,
351 void surface_init(struct d3d9_surface *surface, struct wined3d_texture *wined3d_texture,
352 unsigned int sub_resource_idx, const struct wined3d_parent_ops **parent_ops)
354 IDirect3DBaseTexture9 *texture;
356 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
357 d3d9_resource_init(&surface->resource);
358 surface->resource.refcount = 0;
359 list_init(&surface->rtv_entry);
360 surface->container = wined3d_texture_get_parent(wined3d_texture);
361 surface->wined3d_texture = wined3d_texture;
362 surface->sub_resource_idx = sub_resource_idx;
364 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
365 &IID_IDirect3DBaseTexture9, (void **)&texture)))
367 surface->texture = unsafe_impl_from_IDirect3DBaseTexture9(texture);
368 IDirect3DBaseTexture9_Release(texture);
371 *parent_ops = &d3d9_surface_wined3d_parent_ops;
374 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
376 struct d3d9_surface *surface = parent;
378 /* If the surface reference count drops to zero, we release our reference
379 * to the view, but don't clear the pointer yet, in case e.g. a
380 * GetRenderTarget() call brings the surface back before the view is
381 * actually destroyed. When the view is destroyed, we need to clear the
382 * pointer, or a subsequent surface AddRef() would reference it again.
384 * This is safe because as long as the view still has a reference to the
385 * texture, the surface is also still alive, and we're called before the
386 * view releases that reference. */
387 surface->wined3d_rtv = NULL;
388 list_remove(&surface->rtv_entry);
391 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops =
393 view_wined3d_object_destroyed,
396 struct d3d9_device *d3d9_surface_get_device(const struct d3d9_surface *surface)
398 IDirect3DDevice9Ex *device;
399 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
400 return impl_from_IDirect3DDevice9Ex(device);
403 struct wined3d_rendertarget_view *d3d9_surface_acquire_rendertarget_view(struct d3d9_surface *surface)
405 HRESULT hr;
407 /* The surface reference count can be equal to 0 when this function is
408 * called. In order to properly manage the render target view reference
409 * count, we temporarily increment the surface reference count. */
410 d3d9_surface_AddRef(&surface->IDirect3DSurface9_iface);
412 if (surface->wined3d_rtv)
413 return surface->wined3d_rtv;
415 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
416 surface->sub_resource_idx, surface, &d3d9_view_wined3d_parent_ops, &surface->wined3d_rtv)))
418 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
419 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
420 return NULL;
423 if (surface->texture)
424 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
426 return surface->wined3d_rtv;
429 void d3d9_surface_release_rendertarget_view(struct d3d9_surface *surface,
430 struct wined3d_rendertarget_view *rtv)
432 if (rtv)
433 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
436 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
438 if (!iface)
439 return NULL;
440 assert(iface->lpVtbl == &d3d9_surface_vtbl);
442 return impl_from_IDirect3DSurface9(iface);