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