d3d9: Return D3D_OK from UnlockRect() for not locked textures.
[wine.git] / dlls / d3d9 / surface.c
blob07eaf7b7daf5fd00be4480dd842d0e1a38ae7276
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 refcount = InterlockedDecrement(&surface->resource.refcount);
95 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
97 if (!refcount)
99 IDirect3DDevice9Ex *parent_device = surface->parent_device;
101 wined3d_mutex_lock();
102 if (surface->wined3d_rtv)
103 wined3d_rendertarget_view_decref(surface->wined3d_rtv);
104 wined3d_texture_decref(surface->wined3d_texture);
105 wined3d_mutex_unlock();
107 /* Release the device last, as it may cause the device to be destroyed. */
108 if (parent_device)
109 IDirect3DDevice9Ex_Release(parent_device);
112 return refcount;
115 static HRESULT WINAPI d3d9_surface_GetDevice(IDirect3DSurface9 *iface, IDirect3DDevice9 **device)
117 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
119 TRACE("iface %p, device %p.\n", iface, device);
121 if (surface->texture)
122 return IDirect3DBaseTexture9_GetDevice(&surface->texture->IDirect3DBaseTexture9_iface, device);
124 *device = (IDirect3DDevice9 *)surface->parent_device;
125 IDirect3DDevice9_AddRef(*device);
127 TRACE("Returning device %p.\n", *device);
129 return D3D_OK;
132 static HRESULT WINAPI d3d9_surface_SetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
133 const void *data, DWORD data_size, DWORD flags)
135 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
136 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
137 iface, debugstr_guid(guid), data, data_size, flags);
139 return d3d9_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
142 static HRESULT WINAPI d3d9_surface_GetPrivateData(IDirect3DSurface9 *iface, REFGUID guid,
143 void *data, DWORD *data_size)
145 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
146 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
147 iface, debugstr_guid(guid), data, data_size);
149 return d3d9_resource_get_private_data(&surface->resource, guid, data, data_size);
152 static HRESULT WINAPI d3d9_surface_FreePrivateData(IDirect3DSurface9 *iface, REFGUID guid)
154 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
155 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
157 return d3d9_resource_free_private_data(&surface->resource, guid);
160 static DWORD WINAPI d3d9_surface_SetPriority(IDirect3DSurface9 *iface, DWORD priority)
162 TRACE("iface %p, priority %u. Ignored on surfaces.\n", iface, priority);
163 return 0;
166 static DWORD WINAPI d3d9_surface_GetPriority(IDirect3DSurface9 *iface)
168 TRACE("iface %p. Ignored on surfaces.\n", iface);
169 return 0;
172 static void WINAPI d3d9_surface_PreLoad(IDirect3DSurface9 *iface)
174 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
176 TRACE("iface %p.\n", iface);
178 wined3d_mutex_lock();
179 wined3d_resource_preload(wined3d_texture_get_resource(surface->wined3d_texture));
180 wined3d_mutex_unlock();
183 static D3DRESOURCETYPE WINAPI d3d9_surface_GetType(IDirect3DSurface9 *iface)
185 TRACE("iface %p.\n", iface);
187 return D3DRTYPE_SURFACE;
190 static HRESULT WINAPI d3d9_surface_GetContainer(IDirect3DSurface9 *iface, REFIID riid, void **container)
192 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
193 HRESULT hr;
195 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
197 if (!surface->container)
198 return E_NOINTERFACE;
200 hr = IUnknown_QueryInterface(surface->container, riid, container);
202 TRACE("Returning %p.\n", *container);
204 return hr;
207 static HRESULT WINAPI d3d9_surface_GetDesc(IDirect3DSurface9 *iface, D3DSURFACE_DESC *desc)
209 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
210 struct wined3d_sub_resource_desc wined3d_desc;
212 TRACE("iface %p, desc %p.\n", iface, desc);
214 wined3d_mutex_lock();
215 wined3d_texture_get_sub_resource_desc(surface->wined3d_texture, surface->sub_resource_idx, &wined3d_desc);
216 wined3d_mutex_unlock();
218 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
219 desc->Type = D3DRTYPE_SURFACE;
220 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
221 desc->Pool = wined3d_desc.pool;
222 desc->MultiSampleType = wined3d_desc.multisample_type;
223 desc->MultiSampleQuality = wined3d_desc.multisample_quality;
224 desc->Width = wined3d_desc.width;
225 desc->Height = wined3d_desc.height;
227 return D3D_OK;
230 static HRESULT WINAPI d3d9_surface_LockRect(IDirect3DSurface9 *iface,
231 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
233 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
234 struct wined3d_box box;
235 struct wined3d_map_desc map_desc;
236 HRESULT hr;
238 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
239 iface, locked_rect, wine_dbgstr_rect(rect), flags);
241 if (rect)
243 box.left = rect->left;
244 box.top = rect->top;
245 box.right = rect->right;
246 box.bottom = rect->bottom;
247 box.front = 0;
248 box.back = 1;
251 wined3d_mutex_lock();
252 hr = wined3d_resource_map(wined3d_texture_get_resource(surface->wined3d_texture), surface->sub_resource_idx,
253 &map_desc, rect ? &box : NULL, flags);
254 wined3d_mutex_unlock();
256 if (SUCCEEDED(hr))
258 locked_rect->Pitch = map_desc.row_pitch;
259 locked_rect->pBits = map_desc.data;
262 return hr;
265 static HRESULT WINAPI d3d9_surface_UnlockRect(IDirect3DSurface9 *iface)
267 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(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 = IDirect3DBaseTexture9_GetType(&surface->texture->IDirect3DBaseTexture9_iface);
281 else
282 type = D3DRTYPE_SURFACE;
283 hr = type == D3DRTYPE_TEXTURE ? D3D_OK : D3DERR_INVALIDCALL;
285 return hr;
288 static HRESULT WINAPI d3d9_surface_GetDC(IDirect3DSurface9 *iface, HDC *dc)
290 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
291 HRESULT hr;
293 TRACE("iface %p, dc %p.\n", iface, dc);
295 wined3d_mutex_lock();
296 hr = wined3d_texture_get_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
297 wined3d_mutex_unlock();
299 return hr;
302 static HRESULT WINAPI d3d9_surface_ReleaseDC(IDirect3DSurface9 *iface, HDC dc)
304 struct d3d9_surface *surface = impl_from_IDirect3DSurface9(iface);
305 HRESULT hr;
307 TRACE("iface %p, dc %p.\n", iface, dc);
309 wined3d_mutex_lock();
310 hr = wined3d_texture_release_dc(surface->wined3d_texture, surface->sub_resource_idx, dc);
311 wined3d_mutex_unlock();
313 return hr;
316 static const struct IDirect3DSurface9Vtbl d3d9_surface_vtbl =
318 /* IUnknown */
319 d3d9_surface_QueryInterface,
320 d3d9_surface_AddRef,
321 d3d9_surface_Release,
322 /* IDirect3DResource9 */
323 d3d9_surface_GetDevice,
324 d3d9_surface_SetPrivateData,
325 d3d9_surface_GetPrivateData,
326 d3d9_surface_FreePrivateData,
327 d3d9_surface_SetPriority,
328 d3d9_surface_GetPriority,
329 d3d9_surface_PreLoad,
330 d3d9_surface_GetType,
331 /* IDirect3DSurface9 */
332 d3d9_surface_GetContainer,
333 d3d9_surface_GetDesc,
334 d3d9_surface_LockRect,
335 d3d9_surface_UnlockRect,
336 d3d9_surface_GetDC,
337 d3d9_surface_ReleaseDC,
340 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
342 struct d3d9_surface *surface = parent;
343 d3d9_resource_cleanup(&surface->resource);
344 HeapFree(GetProcessHeap(), 0, surface);
347 static const struct wined3d_parent_ops d3d9_surface_wined3d_parent_ops =
349 surface_wined3d_object_destroyed,
352 void surface_init(struct d3d9_surface *surface, struct wined3d_texture *wined3d_texture,
353 unsigned int sub_resource_idx, const struct wined3d_parent_ops **parent_ops)
355 IDirect3DBaseTexture9 *texture;
357 surface->IDirect3DSurface9_iface.lpVtbl = &d3d9_surface_vtbl;
358 d3d9_resource_init(&surface->resource);
359 surface->resource.refcount = 0;
360 list_init(&surface->rtv_entry);
361 surface->container = wined3d_texture_get_parent(wined3d_texture);
362 surface->wined3d_texture = wined3d_texture;
363 surface->sub_resource_idx = sub_resource_idx;
365 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
366 &IID_IDirect3DBaseTexture9, (void **)&texture)))
368 surface->texture = unsafe_impl_from_IDirect3DBaseTexture9(texture);
369 IDirect3DBaseTexture9_Release(texture);
372 *parent_ops = &d3d9_surface_wined3d_parent_ops;
375 static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
377 struct d3d9_surface *surface = parent;
379 /* If the surface reference count drops to zero, we release our reference
380 * to the view, but don't clear the pointer yet, in case e.g. a
381 * GetRenderTarget() call brings the surface back before the view is
382 * actually destroyed. When the view is destroyed, we need to clear the
383 * pointer, or a subsequent surface AddRef() would reference it again.
385 * This is safe because as long as the view still has a reference to the
386 * texture, the surface is also still alive, and we're called before the
387 * view releases that reference. */
388 surface->wined3d_rtv = NULL;
389 list_remove(&surface->rtv_entry);
392 static const struct wined3d_parent_ops d3d9_view_wined3d_parent_ops =
394 view_wined3d_object_destroyed,
397 struct d3d9_device *d3d9_surface_get_device(const struct d3d9_surface *surface)
399 IDirect3DDevice9Ex *device;
400 device = surface->texture ? surface->texture->parent_device : surface->parent_device;
401 return impl_from_IDirect3DDevice9Ex(device);
404 struct wined3d_rendertarget_view *d3d9_surface_acquire_rendertarget_view(struct d3d9_surface *surface)
406 HRESULT hr;
408 /* The surface reference count can be equal to 0 when this function is
409 * called. In order to properly manage the render target view reference
410 * count, we temporarily increment the surface reference count. */
411 d3d9_surface_AddRef(&surface->IDirect3DSurface9_iface);
413 if (surface->wined3d_rtv)
414 return surface->wined3d_rtv;
416 if (FAILED(hr = wined3d_rendertarget_view_create_from_sub_resource(surface->wined3d_texture,
417 surface->sub_resource_idx, surface, &d3d9_view_wined3d_parent_ops, &surface->wined3d_rtv)))
419 ERR("Failed to create rendertarget view, hr %#x.\n", hr);
420 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
421 return NULL;
424 if (surface->texture)
425 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
427 return surface->wined3d_rtv;
430 void d3d9_surface_release_rendertarget_view(struct d3d9_surface *surface,
431 struct wined3d_rendertarget_view *rtv)
433 if (rtv)
434 d3d9_surface_Release(&surface->IDirect3DSurface9_iface);
437 struct d3d9_surface *unsafe_impl_from_IDirect3DSurface9(IDirect3DSurface9 *iface)
439 if (!iface)
440 return NULL;
441 assert(iface->lpVtbl == &d3d9_surface_vtbl);
443 return impl_from_IDirect3DSurface9(iface);