po: Update French translation.
[wine] / dlls / wined3d / resource.c
1 /*
2  * Copyright 2002-2004 Jason Edmeades
3  * Copyright 2003-2004 Raphael Junqueira
4  * Copyright 2004 Christian Costa
5  * Copyright 2005 Oliver Stieber
6  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27
28 struct private_data
29 {
30     struct list entry;
31
32     GUID tag;
33     DWORD flags; /* DDSPD_* */
34
35     union
36     {
37         void *data;
38         IUnknown *object;
39     } ptr;
40
41     DWORD size;
42 };
43
44 static DWORD resource_access_from_pool(enum wined3d_pool pool)
45 {
46     switch (pool)
47     {
48         case WINED3D_POOL_DEFAULT:
49             return WINED3D_RESOURCE_ACCESS_GPU;
50
51         case WINED3D_POOL_MANAGED:
52             return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
53
54         case WINED3D_POOL_SYSTEM_MEM:
55             return WINED3D_RESOURCE_ACCESS_CPU;
56
57         case WINED3D_POOL_SCRATCH:
58             return WINED3D_RESOURCE_ACCESS_SCRATCH;
59
60         default:
61             FIXME("Unhandled pool %#x.\n", pool);
62             return 0;
63     }
64 }
65
66 static void resource_check_usage(DWORD usage)
67 {
68     static const DWORD handled = WINED3DUSAGE_RENDERTARGET
69             | WINED3DUSAGE_DEPTHSTENCIL
70             | WINED3DUSAGE_DYNAMIC
71             | WINED3DUSAGE_AUTOGENMIPMAP
72             | WINED3DUSAGE_STATICDECL
73             | WINED3DUSAGE_OVERLAY;
74
75     if (usage & ~handled)
76         FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
77 }
78
79 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
80         enum wined3d_resource_type type, const struct wined3d_format *format,
81         enum wined3d_multisample_type multisample_type, UINT multisample_quality,
82         DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
83         void *parent, const struct wined3d_parent_ops *parent_ops,
84         const struct wined3d_resource_ops *resource_ops)
85 {
86     resource->ref = 1;
87     resource->device = device;
88     resource->type = type;
89     resource->format = format;
90     resource->multisample_type = multisample_type;
91     resource->multisample_quality = multisample_quality;
92     resource->usage = usage;
93     resource->pool = pool;
94     resource->access_flags = resource_access_from_pool(pool);
95     if (usage & WINED3DUSAGE_DYNAMIC)
96         resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
97     resource->width = width;
98     resource->height = height;
99     resource->depth = depth;
100     resource->size = size;
101     resource->priority = 0;
102     resource->parent = parent;
103     resource->parent_ops = parent_ops;
104     resource->resource_ops = resource_ops;
105     list_init(&resource->privateData);
106
107     resource_check_usage(usage);
108
109     if (size)
110     {
111         resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
112         if (!resource->heapMemory)
113         {
114             ERR("Out of memory!\n");
115             return WINED3DERR_OUTOFVIDEOMEMORY;
116         }
117     }
118     else
119     {
120         resource->heapMemory = NULL;
121     }
122     resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
123             + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
124
125     /* Check that we have enough video ram left */
126     if (pool == WINED3D_POOL_DEFAULT)
127     {
128         if (size > wined3d_device_get_available_texture_mem(device))
129         {
130             ERR("Out of adapter memory\n");
131             HeapFree(GetProcessHeap(), 0, resource->heapMemory);
132             return WINED3DERR_OUTOFVIDEOMEMORY;
133         }
134         adapter_adjust_memory(device->adapter, size);
135     }
136
137     device_resource_add(device, resource);
138
139     return WINED3D_OK;
140 }
141
142 void resource_cleanup(struct wined3d_resource *resource)
143 {
144     struct private_data *data;
145     struct list *e1, *e2;
146     HRESULT hr;
147
148     TRACE("Cleaning up resource %p.\n", resource);
149
150     if (resource->pool == WINED3D_POOL_DEFAULT)
151     {
152         TRACE("Decrementing device memory pool by %u.\n", resource->size);
153         adapter_adjust_memory(resource->device->adapter, 0 - resource->size);
154     }
155
156     LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
157     {
158         data = LIST_ENTRY(e1, struct private_data, entry);
159         hr = wined3d_resource_free_private_data(resource, &data->tag);
160         if (FAILED(hr))
161             ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
162     }
163
164     HeapFree(GetProcessHeap(), 0, resource->heapMemory);
165     resource->allocatedMemory = 0;
166     resource->heapMemory = 0;
167
168     if (resource->device)
169         device_resource_released(resource->device, resource);
170 }
171
172 void resource_unload(struct wined3d_resource *resource)
173 {
174     if (resource->map_count)
175         ERR("Resource %p is being unloaded while mapped.\n", resource);
176
177     context_resource_unloaded(resource->device,
178             resource, resource->type);
179 }
180
181 static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
182 {
183     struct private_data *data;
184     struct list *entry;
185
186     TRACE("Searching for private data %s\n", debugstr_guid(tag));
187     LIST_FOR_EACH(entry, &resource->privateData)
188     {
189         data = LIST_ENTRY(entry, struct private_data, entry);
190         if (IsEqualGUID(&data->tag, tag)) {
191             TRACE("Found %p\n", data);
192             return data;
193         }
194     }
195     TRACE("Not found\n");
196     return NULL;
197 }
198
199 HRESULT CDECL wined3d_resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
200         const void *data, DWORD data_size, DWORD flags)
201 {
202     struct private_data *d;
203
204     TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
205             resource, debugstr_guid(guid), data, data_size, flags);
206
207     wined3d_resource_free_private_data(resource, guid);
208
209     d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
210     if (!d) return E_OUTOFMEMORY;
211
212     d->tag = *guid;
213     d->flags = flags;
214
215     if (flags & WINED3DSPD_IUNKNOWN)
216     {
217         if (data_size != sizeof(IUnknown *))
218         {
219             WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
220             HeapFree(GetProcessHeap(), 0, d);
221             return WINED3DERR_INVALIDCALL;
222         }
223         d->ptr.object = (IUnknown *)data;
224         d->size = sizeof(IUnknown *);
225         IUnknown_AddRef(d->ptr.object);
226     }
227     else
228     {
229         d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
230         if (!d->ptr.data)
231         {
232             HeapFree(GetProcessHeap(), 0, d);
233             return E_OUTOFMEMORY;
234         }
235         d->size = data_size;
236         memcpy(d->ptr.data, data, data_size);
237     }
238     list_add_tail(&resource->privateData, &d->entry);
239
240     return WINED3D_OK;
241 }
242
243 HRESULT CDECL wined3d_resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid,
244         void *data, DWORD *data_size)
245 {
246     const struct private_data *d;
247
248     TRACE("resource %p, guid %s, data %p, data_size %p.\n",
249             resource, debugstr_guid(guid), data, data_size);
250
251     d = resource_find_private_data(resource, guid);
252     if (!d) return WINED3DERR_NOTFOUND;
253
254     if (*data_size < d->size)
255     {
256         *data_size = d->size;
257         return WINED3DERR_MOREDATA;
258     }
259
260     if (d->flags & WINED3DSPD_IUNKNOWN)
261     {
262         *(IUnknown **)data = d->ptr.object;
263         if (resource->device->wined3d->dxVersion != 7)
264         {
265             /* D3D8 and D3D9 addref the private data, DDraw does not. This
266              * can't be handled in ddraw because it doesn't know if the
267              * pointer returned is an IUnknown * or just a blob. */
268             IUnknown_AddRef(d->ptr.object);
269         }
270     }
271     else
272     {
273         memcpy(data, d->ptr.data, d->size);
274     }
275
276     return WINED3D_OK;
277 }
278 HRESULT CDECL wined3d_resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
279 {
280     struct private_data *data;
281
282     TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
283
284     data = resource_find_private_data(resource, guid);
285     if (!data) return WINED3DERR_NOTFOUND;
286
287     if (data->flags & WINED3DSPD_IUNKNOWN)
288     {
289         if (data->ptr.object)
290             IUnknown_Release(data->ptr.object);
291     }
292     else
293     {
294         HeapFree(GetProcessHeap(), 0, data->ptr.data);
295     }
296     list_remove(&data->entry);
297
298     HeapFree(GetProcessHeap(), 0, data);
299
300     return WINED3D_OK;
301 }
302
303 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
304 {
305     DWORD prev = resource->priority;
306     resource->priority = priority;
307     TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
308     return prev;
309 }
310
311 DWORD resource_get_priority(const struct wined3d_resource *resource)
312 {
313     TRACE("resource %p, returning %u.\n", resource, resource->priority);
314     return resource->priority;
315 }
316
317 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
318 {
319     return resource->parent;
320 }
321
322 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
323 {
324     desc->resource_type = resource->type;
325     desc->format = resource->format->id;
326     desc->multisample_type = resource->multisample_type;
327     desc->multisample_quality = resource->multisample_quality;
328     desc->usage = resource->usage;
329     desc->pool = resource->pool;
330     desc->width = resource->width;
331     desc->height = resource->height;
332     desc->depth = resource->depth;
333     desc->size = resource->size;
334 }