wined3d: Get rid of the WINED3DTEXTUREOP typedef.
[wine] / dlls / ddraw / palette.c
1 /*
2  * Copyright 2006 Stefan Dösinger
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include "ddraw_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
25
26 /*****************************************************************************
27  * IDirectDrawPalette::QueryInterface
28  *
29  * A usual QueryInterface implementation. Can only Query IUnknown and
30  * IDirectDrawPalette
31  *
32  * Params:
33  *  refiid: The interface id queried for
34  *  obj: Address to return the interface pointer at
35  *
36  * Returns:
37  *  S_OK on success
38  *  E_NOINTERFACE if the requested interface wasn't found
39  *****************************************************************************/
40 static HRESULT WINAPI
41 IDirectDrawPaletteImpl_QueryInterface(IDirectDrawPalette *iface,
42                                       REFIID refiid,
43                                       void **obj)
44 {
45     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
46
47     if (IsEqualGUID(refiid, &IID_IUnknown)
48         || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
49     {
50         *obj = iface;
51         IDirectDrawPalette_AddRef(iface);
52         return S_OK;
53     }
54     else
55     {
56         *obj = NULL;
57         return E_NOINTERFACE;
58     }
59 }
60
61 /*****************************************************************************
62  * IDirectDrawPaletteImpl::AddRef
63  *
64  * Increases the refcount.
65  *
66  * Returns:
67  *  The new refcount
68  *
69  *****************************************************************************/
70 static ULONG WINAPI
71 IDirectDrawPaletteImpl_AddRef(IDirectDrawPalette *iface)
72 {
73     IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
74     ULONG ref = InterlockedIncrement(&This->ref);
75
76     TRACE("%p increasing refcount to %u.\n", This, ref);
77
78     return ref;
79 }
80
81 /*****************************************************************************
82  * IDirectDrawPaletteImpl::Release
83  *
84  * Reduces the refcount. If the refcount falls to 0, the object is destroyed
85  *
86  * Returns:
87  *  The new refcount
88  *
89  *****************************************************************************/
90 static ULONG WINAPI
91 IDirectDrawPaletteImpl_Release(IDirectDrawPalette *iface)
92 {
93     IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
94     ULONG ref = InterlockedDecrement(&This->ref);
95
96     TRACE("%p decreasing refcount to %u.\n", This, ref);
97
98     if (ref == 0)
99     {
100         wined3d_mutex_lock();
101         wined3d_palette_decref(This->wineD3DPalette);
102         if(This->ifaceToRelease)
103         {
104             IUnknown_Release(This->ifaceToRelease);
105         }
106         wined3d_mutex_unlock();
107
108         HeapFree(GetProcessHeap(), 0, This);
109     }
110
111     return ref;
112 }
113
114 /*****************************************************************************
115  * IDirectDrawPalette::Initialize
116  *
117  * Initializes the palette. As we start initialized, return
118  * DDERR_ALREADYINITIALIZED
119  *
120  * Params:
121  *  DD: DirectDraw interface this palette is assigned to
122  *  Flags: Some flags, as usual
123  *  ColorTable: The startup color table
124  *
125  * Returns:
126  *  DDERR_ALREADYINITIALIZED
127  *
128  *****************************************************************************/
129 static HRESULT WINAPI
130 IDirectDrawPaletteImpl_Initialize(IDirectDrawPalette *iface,
131                                   IDirectDraw *DD,
132                                   DWORD Flags,
133                                   PALETTEENTRY *ColorTable)
134 {
135     TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
136             iface, DD, Flags, ColorTable);
137
138     return DDERR_ALREADYINITIALIZED;
139 }
140
141 /*****************************************************************************
142  * IDirectDrawPalette::GetCaps
143  *
144  * Returns the palette description
145  *
146  * Params:
147  *  Caps: Address to store the caps at
148  *
149  * Returns:
150  *  D3D_OK on success
151  *  DDERR_INVALIDPARAMS if Caps is NULL
152  *  For more details, see IWineD3DPalette::GetCaps
153  *
154  *****************************************************************************/
155 static HRESULT WINAPI
156 IDirectDrawPaletteImpl_GetCaps(IDirectDrawPalette *iface,
157                                DWORD *Caps)
158 {
159     IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
160
161     TRACE("iface %p, caps %p.\n", iface, Caps);
162
163     wined3d_mutex_lock();
164     *Caps = wined3d_palette_get_flags(This->wineD3DPalette);
165     wined3d_mutex_unlock();
166
167     return D3D_OK;
168 }
169
170 /*****************************************************************************
171  * IDirectDrawPalette::SetEntries
172  *
173  * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
174  * care for updating the surface.
175  *
176  * Params:
177  *  Flags: Flags, as usual
178  *  Start: First palette entry to set
179  *  Count: Number of entries to set
180  *  PalEnt: Source entries
181  *
182  * Returns:
183  *  D3D_OK on success
184  *  DDERR_INVALIDPARAMS if PalEnt is NULL
185  *  For details, see IWineD3DDevice::SetEntries
186  *
187  *****************************************************************************/
188 static HRESULT WINAPI
189 IDirectDrawPaletteImpl_SetEntries(IDirectDrawPalette *iface,
190                                   DWORD Flags,
191                                   DWORD Start,
192                                   DWORD Count,
193                                   PALETTEENTRY *PalEnt)
194 {
195     IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
196     HRESULT hr;
197
198     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
199             iface, Flags, Start, Count, PalEnt);
200
201     if(!PalEnt)
202         return DDERR_INVALIDPARAMS;
203
204     wined3d_mutex_lock();
205     hr = wined3d_palette_set_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
206     wined3d_mutex_unlock();
207
208     return hr;
209 }
210
211 /*****************************************************************************
212  * IDirectDrawPalette::GetEntries
213  *
214  * Returns the entries stored in this interface.
215  *
216  * Params:
217  *  Flags: Flags :)
218  *  Start: First entry to return
219  *  Count: The number of entries to return
220  *  PalEnt: PALETTEENTRY structure to write the entries to
221  *
222  * Returns:
223  *  D3D_OK on success
224  *  DDERR_INVALIDPARAMS if PalEnt is NULL
225  *  For details, see IWineD3DDevice::SetEntries
226  *
227  *****************************************************************************/
228 static HRESULT WINAPI
229 IDirectDrawPaletteImpl_GetEntries(IDirectDrawPalette *iface,
230                                   DWORD Flags,
231                                   DWORD Start,
232                                   DWORD Count,
233                                   PALETTEENTRY *PalEnt)
234 {
235     IDirectDrawPaletteImpl *This = impl_from_IDirectDrawPalette(iface);
236     HRESULT hr;
237
238     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
239             iface, Flags, Start, Count, PalEnt);
240
241     if(!PalEnt)
242         return DDERR_INVALIDPARAMS;
243
244     wined3d_mutex_lock();
245     hr = wined3d_palette_get_entries(This->wineD3DPalette, Flags, Start, Count, PalEnt);
246     wined3d_mutex_unlock();
247
248     return hr;
249 }
250
251 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
252 {
253     /*** IUnknown ***/
254     IDirectDrawPaletteImpl_QueryInterface,
255     IDirectDrawPaletteImpl_AddRef,
256     IDirectDrawPaletteImpl_Release,
257     /*** IDirectDrawPalette ***/
258     IDirectDrawPaletteImpl_GetCaps,
259     IDirectDrawPaletteImpl_GetEntries,
260     IDirectDrawPaletteImpl_Initialize,
261     IDirectDrawPaletteImpl_SetEntries
262 };
263
264 IDirectDrawPaletteImpl *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
265 {
266     if (!iface) return NULL;
267     assert(iface->lpVtbl == &ddraw_palette_vtbl);
268     return CONTAINING_RECORD(iface, IDirectDrawPaletteImpl, IDirectDrawPalette_iface);
269 }
270
271 HRESULT ddraw_palette_init(IDirectDrawPaletteImpl *palette,
272         IDirectDrawImpl *ddraw, DWORD flags, PALETTEENTRY *entries)
273 {
274     HRESULT hr;
275
276     palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
277     palette->ref = 1;
278
279     hr = wined3d_palette_create(ddraw->wined3d_device, flags,
280             entries, palette, &palette->wineD3DPalette);
281     if (FAILED(hr))
282     {
283         WARN("Failed to create wined3d palette, hr %#x.\n", hr);
284         return hr;
285     }
286
287     palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
288     IUnknown_AddRef(palette->ifaceToRelease);
289
290     return DD_OK;
291 }