wined3d: Pass an IWineD3DResourceImpl pointer to resource_free_private_data().
[wine.git] / dlls / wined3d / resource.c
blob76267d5c922d35e305791b79b8f902009aa7787a
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 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;
52 r->device = device;
53 r->resourceType = resource_type;
54 r->ref = 1;
55 r->pool = pool;
56 r->format = format;
57 r->usage = usage;
58 r->size = size;
59 r->priority = 0;
60 r->parent = parent;
61 r->parent_ops = parent_ops;
62 list_init(&r->privateData);
64 if (size)
66 r->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
67 if (!r->heapMemory)
69 ERR("Out of memory!\n");
70 return WINED3DERR_OUTOFVIDEOMEMORY;
73 else
75 r->heapMemory = NULL;
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, (IWineD3DResource *)resource);
93 return WINED3D_OK;
96 void resource_cleanup(struct IWineD3DResourceImpl *resource)
98 struct private_data *data;
99 struct list *e1, *e2;
100 HRESULT hr;
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);
114 if (FAILED(hr))
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, (IWineD3DResource *)resource);
126 void resource_unload(IWineD3DResourceImpl *resource)
128 context_resource_unloaded(resource->resource.device, (IWineD3DResource *)resource,
129 resource->resource.resourceType);
132 static struct private_data *resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
134 struct private_data *data;
135 struct list *entry;
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);
143 return data;
146 TRACE("Not found\n");
147 return NULL;
150 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
151 const void *pData, DWORD SizeOfData, DWORD flags)
153 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
154 struct private_data *data;
156 TRACE("iface %p, riid %s, data %p, data_size %u, flags %#x.\n",
157 iface, debugstr_guid(refguid), pData, SizeOfData, flags);
159 resource_free_private_data(This, refguid);
161 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
162 if (!data) return E_OUTOFMEMORY;
164 data->tag = *refguid;
165 data->flags = flags;
167 if (flags & WINED3DSPD_IUNKNOWN)
169 if (SizeOfData != sizeof(IUnknown *))
171 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
172 HeapFree(GetProcessHeap(), 0, data);
173 return WINED3DERR_INVALIDCALL;
175 data->ptr.object = (LPUNKNOWN)pData;
176 data->size = sizeof(LPUNKNOWN);
177 IUnknown_AddRef(data->ptr.object);
179 else
181 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
182 if (!data->ptr.data)
184 HeapFree(GetProcessHeap(), 0, data);
185 return E_OUTOFMEMORY;
187 data->size = SizeOfData;
188 memcpy(data->ptr.data, pData, SizeOfData);
190 list_add_tail(&This->resource.privateData, &data->entry);
192 return WINED3D_OK;
195 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
197 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
198 struct private_data *data;
200 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
201 data = resource_find_private_data(This, refguid);
202 if (!data) return WINED3DERR_NOTFOUND;
204 if (*pSizeOfData < data->size) {
205 *pSizeOfData = data->size;
206 return WINED3DERR_MOREDATA;
209 if (data->flags & WINED3DSPD_IUNKNOWN) {
210 *(LPUNKNOWN *)pData = data->ptr.object;
211 if (((IWineD3DImpl *)This->resource.device->wined3d)->dxVersion != 7)
213 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
214 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
215 * Blob
217 IUnknown_AddRef(data->ptr.object);
220 else {
221 memcpy(pData, data->ptr.data, data->size);
224 return WINED3D_OK;
226 HRESULT resource_free_private_data(struct IWineD3DResourceImpl *resource, REFGUID guid)
228 struct private_data *data;
230 TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
232 data = resource_find_private_data(resource, guid);
233 if (!data) return WINED3DERR_NOTFOUND;
235 if (data->flags & WINED3DSPD_IUNKNOWN)
237 if (data->ptr.object)
238 IUnknown_Release(data->ptr.object);
240 else
242 HeapFree(GetProcessHeap(), 0, data->ptr.data);
244 list_remove(&data->entry);
246 HeapFree(GetProcessHeap(), 0, data);
248 return WINED3D_OK;
251 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
253 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
254 DWORD PriorityOld = This->resource.priority;
255 This->resource.priority = PriorityNew;
256 TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
257 return PriorityOld;
260 DWORD resource_get_priority(IWineD3DResource *iface)
262 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
263 TRACE("(%p) : returning %d\n", This, This->resource.priority );
264 return This->resource.priority;
267 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
269 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
270 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
271 return This->resource.resourceType;