palette.c 8.67 KB
Newer Older
1
/*
2
 * Copyright 2006 Stefan Dösinger
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "ddraw_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(ddraw);

/*****************************************************************************
 * IDirectDrawPalette::QueryInterface
 *
 * A usual QueryInterface implementation. Can only Query IUnknown and
 * IDirectDrawPalette
 *
 * Params:
 *  refiid: The interface id queried for
 *  obj: Address to return the interface pointer at
 *
 * Returns:
 *  S_OK on success
 *  E_NOINTERFACE if the requested interface wasn't found
 *****************************************************************************/
37
static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
38
{
39
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

    if (IsEqualGUID(refiid, &IID_IUnknown)
        || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
    {
        *obj = iface;
        IDirectDrawPalette_AddRef(iface);
        return S_OK;
    }
    else
    {
        *obj = NULL;
        return E_NOINTERFACE;
    }
}

/*****************************************************************************
 * IDirectDrawPaletteImpl::AddRef
 *
 * Increases the refcount.
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
64
static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
65
{
66
    struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
67 68
    ULONG ref = InterlockedIncrement(&This->ref);

69
    TRACE("%p increasing refcount to %u.\n", This, ref);
70 71 72 73 74 75 76 77 78 79 80 81 82

    return ref;
}

/*****************************************************************************
 * IDirectDrawPaletteImpl::Release
 *
 * Reduces the refcount. If the refcount falls to 0, the object is destroyed
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
83
static ULONG WINAPI ddraw_palette_Release(IDirectDrawPalette *iface)
84
{
85 86
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
    ULONG ref = InterlockedDecrement(&palette->ref);
87

88
    TRACE("%p decreasing refcount to %u.\n", palette, ref);
89 90 91

    if (ref == 0)
    {
92
        wined3d_mutex_lock();
93 94 95 96 97
        wined3d_palette_decref(palette->wined3d_palette);
        if ((palette->flags & DDPCAPS_PRIMARYSURFACE) && palette->ddraw->primary)
            palette->ddraw->primary->palette = NULL;
        if (palette->ifaceToRelease)
            IUnknown_Release(palette->ifaceToRelease);
98 99
        wined3d_mutex_unlock();

100
        heap_free(palette);
101 102 103 104 105 106 107 108 109 110 111 112
    }

    return ref;
}

/*****************************************************************************
 * IDirectDrawPalette::Initialize
 *
 * Initializes the palette. As we start initialized, return
 * DDERR_ALREADYINITIALIZED
 *
 * Params:
Austin English's avatar
Austin English committed
113
 *  DD: DirectDraw interface this palette is assigned to
114 115 116 117 118 119 120
 *  Flags: Some flags, as usual
 *  ColorTable: The startup color table
 *
 * Returns:
 *  DDERR_ALREADYINITIALIZED
 *
 *****************************************************************************/
121 122
static HRESULT WINAPI ddraw_palette_Initialize(IDirectDrawPalette *iface,
        IDirectDraw *ddraw, DWORD flags, PALETTEENTRY *entries)
123
{
124
    TRACE("iface %p, ddraw %p, flags %#x, entries %p.\n",
125
            iface, ddraw, flags, entries);
126

127 128 129
    return DDERR_ALREADYINITIALIZED;
}

130
static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
131
{
132
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
133

134
    TRACE("iface %p, caps %p.\n", iface, caps);
135

136
    wined3d_mutex_lock();
137
    *caps = palette->flags;
138
    wined3d_mutex_unlock();
139 140

    return D3D_OK;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

/*****************************************************************************
 * IDirectDrawPalette::SetEntries
 *
 * Sets the palette entries from a PALETTEENTRY structure. WineD3D takes
 * care for updating the surface.
 *
 * Params:
 *  Flags: Flags, as usual
 *  Start: First palette entry to set
 *  Count: Number of entries to set
 *  PalEnt: Source entries
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if PalEnt is NULL
 *
 *****************************************************************************/
160 161
static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
        DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
162
{
163
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
164
    HRESULT hr;
165 166

    TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
167
            iface, flags, start, count, entries);
168

169
    if (!entries)
170 171
        return DDERR_INVALIDPARAMS;

172
    wined3d_mutex_lock();
173
    hr = wined3d_palette_set_entries(palette->wined3d_palette, flags, start, count, entries);
174 175

    if (SUCCEEDED(hr) && palette->flags & DDPCAPS_PRIMARYSURFACE)
176
        ddraw_surface_update_frontbuffer(palette->ddraw->primary, NULL, FALSE, 0);
177

178 179
    wined3d_mutex_unlock();

180
    return hr;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

/*****************************************************************************
 * IDirectDrawPalette::GetEntries
 *
 * Returns the entries stored in this interface.
 *
 * Params:
 *  Flags: Flags :)
 *  Start: First entry to return
 *  Count: The number of entries to return
 *  PalEnt: PALETTEENTRY structure to write the entries to
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if PalEnt is NULL
 *
 *****************************************************************************/
199 200
static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
        DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
201
{
202
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
203
    HRESULT hr;
204 205

    TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
206
            iface, flags, start, count, entries);
207

208
    if (!entries)
209 210
        return DDERR_INVALIDPARAMS;

211
    wined3d_mutex_lock();
212
    hr = wined3d_palette_get_entries(palette->wined3d_palette, flags, start, count, entries);
213 214
    wined3d_mutex_unlock();

215
    return hr;
216 217
}

218 219
/* Some windowed mode wrappers expect this vtbl to be writable. */
static struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
220 221
{
    /*** IUnknown ***/
222 223 224
    ddraw_palette_QueryInterface,
    ddraw_palette_AddRef,
    ddraw_palette_Release,
225
    /*** IDirectDrawPalette ***/
226 227 228 229
    ddraw_palette_GetCaps,
    ddraw_palette_GetEntries,
    ddraw_palette_Initialize,
    ddraw_palette_SetEntries
230
};
231

232
struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
233 234 235
{
    if (!iface) return NULL;
    assert(iface->lpVtbl == &ddraw_palette_vtbl);
236
    return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
237 238
}

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static unsigned int palette_size(DWORD flags)
{
    switch (flags & (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT))
    {
        case DDPCAPS_1BIT:
            return 2;
        case DDPCAPS_2BIT:
            return 4;
        case DDPCAPS_4BIT:
            return 16;
        case DDPCAPS_8BIT:
            return 256;
        default:
            return ~0u;
    }
}

256
HRESULT ddraw_palette_init(struct ddraw_palette *palette,
257
        struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
258
{
259
    unsigned int entry_count;
260
    DWORD wined3d_flags = 0;
261 262
    HRESULT hr;

263 264 265 266 267 268
    if ((entry_count = palette_size(flags)) == ~0u)
    {
        WARN("Invalid flags %#x.\n", flags);
        return DDERR_INVALIDPARAMS;
    }

269 270 271 272 273 274 275
    if (flags & DDPCAPS_8BITENTRIES)
        wined3d_flags |= WINED3D_PALETTE_8BIT_ENTRIES;
    if (flags & DDPCAPS_ALLOW256)
        wined3d_flags |= WINED3D_PALETTE_ALLOW_256;
    if (flags & DDPCAPS_ALPHA)
        wined3d_flags |= WINED3D_PALETTE_ALPHA;

276
    palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
277
    palette->ref = 1;
278
    palette->flags = flags;
279

280
    if (FAILED(hr = wined3d_palette_create(ddraw->wined3d_device,
281
            wined3d_flags, entry_count, entries, &palette->wined3d_palette)))
282 283 284 285 286
    {
        WARN("Failed to create wined3d palette, hr %#x.\n", hr);
        return hr;
    }

287
    palette->ddraw = ddraw;
288
    palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
289 290 291 292
    IUnknown_AddRef(palette->ifaceToRelease);

    return DD_OK;
}