d3d8/tests: Use a separate device for texop_test().
[wine/multimedia.git] / dlls / wined3d / palette.c
blobe659952bca0691ecd9f91d9e5ff130133fddbdd1
1 /* DirectDraw - IDirectPalette base interface
3 * Copyright 1997-2000 Marcus Meissner
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2006 Stefan Dösinger for CodeWeavers
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
23 #include "winerror.h"
24 #include "wine/debug.h"
26 #include <string.h>
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 ULONG CDECL wined3d_palette_incref(struct wined3d_palette *palette)
34 ULONG refcount = InterlockedIncrement(&palette->ref);
36 TRACE("%p increasing refcount to %u.\n", palette, refcount);
38 return refcount;
41 ULONG CDECL wined3d_palette_decref(struct wined3d_palette *palette)
43 ULONG refcount = InterlockedDecrement(&palette->ref);
45 TRACE("%p decreasing refcount to %u.\n", palette, refcount);
47 if (!refcount)
49 DeleteObject(palette->hpal);
50 HeapFree(GetProcessHeap(), 0, palette);
53 return refcount;
56 HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette,
57 DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
59 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
60 palette, flags, start, count, entries);
62 if (flags)
63 return WINED3DERR_INVALIDCALL; /* unchecked */
64 if (start > palette->palNumEntries || count > palette->palNumEntries - start)
65 return WINED3DERR_INVALIDCALL;
67 if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES)
69 BYTE *entry = (BYTE *)entries;
70 unsigned int i;
72 for (i = start; i < count + start; ++i)
73 *entry++ = palette->palents[i].peRed;
75 else
76 memcpy(entries, palette->palents + start, count * sizeof(*entries));
78 return WINED3D_OK;
81 HRESULT CDECL wined3d_palette_set_entries(struct wined3d_palette *palette,
82 DWORD flags, DWORD start, DWORD count, const PALETTEENTRY *entries)
84 struct wined3d_resource *resource;
86 TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
87 palette, flags, start, count, entries);
88 TRACE("Palette flags: %#x.\n", palette->flags);
90 if (palette->flags & WINED3D_PALETTE_8BIT_ENTRIES)
92 const BYTE *entry = (const BYTE *)entries;
93 unsigned int i;
95 for (i = start; i < count + start; ++i)
96 palette->palents[i].peRed = *entry++;
98 else
100 memcpy(palette->palents + start, entries, count * sizeof(*palette->palents));
102 /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
103 if (!(palette->flags & WINED3D_PALETTE_ALLOW_256))
105 TRACE("WINED3D_PALETTE_ALLOW_256 not set, overriding palette entry 0 with black and 255 with white.\n");
106 palette->palents[0].peRed = 0;
107 palette->palents[0].peGreen = 0;
108 palette->palents[0].peBlue = 0;
110 palette->palents[255].peRed = 255;
111 palette->palents[255].peGreen = 255;
112 palette->palents[255].peBlue = 255;
115 if (palette->hpal)
116 SetPaletteEntries(palette->hpal, start, count, palette->palents + start);
119 /* If the palette is attached to the render target, update all render targets */
120 LIST_FOR_EACH_ENTRY(resource, &palette->device->resources, struct wined3d_resource, resource_list_entry)
122 if (resource->type == WINED3D_RTYPE_SURFACE)
124 struct wined3d_surface *surface = surface_from_resource(resource);
125 if (surface->palette == palette)
126 surface->surface_ops->surface_realize_palette(surface);
130 return WINED3D_OK;
133 static HRESULT wined3d_palette_init(struct wined3d_palette *palette, struct wined3d_device *device,
134 DWORD flags, unsigned int entry_count, const PALETTEENTRY *entries)
136 HRESULT hr;
138 palette->ref = 1;
139 palette->device = device;
140 palette->flags = flags;
142 palette->palNumEntries = entry_count;
143 palette->hpal = CreatePalette((const LOGPALETTE *)&palette->palVersion);
144 if (!palette->hpal)
146 WARN("Failed to create palette.\n");
147 return E_FAIL;
150 if (FAILED(hr = wined3d_palette_set_entries(palette, 0, 0, entry_count, entries)))
152 WARN("Failed to set palette entries, hr %#x.\n", hr);
153 DeleteObject(palette->hpal);
154 return hr;
157 return WINED3D_OK;
160 HRESULT CDECL wined3d_palette_create(struct wined3d_device *device, DWORD flags,
161 unsigned int entry_count, const PALETTEENTRY *entries, struct wined3d_palette **palette)
163 struct wined3d_palette *object;
164 HRESULT hr;
166 TRACE("device %p, flags %#x, entries %p, palette %p.\n",
167 device, flags, entries, palette);
169 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
170 if (!object)
171 return E_OUTOFMEMORY;
173 if (FAILED(hr = wined3d_palette_init(object, device, flags, entry_count, entries)))
175 WARN("Failed to initialize palette, hr %#x.\n", hr);
176 HeapFree(GetProcessHeap(), 0, object);
177 return hr;
180 TRACE("Created palette %p.\n", object);
181 *palette = object;
183 return WINED3D_OK;