2009-01-09 10:23:43 +01:00
|
|
|
/* DirectDraw - IDirectPalette base interface
|
2006-04-17 17:04:59 +02:00
|
|
|
*
|
|
|
|
* Copyright 1997-2000 Marcus Meissner
|
|
|
|
* Copyright 2000-2001 TransGaming Technologies Inc.
|
2008-10-18 19:21:20 +02:00
|
|
|
* Copyright 2006 Stefan Dösinger for CodeWeavers
|
2006-04-17 17:04:59 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-04-17 17:04:59 +02:00
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "wined3d_private.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(d3d);
|
|
|
|
|
2007-04-14 22:44:55 +02:00
|
|
|
#define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
ULONG CDECL wined3d_palette_incref(struct wined3d_palette *palette)
|
2009-12-20 20:41:39 +01:00
|
|
|
{
|
2011-02-02 20:22:52 +01:00
|
|
|
ULONG refcount = InterlockedIncrement(&palette->ref);
|
2006-04-17 17:04:59 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
TRACE("%p increasing refcount to %u.\n", palette, refcount);
|
2006-04-17 17:04:59 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
return refcount;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
ULONG CDECL wined3d_palette_decref(struct wined3d_palette *palette)
|
|
|
|
{
|
|
|
|
ULONG refcount = InterlockedDecrement(&palette->ref);
|
2006-04-17 17:04:59 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
TRACE("%p decreasing refcount to %u.\n", palette, refcount);
|
2006-04-17 17:04:59 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
if (!refcount)
|
|
|
|
{
|
|
|
|
DeleteObject(palette->hpal);
|
|
|
|
HeapFree(GetProcessHeap(), 0, palette);
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
return refcount;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
static WORD wined3d_palette_size(DWORD flags)
|
2010-04-13 20:46:25 +02:00
|
|
|
{
|
2010-11-18 20:50:40 +01:00
|
|
|
switch (flags & SIZE_BITS)
|
|
|
|
{
|
2007-04-14 22:44:55 +02:00
|
|
|
case WINEDDPCAPS_1BIT: return 2;
|
|
|
|
case WINEDDPCAPS_2BIT: return 4;
|
|
|
|
case WINEDDPCAPS_4BIT: return 16;
|
|
|
|
case WINEDDPCAPS_8BIT: return 256;
|
2009-06-22 10:16:00 +02:00
|
|
|
default:
|
2010-11-18 20:50:40 +01:00
|
|
|
FIXME("Unhandled size bits %#x.\n", flags & SIZE_BITS);
|
2009-06-22 10:16:00 +02:00
|
|
|
return 256;
|
2006-04-21 00:00:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
HRESULT CDECL wined3d_palette_get_entries(const struct wined3d_palette *palette,
|
|
|
|
DWORD flags, DWORD start, DWORD count, PALETTEENTRY *entries)
|
2010-11-18 20:50:40 +01:00
|
|
|
{
|
2011-02-02 20:22:52 +01:00
|
|
|
TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
|
|
|
|
palette, flags, start, count, entries);
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2010-11-18 20:50:40 +01:00
|
|
|
if (flags) return WINED3DERR_INVALIDCALL; /* unchecked */
|
2011-02-02 20:22:52 +01:00
|
|
|
if (start + count > wined3d_palette_size(palette->flags))
|
2006-04-21 00:00:30 +02:00
|
|
|
return WINED3DERR_INVALIDCALL;
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
if (palette->flags & WINEDDPCAPS_8BITENTRIES)
|
2006-04-21 00:00:30 +02:00
|
|
|
{
|
2011-02-02 20:22:52 +01:00
|
|
|
BYTE *entry = (BYTE *)entries;
|
2006-04-21 00:00:30 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
for (i = start; i < count + start; ++i)
|
|
|
|
*entry++ = palette->palents[i].peRed;
|
2006-04-21 00:00:30 +02:00
|
|
|
}
|
|
|
|
else
|
2011-02-02 20:22:52 +01:00
|
|
|
memcpy(entries, palette->palents + start, count * sizeof(*entries));
|
2006-04-21 00:00:30 +02:00
|
|
|
|
|
|
|
return WINED3D_OK;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
HRESULT CDECL wined3d_palette_set_entries(struct wined3d_palette *palette,
|
|
|
|
DWORD flags, DWORD start, DWORD count, const PALETTEENTRY *entries)
|
2006-04-21 00:00:30 +02:00
|
|
|
{
|
2011-03-01 09:47:56 +01:00
|
|
|
struct wined3d_resource *resource;
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
TRACE("palette %p, flags %#x, start %u, count %u, entries %p.\n",
|
|
|
|
palette, flags, start, count, entries);
|
|
|
|
TRACE("Palette flags: %#x.\n", palette->flags);
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
if (palette->flags & WINEDDPCAPS_8BITENTRIES)
|
2010-11-15 13:43:33 +01:00
|
|
|
{
|
2011-02-02 20:22:52 +01:00
|
|
|
const BYTE *entry = (const BYTE *)entries;
|
2006-04-21 00:00:30 +02:00
|
|
|
unsigned int i;
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
for (i = start; i < count + start; ++i)
|
|
|
|
palette->palents[i].peRed = *entry++;
|
2006-04-21 00:00:30 +02:00
|
|
|
}
|
2011-02-02 20:22:52 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(palette->palents + start, entries, count * sizeof(*palette->palents));
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2008-02-17 18:02:17 +01:00
|
|
|
/* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
|
2011-02-02 20:22:52 +01:00
|
|
|
if (!(palette->flags & WINEDDPCAPS_ALLOW256))
|
2008-02-17 18:02:17 +01:00
|
|
|
{
|
|
|
|
TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
|
2011-02-02 20:22:52 +01:00
|
|
|
palette->palents[0].peRed = 0;
|
|
|
|
palette->palents[0].peGreen = 0;
|
|
|
|
palette->palents[0].peBlue = 0;
|
2008-02-17 18:02:17 +01:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
palette->palents[255].peRed = 255;
|
|
|
|
palette->palents[255].peGreen = 255;
|
|
|
|
palette->palents[255].peBlue = 255;
|
2008-02-17 18:02:17 +01:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
if (palette->hpal)
|
|
|
|
SetPaletteEntries(palette->hpal, start, count, palette->palents + start);
|
2006-04-21 00:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the palette is attached to the render target, update all render targets */
|
2011-03-01 09:47:56 +01:00
|
|
|
LIST_FOR_EACH_ENTRY(resource, &palette->device->resources, struct wined3d_resource, resource_list_entry)
|
2009-12-09 20:32:08 +01:00
|
|
|
{
|
2012-01-17 21:13:36 +01:00
|
|
|
if (resource->type == WINED3D_RTYPE_SURFACE)
|
2011-01-24 11:19:37 +01:00
|
|
|
{
|
2011-04-29 13:03:42 +02:00
|
|
|
struct wined3d_surface *surface = surface_from_resource(resource);
|
2011-02-02 20:22:52 +01:00
|
|
|
if (surface->palette == palette)
|
2011-01-24 11:19:37 +01:00
|
|
|
surface->surface_ops->surface_realize_palette(surface);
|
2006-04-21 00:00:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WINED3D_OK;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
DWORD CDECL wined3d_palette_get_flags(const struct wined3d_palette *palette)
|
|
|
|
{
|
|
|
|
TRACE("palette %p.\n", palette);
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
return palette->flags;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
void * CDECL wined3d_palette_get_parent(const struct wined3d_palette *palette)
|
2010-08-31 18:41:40 +02:00
|
|
|
{
|
2011-02-02 20:22:52 +01:00
|
|
|
TRACE("palette %p.\n", palette);
|
2006-04-21 00:00:30 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
return palette->parent;
|
2006-04-17 17:04:59 +02:00
|
|
|
}
|
|
|
|
|
2011-05-16 23:01:23 +02:00
|
|
|
static HRESULT wined3d_palette_init(struct wined3d_palette *palette, struct wined3d_device *device,
|
2010-08-31 18:41:40 +02:00
|
|
|
DWORD flags, const PALETTEENTRY *entries, void *parent)
|
2010-04-13 20:46:25 +02:00
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
palette->ref = 1;
|
|
|
|
palette->parent = parent;
|
|
|
|
palette->device = device;
|
2010-11-15 13:43:33 +01:00
|
|
|
palette->flags = flags;
|
2010-04-13 20:46:25 +02:00
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
palette->palNumEntries = wined3d_palette_size(flags);
|
2010-04-13 20:46:25 +02:00
|
|
|
palette->hpal = CreatePalette((const LOGPALETTE *)&palette->palVersion);
|
|
|
|
if (!palette->hpal)
|
|
|
|
{
|
|
|
|
WARN("Failed to create palette.\n");
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2011-02-02 20:22:52 +01:00
|
|
|
hr = wined3d_palette_set_entries(palette, 0, 0, wined3d_palette_size(flags), entries);
|
2010-04-13 20:46:25 +02:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
WARN("Failed to set palette entries, hr %#x.\n", hr);
|
|
|
|
DeleteObject(palette->hpal);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WINED3D_OK;
|
|
|
|
}
|
2011-05-10 21:18:47 +02:00
|
|
|
|
2011-05-16 23:01:23 +02:00
|
|
|
HRESULT CDECL wined3d_palette_create(struct wined3d_device *device, DWORD flags,
|
2011-05-10 21:18:47 +02:00
|
|
|
const PALETTEENTRY *entries, void *parent, struct wined3d_palette **palette)
|
|
|
|
{
|
|
|
|
struct wined3d_palette *object;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
TRACE("device %p, flags %#x, entries %p, palette %p, parent %p.\n",
|
|
|
|
device, flags, entries, palette, parent);
|
|
|
|
|
|
|
|
object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
|
|
|
|
if (!object)
|
|
|
|
{
|
|
|
|
ERR("Failed to allocate palette memory.\n");
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = wined3d_palette_init(object, device, flags, entries, parent);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
WARN("Failed to initialize palette, hr %#x.\n", hr);
|
|
|
|
HeapFree(GetProcessHeap(), 0, object);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("Created palette %p.\n", object);
|
|
|
|
*palette = object;
|
|
|
|
|
|
|
|
return WINED3D_OK;
|
|
|
|
}
|