push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / dlls / wined3d / resource.c
blobb4e49b1a32290995f119721ad7485219a71cb61b
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 HRESULT resource_init(struct IWineD3DResourceClass *resource, WINED3DRESOURCETYPE resource_type,
30 IWineD3DDeviceImpl *device, UINT size, DWORD usage, WINED3DFORMAT format, WINED3DPOOL pool, IUnknown *parent)
32 resource->wineD3DDevice = device;
33 resource->parent = parent;
34 resource->resourceType = resource_type;
35 resource->ref = 1;
36 resource->pool = pool;
37 resource->format = format;
38 resource->usage = usage;
39 resource->size = size;
40 resource->priority = 0;
41 list_init(&resource->privateData);
43 if (size)
45 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
46 if (!resource->heapMemory)
48 ERR("Out of memory!\n");
49 return WINED3DERR_OUTOFVIDEOMEMORY;
52 else
54 resource->heapMemory = NULL;
56 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
58 /* Check that we have enough video ram left */
59 if (pool == WINED3DPOOL_DEFAULT)
61 if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
63 ERR("Out of adapter memory\n");
64 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
65 return WINED3DERR_OUTOFVIDEOMEMORY;
67 WineD3DAdapterChangeGLRam(device, size);
70 return WINED3D_OK;
73 void resource_cleanup(IWineD3DResource *iface)
75 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
76 struct list *e1, *e2;
77 PrivateData *data;
78 HRESULT hr;
80 TRACE("(%p) Cleaning up resource\n", This);
81 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
82 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
83 WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
86 LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
87 data = LIST_ENTRY(e1, PrivateData, entry);
88 hr = resource_free_private_data(iface, &data->tag);
89 if(hr != WINED3D_OK) {
90 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
94 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
95 This->resource.allocatedMemory = 0;
96 This->resource.heapMemory = 0;
98 if (This->resource.wineD3DDevice != NULL) {
99 IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
100 }/* NOTE: this is not really an error for system memory resources */
101 return;
104 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
106 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
107 TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
108 *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
109 IWineD3DDevice_AddRef(*ppDevice);
110 return WINED3D_OK;
113 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
115 PrivateData *data;
116 struct list *entry;
118 TRACE("Searching for private data %s\n", debugstr_guid(tag));
119 LIST_FOR_EACH(entry, &This->resource.privateData)
121 data = LIST_ENTRY(entry, PrivateData, entry);
122 if (IsEqualGUID(&data->tag, tag)) {
123 TRACE("Found %p\n", data);
124 return data;
127 TRACE("Not found\n");
128 return NULL;
131 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
132 const void *pData, DWORD SizeOfData, DWORD Flags)
134 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
135 PrivateData *data;
137 TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
138 resource_free_private_data(iface, refguid);
140 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
141 if (NULL == data) return E_OUTOFMEMORY;
143 data->tag = *refguid;
144 data->flags = Flags;
146 if (Flags & WINED3DSPD_IUNKNOWN) {
147 if(SizeOfData != sizeof(IUnknown *)) {
148 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
149 HeapFree(GetProcessHeap(), 0, data);
150 return WINED3DERR_INVALIDCALL;
152 data->ptr.object = (LPUNKNOWN)pData;
153 data->size = sizeof(LPUNKNOWN);
154 IUnknown_AddRef(data->ptr.object);
156 else
158 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
159 if (NULL == data->ptr.data) {
160 HeapFree(GetProcessHeap(), 0, data);
161 return E_OUTOFMEMORY;
163 data->size = SizeOfData;
164 memcpy(data->ptr.data, pData, SizeOfData);
166 list_add_tail(&This->resource.privateData, &data->entry);
168 return WINED3D_OK;
171 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
173 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
174 PrivateData *data;
176 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
177 data = resource_find_private_data(This, refguid);
178 if (data == NULL) return WINED3DERR_NOTFOUND;
180 if (*pSizeOfData < data->size) {
181 *pSizeOfData = data->size;
182 return WINED3DERR_MOREDATA;
185 if (data->flags & WINED3DSPD_IUNKNOWN) {
186 *(LPUNKNOWN *)pData = data->ptr.object;
187 if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
188 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
189 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
190 * Blob
192 IUnknown_AddRef(data->ptr.object);
195 else {
196 memcpy(pData, data->ptr.data, data->size);
199 return WINED3D_OK;
201 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
203 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
204 PrivateData *data;
206 TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
207 data = resource_find_private_data(This, refguid);
208 if (data == NULL) return WINED3DERR_NOTFOUND;
210 if (data->flags & WINED3DSPD_IUNKNOWN)
212 if (data->ptr.object != NULL)
213 IUnknown_Release(data->ptr.object);
214 } else {
215 HeapFree(GetProcessHeap(), 0, data->ptr.data);
217 list_remove(&data->entry);
219 HeapFree(GetProcessHeap(), 0, data);
221 return WINED3D_OK;
224 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
226 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
227 DWORD PriorityOld = This->resource.priority;
228 This->resource.priority = PriorityNew;
229 TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
230 return PriorityOld;
233 DWORD resource_get_priority(IWineD3DResource *iface)
235 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
236 TRACE("(%p) : returning %d\n", This, This->resource.priority );
237 return This->resource.priority;
240 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
242 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
243 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
244 return This->resource.resourceType;
247 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
249 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
250 IUnknown_AddRef(This->resource.parent);
251 *pParent = This->resource.parent;
252 return WINED3D_OK;
255 void dumpResources(struct list *list) {
256 IWineD3DResourceImpl *resource;
258 LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
259 FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));