wined3d: Store an IWineD3DDeviceImpl pointer in IWineD3DBaseShaderClass.
[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  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29
30 struct private_data
31 {
32     struct list entry;
33
34     GUID tag;
35     DWORD flags; /* DDSPD_* */
36
37     union
38     {
39         void *data;
40         IUnknown *object;
41     } ptr;
42
43     DWORD size;
44 };
45
46 HRESULT resource_init(IWineD3DResource *iface, WINED3DRESOURCETYPE resource_type,
47         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct wined3d_format *format,
48         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
49 {
50     struct IWineD3DResourceClass *resource = &((IWineD3DResourceImpl *)iface)->resource;
51
52     resource->device = device;
53     resource->resourceType = resource_type;
54     resource->ref = 1;
55     resource->pool = pool;
56     resource->format = format;
57     resource->usage = usage;
58     resource->size = size;
59     resource->priority = 0;
60     resource->parent = parent;
61     resource->parent_ops = parent_ops;
62     list_init(&resource->privateData);
63
64     if (size)
65     {
66         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
67         if (!resource->heapMemory)
68         {
69             ERR("Out of memory!\n");
70             return WINED3DERR_OUTOFVIDEOMEMORY;
71         }
72     }
73     else
74     {
75         resource->heapMemory = NULL;
76     }
77     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
78
79     /* Check that we have enough video ram left */
80     if (pool == WINED3DPOOL_DEFAULT)
81     {
82         if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
83         {
84             ERR("Out of adapter memory\n");
85             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
86             return WINED3DERR_OUTOFVIDEOMEMORY;
87         }
88         WineD3DAdapterChangeGLRam(device, size);
89     }
90
91     device_resource_add(device, iface);
92
93     return WINED3D_OK;
94 }
95
96 void resource_cleanup(IWineD3DResource *iface)
97 {
98     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
99     struct private_data *data;
100     struct list *e1, *e2;
101     HRESULT hr;
102
103     TRACE("(%p) Cleaning up resource\n", This);
104     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
105         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
106         WineD3DAdapterChangeGLRam(This->resource.device, -This->resource.size);
107     }
108
109     LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData)
110     {
111         data = LIST_ENTRY(e1, struct private_data, entry);
112         hr = resource_free_private_data(iface, &data->tag);
113         if(hr != WINED3D_OK) {
114             ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
115         }
116     }
117
118     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
119     This->resource.allocatedMemory = 0;
120     This->resource.heapMemory = 0;
121
122     if (This->resource.device) device_resource_released(This->resource.device, iface);
123 }
124
125 void resource_unload(IWineD3DResourceImpl *resource)
126 {
127     context_resource_unloaded(resource->resource.device, (IWineD3DResource *)resource,
128             resource->resource.resourceType);
129 }
130
131 static struct private_data *resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
132 {
133     struct private_data *data;
134     struct list *entry;
135
136     TRACE("Searching for private data %s\n", debugstr_guid(tag));
137     LIST_FOR_EACH(entry, &This->resource.privateData)
138     {
139         data = LIST_ENTRY(entry, struct private_data, entry);
140         if (IsEqualGUID(&data->tag, tag)) {
141             TRACE("Found %p\n", data);
142             return data;
143         }
144     }
145     TRACE("Not found\n");
146     return NULL;
147 }
148
149 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
150         const void *pData, DWORD SizeOfData, DWORD flags)
151 {
152     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
153     struct private_data *data;
154
155     TRACE("iface %p, riid %s, data %p, data_size %u, flags %#x.\n",
156             iface, debugstr_guid(refguid), pData, SizeOfData, flags);
157
158     resource_free_private_data(iface, refguid);
159
160     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
161     if (!data) return E_OUTOFMEMORY;
162
163     data->tag = *refguid;
164     data->flags = flags;
165
166     if (flags & WINED3DSPD_IUNKNOWN)
167     {
168         if (SizeOfData != sizeof(IUnknown *))
169         {
170             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
171             HeapFree(GetProcessHeap(), 0, data);
172             return WINED3DERR_INVALIDCALL;
173         }
174         data->ptr.object = (LPUNKNOWN)pData;
175         data->size = sizeof(LPUNKNOWN);
176         IUnknown_AddRef(data->ptr.object);
177     }
178     else
179     {
180         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
181         if (!data->ptr.data)
182         {
183             HeapFree(GetProcessHeap(), 0, data);
184             return E_OUTOFMEMORY;
185         }
186         data->size = SizeOfData;
187         memcpy(data->ptr.data, pData, SizeOfData);
188     }
189     list_add_tail(&This->resource.privateData, &data->entry);
190
191     return WINED3D_OK;
192 }
193
194 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
195 {
196     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
197     struct private_data *data;
198
199     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
200     data = resource_find_private_data(This, refguid);
201     if (!data) return WINED3DERR_NOTFOUND;
202
203     if (*pSizeOfData < data->size) {
204         *pSizeOfData = data->size;
205         return WINED3DERR_MOREDATA;
206     }
207
208     if (data->flags & WINED3DSPD_IUNKNOWN) {
209         *(LPUNKNOWN *)pData = data->ptr.object;
210         if (((IWineD3DImpl *)This->resource.device->wined3d)->dxVersion != 7)
211         {
212             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
213              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
214              * Blob
215              */
216             IUnknown_AddRef(data->ptr.object);
217         }
218     }
219     else {
220         memcpy(pData, data->ptr.data, data->size);
221     }
222
223     return WINED3D_OK;
224 }
225 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
226 {
227     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
228     struct private_data *data;
229
230     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
231     data = resource_find_private_data(This, refguid);
232     if (!data) return WINED3DERR_NOTFOUND;
233
234     if (data->flags & WINED3DSPD_IUNKNOWN)
235     {
236         if (data->ptr.object)
237             IUnknown_Release(data->ptr.object);
238     }
239     else
240     {
241         HeapFree(GetProcessHeap(), 0, data->ptr.data);
242     }
243     list_remove(&data->entry);
244
245     HeapFree(GetProcessHeap(), 0, data);
246
247     return WINED3D_OK;
248 }
249
250 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
251 {
252     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
253     DWORD PriorityOld = This->resource.priority;
254     This->resource.priority = PriorityNew;
255     TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
256     return PriorityOld;
257 }
258
259 DWORD resource_get_priority(IWineD3DResource *iface)
260 {
261     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
262     TRACE("(%p) : returning %d\n", This, This->resource.priority );
263     return This->resource.priority;
264 }
265
266 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
267 {
268     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
269     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
270     return This->resource.resourceType;
271 }