Don't require execute permission on the process heap.
[wine] / dlls / wined3d / texture.c
1 /*
2  * IDirect3DTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
28
29 /* *******************************************
30    IWineD3DTexture IUnknown parts follow
31    ******************************************* */
32 HRESULT WINAPI IWineD3DTextureImpl_QueryInterface(IWineD3DTexture *iface, REFIID riid, LPVOID *ppobj)
33 {
34     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
35     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36     if (IsEqualGUID(riid, &IID_IUnknown)
37         || IsEqualGUID(riid, &IID_IWineD3DResource)
38         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
39         || IsEqualGUID(riid, &IID_IWineD3DTexture)){
40         IUnknown_AddRef(iface);
41         *ppobj = This;
42         return D3D_OK;
43     }
44     return E_NOINTERFACE;
45 }
46
47 ULONG WINAPI IWineD3DTextureImpl_AddRef(IWineD3DTexture *iface) {
48     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
49     TRACE("(%p) : AddRef increasing from %ld\n", This, This->resource.ref);
50     IUnknown_AddRef(This->resource.parent);
51     return InterlockedIncrement(&This->resource.ref);
52 }
53
54 ULONG WINAPI IWineD3DTextureImpl_Release(IWineD3DTexture *iface) {
55     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
56     ULONG ref;
57     TRACE("(%p) : Releasing from %ld\n", This, This->resource.ref);
58     ref = InterlockedDecrement(&This->resource.ref);
59     if (ref == 0) {
60         int i;
61         for (i = 0; i < This->baseTexture.levels; i++) {
62             if (This->surfaces[i] != NULL) {
63                 /* Because the surfaces were created using a callback we need to release there parent otehrwise we leave the parent hanging */
64                 IUnknown* surfaceParent;
65                 IWineD3DSurface_GetParent(This->surfaces[i], &surfaceParent);
66                 IUnknown_Release(surfaceParent);
67                 IUnknown_Release(surfaceParent);
68             }
69         }
70         IWineD3DBaseTextureImpl_CleanUp((IWineD3DBaseTexture *)iface);
71         IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
72         HeapFree(GetProcessHeap(), 0, This);
73     } else {
74         IUnknown_Release(This->resource.parent);  /* Released the reference to the d3dx object */
75     }
76     return ref;
77 }
78
79 /* ****************************************************
80    IWineD3DTexture IWineD3DResource parts follow
81    **************************************************** */
82 HRESULT WINAPI IWineD3DTextureImpl_GetDevice(IWineD3DTexture *iface, IWineD3DDevice** ppDevice) {
83     return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
84 }
85
86 HRESULT WINAPI IWineD3DTextureImpl_SetPrivateData(IWineD3DTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
87     return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
88 }
89
90 HRESULT WINAPI IWineD3DTextureImpl_GetPrivateData(IWineD3DTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
91     return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
92 }
93
94 HRESULT WINAPI IWineD3DTextureImpl_FreePrivateData(IWineD3DTexture *iface, REFGUID refguid) {
95     return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
96 }
97
98 DWORD WINAPI IWineD3DTextureImpl_SetPriority(IWineD3DTexture *iface, DWORD PriorityNew) {
99     return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
100 }
101
102 DWORD WINAPI IWineD3DTextureImpl_GetPriority(IWineD3DTexture *iface) {
103     return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
104 }
105
106 void WINAPI IWineD3DTextureImpl_PreLoad(IWineD3DTexture *iface) {
107     /* Override the IWineD3DResource Preload method */
108     unsigned int i;
109     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
110     
111     TRACE("(%p) : About to load texture\n", This);
112     IWineD3DTexture_BindTexture(iface);
113     
114     ENTER_GL();
115     /* If were dirty then reload the surfaces */
116     if(This->baseTexture.dirty != FALSE) {
117         for (i = 0; i < This->baseTexture.levels; i++) {
118              IWineD3DSurface_LoadTexture(This->surfaces[i], GL_TEXTURE_2D, i);
119         }
120         
121         /* No longer dirty */
122         This->baseTexture.dirty = FALSE;
123     }
124     LEAVE_GL();
125
126     return ;
127 }
128
129 D3DRESOURCETYPE WINAPI IWineD3DTextureImpl_GetType(IWineD3DTexture *iface) {
130     return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
131 }
132
133 HRESULT WINAPI IWineD3DTextureImpl_GetParent(IWineD3DTexture *iface, IUnknown **pParent) {
134     return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
135 }
136
137 /* ******************************************************
138    IWineD3DTexture IWineD3DBaseTexture parts follow
139    ****************************************************** */
140 DWORD WINAPI IWineD3DTextureImpl_SetLOD(IWineD3DTexture *iface, DWORD LODNew) {
141     return IWineD3DBaseTextureImpl_SetLOD((IWineD3DBaseTexture *)iface, LODNew);
142 }
143
144 DWORD WINAPI IWineD3DTextureImpl_GetLOD(IWineD3DTexture *iface) {
145     return IWineD3DBaseTextureImpl_GetLOD((IWineD3DBaseTexture *)iface);
146 }
147
148 DWORD WINAPI IWineD3DTextureImpl_GetLevelCount(IWineD3DTexture *iface) {
149     return IWineD3DBaseTextureImpl_GetLevelCount((IWineD3DBaseTexture *)iface);
150 }
151
152 HRESULT WINAPI IWineD3DTextureImpl_SetAutoGenFilterType(IWineD3DTexture *iface, D3DTEXTUREFILTERTYPE FilterType) {
153   return IWineD3DBaseTextureImpl_SetAutoGenFilterType((IWineD3DBaseTexture *)iface, FilterType);
154 }
155
156 D3DTEXTUREFILTERTYPE WINAPI IWineD3DTextureImpl_GetAutoGenFilterType(IWineD3DTexture *iface) {
157   return IWineD3DBaseTextureImpl_GetAutoGenFilterType((IWineD3DBaseTexture *)iface);
158 }
159
160 void WINAPI IWineD3DTextureImpl_GenerateMipSubLevels(IWineD3DTexture *iface) {
161   return IWineD3DBaseTextureImpl_GenerateMipSubLevels((IWineD3DBaseTexture *)iface);
162 }
163
164 /* Internal function, No d3d mapping */
165 BOOL WINAPI IWineD3DTextureImpl_SetDirty(IWineD3DTexture *iface, BOOL dirty) {
166     return IWineD3DBaseTextureImpl_SetDirty((IWineD3DBaseTexture *)iface, TRUE);
167 }
168
169 BOOL WINAPI IWineD3DTextureImpl_GetDirty(IWineD3DTexture *iface) {
170     return IWineD3DBaseTextureImpl_GetDirty((IWineD3DBaseTexture *)iface);
171 }
172
173 HRESULT WINAPI IWineD3DTextureImpl_BindTexture(IWineD3DTexture *iface) {
174     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
175     TRACE("(%p) : relay to BaseTexture \n", This);
176     return IWineD3DBaseTextureImpl_BindTexture((IWineD3DBaseTexture *)iface);
177 }
178
179 HRESULT WINAPI IWineD3DTextureImpl_UnBindTexture(IWineD3DTexture *iface) {
180     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
181     TRACE("(%p) : relay to BaseTexture \n", This);
182     return IWineD3DBaseTextureImpl_UnBindTexture((IWineD3DBaseTexture *)iface);
183 }
184
185 UINT WINAPI IWineD3DTextureImpl_GetTextureDimensions(IWineD3DTexture *iface) {
186     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
187     TRACE("(%p) \n", This);
188
189     return GL_TEXTURE_2D;
190 }
191
192
193 /* *******************************************
194    IWineD3DTexture IWineD3DTexture parts follow
195    ******************************************* */
196 HRESULT WINAPI IWineD3DTextureImpl_GetLevelDesc(IWineD3DTexture *iface, UINT Level, WINED3DSURFACE_DESC* pDesc) {
197     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
198
199     if (Level < This->baseTexture.levels) {
200         TRACE("(%p) Level (%d)\n", This, Level);
201         return IWineD3DSurface_GetDesc((IWineD3DSurface *) This->surfaces[Level], pDesc);
202     }
203     FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
204     return D3DERR_INVALIDCALL;
205 }
206
207 HRESULT WINAPI IWineD3DTextureImpl_GetSurfaceLevel(IWineD3DTexture *iface, UINT Level, IWineD3DSurface** ppSurfaceLevel) {
208     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
209     *ppSurfaceLevel = (IWineD3DSurface *) This->surfaces[Level];
210     IWineD3DSurface_AddRef((IWineD3DSurface *) This->surfaces[Level]);
211     TRACE("(%p) : returning %p for level %d\n", This, *ppSurfaceLevel, Level);
212     return D3D_OK;
213 }
214
215 HRESULT WINAPI IWineD3DTextureImpl_LockRect(IWineD3DTexture *iface, UINT Level, D3DLOCKED_RECT *pLockedRect, 
216                                             CONST RECT *pRect, DWORD Flags) {
217     HRESULT hr;
218     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
219
220     if (Level < This->baseTexture.levels) {
221
222         hr = IWineD3DSurface_LockRect((IWineD3DSurface *) This->surfaces[Level], pLockedRect, pRect, Flags);
223         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
224     } else {
225         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
226         return D3DERR_INVALIDCALL;
227     }
228     return hr;
229 }
230
231 HRESULT WINAPI IWineD3DTextureImpl_UnlockRect(IWineD3DTexture *iface, UINT Level) {
232     HRESULT hr;
233     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
234
235     if (Level < This->baseTexture.levels) {
236         hr = IWineD3DSurface_UnlockRect((IWineD3DSurface *) This->surfaces[Level]);
237         TRACE("(%p) Level (%d) success(%lu)\n", This, Level, hr);
238     } else {
239         FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
240         return D3DERR_INVALIDCALL;
241     }
242     return hr;
243 }
244
245 HRESULT WINAPI IWineD3DTextureImpl_AddDirtyRect(IWineD3DTexture *iface, CONST RECT* pDirtyRect) {
246     IWineD3DTextureImpl *This = (IWineD3DTextureImpl *)iface;
247     This->baseTexture.dirty = TRUE;
248     TRACE("(%p) : dirtyfication of surface Level (0)\n", This);    
249     return IWineD3DSurface_AddDirtyRect((IWineD3DSurface *)This->surfaces[0], pDirtyRect);
250 }
251
252 const IWineD3DTextureVtbl IWineD3DTexture_Vtbl =
253 {
254     /* IUnknown */
255     IWineD3DTextureImpl_QueryInterface,
256     IWineD3DTextureImpl_AddRef,
257     IWineD3DTextureImpl_Release,
258     /* IWineD3DResource */    
259     IWineD3DTextureImpl_GetParent,
260     IWineD3DTextureImpl_GetDevice,
261     IWineD3DTextureImpl_SetPrivateData,
262     IWineD3DTextureImpl_GetPrivateData,
263     IWineD3DTextureImpl_FreePrivateData,
264     IWineD3DTextureImpl_SetPriority,
265     IWineD3DTextureImpl_GetPriority,
266     IWineD3DTextureImpl_PreLoad,
267     IWineD3DTextureImpl_GetType,
268     /* IWineD3DBaseTexture */    
269     IWineD3DTextureImpl_SetLOD,
270     IWineD3DTextureImpl_GetLOD,
271     IWineD3DTextureImpl_GetLevelCount,
272     IWineD3DTextureImpl_SetAutoGenFilterType,
273     IWineD3DTextureImpl_GetAutoGenFilterType,
274     IWineD3DTextureImpl_GenerateMipSubLevels,
275     IWineD3DTextureImpl_SetDirty,
276     IWineD3DTextureImpl_GetDirty,
277     IWineD3DTextureImpl_BindTexture,
278     IWineD3DTextureImpl_UnBindTexture,
279     IWineD3DTextureImpl_GetTextureDimensions,    
280     /* IWineD3DTexture */    
281     IWineD3DTextureImpl_GetLevelDesc,
282     IWineD3DTextureImpl_GetSurfaceLevel,
283     IWineD3DTextureImpl_LockRect,
284     IWineD3DTextureImpl_UnlockRect,
285     IWineD3DTextureImpl_AddDirtyRect
286 };