wined3d: Introduce surface_calculate_size().
[wine] / dlls / wined3d / resource.c
1 /*
2  * IWineD3DResource Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28
29 HRESULT resource_init(IWineD3DResource *iface, WINED3DRESOURCETYPE resource_type,
30         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
31         WINED3DPOOL pool, IUnknown *parent)
32 {
33     struct IWineD3DResourceClass *resource = &((IWineD3DResourceImpl *)iface)->resource;
34
35     resource->wineD3DDevice = device;
36     resource->parent = parent;
37     resource->resourceType = resource_type;
38     resource->ref = 1;
39     resource->pool = pool;
40     resource->format_desc = format_desc;
41     resource->usage = usage;
42     resource->size = size;
43     resource->priority = 0;
44     list_init(&resource->privateData);
45
46     if (size)
47     {
48         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
49         if (!resource->heapMemory)
50         {
51             ERR("Out of memory!\n");
52             return WINED3DERR_OUTOFVIDEOMEMORY;
53         }
54     }
55     else
56     {
57         resource->heapMemory = NULL;
58     }
59     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
60
61     /* Check that we have enough video ram left */
62     if (pool == WINED3DPOOL_DEFAULT)
63     {
64         if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
65         {
66             ERR("Out of adapter memory\n");
67             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
68             return WINED3DERR_OUTOFVIDEOMEMORY;
69         }
70         WineD3DAdapterChangeGLRam(device, size);
71     }
72
73     device_resource_add(device, iface);
74
75     return WINED3D_OK;
76 }
77
78 void resource_cleanup(IWineD3DResource *iface)
79 {
80     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
81     struct list *e1, *e2;
82     PrivateData *data;
83     HRESULT hr;
84
85     TRACE("(%p) Cleaning up resource\n", This);
86     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
87         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
88         WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
89     }
90
91     LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
92         data = LIST_ENTRY(e1, PrivateData, entry);
93         hr = resource_free_private_data(iface, &data->tag);
94         if(hr != WINED3D_OK) {
95             ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
96         }
97     }
98
99     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
100     This->resource.allocatedMemory = 0;
101     This->resource.heapMemory = 0;
102
103     if (This->resource.wineD3DDevice) device_resource_released(This->resource.wineD3DDevice, iface);
104 }
105
106 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
107 {
108     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
109     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
110     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
111     IWineD3DDevice_AddRef(*ppDevice);
112     return WINED3D_OK;
113 }
114
115 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
116 {
117     PrivateData *data;
118     struct list *entry;
119
120     TRACE("Searching for private data %s\n", debugstr_guid(tag));
121     LIST_FOR_EACH(entry, &This->resource.privateData)
122     {
123         data = LIST_ENTRY(entry, PrivateData, entry);
124         if (IsEqualGUID(&data->tag, tag)) {
125             TRACE("Found %p\n", data);
126             return data;
127         }
128     }
129     TRACE("Not found\n");
130     return NULL;
131 }
132
133 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
134         const void *pData, DWORD SizeOfData, DWORD Flags)
135 {
136     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
137     PrivateData *data;
138
139     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
140     resource_free_private_data(iface, refguid);
141
142     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
143     if (NULL == data) return E_OUTOFMEMORY;
144
145     data->tag = *refguid;
146     data->flags = Flags;
147
148     if (Flags & WINED3DSPD_IUNKNOWN) {
149         if(SizeOfData != sizeof(IUnknown *)) {
150             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
151             HeapFree(GetProcessHeap(), 0, data);
152             return WINED3DERR_INVALIDCALL;
153         }
154         data->ptr.object = (LPUNKNOWN)pData;
155         data->size = sizeof(LPUNKNOWN);
156         IUnknown_AddRef(data->ptr.object);
157     }
158     else
159     {
160         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
161         if (NULL == data->ptr.data) {
162             HeapFree(GetProcessHeap(), 0, data);
163             return E_OUTOFMEMORY;
164         }
165         data->size = SizeOfData;
166         memcpy(data->ptr.data, pData, SizeOfData);
167     }
168     list_add_tail(&This->resource.privateData, &data->entry);
169
170     return WINED3D_OK;
171 }
172
173 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
174 {
175     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
176     PrivateData *data;
177
178     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
179     data = resource_find_private_data(This, refguid);
180     if (data == NULL) return WINED3DERR_NOTFOUND;
181
182     if (*pSizeOfData < data->size) {
183         *pSizeOfData = data->size;
184         return WINED3DERR_MOREDATA;
185     }
186
187     if (data->flags & WINED3DSPD_IUNKNOWN) {
188         *(LPUNKNOWN *)pData = data->ptr.object;
189         if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
190             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
191              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
192              * Blob
193              */
194             IUnknown_AddRef(data->ptr.object);
195         }
196     }
197     else {
198         memcpy(pData, data->ptr.data, data->size);
199     }
200
201     return WINED3D_OK;
202 }
203 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
204 {
205     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
206     PrivateData *data;
207
208     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
209     data = resource_find_private_data(This, refguid);
210     if (data == NULL) return WINED3DERR_NOTFOUND;
211
212     if (data->flags & WINED3DSPD_IUNKNOWN)
213     {
214         if (data->ptr.object != NULL)
215             IUnknown_Release(data->ptr.object);
216     } else {
217         HeapFree(GetProcessHeap(), 0, data->ptr.data);
218     }
219     list_remove(&data->entry);
220
221     HeapFree(GetProcessHeap(), 0, data);
222
223     return WINED3D_OK;
224 }
225
226 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
227 {
228     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
229     DWORD PriorityOld = This->resource.priority;
230     This->resource.priority = PriorityNew;
231     TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
232     return PriorityOld;
233 }
234
235 DWORD resource_get_priority(IWineD3DResource *iface)
236 {
237     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
238     TRACE("(%p) : returning %d\n", This, This->resource.priority );
239     return This->resource.priority;
240 }
241
242 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
243 {
244     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
245     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
246     return This->resource.resourceType;
247 }
248
249 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
250 {
251     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
252     IUnknown_AddRef(This->resource.parent);
253     *pParent = This->resource.parent;
254     return WINED3D_OK;
255 }
256
257 void dumpResources(struct list *list) {
258     IWineD3DResourceImpl *resource;
259
260     LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
261         FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));
262     }
263 }