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-2010 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
);
35 DWORD flags
; /* DDSPD_* */
46 HRESULT
resource_init(struct wined3d_resource
*resource
, WINED3DRESOURCETYPE resource_type
,
47 IWineD3DDeviceImpl
*device
, UINT size
, DWORD usage
, const struct wined3d_format
*format
,
48 WINED3DPOOL pool
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
49 const struct wined3d_resource_ops
*resource_ops
)
51 resource
->device
= device
;
52 resource
->resourceType
= resource_type
;
54 resource
->pool
= pool
;
55 resource
->format
= format
;
56 resource
->usage
= usage
;
57 resource
->size
= size
;
58 resource
->priority
= 0;
59 resource
->parent
= parent
;
60 resource
->parent_ops
= parent_ops
;
61 resource
->resource_ops
= resource_ops
;
62 list_init(&resource
->privateData
);
66 resource
->heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ RESOURCE_ALIGNMENT
);
67 if (!resource
->heapMemory
)
69 ERR("Out of memory!\n");
70 return WINED3DERR_OUTOFVIDEOMEMORY
;
75 resource
->heapMemory
= NULL
;
77 resource
->allocatedMemory
= (BYTE
*)(((ULONG_PTR
)resource
->heapMemory
78 + (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
80 /* Check that we have enough video ram left */
81 if (pool
== WINED3DPOOL_DEFAULT
)
83 if (size
> IWineD3DDevice_GetAvailableTextureMem((IWineD3DDevice
*)device
))
85 ERR("Out of adapter memory\n");
86 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
87 return WINED3DERR_OUTOFVIDEOMEMORY
;
89 WineD3DAdapterChangeGLRam(device
, size
);
92 device_resource_add(device
, resource
);
97 void resource_cleanup(struct wined3d_resource
*resource
)
99 struct private_data
*data
;
100 struct list
*e1
, *e2
;
103 TRACE("Cleaning up resource %p.\n", resource
);
105 if (resource
->pool
== WINED3DPOOL_DEFAULT
)
107 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
108 WineD3DAdapterChangeGLRam(resource
->device
, -resource
->size
);
111 LIST_FOR_EACH_SAFE(e1
, e2
, &resource
->privateData
)
113 data
= LIST_ENTRY(e1
, struct private_data
, entry
);
114 hr
= resource_free_private_data(resource
, &data
->tag
);
116 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource
, hr
);
119 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
120 resource
->allocatedMemory
= 0;
121 resource
->heapMemory
= 0;
123 if (resource
->device
)
124 device_resource_released(resource
->device
, resource
);
127 void resource_unload(struct wined3d_resource
*resource
)
129 context_resource_unloaded(resource
->device
,
130 resource
, resource
->resourceType
);
133 static struct private_data
*resource_find_private_data(const struct wined3d_resource
*resource
, REFGUID tag
)
135 struct private_data
*data
;
138 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
139 LIST_FOR_EACH(entry
, &resource
->privateData
)
141 data
= LIST_ENTRY(entry
, struct private_data
, entry
);
142 if (IsEqualGUID(&data
->tag
, tag
)) {
143 TRACE("Found %p\n", data
);
147 TRACE("Not found\n");
151 HRESULT
resource_set_private_data(struct wined3d_resource
*resource
, REFGUID guid
,
152 const void *data
, DWORD data_size
, DWORD flags
)
154 struct private_data
*d
;
156 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
157 resource
, debugstr_guid(guid
), data
, data_size
, flags
);
159 resource_free_private_data(resource
, guid
);
161 d
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*d
));
162 if (!d
) return E_OUTOFMEMORY
;
167 if (flags
& WINED3DSPD_IUNKNOWN
)
169 if (data_size
!= sizeof(IUnknown
*))
171 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size
);
172 HeapFree(GetProcessHeap(), 0, d
);
173 return WINED3DERR_INVALIDCALL
;
175 d
->ptr
.object
= (IUnknown
*)data
;
176 d
->size
= sizeof(IUnknown
*);
177 IUnknown_AddRef(d
->ptr
.object
);
181 d
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
184 HeapFree(GetProcessHeap(), 0, d
);
185 return E_OUTOFMEMORY
;
188 memcpy(d
->ptr
.data
, data
, data_size
);
190 list_add_tail(&resource
->privateData
, &d
->entry
);
195 HRESULT
resource_get_private_data(const struct wined3d_resource
*resource
, REFGUID guid
, void *data
, DWORD
*data_size
)
197 const struct private_data
*d
;
199 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
200 resource
, debugstr_guid(guid
), data
, data_size
);
202 d
= resource_find_private_data(resource
, guid
);
203 if (!d
) return WINED3DERR_NOTFOUND
;
205 if (*data_size
< d
->size
)
207 *data_size
= d
->size
;
208 return WINED3DERR_MOREDATA
;
211 if (d
->flags
& WINED3DSPD_IUNKNOWN
)
213 *(IUnknown
**)data
= d
->ptr
.object
;
214 if (resource
->device
->wined3d
->dxVersion
!= 7)
216 /* D3D8 and D3D9 addref the private data, DDraw does not. This
217 * can't be handled in ddraw because it doesn't know if the
218 * pointer returned is an IUnknown * or just a blob. */
219 IUnknown_AddRef(d
->ptr
.object
);
224 memcpy(data
, d
->ptr
.data
, d
->size
);
229 HRESULT
resource_free_private_data(struct wined3d_resource
*resource
, REFGUID guid
)
231 struct private_data
*data
;
233 TRACE("resource %p, guid %s.\n", resource
, debugstr_guid(guid
));
235 data
= resource_find_private_data(resource
, guid
);
236 if (!data
) return WINED3DERR_NOTFOUND
;
238 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
240 if (data
->ptr
.object
)
241 IUnknown_Release(data
->ptr
.object
);
245 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
247 list_remove(&data
->entry
);
249 HeapFree(GetProcessHeap(), 0, data
);
254 DWORD
resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
256 DWORD prev
= resource
->priority
;
257 resource
->priority
= priority
;
258 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
262 DWORD
resource_get_priority(const struct wined3d_resource
*resource
)
264 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
265 return resource
->priority
;
268 WINED3DRESOURCETYPE
resource_get_type(const struct wined3d_resource
*resource
)
270 TRACE("resource %p, returning %#x.\n", resource
, resource
->resourceType
);
271 return resource
->resourceType
;
274 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
276 return resource
->parent
;