VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/resource.c@ 41014

Last change on this file since 41014 was 40388, checked in by vboxsync, 13 years ago

wddm/3d: shared resource destroy handling fixes

  • Property svn:eol-style set to native
File size: 10.0 KB
Line 
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 * Oracle 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, Oracle 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
37WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38
39HRESULT 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#ifdef VBOX_WITH_WDDM
43 , HANDLE *shared_handle
44 , void *pvClientMem
45#endif
46 )
47{
48 struct IWineD3DResourceClass *resource = &((IWineD3DResourceImpl *)iface)->resource;
49
50 resource->device = device;
51 resource->parent = parent;
52 resource->resourceType = resource_type;
53 resource->ref = 1;
54 resource->pool = pool;
55 resource->format_desc = format_desc;
56 resource->usage = usage;
57 resource->size = size;
58 resource->priority = 0;
59 resource->parent_ops = parent_ops;
60 list_init(&resource->privateData);
61
62#ifdef VBOX_WITH_WDDM
63 resource->sharerc_handle = 0;
64 resource->sharerc_flags = 0;
65 resource->sharerc_locks = 0;
66 if (pool == WINED3DPOOL_SYSTEMMEM && pvClientMem)
67 {
68 resource->allocatedMemory = pvClientMem;
69 resource->heapMemory = NULL;
70 }
71 else
72#endif
73 {
74#ifdef VBOX_WITH_WDDM
75 if (pool == WINED3DPOOL_DEFAULT && shared_handle)
76 {
77 resource->sharerc_handle = (DWORD)*shared_handle;
78 resource->sharerc_flags = VBOXSHRC_F_SHARED;
79 if (*shared_handle)
80 resource->sharerc_flags |= VBOXSHRC_F_SHARED_OPENED;
81 }
82#endif
83 if (size)
84 {
85 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
86 if (!resource->heapMemory)
87 {
88 ERR("Out of memory!\n");
89 return WINED3DERR_OUTOFVIDEOMEMORY;
90 }
91 }
92 else
93 {
94 resource->heapMemory = NULL;
95 }
96 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
97 }
98
99 /* Check that we have enough video ram left */
100 if (pool == WINED3DPOOL_DEFAULT)
101 {
102#ifndef VBOX_WITH_WDDM
103 if (size > IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice *)device))
104 {
105 ERR("Out of adapter memory\n");
106 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
107 return WINED3DERR_OUTOFVIDEOMEMORY;
108 }
109#endif
110 WineD3DAdapterChangeGLRam(device, size);
111 }
112
113 device_resource_add(device, iface);
114
115 return WINED3D_OK;
116}
117
118void resource_cleanup(IWineD3DResource *iface)
119{
120 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
121 struct list *e1, *e2;
122 PrivateData *data;
123 HRESULT hr;
124
125 TRACE("(%p) Cleaning up resource\n", This);
126 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
127 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
128 WineD3DAdapterChangeGLRam(This->resource.device, -This->resource.size);
129 }
130
131 LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
132 data = LIST_ENTRY(e1, PrivateData, entry);
133 hr = resource_free_private_data(iface, &data->tag);
134 if(hr != WINED3D_OK) {
135 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
136 }
137 }
138
139 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
140 This->resource.allocatedMemory = 0;
141 This->resource.heapMemory = 0;
142
143 if (This->resource.device) device_resource_released(This->resource.device, iface);
144}
145
146static PrivateData* resource_find_private_data(IWineD3DResourceImpl *This, REFGUID tag)
147{
148 PrivateData *data;
149 struct list *entry;
150
151 TRACE("Searching for private data %s\n", debugstr_guid(tag));
152 LIST_FOR_EACH(entry, &This->resource.privateData)
153 {
154 data = LIST_ENTRY(entry, PrivateData, entry);
155 if (IsEqualGUID(&data->tag, tag)) {
156 TRACE("Found %p\n", data);
157 return data;
158 }
159 }
160 TRACE("Not found\n");
161 return NULL;
162}
163
164HRESULT resource_set_private_data(IWineD3DResource *iface, REFGUID refguid,
165 const void *pData, DWORD SizeOfData, DWORD Flags)
166{
167 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
168 PrivateData *data;
169
170 TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
171 resource_free_private_data(iface, refguid);
172
173 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
174 if (NULL == data) return E_OUTOFMEMORY;
175
176 data->tag = *refguid;
177 data->flags = Flags;
178
179 if (Flags & WINED3DSPD_IUNKNOWN) {
180 if(SizeOfData != sizeof(IUnknown *)) {
181 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
182 HeapFree(GetProcessHeap(), 0, data);
183 return WINED3DERR_INVALIDCALL;
184 }
185 data->ptr.object = (LPUNKNOWN)pData;
186 data->size = sizeof(LPUNKNOWN);
187 IUnknown_AddRef(data->ptr.object);
188 }
189 else
190 {
191 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
192 if (NULL == data->ptr.data) {
193 HeapFree(GetProcessHeap(), 0, data);
194 return E_OUTOFMEMORY;
195 }
196 data->size = SizeOfData;
197 memcpy(data->ptr.data, pData, SizeOfData);
198 }
199 list_add_tail(&This->resource.privateData, &data->entry);
200
201 return WINED3D_OK;
202}
203
204HRESULT resource_get_private_data(IWineD3DResource *iface, REFGUID refguid, void *pData, DWORD *pSizeOfData)
205{
206 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
207 PrivateData *data;
208
209 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
210 data = resource_find_private_data(This, refguid);
211 if (data == NULL) return WINED3DERR_NOTFOUND;
212
213 if (*pSizeOfData < data->size) {
214 *pSizeOfData = data->size;
215 return WINED3DERR_MOREDATA;
216 }
217
218 if (data->flags & WINED3DSPD_IUNKNOWN) {
219 *(LPUNKNOWN *)pData = data->ptr.object;
220 if (((IWineD3DImpl *)This->resource.device->wined3d)->dxVersion != 7)
221 {
222 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
223 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
224 * Blob
225 */
226 IUnknown_AddRef(data->ptr.object);
227 }
228 }
229 else {
230 memcpy(pData, data->ptr.data, data->size);
231 }
232
233 return WINED3D_OK;
234}
235HRESULT resource_free_private_data(IWineD3DResource *iface, REFGUID refguid)
236{
237 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
238 PrivateData *data;
239
240 TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
241 data = resource_find_private_data(This, refguid);
242 if (data == NULL) return WINED3DERR_NOTFOUND;
243
244 if (data->flags & WINED3DSPD_IUNKNOWN)
245 {
246 if (data->ptr.object != NULL)
247 IUnknown_Release(data->ptr.object);
248 } else {
249 HeapFree(GetProcessHeap(), 0, data->ptr.data);
250 }
251 list_remove(&data->entry);
252
253 HeapFree(GetProcessHeap(), 0, data);
254
255 return WINED3D_OK;
256}
257
258DWORD resource_set_priority(IWineD3DResource *iface, DWORD PriorityNew)
259{
260 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
261 DWORD PriorityOld = This->resource.priority;
262 This->resource.priority = PriorityNew;
263 TRACE("(%p) : new priority %d, returning old priority %d\n", This, PriorityNew, PriorityOld );
264 return PriorityOld;
265}
266
267DWORD resource_get_priority(IWineD3DResource *iface)
268{
269 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
270 TRACE("(%p) : returning %d\n", This, This->resource.priority );
271 return This->resource.priority;
272}
273
274WINED3DRESOURCETYPE resource_get_type(IWineD3DResource *iface)
275{
276 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
277 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
278 return This->resource.resourceType;
279}
280
281HRESULT resource_get_parent(IWineD3DResource *iface, IUnknown **pParent)
282{
283 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
284 IUnknown_AddRef(This->resource.parent);
285 *pParent = This->resource.parent;
286 return WINED3D_OK;
287}
288
289#ifdef VBOX_WITH_WDDM
290HRESULT WINAPI IWineD3DResourceImpl_SetShRcState(IWineD3DResource *iface, VBOXWINEEX_SHRC_STATE enmState) {
291 IWineD3DResourceImpl *This = (IWineD3DResourceImpl*)iface;
292 if (!VBOXSHRC_IS_SHARED(This))
293 {
294 ERR("invalid arg");
295 return E_INVALIDARG;
296 }
297
298 switch (enmState)
299 {
300 case VBOXWINEEX_SHRC_STATE_GL_DISABLE:
301 VBOXSHRC_SET_DISABLE(This);
302 break;
303 case VBOXWINEEX_SHRC_STATE_GL_DELETE:
304 VBOXSHRC_SET_DELETE(This);
305 break;
306 default:
307 ERR("invalid arg");
308 return E_INVALIDARG;
309 }
310
311 return WINED3D_OK;
312}
313#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette