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 | /*
|
---|
25 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
26 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
27 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
28 | * a choice of LGPL license versions is made available with the language indicating
|
---|
29 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
30 | * of the LGPL is applied is otherwise unspecified.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #include "config.h"
|
---|
34 | #include "wined3d_private.h"
|
---|
35 |
|
---|
36 | WINE_DEFAULT_DEBUG_CHANNEL(d3d);
|
---|
37 |
|
---|
38 | HRESULT resource_init(IWineD3DResource *iface, WINED3DRESOURCETYPE resource_type,
|
---|
39 | IWineD3DDeviceImpl *device, UINT size, DWORD usage, const struct GlPixelFormatDesc *format_desc,
|
---|
40 | WINED3DPOOL pool, IUnknown *parent)
|
---|
41 | {
|
---|
42 | struct IWineD3DResourceClass *resource = &((IWineD3DResourceImpl *)iface)->resource;
|
---|
43 |
|
---|
44 | resource->wineD3DDevice = device;
|
---|
45 | resource->parent = parent;
|
---|
46 | resource->resourceType = resource_type;
|
---|
47 | resource->ref = 1;
|
---|
48 | resource->pool = pool;
|
---|
49 | resource->format_desc = format_desc;
|
---|
50 | resource->usage = usage;
|
---|
51 | resource->size = size;
|
---|
52 | resource->priority = 0;
|
---|
53 | list_init(&resource->privateData);
|
---|
54 |
|
---|
55 | if (size)
|
---|
56 | {
|
---|
57 | resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
|
---|
58 | if (!resource->heapMemory)
|
---|
59 | {
|
---|
60 | ERR("Out of memory!\n");
|
---|
61 | return WINED3DERR_OUTOFVIDEOMEMORY;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | resource->heapMemory = NULL;
|
---|
67 | }
|
---|
68 | resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
|
---|
69 |
|
---|
70 | /* Check that we have enough video ram left */
|
---|
71 | if (pool == WINED3DPOOL_DEFAULT)
|
---|
72 | {
|
---|
73 | if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
|
---|
74 | {
|
---|
75 | ERR("Out of adapter memory\n");
|
---|
76 | HeapFree(GetProcessHeap(), 0, resource->heapMemory);
|
---|
77 | return WINED3DERR_OUTOFVIDEOMEMORY;
|
---|
78 | }
|
---|
79 | WineD3DAdapterChangeGLRam(device, size);
|
---|
80 | }
|
---|
81 |
|
---|
82 | device_resource_add(device, iface);
|
---|
83 |
|
---|
84 | return WINED3D_OK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | void resource_cleanup(IWineD3DResource *iface)
|
---|
88 | {
|
---|
89 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
90 | struct list *e1, *e2;
|
---|
91 | PrivateData *data;
|
---|
92 | HRESULT hr;
|
---|
93 |
|
---|
94 | TRACE("(%p) Cleaning up resource\n", This);
|
---|
95 | if (This->resource.pool == WINED3DPOOL_DEFAULT) {
|
---|
96 | TRACE("Decrementing device memory pool by %u\n", This->resource.size);
|
---|
97 | WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
|
---|
98 | }
|
---|
99 |
|
---|
100 | LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
|
---|
101 | data = LIST_ENTRY(e1, PrivateData, entry);
|
---|
102 | hr = resource_free_private_data(iface, &data->tag);
|
---|
103 | if(hr != WINED3D_OK) {
|
---|
104 | ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
|
---|
109 | This->resource.allocatedMemory = 0;
|
---|
110 | This->resource.heapMemory = 0;
|
---|
111 |
|
---|
112 | if (This->resource.wineD3DDevice) device_resource_released(This->resource.wineD3DDevice, iface);
|
---|
113 | }
|
---|
114 |
|
---|
115 | HRESULT resource_get_device(IWineD3DResource *iface, IWineD3DDevice** ppDevice)
|
---|
116 | {
|
---|
117 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
118 | TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
|
---|
119 | *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
|
---|
120 | IWineD3DDevice_AddRef(*ppDevice);
|
---|
121 | return WINED3D_OK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
|
---|
125 | {
|
---|
126 | PrivateData *data;
|
---|
127 | struct list *entry;
|
---|
128 |
|
---|
129 | TRACE("Searching for private data %s\n", debugstr_guid(tag));
|
---|
130 | LIST_FOR_EACH(entry, &This->resource.privateData)
|
---|
131 | {
|
---|
132 | data = LIST_ENTRY(entry, PrivateData, entry);
|
---|
133 | if (IsEqualGUID(&data->tag, tag)) {
|
---|
134 | TRACE("Found %p\n", data);
|
---|
135 | return data;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | TRACE("Not found\n");
|
---|
139 | return NULL;
|
---|
140 | }
|
---|
141 |
|
---|
142 | HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
|
---|
143 | const void *pData, DWORD SizeOfData, DWORD Flags)
|
---|
144 | {
|
---|
145 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
146 | PrivateData *data;
|
---|
147 |
|
---|
148 | TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
|
---|
149 | resource_free_private_data(iface, refguid);
|
---|
150 |
|
---|
151 | data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
|
---|
152 | if (NULL == data) return E_OUTOFMEMORY;
|
---|
153 |
|
---|
154 | data->tag = *refguid;
|
---|
155 | data->flags = Flags;
|
---|
156 |
|
---|
157 | if (Flags & WINED3DSPD_IUNKNOWN) {
|
---|
158 | if(SizeOfData != sizeof(IUnknown *)) {
|
---|
159 | WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
|
---|
160 | HeapFree(GetProcessHeap(), 0, data);
|
---|
161 | return WINED3DERR_INVALIDCALL;
|
---|
162 | }
|
---|
163 | data->ptr.object = (LPUNKNOWN)pData;
|
---|
164 | data->size = sizeof(LPUNKNOWN);
|
---|
165 | IUnknown_AddRef(data->ptr.object);
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
|
---|
170 | if (NULL == data->ptr.data) {
|
---|
171 | HeapFree(GetProcessHeap(), 0, data);
|
---|
172 | return E_OUTOFMEMORY;
|
---|
173 | }
|
---|
174 | data->size = SizeOfData;
|
---|
175 | memcpy(data->ptr.data, pData, SizeOfData);
|
---|
176 | }
|
---|
177 | list_add_tail(&This->resource.privateData, &data->entry);
|
---|
178 |
|
---|
179 | return WINED3D_OK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
|
---|
183 | {
|
---|
184 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
185 | PrivateData *data;
|
---|
186 |
|
---|
187 | TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
|
---|
188 | data = resource_find_private_data(This, refguid);
|
---|
189 | if (data == NULL) return WINED3DERR_NOTFOUND;
|
---|
190 |
|
---|
191 | if (*pSizeOfData < data->size) {
|
---|
192 | *pSizeOfData = data->size;
|
---|
193 | return WINED3DERR_MOREDATA;
|
---|
194 | }
|
---|
195 |
|
---|
196 | if (data->flags & WINED3DSPD_IUNKNOWN) {
|
---|
197 | *(LPUNKNOWN *)pData = data->ptr.object;
|
---|
198 | if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
|
---|
199 | /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
|
---|
200 | * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
|
---|
201 | * Blob
|
---|
202 | */
|
---|
203 | IUnknown_AddRef(data->ptr.object);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | else {
|
---|
207 | memcpy(pData, data->ptr.data, data->size);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return WINED3D_OK;
|
---|
211 | }
|
---|
212 | HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
|
---|
213 | {
|
---|
214 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
215 | PrivateData *data;
|
---|
216 |
|
---|
217 | TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
|
---|
218 | data = resource_find_private_data(This, refguid);
|
---|
219 | if (data == NULL) return WINED3DERR_NOTFOUND;
|
---|
220 |
|
---|
221 | if (data->flags & WINED3DSPD_IUNKNOWN)
|
---|
222 | {
|
---|
223 | if (data->ptr.object != NULL)
|
---|
224 | IUnknown_Release(data->ptr.object);
|
---|
225 | } else {
|
---|
226 | HeapFree(GetProcessHeap(), 0, data->ptr.data);
|
---|
227 | }
|
---|
228 | list_remove(&data->entry);
|
---|
229 |
|
---|
230 | HeapFree(GetProcessHeap(), 0, data);
|
---|
231 |
|
---|
232 | return WINED3D_OK;
|
---|
233 | }
|
---|
234 |
|
---|
235 | DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
|
---|
236 | {
|
---|
237 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
238 | DWORD PriorityOld = This->resource.priority;
|
---|
239 | This->resource.priority = PriorityNew;
|
---|
240 | TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
|
---|
241 | return PriorityOld;
|
---|
242 | }
|
---|
243 |
|
---|
244 | DWORD resource_get_priority(IWineD3DResource *iface)
|
---|
245 | {
|
---|
246 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
247 | TRACE("(%p) : returning %d\n", This, This->resource.priority );
|
---|
248 | return This->resource.priority;
|
---|
249 | }
|
---|
250 |
|
---|
251 | WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
|
---|
252 | {
|
---|
253 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
254 | TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
|
---|
255 | return This->resource.resourceType;
|
---|
256 | }
|
---|
257 |
|
---|
258 | HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
|
---|
259 | {
|
---|
260 | IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
|
---|
261 | IUnknown_AddRef(This->resource.parent);
|
---|
262 | *pParent = This->resource.parent;
|
---|
263 | return WINED3D_OK;
|
---|
264 | }
|
---|
265 |
|
---|
266 | void dumpResources(struct list *list) {
|
---|
267 | IWineD3DResourceImpl *resource;
|
---|
268 |
|
---|
269 | LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
|
---|
270 | FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));
|
---|
271 | }
|
---|
272 | }
|
---|