.gitignore: Added wildcards to ignore generated resource files.
[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 %ld\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 %ld\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     for (data = &This->resource.privateData; *data != NULL; data = &(*data)->next)
96     {
97         if (IsEqualGUID(&(*data)->tag, tag)) break;
98     }
99     return data;
100 }
101
102 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
103     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
104     PrivateData **data;
105
106     TRACE("(%p) : %p %p %ld %ld\n", This, refguid, pData, SizeOfData, Flags);
107     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
108     if (*data == NULL)
109     {
110         *data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**data));
111         if (NULL == *data) return E_OUTOFMEMORY;
112
113         (*data)->tag = *refguid;
114         (*data)->flags = Flags;
115 #if 0
116         (*data)->uniquenessValue = This->uniquenessValue;
117 #endif
118         if (Flags & D3DSPD_IUNKNOWN) {
119             (*data)->ptr.object = (LPUNKNOWN)pData;
120             (*data)->size = sizeof(LPUNKNOWN);
121             IUnknown_AddRef((*data)->ptr.object);
122         }
123         else
124         {
125             (*data)->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
126             if (NULL == (*data)->ptr.data) {
127                 HeapFree(GetProcessHeap(), 0, *data);
128                 return E_OUTOFMEMORY;
129             }
130             (*data)->size = SizeOfData;
131             memcpy((*data)->ptr.data, pData, SizeOfData);
132         }
133         /* link it in */
134         (*data)->next = This->resource.privateData;
135         This->resource.privateData = *data;
136         return WINED3D_OK;
137
138     } else {
139         /* I don't actually know how windows handles this case. The only
140             * reason I don't just call FreePrivateData is because I want to
141             * guarantee SetPrivateData working when using LPUNKNOWN or data
142             * that is no larger than the old data. */
143         return E_FAIL;
144
145     }
146
147     return WINED3D_OK;
148 }
149
150 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
151     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
152     PrivateData **data;
153
154     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
155     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
156     if (*data == NULL) return WINED3DERR_NOTFOUND;
157
158
159 #if 0 /* This may not be right. */
160     if (((*data)->flags & D3DSPD_VOLATILE)
161         && (*data)->uniquenessValue != This->uniquenessValue)
162         return DDERR_EXPIRED;
163 #endif
164     if (*pSizeOfData < (*data)->size) {
165         *pSizeOfData = (*data)->size;
166         return WINED3DERR_MOREDATA;
167     }
168
169     if ((*data)->flags & D3DSPD_IUNKNOWN) {
170         *(LPUNKNOWN *)pData = (*data)->ptr.object;
171         IUnknown_AddRef((*data)->ptr.object);
172     }
173     else {
174         memcpy(pData, (*data)->ptr.data, (*data)->size);
175     }
176
177     return WINED3D_OK;
178 }
179 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
180     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
181     PrivateData **data;
182
183     TRACE("(%p) : %p\n", This, refguid);
184     /* TODO: move this code off into a linked list class */
185     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
186     if (*data == NULL) return WINED3DERR_NOTFOUND;
187
188     *data = (*data)->next;
189
190     if ((*data)->flags & D3DSPD_IUNKNOWN)
191     {
192         if ((*data)->ptr.object != NULL)
193             IUnknown_Release((*data)->ptr.object);
194     } else {
195         HeapFree(GetProcessHeap(), 0, (*data)->ptr.data);
196     }
197
198     HeapFree(GetProcessHeap(), 0, *data);
199
200     return WINED3D_OK;
201 }
202
203 /* Priority support is not implemented yet */
204 DWORD    WINAPI        IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
205     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
206     FIXME("(%p) : stub\n", This);
207     return 0;
208 }
209 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
210     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
211     FIXME("(%p) : stub\n", This);
212     return 0;
213 }
214
215 /* Preloading of resources is not supported yet */
216 void     WINAPI        IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
217     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
218     FIXME("(%p) : stub\n", This);
219 }
220
221 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
222     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
223     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
224     return This->resource.resourceType;
225 }
226
227 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
228     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
229     IUnknown_AddRef(This->resource.parent);
230     *pParent = This->resource.parent;
231     return WINED3D_OK;
232 }
233
234 void dumpResources(ResourceList *resources) {
235     ResourceList *iterator = resources;
236
237     while(iterator) {
238         FIXME("Leftover resource %p with type %d,%s\n", iterator->resource, IWineD3DResource_GetType(iterator->resource), debug_d3dresourcetype(IWineD3DResource_GetType(iterator->resource)));
239         iterator = iterator->next;
240     }
241 }
242
243 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
244 {
245     /* IUnknown */
246     IWineD3DResourceImpl_QueryInterface,
247     IWineD3DResourceImpl_AddRef,
248     IWineD3DResourceImpl_Release,
249     /* IWineD3DResource */
250     IWineD3DResourceImpl_GetParent,
251     IWineD3DResourceImpl_GetDevice,
252     IWineD3DResourceImpl_SetPrivateData,
253     IWineD3DResourceImpl_GetPrivateData,
254     IWineD3DResourceImpl_FreePrivateData,
255     IWineD3DResourceImpl_SetPriority,
256     IWineD3DResourceImpl_GetPriority,
257     IWineD3DResourceImpl_PreLoad,
258     IWineD3DResourceImpl_GetType
259 };