fca27068547074bf5302c7a4382bc328ac589952
[reactos.git] / reactos / dll / directx / wine / 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 "ddraw_private.h"
20
21 /*****************************************************************************
22 * IDirectDrawPalette::QueryInterface
23 *
24 * A usual QueryInterface implementation. Can only Query IUnknown and
25 * IDirectDrawPalette
26 *
27 * Params:
28 * refiid: The interface id queried for
29 * obj: Address to return the interface pointer at
30 *
31 * Returns:
32 * S_OK on success
33 * E_NOINTERFACE if the requested interface wasn't found
34 *****************************************************************************/
35 static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
36 {
37 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
38
39 if (IsEqualGUID(refiid, &IID_IUnknown)
40 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
41 {
42 *obj = iface;
43 IDirectDrawPalette_AddRef(iface);
44 return S_OK;
45 }
46 else
47 {
48 *obj = NULL;
49 return E_NOINTERFACE;
50 }
51 }
52
53 /*****************************************************************************
54 * IDirectDrawPaletteImpl::AddRef
55 *
56 * Increases the refcount.
57 *
58 * Returns:
59 * The new refcount
60 *
61 *****************************************************************************/
62 static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
63 {
64 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
65 ULONG ref = InterlockedIncrement(&This->ref);
66
67 TRACE("%p increasing refcount to %u.\n", This, ref);
68
69 return ref;
70 }
71
72 /*****************************************************************************
73 * IDirectDrawPaletteImpl::Release
74 *
75 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
76 *
77 * Returns:
78 * The new refcount
79 *
80 *****************************************************************************/
81 static ULONG WINAPI ddraw_palette_Release(IDirectDrawPalette *iface)
82 {
83 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
84 ULONG ref = InterlockedDecrement(&This->ref);
85
86 TRACE("%p decreasing refcount to %u.\n", This, ref);
87
88 if (ref == 0)
89 {
90 wined3d_mutex_lock();
91 wined3d_palette_decref(This->wineD3DPalette);
92 if ((This->flags & DDPCAPS_PRIMARYSURFACE) && This->ddraw->primary)
93 This->ddraw->primary->palette = NULL;
94 if (This->ifaceToRelease)
95 IUnknown_Release(This->ifaceToRelease);
96 wined3d_mutex_unlock();
97
98 HeapFree(GetProcessHeap(), 0, This);
99 }
100
101 return ref;
102 }
103
104 /*****************************************************************************
105 * IDirectDrawPalette::Initialize
106 *
107 * Initializes the palette. As we start initialized, return
108 * DDERR_ALREADYINITIALIZED
109 *
110 * Params:
111 * DD: DirectDraw interface this palette is assigned to
112 * Flags: Some flags, as usual
113 * ColorTable: The startup color table
114 *
115 * Returns:
116 * DDERR_ALREADYINITIALIZED
117 *
118 *****************************************************************************/
119 static HRESULT WINAPI ddraw_palette_Initialize(IDirectDrawPalette *iface,
120 IDirectDraw *ddraw, DWORD flags, PALETTEENTRY *entries)
121 {
122 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
123 iface, ddraw, flags, entries);
124
125 return DDERR_ALREADYINITIALIZED;
126 }
127
128 static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
129 {
130 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
131
132 TRACE("iface %p, caps %p.\n", iface, caps);
133
134 wined3d_mutex_lock();
135 *caps = palette->flags;
136 wined3d_mutex_unlock();
137
138 return D3D_OK;
139 }
140
141 /*****************************************************************************
142 * IDirectDrawPalette::SetEntries
143 *
144 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
145 * care for updating the surface.
146 *
147 * Params:
148 * Flags: Flags, as usual
149 * Start: First palette entry to set
150 * Count: Number of entries to set
151 * PalEnt: Source entries
152 *
153 * Returns:
154 * D3D_OK on success
155 * DDERR_INVALIDPARAMS if PalEnt is NULL
156 * For details, see IWineD3DDevice::SetEntries
157 *
158 *****************************************************************************/
159 static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
160 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
161 {
162 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
163 HRESULT hr;
164
165 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
166 iface, flags, start, count, entries);
167
168 if (!entries)
169 return DDERR_INVALIDPARAMS;
170
171 wined3d_mutex_lock();
172 hr = wined3d_palette_set_entries(palette->wineD3DPalette, flags, start, count, entries);
173 wined3d_mutex_unlock();
174
175 return hr;
176 }
177
178 /*****************************************************************************
179 * IDirectDrawPalette::GetEntries
180 *
181 * Returns the entries stored in this interface.
182 *
183 * Params:
184 * Flags: Flags :)
185 * Start: First entry to return
186 * Count: The number of entries to return
187 * PalEnt: PALETTEENTRY structure to write the entries to
188 *
189 * Returns:
190 * D3D_OK on success
191 * DDERR_INVALIDPARAMS if PalEnt is NULL
192 * For details, see IWineD3DDevice::SetEntries
193 *
194 *****************************************************************************/
195 static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
196 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
197 {
198 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
199 HRESULT hr;
200
201 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
202 iface, flags, start, count, entries);
203
204 if (!entries)
205 return DDERR_INVALIDPARAMS;
206
207 wined3d_mutex_lock();
208 hr = wined3d_palette_get_entries(palette->wineD3DPalette, flags, start, count, entries);
209 wined3d_mutex_unlock();
210
211 return hr;
212 }
213
214 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
215 {
216 /*** IUnknown ***/
217 ddraw_palette_QueryInterface,
218 ddraw_palette_AddRef,
219 ddraw_palette_Release,
220 /*** IDirectDrawPalette ***/
221 ddraw_palette_GetCaps,
222 ddraw_palette_GetEntries,
223 ddraw_palette_Initialize,
224 ddraw_palette_SetEntries
225 };
226
227 struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
228 {
229 if (!iface) return NULL;
230 assert(iface->lpVtbl == &ddraw_palette_vtbl);
231 return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
232 }
233
234 static unsigned int palette_size(DWORD flags)
235 {
236 switch (flags & (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT))
237 {
238 case DDPCAPS_1BIT:
239 return 2;
240 case DDPCAPS_2BIT:
241 return 4;
242 case DDPCAPS_4BIT:
243 return 16;
244 case DDPCAPS_8BIT:
245 return 256;
246 default:
247 return ~0u;
248 }
249 }
250
251 HRESULT ddraw_palette_init(struct ddraw_palette *palette,
252 struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
253 {
254 unsigned int entry_count;
255 DWORD wined3d_flags = 0;
256 HRESULT hr;
257
258 if ((entry_count = palette_size(flags)) == ~0u)
259 {
260 WARN("Invalid flags %#x.\n", flags);
261 return DDERR_INVALIDPARAMS;
262 }
263
264 if (flags & DDPCAPS_8BITENTRIES)
265 wined3d_flags |= WINED3D_PALETTE_8BIT_ENTRIES;
266 if (flags & DDPCAPS_ALLOW256)
267 wined3d_flags |= WINED3D_PALETTE_ALLOW_256;
268 if (flags & DDPCAPS_ALPHA)
269 wined3d_flags |= WINED3D_PALETTE_ALPHA;
270
271 palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
272 palette->ref = 1;
273 palette->flags = flags;
274
275 if (FAILED(hr = wined3d_palette_create(ddraw->wined3d_device,
276 wined3d_flags, entry_count, entries, &palette->wineD3DPalette)))
277 {
278 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
279 return hr;
280 }
281
282 palette->ddraw = ddraw;
283 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
284 IUnknown_AddRef(palette->ifaceToRelease);
285
286 return DD_OK;
287 }