1 /* DirectDraw - IDirectPalette base interface
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/debug.h"
30 #include "ddraw_private.h"
31 #include "dpalette/main.h"
32 #include "ddraw/main.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
36 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
38 /* For unsigned x. 0 is not a power of 2. */
39 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
41 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable;
43 /******************************************************************************
46 HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
47 IDirectDrawImpl* pDD, DWORD dwFlags)
49 if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;
51 if (dwFlags & DDPCAPS_8BITENTRIES)
52 WARN("creating palette with 8 bit entries\n");
54 This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
57 This->local.lpGbl = &This->global;
58 This->local.lpDD_lcl = &pDD->local;
59 This->global.lpDD_lcl = &pDD->local;
60 This->global.dwProcessId = GetCurrentProcessId();
61 This->global.dwFlags = dwFlags;
63 This->final_release = Main_DirectDrawPalette_final_release;
64 ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);
66 /* we could defer hpal creation until we need it,
67 * but does anyone have a case where it would be useful? */
68 This->hpal = CreatePalette((const LOGPALETTE*)&(This->palVersion));
70 Main_DirectDraw_AddPalette(pDD, This);
76 Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
77 LPDIRECTDRAWPALETTE* ppPalette,
80 IDirectDrawPaletteImpl* This;
83 if (pUnkOuter != NULL)
84 return CLASS_E_NOAGGREGATION; /* unchecked */
86 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
87 if (This == NULL) return E_OUTOFMEMORY;
89 hr = Main_DirectDrawPalette_Construct(This, pDD, dwFlags);
91 HeapFree(GetProcessHeap(), 0, This);
93 *ppPalette = ICOM_INTERFACE(This, IDirectDrawPalette);
98 DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
100 switch (dwFlags & SIZE_BITS)
102 case DDPCAPS_1BIT: return 2;
103 case DDPCAPS_2BIT: return 4;
104 case DDPCAPS_4BIT: return 16;
105 case DDPCAPS_8BIT: return 256;
106 default: assert(0); return 256;
111 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
112 DWORD dwStart, DWORD dwCount,
113 LPPALETTEENTRY palent)
115 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
117 TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
120 if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
121 if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
122 return DDERR_INVALIDPARAMS;
124 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
127 LPBYTE entry = (LPBYTE)palent;
129 for (i=dwStart; i < dwCount+dwStart; i++)
130 *entry++ = This->palents[i].peRed;
133 memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));
139 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
140 DWORD dwStart, DWORD dwCount,
141 LPPALETTEENTRY palent)
143 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
145 TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
148 if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
151 const BYTE* entry = (const BYTE*)palent;
153 for (i=dwStart; i < dwCount+dwStart; i++)
154 This->palents[i].peRed = *entry++;
157 memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));
160 SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);
162 if (This->global.dwFlags & DDPCAPS_PRIMARYSURFACE) {
163 /* update physical palette */
164 LPDIRECTDRAWSURFACE7 psurf = NULL;
165 IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This->ddraw_owner,IDirectDraw7), &psurf);
167 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
168 IDirectDrawSurface7, psurf);
169 surf->update_palette(surf, This, dwStart, dwCount, palent);
170 IDirectDrawSurface7_Release(psurf);
172 else ERR("can't find GDI surface!!\n");
177 /* Now, if we are in 'depth conversion mode', update the screen palette */
178 /* FIXME: we need to update the image or we won't get palette fading. */
179 if (This->ddraw->d->palette_convert != NULL)
180 This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
186 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
188 Main_DirectDraw_RemovePalette(This->ddraw_owner, This);
190 if (This->hpal) DeleteObject(This->hpal);
193 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
195 This->final_release(This);
197 if (This->private != This+1)
198 HeapFree(GetProcessHeap(), 0, This->private);
200 HeapFree(GetProcessHeap(),0,This);
203 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
205 WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
206 Main_DirectDrawPalette_Destroy(This);
210 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
212 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
213 ULONG ref = InterlockedDecrement(&This->ref);
215 TRACE("(%p)->() decrementing from %lu.\n", This, ref + 1);
219 Main_DirectDrawPalette_Destroy(This);
226 ULONG WINAPI Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface) {
227 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
228 ULONG ref = InterlockedIncrement(&This->ref);
230 TRACE("(%p)->() incrementing from %lu.\n", This, ref - 1);
236 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
237 LPDIRECTDRAW ddraw, DWORD dwFlags,
238 LPPALETTEENTRY palent)
240 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
241 TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
242 return DDERR_ALREADYINITIALIZED;
246 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
248 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
249 TRACE("(%p)->(%p)\n",This,lpdwCaps);
251 *lpdwCaps = This->global.dwFlags;
257 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
258 REFIID refiid, LPVOID *obj)
260 IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
261 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
263 if (IsEqualGUID(refiid, &IID_IUnknown)
264 || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
267 IDirectDrawPalette_AddRef(iface);
272 return E_NOINTERFACE;
276 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable =
278 Main_DirectDrawPalette_QueryInterface,
279 Main_DirectDrawPalette_AddRef,
280 Main_DirectDrawPalette_Release,
281 Main_DirectDrawPalette_GetCaps,
282 Main_DirectDrawPalette_GetEntries,
283 Main_DirectDrawPalette_Initialize,
284 Main_DirectDrawPalette_SetEntries