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(IWineD3DResource *iface, 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 *resource = &((IWineD3DResourceImpl *)iface)->resource;
52 resource->device = device;
53 resource->resourceType = resource_type;
55 resource->pool = pool;
56 resource->format = format;
57 resource->usage = usage;
58 resource->size = size;
59 resource->priority = 0;
60 resource->parent = parent;
61 resource->parent_ops = parent_ops;
62 list_init(&resource->privateData);
66 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
67 if (!resource->heapMemory)
69 ERR("Out of memory!\n");
70 return WINED3DERR_OUTOFVIDEOMEMORY;
75 resource->heapMemory = NULL;
77 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->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, resource->heapMemory);
86 return WINED3DERR_OUTOFVIDEOMEMORY;
88 WineD3DAdapterChangeGLRam(device, size);
91 device_resource_add(device, iface);
96 void resource_cleanup(IWineD3DResource *iface)
98 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
99 struct private_data *data;
100 struct list *e1, *e2;
103 TRACE("(%p) Cleaning up resource\n", This);
104 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
105 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
106 WineD3DAdapterChangeGLRam(This->resource.device, -This->resource.size);
109 LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData)
111 data = LIST_ENTRY(e1, struct private_data, entry);
112 hr = resource_free_private_data(iface, &data->tag);
113 if(hr != WINED3D_OK) {
114 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
118 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
119 This->resource.allocatedMemory = 0;
120 This->resource.heapMemory = 0;
122 if (This->resource.device) device_resource_released(This->resource.device, iface);
125 void resource_unload(IWineD3DResourceImpl *resource)
127 context_resource_unloaded(resource->resource.device, (IWineD3DResource *)resource,
128 resource->resource.resourceType);
131 static struct private_data *resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
133 struct private_data *data;
136 TRACE("Searching for private data %s\n", debugstr_guid(tag));
137 LIST_FOR_EACH(entry, &This->resource.privateData)
139 data = LIST_ENTRY(entry, struct private_data, entry);
140 if (IsEqualGUID(&data->tag, tag)) {
141 TRACE("Found %p\n", data);
145 TRACE("Not found\n");
149 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
150 const void *pData, DWORD SizeOfData, DWORD flags)
152 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
153 struct private_data *data;
155 TRACE("iface %p, riid %s, data %p, data_size %u, flags %#x.\n",
156 iface, debugstr_guid(refguid), pData, SizeOfData, flags);
158 resource_free_private_data(iface, refguid);
160 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
161 if (!data) return E_OUTOFMEMORY;
163 data->tag = *refguid;
166 if (flags & WINED3DSPD_IUNKNOWN)
168 if (SizeOfData != sizeof(IUnknown *))
170 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
171 HeapFree(GetProcessHeap(), 0, data);
172 return WINED3DERR_INVALIDCALL;
174 data->ptr.object = (LPUNKNOWN)pData;
175 data->size = sizeof(LPUNKNOWN);
176 IUnknown_AddRef(data->ptr.object);
180 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
183 HeapFree(GetProcessHeap(), 0, data);
184 return E_OUTOFMEMORY;
186 data->size = SizeOfData;
187 memcpy(data->ptr.data, pData, SizeOfData);
189 list_add_tail(&This->resource.privateData, &data->entry);
194 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
196 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
197 struct private_data *data;
199 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
200 data = resource_find_private_data(This, refguid);
201 if (!data) return WINED3DERR_NOTFOUND;
203 if (*pSizeOfData < data->size) {
204 *pSizeOfData = data->size;
205 return WINED3DERR_MOREDATA;
208 if (data->flags & WINED3DSPD_IUNKNOWN) {
209 *(LPUNKNOWN *)pData = data->ptr.object;
210 if (((IWineD3DImpl *)This->resource.device->wined3d)->dxVersion != 7)
212 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
213 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
216 IUnknown_AddRef(data->ptr.object);
220 memcpy(pData, data->ptr.data, data->size);
225 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
227 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
228 struct private_data *data;
230 TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
231 data = resource_find_private_data(This, refguid);
232 if (!data) return WINED3DERR_NOTFOUND;
234 if (data->flags & WINED3DSPD_IUNKNOWN)
236 if (data->ptr.object)
237 IUnknown_Release(data->ptr.object);
241 HeapFree(GetProcessHeap(), 0, data->ptr.data);
243 list_remove(&data->entry);
245 HeapFree(GetProcessHeap(), 0, data);
250 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
252 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
253 DWORD PriorityOld = This->resource.priority;
254 This->resource.priority = PriorityNew;
255 TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
259 DWORD resource_get_priority(IWineD3DResource *iface)
261 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
262 TRACE("(%p) : returning %d\n", This, This->resource.priority );
263 return This->resource.priority;
266 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
268 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
269 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
270 return This->resource.resourceType;