wined3d: Adding AMD HD5670, HD5570, HD5550, HD5450 detection.
[wine] / dlls / 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 static WORD IWineD3DPaletteImpl_Size(DWORD flags)
78 {
79     switch (flags & SIZE_BITS)
80     {
81         case WINEDDPCAPS_1BIT: return 2;
82         case WINEDDPCAPS_2BIT: return 4;
83         case WINEDDPCAPS_4BIT: return 16;
84         case WINEDDPCAPS_8BIT: return 256;
85         default:
86             FIXME("Unhandled size bits %#x.\n", flags & SIZE_BITS);
87             return 256;
88     }
89 }
90
91 static HRESULT WINAPI IWineD3DPaletteImpl_GetEntries(IWineD3DPalette *iface,
92         DWORD flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt)
93 {
94     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
95
96     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
97             iface, flags, Start, Count, PalEnt);
98
99     if (flags) return WINED3DERR_INVALIDCALL; /* unchecked */
100     if (Start + Count > IWineD3DPaletteImpl_Size(This->flags))
101         return WINED3DERR_INVALIDCALL;
102
103     if (This->flags & WINEDDPCAPS_8BITENTRIES)
104     {
105         unsigned int i;
106         LPBYTE entry = (LPBYTE)PalEnt;
107
108         for (i=Start; i < Count+Start; i++)
109             *entry++ = This->palents[i].peRed;
110     }
111     else
112         memcpy(PalEnt, This->palents+Start, Count * sizeof(PALETTEENTRY));
113
114     return WINED3D_OK;
115 }
116
117 static HRESULT  WINAPI IWineD3DPaletteImpl_SetEntries(IWineD3DPalette *iface,
118         DWORD flags, DWORD Start, DWORD Count, const PALETTEENTRY *PalEnt)
119 {
120     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
121     IWineD3DResourceImpl *res;
122
123     TRACE("iface %p, flags %#x, start %u, count %u, entries %p.\n",
124             iface, flags, Start, Count, PalEnt);
125     TRACE("Palette flags: %#x.\n", This->flags);
126
127     if (This->flags & WINEDDPCAPS_8BITENTRIES)
128     {
129         unsigned int i;
130         const BYTE* entry = (const BYTE*)PalEnt;
131
132         for (i=Start; i < Count+Start; i++)
133             This->palents[i].peRed = *entry++;
134     }
135     else {
136         memcpy(This->palents+Start, PalEnt, Count * sizeof(PALETTEENTRY));
137
138         /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
139         if (!(This->flags & WINEDDPCAPS_ALLOW256))
140         {
141             TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
142             This->palents[0].peRed = 0;
143             This->palents[0].peGreen = 0;
144             This->palents[0].peBlue = 0;
145
146             This->palents[255].peRed = 255;
147             This->palents[255].peGreen = 255;
148             This->palents[255].peBlue = 255;
149         }
150
151         if (This->hpal)
152             SetPaletteEntries(This->hpal, Start, Count, This->palents+Start);
153     }
154
155 #if 0
156     /* Now, if we are in 'depth conversion mode', update the screen palette */
157     /* FIXME: we need to update the image or we won't get palette fading. */
158     if (This->ddraw->d->palette_convert)
159             This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
160 #endif
161
162     /* If the palette is attached to the render target, update all render targets */
163
164     LIST_FOR_EACH_ENTRY(res, &This->device->resources, IWineD3DResourceImpl, resource.resource_list_entry)
165     {
166         if (IWineD3DResource_GetType((IWineD3DResource *)res) == WINED3DRTYPE_SURFACE)
167         {
168             IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)res;
169             if (surface->palette == This)
170                 surface->surface_ops->surface_realize_palette(surface);
171         }
172     }
173
174     return WINED3D_OK;
175 }
176
177 static HRESULT  WINAPI IWineD3DPaletteImpl_GetCaps(IWineD3DPalette *iface, DWORD *Caps) {
178     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
179     TRACE("(%p)->(%p)\n", This, Caps);
180
181     *Caps = This->flags;
182     return WINED3D_OK;
183 }
184
185 static void * WINAPI IWineD3DPaletteImpl_GetParent(IWineD3DPalette *iface)
186 {
187     TRACE("iface %p.\n", iface);
188
189     return ((IWineD3DPaletteImpl *)iface)->parent;
190 }
191
192 static const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl =
193 {
194     /*** IUnknown ***/
195     IWineD3DPaletteImpl_QueryInterface,
196     IWineD3DPaletteImpl_AddRef,
197     IWineD3DPaletteImpl_Release,
198     /*** IWineD3DPalette ***/
199     IWineD3DPaletteImpl_GetParent,
200     IWineD3DPaletteImpl_GetEntries,
201     IWineD3DPaletteImpl_GetCaps,
202     IWineD3DPaletteImpl_SetEntries
203 };
204
205 HRESULT wined3d_palette_init(IWineD3DPaletteImpl *palette, IWineD3DDeviceImpl *device,
206         DWORD flags, const PALETTEENTRY *entries, void *parent)
207 {
208     HRESULT hr;
209
210     palette->lpVtbl = &IWineD3DPalette_Vtbl;
211     palette->ref = 1;
212     palette->parent = parent;
213     palette->device = device;
214     palette->flags = flags;
215
216     palette->palNumEntries = IWineD3DPaletteImpl_Size(flags);
217     palette->hpal = CreatePalette((const LOGPALETTE *)&palette->palVersion);
218     if (!palette->hpal)
219     {
220         WARN("Failed to create palette.\n");
221         return E_FAIL;
222     }
223
224     hr = IWineD3DPalette_SetEntries((IWineD3DPalette *)palette, 0, 0, IWineD3DPaletteImpl_Size(flags), entries);
225     if (FAILED(hr))
226     {
227         WARN("Failed to set palette entries, hr %#x.\n", hr);
228         DeleteObject(palette->hpal);
229         return hr;
230     }
231
232     return WINED3D_OK;
233 }