gdi32/tests: Prevent a crash on win95.
[wine/multimedia.git] / dlls / wined3d / resource.c
blob17e540bb208fd8df4652f9184314f4607faafd52
1 /*
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
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
30 /* IWineD3DResource IUnknown parts follow: */
31 HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, LPVOID *ppobj)
33 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
34 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
35 if (IsEqualGUID(riid, &IID_IUnknown)
36 || IsEqualGUID(riid, &IID_IWineD3DBase)
37 || IsEqualGUID(riid, &IID_IWineD3DResource)) {
38 IUnknown_AddRef(iface);
39 *ppobj = This;
40 return S_OK;
42 *ppobj = NULL;
43 return E_NOINTERFACE;
46 ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface) {
47 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
48 ULONG ref = InterlockedIncrement(&This->resource.ref);
49 TRACE("(%p) : AddRef increasing from %d\n", This, ref - 1);
50 return ref;
53 ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface) {
54 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
55 ULONG ref = InterlockedDecrement(&This->resource.ref);
56 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
57 if (ref == 0) {
58 IWineD3DResourceImpl_CleanUp(iface);
59 HeapFree(GetProcessHeap(), 0, This);
61 return ref;
64 /* class static (not in vtable) */
65 void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface){
66 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
67 struct list *e1, *e2;
68 PrivateData *data;
69 HRESULT hr;
71 TRACE("(%p) Cleaning up resource\n", This);
72 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
73 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
74 WineD3DAdapterChangeGLRam(This->resource.wineD3DDevice, -This->resource.size);
77 LIST_FOR_EACH_SAFE(e1, e2, &This->resource.privateData) {
78 data = LIST_ENTRY(e1, PrivateData, entry);
79 hr = IWineD3DResourceImpl_FreePrivateData(iface, &data->tag);
80 if(hr != WINED3D_OK) {
81 ERR("Failed to free private data when destroying resource %p, hr = %08x\n", This, hr);
85 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
86 This->resource.allocatedMemory = 0;
87 This->resource.heapMemory = 0;
89 if (This->resource.wineD3DDevice != NULL) {
90 IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
91 }/* NOTE: this is not really an error for system memory resources */
92 return;
95 /* IWineD3DResource Interface follows: */
96 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
97 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
98 TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
99 *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
100 IWineD3DDevice_AddRef(*ppDevice);
101 return WINED3D_OK;
104 static PrivateData* IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
105 REFGUID tag)
107 PrivateData *data;
108 struct list *entry;
110 TRACE("Searching for private data %s\n", debugstr_guid(tag));
111 LIST_FOR_EACH(entry, &This->resource.privateData)
113 data = LIST_ENTRY(entry, PrivateData, entry);
114 if (IsEqualGUID(&data->tag, tag)) {
115 TRACE("Found %p\n", data);
116 return data;
119 TRACE("Not found\n");
120 return NULL;
123 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
124 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
125 PrivateData *data;
127 TRACE("(%p) : %s %p %d %d\n", This, debugstr_guid(refguid), pData, SizeOfData, Flags);
128 IWineD3DResourceImpl_FreePrivateData(iface, refguid);
130 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
131 if (NULL == data) return E_OUTOFMEMORY;
133 data->tag = *refguid;
134 data->flags = Flags;
135 #if 0
136 (*data)->uniquenessValue = This->uniquenessValue;
137 #endif
138 if (Flags & WINED3DSPD_IUNKNOWN) {
139 if(SizeOfData != sizeof(IUnknown *)) {
140 WARN("IUnknown data with size %d, returning WINED3DERR_INVALIDCALL\n", SizeOfData);
141 HeapFree(GetProcessHeap(), 0, data);
142 return WINED3DERR_INVALIDCALL;
144 data->ptr.object = (LPUNKNOWN)pData;
145 data->size = sizeof(LPUNKNOWN);
146 IUnknown_AddRef(data->ptr.object);
148 else
150 data->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
151 if (NULL == data->ptr.data) {
152 HeapFree(GetProcessHeap(), 0, data);
153 return E_OUTOFMEMORY;
155 data->size = SizeOfData;
156 memcpy(data->ptr.data, pData, SizeOfData);
158 list_add_tail(&This->resource.privateData, &data->entry);
160 return WINED3D_OK;
163 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
164 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
165 PrivateData *data;
167 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
168 data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
169 if (data == NULL) return WINED3DERR_NOTFOUND;
172 #if 0 /* This may not be right. */
173 if (((*data)->flags & WINED3DSPD_VOLATILE)
174 && (*data)->uniquenessValue != This->uniquenessValue)
175 return DDERR_EXPIRED;
176 #endif
177 if (*pSizeOfData < data->size) {
178 *pSizeOfData = data->size;
179 return WINED3DERR_MOREDATA;
182 if (data->flags & WINED3DSPD_IUNKNOWN) {
183 *(LPUNKNOWN *)pData = data->ptr.object;
184 if(((IWineD3DImpl *) This->resource.wineD3DDevice->wineD3D)->dxVersion != 7) {
185 /* D3D8 and D3D9 addref the private data, DDraw does not. This can't be handled in
186 * ddraw because it doesn't know if the pointer returned is an IUnknown * or just a
187 * Blob
189 IUnknown_AddRef(data->ptr.object);
192 else {
193 memcpy(pData, data->ptr.data, data->size);
196 return WINED3D_OK;
198 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
199 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
200 PrivateData *data;
202 TRACE("(%p) : %s\n", This, debugstr_guid(refguid));
203 data = IWineD3DResourceImpl_FindPrivateData(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);
210 } else {
211 HeapFree(GetProcessHeap(), 0, data->ptr.data);
213 list_remove(&data->entry);
215 HeapFree(GetProcessHeap(), 0, data);
217 return WINED3D_OK;
220 /* Priority support is not implemented yet */
221 DWORD WINAPI IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
222 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
223 FIXME("(%p) : stub\n", This);
224 return 0;
226 DWORD WINAPI IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
227 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
228 FIXME("(%p) : stub\n", This);
229 return 0;
232 /* Preloading of resources is not supported yet */
233 void WINAPI IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
234 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
235 FIXME("(%p) : stub\n", This);
238 void WINAPI IWineD3DResourceImpl_UnLoad(IWineD3DResource *iface) {
239 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
240 FIXME("(%p) : stub\n", This);
243 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
244 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
245 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
246 return This->resource.resourceType;
249 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
250 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
251 IUnknown_AddRef(This->resource.parent);
252 *pParent = This->resource.parent;
253 return WINED3D_OK;
256 void dumpResources(struct list *list) {
257 IWineD3DResourceImpl *resource;
259 LIST_FOR_EACH_ENTRY(resource, list, IWineD3DResourceImpl, resource.resource_list_entry) {
260 FIXME("Leftover resource %p with type %d,%s\n", resource, IWineD3DResource_GetType((IWineD3DResource *) resource), debug_d3dresourcetype(IWineD3DResource_GetType((IWineD3DResource *) resource)));
264 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
266 /* IUnknown */
267 IWineD3DResourceImpl_QueryInterface,
268 IWineD3DResourceImpl_AddRef,
269 IWineD3DResourceImpl_Release,
270 /* IWineD3DResource */
271 IWineD3DResourceImpl_GetParent,
272 IWineD3DResourceImpl_GetDevice,
273 IWineD3DResourceImpl_SetPrivateData,
274 IWineD3DResourceImpl_GetPrivateData,
275 IWineD3DResourceImpl_FreePrivateData,
276 IWineD3DResourceImpl_SetPriority,
277 IWineD3DResourceImpl_GetPriority,
278 IWineD3DResourceImpl_PreLoad,
279 IWineD3DResourceImpl_UnLoad,
280 IWineD3DResourceImpl_GetType