wined3d: Rename return values from D3D* to WINED3D*.
[wine] / dlls / wined3d / resource.c
1 /*
2  * IWineD3DResource Implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29
30 /* IWineD3DResource IUnknown parts follow: */
31 HRESULT WINAPI IWineD3DResourceImpl_QueryInterface(IWineD3DResource *iface, REFIID riid, LPVOID *ppobj)
32 {
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 WINED3D_OK;
41     }
42     return E_NOINTERFACE;
43 }
44
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; 
50 }
51
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);
59     }
60     return ref;
61 }
62
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);
70     }
71
72     HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
73     This->resource.allocatedMemory = 0;
74
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;
79 }
80
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 WINED3D_OK;
88 }
89
90 static PrivateData** IWineD3DResourceImpl_FindPrivateData(IWineD3DResourceImpl *This,
91                     REFGUID tag)
92 {
93     PrivateData** data;
94     for (data = &This->resource.privateData; *data != NULL; data = &(*data)->next)
95     {
96         if (IsEqualGUID(&(*data)->tag, tag)) break;
97     }
98     return data;
99 }
100
101 HRESULT WINAPI IWineD3DResourceImpl_SetPrivateData(IWineD3DResource *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
102     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
103     PrivateData **data;
104
105     TRACE("(%p) : %p %p %ld %ld\n", This, refguid, pData, SizeOfData, Flags);
106     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
107     if (*data == NULL)
108     {
109         *data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**data));
110         if (NULL == *data) return E_OUTOFMEMORY;
111
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);
121         }
122         else
123         {
124             (*data)->ptr.data = HeapAlloc(GetProcessHeap(), 0, SizeOfData);
125             if (NULL == (*data)->ptr.data) {
126                 HeapFree(GetProcessHeap(), 0, *data);
127                 return E_OUTOFMEMORY;
128             }
129         }
130         /* link it in */
131         (*data)->next = This->resource.privateData;
132         This->resource.privateData = *data;
133         return WINED3D_OK;
134
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;
141
142     }
143
144     return WINED3D_OK;
145 }
146
147 HRESULT WINAPI IWineD3DResourceImpl_GetPrivateData(IWineD3DResource *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
148     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
149     PrivateData **data;
150
151     TRACE("(%p) : %p %p %p\n", This, refguid, pData, pSizeOfData);
152     data = IWineD3DResourceImpl_FindPrivateData(This, refguid);
153     if (*data == NULL) return WINED3DERR_NOTFOUND;
154
155
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 WINED3DERR_MOREDATA;
164     }
165
166     if ((*data)->flags & D3DSPD_IUNKNOWN) {
167         *(LPUNKNOWN *)pData = (*data)->ptr.object;
168         IUnknown_AddRef((*data)->ptr.object);
169     }
170     else {
171         memcpy(pData, (*data)->ptr.data, (*data)->size);
172     }
173
174     return WINED3D_OK;
175 }
176 HRESULT WINAPI IWineD3DResourceImpl_FreePrivateData(IWineD3DResource *iface, REFGUID refguid) {
177     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
178     PrivateData **data;
179
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 WINED3DERR_NOTFOUND;
184
185     *data = (*data)->next;
186
187     if ((*data)->flags & D3DSPD_IUNKNOWN)
188     {
189         if ((*data)->ptr.object != NULL)
190             IUnknown_Release((*data)->ptr.object);
191     } else {
192         HeapFree(GetProcessHeap(), 0, (*data)->ptr.data);
193     }
194
195     HeapFree(GetProcessHeap(), 0, *data);
196
197     return WINED3D_OK;
198 }
199
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;
205 }
206 DWORD    WINAPI        IWineD3DResourceImpl_GetPriority(IWineD3DResource *iface) {
207     IWineD3DResourceImpl *This = (IWineD3DResourceImpl *)iface;
208     FIXME("(%p) : stub\n", This);
209     return 0;
210 }
211
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);
216 }
217
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;
222 }
223
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 WINED3D_OK;
229 }
230
231
232 static const IWineD3DResourceVtbl IWineD3DResource_Vtbl =
233 {
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
248 };