usp10: Correct an error with the Telugu lexical function.
[wine] / dlls / d3d8 / texture.c
1 /*
2  * IDirect3DTexture8 implementation
3  *
4  * Copyright 2005 Oliver Stieber
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "d3d8_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25
26 /* IDirect3DTexture8 IUnknown parts follow: */
27 static inline IDirect3DTexture8Impl *impl_from_IDirect3DTexture8(IDirect3DTexture8 *iface)
28 {
29     return CONTAINING_RECORD(iface, IDirect3DTexture8Impl, IDirect3DTexture8_iface);
30 }
31
32 static HRESULT WINAPI IDirect3DTexture8Impl_QueryInterface(IDirect3DTexture8 *iface, REFIID riid,
33         void **ppobj)
34 {
35     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
36
37     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
38
39     if (IsEqualGUID(riid, &IID_IUnknown)
40         || IsEqualGUID(riid, &IID_IDirect3DResource8)
41         || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
42         || IsEqualGUID(riid, &IID_IDirect3DTexture8)) {
43         IUnknown_AddRef(iface);
44         *ppobj = This;
45         return S_OK;
46     }
47
48     WARN("(%p)->(%s,%p) not found\n", This, debugstr_guid(riid), ppobj);
49     *ppobj = NULL;
50     return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI IDirect3DTexture8Impl_AddRef(IDirect3DTexture8 *iface)
54 {
55     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
56     ULONG ref = InterlockedIncrement(&This->ref);
57
58     TRACE("%p increasing refcount to %u.\n", iface, ref);
59
60     if (ref == 1)
61     {
62         IDirect3DDevice8_AddRef(This->parentDevice);
63         wined3d_mutex_lock();
64         wined3d_texture_incref(This->wined3d_texture);
65         wined3d_mutex_unlock();
66     }
67
68     return ref;
69 }
70
71 static ULONG WINAPI IDirect3DTexture8Impl_Release(IDirect3DTexture8 *iface)
72 {
73     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
74     ULONG ref = InterlockedDecrement(&This->ref);
75
76     TRACE("%p decreasing refcount to %u.\n", iface, ref);
77
78     if (ref == 0) {
79         IDirect3DDevice8 *parentDevice = This->parentDevice;
80
81         wined3d_mutex_lock();
82         wined3d_texture_decref(This->wined3d_texture);
83         wined3d_mutex_unlock();
84
85         /* Release the device last, as it may cause the device to be destroyed. */
86         IDirect3DDevice8_Release(parentDevice);
87     }
88     return ref;
89 }
90
91 /* IDirect3DTexture8 IDirect3DResource8 Interface follow: */
92 static HRESULT WINAPI IDirect3DTexture8Impl_GetDevice(IDirect3DTexture8 *iface,
93         IDirect3DDevice8 **device)
94 {
95     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
96
97     TRACE("iface %p, device %p.\n", iface, device);
98
99     *device = (IDirect3DDevice8 *)This->parentDevice;
100     IDirect3DDevice8_AddRef(*device);
101
102     TRACE("Returning device %p.\n", *device);
103
104     return D3D_OK;
105 }
106
107 static HRESULT WINAPI IDirect3DTexture8Impl_SetPrivateData(IDirect3DTexture8 *iface,
108         REFGUID refguid, const void *pData, DWORD SizeOfData, DWORD Flags)
109 {
110     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
111     HRESULT hr;
112
113     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
114             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
115
116     wined3d_mutex_lock();
117     hr = wined3d_texture_set_private_data(This->wined3d_texture, refguid, pData, SizeOfData, Flags);
118     wined3d_mutex_unlock();
119
120     return hr;
121 }
122
123 static HRESULT WINAPI IDirect3DTexture8Impl_GetPrivateData(IDirect3DTexture8 *iface,
124         REFGUID refguid, void *pData, DWORD *pSizeOfData)
125 {
126     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
127     HRESULT hr;
128
129     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
130             iface, debugstr_guid(refguid), pData, pSizeOfData);
131
132     wined3d_mutex_lock();
133     hr = wined3d_texture_get_private_data(This->wined3d_texture, refguid, pData, pSizeOfData);
134     wined3d_mutex_unlock();
135
136     return hr;
137 }
138
139 static HRESULT WINAPI IDirect3DTexture8Impl_FreePrivateData(IDirect3DTexture8 *iface,
140         REFGUID refguid)
141 {
142     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
143     HRESULT hr;
144
145     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
146
147     wined3d_mutex_lock();
148     hr = wined3d_texture_free_private_data(This->wined3d_texture, refguid);
149     wined3d_mutex_unlock();
150
151     return hr;
152 }
153
154 static DWORD WINAPI IDirect3DTexture8Impl_SetPriority(IDirect3DTexture8 *iface, DWORD PriorityNew)
155 {
156     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
157     DWORD ret;
158
159     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
160
161     wined3d_mutex_lock();
162     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
163     wined3d_mutex_unlock();
164
165     return ret;
166 }
167
168 static DWORD WINAPI IDirect3DTexture8Impl_GetPriority(IDirect3DTexture8 *iface)
169 {
170     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
171     DWORD ret;
172
173     TRACE("iface %p.\n", iface);
174
175     wined3d_mutex_lock();
176     ret = wined3d_texture_get_priority(This->wined3d_texture);
177     wined3d_mutex_unlock();
178
179     return ret;
180 }
181
182 static void WINAPI IDirect3DTexture8Impl_PreLoad(IDirect3DTexture8 *iface)
183 {
184     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
185
186     TRACE("iface %p.\n", iface);
187
188     wined3d_mutex_lock();
189     wined3d_texture_preload(This->wined3d_texture);
190     wined3d_mutex_unlock();
191 }
192
193 static D3DRESOURCETYPE WINAPI IDirect3DTexture8Impl_GetType(IDirect3DTexture8 *iface)
194 {
195     TRACE("iface %p.\n", iface);
196
197     return D3DRTYPE_TEXTURE;
198 }
199
200 /* IDirect3DTexture8 IDirect3DBaseTexture8 Interface follow: */
201 static DWORD WINAPI IDirect3DTexture8Impl_SetLOD(IDirect3DTexture8 *iface, DWORD LODNew)
202 {
203     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
204     DWORD ret;
205
206     TRACE("iface %p, lod %u.\n", iface, LODNew);
207
208     wined3d_mutex_lock();
209     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
210     wined3d_mutex_unlock();
211
212     return ret;
213 }
214
215 static DWORD WINAPI IDirect3DTexture8Impl_GetLOD(IDirect3DTexture8 *iface)
216 {
217     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
218     DWORD ret;
219
220     TRACE("iface %p.\n", iface);
221
222     wined3d_mutex_lock();
223     ret = wined3d_texture_get_lod(This->wined3d_texture);
224     wined3d_mutex_unlock();
225
226     return ret;
227 }
228
229 static DWORD WINAPI IDirect3DTexture8Impl_GetLevelCount(IDirect3DTexture8 *iface)
230 {
231     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
232     DWORD ret;
233
234     TRACE("iface %p.\n", iface);
235
236     wined3d_mutex_lock();
237     ret = wined3d_texture_get_level_count(This->wined3d_texture);
238     wined3d_mutex_unlock();
239
240     return ret;
241 }
242
243 static HRESULT WINAPI IDirect3DTexture8Impl_GetLevelDesc(IDirect3DTexture8 *iface,
244         UINT level, D3DSURFACE_DESC *desc)
245 {
246     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
247     struct wined3d_resource *sub_resource;
248     HRESULT hr = D3D_OK;
249
250     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
251
252     wined3d_mutex_lock();
253     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
254         hr = D3DERR_INVALIDCALL;
255     else
256     {
257         struct wined3d_resource_desc wined3d_desc;
258
259         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
260         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
261         desc->Type = wined3d_desc.resource_type;
262         desc->Usage = wined3d_desc.usage;
263         desc->Pool = wined3d_desc.pool;
264         desc->Size = wined3d_desc.size;
265         desc->MultiSampleType = wined3d_desc.multisample_type;
266         desc->Width = wined3d_desc.width;
267         desc->Height = wined3d_desc.height;
268     }
269     wined3d_mutex_unlock();
270
271     return hr;
272 }
273
274 static HRESULT WINAPI IDirect3DTexture8Impl_GetSurfaceLevel(IDirect3DTexture8 *iface,
275         UINT level, IDirect3DSurface8 **surface)
276 {
277     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
278     struct wined3d_resource *sub_resource;
279
280     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
281
282     wined3d_mutex_lock();
283     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
284     {
285         wined3d_mutex_unlock();
286         return D3DERR_INVALIDCALL;
287     }
288
289     *surface = wined3d_resource_get_parent(sub_resource);
290     IDirect3DSurface8_AddRef(*surface);
291     wined3d_mutex_unlock();
292
293     return D3D_OK;
294 }
295
296 static HRESULT WINAPI IDirect3DTexture8Impl_LockRect(IDirect3DTexture8 *iface, UINT level,
297         D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
298 {
299     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
300     struct wined3d_resource *sub_resource;
301     HRESULT hr;
302
303     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
304             iface, level, locked_rect, rect, flags);
305
306     wined3d_mutex_lock();
307     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
308         hr = D3DERR_INVALIDCALL;
309     else
310         hr = IDirect3DSurface8_LockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource),
311                 locked_rect, rect, flags);
312     wined3d_mutex_unlock();
313
314     return hr;
315 }
316
317 static HRESULT WINAPI IDirect3DTexture8Impl_UnlockRect(IDirect3DTexture8 *iface, UINT level)
318 {
319     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
320     struct wined3d_resource *sub_resource;
321     HRESULT hr;
322
323     TRACE("iface %p, level %u.\n", iface, level);
324
325     wined3d_mutex_lock();
326     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
327         hr = D3DERR_INVALIDCALL;
328     else
329         hr = IDirect3DSurface8_UnlockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource));
330     wined3d_mutex_unlock();
331
332     return hr;
333 }
334
335 static HRESULT WINAPI IDirect3DTexture8Impl_AddDirtyRect(IDirect3DTexture8 *iface, const RECT *dirty_rect)
336 {
337     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
338     HRESULT hr;
339
340     TRACE("iface %p, dirty_rect %s.\n",
341             iface, wine_dbgstr_rect(dirty_rect));
342
343     wined3d_mutex_lock();
344     if (!dirty_rect)
345         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, NULL);
346     else
347     {
348         WINED3DBOX dirty_region;
349
350         dirty_region.Left = dirty_rect->left;
351         dirty_region.Top = dirty_rect->top;
352         dirty_region.Right = dirty_rect->right;
353         dirty_region.Bottom = dirty_rect->bottom;
354         dirty_region.Front = 0;
355         dirty_region.Back = 1;
356         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, &dirty_region);
357     }
358     wined3d_mutex_unlock();
359
360     return hr;
361 }
362
363 static const IDirect3DTexture8Vtbl Direct3DTexture8_Vtbl =
364 {
365     /* IUnknown */
366     IDirect3DTexture8Impl_QueryInterface,
367     IDirect3DTexture8Impl_AddRef,
368     IDirect3DTexture8Impl_Release,
369      /* IDirect3DResource8 */
370     IDirect3DTexture8Impl_GetDevice,
371     IDirect3DTexture8Impl_SetPrivateData,
372     IDirect3DTexture8Impl_GetPrivateData,
373     IDirect3DTexture8Impl_FreePrivateData,
374     IDirect3DTexture8Impl_SetPriority,
375     IDirect3DTexture8Impl_GetPriority,
376     IDirect3DTexture8Impl_PreLoad,
377     IDirect3DTexture8Impl_GetType,
378     /* IDirect3dBaseTexture8 */
379     IDirect3DTexture8Impl_SetLOD,
380     IDirect3DTexture8Impl_GetLOD,
381     IDirect3DTexture8Impl_GetLevelCount,
382     /* IDirect3DTexture8 */
383     IDirect3DTexture8Impl_GetLevelDesc,
384     IDirect3DTexture8Impl_GetSurfaceLevel,
385     IDirect3DTexture8Impl_LockRect,
386     IDirect3DTexture8Impl_UnlockRect,
387     IDirect3DTexture8Impl_AddDirtyRect
388 };
389
390 static void STDMETHODCALLTYPE d3d8_texture_wined3d_object_destroyed(void *parent)
391 {
392     HeapFree(GetProcessHeap(), 0, parent);
393 }
394
395 static const struct wined3d_parent_ops d3d8_texture_wined3d_parent_ops =
396 {
397     d3d8_texture_wined3d_object_destroyed,
398 };
399
400 HRESULT texture_init(IDirect3DTexture8Impl *texture, IDirect3DDevice8Impl *device,
401         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
402 {
403     HRESULT hr;
404
405     texture->IDirect3DTexture8_iface.lpVtbl = &Direct3DTexture8_Vtbl;
406     texture->ref = 1;
407
408     wined3d_mutex_lock();
409     hr = wined3d_texture_create_2d(device->wined3d_device, width, height, levels,
410             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
411             texture, &d3d8_texture_wined3d_parent_ops, &texture->wined3d_texture);
412     wined3d_mutex_unlock();
413     if (FAILED(hr))
414     {
415         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
416         return hr;
417     }
418
419     texture->parentDevice = &device->IDirect3DDevice8_iface;
420     IDirect3DDevice8_AddRef(texture->parentDevice);
421
422     return D3D_OK;
423 }