gdi32: Get rid of the SetDIBits driver entry point.
[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 void calc_shift_and_len(DWORD mask, int *shift, int *len)
31 {
32     int s, l;
33
34     if(!mask)
35     {
36         *shift = *len = 0;
37         return;
38     }
39
40     s = 0;
41     while ((mask & 1) == 0)
42     {
43         mask >>= 1;
44         s++;
45     }
46     l = 0;
47     while ((mask & 1) == 1)
48     {
49         mask >>= 1;
50         l++;
51     }
52     *shift = s;
53     *len = l;
54 }
55
56 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
57 {
58     dib->red_mask    = bit_fields[0];
59     dib->green_mask  = bit_fields[1];
60     dib->blue_mask   = bit_fields[2];
61     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
62     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
63     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
64 }
65
66 static BOOL init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
67                           RGBQUAD *color_table, int color_table_size, void *bits, enum dib_info_flags flags)
68 {
69     static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
70     static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
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 = bi->biClrUsed;
167
168     if(bi->biCompression == BI_BITFIELDS)
169     {
170         masks = (DWORD *)ptr;
171         ptr += 3 * sizeof(DWORD);
172     }
173
174     if(!num_colors && bi->biBitCount <= 8) num_colors = 1 << bi->biBitCount;
175     if(num_colors)
176     {
177         if(usage == DIB_PAL_COLORS)
178         {
179             PALETTEENTRY entries[256];
180             const WORD *index = (const WORD*) ptr;
181             UINT i, count = GetPaletteEntries( palette, 0, num_colors, entries );
182             for (i = 0; i < num_colors; i++, index++)
183             {
184                 PALETTEENTRY *entry = &entries[*index % count];
185                 pal_table[i].rgbRed      = entry->peRed;
186                 pal_table[i].rgbGreen    = entry->peGreen;
187                 pal_table[i].rgbBlue     = entry->peBlue;
188                 pal_table[i].rgbReserved = 0;
189             }
190             color_table = pal_table;
191             ptr += num_colors * sizeof(WORD);
192         }
193         else
194         {
195             color_table = (RGBQUAD*)ptr;
196             ptr += num_colors * sizeof(*color_table);
197         }
198     }
199
200     return init_dib_info(dib, bi, masks, color_table, num_colors, ptr, private_color_table);
201 }
202
203 BOOL init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
204 {
205     unsigned int colors = 0;
206     void *colorptr = (char *)&info->bmiHeader + info->bmiHeader.biSize;
207     const DWORD *bitfields = (info->bmiHeader.biCompression == BI_BITFIELDS) ? (DWORD *)colorptr : NULL;
208
209     if (info->bmiHeader.biBitCount <= 8)
210     {
211         colors = 1 << info->bmiHeader.biBitCount;
212         if (info->bmiHeader.biClrUsed) colors = info->bmiHeader.biClrUsed;
213     }
214     return init_dib_info( dib, &info->bmiHeader, bitfields, colors ? colorptr : NULL, colors, bits, flags );
215 }
216
217 static void clear_dib_info(dib_info *dib)
218 {
219     dib->color_table = NULL;
220     dib->bits = NULL;
221     dib->ptr_to_free = NULL;
222 }
223
224 /**********************************************************************
225  *      free_dib_info
226  *
227  * Free the resources associated with a dib and optionally the bits
228  */
229 void free_dib_info(dib_info *dib)
230 {
231     if (dib->flags & private_color_table)
232         HeapFree(GetProcessHeap(), 0, dib->color_table);
233     dib->color_table = NULL;
234
235     HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
236     dib->ptr_to_free = NULL;
237     dib->bits = NULL;
238 }
239
240 void copy_dib_color_info(dib_info *dst, const dib_info *src)
241 {
242     dst->bit_count        = src->bit_count;
243     dst->red_mask         = src->red_mask;
244     dst->green_mask       = src->green_mask;
245     dst->blue_mask        = src->blue_mask;
246     dst->red_len          = src->red_len;
247     dst->green_len        = src->green_len;
248     dst->blue_len         = src->blue_len;
249     dst->red_shift        = src->red_shift;
250     dst->green_shift      = src->green_shift;
251     dst->blue_shift       = src->blue_shift;
252     dst->funcs            = src->funcs;
253     dst->color_table_size = src->color_table_size;
254     dst->color_table      = NULL;
255     dst->flags            = src->flags;
256     if(dst->color_table_size)
257     {
258         int size = dst->color_table_size * sizeof(dst->color_table[0]);
259         if (dst->flags & private_color_table)
260         {
261             dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
262             memcpy(dst->color_table, src->color_table, size);
263         }
264         else
265             dst->color_table = src->color_table;
266     }
267 }
268
269 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, const RECT *src_rect,
270                           const BITMAPINFO *dst_info, void *dst_bits )
271 {
272     dib_info src_dib, dst_dib;
273     DWORD ret;
274
275     if ( !init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, 0 ) )
276         return ERROR_BAD_FORMAT;
277     if ( !init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, 0 ) )
278         return ERROR_BAD_FORMAT;
279
280     ret = dst_dib.funcs->convert_to( &dst_dib, &src_dib, src_rect );
281
282     /* We shared the color tables, so there's no need to free the dib_infos here */
283
284     if(!ret) return ERROR_BAD_FORMAT;
285     return ERROR_SUCCESS;
286 }
287
288 static void update_fg_colors( dibdrv_physdev *pdev )
289 {
290     pdev->pen_color   = get_fg_color( pdev, pdev->pen_colorref );
291     pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
292 }
293
294 static void update_masks( dibdrv_physdev *pdev, INT rop )
295 {
296     calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
297     update_brush_rop( pdev, rop );
298     if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
299         calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
300 }
301
302 /***********************************************************************
303  *           dibdrv_DeleteDC
304  */
305 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
306 {
307     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
308     TRACE("(%p)\n", dev);
309     DeleteObject(pdev->clip);
310     free_pattern_brush(pdev);
311     free_dib_info(&pdev->dib);
312     return 0;
313 }
314
315 /***********************************************************************
316  *           dibdrv_SelectBitmap
317  */
318 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
319 {
320     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
321     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
322     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
323     TRACE("(%p, %p)\n", dev, bitmap);
324
325     if (!bmp) return 0;
326     assert(bmp->dib);
327
328     pdev->clip = CreateRectRgn(0, 0, 0, 0);
329     pdev->defer = 0;
330
331     clear_dib_info(&pdev->dib);
332     clear_dib_info(&pdev->brush_dib);
333     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
334
335     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
336                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
337         pdev->defer |= DEFER_FORMAT;
338
339     GDI_ReleaseObj( bitmap );
340
341     return next->funcs->pSelectBitmap( next, bitmap );
342 }
343
344 /***********************************************************************
345  *           dibdrv_SetBkColor
346  */
347 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
348 {
349     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
350     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
351
352     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
353
354     if( GetBkMode(dev->hdc) == OPAQUE )
355         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
356     else
357     {
358         pdev->bkgnd_and = ~0u;
359         pdev->bkgnd_xor = 0;
360     }
361
362     update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
363
364     return next->funcs->pSetBkColor( next, color );
365 }
366
367 /***********************************************************************
368  *           dibdrv_SetBkMode
369  */
370 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
371 {
372     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
373     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
374
375     if( mode == OPAQUE )
376         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
377     else
378     {
379         pdev->bkgnd_and = ~0u;
380         pdev->bkgnd_xor = 0;
381     }
382
383     return next->funcs->pSetBkMode( next, mode );
384 }
385
386 /***********************************************************************
387  *           dibdrv_SetDeviceClipping
388  */
389 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
390 {
391     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
392     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
393     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
394
395     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
396     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
397 }
398
399 /***********************************************************************
400  *           dibdrv_SetDIBColorTable
401  */
402 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
403 {
404     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
405     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
406     TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
407
408     if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
409     {
410         if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
411         memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
412
413         pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
414         update_fg_colors( pdev );
415
416         update_masks( pdev, GetROP2( dev->hdc ) );
417     }
418     return next->funcs->pSetDIBColorTable( next, pos, count, colors );
419 }
420
421 /***********************************************************************
422  *           dibdrv_SetROP2
423  */
424 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
425 {
426     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
427     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
428
429     update_masks( pdev, rop );
430
431     return next->funcs->pSetROP2( next, rop );
432 }
433
434 const DC_FUNCTIONS dib_driver =
435 {
436     NULL,                               /* pAbortDoc */
437     NULL,                               /* pAbortPath */
438     NULL,                               /* pAlphaBlend */
439     NULL,                               /* pAngleArc */
440     NULL,                               /* pArc */
441     NULL,                               /* pArcTo */
442     NULL,                               /* pBeginPath */
443     NULL,                               /* pChoosePixelFormat */
444     NULL,                               /* pChord */
445     NULL,                               /* pCloseFigure */
446     NULL,                               /* pCreateBitmap */
447     NULL,                               /* pCreateDC */
448     NULL,                               /* pCreateDIBSection */
449     NULL,                               /* pDeleteBitmap */
450     dibdrv_DeleteDC,                    /* pDeleteDC */
451     NULL,                               /* pDeleteObject */
452     NULL,                               /* pDescribePixelFormat */
453     NULL,                               /* pDeviceCapabilities */
454     NULL,                               /* pEllipse */
455     NULL,                               /* pEndDoc */
456     NULL,                               /* pEndPage */
457     NULL,                               /* pEndPath */
458     NULL,                               /* pEnumDeviceFonts */
459     NULL,                               /* pEnumICMProfiles */
460     NULL,                               /* pExcludeClipRect */
461     NULL,                               /* pExtDeviceMode */
462     NULL,                               /* pExtEscape */
463     NULL,                               /* pExtFloodFill */
464     NULL,                               /* pExtSelectClipRgn */
465     NULL,                               /* pExtTextOut */
466     NULL,                               /* pFillPath */
467     NULL,                               /* pFillRgn */
468     NULL,                               /* pFlattenPath */
469     NULL,                               /* pFrameRgn */
470     NULL,                               /* pGdiComment */
471     NULL,                               /* pGetBitmapBits */
472     NULL,                               /* pGetCharWidth */
473     NULL,                               /* pGetDeviceCaps */
474     NULL,                               /* pGetDeviceGammaRamp */
475     NULL,                               /* pGetICMProfile */
476     NULL,                               /* pGetImage */
477     NULL,                               /* pGetNearestColor */
478     NULL,                               /* pGetPixel */
479     NULL,                               /* pGetPixelFormat */
480     NULL,                               /* pGetSystemPaletteEntries */
481     NULL,                               /* pGetTextExtentExPoint */
482     NULL,                               /* pGetTextMetrics */
483     NULL,                               /* pIntersectClipRect */
484     NULL,                               /* pInvertRgn */
485     dibdrv_LineTo,                      /* pLineTo */
486     NULL,                               /* pModifyWorldTransform */
487     NULL,                               /* pMoveTo */
488     NULL,                               /* pOffsetClipRgn */
489     NULL,                               /* pOffsetViewportOrg */
490     NULL,                               /* pOffsetWindowOrg */
491     dibdrv_PaintRgn,                    /* pPaintRgn */
492     dibdrv_PatBlt,                      /* pPatBlt */
493     NULL,                               /* pPie */
494     NULL,                               /* pPolyBezier */
495     NULL,                               /* pPolyBezierTo */
496     NULL,                               /* pPolyDraw */
497     NULL,                               /* pPolyPolygon */
498     NULL,                               /* pPolyPolyline */
499     NULL,                               /* pPolygon */
500     NULL,                               /* pPolyline */
501     NULL,                               /* pPolylineTo */
502     NULL,                               /* pPutImage */
503     NULL,                               /* pRealizeDefaultPalette */
504     NULL,                               /* pRealizePalette */
505     dibdrv_Rectangle,                   /* pRectangle */
506     NULL,                               /* pResetDC */
507     NULL,                               /* pRestoreDC */
508     NULL,                               /* pRoundRect */
509     NULL,                               /* pSaveDC */
510     NULL,                               /* pScaleViewportExt */
511     NULL,                               /* pScaleWindowExt */
512     dibdrv_SelectBitmap,                /* pSelectBitmap */
513     dibdrv_SelectBrush,                 /* pSelectBrush */
514     NULL,                               /* pSelectClipPath */
515     NULL,                               /* pSelectFont */
516     NULL,                               /* pSelectPalette */
517     dibdrv_SelectPen,                   /* pSelectPen */
518     NULL,                               /* pSetArcDirection */
519     NULL,                               /* pSetBitmapBits */
520     dibdrv_SetBkColor,                  /* pSetBkColor */
521     dibdrv_SetBkMode,                   /* pSetBkMode */
522     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
523     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
524     dibdrv_SetDIBColorTable,            /* pSetDIBColorTable */
525     NULL,                               /* pSetDIBitsToDevice */
526     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
527     NULL,                               /* pSetDeviceGammaRamp */
528     NULL,                               /* pSetLayout */
529     NULL,                               /* pSetMapMode */
530     NULL,                               /* pSetMapperFlags */
531     NULL,                               /* pSetPixel */
532     NULL,                               /* pSetPixelFormat */
533     NULL,                               /* pSetPolyFillMode */
534     dibdrv_SetROP2,                     /* pSetROP2 */
535     NULL,                               /* pSetRelAbs */
536     NULL,                               /* pSetStretchBltMode */
537     NULL,                               /* pSetTextAlign */
538     NULL,                               /* pSetTextCharacterExtra */
539     NULL,                               /* pSetTextColor */
540     NULL,                               /* pSetTextJustification */
541     NULL,                               /* pSetViewportExt */
542     NULL,                               /* pSetViewportOrg */
543     NULL,                               /* pSetWindowExt */
544     NULL,                               /* pSetWindowOrg */
545     NULL,                               /* pSetWorldTransform */
546     NULL,                               /* pStartDoc */
547     NULL,                               /* pStartPage */
548     NULL,                               /* pStretchBlt */
549     NULL,                               /* pStretchDIBits */
550     NULL,                               /* pStrokeAndFillPath */
551     NULL,                               /* pStrokePath */
552     NULL,                               /* pSwapBuffers */
553     NULL,                               /* pUnrealizePalette */
554     NULL,                               /* pWidenPath */
555     NULL,                               /* pwglCopyContext */
556     NULL,                               /* pwglCreateContext */
557     NULL,                               /* pwglCreateContextAttribsARB */
558     NULL,                               /* pwglDeleteContext */
559     NULL,                               /* pwglGetPbufferDCARB */
560     NULL,                               /* pwglGetProcAddress */
561     NULL,                               /* pwglMakeContextCurrentARB */
562     NULL,                               /* pwglMakeCurrent */
563     NULL,                               /* pwglSetPixelFormatWINE */
564     NULL,                               /* pwglShareLists */
565     NULL,                               /* pwglUseFontBitmapsA */
566     NULL                                /* pwglUseFontBitmapsW */
567 };