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
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.
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.
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
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
29 HRESULT
resource_init(struct IWineD3DResourceClass
*resource
, WINED3DRESOURCETYPE resource_type
,
30 IWineD3DDeviceImpl
*device
, UINT size
, DWORD usage
, const struct GlPixelFormatDesc
*format_desc
,
31 WINED3DPOOL pool
, IUnknown
*parent
)
33 resource
->wineD3DDevice
= device
;
34 resource
->parent
= parent
;
35 resource
->resourceType
= resource_type
;
37 resource
->pool
= pool
;
38 resource
->format_desc
= format_desc
;
39 resource
->usage
= usage
;
40 resource
->size
= size
;
41 resource
->priority
= 0;
42 list_init(&resource
->privateData
);
46 resource
->heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ RESOURCE_ALIGNMENT
);
47 if (!resource
->heapMemory
)
49 ERR("Out of memory!\n");
50 return WINED3DERR_OUTOFVIDEOMEMORY
;
55 resource
->heapMemory
= NULL
;
57 resource
->allocatedMemory
= (BYTE
*)(((ULONG_PTR
)resource
->heapMemory
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
59 /* Check that we have enough video ram left */
60 if (pool
== WINED3DPOOL_DEFAULT
)
62 if (size
> IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice
*)device
))
64 ERR("Out of adapter memory\n");
65 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
66 return WINED3DERR_OUTOFVIDEOMEMORY
;
68 WineD3DAdapterChangeGLRam(device
, size
);
74 void resource_cleanup(IWineD3DResource
*iface
)
76 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
81 TRACE("(%p) Cleaning up resource\n", This
);
82 if (This
->resource
.pool
== WINED3DPOOL_DEFAULT
) {
83 TRACE("Decrementing device memory pool by %u\n", This
->resource
.size
);
84 WineD3DAdapterChangeGLRam(This
->resource
.wineD3DDevice
, -This
->resource
.size
);
87 LIST_FOR_EACH_SAFE(e1
, e2
, &This
->resource
.privateData
) {
88 data
= LIST_ENTRY(e1
, PrivateData
, entry
);
89 hr
= resource_free_private_data(iface
, &data
->tag
);
90 if(hr
!= WINED3D_OK
) {
91 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This
, hr
);
95 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
96 This
->resource
.allocatedMemory
= 0;
97 This
->resource
.heapMemory
= 0;
99 if (This
->resource
.wineD3DDevice
) device_resource_released(This
->resource
.wineD3DDevice
, iface
);
102 HRESULT
resource_get_device(IWineD3DResource
*iface
, IWineD3DDevice
** ppDevice
)
104 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
105 TRACE("(%p) : returning %p\n", This
, This
->resource
.wineD3DDevice
);
106 *ppDevice
= (IWineD3DDevice
*) This
->resource
.wineD3DDevice
;
107 IWineD3DDevice_AddRef(*ppDevice
);
111 static PrivateData
* resource_find_private_data(IWineD3DResourceImpl
*This
, REFGUID tag
)
116 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
117 LIST_FOR_EACH(entry
, &This
->resource
.privateData
)
119 data
= LIST_ENTRY(entry
, PrivateData
, entry
);
120 if (IsEqualGUID(&data
->tag
, tag
)) {
121 TRACE("Found %p\n", data
);
125 TRACE("Not found\n");
129 HRESULT
resource_set_private_data(IWineD3DResource
*iface
, REFGUID refguid
,
130 const void *pData
, DWORD SizeOfData
, DWORD Flags
)
132 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
135 TRACE("(%p) : %s %p %d %d\n", This
, debugstr_guid(refguid
), pData
, SizeOfData
, Flags
);
136 resource_free_private_data(iface
, refguid
);
138 data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*data
));
139 if (NULL
== data
) return E_OUTOFMEMORY
;
141 data
->tag
= *refguid
;
144 if (Flags
& WINED3DSPD_IUNKNOWN
) {
145 if(SizeOfData
!= sizeof(IUnknown
*)) {
146 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData
);
147 HeapFree(GetProcessHeap(), 0, data
);
148 return WINED3DERR_INVALIDCALL
;
150 data
->ptr
.object
= (LPUNKNOWN
)pData
;
151 data
->size
= sizeof(LPUNKNOWN
);
152 IUnknown_AddRef(data
->ptr
.object
);
156 data
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, SizeOfData
);
157 if (NULL
== data
->ptr
.data
) {
158 HeapFree(GetProcessHeap(), 0, data
);
159 return E_OUTOFMEMORY
;
161 data
->size
= SizeOfData
;
162 memcpy(data
->ptr
.data
, pData
, SizeOfData
);
164 list_add_tail(&This
->resource
.privateData
, &data
->entry
);
169 HRESULT
resource_get_private_data(IWineD3DResource
*iface
, REFGUID refguid
, void *pData
, DWORD
*pSizeOfData
)
171 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
174 TRACE("(%p) : %p %p %p\n", This
, refguid
, pData
, pSizeOfData
);
175 data
= resource_find_private_data(This
, refguid
);
176 if (data
== NULL
) return WINED3DERR_NOTFOUND
;
178 if (*pSizeOfData
< data
->size
) {
179 *pSizeOfData
= data
->size
;
180 return WINED3DERR_MOREDATA
;
183 if (data
->flags
& WINED3DSPD_IUNKNOWN
) {
184 *(LPUNKNOWN
*)pData
= data
->ptr
.object
;
185 if(((IWineD3DImpl
*) This
->resource
.wineD3DDevice
->wineD3D
)->dxVersion
!= 7) {
186 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
187 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
190 IUnknown_AddRef(data
->ptr
.object
);
194 memcpy(pData
, data
->ptr
.data
, data
->size
);
199 HRESULT
resource_free_private_data(IWineD3DResource
*iface
, REFGUID refguid
)
201 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
204 TRACE("(%p) : %s\n", This
, debugstr_guid(refguid
));
205 data
= resource_find_private_data(This
, refguid
);
206 if (data
== NULL
) return WINED3DERR_NOTFOUND
;
208 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
210 if (data
->ptr
.object
!= NULL
)
211 IUnknown_Release(data
->ptr
.object
);
213 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
215 list_remove(&data
->entry
);
217 HeapFree(GetProcessHeap(), 0, data
);
222 DWORD
resource_set_priority(IWineD3DResource
*iface
, DWORD PriorityNew
)
224 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
225 DWORD PriorityOld
= This
->resource
.priority
;
226 This
->resource
.priority
= PriorityNew
;
227 TRACE("(%p) : new priority %d, returning old priority %d\n", This
, PriorityNew
, PriorityOld
);
231 DWORD
resource_get_priority(IWineD3DResource
*iface
)
233 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
234 TRACE("(%p) : returning %d\n", This
, This
->resource
.priority
);
235 return This
->resource
.priority
;
238 WINED3DRESOURCETYPE
resource_get_type(IWineD3DResource
*iface
)
240 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
241 TRACE("(%p) : returning %d\n", This
, This
->resource
.resourceType
);
242 return This
->resource
.resourceType
;
245 HRESULT
resource_get_parent(IWineD3DResource
*iface
, IUnknown
**pParent
)
247 IWineD3DResourceImpl
*This
= (IWineD3DResourceImpl
*)iface
;
248 IUnknown_AddRef(This
->resource
.parent
);
249 *pParent
= This
->resource
.parent
;
253 void dumpResources(struct list
*list
) {
254 IWineD3DResourceImpl
*resource
;
256 LIST_FOR_EACH_ENTRY(resource
, list
, IWineD3DResourceImpl
, resource
.resource_list_entry
) {
257 FIXME("Leftover resource %p with type %d,%s\n", resource
, IWineD3DResource_GetType((IWineD3DResource
*) resource
), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource
*) resource
)));