mcicda: Exclude unused headers.
[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     TRACE("(%p) Cleaning up resource\n", This);
68     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
69         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
70         globalChangeGlRam(-This->resource.size);
71     }
72
73     HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
74     This->resource.allocatedMemory = 0;
75
76     if (This->resource.wineD3DDevice != NULL) {
77         IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
78     }/* NOTE: this is not really an error for systemmem resoruces */
79     return;
80 }
81
82 /* IWineD3DResource Interface follow: */
83 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
84     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
85     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
86     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
87     IWineD3DDevice_AddRef(*ppDevice);
88     return WINED3D_OK;
89 }
90
91 static PrivateData* IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
92                     REFGUID tag)
93 {
94     PrivateData *data;
95     struct list *entry;
96
97     TRACE("Searching for private data %s\n", debugstr_guid(tag));
98     LIST_FOR_EACH(entry, &This->resource.privateData)
99     {
100         data = LIST_ENTRY(entry, PrivateData, entry);
101         if (IsEqualGUID(&data->tag, tag)) {
102             TRACE("Found %p\n", data);
103             return data;
104         }
105     }
106     TRACE("Not found\n");
107     return NULL;
108 }
109
110 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
111     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
112     PrivateData *data;
113
114     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
115     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
116     if (data == NULL)
117     {
118         data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
119         if (NULL == data) return E_OUTOFMEMORY;
120
121         data->tag = *refguid;
122         data->flags = Flags;
123 #if 0
124         (*data)->uniquenessValue = This->uniquenessValue;
125 #endif
126         if (Flags & WINED3DSPD_IUNKNOWN) {
127             data->ptr.object = (LPUNKNOWN)pData;
128             data->size = sizeof(LPUNKNOWN);
129             IUnknown_AddRef(data->ptr.object);
130         }
131         else
132         {
133             data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
134             if (NULL == data->ptr.data) {
135                 HeapFree(GetProcessHeap(), 0, data);
136                 return E_OUTOFMEMORY;
137             }
138             data->size = SizeOfData;
139             memcpy(data->ptr.data, pData, SizeOfData);
140         }
141         list_add_tail(&This->resource.privateData, &data->entry);
142         return WINED3D_OK;
143
144     } else {
145         /* I don't actually know how windows handles this case. The only
146          * reason I don't just call FreePrivateData is because I want to
147          * guarantee SetPrivateData working when using LPUNKNOWN or data
148          * that is no larger than the old data.
149          */
150         FIXME("Handle overwriting private data in SetPrivateData\n");
151         return E_FAIL;
152
153     }
154
155     return WINED3D_OK;
156 }
157
158 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
159     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
160     PrivateData *data;
161
162     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
163     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
164     if (data == NULL) return WINED3DERR_NOTFOUND;
165
166
167 #if 0 /* This may not be right. */
168     if (((*data)->flags & WINED3DSPD_VOLATILE)
169         && (*data)->uniquenessValue != This->uniquenessValue)
170         return DDERR_EXPIRED;
171 #endif
172     if (*pSizeOfData < data->size) {
173         *pSizeOfData = data->size;
174         return WINED3DERR_MOREDATA;
175     }
176
177     if (data->flags & WINED3DSPD_IUNKNOWN) {
178         *(LPUNKNOWN *)pData = data->ptr.object;
179         IUnknown_AddRef(data->ptr.object);
180     }
181     else {
182         memcpy(pData, data->ptr.data, data->size);
183     }
184
185     return WINED3D_OK;
186 }
187 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
188     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
189     PrivateData *data;
190
191     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
192     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
193     if (data == NULL) return WINED3DERR_NOTFOUND;
194
195     if (data->flags & WINED3DSPD_IUNKNOWN)
196     {
197         if (data->ptr.object != NULL)
198             IUnknown_Release(data->ptr.object);
199     } else {
200         HeapFree(GetProcessHeap(), 0, data->ptr.data);
201     }
202     list_remove(&data->entry);
203
204     HeapFree(GetProcessHeap(), 0, data);
205
206     return WINED3D_OK;
207 }
208
209 /* Priority support is not implemented yet */
210 DWORD    WINAPI        IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
211     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
212     FIXME("(%p) : stub\n", This);
213     return 0;
214 }
215 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
216     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
217     FIXME("(%p) : stub\n", This);
218     return 0;
219 }
220
221 /* Preloading of resources is not supported yet */
222 void     WINAPI        IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
223     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
224     FIXME("(%p) : stub\n", This);
225 }
226
227 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
228     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
229     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
230     return This->resource.resourceType;
231 }
232
233 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
234     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
235     IUnknown_AddRef(This->resource.parent);
236     *pParent = This->resource.parent;
237     return WINED3D_OK;
238 }
239
240 void dumpResources(ResourceList *resources) {
241     ResourceList *iterator = resources;
242
243     while(iterator) {
244         FIXME("Leftover resource %p with type %d,%s\n", iterator->resource, IWineD3DResource_GetType(iterator->resource), debug_d3dresourcetype(IWineD3DResource_GetType(iterator->resource)));
245         iterator = iterator->next;
246     }
247 }
248
249 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
250 {
251     /* IUnknown */
252     IWineD3DResourceImpl_QueryInterface,
253     IWineD3DResourceImpl_AddRef,
254     IWineD3DResourceImpl_Release,
255     /* IWineD3DResource */
256     IWineD3DResourceImpl_GetParent,
257     IWineD3DResourceImpl_GetDevice,
258     IWineD3DResourceImpl_SetPrivateData,
259     IWineD3DResourceImpl_GetPrivateData,
260     IWineD3DResourceImpl_FreePrivateData,
261     IWineD3DResourceImpl_SetPriority,
262     IWineD3DResourceImpl_GetPriority,
263     IWineD3DResourceImpl_PreLoad,
264     IWineD3DResourceImpl_GetType
265 };