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"
28 #include "ddraw_private.h"
29 #include "dpalette/main.h"
30 #include "ddraw/main.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
34 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
36 /* For unsigned x. 0 is not a power of 2. */
37 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
39 static IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable
;
41 /******************************************************************************
44 HRESULT
Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl
* This
,
45 IDirectDrawImpl
* pDD
, DWORD dwFlags
)
47 if (!IS_POW_2(dwFlags
& SIZE_BITS
)) return DDERR_INVALIDPARAMS
;
49 if (dwFlags
& DDPCAPS_8BITENTRIES
)
50 WARN("creating palette with 8 bit entries\n");
52 This
->palNumEntries
= Main_DirectDrawPalette_Size(dwFlags
);
55 This
->local
.lpGbl
= &This
->global
;
56 This
->local
.lpDD_lcl
= &pDD
->local
;
57 This
->global
.lpDD_lcl
= &pDD
->local
;
58 This
->global
.dwProcessId
= GetCurrentProcessId();
59 This
->global
.dwFlags
= dwFlags
;
61 This
->final_release
= Main_DirectDrawPalette_final_release
;
62 ICOM_INIT_INTERFACE(This
, IDirectDrawPalette
, DDRAW_Main_Palette_VTable
);
64 /* we could defer hpal creation until we need it,
65 * but does anyone have a case where it would be useful? */
66 This
->hpal
= CreatePalette((const LOGPALETTE
*)&(This
->palVersion
));
68 Main_DirectDraw_AddPalette(pDD
, This
);
74 Main_DirectDrawPalette_Create(IDirectDrawImpl
* pDD
, DWORD dwFlags
,
75 LPDIRECTDRAWPALETTE
* ppPalette
,
78 IDirectDrawPaletteImpl
* This
;
81 if (pUnkOuter
!= NULL
)
82 return CLASS_E_NOAGGREGATION
; /* unchecked */
84 This
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*This
));
85 if (This
== NULL
) return E_OUTOFMEMORY
;
87 hr
= Main_DirectDrawPalette_Construct(This
, pDD
, dwFlags
);
89 HeapFree(GetProcessHeap(), 0, This
);
91 *ppPalette
= ICOM_INTERFACE(This
, IDirectDrawPalette
);
96 DWORD
Main_DirectDrawPalette_Size(DWORD dwFlags
)
98 switch (dwFlags
& SIZE_BITS
)
100 case DDPCAPS_1BIT
: return 2;
101 case DDPCAPS_2BIT
: return 4;
102 case DDPCAPS_4BIT
: return 16;
103 case DDPCAPS_8BIT
: return 256;
104 default: assert(0); return 256;
109 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface
, DWORD dwFlags
,
110 DWORD dwStart
, DWORD dwCount
,
111 LPPALETTEENTRY palent
)
113 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
115 TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This
,dwFlags
,dwStart
,dwCount
,
118 if (dwFlags
!= 0) return DDERR_INVALIDPARAMS
; /* unchecked */
119 if (dwStart
+ dwCount
> Main_DirectDrawPalette_Size(This
->global
.dwFlags
))
120 return DDERR_INVALIDPARAMS
;
122 if (This
->global
.dwFlags
& DDPCAPS_8BITENTRIES
)
125 LPBYTE entry
= (LPBYTE
)palent
;
127 for (i
=dwStart
; i
< dwCount
+dwStart
; i
++)
128 *entry
++ = This
->palents
[i
].peRed
;
131 memcpy(palent
, This
->palents
+dwStart
, dwCount
* sizeof(PALETTEENTRY
));
137 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface
, DWORD dwFlags
,
138 DWORD dwStart
, DWORD dwCount
,
139 LPPALETTEENTRY palent
)
141 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
143 TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This
,dwFlags
,dwStart
,dwCount
,
146 if (This
->global
.dwFlags
& DDPCAPS_8BITENTRIES
)
149 const BYTE
* entry
= (const BYTE
*)palent
;
151 for (i
=dwStart
; i
< dwCount
+dwStart
; i
++)
152 This
->palents
[i
].peRed
= *entry
++;
155 memcpy(This
->palents
+dwStart
, palent
, dwCount
* sizeof(PALETTEENTRY
));
158 SetPaletteEntries(This
->hpal
, dwStart
, dwCount
, This
->palents
+dwStart
);
160 if (This
->global
.dwFlags
& DDPCAPS_PRIMARYSURFACE
) {
161 /* update physical palette */
162 LPDIRECTDRAWSURFACE7 psurf
= NULL
;
163 IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This
->ddraw_owner
,IDirectDraw7
), &psurf
);
165 IDirectDrawSurfaceImpl
*surf
= ICOM_OBJECT(IDirectDrawSurfaceImpl
,
166 IDirectDrawSurface7
, psurf
);
167 surf
->update_palette(surf
, This
, dwStart
, dwCount
, palent
);
168 IDirectDrawSurface7_Release(psurf
);
170 else ERR("can't find GDI surface!!\n");
175 /* Now, if we are in 'depth conversion mode', update the screen palette */
176 /* FIXME: we need to update the image or we won't get palette fading. */
177 if (This
->ddraw
->d
->palette_convert
!= NULL
)
178 This
->ddraw
->d
->palette_convert(palent
,This
->screen_palents
,start
,count
);
184 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl
* This
)
186 Main_DirectDraw_RemovePalette(This
->ddraw_owner
, This
);
188 if (This
->hpal
) DeleteObject(This
->hpal
);
191 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl
* This
)
193 This
->final_release(This
);
195 if (This
->private != This
+1)
196 HeapFree(GetProcessHeap(), 0, This
->private);
198 HeapFree(GetProcessHeap(),0,This
);
201 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl
* This
)
203 WARN("deleting palette %p with refcnt %lu\n", This
, This
->ref
);
204 Main_DirectDrawPalette_Destroy(This
);
208 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface
)
210 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
211 ULONG ref
= InterlockedDecrement(&This
->ref
);
213 TRACE("(%p)->() decrementing from %lu.\n", This
, ref
+ 1);
217 Main_DirectDrawPalette_Destroy(This
);
224 ULONG WINAPI
Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface
) {
225 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
226 ULONG ref
= InterlockedIncrement(&This
->ref
);
228 TRACE("(%p)->() incrementing from %lu.\n", This
, ref
- 1);
234 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface
,
235 LPDIRECTDRAW ddraw
, DWORD dwFlags
,
236 LPPALETTEENTRY palent
)
238 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
239 TRACE("(%p)->(%p,%ld,%p)\n", This
, ddraw
, dwFlags
, palent
);
240 return DDERR_ALREADYINITIALIZED
;
244 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface
, LPDWORD lpdwCaps
)
246 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
247 TRACE("(%p)->(%p)\n",This
,lpdwCaps
);
249 *lpdwCaps
= This
->global
.dwFlags
;
255 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface
,
256 REFIID refiid
, LPVOID
*obj
)
258 IDirectDrawPaletteImpl
*This
= (IDirectDrawPaletteImpl
*)iface
;
259 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(refiid
),obj
);
261 if (IsEqualGUID(refiid
, &IID_IUnknown
)
262 || IsEqualGUID(refiid
, &IID_IDirectDrawPalette
))
265 IDirectDrawPalette_AddRef(iface
);
270 return E_NOINTERFACE
;
274 static IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable
=
276 Main_DirectDrawPalette_QueryInterface
,
277 Main_DirectDrawPalette_AddRef
,
278 Main_DirectDrawPalette_Release
,
279 Main_DirectDrawPalette_GetCaps
,
280 Main_DirectDrawPalette_GetEntries
,
281 Main_DirectDrawPalette_Initialize
,
282 Main_DirectDrawPalette_SetEntries