mshtml: Added IHTMLStyle2::overflowX implementation.
[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 static inline struct d3d8_texture *impl_from_IDirect3DTexture8(IDirect3DTexture8 *iface)
27 {
28     return CONTAINING_RECORD(iface, struct d3d8_texture, IDirect3DBaseTexture8_iface);
29 }
30
31 static HRESULT WINAPI d3d8_texture_2d_QueryInterface(IDirect3DTexture8 *iface, REFIID riid, void **out)
32 {
33     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
34
35     if (IsEqualGUID(riid, &IID_IDirect3DTexture8)
36             || IsEqualGUID(riid, &IID_IDirect3DBaseTexture8)
37             || IsEqualGUID(riid, &IID_IDirect3DResource8)
38             || IsEqualGUID(riid, &IID_IUnknown))
39     {
40         IUnknown_AddRef(iface);
41         *out = iface;
42         return S_OK;
43     }
44
45     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46
47     *out = NULL;
48     return E_NOINTERFACE;
49 }
50
51 static ULONG WINAPI d3d8_texture_2d_AddRef(IDirect3DTexture8 *iface)
52 {
53     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
54     ULONG ref = InterlockedIncrement(&texture->refcount);
55
56     TRACE("%p increasing refcount to %u.\n", iface, ref);
57
58     if (ref == 1)
59     {
60         IDirect3DDevice8_AddRef(texture->parent_device);
61         wined3d_mutex_lock();
62         wined3d_texture_incref(texture->wined3d_texture);
63         wined3d_mutex_unlock();
64     }
65
66     return ref;
67 }
68
69 static ULONG WINAPI d3d8_texture_2d_Release(IDirect3DTexture8 *iface)
70 {
71     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
72     ULONG ref = InterlockedDecrement(&texture->refcount);
73
74     TRACE("%p decreasing refcount to %u.\n", iface, ref);
75
76     if (!ref)
77     {
78         IDirect3DDevice8 *parent_device = texture->parent_device;
79
80         wined3d_mutex_lock();
81         wined3d_texture_decref(texture->wined3d_texture);
82         wined3d_mutex_unlock();
83
84         /* Release the device last, as it may cause the device to be destroyed. */
85         IDirect3DDevice8_Release(parent_device);
86     }
87     return ref;
88 }
89
90 static HRESULT WINAPI d3d8_texture_2d_GetDevice(IDirect3DTexture8 *iface, IDirect3DDevice8 **device)
91 {
92     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
93
94     TRACE("iface %p, device %p.\n", iface, device);
95
96     *device = texture->parent_device;
97     IDirect3DDevice8_AddRef(*device);
98
99     TRACE("Returning device %p.\n", *device);
100
101     return D3D_OK;
102 }
103
104 static HRESULT WINAPI d3d8_texture_2d_SetPrivateData(IDirect3DTexture8 *iface,
105         REFGUID guid, const void *data, DWORD data_size, DWORD flags)
106 {
107     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
108     struct wined3d_resource *resource;
109     HRESULT hr;
110
111     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
112             iface, debugstr_guid(guid), data, data_size, flags);
113
114     wined3d_mutex_lock();
115     resource = wined3d_texture_get_resource(texture->wined3d_texture);
116     hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
117     wined3d_mutex_unlock();
118
119     return hr;
120 }
121
122 static HRESULT WINAPI d3d8_texture_2d_GetPrivateData(IDirect3DTexture8 *iface,
123         REFGUID guid, void *data, DWORD *data_size)
124 {
125     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
126     struct wined3d_resource *resource;
127     HRESULT hr;
128
129     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
130             iface, debugstr_guid(guid), data, data_size);
131
132     wined3d_mutex_lock();
133     resource = wined3d_texture_get_resource(texture->wined3d_texture);
134     hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
135     wined3d_mutex_unlock();
136
137     return hr;
138 }
139
140 static HRESULT WINAPI d3d8_texture_2d_FreePrivateData(IDirect3DTexture8 *iface, REFGUID guid)
141 {
142     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
143     struct wined3d_resource *resource;
144     HRESULT hr;
145
146     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
147
148     wined3d_mutex_lock();
149     resource = wined3d_texture_get_resource(texture->wined3d_texture);
150     hr = wined3d_resource_free_private_data(resource, guid);
151     wined3d_mutex_unlock();
152
153     return hr;
154 }
155
156 static DWORD WINAPI d3d8_texture_2d_SetPriority(IDirect3DTexture8 *iface, DWORD priority)
157 {
158     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
159     DWORD ret;
160
161     TRACE("iface %p, priority %u.\n", iface, priority);
162
163     wined3d_mutex_lock();
164     ret = wined3d_texture_set_priority(texture->wined3d_texture, priority);
165     wined3d_mutex_unlock();
166
167     return ret;
168 }
169
170 static DWORD WINAPI d3d8_texture_2d_GetPriority(IDirect3DTexture8 *iface)
171 {
172     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
173     DWORD ret;
174
175     TRACE("iface %p.\n", iface);
176
177     wined3d_mutex_lock();
178     ret = wined3d_texture_get_priority(texture->wined3d_texture);
179     wined3d_mutex_unlock();
180
181     return ret;
182 }
183
184 static void WINAPI d3d8_texture_2d_PreLoad(IDirect3DTexture8 *iface)
185 {
186     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
187
188     TRACE("iface %p.\n", iface);
189
190     wined3d_mutex_lock();
191     wined3d_texture_preload(texture->wined3d_texture);
192     wined3d_mutex_unlock();
193 }
194
195 static D3DRESOURCETYPE WINAPI d3d8_texture_2d_GetType(IDirect3DTexture8 *iface)
196 {
197     TRACE("iface %p.\n", iface);
198
199     return D3DRTYPE_TEXTURE;
200 }
201
202 static DWORD WINAPI d3d8_texture_2d_SetLOD(IDirect3DTexture8 *iface, DWORD lod)
203 {
204     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
205     DWORD ret;
206
207     TRACE("iface %p, lod %u.\n", iface, lod);
208
209     wined3d_mutex_lock();
210     ret = wined3d_texture_set_lod(texture->wined3d_texture, lod);
211     wined3d_mutex_unlock();
212
213     return ret;
214 }
215
216 static DWORD WINAPI d3d8_texture_2d_GetLOD(IDirect3DTexture8 *iface)
217 {
218     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
219     DWORD ret;
220
221     TRACE("iface %p.\n", iface);
222
223     wined3d_mutex_lock();
224     ret = wined3d_texture_get_lod(texture->wined3d_texture);
225     wined3d_mutex_unlock();
226
227     return ret;
228 }
229
230 static DWORD WINAPI d3d8_texture_2d_GetLevelCount(IDirect3DTexture8 *iface)
231 {
232     struct d3d8_texture *texture = impl_from_IDirect3DTexture8(iface);
233     DWORD ret;
234
235     TRACE("iface %p.\n", iface);
236
237     wined3d_mutex_lock();
238     ret = wined3d_texture_get_level_count(texture->wined3d_texture);
239     wined3d_mutex_unlock();
240
241     return ret;
242 }
243
244 static HRESULT WINAPI d3d8_texture_2d_GetLevelDesc(IDirect3DTexture8 *iface, UINT level, D3DSURFACE_DESC *desc)
245 {
246     struct d3d8_texture *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 & WINED3DUSAGE_MASK;
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 d3d8_texture_2d_GetSurfaceLevel(IDirect3DTexture8 *iface,
275         UINT level, IDirect3DSurface8 **surface)
276 {
277     struct d3d8_texture *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 d3d8_texture_2d_LockRect(IDirect3DTexture8 *iface, UINT level,
297         D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
298 {
299     struct d3d8_texture *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 d3d8_texture_2d_UnlockRect(IDirect3DTexture8 *iface, UINT level)
318 {
319     struct d3d8_texture *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 d3d8_texture_2d_AddDirtyRect(IDirect3DTexture8 *iface, const RECT *dirty_rect)
336 {
337     struct d3d8_texture *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         struct wined3d_box 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     d3d8_texture_2d_QueryInterface,
367     d3d8_texture_2d_AddRef,
368     d3d8_texture_2d_Release,
369     /* IDirect3DResource8 */
370     d3d8_texture_2d_GetDevice,
371     d3d8_texture_2d_SetPrivateData,
372     d3d8_texture_2d_GetPrivateData,
373     d3d8_texture_2d_FreePrivateData,
374     d3d8_texture_2d_SetPriority,
375     d3d8_texture_2d_GetPriority,
376     d3d8_texture_2d_PreLoad,
377     d3d8_texture_2d_GetType,
378     /* IDirect3dBaseTexture8 */
379     d3d8_texture_2d_SetLOD,
380     d3d8_texture_2d_GetLOD,
381     d3d8_texture_2d_GetLevelCount,
382     /* IDirect3DTexture8 */
383     d3d8_texture_2d_GetLevelDesc,
384     d3d8_texture_2d_GetSurfaceLevel,
385     d3d8_texture_2d_LockRect,
386     d3d8_texture_2d_UnlockRect,
387     d3d8_texture_2d_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(struct d3d8_texture *texture, IDirect3DDevice8Impl *device,
401         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
402 {
403     HRESULT hr;
404
405     texture->IDirect3DBaseTexture8_iface.lpVtbl = (const IDirect3DBaseTexture8Vtbl *)&Direct3DTexture8_Vtbl;
406     texture->refcount = 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->parent_device = &device->IDirect3DDevice8_iface;
420     IDirect3DDevice8_AddRef(texture->parent_device);
421
422     return D3D_OK;
423 }