wined3d: Move PARAM C[] program.env[] into baseshader and out of vertex shaders.
[wine] / dlls / ddraw / palette_main.c
1 /*              DirectDraw - IDirectPalette base interface
2  *
3  * Copyright 1997-2000 Marcus Meissner
4  * Copyright 2000-2001 TransGaming Technologies Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
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 "ddraw_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
31
32 #define SIZE_BITS (DDPCAPS_1BIT | DDPCAPS_2BIT | DDPCAPS_4BIT | DDPCAPS_8BIT)
33
34 /* For unsigned x. 0 is not a power of 2. */
35 #define IS_POW_2(x) (((x) & ((x) - 1)) == 0)
36
37 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable;
38
39 /******************************************************************************
40  *                      IDirectDrawPalette
41  */
42 HRESULT Main_DirectDrawPalette_Construct(IDirectDrawPaletteImpl* This,
43                                          IDirectDrawImpl* pDD, DWORD dwFlags)
44 {
45     if (!IS_POW_2(dwFlags & SIZE_BITS)) return DDERR_INVALIDPARAMS;
46
47     if (dwFlags & DDPCAPS_8BITENTRIES)
48         WARN("creating palette with 8 bit entries\n");
49
50     This->palNumEntries = Main_DirectDrawPalette_Size(dwFlags);
51     This->ref = 1;
52
53     This->local.lpGbl = &This->global;
54     This->local.lpDD_lcl = &pDD->local;
55     This->global.lpDD_lcl = &pDD->local;
56     This->global.dwProcessId = GetCurrentProcessId();
57     This->global.dwFlags = dwFlags;
58
59     This->final_release = Main_DirectDrawPalette_final_release;
60     ICOM_INIT_INTERFACE(This, IDirectDrawPalette, DDRAW_Main_Palette_VTable);
61
62     /* we could defer hpal creation until we need it,
63      * but does anyone have a case where it would be useful? */
64     This->hpal = CreatePalette((const LOGPALETTE*)&(This->palVersion));
65
66     Main_DirectDraw_AddPalette(pDD, This);
67
68     return DD_OK;
69 }
70
71 HRESULT
72 Main_DirectDrawPalette_Create(IDirectDrawImpl* pDD, DWORD dwFlags,
73                               LPDIRECTDRAWPALETTE* ppPalette,
74                               LPUNKNOWN pUnkOuter)
75 {
76     IDirectDrawPaletteImpl* This;
77     HRESULT hr;
78
79     if (pUnkOuter != NULL)
80         return CLASS_E_NOAGGREGATION; /* unchecked */
81
82     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
83     if (This == NULL) return E_OUTOFMEMORY;
84
85     hr = Main_DirectDrawPalette_Construct(This, pDD, dwFlags);
86     if (FAILED(hr))
87         HeapFree(GetProcessHeap(), 0, This);
88     else
89         *ppPalette = ICOM_INTERFACE(This, IDirectDrawPalette);
90
91     return hr;
92 }
93
94 DWORD Main_DirectDrawPalette_Size(DWORD dwFlags)
95 {
96     switch (dwFlags & SIZE_BITS)
97     {
98     case DDPCAPS_1BIT: return 2;
99     case DDPCAPS_2BIT: return 4;
100     case DDPCAPS_4BIT: return 16;
101     case DDPCAPS_8BIT: return 256;
102     default: assert(0); return 256;
103     }
104 }
105
106 HRESULT WINAPI
107 Main_DirectDrawPalette_GetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
108                                   DWORD dwStart, DWORD dwCount,
109                                   LPPALETTEENTRY palent)
110 {
111     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
112
113     TRACE("(%p)->GetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
114           palent);
115
116     if (dwFlags != 0) return DDERR_INVALIDPARAMS; /* unchecked */
117     if (dwStart + dwCount > Main_DirectDrawPalette_Size(This->global.dwFlags))
118         return DDERR_INVALIDPARAMS;
119
120     if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
121     {
122         unsigned int i;
123         LPBYTE entry = (LPBYTE)palent;
124
125         for (i=dwStart; i < dwCount+dwStart; i++)
126             *entry++ = This->palents[i].peRed;
127     }
128     else
129         memcpy(palent, This->palents+dwStart, dwCount * sizeof(PALETTEENTRY));
130
131     return DD_OK;
132 }
133
134 HRESULT WINAPI
135 Main_DirectDrawPalette_SetEntries(LPDIRECTDRAWPALETTE iface, DWORD dwFlags,
136                                   DWORD dwStart, DWORD dwCount,
137                                   LPPALETTEENTRY palent)
138 {
139     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
140
141     TRACE("(%p)->SetEntries(%08lx,%ld,%ld,%p)\n",This,dwFlags,dwStart,dwCount,
142           palent);
143
144     if (This->global.dwFlags & DDPCAPS_8BITENTRIES)
145     {
146         unsigned int i;
147         const BYTE* entry = (const BYTE*)palent;
148
149         for (i=dwStart; i < dwCount+dwStart; i++)
150             This->palents[i].peRed = *entry++;
151     }
152     else {
153         memcpy(This->palents+dwStart, palent, dwCount * sizeof(PALETTEENTRY));
154
155         if (This->hpal)
156             SetPaletteEntries(This->hpal, dwStart, dwCount, This->palents+dwStart);
157
158         if (This->global.dwFlags & DDPCAPS_PRIMARYSURFACE) {
159             /* update physical palette */
160             LPDIRECTDRAWSURFACE7 psurf = NULL;
161             IDirectDraw7_GetGDISurface(ICOM_INTERFACE(This->ddraw_owner,IDirectDraw7), &psurf);
162             if (psurf) {
163                 IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl,
164                                                            IDirectDrawSurface7, psurf);
165                 surf->update_palette(surf, This, dwStart, dwCount, palent);
166                 IDirectDrawSurface7_Release(psurf);
167             }
168             else ERR("can't find GDI surface!!\n");
169         }
170     }
171
172 #if 0
173     /* Now, if we are in 'depth conversion mode', update the screen palette */
174     /* FIXME: we need to update the image or we won't get palette fading. */
175     if (This->ddraw->d->palette_convert != NULL)
176         This->ddraw->d->palette_convert(palent,This->screen_palents,start,count);
177 #endif
178
179     return DD_OK;
180 }
181
182 void Main_DirectDrawPalette_final_release(IDirectDrawPaletteImpl* This)
183 {
184     Main_DirectDraw_RemovePalette(This->ddraw_owner, This);
185
186     if (This->hpal) DeleteObject(This->hpal);
187 }
188
189 static void Main_DirectDrawPalette_Destroy(IDirectDrawPaletteImpl* This)
190 {
191     This->final_release(This);
192
193     if (This->private != This+1)
194         HeapFree(GetProcessHeap(), 0, This->private);
195
196     HeapFree(GetProcessHeap(),0,This);
197 }
198
199 void Main_DirectDrawPalette_ForceDestroy(IDirectDrawPaletteImpl* This)
200 {
201     WARN("deleting palette %p with refcnt %lu\n", This, This->ref);
202     Main_DirectDrawPalette_Destroy(This);
203 }
204
205 ULONG WINAPI
206 Main_DirectDrawPalette_Release(LPDIRECTDRAWPALETTE iface)
207 {
208     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
209     ULONG ref = InterlockedDecrement(&This->ref);
210
211     TRACE("(%p)->() decrementing from %lu.\n", This, ref + 1);
212
213     if (!ref)
214     {
215         Main_DirectDrawPalette_Destroy(This);
216         return 0;
217     }
218
219     return ref;
220 }
221
222 ULONG WINAPI Main_DirectDrawPalette_AddRef(LPDIRECTDRAWPALETTE iface) {
223     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
224     ULONG ref = InterlockedIncrement(&This->ref);
225
226     TRACE("(%p)->() incrementing from %lu.\n", This, ref - 1);
227
228     return ref;
229 }
230
231 HRESULT WINAPI
232 Main_DirectDrawPalette_Initialize(LPDIRECTDRAWPALETTE iface,
233                                   LPDIRECTDRAW ddraw, DWORD dwFlags,
234                                   LPPALETTEENTRY palent)
235 {
236     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
237     TRACE("(%p)->(%p,%ld,%p)\n", This, ddraw, dwFlags, palent);
238     return DDERR_ALREADYINITIALIZED;
239 }
240
241 HRESULT WINAPI
242 Main_DirectDrawPalette_GetCaps(LPDIRECTDRAWPALETTE iface, LPDWORD lpdwCaps)
243 {
244    IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
245    TRACE("(%p)->(%p)\n",This,lpdwCaps);
246
247    *lpdwCaps = This->global.dwFlags;
248
249    return DD_OK;
250 }
251
252 HRESULT WINAPI
253 Main_DirectDrawPalette_QueryInterface(LPDIRECTDRAWPALETTE iface,
254                                       REFIID refiid, LPVOID *obj)
255 {
256     IDirectDrawPaletteImpl *This = (IDirectDrawPaletteImpl *)iface;
257     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
258
259     if (IsEqualGUID(refiid, &IID_IUnknown)
260         || IsEqualGUID(refiid, &IID_IDirectDrawPalette))
261     {
262         *obj = iface;
263         IDirectDrawPalette_AddRef(iface);
264         return S_OK;
265     }
266     else
267     {
268         return E_NOINTERFACE;
269     }
270 }
271
272 static const IDirectDrawPaletteVtbl DDRAW_Main_Palette_VTable =
273 {
274     Main_DirectDrawPalette_QueryInterface,
275     Main_DirectDrawPalette_AddRef,
276     Main_DirectDrawPalette_Release,
277     Main_DirectDrawPalette_GetCaps,
278     Main_DirectDrawPalette_GetEntries,
279     Main_DirectDrawPalette_Initialize,
280     Main_DirectDrawPalette_SetEntries
281 };