msvfw32: Move MSVIDEO_SendMessage() up and make it and MSVIDEO_GetHicPtr() static.
[wine] / dlls / d3d9 / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 /* IDirect3DTexture9 IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DTexture9Impl_QueryInterface(LPDIRECT3DTEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
30     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource9)
34         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
35         || IsEqualGUID(riid, &IID_IDirect3DTexture9)) {
36         IDirect3DTexture9_AddRef(iface);
37         *ppobj = This;
38         return S_OK;
39     }
40
41     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
42     *ppobj = NULL;
43     return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DTexture9Impl_AddRef(LPDIRECT3DTEXTURE9 iface) {
47     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
48     ULONG ref = InterlockedIncrement(&This->ref);
49
50     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
51
52     return ref;
53 }
54
55 static ULONG WINAPI IDirect3DTexture9Impl_Release(LPDIRECT3DTEXTURE9 iface) {
56     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
57     ULONG ref = InterlockedDecrement(&This->ref);
58
59     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
60
61     if (ref == 0) {
62         EnterCriticalSection(&d3d9_cs);
63         IWineD3DTexture_Destroy(This->wineD3DTexture, D3D9CB_DestroySurface);
64         LeaveCriticalSection(&d3d9_cs);
65         IDirect3DDevice9Ex_Release(This->parentDevice);
66         HeapFree(GetProcessHeap(), 0, This);
67     }
68     return ref;
69 }
70
71 /* IDirect3DTexture9 IDirect3DResource9 Interface follow: */
72 static HRESULT WINAPI IDirect3DTexture9Impl_GetDevice(LPDIRECT3DTEXTURE9 iface, IDirect3DDevice9** ppDevice) {
73     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
74     IWineD3DDevice *wined3d_device;
75     HRESULT hr;
76     TRACE("(%p) Relay\n", This);
77
78     EnterCriticalSection(&d3d9_cs);
79     hr = IWineD3DTexture_GetDevice(This->wineD3DTexture, &wined3d_device);
80     if (SUCCEEDED(hr))
81     {
82         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
83         IWineD3DDevice_Release(wined3d_device);
84     }
85     LeaveCriticalSection(&d3d9_cs);
86     return hr;
87 }
88
89 static HRESULT WINAPI IDirect3DTexture9Impl_SetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
90     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
91     HRESULT hr;
92     TRACE("(%p) Relay\n", This);
93
94     EnterCriticalSection(&d3d9_cs);
95     hr = IWineD3DTexture_SetPrivateData(This->wineD3DTexture, refguid, pData, SizeOfData, Flags);
96     LeaveCriticalSection(&d3d9_cs);
97     return hr;
98 }
99
100 static HRESULT WINAPI IDirect3DTexture9Impl_GetPrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
101     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
102     HRESULT hr;
103     TRACE("(%p) Relay\n", This);
104
105     EnterCriticalSection(&d3d9_cs);
106     hr = IWineD3DTexture_GetPrivateData(This->wineD3DTexture, refguid, pData, pSizeOfData);
107     LeaveCriticalSection(&d3d9_cs);
108     return hr;
109 }
110
111 static HRESULT WINAPI IDirect3DTexture9Impl_FreePrivateData(LPDIRECT3DTEXTURE9 iface, REFGUID refguid) {
112     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
113     HRESULT hr;
114     TRACE("(%p) Relay\n", This);
115
116     EnterCriticalSection(&d3d9_cs);
117     hr = IWineD3DTexture_FreePrivateData(This->wineD3DTexture, refguid);
118     LeaveCriticalSection(&d3d9_cs);
119     return hr;
120 }
121
122 static DWORD WINAPI IDirect3DTexture9Impl_SetPriority(LPDIRECT3DTEXTURE9 iface, DWORD PriorityNew) {
123     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
124     DWORD ret;
125     TRACE("(%p) Relay\n", This);
126
127     EnterCriticalSection(&d3d9_cs);
128     ret = IWineD3DTexture_SetPriority(This->wineD3DTexture, PriorityNew);
129     LeaveCriticalSection(&d3d9_cs);
130     return ret;
131 }
132
133 static DWORD WINAPI IDirect3DTexture9Impl_GetPriority(LPDIRECT3DTEXTURE9 iface) {
134     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
135     DWORD ret;
136     TRACE("(%p) Relay\n", This);
137
138     EnterCriticalSection(&d3d9_cs);
139     ret = IWineD3DTexture_GetPriority(This->wineD3DTexture);
140     LeaveCriticalSection(&d3d9_cs);
141     return ret;
142 }
143
144 static void WINAPI IDirect3DTexture9Impl_PreLoad(LPDIRECT3DTEXTURE9 iface) {
145     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
146     TRACE("(%p) Relay\n", This);
147
148     EnterCriticalSection(&d3d9_cs);
149     IWineD3DTexture_PreLoad(This->wineD3DTexture);
150     LeaveCriticalSection(&d3d9_cs);
151 }
152
153 static D3DRESOURCETYPE WINAPI IDirect3DTexture9Impl_GetType(LPDIRECT3DTEXTURE9 iface) {
154     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
155     HRESULT ret;
156     TRACE("(%p) Relay\n", This);
157
158     EnterCriticalSection(&d3d9_cs);
159     ret = IWineD3DTexture_GetType(This->wineD3DTexture);
160     LeaveCriticalSection(&d3d9_cs);
161     return ret;
162 }
163
164 /* IDirect3DTexture9 IDirect3DBaseTexture9 Interface follow: */
165 static DWORD WINAPI IDirect3DTexture9Impl_SetLOD(LPDIRECT3DTEXTURE9 iface, DWORD LODNew) {
166     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
167     DWORD ret;
168     TRACE("(%p) Relay\n", This);
169
170     EnterCriticalSection(&d3d9_cs);
171     ret = IWineD3DTexture_SetLOD(This->wineD3DTexture, LODNew);
172     LeaveCriticalSection(&d3d9_cs);
173     return ret;
174 }
175
176 static DWORD WINAPI IDirect3DTexture9Impl_GetLOD(LPDIRECT3DTEXTURE9 iface) {
177     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
178     DWORD ret;
179     TRACE("(%p) Relay\n", This);
180
181     EnterCriticalSection(&d3d9_cs);
182     ret = IWineD3DTexture_GetLOD(This->wineD3DTexture);
183     LeaveCriticalSection(&d3d9_cs);
184     return ret;
185 }
186
187 static DWORD WINAPI IDirect3DTexture9Impl_GetLevelCount(LPDIRECT3DTEXTURE9 iface) {
188     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
189     DWORD ret;
190     TRACE("(%p) Relay\n", This);
191
192     EnterCriticalSection(&d3d9_cs);
193     ret = IWineD3DTexture_GetLevelCount(This->wineD3DTexture);
194     LeaveCriticalSection(&d3d9_cs);
195     return ret;
196 }
197
198 static HRESULT WINAPI IDirect3DTexture9Impl_SetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
199     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
200     HRESULT hr;
201     TRACE("(%p) Relay\n", This);
202
203     EnterCriticalSection(&d3d9_cs);
204     hr = IWineD3DTexture_SetAutoGenFilterType(This->wineD3DTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
205     LeaveCriticalSection(&d3d9_cs);
206     return hr;
207 }
208
209 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DTexture9Impl_GetAutoGenFilterType(LPDIRECT3DTEXTURE9 iface) {
210     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
211     D3DTEXTUREFILTERTYPE ret;
212     TRACE("(%p) Relay\n", This);
213
214     EnterCriticalSection(&d3d9_cs);
215     ret = (D3DTEXTUREFILTERTYPE) IWineD3DTexture_GetAutoGenFilterType(This->wineD3DTexture);
216     LeaveCriticalSection(&d3d9_cs);
217     return ret;
218 }
219
220 static void WINAPI IDirect3DTexture9Impl_GenerateMipSubLevels(LPDIRECT3DTEXTURE9 iface) {
221     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
222     TRACE("(%p) Relay\n", This);
223
224     EnterCriticalSection(&d3d9_cs);
225     IWineD3DTexture_GenerateMipSubLevels(This->wineD3DTexture);
226     LeaveCriticalSection(&d3d9_cs);
227 }
228
229 /* IDirect3DTexture9 Interface follow: */
230 static HRESULT WINAPI IDirect3DTexture9Impl_GetLevelDesc(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DSURFACE_DESC* pDesc) {
231     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
232     WINED3DSURFACE_DESC wined3ddesc;
233     HRESULT hr;
234
235     TRACE("(%p) Relay\n", This);
236
237     EnterCriticalSection(&d3d9_cs);
238     hr = IWineD3DTexture_GetLevelDesc(This->wineD3DTexture, Level, &wined3ddesc);
239     LeaveCriticalSection(&d3d9_cs);
240
241     if (SUCCEEDED(hr))
242     {
243         pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
244         pDesc->Type = wined3ddesc.resource_type;
245         pDesc->Usage = wined3ddesc.usage;
246         pDesc->Pool = wined3ddesc.pool;
247         pDesc->MultiSampleType = wined3ddesc.multisample_type;
248         pDesc->MultiSampleQuality = wined3ddesc.multisample_quality;
249         pDesc->Width = wined3ddesc.width;
250         pDesc->Height = wined3ddesc.height;
251     }
252
253     return hr;
254 }
255
256 static HRESULT WINAPI IDirect3DTexture9Impl_GetSurfaceLevel(LPDIRECT3DTEXTURE9 iface, UINT Level, IDirect3DSurface9** ppSurfaceLevel) {
257     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
258     HRESULT hrc = D3D_OK;
259     IWineD3DSurface *mySurface = NULL;
260
261     TRACE("(%p) Relay\n", This);
262     EnterCriticalSection(&d3d9_cs);
263     hrc = IWineD3DTexture_GetSurfaceLevel(This->wineD3DTexture, Level, &mySurface);
264     if (hrc == D3D_OK && NULL != ppSurfaceLevel) {
265        IWineD3DSurface_GetParent(mySurface, (IUnknown **)ppSurfaceLevel);
266        IWineD3DSurface_Release(mySurface);
267     }
268     LeaveCriticalSection(&d3d9_cs);
269     return hrc;
270 }
271
272 static HRESULT WINAPI IDirect3DTexture9Impl_LockRect(LPDIRECT3DTEXTURE9 iface, UINT Level, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
273     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
274     HRESULT hr;
275     TRACE("(%p) Relay\n", This);
276
277     EnterCriticalSection(&d3d9_cs);
278     hr = IWineD3DTexture_LockRect(This->wineD3DTexture, Level, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
279     LeaveCriticalSection(&d3d9_cs);
280     return hr;
281 }
282
283 static HRESULT WINAPI IDirect3DTexture9Impl_UnlockRect(LPDIRECT3DTEXTURE9 iface, UINT Level) {
284     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
285     HRESULT hr;
286     TRACE("(%p) Relay\n", This);
287
288     EnterCriticalSection(&d3d9_cs);
289     hr = IWineD3DTexture_UnlockRect(This->wineD3DTexture, Level);
290     LeaveCriticalSection(&d3d9_cs);
291     return hr;
292 }
293
294 static HRESULT WINAPI IDirect3DTexture9Impl_AddDirtyRect(LPDIRECT3DTEXTURE9 iface, CONST RECT* pDirtyRect) {
295     IDirect3DTexture9Impl *This = (IDirect3DTexture9Impl *)iface;
296     HRESULT hr;
297     TRACE("(%p) Relay\n", This);
298
299     EnterCriticalSection(&d3d9_cs);
300     hr = IWineD3DTexture_AddDirtyRect(This->wineD3DTexture, pDirtyRect);
301     LeaveCriticalSection(&d3d9_cs);
302     return hr;
303 }
304
305 static const IDirect3DTexture9Vtbl Direct3DTexture9_Vtbl =
306 {
307     /* IUnknown */
308     IDirect3DTexture9Impl_QueryInterface,
309     IDirect3DTexture9Impl_AddRef,
310     IDirect3DTexture9Impl_Release,
311      /* IDirect3DResource9 */
312     IDirect3DTexture9Impl_GetDevice,
313     IDirect3DTexture9Impl_SetPrivateData,
314     IDirect3DTexture9Impl_GetPrivateData,
315     IDirect3DTexture9Impl_FreePrivateData,
316     IDirect3DTexture9Impl_SetPriority,
317     IDirect3DTexture9Impl_GetPriority,
318     IDirect3DTexture9Impl_PreLoad,
319     IDirect3DTexture9Impl_GetType,
320     /* IDirect3dBaseTexture9 */
321     IDirect3DTexture9Impl_SetLOD,
322     IDirect3DTexture9Impl_GetLOD,
323     IDirect3DTexture9Impl_GetLevelCount,
324     IDirect3DTexture9Impl_SetAutoGenFilterType,
325     IDirect3DTexture9Impl_GetAutoGenFilterType,
326     IDirect3DTexture9Impl_GenerateMipSubLevels,
327     /* IDirect3DTexture9 */
328     IDirect3DTexture9Impl_GetLevelDesc,
329     IDirect3DTexture9Impl_GetSurfaceLevel,
330     IDirect3DTexture9Impl_LockRect,
331     IDirect3DTexture9Impl_UnlockRect,
332     IDirect3DTexture9Impl_AddDirtyRect
333 };
334
335
336 /* IDirect3DDevice9 IDirect3DTexture9 Methods follow: */
337 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateTexture(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
338                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture9** ppTexture, HANDLE* pSharedHandle) {
339     IDirect3DTexture9Impl *object;
340     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
341     HRESULT hrc = D3D_OK;
342     
343     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%#x), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
344
345     /* Allocate the storage for the device */
346     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture9Impl));
347
348     if (NULL == object) {
349         ERR("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
350         return D3DERR_OUTOFVIDEOMEMORY;
351     }
352
353     object->lpVtbl = &Direct3DTexture9_Vtbl;
354     object->ref = 1;
355     EnterCriticalSection(&d3d9_cs);
356     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
357             wined3dformat_from_d3dformat(Format), Pool, &object->wineD3DTexture, (IUnknown *)object);
358     LeaveCriticalSection(&d3d9_cs);
359     if (FAILED(hrc)) {
360
361         /* free up object */
362         WARN("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
363         HeapFree(GetProcessHeap(), 0, object);
364    } else {
365         IDirect3DDevice9Ex_AddRef(iface);
366         object->parentDevice = iface;
367         *ppTexture= (LPDIRECT3DTEXTURE9) object;
368         TRACE("(%p) Created Texture %p, %p\n", This, object, object->wineD3DTexture);
369    }
370
371    return hrc;
372 }