mshtml: Added IHTMLWindow6::get_sessionStorage implementation.
[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/exception.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
30
31 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
32 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
33
34 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
35 {
36     int s, l;
37
38     if(!mask)
39     {
40         *shift = *len = 0;
41         return;
42     }
43
44     s = 0;
45     while ((mask & 1) == 0)
46     {
47         mask >>= 1;
48         s++;
49     }
50     l = 0;
51     while ((mask & 1) == 1)
52     {
53         mask >>= 1;
54         l++;
55     }
56     *shift = s;
57     *len = l;
58 }
59
60 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
61 {
62     dib->red_mask    = bit_fields[0];
63     dib->green_mask  = bit_fields[1];
64     dib->blue_mask   = bit_fields[2];
65     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
66     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
67     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
68 }
69
70 static void init_dib_info(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
71                           const RGBQUAD *color_table, void *bits)
72 {
73     dib->bit_count    = bi->biBitCount;
74     dib->width        = bi->biWidth;
75     dib->height       = bi->biHeight;
76     dib->rect.left    = 0;
77     dib->rect.top     = 0;
78     dib->rect.right   = bi->biWidth;
79     dib->rect.bottom  = abs( bi->biHeight );
80     dib->compression  = bi->biCompression;
81     dib->stride       = get_dib_stride( dib->width, dib->bit_count );
82     dib->bits.ptr     = bits;
83     dib->bits.is_copy = FALSE;
84     dib->bits.free    = NULL;
85     dib->bits.param   = NULL;
86
87     if(dib->height < 0) /* top-down */
88     {
89         dib->height = -dib->height;
90     }
91     else /* bottom-up */
92     {
93         /* bits always points to the top-left corner and the stride is -ve */
94         dib->bits.ptr = (BYTE*)dib->bits.ptr + (dib->height - 1) * dib->stride;
95         dib->stride  = -dib->stride;
96     }
97
98     dib->funcs = &funcs_null;
99
100     switch(dib->bit_count)
101     {
102     case 32:
103         if(bi->biCompression == BI_RGB)
104             bit_fields = bit_fields_888;
105
106         init_bit_fields(dib, bit_fields);
107
108         if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
109             dib->funcs = &funcs_8888;
110         else
111             dib->funcs = &funcs_32;
112         break;
113
114     case 24:
115         dib->funcs = &funcs_24;
116         break;
117
118     case 16:
119         if(bi->biCompression == BI_RGB)
120             bit_fields = bit_fields_555;
121
122         init_bit_fields(dib, bit_fields);
123
124         if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
125             dib->funcs = &funcs_555;
126         else
127             dib->funcs = &funcs_16;
128         break;
129
130     case 8:
131         dib->funcs = &funcs_8;
132         break;
133
134     case 4:
135         dib->funcs = &funcs_4;
136         break;
137
138     case 1:
139         dib->funcs = &funcs_1;
140         break;
141     }
142
143     if (color_table && bi->biClrUsed)
144     {
145         dib->color_table = color_table;
146         dib->color_table_size = bi->biClrUsed;
147     }
148     else
149     {
150         dib->color_table = NULL;
151         dib->color_table_size = 0;
152     }
153 }
154
155 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits)
156 {
157     init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits );
158 }
159
160 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp)
161 {
162     if (!is_bitmapobj_dib( bmp ))
163     {
164         BITMAPINFO info;
165
166         get_ddb_bitmapinfo( bmp, &info );
167         if (!bmp->dib.dsBm.bmBits)
168         {
169             int width_bytes = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
170             bmp->dib.dsBm.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
171                                               bmp->dib.dsBm.bmHeight * width_bytes );
172             if (!bmp->dib.dsBm.bmBits) return FALSE;
173         }
174         init_dib_info_from_bitmapinfo( dib, &info, bmp->dib.dsBm.bmBits );
175     }
176     else init_dib_info( dib, &bmp->dib.dsBmih, bmp->dib.dsBitfields,
177                         bmp->color_table, bmp->dib.dsBm.bmBits );
178     return TRUE;
179 }
180
181 static void clear_dib_info(dib_info *dib)
182 {
183     dib->bits.ptr    = NULL;
184     dib->bits.free   = NULL;
185     dib->bits.param  = NULL;
186 }
187
188 /**********************************************************************
189  *      free_dib_info
190  *
191  * Free the resources associated with a dib and optionally the bits
192  */
193 void free_dib_info(dib_info *dib)
194 {
195     if (dib->bits.free) dib->bits.free( &dib->bits );
196     clear_dib_info( dib );
197 }
198
199 void copy_dib_color_info(dib_info *dst, const dib_info *src)
200 {
201     dst->bit_count        = src->bit_count;
202     dst->red_mask         = src->red_mask;
203     dst->green_mask       = src->green_mask;
204     dst->blue_mask        = src->blue_mask;
205     dst->red_len          = src->red_len;
206     dst->green_len        = src->green_len;
207     dst->blue_len         = src->blue_len;
208     dst->red_shift        = src->red_shift;
209     dst->green_shift      = src->green_shift;
210     dst->blue_shift       = src->blue_shift;
211     dst->funcs            = src->funcs;
212     dst->color_table_size = src->color_table_size;
213     dst->color_table      = src->color_table;
214 }
215
216 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
217                           const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
218 {
219     dib_info src_dib, dst_dib;
220     DWORD ret;
221
222     init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits );
223     init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits );
224
225     __TRY
226     {
227         dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect, FALSE );
228         ret = TRUE;
229     }
230     __EXCEPT_PAGE_FAULT
231     {
232         WARN( "invalid bits pointer %p\n", src_bits );
233         ret = FALSE;
234     }
235     __ENDTRY
236
237     /* We shared the color tables, so there's no need to free the dib_infos here */
238     if(!ret) return ERROR_BAD_FORMAT;
239
240     /* update coordinates, the destination rectangle is always stored at 0,0 */
241     src->x -= src->visrect.left;
242     src->y -= src->visrect.top;
243     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
244
245     if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
246     {
247         DWORD *pixel = dst_dib.bits.ptr;
248         int x, y;
249
250         for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
251             for (x = src->visrect.left; x < src->visrect.right; x++)
252                 pixel[x] |= 0xff000000;
253     }
254
255     return ERROR_SUCCESS;
256 }
257
258 int clip_rect_to_dib( const dib_info *dib, RECT *rc )
259 {
260     RECT rect;
261
262     rect.left   = max( 0, -dib->rect.left );
263     rect.top    = max( 0, -dib->rect.top );
264     rect.right  = min( dib->rect.right, dib->width ) - dib->rect.left;
265     rect.bottom = min( dib->rect.bottom, dib->height ) - dib->rect.top;
266     if (is_rect_empty( &rect )) return 0;
267     return intersect_rect( rc, &rect, rc );
268 }
269
270 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
271 {
272     const WINEREGION *region;
273     RECT rect, *out = clip_rects->buffer;
274     int i;
275
276     init_clipped_rects( clip_rects );
277
278     rect.left   = max( 0, -dib->rect.left );
279     rect.top    = max( 0, -dib->rect.top );
280     rect.right  = min( dib->rect.right, dib->width ) - dib->rect.left;
281     rect.bottom = min( dib->rect.bottom, dib->height ) - dib->rect.top;
282     if (is_rect_empty( &rect )) return 0;
283     if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
284
285     if (!clip)
286     {
287         *out = rect;
288         clip_rects->count = 1;
289         return 1;
290     }
291
292     if (!(region = get_wine_region( clip ))) return 0;
293
294     for (i = 0; i < region->numRects; i++)
295     {
296         if (region->rects[i].top >= rect.bottom) break;
297         if (!intersect_rect( out, &rect, &region->rects[i] )) continue;
298         out++;
299         if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
300         {
301             clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
302             if (!clip_rects->rects) return 0;
303             memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
304             out = clip_rects->rects + (out - clip_rects->buffer);
305         }
306     }
307     release_wine_region( clip );
308     clip_rects->count = out - clip_rects->rects;
309     return clip_rects->count;
310 }
311
312 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
313 {
314     const WINEREGION *region;
315     RECT rc;
316
317     if (!dev->bounds) return;
318     if (clip)
319     {
320         if (!(region = get_wine_region( clip ))) return;
321         if (!rect) rc = region->extents;
322         else intersect_rect( &rc, rect, &region->extents );
323         release_wine_region( clip );
324     }
325     else rc = *rect;
326
327     if (is_rect_empty( &rc )) return;
328     offset_rect( &rc, dev->dib.rect.left, dev->dib.rect.top );
329     add_bounds_rect( dev->bounds, &rc );
330 }
331
332 /**********************************************************************
333  *           dibdrv_CreateDC
334  */
335 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
336                              LPCWSTR output, const DEVMODEW *data )
337 {
338     dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
339
340     if (!pdev) return FALSE;
341     clear_dib_info(&pdev->dib);
342     clear_dib_info(&pdev->brush.dib);
343     clear_dib_info(&pdev->pen_brush.dib);
344     push_dc_driver( dev, &pdev->dev, &dib_driver );
345     return TRUE;
346 }
347
348 /***********************************************************************
349  *           dibdrv_DeleteDC
350  */
351 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
352 {
353     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
354     TRACE("(%p)\n", dev);
355     free_pattern_brush( &pdev->brush );
356     free_pattern_brush( &pdev->pen_brush );
357     HeapFree( GetProcessHeap(), 0, pdev );
358     return TRUE;
359 }
360
361 /***********************************************************************
362  *           dibdrv_SelectBitmap
363  */
364 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
365 {
366     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
367     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
368     dib_info dib;
369
370     TRACE("(%p, %p)\n", dev, bitmap);
371
372     if (!bmp) return 0;
373
374     if (!init_dib_info_from_bitmapobj(&dib, bmp))
375     {
376         GDI_ReleaseObj( bitmap );
377         return 0;
378     }
379     pdev->dib = dib;
380     GDI_ReleaseObj( bitmap );
381
382     return bitmap;
383 }
384
385 /***********************************************************************
386  *           dibdrv_SetDeviceClipping
387  */
388 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
389 {
390     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
391     TRACE("(%p, %p)\n", dev, rgn);
392
393     pdev->clip = rgn;
394 }
395
396 /***********************************************************************
397  *           dibdrv_SetBoundsRect
398  */
399 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
400 {
401     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
402
403     if (flags & DCB_DISABLE) pdev->bounds = NULL;
404     else if (flags & DCB_ENABLE) pdev->bounds = rect;
405     return DCB_RESET;  /* we don't have device-specific bounds */
406 }
407
408 const struct gdi_dc_funcs dib_driver =
409 {
410     NULL,                               /* pAbortDoc */
411     NULL,                               /* pAbortPath */
412     dibdrv_AlphaBlend,                  /* pAlphaBlend */
413     NULL,                               /* pAngleArc */
414     dibdrv_Arc,                         /* pArc */
415     dibdrv_ArcTo,                       /* pArcTo */
416     NULL,                               /* pBeginPath */
417     dibdrv_BlendImage,                  /* pBlendImage */
418     dibdrv_Chord,                       /* pChord */
419     NULL,                               /* pCloseFigure */
420     NULL,                               /* pCreateCompatibleDC */
421     dibdrv_CreateDC,                    /* pCreateDC */
422     dibdrv_DeleteDC,                    /* pDeleteDC */
423     NULL,                               /* pDeleteObject */
424     NULL,                               /* pDeviceCapabilities */
425     dibdrv_Ellipse,                     /* pEllipse */
426     NULL,                               /* pEndDoc */
427     NULL,                               /* pEndPage */
428     NULL,                               /* pEndPath */
429     NULL,                               /* pEnumFonts */
430     NULL,                               /* pEnumICMProfiles */
431     NULL,                               /* pExcludeClipRect */
432     NULL,                               /* pExtDeviceMode */
433     NULL,                               /* pExtEscape */
434     dibdrv_ExtFloodFill,                /* pExtFloodFill */
435     NULL,                               /* pExtSelectClipRgn */
436     dibdrv_ExtTextOut,                  /* pExtTextOut */
437     NULL,                               /* pFillPath */
438     NULL,                               /* pFillRgn */
439     NULL,                               /* pFlattenPath */
440     NULL,                               /* pFontIsLinked */
441     NULL,                               /* pFrameRgn */
442     NULL,                               /* pGdiComment */
443     NULL,                               /* pGdiRealizationInfo */
444     NULL,                               /* pGetBoundsRect */
445     NULL,                               /* pGetCharABCWidths */
446     NULL,                               /* pGetCharABCWidthsI */
447     NULL,                               /* pGetCharWidth */
448     NULL,                               /* pGetDeviceCaps */
449     NULL,                               /* pGetDeviceGammaRamp */
450     NULL,                               /* pGetFontData */
451     NULL,                               /* pGetFontUnicodeRanges */
452     NULL,                               /* pGetGlyphIndices */
453     NULL,                               /* pGetGlyphOutline */
454     NULL,                               /* pGetICMProfile */
455     dibdrv_GetImage,                    /* pGetImage */
456     NULL,                               /* pGetKerningPairs */
457     dibdrv_GetNearestColor,             /* pGetNearestColor */
458     NULL,                               /* pGetOutlineTextMetrics */
459     dibdrv_GetPixel,                    /* pGetPixel */
460     NULL,                               /* pGetSystemPaletteEntries */
461     NULL,                               /* pGetTextCharsetInfo */
462     NULL,                               /* pGetTextExtentExPoint */
463     NULL,                               /* pGetTextExtentExPointI */
464     NULL,                               /* pGetTextFace */
465     NULL,                               /* pGetTextMetrics */
466     dibdrv_GradientFill,                /* pGradientFill */
467     NULL,                               /* pIntersectClipRect */
468     NULL,                               /* pInvertRgn */
469     dibdrv_LineTo,                      /* pLineTo */
470     NULL,                               /* pModifyWorldTransform */
471     NULL,                               /* pMoveTo */
472     NULL,                               /* pOffsetClipRgn */
473     NULL,                               /* pOffsetViewportOrg */
474     NULL,                               /* pOffsetWindowOrg */
475     dibdrv_PaintRgn,                    /* pPaintRgn */
476     dibdrv_PatBlt,                      /* pPatBlt */
477     dibdrv_Pie,                         /* pPie */
478     NULL,                               /* pPolyBezier */
479     NULL,                               /* pPolyBezierTo */
480     NULL,                               /* pPolyDraw */
481     dibdrv_PolyPolygon,                 /* pPolyPolygon */
482     dibdrv_PolyPolyline,                /* pPolyPolyline */
483     dibdrv_Polygon,                     /* pPolygon */
484     dibdrv_Polyline,                    /* pPolyline */
485     NULL,                               /* pPolylineTo */
486     dibdrv_PutImage,                    /* pPutImage */
487     NULL,                               /* pRealizeDefaultPalette */
488     NULL,                               /* pRealizePalette */
489     dibdrv_Rectangle,                   /* pRectangle */
490     NULL,                               /* pResetDC */
491     NULL,                               /* pRestoreDC */
492     dibdrv_RoundRect,                   /* pRoundRect */
493     NULL,                               /* pSaveDC */
494     NULL,                               /* pScaleViewportExt */
495     NULL,                               /* pScaleWindowExt */
496     dibdrv_SelectBitmap,                /* pSelectBitmap */
497     dibdrv_SelectBrush,                 /* pSelectBrush */
498     NULL,                               /* pSelectClipPath */
499     NULL,                               /* pSelectFont */
500     NULL,                               /* pSelectPalette */
501     dibdrv_SelectPen,                   /* pSelectPen */
502     NULL,                               /* pSetArcDirection */
503     NULL,                               /* pSetBkColor */
504     NULL,                               /* pSetBkMode */
505     dibdrv_SetBoundsRect,               /* pSetBoundsRect */
506     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
507     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
508     NULL,                               /* pSetDIBitsToDevice */
509     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
510     NULL,                               /* pSetDeviceGammaRamp */
511     NULL,                               /* pSetLayout */
512     NULL,                               /* pSetMapMode */
513     NULL,                               /* pSetMapperFlags */
514     dibdrv_SetPixel,                    /* pSetPixel */
515     NULL,                               /* pSetPolyFillMode */
516     NULL,                               /* pSetROP2 */
517     NULL,                               /* pSetRelAbs */
518     NULL,                               /* pSetStretchBltMode */
519     NULL,                               /* pSetTextAlign */
520     NULL,                               /* pSetTextCharacterExtra */
521     NULL,                               /* pSetTextColor */
522     NULL,                               /* pSetTextJustification */
523     NULL,                               /* pSetViewportExt */
524     NULL,                               /* pSetViewportOrg */
525     NULL,                               /* pSetWindowExt */
526     NULL,                               /* pSetWindowOrg */
527     NULL,                               /* pSetWorldTransform */
528     NULL,                               /* pStartDoc */
529     NULL,                               /* pStartPage */
530     dibdrv_StretchBlt,                  /* pStretchBlt */
531     NULL,                               /* pStretchDIBits */
532     NULL,                               /* pStrokeAndFillPath */
533     NULL,                               /* pStrokePath */
534     NULL,                               /* pSwapBuffers */
535     NULL,                               /* pUnrealizePalette */
536     NULL,                               /* pWidenPath */
537     dibdrv_wine_get_wgl_driver,         /* wine_get_wgl_driver */
538     GDI_PRIORITY_DIB_DRV                /* priority */
539 };