wined3d: Update the overlay when the destination was drawn to.
[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 <assert.h>
26 #include <string.h>
27
28 #include "wined3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31
32 #define SIZE_BITS (WINEDDPCAPS_1BIT | WINEDDPCAPS_2BIT | WINEDDPCAPS_4BIT | WINEDDPCAPS_8BIT)
33
34 static HRESULT  WINAPI IWineD3DPaletteImpl_QueryInterface(IWineD3DPalette *iface, REFIID refiid, void **obj) {
35     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
36     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
37
38     if (IsEqualGUID(refiid, &IID_IUnknown)
39         || IsEqualGUID(refiid, &IID_IWineD3DPalette)) {
40         *obj = iface;
41         IWineD3DPalette_AddRef(iface);
42         return S_OK;
43     }
44     else {
45         *obj = NULL;
46         return E_NOINTERFACE;
47     }
48 }
49
50 static ULONG  WINAPI IWineD3DPaletteImpl_AddRef(IWineD3DPalette *iface) {
51     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
52     ULONG ref = InterlockedIncrement(&This->ref);
53
54     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
55
56     return ref;
57 }
58
59 static ULONG  WINAPI IWineD3DPaletteImpl_Release(IWineD3DPalette *iface) {
60     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
61     ULONG ref = InterlockedDecrement(&This->ref);
62
63     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
64
65     if (!ref) {
66         DeleteObject(This->hpal);
67         HeapFree(GetProcessHeap(), 0, This);
68         return 0;
69     }
70
71     return ref;
72 }
73
74 /* Not called from the vtable */
75 DWORD IWineD3DPaletteImpl_Size(DWORD dwFlags) {
76     switch (dwFlags & SIZE_BITS) {
77         case WINEDDPCAPS_1BIT: return 2;
78         case WINEDDPCAPS_2BIT: return 4;
79         case WINEDDPCAPS_4BIT: return 16;
80         case WINEDDPCAPS_8BIT: return 256;
81         default: assert(0); return 256;
82     }
83 }
84
85 static HRESULT  WINAPI IWineD3DPaletteImpl_GetEntries(IWineD3DPalette *iface, DWORD Flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt) {
86     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
87
88     TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
89
90     if (Flags != 0) return WINED3DERR_INVALIDCALL; /* unchecked */
91     if (Start + Count > IWineD3DPaletteImpl_Size(This->Flags))
92         return WINED3DERR_INVALIDCALL;
93
94     if (This->Flags & WINEDDPCAPS_8BITENTRIES)
95     {
96         unsigned int i;
97         LPBYTE entry = (LPBYTE)PalEnt;
98
99         for (i=Start; i < Count+Start; i++)
100             *entry++ = This->palents[i].peRed;
101     }
102     else
103         memcpy(PalEnt, This->palents+Start, Count * sizeof(PALETTEENTRY));
104
105     return WINED3D_OK;
106 }
107
108 static HRESULT  WINAPI IWineD3DPaletteImpl_SetEntries(IWineD3DPalette *iface, DWORD Flags, DWORD Start, DWORD Count, PALETTEENTRY *PalEnt)
109 {
110     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
111     IWineD3DResourceImpl *res;
112
113     TRACE("(%p)->(%08x,%d,%d,%p)\n",This,Flags,Start,Count,PalEnt);
114     TRACE("Palette flags: %#x\n", This->Flags);
115
116     if (This->Flags & WINEDDPCAPS_8BITENTRIES) {
117         unsigned int i;
118         const BYTE* entry = (const BYTE*)PalEnt;
119
120         for (i=Start; i < Count+Start; i++)
121             This->palents[i].peRed = *entry++;
122     }
123     else {
124         memcpy(This->palents+Start, PalEnt, Count * sizeof(PALETTEENTRY));
125
126         /* When WINEDDCAPS_ALLOW256 isn't set we need to override entry 0 with black and 255 with white */
127         if(!(This->Flags & WINEDDPCAPS_ALLOW256))
128         {
129             TRACE("WINEDDPCAPS_ALLOW256 set, overriding palette entry 0 with black and 255 with white\n");
130             This->palents[0].peRed = 0;
131             This->palents[0].peGreen = 0;
132             This->palents[0].peBlue = 0;
133
134             This->palents[255].peRed = 255;
135             This->palents[255].peGreen = 255;
136             This->palents[255].peBlue = 255;
137         }
138
139         if (This->hpal)
140             SetPaletteEntries(This->hpal, Start, Count, This->palents+Start);
141     }
142
143 #if 0
144     /* Now, if we are in 'depth conversion mode', update the screen palette */
145     /* FIXME: we need to update the image or we won't get palette fading. */
146     if (This->ddraw->d->palette_convert != NULL)
147         This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
148 #endif
149
150     /* If the palette is attached to the render target, update all render targets */
151
152     LIST_FOR_EACH_ENTRY(res, &This->wineD3DDevice->resources, IWineD3DResourceImpl, resource.resource_list_entry) {
153         if(IWineD3DResource_GetType((IWineD3DResource *) res) == WINED3DRTYPE_SURFACE) {
154             IWineD3DSurfaceImpl *impl = (IWineD3DSurfaceImpl *) res;
155             if(impl->palette == This)
156                 IWineD3DSurface_RealizePalette((IWineD3DSurface *) res);
157         }
158     }
159
160     return WINED3D_OK;
161 }
162
163 static HRESULT  WINAPI IWineD3DPaletteImpl_GetCaps(IWineD3DPalette *iface, DWORD *Caps) {
164     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
165     TRACE("(%p)->(%p)\n", This, Caps);
166
167     *Caps = This->Flags;
168     return WINED3D_OK;
169 }
170
171 static HRESULT  WINAPI IWineD3DPaletteImpl_GetParent(IWineD3DPalette *iface, IUnknown **Parent) {
172     IWineD3DPaletteImpl *This = (IWineD3DPaletteImpl *)iface;
173     TRACE("(%p)->(%p)\n", This, Parent);
174
175     *Parent = This->parent;
176     IUnknown_AddRef(This->parent);
177     return WINED3D_OK;
178 }
179
180 const IWineD3DPaletteVtbl IWineD3DPalette_Vtbl =
181 {
182     /*** IUnknown ***/
183     IWineD3DPaletteImpl_QueryInterface,
184     IWineD3DPaletteImpl_AddRef,
185     IWineD3DPaletteImpl_Release,
186     /*** IWineD3DPalette ***/
187     IWineD3DPaletteImpl_GetParent,
188     IWineD3DPaletteImpl_GetEntries,
189     IWineD3DPaletteImpl_GetCaps,
190     IWineD3DPaletteImpl_SetEntries
191 };