wined3d: Consistently store format bitcounts in BYTEs.
[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 static DWORD resource_access_from_pool(WINED3DPOOL pool)
47 {
48     switch (pool)
49     {
50         case WINED3DPOOL_DEFAULT:
51             return WINED3D_RESOURCE_ACCESS_GPU;
52
53         case WINED3DPOOL_MANAGED:
54             return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
55
56         case WINED3DPOOL_SYSTEMMEM:
57             return WINED3D_RESOURCE_ACCESS_CPU;
58
59         case WINED3DPOOL_SCRATCH:
60             return WINED3D_RESOURCE_ACCESS_SCRATCH;
61
62         default:
63             FIXME("Unhandled pool %#x.\n", pool);
64             return 0;
65     }
66 }
67
68 HRESULT resource_init(struct wined3d_resource *resource, IWineD3DDeviceImpl *device,
69         WINED3DRESOURCETYPE resource_type, const struct wined3d_format *format,
70         WINED3DMULTISAMPLE_TYPE multisample_type, UINT multisample_quality,
71         DWORD usage, WINED3DPOOL pool, UINT width, UINT height, UINT depth, UINT size,
72         void *parent, const struct wined3d_parent_ops *parent_ops,
73         const struct wined3d_resource_ops *resource_ops)
74 {
75     resource->ref = 1;
76     resource->device = device;
77     resource->resourceType = resource_type;
78     resource->format = format;
79     resource->multisample_type = multisample_type;
80     resource->multisample_quality = multisample_quality;
81     resource->usage = usage;
82     resource->pool = pool;
83     resource->access_flags = resource_access_from_pool(pool);
84     if (usage & WINED3DUSAGE_DYNAMIC)
85         resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
86     resource->width = width;
87     resource->height = height;
88     resource->depth = depth;
89     resource->size = size;
90     resource->priority = 0;
91     resource->parent = parent;
92     resource->parent_ops = parent_ops;
93     resource->resource_ops = resource_ops;
94     list_init(&resource->privateData);
95
96     if (size)
97     {
98         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
99         if (!resource->heapMemory)
100         {
101             ERR("Out of memory!\n");
102             return WINED3DERR_OUTOFVIDEOMEMORY;
103         }
104     }
105     else
106     {
107         resource->heapMemory = NULL;
108     }
109     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
110             + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
111
112     /* Check that we have enough video ram left */
113     if (pool == WINED3DPOOL_DEFAULT)
114     {
115         if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
116         {
117             ERR("Out of adapter memory\n");
118             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
119             return WINED3DERR_OUTOFVIDEOMEMORY;
120         }
121         WineD3DAdapterChangeGLRam(device, size);
122     }
123
124     device_resource_add(device, resource);
125
126     return WINED3D_OK;
127 }
128
129 void resource_cleanup(struct wined3d_resource *resource)
130 {
131     struct private_data *data;
132     struct list *e1, *e2;
133     HRESULT hr;
134
135     TRACE("Cleaning up resource %p.\n", resource);
136
137     if (resource->pool == WINED3DPOOL_DEFAULT)
138     {
139         TRACE("Decrementing device memory pool by %u.\n", resource->size);
140         WineD3DAdapterChangeGLRam(resource->device,  0 - resource->size);
141     }
142
143     LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
144     {
145         data = LIST_ENTRY(e1, struct private_data, entry);
146         hr = resource_free_private_data(resource, &data->tag);
147         if (FAILED(hr))
148             ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
149     }
150
151     HeapFree(GetProcessHeap(), 0, resource->heapMemory);
152     resource->allocatedMemory = 0;
153     resource->heapMemory = 0;
154
155     if (resource->device)
156         device_resource_released(resource->device, resource);
157 }
158
159 void resource_unload(struct wined3d_resource *resource)
160 {
161     context_resource_unloaded(resource->device,
162             resource, resource->resourceType);
163 }
164
165 static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
166 {
167     struct private_data *data;
168     struct list *entry;
169
170     TRACE("Searching for private data %s\n", debugstr_guid(tag));
171     LIST_FOR_EACH(entry, &resource->privateData)
172     {
173         data = LIST_ENTRY(entry, struct private_data, entry);
174         if (IsEqualGUID(&data->tag, tag)) {
175             TRACE("Found %p\n", data);
176             return data;
177         }
178     }
179     TRACE("Not found\n");
180     return NULL;
181 }
182
183 HRESULT resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
184         const void *data, DWORD data_size, DWORD flags)
185 {
186     struct private_data *d;
187
188     TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
189             resource, debugstr_guid(guid), data, data_size, flags);
190
191     resource_free_private_data(resource, guid);
192
193     d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
194     if (!d) return E_OUTOFMEMORY;
195
196     d->tag = *guid;
197     d->flags = flags;
198
199     if (flags & WINED3DSPD_IUNKNOWN)
200     {
201         if (data_size != sizeof(IUnknown *))
202         {
203             WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
204             HeapFree(GetProcessHeap(), 0, d);
205             return WINED3DERR_INVALIDCALL;
206         }
207         d->ptr.object = (IUnknown *)data;
208         d->size = sizeof(IUnknown *);
209         IUnknown_AddRef(d->ptr.object);
210     }
211     else
212     {
213         d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
214         if (!d->ptr.data)
215         {
216             HeapFree(GetProcessHeap(), 0, d);
217             return E_OUTOFMEMORY;
218         }
219         d->size = data_size;
220         memcpy(d->ptr.data, data, data_size);
221     }
222     list_add_tail(&resource->privateData, &d->entry);
223
224     return WINED3D_OK;
225 }
226
227 HRESULT resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid, void *data, DWORD *data_size)
228 {
229     const struct private_data *d;
230
231     TRACE("resource %p, guid %s, data %p, data_size %p.\n",
232             resource, debugstr_guid(guid), data, data_size);
233
234     d = resource_find_private_data(resource, guid);
235     if (!d) return WINED3DERR_NOTFOUND;
236
237     if (*data_size < d->size)
238     {
239         *data_size = d->size;
240         return WINED3DERR_MOREDATA;
241     }
242
243     if (d->flags & WINED3DSPD_IUNKNOWN)
244     {
245         *(IUnknown **)data = d->ptr.object;
246         if (resource->device->wined3d->dxVersion != 7)
247         {
248             /* D3D8 and D3D9 addref the private data, DDraw does not. This
249              * can't be handled in ddraw because it doesn't know if the
250              * pointer returned is an IUnknown * or just a blob. */
251             IUnknown_AddRef(d->ptr.object);
252         }
253     }
254     else
255     {
256         memcpy(data, d->ptr.data, d->size);
257     }
258
259     return WINED3D_OK;
260 }
261 HRESULT resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
262 {
263     struct private_data *data;
264
265     TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
266
267     data = resource_find_private_data(resource, guid);
268     if (!data) return WINED3DERR_NOTFOUND;
269
270     if (data->flags & WINED3DSPD_IUNKNOWN)
271     {
272         if (data->ptr.object)
273             IUnknown_Release(data->ptr.object);
274     }
275     else
276     {
277         HeapFree(GetProcessHeap(), 0, data->ptr.data);
278     }
279     list_remove(&data->entry);
280
281     HeapFree(GetProcessHeap(), 0, data);
282
283     return WINED3D_OK;
284 }
285
286 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
287 {
288     DWORD prev = resource->priority;
289     resource->priority = priority;
290     TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
291     return prev;
292 }
293
294 DWORD resource_get_priority(const struct wined3d_resource *resource)
295 {
296     TRACE("resource %p, returning %u.\n", resource, resource->priority);
297     return resource->priority;
298 }
299
300 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
301 {
302     return resource->parent;
303 }
304
305 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
306 {
307     desc->resource_type = resource->resourceType;
308     desc->format = resource->format->id;
309     desc->multisample_type = resource->multisample_type;
310     desc->multisample_quality = resource->multisample_quality;
311     desc->usage = resource->usage;
312     desc->pool = resource->pool;
313     desc->width = resource->width;
314     desc->height = resource->height;
315     desc->depth = resource->depth;
316     desc->size = resource->size;
317 }