api-ms-win-service-management-l1-1-0: Add stub dll.
[wine/multimedia.git] / dlls / ddraw / palette.c
blobd04f42606e0c1216cf57e5ad65dcbf75a5382876
1 /*
2 * Copyright 2006 Stefan Dösinger
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include "ddraw_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
26 /*****************************************************************************
27 * IDirectDrawPalette::QueryInterface
29 * A usual QueryInterface implementation. Can only Query IUnknown and
30 * IDirectDrawPalette
32 * Params:
33 * refiid: The interface id queried for
34 * obj: Address to return the interface pointer at
36 * Returns:
37 * S_OK on success
38 * E_NOINTERFACE if the requested interface wasn't found
39 *****************************************************************************/
40 static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
42 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
44 if (IsEqualGUID(refiid, &IID_IUnknown)
45 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
47 *obj = iface;
48 IDirectDrawPalette_AddRef(iface);
49 return S_OK;
51 else
53 *obj = NULL;
54 return E_NOINTERFACE;
58 /*****************************************************************************
59 * IDirectDrawPaletteImpl::AddRef
61 * Increases the refcount.
63 * Returns:
64 * The new refcount
66 *****************************************************************************/
67 static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
69 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
70 ULONG ref = InterlockedIncrement(&This->ref);
72 TRACE("%p increasing refcount to %u.\n", This, ref);
74 return ref;
77 /*****************************************************************************
78 * IDirectDrawPaletteImpl::Release
80 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
82 * Returns:
83 * The new refcount
85 *****************************************************************************/
86 static ULONG WINAPI ddraw_palette_Release(IDirectDrawPalette *iface)
88 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
89 ULONG ref = InterlockedDecrement(&This->ref);
91 TRACE("%p decreasing refcount to %u.\n", This, ref);
93 if (ref == 0)
95 wined3d_mutex_lock();
96 wined3d_palette_decref(This->wineD3DPalette);
97 if ((This->flags & DDPCAPS_PRIMARYSURFACE) && This->ddraw->primary)
98 This->ddraw->primary->palette = NULL;
99 if (This->ifaceToRelease)
100 IUnknown_Release(This->ifaceToRelease);
101 wined3d_mutex_unlock();
103 HeapFree(GetProcessHeap(), 0, This);
106 return ref;
109 /*****************************************************************************
110 * IDirectDrawPalette::Initialize
112 * Initializes the palette. As we start initialized, return
113 * DDERR_ALREADYINITIALIZED
115 * Params:
116 * DD: DirectDraw interface this palette is assigned to
117 * Flags: Some flags, as usual
118 * ColorTable: The startup color table
120 * Returns:
121 * DDERR_ALREADYINITIALIZED
123 *****************************************************************************/
124 static HRESULT WINAPI ddraw_palette_Initialize(IDirectDrawPalette *iface,
125 IDirectDraw *ddraw, DWORD flags, PALETTEENTRY *entries)
127 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
128 iface, ddraw, flags, entries);
130 return DDERR_ALREADYINITIALIZED;
133 static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
135 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
137 TRACE("iface %p, caps %p.\n", iface, caps);
139 wined3d_mutex_lock();
140 *caps = palette->flags;
141 wined3d_mutex_unlock();
143 return D3D_OK;
146 /*****************************************************************************
147 * IDirectDrawPalette::SetEntries
149 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
150 * care for updating the surface.
152 * Params:
153 * Flags: Flags, as usual
154 * Start: First palette entry to set
155 * Count: Number of entries to set
156 * PalEnt: Source entries
158 * Returns:
159 * D3D_OK on success
160 * DDERR_INVALIDPARAMS if PalEnt is NULL
161 * For details, see IWineD3DDevice::SetEntries
163 *****************************************************************************/
164 static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
165 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
167 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
168 HRESULT hr;
170 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
171 iface, flags, start, count, entries);
173 if (!entries)
174 return DDERR_INVALIDPARAMS;
176 wined3d_mutex_lock();
177 hr = wined3d_palette_set_entries(palette->wineD3DPalette, flags, start, count, entries);
178 wined3d_mutex_unlock();
180 return hr;
183 /*****************************************************************************
184 * IDirectDrawPalette::GetEntries
186 * Returns the entries stored in this interface.
188 * Params:
189 * Flags: Flags :)
190 * Start: First entry to return
191 * Count: The number of entries to return
192 * PalEnt: PALETTEENTRY structure to write the entries to
194 * Returns:
195 * D3D_OK on success
196 * DDERR_INVALIDPARAMS if PalEnt is NULL
197 * For details, see IWineD3DDevice::SetEntries
199 *****************************************************************************/
200 static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
201 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
203 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
204 HRESULT hr;
206 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
207 iface, flags, start, count, entries);
209 if (!entries)
210 return DDERR_INVALIDPARAMS;
212 wined3d_mutex_lock();
213 hr = wined3d_palette_get_entries(palette->wineD3DPalette, flags, start, count, entries);
214 wined3d_mutex_unlock();
216 return hr;
219 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
221 /*** IUnknown ***/
222 ddraw_palette_QueryInterface,
223 ddraw_palette_AddRef,
224 ddraw_palette_Release,
225 /*** IDirectDrawPalette ***/
226 ddraw_palette_GetCaps,
227 ddraw_palette_GetEntries,
228 ddraw_palette_Initialize,
229 ddraw_palette_SetEntries
232 struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
234 if (!iface) return NULL;
235 assert(iface->lpVtbl == &ddraw_palette_vtbl);
236 return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
239 static unsigned int palette_size(DWORD flags)
241 switch (flags & (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT))
243 case DDPCAPS_1BIT:
244 return 2;
245 case DDPCAPS_2BIT:
246 return 4;
247 case DDPCAPS_4BIT:
248 return 16;
249 case DDPCAPS_8BIT:
250 return 256;
251 default:
252 return ~0u;
256 HRESULT ddraw_palette_init(struct ddraw_palette *palette,
257 struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
259 unsigned int entry_count;
260 DWORD wined3d_flags = 0;
261 HRESULT hr;
263 if ((entry_count = palette_size(flags)) == ~0u)
265 WARN("Invalid flags %#x.\n", flags);
266 return DDERR_INVALIDPARAMS;
269 if (flags & DDPCAPS_8BITENTRIES)
270 wined3d_flags |= WINED3D_PALETTE_8BIT_ENTRIES;
271 if (flags & DDPCAPS_ALLOW256)
272 wined3d_flags |= WINED3D_PALETTE_ALLOW_256;
273 if (flags & DDPCAPS_ALPHA)
274 wined3d_flags |= WINED3D_PALETTE_ALPHA;
276 palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
277 palette->ref = 1;
278 palette->flags = flags;
280 if (FAILED(hr = wined3d_palette_create(ddraw->wined3d_device,
281 wined3d_flags, entry_count, entries, &palette->wineD3DPalette)))
283 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
284 return hr;
287 palette->ddraw = ddraw;
288 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
289 IUnknown_AddRef(palette->ifaceToRelease);
291 return DD_OK;