[WINED3D]
[reactos.git] / reactos / 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
22 #include "wined3d_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25
26 #define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
27
28 ULONG CDECL wined3d_palette_incref(struct wined3d_palette *palette)
29 {
30 ULONG refcount = InterlockedIncrement(&palette->ref);
31
32 TRACE("%p increasing refcount to %u.\n", palette, refcount);
33
34 return refcount;
35 }
36
37 ULONG CDECL wined3d_palette_decref(struct wined3d_palette *palette)
38 {
39 ULONG refcount = InterlockedDecrement(&palette->ref);
40
41 TRACE("%p decreasing refcount to %u.\n", palette, refcount);
42
43 if (!refcount)
44 {
45 DeleteObject(palette->hpal);
46 HeapFree(GetProcessHeap(), 0, palette);
47 }
48
49 return refcount;
50 }
51
52 static WORD wined3d_palette_size(DWORD flags)
53 {
54 switch (flags & SIZE_BITS)
55 {
56 case WINEDDPCAPS_1BIT: return 2;
57 case WINEDDPCAPS_2BIT: return 4;
58 case WINEDDPCAPS_4BIT: return 16;
59 case WINEDDPCAPS_8BIT: return 256;
60 default:
61 FIXME("Unhandled size bits %#x.\n", flags & SIZE_BITS);
62 return 256;
63 }
64 }
65
66 HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette,
67 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
68 {
69 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
70 palette, flags, start, count, entries);
71
72 if (flags) return WINED3DERR_INVALIDCALL; /* unchecked */
73 if (start + count > wined3d_palette_size(palette->flags))
74 return WINED3DERR_INVALIDCALL;
75
76 if (palette->flags & WINEDDPCAPS_8BITENTRIES)
77 {
78 BYTE *entry = (BYTE *)entries;
79 unsigned int i;
80
81 for (i = start; i < count + start; ++i)
82 *entry++ = palette->palents[i].peRed;
83 }
84 else
85 memcpy(entries, palette->palents + start, count * sizeof(*entries));
86
87 return WINED3D_OK;
88 }
89
90 HRESULT CDECL wined3d_palette_set_entries(struct wined3d_palette *palette,
91 DWORD flags, DWORD start, DWORD count, const PALETTEENTRY *entries)
92 {
93 struct wined3d_resource *resource;
94
95 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
96 palette, flags, start, count, entries);
97 TRACE("Palette flags: %#x.\n", palette->flags);
98
99 if (palette->flags & WINEDDPCAPS_8BITENTRIES)
100 {
101 const BYTE *entry = (const BYTE *)entries;
102 unsigned int i;
103
104 for (i = start; i < count + start; ++i)
105 palette->palents[i].peRed = *entry++;
106 }
107 else
108 {
109 memcpy(palette->palents + start, entries, count * sizeof(*palette->palents));
110
111 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
112 if (!(palette->flags & WINEDDPCAPS_ALLOW256))
113 {
114 TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
115 palette->palents[0].peRed = 0;
116 palette->palents[0].peGreen = 0;
117 palette->palents[0].peBlue = 0;
118
119 palette->palents[255].peRed = 255;
120 palette->palents[255].peGreen = 255;
121 palette->palents[255].peBlue = 255;
122 }
123
124 if (palette->hpal)
125 SetPaletteEntries(palette->hpal, start, count, palette->palents + start);
126 }
127
128 /* If the palette is attached to the render target, update all render targets */
129 LIST_FOR_EACH_ENTRY(resource, &palette->device->resources, struct wined3d_resource, resource_list_entry)
130 {
131 if (resource->type == WINED3D_RTYPE_SURFACE)
132 {
133 struct wined3d_surface *surface = surface_from_resource(resource);
134 if (surface->palette == palette)
135 surface->surface_ops->surface_realize_palette(surface);
136 }
137 }
138
139 return WINED3D_OK;
140 }
141
142 DWORD CDECL wined3d_palette_get_flags(const struct wined3d_palette *palette)
143 {
144 TRACE("palette %p.\n", palette);
145
146 return palette->flags;
147 }
148
149 void * CDECL wined3d_palette_get_parent(const struct wined3d_palette *palette)
150 {
151 TRACE("palette %p.\n", palette);
152
153 return palette->parent;
154 }
155
156 static HRESULT wined3d_palette_init(struct wined3d_palette *palette, struct wined3d_device *device,
157 DWORD flags, const PALETTEENTRY *entries, void *parent)
158 {
159 HRESULT hr;
160
161 palette->ref = 1;
162 palette->parent = parent;
163 palette->device = device;
164 palette->flags = flags;
165
166 palette->palNumEntries = wined3d_palette_size(flags);
167 palette->hpal = CreatePalette((const LOGPALETTE *)&palette->palVersion);
168 if (!palette->hpal)
169 {
170 WARN("Failed to create palette.\n");
171 return E_FAIL;
172 }
173
174 hr = wined3d_palette_set_entries(palette, 0, 0, wined3d_palette_size(flags), entries);
175 if (FAILED(hr))
176 {
177 WARN("Failed to set palette entries, hr %#x.\n", hr);
178 DeleteObject(palette->hpal);
179 return hr;
180 }
181
182 return WINED3D_OK;
183 }
184
185 HRESULT CDECL wined3d_palette_create(struct wined3d_device *device, DWORD flags,
186 const PALETTEENTRY *entries, void *parent, struct wined3d_palette **palette)
187 {
188 struct wined3d_palette *object;
189 HRESULT hr;
190
191 TRACE("device %p, flags %#x, entries %p, palette %p, parent %p.\n",
192 device, flags, entries, palette, parent);
193
194 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
195 if (!object)
196 return E_OUTOFMEMORY;
197
198 hr = wined3d_palette_init(object, device, flags, entries, parent);
199 if (FAILED(hr))
200 {
201 WARN("Failed to initialize palette, hr %#x.\n", hr);
202 HeapFree(GetProcessHeap(), 0, object);
203 return hr;
204 }
205
206 TRACE("Created palette %p.\n", object);
207 *palette = object;
208
209 return WINED3D_OK;
210 }