winex11.drv: Ignore .dwAspect in FORMATETC during XDnD.
[wine.git] / dlls / ddraw / palette.c
blob980c62b60b446fddb8bca838b34e453a5e9d2b79
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 "ddraw_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
23 /*****************************************************************************
24 * IDirectDrawPalette::QueryInterface
26 * A usual QueryInterface implementation. Can only Query IUnknown and
27 * IDirectDrawPalette
29 * Params:
30 * refiid: The interface id queried for
31 * obj: Address to return the interface pointer at
33 * Returns:
34 * S_OK on success
35 * E_NOINTERFACE if the requested interface wasn't found
36 *****************************************************************************/
37 static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
39 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
41 if (IsEqualGUID(refiid, &IID_IUnknown)
42 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
44 *obj = iface;
45 IDirectDrawPalette_AddRef(iface);
46 return S_OK;
48 else
50 *obj = NULL;
51 return E_NOINTERFACE;
55 /*****************************************************************************
56 * IDirectDrawPaletteImpl::AddRef
58 * Increases the refcount.
60 * Returns:
61 * The new refcount
63 *****************************************************************************/
64 static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
66 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
67 ULONG ref = InterlockedIncrement(&This->ref);
69 TRACE("%p increasing refcount to %u.\n", This, ref);
71 return ref;
74 /*****************************************************************************
75 * IDirectDrawPaletteImpl::Release
77 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
79 * Returns:
80 * The new refcount
82 *****************************************************************************/
83 static ULONG WINAPI ddraw_palette_Release(IDirectDrawPalette *iface)
85 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
86 ULONG ref = InterlockedDecrement(&palette->ref);
88 TRACE("%p decreasing refcount to %u.\n", palette, ref);
90 if (ref == 0)
92 wined3d_mutex_lock();
93 wined3d_palette_decref(palette->wined3d_palette);
94 if ((palette->flags & DDPCAPS_PRIMARYSURFACE) && palette->ddraw->primary)
95 palette->ddraw->primary->palette = NULL;
96 if (palette->ifaceToRelease)
97 IUnknown_Release(palette->ifaceToRelease);
98 wined3d_mutex_unlock();
100 heap_free(palette);
103 return ref;
106 /*****************************************************************************
107 * IDirectDrawPalette::Initialize
109 * Initializes the palette. As we start initialized, return
110 * DDERR_ALREADYINITIALIZED
112 * Params:
113 * DD: DirectDraw interface this palette is assigned to
114 * Flags: Some flags, as usual
115 * ColorTable: The startup color table
117 * Returns:
118 * DDERR_ALREADYINITIALIZED
120 *****************************************************************************/
121 static HRESULT WINAPI ddraw_palette_Initialize(IDirectDrawPalette *iface,
122 IDirectDraw *ddraw, DWORD flags, PALETTEENTRY *entries)
124 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
125 iface, ddraw, flags, entries);
127 return DDERR_ALREADYINITIALIZED;
130 static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
132 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
134 TRACE("iface %p, caps %p.\n", iface, caps);
136 wined3d_mutex_lock();
137 *caps = palette->flags;
138 wined3d_mutex_unlock();
140 return D3D_OK;
143 /*****************************************************************************
144 * IDirectDrawPalette::SetEntries
146 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
147 * care for updating the surface.
149 * Params:
150 * Flags: Flags, as usual
151 * Start: First palette entry to set
152 * Count: Number of entries to set
153 * PalEnt: Source entries
155 * Returns:
156 * D3D_OK on success
157 * DDERR_INVALIDPARAMS if PalEnt is NULL
159 *****************************************************************************/
160 static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
161 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
163 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
164 HRESULT hr;
166 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
167 iface, flags, start, count, entries);
169 if (!entries)
170 return DDERR_INVALIDPARAMS;
172 wined3d_mutex_lock();
173 hr = wined3d_palette_set_entries(palette->wined3d_palette, flags, start, count, entries);
175 if (SUCCEEDED(hr) && palette->flags & DDPCAPS_PRIMARYSURFACE)
176 ddraw_surface_update_frontbuffer(palette->ddraw->primary, NULL, FALSE, 0);
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
198 *****************************************************************************/
199 static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
200 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
202 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
203 HRESULT hr;
205 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
206 iface, flags, start, count, entries);
208 if (!entries)
209 return DDERR_INVALIDPARAMS;
211 wined3d_mutex_lock();
212 hr = wined3d_palette_get_entries(palette->wined3d_palette, flags, start, count, entries);
213 wined3d_mutex_unlock();
215 return hr;
218 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
220 /*** IUnknown ***/
221 ddraw_palette_QueryInterface,
222 ddraw_palette_AddRef,
223 ddraw_palette_Release,
224 /*** IDirectDrawPalette ***/
225 ddraw_palette_GetCaps,
226 ddraw_palette_GetEntries,
227 ddraw_palette_Initialize,
228 ddraw_palette_SetEntries
231 struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
233 if (!iface) return NULL;
234 assert(iface->lpVtbl == &ddraw_palette_vtbl);
235 return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
238 static unsigned int palette_size(DWORD flags)
240 switch (flags & (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT))
242 case DDPCAPS_1BIT:
243 return 2;
244 case DDPCAPS_2BIT:
245 return 4;
246 case DDPCAPS_4BIT:
247 return 16;
248 case DDPCAPS_8BIT:
249 return 256;
250 default:
251 return ~0u;
255 HRESULT ddraw_palette_init(struct ddraw_palette *palette,
256 struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
258 unsigned int entry_count;
259 DWORD wined3d_flags = 0;
260 HRESULT hr;
262 if ((entry_count = palette_size(flags)) == ~0u)
264 WARN("Invalid flags %#x.\n", flags);
265 return DDERR_INVALIDPARAMS;
268 if (flags & DDPCAPS_8BITENTRIES)
269 wined3d_flags |= WINED3D_PALETTE_8BIT_ENTRIES;
270 if (flags & DDPCAPS_ALLOW256)
271 wined3d_flags |= WINED3D_PALETTE_ALLOW_256;
272 if (flags & DDPCAPS_ALPHA)
273 wined3d_flags |= WINED3D_PALETTE_ALPHA;
275 palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
276 palette->ref = 1;
277 palette->flags = flags;
279 if (FAILED(hr = wined3d_palette_create(ddraw->wined3d_device,
280 wined3d_flags, entry_count, entries, &palette->wined3d_palette)))
282 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
283 return hr;
286 palette->ddraw = ddraw;
287 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
288 IUnknown_AddRef(palette->ifaceToRelease);
290 return DD_OK;