blob: 7d879ad3b10dacb67b99198ecd03ff76af0dae53 [file] [log] [blame]
Henri Verbeetb1a385d2010-09-08 11:24:49 +02001/*
Alexandre Julliarde1ced222008-10-18 19:19:45 +02002 * Copyright 2006 Stefan Dösinger
Stefan Dösingerc8901d62006-06-09 19:36:12 +02003 *
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"
Henri Verbeetc5a14082010-08-18 19:26:20 +020020#include "wine/port.h"
Stefan Dösingerc8901d62006-06-09 19:36:12 +020021
22#include "ddraw_private.h"
23
24WINE_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 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +010040static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
Stefan Dösingerc8901d62006-06-09 19:36:12 +020041{
Henri Verbeet20a42b42010-08-19 18:57:50 +020042 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
Stefan Dösingerc8901d62006-06-09 19:36:12 +020043
44 if (IsEqualGUID(refiid, &IID_IUnknown)
45 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
46 {
47 *obj = iface;
48 IDirectDrawPalette_AddRef(iface);
49 return S_OK;
50 }
51 else
52 {
53 *obj = NULL;
54 return E_NOINTERFACE;
55 }
56}
57
58/*****************************************************************************
59 * IDirectDrawPaletteImpl::AddRef
60 *
61 * Increases the refcount.
62 *
63 * Returns:
64 * The new refcount
65 *
66 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +010067static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
Stefan Dösingerc8901d62006-06-09 19:36:12 +020068{
Henri Verbeetd0a37cd2012-03-12 19:56:10 +010069 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
Stefan Dösingerc8901d62006-06-09 19:36:12 +020070 ULONG ref = InterlockedIncrement(&This->ref);
71
Henri Verbeet20a42b42010-08-19 18:57:50 +020072 TRACE("%p increasing refcount to %u.\n", This, ref);
Stefan Dösingerc8901d62006-06-09 19:36:12 +020073
74 return ref;
75}
76
77/*****************************************************************************
78 * IDirectDrawPaletteImpl::Release
79 *
80 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
81 *
82 * Returns:
83 * The new refcount
84 *
85 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +010086static ULONG WINAPI ddraw_palette_Release(IDirectDrawPalette *iface)
Stefan Dösingerc8901d62006-06-09 19:36:12 +020087{
Henri Verbeetd0a37cd2012-03-12 19:56:10 +010088 struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
Stefan Dösingerc8901d62006-06-09 19:36:12 +020089 ULONG ref = InterlockedDecrement(&This->ref);
90
Henri Verbeet20a42b42010-08-19 18:57:50 +020091 TRACE("%p decreasing refcount to %u.\n", This, ref);
Stefan Dösingerc8901d62006-06-09 19:36:12 +020092
93 if (ref == 0)
94 {
Henri Verbeeta2963b62011-11-13 19:52:24 +010095 wined3d_mutex_lock();
Henri Verbeet59634832011-02-02 20:22:52 +010096 wined3d_palette_decref(This->wineD3DPalette);
Stefan Dösinger915174d2006-07-10 10:53:11 +020097 if(This->ifaceToRelease)
98 {
99 IUnknown_Release(This->ifaceToRelease);
100 }
Henri Verbeeta2963b62011-11-13 19:52:24 +0100101 wined3d_mutex_unlock();
102
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200103 HeapFree(GetProcessHeap(), 0, This);
104 }
105
106 return ref;
107}
108
109/*****************************************************************************
110 * IDirectDrawPalette::Initialize
111 *
112 * Initializes the palette. As we start initialized, return
113 * DDERR_ALREADYINITIALIZED
114 *
115 * Params:
Austin English825baa22008-04-02 14:26:42 -0500116 * DD: DirectDraw interface this palette is assigned to
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200117 * Flags: Some flags, as usual
118 * ColorTable: The startup color table
119 *
120 * Returns:
121 * DDERR_ALREADYINITIALIZED
122 *
123 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100124static HRESULT WINAPI ddraw_palette_Initialize(IDirectDrawPalette *iface,
125 IDirectDraw *ddraw, DWORD flags, PALETTEENTRY *entries)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200126{
Henri Verbeet20a42b42010-08-19 18:57:50 +0200127 TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100128 iface, ddraw, flags, entries);
Henri Verbeet20a42b42010-08-19 18:57:50 +0200129
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200130 return DDERR_ALREADYINITIALIZED;
131}
132
133/*****************************************************************************
134 * IDirectDrawPalette::GetCaps
135 *
136 * Returns the palette description
137 *
138 * Params:
139 * Caps: Address to store the caps at
140 *
141 * Returns:
142 * D3D_OK on success
143 * DDERR_INVALIDPARAMS if Caps is NULL
144 * For more details, see IWineD3DPalette::GetCaps
145 *
146 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100147static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200148{
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100149 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
Henri Verbeet20a42b42010-08-19 18:57:50 +0200150
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100151 TRACE("iface %p, caps %p.\n", iface, caps);
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200152
Henri Verbeeta2963b62011-11-13 19:52:24 +0100153 wined3d_mutex_lock();
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100154 *caps = wined3d_palette_get_flags(palette->wineD3DPalette);
Henri Verbeeta2963b62011-11-13 19:52:24 +0100155 wined3d_mutex_unlock();
Henri Verbeet59634832011-02-02 20:22:52 +0100156
157 return D3D_OK;
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200158}
159
160/*****************************************************************************
161 * IDirectDrawPalette::SetEntries
162 *
163 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
164 * care for updating the surface.
165 *
166 * Params:
167 * Flags: Flags, as usual
168 * Start: First palette entry to set
169 * Count: Number of entries to set
170 * PalEnt: Source entries
171 *
172 * Returns:
173 * D3D_OK on success
174 * DDERR_INVALIDPARAMS if PalEnt is NULL
175 * For details, see IWineD3DDevice::SetEntries
176 *
177 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100178static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
179 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200180{
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100181 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
Stefan Dösingera2227de2007-05-30 16:11:25 +0200182 HRESULT hr;
Henri Verbeet20a42b42010-08-19 18:57:50 +0200183
184 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100185 iface, flags, start, count, entries);
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200186
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100187 if (!entries)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200188 return DDERR_INVALIDPARAMS;
189
Henri Verbeeta2963b62011-11-13 19:52:24 +0100190 wined3d_mutex_lock();
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100191 hr = wined3d_palette_set_entries(palette->wineD3DPalette, flags, start, count, entries);
Henri Verbeeta2963b62011-11-13 19:52:24 +0100192 wined3d_mutex_unlock();
193
Stefan Dösingera2227de2007-05-30 16:11:25 +0200194 return hr;
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200195}
196
197/*****************************************************************************
198 * IDirectDrawPalette::GetEntries
199 *
200 * Returns the entries stored in this interface.
201 *
202 * Params:
203 * Flags: Flags :)
204 * Start: First entry to return
205 * Count: The number of entries to return
206 * PalEnt: PALETTEENTRY structure to write the entries to
207 *
208 * Returns:
209 * D3D_OK on success
210 * DDERR_INVALIDPARAMS if PalEnt is NULL
211 * For details, see IWineD3DDevice::SetEntries
212 *
213 *****************************************************************************/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100214static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
215 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200216{
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100217 struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
Stefan Dösingera2227de2007-05-30 16:11:25 +0200218 HRESULT hr;
Henri Verbeet20a42b42010-08-19 18:57:50 +0200219
220 TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100221 iface, flags, start, count, entries);
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200222
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100223 if (!entries)
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200224 return DDERR_INVALIDPARAMS;
225
Henri Verbeeta2963b62011-11-13 19:52:24 +0100226 wined3d_mutex_lock();
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100227 hr = wined3d_palette_get_entries(palette->wineD3DPalette, flags, start, count, entries);
Henri Verbeeta2963b62011-11-13 19:52:24 +0100228 wined3d_mutex_unlock();
229
Stefan Dösingera2227de2007-05-30 16:11:25 +0200230 return hr;
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200231}
232
Henri Verbeete2021722010-08-19 18:57:46 +0200233static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200234{
235 /*** IUnknown ***/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100236 ddraw_palette_QueryInterface,
237 ddraw_palette_AddRef,
238 ddraw_palette_Release,
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200239 /*** IDirectDrawPalette ***/
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100240 ddraw_palette_GetCaps,
241 ddraw_palette_GetEntries,
242 ddraw_palette_Initialize,
243 ddraw_palette_SetEntries
Stefan Dösingerc8901d62006-06-09 19:36:12 +0200244};
Henri Verbeete2021722010-08-19 18:57:46 +0200245
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100246struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
Ričardas Barkauskasa8616b72011-10-03 17:14:08 -0500247{
248 if (!iface) return NULL;
249 assert(iface->lpVtbl == &ddraw_palette_vtbl);
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100250 return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
Ričardas Barkauskasa8616b72011-10-03 17:14:08 -0500251}
252
Henri Verbeetd0a37cd2012-03-12 19:56:10 +0100253HRESULT ddraw_palette_init(struct ddraw_palette *palette,
Henri Verbeet1cee1892012-03-08 20:27:16 +0100254 struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
Henri Verbeete2021722010-08-19 18:57:46 +0200255{
256 HRESULT hr;
257
Ričardas Barkauskasa6656462011-10-03 17:14:27 -0500258 palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
Henri Verbeete2021722010-08-19 18:57:46 +0200259 palette->ref = 1;
260
Henri Verbeet8d4ca1f2011-05-16 23:01:22 +0200261 hr = wined3d_palette_create(ddraw->wined3d_device, flags,
Henri Verbeet6c4c3512010-08-31 18:41:40 +0200262 entries, palette, &palette->wineD3DPalette);
Henri Verbeete2021722010-08-19 18:57:46 +0200263 if (FAILED(hr))
264 {
265 WARN("Failed to create wined3d palette, hr %#x.\n", hr);
266 return hr;
267 }
268
Michael Stefaniuc97451082011-01-19 01:42:43 +0100269 palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
Henri Verbeete2021722010-08-19 18:57:46 +0200270 IUnknown_AddRef(palette->ifaceToRelease);
271
272 return DD_OK;
273}