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