- Merge from trunk up to r45543
[reactos.git] / dll / directx / wine / wined3d / palette.c
1 /* DirectDraw - IDirectPalette base interface
2 *
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger for CodeWeavers
6 *
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.
11 *
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.
16 *
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
20 */
21 #include "config.h"
22 #include "winerror.h"
23 #include "wine/debug.h"
24
25 #include <string.h>
26
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30
31 #define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
32
33 static HRESULT WINAPI IWineD3DPaletteImpl_QueryInterface(IWineD3DPalette *iface, REFIID riid, void **object)
34 {
35 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
36
37 if (IsEqualGUID(riid, &IID_IWineD3DPalette)
38 || IsEqualGUID(riid, &IID_IWineD3DBase)
39 || IsEqualGUID(riid, &IID_IUnknown))
40 {
41 IUnknown_AddRef(iface);
42 *object = iface;
43 return S_OK;
44 }
45
46 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
47
48 *object = NULL;
49 return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IWineD3DPaletteImpl_AddRef(IWineD3DPalette *iface) {
53 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
54 ULONG ref = InterlockedIncrement(&This->ref);
55
56 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
57
58 return ref;
59 }
60
61 static ULONG WINAPI IWineD3DPaletteImpl_Release(IWineD3DPalette *iface) {
62 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
63 ULONG ref = InterlockedDecrement(&This->ref);
64
65 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
66
67 if (!ref) {
68 DeleteObject(This->hpal);
69 HeapFree(GetProcessHeap(), 0, This);
70 return 0;
71 }
72
73 return ref;
74 }
75
76 /* Not called from the vtable */
77 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags) {
78 switch (dwFlags & SIZE_BITS) {
79 case WINEDDPCAPS_1BIT: return 2;
80 case WINEDDPCAPS_2BIT: return 4;
81 case WINEDDPCAPS_4BIT: return 16;
82 case WINEDDPCAPS_8BIT: return 256;
83 default:
84 FIXME("Unhandled size bits %#x.\n", dwFlags & SIZE_BITS);
85 return 256;
86 }
87 }
88
89 static HRESULT WINAPI IWineD3DPaletteImpl_GetEntries(IWineD3DPalette *iface, DWORD Flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt) {
90 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
91
92 TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
93
94 if (Flags != 0) return WINED3DERR_INVALIDCALL; /* unchecked */
95 if (Start + Count > IWineD3DPaletteImpl_Size(This->Flags))
96 return WINED3DERR_INVALIDCALL;
97
98 if (This->Flags & WINEDDPCAPS_8BITENTRIES)
99 {
100 unsigned int i;
101 LPBYTE entry = (LPBYTE)PalEnt;
102
103 for (i=Start; i < Count+Start; i++)
104 *entry++ = This->palents[i].peRed;
105 }
106 else
107 memcpy(PalEnt, This->palents+Start, Count * sizeof(PALETTEENTRY));
108
109 return WINED3D_OK;
110 }
111
112 static HRESULT WINAPI IWineD3DPaletteImpl_SetEntries(IWineD3DPalette *iface,
113 DWORD Flags, DWORD Start, DWORD Count, const PALETTEENTRY *PalEnt)
114 {
115 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
116 IWineD3DResourceImpl *res;
117
118 TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
119 TRACE("Palette flags: %#x\n", This->Flags);
120
121 if (This->Flags & WINEDDPCAPS_8BITENTRIES) {
122 unsigned int i;
123 const BYTE* entry = (const BYTE*)PalEnt;
124
125 for (i=Start; i < Count+Start; i++)
126 This->palents[i].peRed = *entry++;
127 }
128 else {
129 memcpy(This->palents+Start, PalEnt, Count * sizeof(PALETTEENTRY));
130
131 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
132 if(!(This->Flags & WINEDDPCAPS_ALLOW256))
133 {
134 TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
135 This->palents[0].peRed = 0;
136 This->palents[0].peGreen = 0;
137 This->palents[0].peBlue = 0;
138
139 This->palents[255].peRed = 255;
140 This->palents[255].peGreen = 255;
141 This->palents[255].peBlue = 255;
142 }
143
144 if (This->hpal)
145 SetPaletteEntries(This->hpal, Start, Count, This->palents+Start);
146 }
147
148 #if 0
149 /* Now, if we are in 'depth conversion mode', update the screen palette */
150 /* FIXME: we need to update the image or we won't get palette fading. */
151 if (This->ddraw->d->palette_convert != NULL)
152 This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
153 #endif
154
155 /* If the palette is attached to the render target, update all render targets */
156
157 LIST_FOR_EACH_ENTRY(res, &This->device->resources, IWineD3DResourceImpl, resource.resource_list_entry)
158 {
159 if(IWineD3DResource_GetType((IWineD3DResource *) res) == WINED3DRTYPE_SURFACE) {
160 IWineD3DSurfaceImpl *impl = (IWineD3DSurfaceImpl *) res;
161 if(impl->palette == This)
162 IWineD3DSurface_RealizePalette((IWineD3DSurface *) res);
163 }
164 }
165
166 return WINED3D_OK;
167 }
168
169 static HRESULT WINAPI IWineD3DPaletteImpl_GetCaps(IWineD3DPalette *iface, DWORD *Caps) {
170 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
171 TRACE("(%p)->(%p)\n", This, Caps);
172
173 *Caps = This->Flags;
174 return WINED3D_OK;
175 }
176
177 static HRESULT WINAPI IWineD3DPaletteImpl_GetParent(IWineD3DPalette *iface, IUnknown **Parent) {
178 IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
179 TRACE("(%p)->(%p)\n", This, Parent);
180
181 *Parent = This->parent;
182 IUnknown_AddRef(This->parent);
183 return WINED3D_OK;
184 }
185
186 const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl =
187 {
188 /*** IUnknown ***/
189 IWineD3DPaletteImpl_QueryInterface,
190 IWineD3DPaletteImpl_AddRef,
191 IWineD3DPaletteImpl_Release,
192 /*** IWineD3DPalette ***/
193 IWineD3DPaletteImpl_GetParent,
194 IWineD3DPaletteImpl_GetEntries,
195 IWineD3DPaletteImpl_GetCaps,
196 IWineD3DPaletteImpl_SetEntries
197 };