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 /*****************************************************************************
22 * IDirectDrawPalette::QueryInterface
24 * A usual QueryInterface implementation. Can only Query IUnknown and
28 * refiid: The interface id queried for
29 * obj: Address to return the interface pointer at
33 * E_NOINTERFACE if the requested interface wasn't found
34 *****************************************************************************/
35 static HRESULT WINAPI
ddraw_palette_QueryInterface(IDirectDrawPalette
*iface
, REFIID refiid
, void **obj
)
37 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(refiid
), obj
);
39 if (IsEqualGUID(refiid
, &IID_IUnknown
)
40 || IsEqualGUID(refiid
, &IID_IDirectDrawPalette
))
43 IDirectDrawPalette_AddRef(iface
);
53 /*****************************************************************************
54 * IDirectDrawPaletteImpl::AddRef
56 * Increases the refcount.
61 *****************************************************************************/
62 static ULONG WINAPI
ddraw_palette_AddRef(IDirectDrawPalette
*iface
)
64 struct ddraw_palette
*This
= impl_from_IDirectDrawPalette(iface
);
65 ULONG ref
= InterlockedIncrement(&This
->ref
);
67 TRACE("%p increasing refcount to %u.\n", This
, ref
);
72 /*****************************************************************************
73 * IDirectDrawPaletteImpl::Release
75 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
80 *****************************************************************************/
81 static ULONG WINAPI
ddraw_palette_Release(IDirectDrawPalette
*iface
)
83 struct ddraw_palette
*This
= impl_from_IDirectDrawPalette(iface
);
84 ULONG ref
= InterlockedDecrement(&This
->ref
);
86 TRACE("%p decreasing refcount to %u.\n", This
, ref
);
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();
98 HeapFree(GetProcessHeap(), 0, This
);
104 /*****************************************************************************
105 * IDirectDrawPalette::Initialize
107 * Initializes the palette. As we start initialized, return
108 * DDERR_ALREADYINITIALIZED
111 * DD: DirectDraw interface this palette is assigned to
112 * Flags: Some flags, as usual
113 * ColorTable: The startup color table
116 * DDERR_ALREADYINITIALIZED
118 *****************************************************************************/
119 static HRESULT WINAPI
ddraw_palette_Initialize(IDirectDrawPalette
*iface
,
120 IDirectDraw
*ddraw
, DWORD flags
, PALETTEENTRY
*entries
)
122 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
123 iface
, ddraw
, flags
, entries
);
125 return DDERR_ALREADYINITIALIZED
;
128 static HRESULT WINAPI
ddraw_palette_GetCaps(IDirectDrawPalette
*iface
, DWORD
*caps
)
130 struct ddraw_palette
*palette
= impl_from_IDirectDrawPalette(iface
);
132 TRACE("iface %p, caps %p.\n", iface
, caps
);
134 wined3d_mutex_lock();
135 *caps
= palette
->flags
;
136 wined3d_mutex_unlock();
141 /*****************************************************************************
142 * IDirectDrawPalette::SetEntries
144 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
145 * care for updating the surface.
148 * Flags: Flags, as usual
149 * Start: First palette entry to set
150 * Count: Number of entries to set
151 * PalEnt: Source entries
155 * DDERR_INVALIDPARAMS if PalEnt is NULL
156 * For details, see IWineD3DDevice::SetEntries
158 *****************************************************************************/
159 static HRESULT WINAPI
ddraw_palette_SetEntries(IDirectDrawPalette
*iface
,
160 DWORD flags
, DWORD start
, DWORD count
, PALETTEENTRY
*entries
)
162 struct ddraw_palette
*palette
= impl_from_IDirectDrawPalette(iface
);
165 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
166 iface
, flags
, start
, count
, entries
);
169 return DDERR_INVALIDPARAMS
;
171 wined3d_mutex_lock();
172 hr
= wined3d_palette_set_entries(palette
->wineD3DPalette
, flags
, start
, count
, entries
);
173 wined3d_mutex_unlock();
178 /*****************************************************************************
179 * IDirectDrawPalette::GetEntries
181 * Returns the entries stored in this interface.
185 * Start: First entry to return
186 * Count: The number of entries to return
187 * PalEnt: PALETTEENTRY structure to write the entries to
191 * DDERR_INVALIDPARAMS if PalEnt is NULL
192 * For details, see IWineD3DDevice::SetEntries
194 *****************************************************************************/
195 static HRESULT WINAPI
ddraw_palette_GetEntries(IDirectDrawPalette
*iface
,
196 DWORD flags
, DWORD start
, DWORD count
, PALETTEENTRY
*entries
)
198 struct ddraw_palette
*palette
= impl_from_IDirectDrawPalette(iface
);
201 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
202 iface
, flags
, start
, count
, entries
);
205 return DDERR_INVALIDPARAMS
;
207 wined3d_mutex_lock();
208 hr
= wined3d_palette_get_entries(palette
->wineD3DPalette
, flags
, start
, count
, entries
);
209 wined3d_mutex_unlock();
214 static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl
=
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
227 struct ddraw_palette
*unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette
*iface
)
229 if (!iface
) return NULL
;
230 assert(iface
->lpVtbl
== &ddraw_palette_vtbl
);
231 return CONTAINING_RECORD(iface
, struct ddraw_palette
, IDirectDrawPalette_iface
);
234 static unsigned int palette_size(DWORD flags
)
236 switch (flags
& (DDPCAPS_1BIT
| DDPCAPS_2BIT
| DDPCAPS_4BIT
| DDPCAPS_8BIT
))
251 HRESULT
ddraw_palette_init(struct ddraw_palette
*palette
,
252 struct ddraw
*ddraw
, DWORD flags
, PALETTEENTRY
*entries
)
254 unsigned int entry_count
;
255 DWORD wined3d_flags
= 0;
258 if ((entry_count
= palette_size(flags
)) == ~0u)
260 WARN("Invalid flags %#x.\n", flags
);
261 return DDERR_INVALIDPARAMS
;
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
;
271 palette
->IDirectDrawPalette_iface
.lpVtbl
= &ddraw_palette_vtbl
;
273 palette
->flags
= flags
;
275 if (FAILED(hr
= wined3d_palette_create(ddraw
->wined3d_device
,
276 wined3d_flags
, entry_count
, entries
, &palette
->wineD3DPalette
)))
278 WARN("Failed to create wined3d palette, hr %#x.\n", hr
);
282 palette
->ddraw
= ddraw
;
283 palette
->ifaceToRelease
= (IUnknown
*)&ddraw
->IDirectDraw7_iface
;
284 IUnknown_AddRef(palette
->ifaceToRelease
);