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