wined3d: Allow surface flags to be passed to texture creation functions.
[wine/multimedia.git] / dlls / d3d8 / surface.c
blob21960116e771a5c4e9c4a41d863a66b2da2c39b3
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 "config.h"
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
28 return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
31 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
33 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), out);
35 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
36 || IsEqualGUID(riid, &IID_IDirect3DResource8)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DSurface8_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
52 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
54 TRACE("iface %p.\n", iface);
56 if (surface->forwardReference)
58 /* Forward refcounting */
59 TRACE("Forwarding to %p.\n", surface->forwardReference);
60 return IUnknown_AddRef(surface->forwardReference);
62 else
64 /* No container, handle our own refcounting */
65 ULONG ref = InterlockedIncrement(&surface->refcount);
67 TRACE("%p increasing refcount to %u.\n", iface, ref);
69 if (ref == 1)
71 if (surface->parent_device)
72 IDirect3DDevice8_AddRef(surface->parent_device);
73 wined3d_mutex_lock();
74 wined3d_surface_incref(surface->wined3d_surface);
75 wined3d_mutex_unlock();
78 return ref;
82 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
84 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
86 TRACE("iface %p.\n", iface);
88 if (surface->forwardReference)
90 /* Forward refcounting */
91 TRACE("Forwarding to %p.\n", surface->forwardReference);
92 return IUnknown_Release(surface->forwardReference);
94 else
96 /* No container, handle our own refcounting */
97 ULONG ref = InterlockedDecrement(&surface->refcount);
99 TRACE("%p decreasing refcount to %u.\n", iface, ref);
101 if (!ref)
103 IDirect3DDevice8 *parent_device = surface->parent_device;
105 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
106 wined3d_mutex_lock();
107 wined3d_surface_decref(surface->wined3d_surface);
108 wined3d_mutex_unlock();
110 if (parent_device)
111 IDirect3DDevice8_Release(parent_device);
114 return ref;
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->forwardReference)
126 IDirect3DResource8 *resource;
127 HRESULT hr;
129 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
130 if (SUCCEEDED(hr))
132 hr = IDirect3DResource8_GetDevice(resource, device);
133 IDirect3DResource8_Release(resource);
135 TRACE("Returning device %p.\n", *device);
138 return hr;
141 *device = surface->parent_device;
142 IDirect3DDevice8_AddRef(*device);
144 TRACE("Returning device %p.\n", *device);
146 return D3D_OK;
149 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
150 const void *data, DWORD data_size, DWORD flags)
152 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
153 struct wined3d_resource *resource;
154 HRESULT hr;
156 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
157 iface, debugstr_guid(guid), data, data_size, flags);
159 wined3d_mutex_lock();
160 resource = wined3d_surface_get_resource(surface->wined3d_surface);
161 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
162 wined3d_mutex_unlock();
164 return hr;
167 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
168 void *data, DWORD *data_size)
170 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
171 struct wined3d_resource *resource;
172 HRESULT hr;
174 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
175 iface, debugstr_guid(guid), data, data_size);
177 wined3d_mutex_lock();
178 resource = wined3d_surface_get_resource(surface->wined3d_surface);
179 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
180 wined3d_mutex_unlock();
182 return hr;
185 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
187 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
188 struct wined3d_resource *resource;
189 HRESULT hr;
191 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
193 wined3d_mutex_lock();
194 resource = wined3d_surface_get_resource(surface->wined3d_surface);
195 hr = wined3d_resource_free_private_data(resource, guid);
196 wined3d_mutex_unlock();
198 return hr;
201 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
203 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
204 HRESULT hr;
206 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
208 if (!surface->container)
209 return E_NOINTERFACE;
211 hr = IUnknown_QueryInterface(surface->container, riid, container);
213 TRACE("Returning %p.\n", *container);
215 return hr;
218 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
220 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
221 struct wined3d_resource_desc wined3d_desc;
222 struct wined3d_resource *wined3d_resource;
224 TRACE("iface %p, desc %p.\n", iface, desc);
226 wined3d_mutex_lock();
227 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
228 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
229 wined3d_mutex_unlock();
231 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
232 desc->Type = wined3d_desc.resource_type;
233 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
234 desc->Pool = wined3d_desc.pool;
235 desc->Size = wined3d_desc.size;
236 desc->MultiSampleType = wined3d_desc.multisample_type;
237 desc->Width = wined3d_desc.width;
238 desc->Height = wined3d_desc.height;
240 return D3D_OK;
243 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
244 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
246 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
247 struct wined3d_map_desc map_desc;
248 HRESULT hr;
250 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
251 iface, locked_rect, wine_dbgstr_rect(rect), flags);
253 wined3d_mutex_lock();
254 if (rect)
256 D3DSURFACE_DESC desc;
257 IDirect3DSurface8_GetDesc(iface, &desc);
259 if ((rect->left < 0)
260 || (rect->top < 0)
261 || (rect->left >= rect->right)
262 || (rect->top >= rect->bottom)
263 || (rect->right > desc.Width)
264 || (rect->bottom > desc.Height))
266 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
267 wined3d_mutex_unlock();
269 return D3DERR_INVALIDCALL;
273 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
274 wined3d_mutex_unlock();
276 locked_rect->Pitch = map_desc.row_pitch;
277 locked_rect->pBits = map_desc.data;
279 return hr;
282 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
284 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
285 HRESULT hr;
287 TRACE("iface %p.\n", iface);
289 wined3d_mutex_lock();
290 hr = wined3d_surface_unmap(surface->wined3d_surface);
291 wined3d_mutex_unlock();
293 switch(hr)
295 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
296 default: return hr;
300 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
302 /* IUnknown */
303 d3d8_surface_QueryInterface,
304 d3d8_surface_AddRef,
305 d3d8_surface_Release,
306 /* IDirect3DResource8 */
307 d3d8_surface_GetDevice,
308 d3d8_surface_SetPrivateData,
309 d3d8_surface_GetPrivateData,
310 d3d8_surface_FreePrivateData,
311 /* IDirect3DSurface8 */
312 d3d8_surface_GetContainer,
313 d3d8_surface_GetDesc,
314 d3d8_surface_LockRect,
315 d3d8_surface_UnlockRect,
318 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
320 HeapFree(GetProcessHeap(), 0, parent);
323 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
325 surface_wined3d_object_destroyed,
328 HRESULT surface_init(struct d3d8_surface *surface, struct d3d8_device *device, UINT width, UINT height,
329 D3DFORMAT format, DWORD flags, DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type,
330 DWORD multisample_quality)
332 HRESULT hr;
334 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
335 surface->refcount = 1;
337 /* FIXME: Check MAX bounds of MultisampleQuality. */
338 if (multisample_quality > 0)
340 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
341 multisample_quality = 0;
344 wined3d_mutex_lock();
345 hr = wined3d_surface_create(device->wined3d_device, width, height, wined3dformat_from_d3dformat(format),
346 usage & WINED3DUSAGE_MASK, (enum wined3d_pool)pool, multisample_type, multisample_quality,
347 flags, surface, &d3d8_surface_wined3d_parent_ops, &surface->wined3d_surface);
348 wined3d_mutex_unlock();
349 if (FAILED(hr))
351 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
352 return hr;
355 surface->parent_device = &device->IDirect3DDevice8_iface;
356 IDirect3DDevice8_AddRef(surface->parent_device);
358 return D3D_OK;
361 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
363 if (!iface)
364 return NULL;
365 assert(iface->lpVtbl == &d3d8_surface_vtbl);
367 return impl_from_IDirect3DSurface8(iface);