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