2 * IWineD3DResource Implementation
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
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.
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.
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
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
30 HRESULT
resource_init(IWineD3DResource
*iface
, WINED3DRESOURCETYPE resource_type
,
31 IWineD3DDeviceImpl
*device
, UINT size
, DWORD usage
, const struct wined3d_format_desc
*format_desc
,
32 WINED3DPOOL pool
, IUnknown
*parent
, const struct wined3d_parent_ops
*parent_ops
)
34 struct IWineD3DResourceClass
*resource
= &((IWineD3DResourceImpl
*)iface
)->resource
;
36 resource
->device
= device
;
37 resource
->parent
= parent
;
38 resource
->resourceType
= resource_type
;
40 resource
->pool
= pool
;
41 resource
->format_desc
= format_desc
;
42 resource
->usage
= usage
;
43 resource
->size
= size
;
44 resource
->priority
= 0;
45 resource
->parent_ops
= parent_ops
;
46 list_init(&resource
->privateData
);
50 resource
->heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ RESOURCE_ALIGNMENT
);
51 if (!resource
->heapMemory
)
53 ERR("Out of memory!\n");
54 return WINED3DERR_OUTOFVIDEOMEMORY
;
59 resource
->heapMemory
= NULL
;
61 resource
->allocatedMemory
= (BYTE
*)(((ULONG_PTR
)resource
->heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
63 /* Check that we have enough video ram left */
64 if (pool
== WINED3DPOOL_DEFAULT
)
66 if (size
> IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice
*)device
))
68 ERR("Out of adapter memory\n");
69 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
70 return WINED3DERR_OUTOFVIDEOMEMORY
;
72 WineD3DAdapterChangeGLRam(device
, size
);
75 device_resource_add(device
, iface
);
80 void resource_cleanup(IWineD3DResource
*iface
)
82 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
87 TRACE("(%p) Cleaning up resource\n", This
);
88 if (This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
89 TRACE("Decrementing device memory pool by %u\n", This
->resource
.size
);
90 WineD3DAdapterChangeGLRam(This
->resource
.device
, -This
->resource
.size
);
93 LIST_FOR_EACH_SAFE(e1
, e2
, &This
->resource
.privateData
) {
94 data
= LIST_ENTRY(e1
, PrivateData
, entry
);
95 hr
= resource_free_private_data(iface
, &data
->tag
);
96 if(hr
!= WINED3D_OK
) {
97 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This
, hr
);
101 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
102 This
->resource
.allocatedMemory
= 0;
103 This
->resource
.heapMemory
= 0;
105 if (This
->resource
.device
) device_resource_released(This
->resource
.device
, iface
);
108 static PrivateData
* resource_find_private_data(IWineD3DResourceImpl
*This
, REFGUID tag
)
113 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
114 LIST_FOR_EACH(entry
, &This
->resource
.privateData
)
116 data
= LIST_ENTRY(entry
, PrivateData
, entry
);
117 if (IsEqualGUID(&data
->tag
, tag
)) {
118 TRACE("Found %p\n", data
);
122 TRACE("Not found\n");
126 HRESULT
resource_set_private_data(IWineD3DResource
*iface
, REFGUID refguid
,
127 const void *pData
, DWORD SizeOfData
, DWORD Flags
)
129 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
132 TRACE("(%p) : %s %p %d %d\n", This
, debugstr_guid(refguid
), pData
, SizeOfData
, Flags
);
133 resource_free_private_data(iface
, refguid
);
135 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
136 if (NULL
== data
) return E_OUTOFMEMORY
;
138 data
->tag
= *refguid
;
141 if (Flags
& WINED3DSPD_IUNKNOWN
) {
142 if(SizeOfData
!= sizeof(IUnknown
*)) {
143 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData
);
144 HeapFree(GetProcessHeap(), 0, data
);
145 return WINED3DERR_INVALIDCALL
;
147 data
->ptr
.object
= (LPUNKNOWN
)pData
;
148 data
->size
= sizeof(LPUNKNOWN
);
149 IUnknown_AddRef(data
->ptr
.object
);
153 data
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, SizeOfData
);
154 if (NULL
== data
->ptr
.data
) {
155 HeapFree(GetProcessHeap(), 0, data
);
156 return E_OUTOFMEMORY
;
158 data
->size
= SizeOfData
;
159 memcpy(data
->ptr
.data
, pData
, SizeOfData
);
161 list_add_tail(&This
->resource
.privateData
, &data
->entry
);
166 HRESULT
resource_get_private_data(IWineD3DResource
*iface
, REFGUID refguid
, void *pData
, DWORD
*pSizeOfData
)
168 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
171 TRACE("(%p) : %p %p %p\n", This
, refguid
, pData
, pSizeOfData
);
172 data
= resource_find_private_data(This
, refguid
);
173 if (data
== NULL
) return WINED3DERR_NOTFOUND
;
175 if (*pSizeOfData
< data
->size
) {
176 *pSizeOfData
= data
->size
;
177 return WINED3DERR_MOREDATA
;
180 if (data
->flags
& WINED3DSPD_IUNKNOWN
) {
181 *(LPUNKNOWN
*)pData
= data
->ptr
.object
;
182 if (((IWineD3DImpl
*)This
->resource
.device
->wined3d
)->dxVersion
!= 7)
184 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
185 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
188 IUnknown_AddRef(data
->ptr
.object
);
192 memcpy(pData
, data
->ptr
.data
, data
->size
);
197 HRESULT
resource_free_private_data(IWineD3DResource
*iface
, REFGUID refguid
)
199 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
202 TRACE("(%p) : %s\n", This
, debugstr_guid(refguid
));
203 data
= resource_find_private_data(This
, refguid
);
204 if (data
== NULL
) return WINED3DERR_NOTFOUND
;
206 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
208 if (data
->ptr
.object
!= NULL
)
209 IUnknown_Release(data
->ptr
.object
);
211 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
213 list_remove(&data
->entry
);
215 HeapFree(GetProcessHeap(), 0, data
);
220 DWORD
resource_set_priority(IWineD3DResource
*iface
, DWORD PriorityNew
)
222 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
223 DWORD PriorityOld
= This
->resource
.priority
;
224 This
->resource
.priority
= PriorityNew
;
225 TRACE("(%p) : new priority %d, returning old priority %d\n", This
, PriorityNew
, PriorityOld
);
229 DWORD
resource_get_priority(IWineD3DResource
*iface
)
231 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
232 TRACE("(%p) : returning %d\n", This
, This
->resource
.priority
);
233 return This
->resource
.priority
;
236 WINED3DRESOURCETYPE
resource_get_type(IWineD3DResource
*iface
)
238 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
239 TRACE("(%p) : returning %d\n", This
, This
->resource
.resourceType
);
240 return This
->resource
.resourceType
;
243 HRESULT
resource_get_parent(IWineD3DResource
*iface
, IUnknown
**pParent
)
245 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
246 IUnknown_AddRef(This
->resource
.parent
);
247 *pParent
= This
->resource
.parent
;
251 void dumpResources(struct list
*list
) {
252 IWineD3DResourceImpl
*resource
;
254 LIST_FOR_EACH_ENTRY(resource
, list
, IWineD3DResourceImpl
, resource
.resource_list_entry
) {
255 FIXME("Leftover resource %p with type %d,%s\n", resource
, IWineD3DResource_GetType((IWineD3DResource
*) resource
), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource
*) resource
)));