wined3d: Delete the static critical sections when unloading the dll.
[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 = 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     struct wined3d_resource *resource;
112     HRESULT hr;
113
114     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
115             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
116
117     wined3d_mutex_lock();
118     resource = wined3d_texture_get_resource(This->wined3d_texture);
119     hr = wined3d_resource_set_private_data(resource, refguid, pData, SizeOfData, Flags);
120     wined3d_mutex_unlock();
121
122     return hr;
123 }
124
125 static HRESULT WINAPI IDirect3DTexture8Impl_GetPrivateData(IDirect3DTexture8 *iface,
126         REFGUID refguid, void *pData, DWORD *pSizeOfData)
127 {
128     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
129     struct wined3d_resource *resource;
130     HRESULT hr;
131
132     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
133             iface, debugstr_guid(refguid), pData, pSizeOfData);
134
135     wined3d_mutex_lock();
136     resource = wined3d_texture_get_resource(This->wined3d_texture);
137     hr = wined3d_resource_get_private_data(resource, refguid, pData, pSizeOfData);
138     wined3d_mutex_unlock();
139
140     return hr;
141 }
142
143 static HRESULT WINAPI IDirect3DTexture8Impl_FreePrivateData(IDirect3DTexture8 *iface,
144         REFGUID refguid)
145 {
146     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
147     struct wined3d_resource *resource;
148     HRESULT hr;
149
150     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
151
152     wined3d_mutex_lock();
153     resource = wined3d_texture_get_resource(This->wined3d_texture);
154     hr = wined3d_resource_free_private_data(resource, refguid);
155     wined3d_mutex_unlock();
156
157     return hr;
158 }
159
160 static DWORD WINAPI IDirect3DTexture8Impl_SetPriority(IDirect3DTexture8 *iface, DWORD PriorityNew)
161 {
162     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
163     DWORD ret;
164
165     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
166
167     wined3d_mutex_lock();
168     ret = wined3d_texture_set_priority(This->wined3d_texture, PriorityNew);
169     wined3d_mutex_unlock();
170
171     return ret;
172 }
173
174 static DWORD WINAPI IDirect3DTexture8Impl_GetPriority(IDirect3DTexture8 *iface)
175 {
176     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
177     DWORD ret;
178
179     TRACE("iface %p.\n", iface);
180
181     wined3d_mutex_lock();
182     ret = wined3d_texture_get_priority(This->wined3d_texture);
183     wined3d_mutex_unlock();
184
185     return ret;
186 }
187
188 static void WINAPI IDirect3DTexture8Impl_PreLoad(IDirect3DTexture8 *iface)
189 {
190     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
191
192     TRACE("iface %p.\n", iface);
193
194     wined3d_mutex_lock();
195     wined3d_texture_preload(This->wined3d_texture);
196     wined3d_mutex_unlock();
197 }
198
199 static D3DRESOURCETYPE WINAPI IDirect3DTexture8Impl_GetType(IDirect3DTexture8 *iface)
200 {
201     TRACE("iface %p.\n", iface);
202
203     return D3DRTYPE_TEXTURE;
204 }
205
206 /* IDirect3DTexture8 IDirect3DBaseTexture8 Interface follow: */
207 static DWORD WINAPI IDirect3DTexture8Impl_SetLOD(IDirect3DTexture8 *iface, DWORD LODNew)
208 {
209     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
210     DWORD ret;
211
212     TRACE("iface %p, lod %u.\n", iface, LODNew);
213
214     wined3d_mutex_lock();
215     ret = wined3d_texture_set_lod(This->wined3d_texture, LODNew);
216     wined3d_mutex_unlock();
217
218     return ret;
219 }
220
221 static DWORD WINAPI IDirect3DTexture8Impl_GetLOD(IDirect3DTexture8 *iface)
222 {
223     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
224     DWORD ret;
225
226     TRACE("iface %p.\n", iface);
227
228     wined3d_mutex_lock();
229     ret = wined3d_texture_get_lod(This->wined3d_texture);
230     wined3d_mutex_unlock();
231
232     return ret;
233 }
234
235 static DWORD WINAPI IDirect3DTexture8Impl_GetLevelCount(IDirect3DTexture8 *iface)
236 {
237     IDirect3DTexture8Impl *This = impl_from_IDirect3DTexture8(iface);
238     DWORD ret;
239
240     TRACE("iface %p.\n", iface);
241
242     wined3d_mutex_lock();
243     ret = wined3d_texture_get_level_count(This->wined3d_texture);
244     wined3d_mutex_unlock();
245
246     return ret;
247 }
248
249 static HRESULT WINAPI IDirect3DTexture8Impl_GetLevelDesc(IDirect3DTexture8 *iface,
250         UINT level, D3DSURFACE_DESC *desc)
251 {
252     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
253     struct wined3d_resource *sub_resource;
254     HRESULT hr = D3D_OK;
255
256     TRACE("iface %p, level %u, desc %p.\n", iface, level, desc);
257
258     wined3d_mutex_lock();
259     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
260         hr = D3DERR_INVALIDCALL;
261     else
262     {
263         struct wined3d_resource_desc wined3d_desc;
264
265         wined3d_resource_get_desc(sub_resource, &wined3d_desc);
266         desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
267         desc->Type = wined3d_desc.resource_type;
268         desc->Usage = wined3d_desc.usage;
269         desc->Pool = wined3d_desc.pool;
270         desc->Size = wined3d_desc.size;
271         desc->MultiSampleType = wined3d_desc.multisample_type;
272         desc->Width = wined3d_desc.width;
273         desc->Height = wined3d_desc.height;
274     }
275     wined3d_mutex_unlock();
276
277     return hr;
278 }
279
280 static HRESULT WINAPI IDirect3DTexture8Impl_GetSurfaceLevel(IDirect3DTexture8 *iface,
281         UINT level, IDirect3DSurface8 **surface)
282 {
283     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
284     struct wined3d_resource *sub_resource;
285
286     TRACE("iface %p, level %u, surface %p.\n", iface, level, surface);
287
288     wined3d_mutex_lock();
289     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
290     {
291         wined3d_mutex_unlock();
292         return D3DERR_INVALIDCALL;
293     }
294
295     *surface = wined3d_resource_get_parent(sub_resource);
296     IDirect3DSurface8_AddRef(*surface);
297     wined3d_mutex_unlock();
298
299     return D3D_OK;
300 }
301
302 static HRESULT WINAPI IDirect3DTexture8Impl_LockRect(IDirect3DTexture8 *iface, UINT level,
303         D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
304 {
305     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
306     struct wined3d_resource *sub_resource;
307     HRESULT hr;
308
309     TRACE("iface %p, level %u, locked_rect %p, rect %p, flags %#x.\n",
310             iface, level, locked_rect, rect, flags);
311
312     wined3d_mutex_lock();
313     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
314         hr = D3DERR_INVALIDCALL;
315     else
316         hr = IDirect3DSurface8_LockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource),
317                 locked_rect, rect, flags);
318     wined3d_mutex_unlock();
319
320     return hr;
321 }
322
323 static HRESULT WINAPI IDirect3DTexture8Impl_UnlockRect(IDirect3DTexture8 *iface, UINT level)
324 {
325     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
326     struct wined3d_resource *sub_resource;
327     HRESULT hr;
328
329     TRACE("iface %p, level %u.\n", iface, level);
330
331     wined3d_mutex_lock();
332     if (!(sub_resource = wined3d_texture_get_sub_resource(texture->wined3d_texture, level)))
333         hr = D3DERR_INVALIDCALL;
334     else
335         hr = IDirect3DSurface8_UnlockRect((IDirect3DSurface8 *)wined3d_resource_get_parent(sub_resource));
336     wined3d_mutex_unlock();
337
338     return hr;
339 }
340
341 static HRESULT WINAPI IDirect3DTexture8Impl_AddDirtyRect(IDirect3DTexture8 *iface, const RECT *dirty_rect)
342 {
343     IDirect3DTexture8Impl *texture = impl_from_IDirect3DTexture8(iface);
344     HRESULT hr;
345
346     TRACE("iface %p, dirty_rect %s.\n",
347             iface, wine_dbgstr_rect(dirty_rect));
348
349     wined3d_mutex_lock();
350     if (!dirty_rect)
351         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, NULL);
352     else
353     {
354         WINED3DBOX dirty_region;
355
356         dirty_region.Left = dirty_rect->left;
357         dirty_region.Top = dirty_rect->top;
358         dirty_region.Right = dirty_rect->right;
359         dirty_region.Bottom = dirty_rect->bottom;
360         dirty_region.Front = 0;
361         dirty_region.Back = 1;
362         hr = wined3d_texture_add_dirty_region(texture->wined3d_texture, 0, &dirty_region);
363     }
364     wined3d_mutex_unlock();
365
366     return hr;
367 }
368
369 static const IDirect3DTexture8Vtbl Direct3DTexture8_Vtbl =
370 {
371     /* IUnknown */
372     IDirect3DTexture8Impl_QueryInterface,
373     IDirect3DTexture8Impl_AddRef,
374     IDirect3DTexture8Impl_Release,
375      /* IDirect3DResource8 */
376     IDirect3DTexture8Impl_GetDevice,
377     IDirect3DTexture8Impl_SetPrivateData,
378     IDirect3DTexture8Impl_GetPrivateData,
379     IDirect3DTexture8Impl_FreePrivateData,
380     IDirect3DTexture8Impl_SetPriority,
381     IDirect3DTexture8Impl_GetPriority,
382     IDirect3DTexture8Impl_PreLoad,
383     IDirect3DTexture8Impl_GetType,
384     /* IDirect3dBaseTexture8 */
385     IDirect3DTexture8Impl_SetLOD,
386     IDirect3DTexture8Impl_GetLOD,
387     IDirect3DTexture8Impl_GetLevelCount,
388     /* IDirect3DTexture8 */
389     IDirect3DTexture8Impl_GetLevelDesc,
390     IDirect3DTexture8Impl_GetSurfaceLevel,
391     IDirect3DTexture8Impl_LockRect,
392     IDirect3DTexture8Impl_UnlockRect,
393     IDirect3DTexture8Impl_AddDirtyRect
394 };
395
396 static void STDMETHODCALLTYPE d3d8_texture_wined3d_object_destroyed(void *parent)
397 {
398     HeapFree(GetProcessHeap(), 0, parent);
399 }
400
401 static const struct wined3d_parent_ops d3d8_texture_wined3d_parent_ops =
402 {
403     d3d8_texture_wined3d_object_destroyed,
404 };
405
406 HRESULT texture_init(IDirect3DTexture8Impl *texture, IDirect3DDevice8Impl *device,
407         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool)
408 {
409     HRESULT hr;
410
411     texture->IDirect3DTexture8_iface.lpVtbl = &Direct3DTexture8_Vtbl;
412     texture->ref = 1;
413
414     wined3d_mutex_lock();
415     hr = wined3d_texture_create_2d(device->wined3d_device, width, height, levels,
416             usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(format), pool,
417             texture, &d3d8_texture_wined3d_parent_ops, &texture->wined3d_texture);
418     wined3d_mutex_unlock();
419     if (FAILED(hr))
420     {
421         WARN("Failed to create wined3d texture, hr %#x.\n", hr);
422         return hr;
423     }
424
425     texture->parentDevice = &device->IDirect3DDevice8_iface;
426     IDirect3DDevice8_AddRef(texture->parentDevice);
427
428     return D3D_OK;
429 }