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