DIB Engine: Hook the engine between GDI32 and Display driver
[wine/hacks.git] / dlls / wined3d / palette.c
blob3fd8be9dd100292dc821e81333de6e5d86a312d3
1 /* DirectDraw - IDirectPalette base interface
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "winerror.h"
23 #include "wine/debug.h"
25 #include <string.h>
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31 #define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
33 static HRESULT WINAPI IWineD3DPaletteImpl_QueryInterface(IWineD3DPalette *iface, REFIID riid, void **object)
35 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
37 if (IsEqualGUID(riid, &IID_IWineD3DPalette)
38 || IsEqualGUID(riid, &IID_IWineD3DBase)
39 || IsEqualGUID(riid, &IID_IUnknown))
41 IUnknown_AddRef(iface);
42 *object = iface;
43 return S_OK;
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
48 *object = NULL;
49 return E_NOINTERFACE;
52 static ULONG WINAPI IWineD3DPaletteImpl_AddRef(IWineD3DPalette *iface) {
53 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
54 ULONG ref = InterlockedIncrement(&This->ref);
56 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
58 return ref;
61 static ULONG WINAPI IWineD3DPaletteImpl_Release(IWineD3DPalette *iface) {
62 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
63 ULONG ref = InterlockedDecrement(&This->ref);
65 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
67 if (!ref) {
68 DeleteObject(This->hpal);
69 HeapFree(GetProcessHeap(), 0, This);
70 return 0;
73 return ref;
76 /* Not called from the vtable */
77 static DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags)
79 switch (dwFlags & SIZE_BITS) {
80 case WINEDDPCAPS_1BIT: return 2;
81 case WINEDDPCAPS_2BIT: return 4;
82 case WINEDDPCAPS_4BIT: return 16;
83 case WINEDDPCAPS_8BIT: return 256;
84 default:
85 FIXME("Unhandled size bits %#x.\n", dwFlags & SIZE_BITS);
86 return 256;
90 static HRESULT WINAPI IWineD3DPaletteImpl_GetEntries(IWineD3DPalette *iface, DWORD Flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt) {
91 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
93 TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
95 if (Flags != 0) return WINED3DERR_INVALIDCALL; /* unchecked */
96 if (Start + Count > IWineD3DPaletteImpl_Size(This->Flags))
97 return WINED3DERR_INVALIDCALL;
99 if (This->Flags & WINEDDPCAPS_8BITENTRIES)
101 unsigned int i;
102 LPBYTE entry = (LPBYTE)PalEnt;
104 for (i=Start; i < Count+Start; i++)
105 *entry++ = This->palents[i].peRed;
107 else
108 memcpy(PalEnt, This->palents+Start, Count * sizeof(PALETTEENTRY));
110 return WINED3D_OK;
113 static HRESULT WINAPI IWineD3DPaletteImpl_SetEntries(IWineD3DPalette *iface,
114 DWORD Flags, DWORD Start, DWORD Count, const PALETTEENTRY *PalEnt)
116 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
117 IWineD3DResourceImpl *res;
119 TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
120 TRACE("Palette flags: %#x\n", This->Flags);
122 if (This->Flags & WINEDDPCAPS_8BITENTRIES) {
123 unsigned int i;
124 const BYTE* entry = (const BYTE*)PalEnt;
126 for (i=Start; i < Count+Start; i++)
127 This->palents[i].peRed = *entry++;
129 else {
130 memcpy(This->palents+Start, PalEnt, Count * sizeof(PALETTEENTRY));
132 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
133 if(!(This->Flags & WINEDDPCAPS_ALLOW256))
135 TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
136 This->palents[0].peRed = 0;
137 This->palents[0].peGreen = 0;
138 This->palents[0].peBlue = 0;
140 This->palents[255].peRed = 255;
141 This->palents[255].peGreen = 255;
142 This->palents[255].peBlue = 255;
145 if (This->hpal)
146 SetPaletteEntries(This->hpal, Start, Count, This->palents+Start);
149 #if 0
150 /* Now, if we are in 'depth conversion mode', update the screen palette */
151 /* FIXME: we need to update the image or we won't get palette fading. */
152 if (This->ddraw->d->palette_convert != NULL)
153 This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
154 #endif
156 /* If the palette is attached to the render target, update all render targets */
158 LIST_FOR_EACH_ENTRY(res, &This->device->resources, IWineD3DResourceImpl, resource.resource_list_entry)
160 if(IWineD3DResource_GetType((IWineD3DResource *) res) == WINED3DRTYPE_SURFACE) {
161 IWineD3DSurfaceImpl *impl = (IWineD3DSurfaceImpl *) res;
162 if(impl->palette == This)
163 IWineD3DSurface_RealizePalette((IWineD3DSurface *) res);
167 return WINED3D_OK;
170 static HRESULT WINAPI IWineD3DPaletteImpl_GetCaps(IWineD3DPalette *iface, DWORD *Caps) {
171 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
172 TRACE("(%p)->(%p)\n", This, Caps);
174 *Caps = This->Flags;
175 return WINED3D_OK;
178 static HRESULT WINAPI IWineD3DPaletteImpl_GetParent(IWineD3DPalette *iface, IUnknown **Parent) {
179 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
180 TRACE("(%p)->(%p)\n", This, Parent);
182 *Parent = This->parent;
183 IUnknown_AddRef(This->parent);
184 return WINED3D_OK;
187 static const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl =
189 /*** IUnknown ***/
190 IWineD3DPaletteImpl_QueryInterface,
191 IWineD3DPaletteImpl_AddRef,
192 IWineD3DPaletteImpl_Release,
193 /*** IWineD3DPalette ***/
194 IWineD3DPaletteImpl_GetParent,
195 IWineD3DPaletteImpl_GetEntries,
196 IWineD3DPaletteImpl_GetCaps,
197 IWineD3DPaletteImpl_SetEntries
200 HRESULT wined3d_palette_init(IWineD3DPaletteImpl *palette, IWineD3DDeviceImpl *device,
201 DWORD flags, const PALETTEENTRY *entries, IUnknown *parent)
203 HRESULT hr;
205 palette->lpVtbl = &IWineD3DPalette_Vtbl;
206 palette->ref = 1;
207 palette->parent = parent;
208 palette->device = device;
209 palette->Flags = flags;
211 palette->palNumEntries = IWineD3DPaletteImpl_Size(flags);
212 palette->hpal = CreatePalette((const LOGPALETTE *)&palette->palVersion);
213 if (!palette->hpal)
215 WARN("Failed to create palette.\n");
216 return E_FAIL;
219 hr = IWineD3DPalette_SetEntries((IWineD3DPalette *)palette, 0, 0, IWineD3DPaletteImpl_Size(flags), entries);
220 if (FAILED(hr))
222 WARN("Failed to set palette entries, hr %#x.\n", hr);
223 DeleteObject(palette->hpal);
224 return hr;
227 return WINED3D_OK;