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
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, const struct GlPixelFormatDesc *format_desc,
31 WINED3DPOOL pool, IUnknown *parent)
33 resource->wineD3DDevice = device;
34 resource->parent = parent;
35 resource->resourceType = resource_type;
37 resource->pool = pool;
38 resource->format_desc = format_desc;
39 resource->usage = usage;
40 resource->size = size;
41 resource->priority = 0;
42 list_init(&resource->privateData);
46 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
47 if (!resource->heapMemory)
49 ERR("Out of memory!\n");
50 return WINED3DERR_OUTOFVIDEOMEMORY;
55 resource->heapMemory = NULL;
57 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
59 /* Check that we have enough video ram left */
60 if (pool == WINED3DPOOL_DEFAULT)
62 if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
64 ERR("Out of adapter memory\n");
65 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
66 return WINED3DERR_OUTOFVIDEOMEMORY;
68 WineD3DAdapterChangeGLRam(device, size);
74 void resource_cleanup(IWineD3DResource *iface)
76 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
81 TRACE("(%p) Cleaning up resource\n", This);
82 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
83 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
84 WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
87 LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
88 data = LIST_ENTRY(e1, PrivateData, entry);
89 hr = resource_free_private_data(iface, &data->tag);
90 if(hr != WINED3D_OK) {
91 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
95 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
96 This->resource.allocatedMemory = 0;
97 This->resource.heapMemory = 0;
99 if (This->resource.wineD3DDevice != NULL) {
100 IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
101 }/* NOTE: this is not really an error for system memory resources */
105 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
107 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
108 TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
109 *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
110 IWineD3DDevice_AddRef(*ppDevice);
114 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
119 TRACE("Searching for private data %s\n", debugstr_guid(tag));
120 LIST_FOR_EACH(entry, &This->resource.privateData)
122 data = LIST_ENTRY(entry, PrivateData, entry);
123 if (IsEqualGUID(&data->tag, tag)) {
124 TRACE("Found %p\n", data);
128 TRACE("Not found\n");
132 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
133 const void *pData, DWORD SizeOfData, DWORD Flags)
135 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
138 TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
139 resource_free_private_data(iface, refguid);
141 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
142 if (NULL == data) return E_OUTOFMEMORY;
144 data->tag = *refguid;
147 if (Flags & WINED3DSPD_IUNKNOWN) {
148 if(SizeOfData != sizeof(IUnknown *)) {
149 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
150 HeapFree(GetProcessHeap(), 0, data);
151 return WINED3DERR_INVALIDCALL;
153 data->ptr.object = (LPUNKNOWN)pData;
154 data->size = sizeof(LPUNKNOWN);
155 IUnknown_AddRef(data->ptr.object);
159 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
160 if (NULL == data->ptr.data) {
161 HeapFree(GetProcessHeap(), 0, data);
162 return E_OUTOFMEMORY;
164 data->size = SizeOfData;
165 memcpy(data->ptr.data, pData, SizeOfData);
167 list_add_tail(&This->resource.privateData, &data->entry);
172 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
174 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
177 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
178 data = resource_find_private_data(This, refguid);
179 if (data == NULL) return WINED3DERR_NOTFOUND;
181 if (*pSizeOfData < data->size) {
182 *pSizeOfData = data->size;
183 return WINED3DERR_MOREDATA;
186 if (data->flags & WINED3DSPD_IUNKNOWN) {
187 *(LPUNKNOWN *)pData = data->ptr.object;
188 if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
189 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
190 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
193 IUnknown_AddRef(data->ptr.object);
197 memcpy(pData, data->ptr.data, data->size);
202 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
204 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
207 TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
208 data = resource_find_private_data(This, refguid);
209 if (data == NULL) return WINED3DERR_NOTFOUND;
211 if (data->flags & WINED3DSPD_IUNKNOWN)
213 if (data->ptr.object != NULL)
214 IUnknown_Release(data->ptr.object);
216 HeapFree(GetProcessHeap(), 0, data->ptr.data);
218 list_remove(&data->entry);
220 HeapFree(GetProcessHeap(), 0, data);
225 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
227 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
228 DWORD PriorityOld = This->resource.priority;
229 This->resource.priority = PriorityNew;
230 TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
234 DWORD resource_get_priority(IWineD3DResource *iface)
236 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
237 TRACE("(%p) : returning %d\n", This, This->resource.priority );
238 return This->resource.priority;
241 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
243 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
244 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
245 return This->resource.resourceType;
248 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
250 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
251 IUnknown_AddRef(This->resource.parent);
252 *pParent = This->resource.parent;
256 void dumpResources(struct list *list) {
257 IWineD3DResourceImpl *resource;
259 LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
260 FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));