crypt32: Add additional path for Solaris 11 Express.
[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    = ((dib->width * dib->bit_count + 31) >> 3) & ~3;
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 static void clear_dib_info(dib_info *dib)
204 {
205     dib->color_table = NULL;
206     dib->bits = NULL;
207     dib->ptr_to_free = NULL;
208 }
209
210 /**********************************************************************
211  *      free_dib_info
212  *
213  * Free the resources associated with a dib and optionally the bits
214  */
215 void free_dib_info(dib_info *dib)
216 {
217     if (dib->flags & private_color_table)
218         HeapFree(GetProcessHeap(), 0, dib->color_table);
219     dib->color_table = NULL;
220
221     HeapFree(GetProcessHeap(), 0, dib->ptr_to_free);
222     dib->ptr_to_free = NULL;
223     dib->bits = NULL;
224 }
225
226 void copy_dib_color_info(dib_info *dst, const dib_info *src)
227 {
228     dst->bit_count        = src->bit_count;
229     dst->red_mask         = src->red_mask;
230     dst->green_mask       = src->green_mask;
231     dst->blue_mask        = src->blue_mask;
232     dst->red_len          = src->red_len;
233     dst->green_len        = src->green_len;
234     dst->blue_len         = src->blue_len;
235     dst->red_shift        = src->red_shift;
236     dst->green_shift      = src->green_shift;
237     dst->blue_shift       = src->blue_shift;
238     dst->funcs            = src->funcs;
239     dst->color_table_size = src->color_table_size;
240     dst->color_table      = NULL;
241     dst->flags            = src->flags;
242     if(dst->color_table_size)
243     {
244         int size = dst->color_table_size * sizeof(dst->color_table[0]);
245         if (dst->flags & private_color_table)
246         {
247             dst->color_table = HeapAlloc(GetProcessHeap(), 0, size);
248             memcpy(dst->color_table, src->color_table, size);
249         }
250         else
251             dst->color_table = src->color_table;
252     }
253 }
254
255 /**************************************************************
256  *            convert_dib
257  *
258  * Converts src into the format specified in dst.
259  *
260  * FIXME: At the moment this always creates a top-down dib,
261  * do we want to give the option of bottom-up?
262  */
263 BOOL convert_dib(dib_info *dst, const dib_info *src)
264 {
265     BOOL ret;
266     RECT src_rect;
267
268     dst->height = src->height;
269     dst->width = src->width;
270     dst->stride = ((dst->width * dst->bit_count + 31) >> 3) & ~3;
271     dst->ptr_to_free = dst->bits = HeapAlloc(GetProcessHeap(), 0, dst->height * dst->stride);
272
273     src_rect.left = src_rect.top = 0;
274     src_rect.right = src->width;
275     src_rect.bottom = src->height;
276
277     ret = dst->funcs->convert_to(dst, src, &src_rect);
278
279     if(!ret) free_dib_info(dst);
280     return ret;
281 }
282
283 static void update_fg_colors( dibdrv_physdev *pdev )
284 {
285     pdev->pen_color   = get_fg_color( pdev, pdev->pen_colorref );
286     pdev->brush_color = get_fg_color( pdev, pdev->brush_colorref );
287 }
288
289 static void update_masks( dibdrv_physdev *pdev, INT rop )
290 {
291     calc_and_xor_masks( rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor );
292     update_brush_rop( pdev, rop );
293     if( GetBkMode( pdev->dev.hdc ) == OPAQUE )
294         calc_and_xor_masks( rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
295 }
296
297 /***********************************************************************
298  *           dibdrv_DeleteDC
299  */
300 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
301 {
302     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
303     TRACE("(%p)\n", dev);
304     DeleteObject(pdev->clip);
305     free_pattern_brush(pdev);
306     free_dib_info(&pdev->dib);
307     return 0;
308 }
309
310 /***********************************************************************
311  *           dibdrv_SelectBitmap
312  */
313 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
314 {
315     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
316     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
317     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
318     TRACE("(%p, %p)\n", dev, bitmap);
319
320     if (!bmp) return 0;
321     assert(bmp->dib);
322
323     pdev->clip = CreateRectRgn(0, 0, 0, 0);
324     pdev->defer = 0;
325
326     clear_dib_info(&pdev->dib);
327     clear_dib_info(&pdev->brush_dib);
328     pdev->brush_and_bits = pdev->brush_xor_bits = NULL;
329
330     if(!init_dib_info(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
331                       bmp->color_table, bmp->nb_colors, bmp->dib->dsBm.bmBits, private_color_table))
332         pdev->defer |= DEFER_FORMAT;
333
334     GDI_ReleaseObj( bitmap );
335
336     return next->funcs->pSelectBitmap( next, bitmap );
337 }
338
339 /***********************************************************************
340  *           dibdrv_SetBkColor
341  */
342 static COLORREF dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
343 {
344     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
345     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
346
347     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
348
349     if( GetBkMode(dev->hdc) == OPAQUE )
350         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
351     else
352     {
353         pdev->bkgnd_and = ~0u;
354         pdev->bkgnd_xor = 0;
355     }
356
357     update_fg_colors( pdev ); /* Only needed in the 1 bpp case */
358
359     return next->funcs->pSetBkColor( next, color );
360 }
361
362 /***********************************************************************
363  *           dibdrv_SetBkMode
364  */
365 static INT dibdrv_SetBkMode( PHYSDEV dev, INT mode )
366 {
367     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
368     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
369
370     if( mode == OPAQUE )
371         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
372     else
373     {
374         pdev->bkgnd_and = ~0u;
375         pdev->bkgnd_xor = 0;
376     }
377
378     return next->funcs->pSetBkMode( next, mode );
379 }
380
381 /***********************************************************************
382  *           dibdrv_SetDeviceClipping
383  */
384 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
385 {
386     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
387     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
388     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
389
390     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
391     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
392 }
393
394 /***********************************************************************
395  *           dibdrv_SetDIBColorTable
396  */
397 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
398 {
399     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
400     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
401     TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
402
403     if( pdev->dib.color_table && pos < pdev->dib.color_table_size )
404     {
405         if( pos + count > pdev->dib.color_table_size ) count = pdev->dib.color_table_size - pos;
406         memcpy( pdev->dib.color_table + pos, colors, count * sizeof(RGBQUAD) );
407
408         pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, GetBkColor( dev->hdc ) );
409         update_fg_colors( pdev );
410
411         update_masks( pdev, GetROP2( dev->hdc ) );
412     }
413     return next->funcs->pSetDIBColorTable( next, pos, count, colors );
414 }
415
416 /***********************************************************************
417  *           dibdrv_SetROP2
418  */
419 static INT dibdrv_SetROP2( PHYSDEV dev, INT rop )
420 {
421     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
422     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
423
424     update_masks( pdev, rop );
425
426     return next->funcs->pSetROP2( next, rop );
427 }
428
429 const DC_FUNCTIONS dib_driver =
430 {
431     NULL,                               /* pAbortDoc */
432     NULL,                               /* pAbortPath */
433     NULL,                               /* pAlphaBlend */
434     NULL,                               /* pAngleArc */
435     NULL,                               /* pArc */
436     NULL,                               /* pArcTo */
437     NULL,                               /* pBeginPath */
438     NULL,                               /* pChoosePixelFormat */
439     NULL,                               /* pChord */
440     NULL,                               /* pCloseFigure */
441     NULL,                               /* pCreateBitmap */
442     NULL,                               /* pCreateDC */
443     NULL,                               /* pCreateDIBSection */
444     NULL,                               /* pDeleteBitmap */
445     dibdrv_DeleteDC,                    /* pDeleteDC */
446     NULL,                               /* pDeleteObject */
447     NULL,                               /* pDescribePixelFormat */
448     NULL,                               /* pDeviceCapabilities */
449     NULL,                               /* pEllipse */
450     NULL,                               /* pEndDoc */
451     NULL,                               /* pEndPage */
452     NULL,                               /* pEndPath */
453     NULL,                               /* pEnumDeviceFonts */
454     NULL,                               /* pEnumICMProfiles */
455     NULL,                               /* pExcludeClipRect */
456     NULL,                               /* pExtDeviceMode */
457     NULL,                               /* pExtEscape */
458     NULL,                               /* pExtFloodFill */
459     NULL,                               /* pExtSelectClipRgn */
460     NULL,                               /* pExtTextOut */
461     NULL,                               /* pFillPath */
462     NULL,                               /* pFillRgn */
463     NULL,                               /* pFlattenPath */
464     NULL,                               /* pFrameRgn */
465     NULL,                               /* pGdiComment */
466     NULL,                               /* pGetBitmapBits */
467     NULL,                               /* pGetCharWidth */
468     NULL,                               /* pGetDIBits */
469     NULL,                               /* pGetDeviceCaps */
470     NULL,                               /* pGetDeviceGammaRamp */
471     NULL,                               /* pGetICMProfile */
472     NULL,                               /* pGetImage */
473     NULL,                               /* pGetNearestColor */
474     NULL,                               /* pGetPixel */
475     NULL,                               /* pGetPixelFormat */
476     NULL,                               /* pGetSystemPaletteEntries */
477     NULL,                               /* pGetTextExtentExPoint */
478     NULL,                               /* pGetTextMetrics */
479     NULL,                               /* pIntersectClipRect */
480     NULL,                               /* pInvertRgn */
481     dibdrv_LineTo,                      /* pLineTo */
482     NULL,                               /* pModifyWorldTransform */
483     NULL,                               /* pMoveTo */
484     NULL,                               /* pOffsetClipRgn */
485     NULL,                               /* pOffsetViewportOrg */
486     NULL,                               /* pOffsetWindowOrg */
487     dibdrv_PaintRgn,                    /* pPaintRgn */
488     dibdrv_PatBlt,                      /* pPatBlt */
489     NULL,                               /* pPie */
490     NULL,                               /* pPolyBezier */
491     NULL,                               /* pPolyBezierTo */
492     NULL,                               /* pPolyDraw */
493     NULL,                               /* pPolyPolygon */
494     NULL,                               /* pPolyPolyline */
495     NULL,                               /* pPolygon */
496     NULL,                               /* pPolyline */
497     NULL,                               /* pPolylineTo */
498     NULL,                               /* pPutImage */
499     NULL,                               /* pRealizeDefaultPalette */
500     NULL,                               /* pRealizePalette */
501     dibdrv_Rectangle,                   /* pRectangle */
502     NULL,                               /* pResetDC */
503     NULL,                               /* pRestoreDC */
504     NULL,                               /* pRoundRect */
505     NULL,                               /* pSaveDC */
506     NULL,                               /* pScaleViewportExt */
507     NULL,                               /* pScaleWindowExt */
508     dibdrv_SelectBitmap,                /* pSelectBitmap */
509     dibdrv_SelectBrush,                 /* pSelectBrush */
510     NULL,                               /* pSelectClipPath */
511     NULL,                               /* pSelectFont */
512     NULL,                               /* pSelectPalette */
513     dibdrv_SelectPen,                   /* pSelectPen */
514     NULL,                               /* pSetArcDirection */
515     NULL,                               /* pSetBitmapBits */
516     dibdrv_SetBkColor,                  /* pSetBkColor */
517     dibdrv_SetBkMode,                   /* pSetBkMode */
518     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
519     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
520     dibdrv_SetDIBColorTable,            /* pSetDIBColorTable */
521     NULL,                               /* pSetDIBits */
522     NULL,                               /* pSetDIBitsToDevice */
523     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
524     NULL,                               /* pSetDeviceGammaRamp */
525     NULL,                               /* pSetLayout */
526     NULL,                               /* pSetMapMode */
527     NULL,                               /* pSetMapperFlags */
528     NULL,                               /* pSetPixel */
529     NULL,                               /* pSetPixelFormat */
530     NULL,                               /* pSetPolyFillMode */
531     dibdrv_SetROP2,                     /* pSetROP2 */
532     NULL,                               /* pSetRelAbs */
533     NULL,                               /* pSetStretchBltMode */
534     NULL,                               /* pSetTextAlign */
535     NULL,                               /* pSetTextCharacterExtra */
536     NULL,                               /* pSetTextColor */
537     NULL,                               /* pSetTextJustification */
538     NULL,                               /* pSetViewportExt */
539     NULL,                               /* pSetViewportOrg */
540     NULL,                               /* pSetWindowExt */
541     NULL,                               /* pSetWindowOrg */
542     NULL,                               /* pSetWorldTransform */
543     NULL,                               /* pStartDoc */
544     NULL,                               /* pStartPage */
545     NULL,                               /* pStretchBlt */
546     NULL,                               /* pStretchDIBits */
547     NULL,                               /* pStrokeAndFillPath */
548     NULL,                               /* pStrokePath */
549     NULL,                               /* pSwapBuffers */
550     NULL,                               /* pUnrealizePalette */
551     NULL,                               /* pWidenPath */
552     NULL,                               /* pwglCopyContext */
553     NULL,                               /* pwglCreateContext */
554     NULL,                               /* pwglCreateContextAttribsARB */
555     NULL,                               /* pwglDeleteContext */
556     NULL,                               /* pwglGetPbufferDCARB */
557     NULL,                               /* pwglGetProcAddress */
558     NULL,                               /* pwglMakeContextCurrentARB */
559     NULL,                               /* pwglMakeCurrent */
560     NULL,                               /* pwglSetPixelFormatWINE */
561     NULL,                               /* pwglShareLists */
562     NULL,                               /* pwglUseFontBitmapsA */
563     NULL                                /* pwglUseFontBitmapsW */
564 };