msi: Constify some variables.
[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 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29
30 /* IWineD3DResource IUnknown parts follow: */
31 HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, LPVOID *ppobj)
32 {
33     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
34     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
35     if (IsEqualGUID(riid, &IID_IUnknown)
36         || IsEqualGUID(riid, &IID_IWineD3DBase)
37         || IsEqualGUID(riid, &IID_IWineD3DResource)) {
38         IUnknown_AddRef(iface);
39         *ppobj = This;
40         return S_OK;
41     }
42     *ppobj = NULL;
43     return E_NOINTERFACE;
44 }
45
46 ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface) {
47     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
48     ULONG ref = InterlockedIncrement(&This->resource.ref);
49     TRACE("(%p) : AddRef increasing from %d\n", This, ref - 1);
50     return ref; 
51 }
52
53 ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface) {
54     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
55     ULONG ref = InterlockedDecrement(&This->resource.ref);
56     TRACE("(%p) : Releasing from %d\n", This, ref + 1);
57     if (ref == 0) {
58         IWineD3DResourceImpl_CleanUp(iface);
59         HeapFree(GetProcessHeap(), 0, This);
60     }
61     return ref;
62 }
63
64 /* class static (not in vtable) */
65 void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface){
66     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
67     struct list *e1, *e2;
68     PrivateData *data;
69     HRESULT hr;
70
71     TRACE("(%p) Cleaning up resource\n", This);
72     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
73         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
74         globalChangeGlRam(-This->resource.size);
75     }
76
77     LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
78         data = LIST_ENTRY(e1, PrivateData, entry);
79         hr = IWineD3DResourceImpl_FreePrivateData(iface, &data->tag);
80         if(hr != WINED3D_OK) {
81             ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
82         }
83     }
84
85     HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
86     This->resource.allocatedMemory = 0;
87
88     if (This->resource.wineD3DDevice != NULL) {
89         IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
90     }/* NOTE: this is not really an error for systemmem resoruces */
91     return;
92 }
93
94 /* IWineD3DResource Interface follow: */
95 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
96     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
97     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
98     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
99     IWineD3DDevice_AddRef(*ppDevice);
100     return WINED3D_OK;
101 }
102
103 static PrivateData* IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
104                     REFGUID tag)
105 {
106     PrivateData *data;
107     struct list *entry;
108
109     TRACE("Searching for private data %s\n", debugstr_guid(tag));
110     LIST_FOR_EACH(entry, &This->resource.privateData)
111     {
112         data = LIST_ENTRY(entry, PrivateData, entry);
113         if (IsEqualGUID(&data->tag, tag)) {
114             TRACE("Found %p\n", data);
115             return data;
116         }
117     }
118     TRACE("Not found\n");
119     return NULL;
120 }
121
122 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
123     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
124     PrivateData *data;
125
126     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
127     IWineD3DResourceImpl_FreePrivateData(iface, refguid);
128
129     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
130     if (NULL == data) return E_OUTOFMEMORY;
131
132     data->tag = *refguid;
133     data->flags = Flags;
134 #if 0
135         (*data)->uniquenessValue = This->uniquenessValue;
136 #endif
137     if (Flags & WINED3DSPD_IUNKNOWN) {
138         if(SizeOfData != sizeof(IUnknown *)) {
139             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
140             return WINED3DERR_INVALIDCALL;
141         }
142         data->ptr.object = (LPUNKNOWN)pData;
143         data->size = sizeof(LPUNKNOWN);
144         IUnknown_AddRef(data->ptr.object);
145     }
146     else
147     {
148         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
149         if (NULL == data->ptr.data) {
150             HeapFree(GetProcessHeap(), 0, data);
151             return E_OUTOFMEMORY;
152         }
153         data->size = SizeOfData;
154         memcpy(data->ptr.data, pData, SizeOfData);
155     }
156     list_add_tail(&This->resource.privateData, &data->entry);
157
158     return WINED3D_OK;
159 }
160
161 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
162     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
163     PrivateData *data;
164
165     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
166     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
167     if (data == NULL) return WINED3DERR_NOTFOUND;
168
169
170 #if 0 /* This may not be right. */
171     if (((*data)->flags & WINED3DSPD_VOLATILE)
172         && (*data)->uniquenessValue != This->uniquenessValue)
173         return DDERR_EXPIRED;
174 #endif
175     if (*pSizeOfData < data->size) {
176         *pSizeOfData = data->size;
177         return WINED3DERR_MOREDATA;
178     }
179
180     if (data->flags & WINED3DSPD_IUNKNOWN) {
181         *(LPUNKNOWN *)pData = data->ptr.object;
182         if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
183             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
184              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
185              * Blob
186              */
187             IUnknown_AddRef(data->ptr.object);
188         }
189     }
190     else {
191         memcpy(pData, data->ptr.data, data->size);
192     }
193
194     return WINED3D_OK;
195 }
196 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
197     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
198     PrivateData *data;
199
200     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
201     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
202     if (data == NULL) return WINED3DERR_NOTFOUND;
203
204     if (data->flags & WINED3DSPD_IUNKNOWN)
205     {
206         if (data->ptr.object != NULL)
207             IUnknown_Release(data->ptr.object);
208     } else {
209         HeapFree(GetProcessHeap(), 0, data->ptr.data);
210     }
211     list_remove(&data->entry);
212
213     HeapFree(GetProcessHeap(), 0, data);
214
215     return WINED3D_OK;
216 }
217
218 /* Priority support is not implemented yet */
219 DWORD    WINAPI        IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
220     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
221     FIXME("(%p) : stub\n", This);
222     return 0;
223 }
224 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
225     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
226     FIXME("(%p) : stub\n", This);
227     return 0;
228 }
229
230 /* Preloading of resources is not supported yet */
231 void     WINAPI        IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
232     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
233     FIXME("(%p) : stub\n", This);
234 }
235
236 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
237     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
238     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
239     return This->resource.resourceType;
240 }
241
242 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
243     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
244     IUnknown_AddRef(This->resource.parent);
245     *pParent = This->resource.parent;
246     return WINED3D_OK;
247 }
248
249 void dumpResources(ResourceList *resources) {
250     ResourceList *iterator = resources;
251
252     while(iterator) {
253         FIXME("Leftover resource %p with type %d,%s\n", iterator->resource, IWineD3DResource_GetType(iterator->resource), debug_d3dresourcetype(IWineD3DResource_GetType(iterator->resource)));
254         iterator = iterator->next;
255     }
256 }
257
258 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
259 {
260     /* IUnknown */
261     IWineD3DResourceImpl_QueryInterface,
262     IWineD3DResourceImpl_AddRef,
263     IWineD3DResourceImpl_Release,
264     /* IWineD3DResource */
265     IWineD3DResourceImpl_GetParent,
266     IWineD3DResourceImpl_GetDevice,
267     IWineD3DResourceImpl_SetPrivateData,
268     IWineD3DResourceImpl_GetPrivateData,
269     IWineD3DResourceImpl_FreePrivateData,
270     IWineD3DResourceImpl_SetPriority,
271     IWineD3DResourceImpl_GetPriority,
272     IWineD3DResourceImpl_PreLoad,
273     IWineD3DResourceImpl_GetType
274 };