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
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35 DWORD flags; /* DDSPD_* */
46 HRESULT resource_init(struct IWineD3DResourceImpl *resource, WINED3DRESOURCETYPE resource_type,
47 IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct wined3d_format *format,
48 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
50 struct IWineD3DResourceClass *r = &resource->resource;
53 r->resourceType = resource_type;
61 r->parent_ops = parent_ops;
62 list_init(&r->privateData);
66 r->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
69 ERR("Out of memory!\n");
70 return WINED3DERR_OUTOFVIDEOMEMORY;
77 r->allocatedMemory = (BYTE *)(((ULONG_PTR)r->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
79 /* Check that we have enough video ram left */
80 if (pool == WINED3DPOOL_DEFAULT)
82 if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
84 ERR("Out of adapter memory\n");
85 HeapFree(GetProcessHeap(), 0, r->heapMemory);
86 return WINED3DERR_OUTOFVIDEOMEMORY;
88 WineD3DAdapterChangeGLRam(device, size);
91 device_resource_add(device, resource);
96 void resource_cleanup(struct IWineD3DResourceImpl *resource)
98 struct private_data *data;
102 TRACE("Cleaning up resource %p.\n", resource);
104 if (resource->resource.pool == WINED3DPOOL_DEFAULT)
106 TRACE("Decrementing device memory pool by %u.\n", resource->resource.size);
107 WineD3DAdapterChangeGLRam(resource->resource.device, -resource->resource.size);
110 LIST_FOR_EACH_SAFE(e1, e2, &resource->resource.privateData)
112 data = LIST_ENTRY(e1, struct private_data, entry);
113 hr = resource_free_private_data(resource, &data->tag);
115 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
118 HeapFree(GetProcessHeap(), 0, resource->resource.heapMemory);
119 resource->resource.allocatedMemory = 0;
120 resource->resource.heapMemory = 0;
122 if (resource->resource.device)
123 device_resource_released(resource->resource.device, resource);
126 void resource_unload(IWineD3DResourceImpl *resource)
128 context_resource_unloaded(resource->resource.device,
129 resource, resource->resource.resourceType);
132 static struct private_data *resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
134 struct private_data *data;
137 TRACE("Searching for private data %s\n", debugstr_guid(tag));
138 LIST_FOR_EACH(entry, &This->resource.privateData)
140 data = LIST_ENTRY(entry, struct private_data, entry);
141 if (IsEqualGUID(&data->tag, tag)) {
142 TRACE("Found %p\n", data);
146 TRACE("Not found\n");
150 HRESULT resource_set_private_data(struct IWineD3DResourceImpl *resource, REFGUID guid,
151 const void *data, DWORD data_size, DWORD flags)
153 struct private_data *d;
155 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
156 resource, debugstr_guid(guid), data, data_size, flags);
158 resource_free_private_data(resource, guid);
160 d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
161 if (!d) return E_OUTOFMEMORY;
166 if (flags & WINED3DSPD_IUNKNOWN)
168 if (data_size != sizeof(IUnknown *))
170 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
171 HeapFree(GetProcessHeap(), 0, d);
172 return WINED3DERR_INVALIDCALL;
174 d->ptr.object = (IUnknown *)data;
175 d->size = sizeof(IUnknown *);
176 IUnknown_AddRef(d->ptr.object);
180 d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
183 HeapFree(GetProcessHeap(), 0, d);
184 return E_OUTOFMEMORY;
187 memcpy(d->ptr.data, data, data_size);
189 list_add_tail(&resource->resource.privateData, &d->entry);
194 HRESULT resource_get_private_data(struct IWineD3DResourceImpl *resource, REFGUID guid, void *data, DWORD *data_size)
196 const struct private_data *d;
198 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
199 resource, debugstr_guid(guid), data, data_size);
201 d = resource_find_private_data(resource, guid);
202 if (!d) return WINED3DERR_NOTFOUND;
204 if (*data_size < d->size)
206 *data_size = d->size;
207 return WINED3DERR_MOREDATA;
210 if (d->flags & WINED3DSPD_IUNKNOWN)
212 *(IUnknown **)data = d->ptr.object;
213 if (resource->resource.device->wined3d->dxVersion != 7)
215 /* D3D8 and D3D9 addref the private data, DDraw does not. This
216 * can't be handled in ddraw because it doesn't know if the
217 * pointer returned is an IUnknown * or just a blob. */
218 IUnknown_AddRef(d->ptr.object);
223 memcpy(data, d->ptr.data, d->size);
228 HRESULT resource_free_private_data(struct IWineD3DResourceImpl *resource, REFGUID guid)
230 struct private_data *data;
232 TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
234 data = resource_find_private_data(resource, guid);
235 if (!data) return WINED3DERR_NOTFOUND;
237 if (data->flags & WINED3DSPD_IUNKNOWN)
239 if (data->ptr.object)
240 IUnknown_Release(data->ptr.object);
244 HeapFree(GetProcessHeap(), 0, data->ptr.data);
246 list_remove(&data->entry);
248 HeapFree(GetProcessHeap(), 0, data);
253 DWORD resource_set_priority(struct IWineD3DResourceImpl *resource, DWORD priority)
255 DWORD prev = resource->resource.priority;
256 resource->resource.priority = priority;
257 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
261 DWORD resource_get_priority(struct IWineD3DResourceImpl *resource)
263 TRACE("resource %p, returning %u.\n", resource, resource->resource.priority);
264 return resource->resource.priority;
267 WINED3DRESOURCETYPE resource_get_type(struct IWineD3DResourceImpl *resource)
269 TRACE("resource %p, returning %#x.\n", resource, resource->resource.resourceType);
270 return resource->resource.resourceType;