wined3d: Add WINED3DPOOL to wined3d_types.h.
[wine/dibdrv.git] / dlls / wined3d / resource.c
blob56efcb7b6b7063c25c432a76488dfc41d7628658
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 D3D_OK;
42 return E_NOINTERFACE;
45 ULONG WINAPI IWineD3DResourceImpl_AddRef(IWineD3DResource *iface) {
46 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
47 ULONG ref = InterlockedIncrement(&This->resource.ref);
48 TRACE("(%p) : AddRef increasing from %ld\n", This, ref - 1);
49 return ref;
52 ULONG WINAPI IWineD3DResourceImpl_Release(IWineD3DResource *iface) {
53 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
54 ULONG ref = InterlockedDecrement(&This->resource.ref);
55 TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
56 if (ref == 0) {
57 IWineD3DResourceImpl_CleanUp(iface);
58 HeapFree(GetProcessHeap(), 0, This);
60 return ref;
63 /* class static (not in vtable) */
64 void IWineD3DResourceImpl_CleanUp(IWineD3DResource *iface){
65 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
66 TRACE("(%p) Cleaning up resource\n", This);
67 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
68 TRACE("Decrementing device memory pool by %u\n", This->resource.size);
69 globalChangeGlRam(-This->resource.size);
72 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
73 This->resource.allocatedMemory = 0;
75 if (This->resource.wineD3DDevice != NULL) {
76 IWineD3DDevice_ResourceReleased((IWineD3DDevice *)This->resource.wineD3DDevice, iface);
77 }/* NOTE: this is not really an error for systemmem resoruces */
78 return;
81 /* IWineD3DResource Interface follow: */
82 HRESULT WINAPI IWineD3DResourceImpl_GetDevice(IWineD3DResource *iface, IWineD3DDevice** ppDevice) {
83 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
84 TRACE("(%p) : returning %p\n", This, This->resource.wineD3DDevice);
85 *ppDevice = (IWineD3DDevice *) This->resource.wineD3DDevice;
86 IWineD3DDevice_AddRef(*ppDevice);
87 return D3D_OK;
90 static PrivateData** IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
91 REFGUID tag)
93 PrivateData** data;
94 for (data = &This->resource.privateData; *data != NULL; data = &(*data)->next)
96 if (IsEqualGUID(&(*data)->tag, tag)) break;
98 return data;
101 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
102 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
103 PrivateData **data;
105 TRACE("(%p) : %p %p %ld %ld\n", This, refguid, pData, SizeOfData, Flags);
106 data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
107 if (*data == NULL)
109 *data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**data));
110 if (NULL == *data) return E_OUTOFMEMORY;
112 (*data)->tag = *refguid;
113 (*data)->flags = Flags;
114 #if 0
115 (*data)->uniquenessValue = This->uniquenessValue;
116 #endif
117 if (Flags & D3DSPD_IUNKNOWN) {
118 (*data)->ptr.object = (LPUNKNOWN)pData;
119 (*data)->size = sizeof(LPUNKNOWN);
120 IUnknown_AddRef((*data)->ptr.object);
122 else
124 (*data)->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
125 if (NULL == (*data)->ptr.data) {
126 HeapFree(GetProcessHeap(), 0, *data);
127 return E_OUTOFMEMORY;
130 /* link it in */
131 (*data)->next = This->resource.privateData;
132 This->resource.privateData = *data;
133 return D3D_OK;
135 } else {
136 /* I don't actually know how windows handles this case. The only
137 * reason I don't just call FreePrivateData is because I want to
138 * guarantee SetPrivateData working when using LPUNKNOWN or data
139 * that is no larger than the old data. */
140 return E_FAIL;
144 return D3D_OK;
147 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
148 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
149 PrivateData **data;
151 TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
152 data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
153 if (*data == NULL) return D3DERR_NOTFOUND;
156 #if 0 /* This may not be right. */
157 if (((*data)->flags & D3DSPD_VOLATILE)
158 && (*data)->uniquenessValue != This->uniquenessValue)
159 return DDERR_EXPIRED;
160 #endif
161 if (*pSizeOfData < (*data)->size) {
162 *pSizeOfData = (*data)->size;
163 return D3DERR_MOREDATA;
166 if ((*data)->flags & D3DSPD_IUNKNOWN) {
167 *(LPUNKNOWN *)pData = (*data)->ptr.object;
168 IUnknown_AddRef((*data)->ptr.object);
170 else {
171 memcpy(pData, (*data)->ptr.data, (*data)->size);
174 return D3D_OK;
176 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
177 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
178 PrivateData **data;
180 TRACE("(%p) : %p\n", This, refguid);
181 /* TODO: move this code off into a linked list class */
182 data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
183 if (*data == NULL) return D3DERR_NOTFOUND;
185 *data = (*data)->next;
187 if ((*data)->flags & D3DSPD_IUNKNOWN)
189 if ((*data)->ptr.object != NULL)
190 IUnknown_Release((*data)->ptr.object);
191 } else {
192 HeapFree(GetProcessHeap(), 0, (*data)->ptr.data);
195 HeapFree(GetProcessHeap(), 0, *data);
197 return D3D_OK;
200 /* Priority support is not implemented yet */
201 DWORD WINAPI IWineD3DResourceImpl_SetPriority(IWineD3DResource *iface, DWORD PriorityNew) {
202 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
203 FIXME("(%p) : stub\n", This);
204 return 0;
206 DWORD WINAPI IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
207 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
208 FIXME("(%p) : stub\n", This);
209 return 0;
212 /* Preloading of resources is not supported yet */
213 void WINAPI IWineD3DResourceImpl_PreLoad(IWineD3DResource *iface) {
214 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
215 FIXME("(%p) : stub\n", This);
218 WINED3DRESOURCETYPE WINAPI IWineD3DResourceImpl_GetType(IWineD3DResource *iface) {
219 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
220 TRACE("(%p) : returning %d\n", This, This->resource.resourceType);
221 return This->resource.resourceType;
224 HRESULT WINAPI IWineD3DResourceImpl_GetParent(IWineD3DResource *iface, IUnknown **pParent) {
225 IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
226 IUnknown_AddRef(This->resource.parent);
227 *pParent = This->resource.parent;
228 return D3D_OK;
232 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
234 /* IUnknown */
235 IWineD3DResourceImpl_QueryInterface,
236 IWineD3DResourceImpl_AddRef,
237 IWineD3DResourceImpl_Release,
238 /* IWineD3DResource */
239 IWineD3DResourceImpl_GetParent,
240 IWineD3DResourceImpl_GetDevice,
241 IWineD3DResourceImpl_SetPrivateData,
242 IWineD3DResourceImpl_GetPrivateData,
243 IWineD3DResourceImpl_FreePrivateData,
244 IWineD3DResourceImpl_SetPriority,
245 IWineD3DResourceImpl_GetPriority,
246 IWineD3DResourceImpl_PreLoad,
247 IWineD3DResourceImpl_GetType