wined3d: Alloc / free shader private data and blt stuff in Init3D/Uninit3D.
[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         WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -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.heapMemory);
86     This->resource.allocatedMemory = 0;
87     This->resource.heapMemory = 0;
88
89     if (This->resource.wineD3DDevice != NULL) {
90         IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
91     }/* NOTE: this is not really an error for system memory resources */
92     return;
93 }
94
95 /* IWineD3DResource Interface follows: */
96 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
97     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
98     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
99     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
100     IWineD3DDevice_AddRef(*ppDevice);
101     return WINED3D_OK;
102 }
103
104 static PrivateData* IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
105                     REFGUID tag)
106 {
107     PrivateData *data;
108     struct list *entry;
109
110     TRACE("Searching for private data %s\n", debugstr_guid(tag));
111     LIST_FOR_EACH(entry, &This->resource.privateData)
112     {
113         data = LIST_ENTRY(entry, PrivateData, entry);
114         if (IsEqualGUID(&data->tag, tag)) {
115             TRACE("Found %p\n", data);
116             return data;
117         }
118     }
119     TRACE("Not found\n");
120     return NULL;
121 }
122
123 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
124     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
125     PrivateData *data;
126
127     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
128     IWineD3DResourceImpl_FreePrivateData(iface, refguid);
129
130     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
131     if (NULL == data) return E_OUTOFMEMORY;
132
133     data->tag = *refguid;
134     data->flags = Flags;
135 #if 0
136         (*data)->uniquenessValue = This->uniquenessValue;
137 #endif
138     if (Flags & WINED3DSPD_IUNKNOWN) {
139         if(SizeOfData != sizeof(IUnknown *)) {
140             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
141             HeapFree(GetProcessHeap(), 0, data);
142             return WINED3DERR_INVALIDCALL;
143         }
144         data->ptr.object = (LPUNKNOWN)pData;
145         data->size = sizeof(LPUNKNOWN);
146         IUnknown_AddRef(data->ptr.object);
147     }
148     else
149     {
150         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
151         if (NULL == data->ptr.data) {
152             HeapFree(GetProcessHeap(), 0, data);
153             return E_OUTOFMEMORY;
154         }
155         data->size = SizeOfData;
156         memcpy(data->ptr.data, pData, SizeOfData);
157     }
158     list_add_tail(&This->resource.privateData, &data->entry);
159
160     return WINED3D_OK;
161 }
162
163 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
164     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
165     PrivateData *data;
166
167     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
168     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
169     if (data == NULL) return WINED3DERR_NOTFOUND;
170
171
172 #if 0 /* This may not be right. */
173     if (((*data)->flags & WINED3DSPD_VOLATILE)
174         && (*data)->uniquenessValue != This->uniquenessValue)
175         return DDERR_EXPIRED;
176 #endif
177     if (*pSizeOfData < data->size) {
178         *pSizeOfData = data->size;
179         return WINED3DERR_MOREDATA;
180     }
181
182     if (data->flags & WINED3DSPD_IUNKNOWN) {
183         *(LPUNKNOWN *)pData = data->ptr.object;
184         if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
185             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
186              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
187              * Blob
188              */
189             IUnknown_AddRef(data->ptr.object);
190         }
191     }
192     else {
193         memcpy(pData, data->ptr.data, data->size);
194     }
195
196     return WINED3D_OK;
197 }
198 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
199     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
200     PrivateData *data;
201
202     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
203     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
204     if (data == NULL) return WINED3DERR_NOTFOUND;
205
206     if (data->flags & WINED3DSPD_IUNKNOWN)
207     {
208         if (data->ptr.object != NULL)
209             IUnknown_Release(data->ptr.object);
210     } else {
211         HeapFree(GetProcessHeap(), 0, data->ptr.data);
212     }
213     list_remove(&data->entry);
214
215     HeapFree(GetProcessHeap(), 0, data);
216
217     return WINED3D_OK;
218 }
219
220 /* Priority support is not implemented yet */
221 DWORD    WINAPI        IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
222     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
223     FIXME("(%p) : stub\n", This);
224     return 0;
225 }
226 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
227     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
228     FIXME("(%p) : stub\n", This);
229     return 0;
230 }
231
232 /* Preloading of resources is not supported yet */
233 void     WINAPI        IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
234     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
235     FIXME("(%p) : stub\n", This);
236 }
237
238 void     WINAPI        IWineD3DResourceImpl_UnLoad(IWineD3DResource *iface) {
239     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
240     FIXME("(%p) : stub\n", This);
241 }
242
243 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
244     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
245     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
246     return This->resource.resourceType;
247 }
248
249 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
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 }
263
264 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
265 {
266     /* IUnknown */
267     IWineD3DResourceImpl_QueryInterface,
268     IWineD3DResourceImpl_AddRef,
269     IWineD3DResourceImpl_Release,
270     /* IWineD3DResource */
271     IWineD3DResourceImpl_GetParent,
272     IWineD3DResourceImpl_GetDevice,
273     IWineD3DResourceImpl_SetPrivateData,
274     IWineD3DResourceImpl_GetPrivateData,
275     IWineD3DResourceImpl_FreePrivateData,
276     IWineD3DResourceImpl_SetPriority,
277     IWineD3DResourceImpl_GetPriority,
278     IWineD3DResourceImpl_PreLoad,
279     IWineD3DResourceImpl_UnLoad,
280     IWineD3DResourceImpl_GetType
281 };