gdi32: Fix dibdrv_PutImage for the selected bitmap case.
[wine] / dlls / gdi32 / dibdrv / dc.c
1 /*
2  * DIB driver initialization and DC functions.
3  *
4  * Copyright 2011 Huw Davies
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22
23 #include "gdi_private.h"
24 #include "dibdrv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
29
30 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
31 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
32
33 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
34 {
35     int s, l;
36
37     if(!mask)
38     {
39         *shift = *len = 0;
40         return;
41     }
42
43     s = 0;
44     while ((mask & 1) == 0)
45     {
46         mask >>= 1;
47         s++;
48     }
49     l = 0;
50     while ((mask & 1) == 1)
51     {
52         mask >>= 1;
53         l++;
54     }
55     *shift = s;
56     *len = l;
57 }
58
59 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
60 {
61     dib->red_mask    = bit_fields[0];
62     dib->green_mask  = bit_fields[1];
63     dib->blue_mask   = bit_fields[2];
64     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
65     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
66     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
67 }
68
69 static BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
70                           RGBQUAD *color_table, int color_table_size, void *bits, enum dib_info_flags flags)
71 {
72     dib->bit_count = bi->biBitCount;
73     dib->width     = bi->biWidth;
74     dib->height    = bi->biHeight;
75     dib->stride    = get_dib_stride( dib->width, dib->bit_count );
76     dib->bits      = bits;
77     dib->ptr_to_free = NULL;
78     dib->flags     = flags;
79
80     if(dib->height < 0) /* top-down */
81     {
82         dib->height = -dib->height;
83     }
84     else /* bottom-up */
85     {
86         /* bits always points to the top-left corner and the stride is -ve */
87         dib->bits    = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
88         dib->stride  = -dib->stride;
89     }
90
91     dib->funcs = &funcs_null;
92
93     switch(dib->bit_count)
94     {
95     case 32:
96         if(bi->biCompression == BI_RGB)
97             bit_fields = bit_fields_888;
98
99         init_bit_fields(dib, bit_fields);
100
101         if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
102             dib->funcs = &funcs_8888;
103         else
104             dib->funcs = &funcs_32;
105         break;
106
107     case 24:
108         dib->funcs = &funcs_24;
109         break;
110
111     case 16:
112         if(bi->biCompression == BI_RGB)
113             bit_fields = bit_fields_555;
114
115         init_bit_fields(dib, bit_fields);
116
117         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
118             dib->funcs = &funcs_555;
119         else
120             dib->funcs = &funcs_16;
121         break;
122
123     case 8:
124         dib->funcs = &funcs_8;
125         break;
126
127     case 4:
128         dib->funcs = &funcs_4;
129         break;
130
131     case 1:
132         dib->funcs = &funcs_1;
133         break;
134
135     default:
136         TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
137         return FALSE;
138     }
139
140     if(color_table)
141     {
142         if (flags & private_color_table)
143         {
144             dib->color_table = HeapAlloc(GetProcessHeap(), 0, color_table_size * sizeof(dib->color_table[0]));
145             if(!dib->color_table) return FALSE;
146             memcpy(dib->color_table, color_table, color_table_size * sizeof(color_table[0]));
147         }
148         else
149             dib->color_table = color_table;
150         dib->color_table_size = color_table_size;
151     }
152     else
153     {
154         dib->color_table = NULL;
155         dib->color_table_size = 0;
156     }
157
158     return TRUE;
159 }
160
161 BOOL init_dib_info_from_packed(dib_info *dib, const BITMAPINFOHEADER *bi, WORD usage, HPALETTE palette)
162 {
163     DWORD *masks = NULL;
164     RGBQUAD *color_table = NULL, pal_table[256];
165     BYTE *ptr = (BYTE*)bi + bi->biSize;
166     int num_colors = get_dib_num_of_colors( (const BITMAPINFO *)bi );
167
168     if(bi->biCompression == BI_BITFIELDS)
169     {
170         masks = (DWORD *)ptr;
171         ptr += 3 * sizeof(DWORD);
172     }
173
174     if(num_colors)
175     {
176         if(usage == DIB_PAL_COLORS)
177         {
178             PALETTEENTRY entries[256];
179             const WORD *index = (const WORD*) ptr;
180             UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
181             for (i = 0; i < num_colors; i++, index++)
182             {
183                 PALETTEENTRY *entry = &entries[*index % count];
184                 pal_table[i].rgbRed      = entry->peRed;
185                 pal_table[i].rgbGreen    = entry->peGreen;
186                 pal_table[i].rgbBlue     = entry->peBlue;
187                 pal_table[i].rgbReserved = 0;
188             }
189             color_table = pal_table;
190             ptr += num_colors * sizeof(WORD);
191         }
192         else
193         {
194             color_table = (RGBQUAD*)ptr;
195             ptr += num_colors * sizeof(*color_table);
196         }
197     }
198
199     return init_dib_info(dib, bi, masks, color_table, num_colors, ptr, private_color_table);
200 }
201
202 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
203 {
204     unsigned int colors = get_dib_num_of_colors( info );
205     void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
206     const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
207
208     return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
209 }
210
211 static void clear_dib_info(dib_info *dib)
212 {
213     dib->color_table = NULL;
214     dib->bits = NULL;
215     dib->ptr_to_free = NULL;
216 }
217
218 /**********************************************************************
219  *      free_dib_info
220  *
221  * Free the resources associated with a dib and optionally the bits
222  */
223 void free_dib_info(dib_info *dib)
224 {
225     if (dib->flags & private_color_table)
226         HeapFree(GetProcessHeap(), 0, dib->color_table);
227     dib->color_table = NULL;
228
229     HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
230     dib->ptr_to_free = NULL;
231     dib->bits = NULL;
232 }
233
234 void copy_dib_color_info(dib_info *dst, const dib_info *src)
235 {
236     dst->bit_count        = src->bit_count;
237     dst->red_mask         = src->red_mask;
238     dst->green_mask       = src->green_mask;
239     dst->blue_mask        = src->blue_mask;
240     dst->red_len          = src->red_len;
241     dst->green_len        = src->green_len;
242     dst->blue_len         = src->blue_len;
243     dst->red_shift        = src->red_shift;
244     dst->green_shift      = src->green_shift;
245     dst->blue_shift       = src->blue_shift;
246     dst->funcs            = src->funcs;
247     dst->color_table_size = src->color_table_size;
248     dst->color_table      = NULL;
249     dst->flags            = src->flags;
250     if(dst->color_table_size)
251     {
252         int size = dst->color_table_size * sizeof(dst->color_table[0]);
253         if (dst->flags & private_color_table)
254         {
255             dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
256             memcpy(dst->color_table, src->color_table, size);
257         }
258         else
259             dst->color_table = src->color_table;
260     }
261 }
262
263 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
264                           const BITMAPINFO *dst_info, void *dst_bits )
265 {
266     dib_info src_dib, dst_dib;
267     DWORD ret;
268
269     if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
270         return ERROR_BAD_FORMAT;
271     if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
272         return ERROR_BAD_FORMAT;
273
274     ret = dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
275
276     /* We shared the color tables, so there's no need to free the dib_infos here */
277     if(!ret) return ERROR_BAD_FORMAT;
278
279     /* update coordinates, the destination rectangle is always stored at 0,0 */
280     src->x -= src->visrect.left;
281     src->y -= src->visrect.top;
282     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
283     return ERROR_SUCCESS;
284 }
285
286 static void update_fg_colors( dibdrv_physdev *pdev )
287 {
288     pdev->pen_color   = get_fg_color( pdev, pdev->pen_colorref );
289     pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
290 }
291
292 static void update_masks( dibdrv_physdev *pdev, INT rop )
293 {
294     calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
295     update_brush_rop( pdev, rop );
296     if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
297         calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
298 }
299
300  /***********************************************************************
301  *           add_extra_clipping_region
302  *
303  * Temporarily add a region to the current clipping region.
304  * The returned region must be restored with restore_clipping_region.
305  */
306 static HRGN add_extra_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
307 {
308     HRGN ret, clip;
309
310     if (!(clip = CreateRectRgn( 0, 0, 0, 0 ))) return 0;
311     CombineRgn( clip, pdev->clip, rgn, RGN_AND );
312     ret = pdev->clip;
313     pdev->clip = clip;
314     return ret;
315 }
316
317 /***********************************************************************
318  *           restore_clipping_region
319  */
320 static void restore_clipping_region( dibdrv_physdev *pdev, HRGN rgn )
321 {
322     if (!rgn) return;
323     DeleteObject( pdev->clip );
324     pdev->clip = rgn;
325 }
326
327 /***********************************************************************
328  *           dibdrv_DeleteDC
329  */
330 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
331 {
332     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
333     TRACE("(%p)\n", dev);
334     DeleteObject(pdev->clip);
335     free_pattern_brush(pdev);
336     free_dib_info(&pdev->dib);
337     return 0;
338 }
339
340 static void set_color_info( const dib_info *dib, BITMAPINFO *info )
341 {
342     DWORD *masks = (DWORD *)info->bmiColors;
343
344     info->bmiHeader.biCompression = BI_RGB;
345     info->bmiHeader.biClrUsed     = 0;
346
347     switch (info->bmiHeader.biBitCount)
348     {
349     case 1:
350     case 4:
351     case 8:
352         if (dib->color_table)
353         {
354             info->bmiHeader.biClrUsed = min( dib->color_table_size, 1 << dib->bit_count );
355             memcpy( info->bmiColors, dib->color_table,
356                     info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
357         }
358         break;
359     case 16:
360         masks[0] = dib->red_mask;
361         masks[1] = dib->green_mask;
362         masks[2] = dib->blue_mask;
363         info->bmiHeader.biCompression = BI_BITFIELDS;
364         break;
365     case 32:
366         if (dib->funcs != &funcs_8888)
367         {
368             masks[0] = dib->red_mask;
369             masks[1] = dib->green_mask;
370             masks[2] = dib->blue_mask;
371             info->bmiHeader.biCompression = BI_BITFIELDS;
372         }
373         break;
374     }
375 }
376
377 /***********************************************************************
378  *           dibdrv_GetImage
379  */
380 static DWORD dibdrv_GetImage( PHYSDEV dev, HBITMAP hbitmap, BITMAPINFO *info,
381                               struct gdi_image_bits *bits, struct bitblt_coords *src )
382 {
383     TRACE( "%p %p %p\n", dev, hbitmap, info );
384
385     info->bmiHeader.biSize          = sizeof(info->bmiHeader);
386     info->bmiHeader.biPlanes        = 1;
387     info->bmiHeader.biCompression   = BI_RGB;
388     info->bmiHeader.biXPelsPerMeter = 0;
389     info->bmiHeader.biYPelsPerMeter = 0;
390     info->bmiHeader.biClrUsed       = 0;
391     info->bmiHeader.biClrImportant  = 0;
392
393     if (hbitmap)
394     {
395         BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
396
397         if (!bmp) return ERROR_INVALID_HANDLE;
398         assert(bmp->dib);
399
400         info->bmiHeader.biWidth     = bmp->dib->dsBmih.biWidth;
401         info->bmiHeader.biHeight    = bmp->dib->dsBmih.biHeight;
402         info->bmiHeader.biBitCount  = bmp->dib->dsBmih.biBitCount;
403         info->bmiHeader.biSizeImage = get_dib_image_size( (BITMAPINFO *)&bmp->dib->dsBmih );
404
405         switch (info->bmiHeader.biBitCount)
406         {
407         case 1:
408         case 4:
409         case 8:
410             if (bmp->color_table)
411             {
412                 info->bmiHeader.biClrUsed = min( bmp->nb_colors, 1 << info->bmiHeader.biBitCount );
413                 memcpy( info->bmiColors, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
414             }
415             break;
416         case 16:
417             info->bmiHeader.biCompression = BI_BITFIELDS;
418             memcpy( info->bmiColors, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
419             break;
420         case 32:
421             if (bmp->dib->dsBmih.biCompression == BI_BITFIELDS &&
422                 memcmp( bmp->dib->dsBitfields, bit_fields_888, sizeof(bmp->dib->dsBitfields) ))
423             {
424                 info->bmiHeader.biCompression = BI_BITFIELDS;
425                 memcpy( info->bmiColors, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
426             }
427             break;
428         }
429         if (bits)
430         {
431             bits->ptr = bmp->dib->dsBm.bmBits;
432             bits->is_copy = FALSE;
433             bits->free = NULL;
434         }
435         GDI_ReleaseObj( hbitmap );
436     }
437     else
438     {
439         dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
440
441         info->bmiHeader.biWidth     = pdev->dib.width;
442         info->bmiHeader.biHeight    = pdev->dib.stride > 0 ? -pdev->dib.height : pdev->dib.height;
443         info->bmiHeader.biBitCount  = pdev->dib.bit_count;
444         info->bmiHeader.biSizeImage = pdev->dib.height * abs(pdev->dib.stride);
445
446         set_color_info( &pdev->dib, info );
447
448         if (bits)
449         {
450             bits->ptr = pdev->dib.bits;
451             if (pdev->dib.stride < 0)
452                 bits->ptr = (char *)bits->ptr + (pdev->dib.height - 1) * pdev->dib.stride;
453             bits->is_copy = FALSE;
454             bits->free = NULL;
455         }
456     }
457     return ERROR_SUCCESS;
458 }
459
460 static BOOL matching_color_info( const dib_info *dib, const BITMAPINFO *info )
461 {
462     switch (info->bmiHeader.biBitCount)
463     {
464     case 1:
465     case 4:
466     case 8:
467     {
468         RGBQUAD *color_table = (RGBQUAD *)((char *)info + info->bmiHeader.biSize);
469         if (dib->color_table_size != info->bmiHeader.biClrUsed ) return FALSE;
470         return memcmp( color_table, dib->color_table, dib->color_table_size * sizeof(RGBQUAD) );
471     }
472
473     case 16:
474     {
475         DWORD *masks = (DWORD *)info->bmiColors;
476         if (info->bmiHeader.biCompression == BI_RGB) return dib->funcs == &funcs_555;
477         if (info->bmiHeader.biCompression == BI_BITFIELDS)
478             return masks[0] == dib->red_mask && masks[1] == dib->green_mask && masks[2] == dib->blue_mask;
479         break;
480     }
481
482     case 24:
483         return TRUE;
484
485     case 32:
486     {
487         DWORD *masks = (DWORD *)info->bmiColors;
488         if (info->bmiHeader.biCompression == BI_RGB) return dib->funcs == &funcs_8888;
489         if (info->bmiHeader.biCompression == BI_BITFIELDS)
490             return masks[0] == dib->red_mask && masks[1] == dib->green_mask && masks[2] == dib->blue_mask;
491         break;
492     }
493
494     }
495
496     return FALSE;
497 }
498
499 static inline BOOL rop_uses_pat(DWORD rop)
500 {
501     return ((rop >> 4) & 0x0f0000) != (rop & 0x0f0000);
502 }
503
504 /***********************************************************************
505  *           dibdrv_PutImage
506  */
507 static DWORD dibdrv_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
508                               const struct gdi_image_bits *bits, struct bitblt_coords *src,
509                               struct bitblt_coords *dst, DWORD rop )
510 {
511     dib_info *dib, stand_alone;
512     DWORD ret;
513     POINT origin;
514     dib_info src_dib;
515     HRGN total_clip, saved_clip = NULL;
516     dibdrv_physdev *pdev = NULL;
517     const WINEREGION *clip_data;
518     int i, rop2;
519
520     TRACE( "%p %p %p\n", dev, hbitmap, info );
521
522     if (!hbitmap && rop_uses_pat( rop ))
523     {
524         PHYSDEV next = GET_NEXT_PHYSDEV( dev, pPutImage );
525         FIXME( "rop %08x unsupported, forwarding to graphics driver\n", rop );
526         return next->funcs->pPutImage( next, 0, clip, info, bits, src, dst, rop );
527     }
528
529     if (hbitmap)
530     {
531         BITMAPOBJ *bmp = GDI_GetObjPtr( hbitmap, OBJ_BITMAP );
532
533         if (!bmp) return ERROR_INVALID_HANDLE;
534         assert(bmp->dib);
535
536         if (!init_dib_info( &stand_alone, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
537                             bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, 0 ))
538         {
539             ret = ERROR_BAD_FORMAT;
540             goto done;
541         }
542         dib = &stand_alone;
543     }
544     else
545     {
546         pdev = get_dibdrv_pdev( dev );
547         dib = &pdev->dib;
548     }
549
550     if (info->bmiHeader.biPlanes != 1) goto update_format;
551     if (info->bmiHeader.biBitCount != dib->bit_count) goto update_format;
552     if (!matching_color_info( dib, info )) goto update_format;
553     if (!bits)
554     {
555         ret = ERROR_SUCCESS;
556         goto done;
557     }
558     if ((src->width != dst->width) || (src->height != dst->height))
559     {
560         ret = ERROR_TRANSFORM_NOT_SUPPORTED;
561         goto done;
562     }
563
564     init_dib_info_from_bitmapinfo( &src_dib, info, bits->ptr, 0 );
565
566     origin.x = src->visrect.left;
567     origin.y = src->visrect.top;
568
569     if (hbitmap)
570     {
571         total_clip = clip;
572         rop2 = R2_COPYPEN;
573     }
574     else
575     {
576         if (clip) saved_clip = add_extra_clipping_region( pdev, clip );
577         total_clip = pdev->clip;
578         rop2 =  ((rop >> 16) & 0xf) + 1;
579     }
580
581     if (total_clip == NULL) dib->funcs->copy_rect( dib, &dst->visrect, &src_dib, &origin, rop2 );
582     else
583     {
584         clip_data = get_wine_region( total_clip );
585         for (i = 0; i < clip_data->numRects; i++)
586         {
587             RECT clipped_rect;
588
589             if (intersect_rect( &clipped_rect, &dst->visrect, clip_data->rects + i ))
590             {
591                 origin.x = src->visrect.left + clipped_rect.left - dst->visrect.left;
592                 origin.y = src->visrect.top + clipped_rect.top  - dst->visrect.top;
593                 dib->funcs->copy_rect( dib, &clipped_rect, &src_dib, &origin, rop2 );
594             }
595         }
596         release_wine_region( total_clip );
597     }
598     ret = ERROR_SUCCESS;
599
600     if (saved_clip) restore_clipping_region( pdev, saved_clip );
601
602     goto done;
603
604 update_format:
605     info->bmiHeader.biPlanes   = 1;
606     info->bmiHeader.biBitCount = dib->bit_count;
607     set_color_info( dib, info );
608     ret = ERROR_BAD_FORMAT;
609
610 done:
611     if (hbitmap) GDI_ReleaseObj( hbitmap );
612
613     return ret;
614 }
615
616 /***********************************************************************
617  *           dibdrv_SelectBitmap
618  */
619 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
620 {
621     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
622     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
623     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
624     TRACE("(%p, %p)\n", dev, bitmap);
625
626     if (!bmp) return 0;
627     assert(bmp->dib);
628
629     pdev->clip = CreateRectRgn(0, 0, 0, 0);
630     pdev->defer = 0;
631
632     clear_dib_info(&pdev->dib);
633     clear_dib_info(&pdev->brush_dib);
634     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
635
636     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
637                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
638         pdev->defer |= DEFER_FORMAT;
639
640     GDI_ReleaseObj( bitmap );
641
642     return next->funcs->pSelectBitmap( next, bitmap );
643 }
644
645 /***********************************************************************
646  *           dibdrv_SetBkColor
647  */
648 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
649 {
650     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
651     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
652
653     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
654
655     if( GetBkMode(dev->hdc) == OPAQUE )
656         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
657     else
658     {
659         pdev->bkgnd_and = ~0u;
660         pdev->bkgnd_xor = 0;
661     }
662
663     update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
664
665     return next->funcs->pSetBkColor( next, color );
666 }
667
668 /***********************************************************************
669  *           dibdrv_SetBkMode
670  */
671 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
672 {
673     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
674     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
675
676     if( mode == OPAQUE )
677         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
678     else
679     {
680         pdev->bkgnd_and = ~0u;
681         pdev->bkgnd_xor = 0;
682     }
683
684     return next->funcs->pSetBkMode( next, mode );
685 }
686
687 /***********************************************************************
688  *           dibdrv_SetDeviceClipping
689  */
690 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
691 {
692     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
693     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
694     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
695
696     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
697     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
698 }
699
700 /***********************************************************************
701  *           dibdrv_SetDIBColorTable
702  */
703 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
704 {
705     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
706     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
707     TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
708
709     if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
710     {
711         if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
712         memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
713
714         pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
715         update_fg_colors( pdev );
716
717         update_masks( pdev, GetROP2( dev->hdc ) );
718     }
719     return next->funcs->pSetDIBColorTable( next, pos, count, colors );
720 }
721
722 /***********************************************************************
723  *           dibdrv_SetROP2
724  */
725 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
726 {
727     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
728     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
729
730     update_masks( pdev, rop );
731
732     return next->funcs->pSetROP2( next, rop );
733 }
734
735 const DC_FUNCTIONS dib_driver =
736 {
737     NULL,                               /* pAbortDoc */
738     NULL,                               /* pAbortPath */
739     NULL,                               /* pAlphaBlend */
740     NULL,                               /* pAngleArc */
741     NULL,                               /* pArc */
742     NULL,                               /* pArcTo */
743     NULL,                               /* pBeginPath */
744     NULL,                               /* pChoosePixelFormat */
745     NULL,                               /* pChord */
746     NULL,                               /* pCloseFigure */
747     NULL,                               /* pCreateBitmap */
748     NULL,                               /* pCreateDC */
749     NULL,                               /* pCreateDIBSection */
750     NULL,                               /* pDeleteBitmap */
751     dibdrv_DeleteDC,                    /* pDeleteDC */
752     NULL,                               /* pDeleteObject */
753     NULL,                               /* pDescribePixelFormat */
754     NULL,                               /* pDeviceCapabilities */
755     NULL,                               /* pEllipse */
756     NULL,                               /* pEndDoc */
757     NULL,                               /* pEndPage */
758     NULL,                               /* pEndPath */
759     NULL,                               /* pEnumDeviceFonts */
760     NULL,                               /* pEnumICMProfiles */
761     NULL,                               /* pExcludeClipRect */
762     NULL,                               /* pExtDeviceMode */
763     NULL,                               /* pExtEscape */
764     NULL,                               /* pExtFloodFill */
765     NULL,                               /* pExtSelectClipRgn */
766     NULL,                               /* pExtTextOut */
767     NULL,                               /* pFillPath */
768     NULL,                               /* pFillRgn */
769     NULL,                               /* pFlattenPath */
770     NULL,                               /* pFrameRgn */
771     NULL,                               /* pGdiComment */
772     NULL,                               /* pGetCharWidth */
773     NULL,                               /* pGetDeviceCaps */
774     NULL,                               /* pGetDeviceGammaRamp */
775     NULL,                               /* pGetICMProfile */
776     dibdrv_GetImage,                    /* pGetImage */
777     NULL,                               /* pGetNearestColor */
778     NULL,                               /* pGetPixel */
779     NULL,                               /* pGetPixelFormat */
780     NULL,                               /* pGetSystemPaletteEntries */
781     NULL,                               /* pGetTextExtentExPoint */
782     NULL,                               /* pGetTextMetrics */
783     NULL,                               /* pIntersectClipRect */
784     NULL,                               /* pInvertRgn */
785     dibdrv_LineTo,                      /* pLineTo */
786     NULL,                               /* pModifyWorldTransform */
787     NULL,                               /* pMoveTo */
788     NULL,                               /* pOffsetClipRgn */
789     NULL,                               /* pOffsetViewportOrg */
790     NULL,                               /* pOffsetWindowOrg */
791     dibdrv_PaintRgn,                    /* pPaintRgn */
792     dibdrv_PatBlt,                      /* pPatBlt */
793     NULL,                               /* pPie */
794     NULL,                               /* pPolyBezier */
795     NULL,                               /* pPolyBezierTo */
796     NULL,                               /* pPolyDraw */
797     NULL,                               /* pPolyPolygon */
798     NULL,                               /* pPolyPolyline */
799     NULL,                               /* pPolygon */
800     NULL,                               /* pPolyline */
801     NULL,                               /* pPolylineTo */
802     dibdrv_PutImage,                    /* pPutImage */
803     NULL,                               /* pRealizeDefaultPalette */
804     NULL,                               /* pRealizePalette */
805     dibdrv_Rectangle,                   /* pRectangle */
806     NULL,                               /* pResetDC */
807     NULL,                               /* pRestoreDC */
808     NULL,                               /* pRoundRect */
809     NULL,                               /* pSaveDC */
810     NULL,                               /* pScaleViewportExt */
811     NULL,                               /* pScaleWindowExt */
812     dibdrv_SelectBitmap,                /* pSelectBitmap */
813     dibdrv_SelectBrush,                 /* pSelectBrush */
814     NULL,                               /* pSelectClipPath */
815     NULL,                               /* pSelectFont */
816     NULL,                               /* pSelectPalette */
817     dibdrv_SelectPen,                   /* pSelectPen */
818     NULL,                               /* pSetArcDirection */
819     dibdrv_SetBkColor,                  /* pSetBkColor */
820     dibdrv_SetBkMode,                   /* pSetBkMode */
821     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
822     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
823     dibdrv_SetDIBColorTable,            /* pSetDIBColorTable */
824     NULL,                               /* pSetDIBitsToDevice */
825     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
826     NULL,                               /* pSetDeviceGammaRamp */
827     NULL,                               /* pSetLayout */
828     NULL,                               /* pSetMapMode */
829     NULL,                               /* pSetMapperFlags */
830     NULL,                               /* pSetPixel */
831     NULL,                               /* pSetPixelFormat */
832     NULL,                               /* pSetPolyFillMode */
833     dibdrv_SetROP2,                     /* pSetROP2 */
834     NULL,                               /* pSetRelAbs */
835     NULL,                               /* pSetStretchBltMode */
836     NULL,                               /* pSetTextAlign */
837     NULL,                               /* pSetTextCharacterExtra */
838     NULL,                               /* pSetTextColor */
839     NULL,                               /* pSetTextJustification */
840     NULL,                               /* pSetViewportExt */
841     NULL,                               /* pSetViewportOrg */
842     NULL,                               /* pSetWindowExt */
843     NULL,                               /* pSetWindowOrg */
844     NULL,                               /* pSetWorldTransform */
845     NULL,                               /* pStartDoc */
846     NULL,                               /* pStartPage */
847     NULL,                               /* pStretchBlt */
848     NULL,                               /* pStretchDIBits */
849     NULL,                               /* pStrokeAndFillPath */
850     NULL,                               /* pStrokePath */
851     NULL,                               /* pSwapBuffers */
852     NULL,                               /* pUnrealizePalette */
853     NULL,                               /* pWidenPath */
854     NULL,                               /* pwglCopyContext */
855     NULL,                               /* pwglCreateContext */
856     NULL,                               /* pwglCreateContextAttribsARB */
857     NULL,                               /* pwglDeleteContext */
858     NULL,                               /* pwglGetPbufferDCARB */
859     NULL,                               /* pwglGetProcAddress */
860     NULL,                               /* pwglMakeContextCurrentARB */
861     NULL,                               /* pwglMakeCurrent */
862     NULL,                               /* pwglSetPixelFormatWINE */
863     NULL,                               /* pwglShareLists */
864     NULL,                               /* pwglUseFontBitmapsA */
865     NULL                                /* pwglUseFontBitmapsW */
866 };