palette.c 8.04 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
 *
 * 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 "config.h"
20
#include "wine/port.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#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
 *****************************************************************************/
40
static HRESULT WINAPI ddraw_palette_QueryInterface(IDirectDrawPalette *iface, REFIID refiid, void **obj)
41
{
42
    TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(refiid), obj);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    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
 *
 *****************************************************************************/
67
static ULONG WINAPI ddraw_palette_AddRef(IDirectDrawPalette *iface)
68
{
69
    struct ddraw_palette *This = impl_from_IDirectDrawPalette(iface);
70 71
    ULONG ref = InterlockedIncrement(&This->ref);

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

    return ref;
}

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

91
    TRACE("%p decreasing refcount to %u.\n", This, ref);
92 93 94

    if (ref == 0)
    {
95
        wined3d_mutex_lock();
96
        wined3d_palette_decref(This->wineD3DPalette);
97 98 99 100
        if(This->ifaceToRelease)
        {
            IUnknown_Release(This->ifaceToRelease);
        }
101 102
        wined3d_mutex_unlock();

103 104 105 106 107 108 109 110 111 112 113 114 115
        HeapFree(GetProcessHeap(), 0, This);
    }

    return ref;
}

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

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    return DDERR_ALREADYINITIALIZED;
}

/*****************************************************************************
 * IDirectDrawPalette::GetCaps
 *
 * Returns the palette description
 *
 * Params:
 *  Caps: Address to store the caps at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Caps is NULL
 *  For more details, see IWineD3DPalette::GetCaps
 *
 *****************************************************************************/
147
static HRESULT WINAPI ddraw_palette_GetCaps(IDirectDrawPalette *iface, DWORD *caps)
148
{
149
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
150

151
    TRACE("iface %p, caps %p.\n", iface, caps);
152

153
    wined3d_mutex_lock();
154
    *caps = wined3d_palette_get_flags(palette->wineD3DPalette);
155
    wined3d_mutex_unlock();
156 157

    return D3D_OK;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

/*****************************************************************************
 * 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
 *  For details, see IWineD3DDevice::SetEntries
 *
 *****************************************************************************/
178 179
static HRESULT WINAPI ddraw_palette_SetEntries(IDirectDrawPalette *iface,
        DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
180
{
181
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
182
    HRESULT hr;
183 184

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

187
    if (!entries)
188 189
        return DDERR_INVALIDPARAMS;

190
    wined3d_mutex_lock();
191
    hr = wined3d_palette_set_entries(palette->wineD3DPalette, flags, start, count, entries);
192 193
    wined3d_mutex_unlock();

194
    return hr;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

/*****************************************************************************
 * 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
 *  For details, see IWineD3DDevice::SetEntries
 *
 *****************************************************************************/
214 215
static HRESULT WINAPI ddraw_palette_GetEntries(IDirectDrawPalette *iface,
        DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
216
{
217
    struct ddraw_palette *palette = impl_from_IDirectDrawPalette(iface);
218
    HRESULT hr;
219 220

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

223
    if (!entries)
224 225
        return DDERR_INVALIDPARAMS;

226
    wined3d_mutex_lock();
227
    hr = wined3d_palette_get_entries(palette->wineD3DPalette, flags, start, count, entries);
228 229
    wined3d_mutex_unlock();

230
    return hr;
231 232
}

233
static const struct IDirectDrawPaletteVtbl ddraw_palette_vtbl =
234 235
{
    /*** IUnknown ***/
236 237 238
    ddraw_palette_QueryInterface,
    ddraw_palette_AddRef,
    ddraw_palette_Release,
239
    /*** IDirectDrawPalette ***/
240 241 242 243
    ddraw_palette_GetCaps,
    ddraw_palette_GetEntries,
    ddraw_palette_Initialize,
    ddraw_palette_SetEntries
244
};
245

246
struct ddraw_palette *unsafe_impl_from_IDirectDrawPalette(IDirectDrawPalette *iface)
247 248 249
{
    if (!iface) return NULL;
    assert(iface->lpVtbl == &ddraw_palette_vtbl);
250
    return CONTAINING_RECORD(iface, struct ddraw_palette, IDirectDrawPalette_iface);
251 252
}

253
HRESULT ddraw_palette_init(struct ddraw_palette *palette,
254
        struct ddraw *ddraw, DWORD flags, PALETTEENTRY *entries)
255 256 257
{
    HRESULT hr;

258
    palette->IDirectDrawPalette_iface.lpVtbl = &ddraw_palette_vtbl;
259 260
    palette->ref = 1;

261
    hr = wined3d_palette_create(ddraw->wined3d_device, flags,
262
            entries, palette, &palette->wineD3DPalette);
263 264 265 266 267 268
    if (FAILED(hr))
    {
        WARN("Failed to create wined3d palette, hr %#x.\n", hr);
        return hr;
    }

269
    palette->ifaceToRelease = (IUnknown *)&ddraw->IDirectDraw7_iface;
270 271 272 273
    IUnknown_AddRef(palette->ifaceToRelease);

    return DD_OK;
}