mshtml/tests: Fix some test failures on IE8.
[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(struct IWineD3DResourceClass *resource, WINED3DRESOURCETYPE resource_type,
30         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
31         WINED3DPOOL pool, IUnknown *parent)
32 {
33     resource->wineD3DDevice = device;
34     resource->parent = parent;
35     resource->resourceType = resource_type;
36     resource->ref = 1;
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);
43
44     if (size)
45     {
46         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
47         if (!resource->heapMemory)
48         {
49             ERR("Out of memory!\n");
50             return WINED3DERR_OUTOFVIDEOMEMORY;
51         }
52     }
53     else
54     {
55         resource->heapMemory = NULL;
56     }
57     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
58
59     /* Check that we have enough video ram left */
60     if (pool == WINED3DPOOL_DEFAULT)
61     {
62         if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
63         {
64             ERR("Out of adapter memory\n");
65             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
66             return WINED3DERR_OUTOFVIDEOMEMORY;
67         }
68         WineD3DAdapterChangeGLRam(device, size);
69     }
70
71     return WINED3D_OK;
72 }
73
74 void resource_cleanup(IWineD3DResource *iface)
75 {
76     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
77     struct list *e1, *e2;
78     PrivateData *data;
79     HRESULT hr;
80
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);
85     }
86
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);
92         }
93     }
94
95     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
96     This->resource.allocatedMemory = 0;
97     This->resource.heapMemory = 0;
98
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 */
102     return;
103 }
104
105 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
106 {
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);
111     return WINED3D_OK;
112 }
113
114 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
115 {
116     PrivateData *data;
117     struct list *entry;
118
119     TRACE("Searching for private data %s\n", debugstr_guid(tag));
120     LIST_FOR_EACH(entry, &This->resource.privateData)
121     {
122         data = LIST_ENTRY(entry, PrivateData, entry);
123         if (IsEqualGUID(&data->tag, tag)) {
124             TRACE("Found %p\n", data);
125             return data;
126         }
127     }
128     TRACE("Not found\n");
129     return NULL;
130 }
131
132 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
133         const void *pData, DWORD SizeOfData, DWORD Flags)
134 {
135     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
136     PrivateData *data;
137
138     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
139     resource_free_private_data(iface, refguid);
140
141     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
142     if (NULL == data) return E_OUTOFMEMORY;
143
144     data->tag = *refguid;
145     data->flags = Flags;
146
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;
152         }
153         data->ptr.object = (LPUNKNOWN)pData;
154         data->size = sizeof(LPUNKNOWN);
155         IUnknown_AddRef(data->ptr.object);
156     }
157     else
158     {
159         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
160         if (NULL == data->ptr.data) {
161             HeapFree(GetProcessHeap(), 0, data);
162             return E_OUTOFMEMORY;
163         }
164         data->size = SizeOfData;
165         memcpy(data->ptr.data, pData, SizeOfData);
166     }
167     list_add_tail(&This->resource.privateData, &data->entry);
168
169     return WINED3D_OK;
170 }
171
172 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
173 {
174     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
175     PrivateData *data;
176
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;
180
181     if (*pSizeOfData < data->size) {
182         *pSizeOfData = data->size;
183         return WINED3DERR_MOREDATA;
184     }
185
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
191              * Blob
192              */
193             IUnknown_AddRef(data->ptr.object);
194         }
195     }
196     else {
197         memcpy(pData, data->ptr.data, data->size);
198     }
199
200     return WINED3D_OK;
201 }
202 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
203 {
204     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
205     PrivateData *data;
206
207     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
208     data = resource_find_private_data(This, refguid);
209     if (data == NULL) return WINED3DERR_NOTFOUND;
210
211     if (data->flags & WINED3DSPD_IUNKNOWN)
212     {
213         if (data->ptr.object != NULL)
214             IUnknown_Release(data->ptr.object);
215     } else {
216         HeapFree(GetProcessHeap(), 0, data->ptr.data);
217     }
218     list_remove(&data->entry);
219
220     HeapFree(GetProcessHeap(), 0, data);
221
222     return WINED3D_OK;
223 }
224
225 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
226 {
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 );
231     return PriorityOld;
232 }
233
234 DWORD resource_get_priority(IWineD3DResource *iface)
235 {
236     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
237     TRACE("(%p) : returning %d\n", This, This->resource.priority );
238     return This->resource.priority;
239 }
240
241 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
242 {
243     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
244     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
245     return This->resource.resourceType;
246 }
247
248 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
249 {
250     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
251     IUnknown_AddRef(This->resource.parent);
252     *pParent = This->resource.parent;
253     return WINED3D_OK;
254 }
255
256 void dumpResources(struct list *list) {
257     IWineD3DResourceImpl *resource;
258
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)));
261     }
262 }