shell32/tests: Use a different name for the return value.
[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 void resource_cleanup(IWineD3DResource *iface)
31 {
32     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
33     struct list *e1, *e2;
34     PrivateData *data;
35     HRESULT hr;
36
37     TRACE("(%p) Cleaning up resource\n", This);
38     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
39         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
40         WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
41     }
42
43     LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
44         data = LIST_ENTRY(e1, PrivateData, entry);
45         hr = resource_free_private_data(iface, &data->tag);
46         if(hr != WINED3D_OK) {
47             ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
48         }
49     }
50
51     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
52     This->resource.allocatedMemory = 0;
53     This->resource.heapMemory = 0;
54
55     if (This->resource.wineD3DDevice != NULL) {
56         IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
57     }/* NOTE: this is not really an error for system memory resources */
58     return;
59 }
60
61 HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
62 {
63     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
64     TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
65     *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
66     IWineD3DDevice_AddRef(*ppDevice);
67     return WINED3D_OK;
68 }
69
70 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
71 {
72     PrivateData *data;
73     struct list *entry;
74
75     TRACE("Searching for private data %s\n", debugstr_guid(tag));
76     LIST_FOR_EACH(entry, &This->resource.privateData)
77     {
78         data = LIST_ENTRY(entry, PrivateData, entry);
79         if (IsEqualGUID(&data->tag, tag)) {
80             TRACE("Found %p\n", data);
81             return data;
82         }
83     }
84     TRACE("Not found\n");
85     return NULL;
86 }
87
88 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
89         const void *pData, DWORD SizeOfData, DWORD Flags)
90 {
91     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
92     PrivateData *data;
93
94     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
95     resource_free_private_data(iface, refguid);
96
97     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
98     if (NULL == data) return E_OUTOFMEMORY;
99
100     data->tag = *refguid;
101     data->flags = Flags;
102
103     if (Flags & WINED3DSPD_IUNKNOWN) {
104         if(SizeOfData != sizeof(IUnknown *)) {
105             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
106             HeapFree(GetProcessHeap(), 0, data);
107             return WINED3DERR_INVALIDCALL;
108         }
109         data->ptr.object = (LPUNKNOWN)pData;
110         data->size = sizeof(LPUNKNOWN);
111         IUnknown_AddRef(data->ptr.object);
112     }
113     else
114     {
115         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
116         if (NULL == data->ptr.data) {
117             HeapFree(GetProcessHeap(), 0, data);
118             return E_OUTOFMEMORY;
119         }
120         data->size = SizeOfData;
121         memcpy(data->ptr.data, pData, SizeOfData);
122     }
123     list_add_tail(&This->resource.privateData, &data->entry);
124
125     return WINED3D_OK;
126 }
127
128 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
129 {
130     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
131     PrivateData *data;
132
133     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
134     data = resource_find_private_data(This, refguid);
135     if (data == NULL) return WINED3DERR_NOTFOUND;
136
137     if (*pSizeOfData < data->size) {
138         *pSizeOfData = data->size;
139         return WINED3DERR_MOREDATA;
140     }
141
142     if (data->flags & WINED3DSPD_IUNKNOWN) {
143         *(LPUNKNOWN *)pData = data->ptr.object;
144         if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
145             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
146              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
147              * Blob
148              */
149             IUnknown_AddRef(data->ptr.object);
150         }
151     }
152     else {
153         memcpy(pData, data->ptr.data, data->size);
154     }
155
156     return WINED3D_OK;
157 }
158 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
159 {
160     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
161     PrivateData *data;
162
163     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
164     data = resource_find_private_data(This, refguid);
165     if (data == NULL) return WINED3DERR_NOTFOUND;
166
167     if (data->flags & WINED3DSPD_IUNKNOWN)
168     {
169         if (data->ptr.object != NULL)
170             IUnknown_Release(data->ptr.object);
171     } else {
172         HeapFree(GetProcessHeap(), 0, data->ptr.data);
173     }
174     list_remove(&data->entry);
175
176     HeapFree(GetProcessHeap(), 0, data);
177
178     return WINED3D_OK;
179 }
180
181 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
182 {
183     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
184     DWORD PriorityOld = This->resource.priority;
185     This->resource.priority = PriorityNew;
186     TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
187     return PriorityOld;
188 }
189
190 DWORD resource_get_priority(IWineD3DResource *iface)
191 {
192     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
193     TRACE("(%p) : returning %d\n", This, This->resource.priority );
194     return This->resource.priority;
195 }
196
197 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
198 {
199     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
200     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
201     return This->resource.resourceType;
202 }
203
204 HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
205 {
206     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
207     IUnknown_AddRef(This->resource.parent);
208     *pParent = This->resource.parent;
209     return WINED3D_OK;
210 }
211
212 void dumpResources(struct list *list) {
213     IWineD3DResourceImpl *resource;
214
215     LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
216         FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));
217     }
218 }