wined3d: Rename the IWineD3DPaletteImpl "Flags" field to "flags".
[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 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 HRESULT resource_init(IWineD3DResource *iface, WINED3DRESOURCETYPE resource_type,
31         IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct wined3d_format *format,
32         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
33 {
34     struct IWineD3DResourceClass *resource = &((IWineD3DResourceImpl *)iface)->resource;
35
36     resource->device = device;
37     resource->resourceType = resource_type;
38     resource->ref = 1;
39     resource->pool = pool;
40     resource->format = format;
41     resource->usage = usage;
42     resource->size = size;
43     resource->priority = 0;
44     resource->parent = parent;
45     resource->parent_ops = parent_ops;
46     list_init(&resource->privateData);
47
48     if (size)
49     {
50         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
51         if (!resource->heapMemory)
52         {
53             ERR("Out of memory!\n");
54             return WINED3DERR_OUTOFVIDEOMEMORY;
55         }
56     }
57     else
58     {
59         resource->heapMemory = NULL;
60     }
61     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
62
63     /* Check that we have enough video ram left */
64     if (pool == WINED3DPOOL_DEFAULT)
65     {
66         if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
67         {
68             ERR("Out of adapter memory\n");
69             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
70             return WINED3DERR_OUTOFVIDEOMEMORY;
71         }
72         WineD3DAdapterChangeGLRam(device, size);
73     }
74
75     device_resource_add(device, iface);
76
77     return WINED3D_OK;
78 }
79
80 void resource_cleanup(IWineD3DResource *iface)
81 {
82     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
83     struct list *e1, *e2;
84     PrivateData *data;
85     HRESULT hr;
86
87     TRACE("(%p) Cleaning up resource\n", This);
88     if (This->resource.pool == WINED3DPOOL_DEFAULT) {
89         TRACE("Decrementing device memory pool by %u\n", This->resource.size);
90         WineD3DAdapterChangeGLRam(This->resource.device, -This->resource.size);
91     }
92
93     LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
94         data = LIST_ENTRY(e1, PrivateData, entry);
95         hr = resource_free_private_data(iface, &data->tag);
96         if(hr != WINED3D_OK) {
97             ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
98         }
99     }
100
101     HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
102     This->resource.allocatedMemory = 0;
103     This->resource.heapMemory = 0;
104
105     if (This->resource.device) device_resource_released(This->resource.device, iface);
106 }
107
108 void resource_unload(IWineD3DResourceImpl *resource)
109 {
110     context_resource_unloaded(resource->resource.device, (IWineD3DResource *)resource,
111             resource->resource.resourceType);
112 }
113
114 static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
115 {
116     PrivateData *data;
117     struct list *entry;
118
119     TRACE("Searching for private data %s\n", debugstr_guid(tag));
120     LIST_FOR_EACH(entry, &This->resource.privateData)
121     {
122         data = LIST_ENTRY(entry, PrivateData, entry);
123         if (IsEqualGUID(&data->tag, tag)) {
124             TRACE("Found %p\n", data);
125             return data;
126         }
127     }
128     TRACE("Not found\n");
129     return NULL;
130 }
131
132 HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
133         const void *pData, DWORD SizeOfData, DWORD Flags)
134 {
135     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
136     PrivateData *data;
137
138     TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
139     resource_free_private_data(iface, refguid);
140
141     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
142     if (!data) return E_OUTOFMEMORY;
143
144     data->tag = *refguid;
145     data->flags = Flags;
146
147     if (Flags & WINED3DSPD_IUNKNOWN) {
148         if(SizeOfData != sizeof(IUnknown *)) {
149             WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
150             HeapFree(GetProcessHeap(), 0, data);
151             return WINED3DERR_INVALIDCALL;
152         }
153         data->ptr.object = (LPUNKNOWN)pData;
154         data->size = sizeof(LPUNKNOWN);
155         IUnknown_AddRef(data->ptr.object);
156     }
157     else
158     {
159         data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
160         if (!data->ptr.data)
161         {
162             HeapFree(GetProcessHeap(), 0, data);
163             return E_OUTOFMEMORY;
164         }
165         data->size = SizeOfData;
166         memcpy(data->ptr.data, pData, SizeOfData);
167     }
168     list_add_tail(&This->resource.privateData, &data->entry);
169
170     return WINED3D_OK;
171 }
172
173 HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
174 {
175     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
176     PrivateData *data;
177
178     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
179     data = resource_find_private_data(This, refguid);
180     if (!data) return WINED3DERR_NOTFOUND;
181
182     if (*pSizeOfData < data->size) {
183         *pSizeOfData = data->size;
184         return WINED3DERR_MOREDATA;
185     }
186
187     if (data->flags & WINED3DSPD_IUNKNOWN) {
188         *(LPUNKNOWN *)pData = data->ptr.object;
189         if (((IWineD3DImpl *)This->resource.device->wined3d)->dxVersion != 7)
190         {
191             /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
192              * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
193              * Blob
194              */
195             IUnknown_AddRef(data->ptr.object);
196         }
197     }
198     else {
199         memcpy(pData, data->ptr.data, data->size);
200     }
201
202     return WINED3D_OK;
203 }
204 HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
205 {
206     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
207     PrivateData *data;
208
209     TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
210     data = resource_find_private_data(This, refguid);
211     if (!data) return WINED3DERR_NOTFOUND;
212
213     if (data->flags & WINED3DSPD_IUNKNOWN)
214     {
215         if (data->ptr.object)
216             IUnknown_Release(data->ptr.object);
217     }
218     else
219     {
220         HeapFree(GetProcessHeap(), 0, data->ptr.data);
221     }
222     list_remove(&data->entry);
223
224     HeapFree(GetProcessHeap(), 0, data);
225
226     return WINED3D_OK;
227 }
228
229 DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
230 {
231     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
232     DWORD PriorityOld = This->resource.priority;
233     This->resource.priority = PriorityNew;
234     TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
235     return PriorityOld;
236 }
237
238 DWORD resource_get_priority(IWineD3DResource *iface)
239 {
240     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
241     TRACE("(%p) : returning %d\n", This, This->resource.priority );
242     return This->resource.priority;
243 }
244
245 WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
246 {
247     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
248     TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
249     return This->resource.resourceType;
250 }