winecoreaudio.drv: Implement IAudioSessionControl::GetState.
[wine/multimedia.git] / dlls / wined3d / resource.c
blobcdc483d8fa2b88c1a178126bff2966ffd8276c74
1 /*
2 * IWineD3DResource Implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 struct private_data
32 struct list entry;
34 GUID tag;
35 DWORD flags; /* DDSPD_* */
37 union
39 void *data;
40 IUnknown *object;
41 } ptr;
43 DWORD size;
46 HRESULT resource_init(struct wined3d_resource *resource, IWineD3DDeviceImpl *device,
47 WINED3DRESOURCETYPE resource_type, const struct wined3d_format *format,
48 WINED3DMULTISAMPLE_TYPE multisample_type, UINT multisample_quality,
49 DWORD usage, WINED3DPOOL pool, UINT width, UINT height, UINT depth, UINT size,
50 void *parent, const struct wined3d_parent_ops *parent_ops,
51 const struct wined3d_resource_ops *resource_ops)
53 resource->ref = 1;
54 resource->device = device;
55 resource->resourceType = resource_type;
56 resource->format = format;
57 resource->multisample_type = multisample_type;
58 resource->multisample_quality = multisample_quality;
59 resource->usage = usage;
60 resource->pool = pool;
61 resource->width = width;
62 resource->height = height;
63 resource->depth = depth;
64 resource->size = size;
65 resource->priority = 0;
66 resource->parent = parent;
67 resource->parent_ops = parent_ops;
68 resource->resource_ops = resource_ops;
69 list_init(&resource->privateData);
71 if (size)
73 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
74 if (!resource->heapMemory)
76 ERR("Out of memory!\n");
77 return WINED3DERR_OUTOFVIDEOMEMORY;
80 else
82 resource->heapMemory = NULL;
84 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
85 + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
87 /* Check that we have enough video ram left */
88 if (pool == WINED3DPOOL_DEFAULT)
90 if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
92 ERR("Out of adapter memory\n");
93 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
94 return WINED3DERR_OUTOFVIDEOMEMORY;
96 WineD3DAdapterChangeGLRam(device, size);
99 device_resource_add(device, resource);
101 return WINED3D_OK;
104 void resource_cleanup(struct wined3d_resource *resource)
106 struct private_data *data;
107 struct list *e1, *e2;
108 HRESULT hr;
110 TRACE("Cleaning up resource %p.\n", resource);
112 if (resource->pool == WINED3DPOOL_DEFAULT)
114 TRACE("Decrementing device memory pool by %u.\n", resource->size);
115 WineD3DAdapterChangeGLRam(resource->device, -resource->size);
118 LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
120 data = LIST_ENTRY(e1, struct private_data, entry);
121 hr = resource_free_private_data(resource, &data->tag);
122 if (FAILED(hr))
123 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
126 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
127 resource->allocatedMemory = 0;
128 resource->heapMemory = 0;
130 if (resource->device)
131 device_resource_released(resource->device, resource);
134 void resource_unload(struct wined3d_resource *resource)
136 context_resource_unloaded(resource->device,
137 resource, resource->resourceType);
140 static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
142 struct private_data *data;
143 struct list *entry;
145 TRACE("Searching for private data %s\n", debugstr_guid(tag));
146 LIST_FOR_EACH(entry, &resource->privateData)
148 data = LIST_ENTRY(entry, struct private_data, entry);
149 if (IsEqualGUID(&data->tag, tag)) {
150 TRACE("Found %p\n", data);
151 return data;
154 TRACE("Not found\n");
155 return NULL;
158 HRESULT resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
159 const void *data, DWORD data_size, DWORD flags)
161 struct private_data *d;
163 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
164 resource, debugstr_guid(guid), data, data_size, flags);
166 resource_free_private_data(resource, guid);
168 d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
169 if (!d) return E_OUTOFMEMORY;
171 d->tag = *guid;
172 d->flags = flags;
174 if (flags & WINED3DSPD_IUNKNOWN)
176 if (data_size != sizeof(IUnknown *))
178 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
179 HeapFree(GetProcessHeap(), 0, d);
180 return WINED3DERR_INVALIDCALL;
182 d->ptr.object = (IUnknown *)data;
183 d->size = sizeof(IUnknown *);
184 IUnknown_AddRef(d->ptr.object);
186 else
188 d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
189 if (!d->ptr.data)
191 HeapFree(GetProcessHeap(), 0, d);
192 return E_OUTOFMEMORY;
194 d->size = data_size;
195 memcpy(d->ptr.data, data, data_size);
197 list_add_tail(&resource->privateData, &d->entry);
199 return WINED3D_OK;
202 HRESULT resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid, void *data, DWORD *data_size)
204 const struct private_data *d;
206 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
207 resource, debugstr_guid(guid), data, data_size);
209 d = resource_find_private_data(resource, guid);
210 if (!d) return WINED3DERR_NOTFOUND;
212 if (*data_size < d->size)
214 *data_size = d->size;
215 return WINED3DERR_MOREDATA;
218 if (d->flags & WINED3DSPD_IUNKNOWN)
220 *(IUnknown **)data = d->ptr.object;
221 if (resource->device->wined3d->dxVersion != 7)
223 /* D3D8 and D3D9 addref the private data, DDraw does not. This
224 * can't be handled in ddraw because it doesn't know if the
225 * pointer returned is an IUnknown * or just a blob. */
226 IUnknown_AddRef(d->ptr.object);
229 else
231 memcpy(data, d->ptr.data, d->size);
234 return WINED3D_OK;
236 HRESULT resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
238 struct private_data *data;
240 TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
242 data = resource_find_private_data(resource, guid);
243 if (!data) return WINED3DERR_NOTFOUND;
245 if (data->flags & WINED3DSPD_IUNKNOWN)
247 if (data->ptr.object)
248 IUnknown_Release(data->ptr.object);
250 else
252 HeapFree(GetProcessHeap(), 0, data->ptr.data);
254 list_remove(&data->entry);
256 HeapFree(GetProcessHeap(), 0, data);
258 return WINED3D_OK;
261 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
263 DWORD prev = resource->priority;
264 resource->priority = priority;
265 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
266 return prev;
269 DWORD resource_get_priority(const struct wined3d_resource *resource)
271 TRACE("resource %p, returning %u.\n", resource, resource->priority);
272 return resource->priority;
275 WINED3DRESOURCETYPE resource_get_type(const struct wined3d_resource *resource)
277 TRACE("resource %p, returning %#x.\n", resource, resource->resourceType);
278 return resource->resourceType;
281 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
283 return resource->parent;
286 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
288 desc->resource_type = resource->resourceType;
289 desc->format = resource->format->id;
290 desc->multisample_type = resource->multisample_type;
291 desc->multisample_quality = resource->multisample_quality;
292 desc->usage = resource->usage;
293 desc->pool = resource->pool;
294 desc->width = resource->width;
295 desc->height = resource->height;
296 desc->depth = resource->depth;
297 desc->size = resource->size;