gdiplus: Implement GdipSetImageAttributesGamma.
[wine] / dlls / d3d8 / indexbuffer.c
1 /*
2  * IDirect3DIndexBuffer8 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 /* IDirect3DIndexBuffer8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_QueryInterface(LPDIRECT3DINDEXBUFFER8 iface, REFIID riid, LPVOID *ppobj) {
28     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
29
30     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3DResource8)
34         || IsEqualGUID(riid, &IID_IDirect3DIndexBuffer8)) {
35         IUnknown_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DIndexBuffer8Impl_AddRef(LPDIRECT3DINDEXBUFFER8 iface) {
46     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("%p increasing refcount to %u.\n", iface, ref);
50
51     if (ref == 1)
52     {
53         IDirect3DDevice8_AddRef(This->parentDevice);
54         wined3d_mutex_lock();
55         IWineD3DBuffer_AddRef(This->wineD3DIndexBuffer);
56         wined3d_mutex_unlock();
57     }
58
59     return ref;
60 }
61
62 static ULONG WINAPI IDirect3DIndexBuffer8Impl_Release(LPDIRECT3DINDEXBUFFER8 iface) {
63     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
64     ULONG ref = InterlockedDecrement(&This->ref);
65
66     TRACE("%p decreasing refcount to %u.\n", iface, ref);
67
68     if (ref == 0) {
69         IDirect3DDevice8 *parentDevice = This->parentDevice;
70
71         wined3d_mutex_lock();
72         IWineD3DBuffer_Release(This->wineD3DIndexBuffer);
73         wined3d_mutex_unlock();
74
75         /* Release the device last, as it may cause the device to be destroyed. */
76         IDirect3DDevice8_Release(parentDevice);
77     }
78     return ref;
79 }
80
81 /* IDirect3DIndexBuffer8 IDirect3DResource8 Interface follow: */
82 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDevice(IDirect3DIndexBuffer8 *iface, IDirect3DDevice8 **device)
83 {
84     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
85
86     TRACE("iface %p, device %p.\n", iface, device);
87
88     *device = (IDirect3DDevice8 *)This->parentDevice;
89     IDirect3DDevice8_AddRef(*device);
90
91     TRACE("Returning device %p.\n", *device);
92
93     return D3D_OK;
94 }
95
96 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_SetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
97     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
98     HRESULT hr;
99
100     TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
101             iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
102
103     wined3d_mutex_lock();
104     hr = IWineD3DBuffer_SetPrivateData(This->wineD3DIndexBuffer, refguid, pData, SizeOfData, Flags);
105     wined3d_mutex_unlock();
106
107     return hr;
108 }
109
110 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetPrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
111     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
112     HRESULT hr;
113
114     TRACE("iface %p, guid %s, data %p, data_size %p.\n",
115             iface, debugstr_guid(refguid), pData, pSizeOfData);
116
117     wined3d_mutex_lock();
118     hr = IWineD3DBuffer_GetPrivateData(This->wineD3DIndexBuffer, refguid, pData, pSizeOfData);
119     wined3d_mutex_unlock();
120
121     return hr;
122 }
123
124 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_FreePrivateData(LPDIRECT3DINDEXBUFFER8 iface, REFGUID refguid) {
125     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
126     HRESULT hr;
127
128     TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
129
130     wined3d_mutex_lock();
131     hr = IWineD3DBuffer_FreePrivateData(This->wineD3DIndexBuffer, refguid);
132     wined3d_mutex_unlock();
133
134     return hr;
135 }
136
137 static DWORD WINAPI IDirect3DIndexBuffer8Impl_SetPriority(LPDIRECT3DINDEXBUFFER8 iface, DWORD PriorityNew) {
138     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
139     DWORD ret;
140
141     TRACE("iface %p, priority %u.\n", iface, PriorityNew);
142
143     wined3d_mutex_lock();
144     ret = IWineD3DBuffer_SetPriority(This->wineD3DIndexBuffer, PriorityNew);
145     wined3d_mutex_unlock();
146
147     return ret;
148 }
149
150 static DWORD WINAPI IDirect3DIndexBuffer8Impl_GetPriority(LPDIRECT3DINDEXBUFFER8 iface) {
151     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
152     DWORD ret;
153
154     TRACE("iface %p.\n", iface);
155
156     wined3d_mutex_lock();
157     ret = IWineD3DBuffer_GetPriority(This->wineD3DIndexBuffer);
158     wined3d_mutex_unlock();
159
160     return ret;
161 }
162
163 static void WINAPI IDirect3DIndexBuffer8Impl_PreLoad(LPDIRECT3DINDEXBUFFER8 iface) {
164     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
165
166     TRACE("iface %p.\n", iface);
167
168     wined3d_mutex_lock();
169     IWineD3DBuffer_PreLoad(This->wineD3DIndexBuffer);
170     wined3d_mutex_unlock();
171 }
172
173 static D3DRESOURCETYPE WINAPI IDirect3DIndexBuffer8Impl_GetType(IDirect3DIndexBuffer8 *iface)
174 {
175     TRACE("iface %p.\n", iface);
176
177     return D3DRTYPE_INDEXBUFFER;
178 }
179
180 /* IDirect3DIndexBuffer8 Interface follow: */
181 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Lock(LPDIRECT3DINDEXBUFFER8 iface, UINT OffsetToLock, UINT SizeToLock, BYTE **ppbData, DWORD Flags) {
182     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
183     HRESULT hr;
184
185     TRACE("iface %p, offset %u, size %u, data %p, flags %#x.\n",
186             iface, OffsetToLock, SizeToLock, ppbData, Flags);
187
188     wined3d_mutex_lock();
189     hr = IWineD3DBuffer_Map(This->wineD3DIndexBuffer, OffsetToLock, SizeToLock, ppbData, Flags);
190     wined3d_mutex_unlock();
191
192     return hr;
193 }
194
195 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_Unlock(LPDIRECT3DINDEXBUFFER8 iface) {
196     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
197     HRESULT hr;
198
199     TRACE("iface %p.\n", iface);
200
201     wined3d_mutex_lock();
202     hr = IWineD3DBuffer_Unmap(This->wineD3DIndexBuffer);
203     wined3d_mutex_unlock();
204
205     return hr;
206 }
207
208 static HRESULT WINAPI IDirect3DIndexBuffer8Impl_GetDesc(LPDIRECT3DINDEXBUFFER8 iface, D3DINDEXBUFFER_DESC *pDesc) {
209     IDirect3DIndexBuffer8Impl *This = (IDirect3DIndexBuffer8Impl *)iface;
210     HRESULT hr;
211     WINED3DBUFFER_DESC desc;
212
213     TRACE("iface %p, desc %p.\n", iface, pDesc);
214
215     wined3d_mutex_lock();
216     hr = IWineD3DBuffer_GetDesc(This->wineD3DIndexBuffer, &desc);
217     wined3d_mutex_unlock();
218
219     if (SUCCEEDED(hr)) {
220         pDesc->Format = d3dformat_from_wined3dformat(This->format);
221         pDesc->Type = D3DRTYPE_INDEXBUFFER;
222         pDesc->Usage = desc.Usage;
223         pDesc->Pool = desc.Pool;
224         pDesc->Size = desc.Size;
225     }
226
227     return hr;
228 }
229
230 static const IDirect3DIndexBuffer8Vtbl Direct3DIndexBuffer8_Vtbl =
231 {
232     /* IUnknown */
233     IDirect3DIndexBuffer8Impl_QueryInterface,
234     IDirect3DIndexBuffer8Impl_AddRef,
235     IDirect3DIndexBuffer8Impl_Release,
236     /* IDirect3DResource8 */
237     IDirect3DIndexBuffer8Impl_GetDevice,
238     IDirect3DIndexBuffer8Impl_SetPrivateData,
239     IDirect3DIndexBuffer8Impl_GetPrivateData,
240     IDirect3DIndexBuffer8Impl_FreePrivateData,
241     IDirect3DIndexBuffer8Impl_SetPriority,
242     IDirect3DIndexBuffer8Impl_GetPriority,
243     IDirect3DIndexBuffer8Impl_PreLoad,
244     IDirect3DIndexBuffer8Impl_GetType,
245     /* IDirect3DIndexBuffer8 */
246     IDirect3DIndexBuffer8Impl_Lock,
247     IDirect3DIndexBuffer8Impl_Unlock,
248     IDirect3DIndexBuffer8Impl_GetDesc
249 };
250
251 static void STDMETHODCALLTYPE d3d8_indexbuffer_wined3d_object_destroyed(void *parent)
252 {
253     HeapFree(GetProcessHeap(), 0, parent);
254 }
255
256 static const struct wined3d_parent_ops d3d8_indexbuffer_wined3d_parent_ops =
257 {
258     d3d8_indexbuffer_wined3d_object_destroyed,
259 };
260
261 HRESULT indexbuffer_init(IDirect3DIndexBuffer8Impl *buffer, IDirect3DDevice8Impl *device,
262         UINT size, DWORD usage, D3DFORMAT format, D3DPOOL pool)
263 {
264     HRESULT hr;
265
266     buffer->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
267     buffer->ref = 1;
268     buffer->format = wined3dformat_from_d3dformat(format);
269
270     wined3d_mutex_lock();
271     hr = IWineD3DDevice_CreateIndexBuffer(device->WineD3DDevice, size,
272             usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool, &buffer->wineD3DIndexBuffer,
273             (IUnknown *)buffer, &d3d8_indexbuffer_wined3d_parent_ops);
274     wined3d_mutex_unlock();
275     if (FAILED(hr))
276     {
277         WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
278         return hr;
279     }
280
281     buffer->parentDevice = (IDirect3DDevice8 *)device;
282     IUnknown_AddRef(buffer->parentDevice);
283
284     return D3D_OK;
285 }