main.c 8.01 KB
Newer Older
1 2 3
/*		DirectDraw - IDirectPalette base interface
 *
 * Copyright 1997-2000 Marcus Meissner
4
 * Copyright 2000-2001 TransGaming Technologies Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20 21 22
 */

#include "config.h"
#include "winerror.h"
23
#include "wine/debug.h"
24 25 26 27 28

#include <assert.h>
#include <string.h>

#include "ddraw_private.h"
29 30
#include "dpalette/main.h"
#include "ddraw/main.h"
31

32
WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
33

34 35 36 37 38
#define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)

/* For unsigned x. 0 is not a power of 2. */
#define IS_POW_2(x) (((x) & ((x) - 1)) == 0)

39
static IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable;
40

41 42 43
/******************************************************************************
 *			IDirectDrawPalette
 */
44 45 46 47 48 49 50 51 52 53
HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
					 IDirectDrawImpl* pDD, DWORD dwFlags)
{
    if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;

    if (dwFlags & DDPCAPS_8BITENTRIES)
	WARN("creating palette with 8 bit entries\n");

    This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
    This->ref = 1;
54 55 56 57 58

    This->local.lpGbl = &This->global;
    This->local.lpDD_lcl = &pDD->local;
    This->global.lpDD_lcl = &pDD->local;
    This->global.dwProcessId = GetCurrentProcessId();
59
    This->global.dwFlags = dwFlags;
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    This->final_release = Main_DirectDrawPalette_final_release;
    ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);

    /* we could defer hpal creation until we need it,
     * but does anyone have a case where it would be useful? */
    This->hpal = CreatePalette((const LOGPALETTE*)&(This->palVersion));

    Main_DirectDraw_AddPalette(pDD, This);

    return DD_OK;
}

HRESULT
Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
			      LPDIRECTDRAWPALETTE* ppPalette,
			      LPUNKNOWN pUnkOuter)
{
    IDirectDrawPaletteImpl* This;
    HRESULT hr;

    if (pUnkOuter != NULL)
	return CLASS_E_NOAGGREGATION; /* unchecked */

    This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
    if (This == NULL) return E_OUTOFMEMORY;

    hr = Main_DirectDrawPalette_Construct(This, pDD, dwFlags);
    if (FAILED(hr))
	HeapFree(GetProcessHeap(), 0, This);
    else
	*ppPalette = ICOM_INTERFACE(This, IDirectDrawPalette);

    return hr;
}

DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
{
    switch (dwFlags & SIZE_BITS)
    {
    case DDPCAPS_1BIT: return 2;
    case DDPCAPS_2BIT: return 4;
    case DDPCAPS_4BIT: return 16;
    case DDPCAPS_8BIT: return 256;
    default: assert(0); return 256;
    }
}

HRESULT WINAPI
Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
				  DWORD dwStart, DWORD dwCount,
				  LPPALETTEENTRY palent)
{
113
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
114

115 116
    TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
	  palent);
117

118
    if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
119
    if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
120 121
	return DDERR_INVALIDPARAMS;

122
    if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
123
    {
124
	unsigned int i;
125 126 127 128
	LPBYTE entry = (LPBYTE)palent;

	for (i=dwStart; i < dwCount+dwStart; i++)
	    *entry++ = This->palents[i].peRed;
129
    }
130 131 132
    else
	memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));

133 134 135
    return DD_OK;
}

136 137 138 139 140
HRESULT WINAPI
Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
				  DWORD dwStart, DWORD dwCount,
				  LPPALETTEENTRY palent)
{
141
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
142 143 144 145

    TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
	  palent);

146
    if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
147
    {
148
	unsigned int i;
149 150 151 152 153 154 155 156 157 158 159
	const BYTE* entry = (const BYTE*)palent;

	for (i=dwStart; i < dwCount+dwStart; i++)
	    This->palents[i].peRed = *entry++;
    }
    else {
	memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));

	if (This->hpal)
	    SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);

160
	if (This->global.dwFlags & DDPCAPS_PRIMARYSURFACE) {
161 162 163 164 165 166 167 168 169 170 171
	    /* update physical palette */
	    LPDIRECTDRAWSURFACE7 psurf = NULL;
	    IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This->ddraw_owner,IDirectDraw7), &psurf);
	    if (psurf) {
		IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
							   IDirectDrawSurface7, psurf);
		surf->update_palette(surf, This, dwStart, dwCount, palent);
		IDirectDrawSurface7_Release(psurf);
	    }
	    else ERR("can't find GDI surface!!\n");
	}
172 173
    }

174
#if 0
175 176
    /* Now, if we are in 'depth conversion mode', update the screen palette */
    /* FIXME: we need to update the image or we won't get palette fading. */
177 178
    if (This->ddraw->d->palette_convert != NULL)
	This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
179 180
#endif

181 182 183
    return DD_OK;
}

184 185 186 187 188 189 190 191 192 193 194 195 196
void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
{
    Main_DirectDraw_RemovePalette(This->ddraw_owner, This);

    if (This->hpal) DeleteObject(This->hpal);
}

static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
{
    This->final_release(This);

    if (This->private != This+1)
	HeapFree(GetProcessHeap(), 0, This->private);
197

198 199 200 201 202 203 204 205 206 207 208 209
    HeapFree(GetProcessHeap(),0,This);
}

void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
{
    WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
    Main_DirectDrawPalette_Destroy(This);
}

ULONG WINAPI
Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
{
210
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
211
    ULONG ref = InterlockedDecrement(&This->ref);
212

213 214 215
    TRACE("(%p)->() decrementing from %lu.\n", This, ref + 1);

    if (!ref)
216 217 218
    {
	Main_DirectDrawPalette_Destroy(This);
	return 0;
219
    }
220

221
    return ref;
222 223
}

224
ULONG WINAPI Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface) {
225
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
226 227 228 229 230
    ULONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p)->() incrementing from %lu.\n", This, ref - 1);

    return ref;
231 232
}

233 234 235 236 237
HRESULT WINAPI
Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
				  LPDIRECTDRAW ddraw, DWORD dwFlags,
				  LPPALETTEENTRY palent)
{
238
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
239
    TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
240 241 242
    return DDERR_ALREADYINITIALIZED;
}

243 244
HRESULT WINAPI
Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
245
{
246
   IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
247 248
   TRACE("(%p)->(%p)\n",This,lpdwCaps);

249
   *lpdwCaps = This->global.dwFlags;
250

251
   return DD_OK;
252
}
253

254 255 256
HRESULT WINAPI
Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
				      REFIID refiid, LPVOID *obj)
257
{
258
    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
259 260 261 262 263 264 265 266 267 268 269 270 271
    TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);

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

274
static IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable =
275
{
276 277 278 279 280 281 282
    Main_DirectDrawPalette_QueryInterface,
    Main_DirectDrawPalette_AddRef,
    Main_DirectDrawPalette_Release,
    Main_DirectDrawPalette_GetCaps,
    Main_DirectDrawPalette_GetEntries,
    Main_DirectDrawPalette_Initialize,
    Main_DirectDrawPalette_SetEntries
283
};