d3drm: Implement IDirect3DRMMesh_AddGroup.
[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, enum dib_info_flags flags)
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 if (flags & default_color_table)
149     {
150         dib->color_table = get_default_color_table( dib->bit_count );
151         dib->color_table_size = dib->color_table ? (1 << dib->bit_count) : 0;
152     }
153     else
154     {
155         dib->color_table = NULL;
156         dib->color_table_size = 0;
157     }
158 }
159
160 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
161 {
162     init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits, flags );
163 }
164
165 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp, enum dib_info_flags flags)
166 {
167     if (!is_bitmapobj_dib( bmp ))
168     {
169         BITMAPINFO info;
170
171         get_ddb_bitmapinfo( bmp, &info );
172         if (!bmp->dib.dsBm.bmBits)
173         {
174             int width_bytes = get_dib_stride( bmp->dib.dsBm.bmWidth, bmp->dib.dsBm.bmBitsPixel );
175             bmp->dib.dsBm.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
176                                               bmp->dib.dsBm.bmHeight * width_bytes );
177             if (!bmp->dib.dsBm.bmBits) return FALSE;
178         }
179         init_dib_info_from_bitmapinfo( dib, &info, bmp->dib.dsBm.bmBits, flags );
180     }
181     else init_dib_info( dib, &bmp->dib.dsBmih, bmp->dib.dsBitfields,
182                         bmp->color_table, bmp->dib.dsBm.bmBits, flags );
183     return TRUE;
184 }
185
186 static void clear_dib_info(dib_info *dib)
187 {
188     dib->color_table = NULL;
189     dib->bits.ptr    = NULL;
190     dib->bits.free   = NULL;
191     dib->bits.param  = NULL;
192 }
193
194 /**********************************************************************
195  *      free_dib_info
196  *
197  * Free the resources associated with a dib and optionally the bits
198  */
199 void free_dib_info(dib_info *dib)
200 {
201     if (dib->bits.free) dib->bits.free( &dib->bits );
202     clear_dib_info( dib );
203 }
204
205 void copy_dib_color_info(dib_info *dst, const dib_info *src)
206 {
207     dst->bit_count        = src->bit_count;
208     dst->red_mask         = src->red_mask;
209     dst->green_mask       = src->green_mask;
210     dst->blue_mask        = src->blue_mask;
211     dst->red_len          = src->red_len;
212     dst->green_len        = src->green_len;
213     dst->blue_len         = src->blue_len;
214     dst->red_shift        = src->red_shift;
215     dst->green_shift      = src->green_shift;
216     dst->blue_shift       = src->blue_shift;
217     dst->funcs            = src->funcs;
218     dst->color_table_size = src->color_table_size;
219     dst->color_table      = src->color_table;
220 }
221
222 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
223                           const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
224 {
225     dib_info src_dib, dst_dib;
226     DWORD ret;
227
228     init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, default_color_table );
229     init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, default_color_table );
230
231     __TRY
232     {
233         dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
234         ret = TRUE;
235     }
236     __EXCEPT_PAGE_FAULT
237     {
238         WARN( "invalid bits pointer %p\n", src_bits );
239         ret = FALSE;
240     }
241     __ENDTRY
242
243     /* We shared the color tables, so there's no need to free the dib_infos here */
244     if(!ret) return ERROR_BAD_FORMAT;
245
246     /* update coordinates, the destination rectangle is always stored at 0,0 */
247     src->x -= src->visrect.left;
248     src->y -= src->visrect.top;
249     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
250
251     if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
252     {
253         DWORD *pixel = dst_dib.bits.ptr;
254         int x, y;
255
256         for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
257             for (x = src->visrect.left; x < src->visrect.right; x++)
258                 pixel[x] |= 0xff000000;
259     }
260
261     return ERROR_SUCCESS;
262 }
263
264 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
265 {
266     const WINEREGION *region;
267     RECT rect, *out = clip_rects->buffer;
268     int i;
269
270     init_clipped_rects( clip_rects );
271
272     rect.left   = 0;
273     rect.top    = 0;
274     rect.right  = dib->rect.right - dib->rect.left;
275     rect.bottom = dib->rect.bottom - dib->rect.top;
276     if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
277
278     if (!clip)
279     {
280         *out = rect;
281         clip_rects->count = 1;
282         return 1;
283     }
284
285     if (!(region = get_wine_region( clip ))) return 0;
286
287     for (i = 0; i < region->numRects; i++)
288     {
289         if (region->rects[i].top >= rect.bottom) break;
290         if (!intersect_rect( out, &rect, &region->rects[i] )) continue;
291         out++;
292         if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
293         {
294             clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
295             if (!clip_rects->rects) return 0;
296             memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
297             out = clip_rects->rects + (out - clip_rects->buffer);
298         }
299     }
300     release_wine_region( clip );
301     clip_rects->count = out - clip_rects->rects;
302     return clip_rects->count;
303 }
304
305 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
306 {
307     const WINEREGION *region;
308     RECT rc;
309
310     if (!dev->bounds) return;
311     if (clip)
312     {
313         if (!(region = get_wine_region( clip ))) return;
314         if (!rect) rc = region->extents;
315         else intersect_rect( &rc, rect, &region->extents );
316         release_wine_region( clip );
317     }
318     else rc = *rect;
319
320     if (is_rect_empty( &rc )) return;
321     offset_rect( &rc, dev->dib.rect.left, dev->dib.rect.top );
322     add_bounds_rect( dev->bounds, &rc );
323 }
324
325 /**********************************************************************
326  *           dibdrv_CreateDC
327  */
328 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
329                              LPCWSTR output, const DEVMODEW *data )
330 {
331     dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
332
333     if (!pdev) return FALSE;
334     clear_dib_info(&pdev->dib);
335     clear_dib_info(&pdev->brush.dib);
336     clear_dib_info(&pdev->pen_brush.dib);
337     push_dc_driver( dev, &pdev->dev, &dib_driver );
338     return TRUE;
339 }
340
341 /***********************************************************************
342  *           dibdrv_DeleteDC
343  */
344 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
345 {
346     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
347     TRACE("(%p)\n", dev);
348     free_pattern_brush( &pdev->brush );
349     free_pattern_brush( &pdev->pen_brush );
350     HeapFree( GetProcessHeap(), 0, pdev );
351     return TRUE;
352 }
353
354 /***********************************************************************
355  *           dibdrv_CopyBitmap
356  */
357 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
358 {
359     return nulldrv_CopyBitmap( src, dst );
360 }
361
362 /***********************************************************************
363  *           dibdrv_DeleteBitmap
364  */
365 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
366 {
367     return TRUE;
368 }
369
370 /***********************************************************************
371  *           dibdrv_SelectBitmap
372  */
373 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
374 {
375     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
376     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
377     dib_info dib;
378
379     TRACE("(%p, %p)\n", dev, bitmap);
380
381     if (!bmp) return 0;
382
383     if(!init_dib_info_from_bitmapobj(&dib, bmp, default_color_table))
384     {
385         GDI_ReleaseObj( bitmap );
386         return 0;
387     }
388     pdev->dib = dib;
389     GDI_ReleaseObj( bitmap );
390
391     return bitmap;
392 }
393
394 /***********************************************************************
395  *           dibdrv_SetDeviceClipping
396  */
397 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
398 {
399     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
400     TRACE("(%p, %p)\n", dev, rgn);
401
402     pdev->clip = rgn;
403 }
404
405 /***********************************************************************
406  *           dibdrv_SetBoundsRect
407  */
408 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
409 {
410     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
411
412     if (flags & DCB_DISABLE) pdev->bounds = NULL;
413     else if (flags & DCB_ENABLE) pdev->bounds = rect;
414     return DCB_RESET;  /* we don't have device-specific bounds */
415 }
416
417 /***********************************************************************
418  *           dibdrv_GetDeviceGammaRamp
419  */
420 static BOOL dibdrv_GetDeviceGammaRamp( PHYSDEV dev, void *ramp )
421 {
422     SetLastError( ERROR_INVALID_PARAMETER );
423     return FALSE;
424 }
425
426 /***********************************************************************
427  *           dibdrv_SetDeviceGammaRamp
428  */
429 static BOOL dibdrv_SetDeviceGammaRamp( PHYSDEV dev, void *ramp )
430 {
431     SetLastError( ERROR_INVALID_PARAMETER );
432     return FALSE;
433 }
434
435 const struct gdi_dc_funcs dib_driver =
436 {
437     NULL,                               /* pAbortDoc */
438     NULL,                               /* pAbortPath */
439     dibdrv_AlphaBlend,                  /* pAlphaBlend */
440     NULL,                               /* pAngleArc */
441     dibdrv_Arc,                         /* pArc */
442     dibdrv_ArcTo,                       /* pArcTo */
443     NULL,                               /* pBeginPath */
444     dibdrv_BlendImage,                  /* pBlendImage */
445     NULL,                               /* pChoosePixelFormat */
446     dibdrv_Chord,                       /* pChord */
447     NULL,                               /* pCloseFigure */
448     dibdrv_CopyBitmap,                  /* pCopyBitmap */
449     NULL,                               /* pCreateBitmap */
450     NULL,                               /* pCreateCompatibleDC */
451     dibdrv_CreateDC,                    /* pCreateDC */
452     dibdrv_DeleteBitmap,                /* pDeleteBitmap */
453     dibdrv_DeleteDC,                    /* pDeleteDC */
454     NULL,                               /* pDeleteObject */
455     NULL,                               /* pDescribePixelFormat */
456     NULL,                               /* pDeviceCapabilities */
457     dibdrv_Ellipse,                     /* pEllipse */
458     NULL,                               /* pEndDoc */
459     NULL,                               /* pEndPage */
460     NULL,                               /* pEndPath */
461     NULL,                               /* pEnumFonts */
462     NULL,                               /* pEnumICMProfiles */
463     NULL,                               /* pExcludeClipRect */
464     NULL,                               /* pExtDeviceMode */
465     NULL,                               /* pExtEscape */
466     dibdrv_ExtFloodFill,                /* pExtFloodFill */
467     NULL,                               /* pExtSelectClipRgn */
468     dibdrv_ExtTextOut,                  /* pExtTextOut */
469     NULL,                               /* pFillPath */
470     NULL,                               /* pFillRgn */
471     NULL,                               /* pFlattenPath */
472     NULL,                               /* pFontIsLinked */
473     NULL,                               /* pFrameRgn */
474     NULL,                               /* pGdiComment */
475     NULL,                               /* pGdiRealizationInfo */
476     NULL,                               /* pGetBoundsRect */
477     NULL,                               /* pGetCharABCWidths */
478     NULL,                               /* pGetCharABCWidthsI */
479     NULL,                               /* pGetCharWidth */
480     NULL,                               /* pGetDeviceCaps */
481     dibdrv_GetDeviceGammaRamp,          /* pGetDeviceGammaRamp */
482     NULL,                               /* pGetFontData */
483     NULL,                               /* pGetFontUnicodeRanges */
484     NULL,                               /* pGetGlyphIndices */
485     NULL,                               /* pGetGlyphOutline */
486     NULL,                               /* pGetICMProfile */
487     dibdrv_GetImage,                    /* pGetImage */
488     NULL,                               /* pGetKerningPairs */
489     dibdrv_GetNearestColor,             /* pGetNearestColor */
490     NULL,                               /* pGetOutlineTextMetrics */
491     dibdrv_GetPixel,                    /* pGetPixel */
492     NULL,                               /* pGetPixelFormat */
493     NULL,                               /* pGetSystemPaletteEntries */
494     NULL,                               /* pGetTextCharsetInfo */
495     NULL,                               /* pGetTextExtentExPoint */
496     NULL,                               /* pGetTextExtentExPointI */
497     NULL,                               /* pGetTextFace */
498     NULL,                               /* pGetTextMetrics */
499     dibdrv_GradientFill,                /* pGradientFill */
500     NULL,                               /* pIntersectClipRect */
501     NULL,                               /* pInvertRgn */
502     dibdrv_LineTo,                      /* pLineTo */
503     NULL,                               /* pModifyWorldTransform */
504     NULL,                               /* pMoveTo */
505     NULL,                               /* pOffsetClipRgn */
506     NULL,                               /* pOffsetViewportOrg */
507     NULL,                               /* pOffsetWindowOrg */
508     dibdrv_PaintRgn,                    /* pPaintRgn */
509     dibdrv_PatBlt,                      /* pPatBlt */
510     dibdrv_Pie,                         /* pPie */
511     NULL,                               /* pPolyBezier */
512     NULL,                               /* pPolyBezierTo */
513     NULL,                               /* pPolyDraw */
514     dibdrv_PolyPolygon,                 /* pPolyPolygon */
515     dibdrv_PolyPolyline,                /* pPolyPolyline */
516     dibdrv_Polygon,                     /* pPolygon */
517     dibdrv_Polyline,                    /* pPolyline */
518     NULL,                               /* pPolylineTo */
519     dibdrv_PutImage,                    /* pPutImage */
520     NULL,                               /* pRealizeDefaultPalette */
521     NULL,                               /* pRealizePalette */
522     dibdrv_Rectangle,                   /* pRectangle */
523     NULL,                               /* pResetDC */
524     NULL,                               /* pRestoreDC */
525     dibdrv_RoundRect,                   /* pRoundRect */
526     NULL,                               /* pSaveDC */
527     NULL,                               /* pScaleViewportExt */
528     NULL,                               /* pScaleWindowExt */
529     dibdrv_SelectBitmap,                /* pSelectBitmap */
530     dibdrv_SelectBrush,                 /* pSelectBrush */
531     NULL,                               /* pSelectClipPath */
532     NULL,                               /* pSelectFont */
533     NULL,                               /* pSelectPalette */
534     dibdrv_SelectPen,                   /* pSelectPen */
535     NULL,                               /* pSetArcDirection */
536     NULL,                               /* pSetBkColor */
537     NULL,                               /* pSetBkMode */
538     dibdrv_SetBoundsRect,               /* pSetBoundsRect */
539     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
540     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
541     NULL,                               /* pSetDIBitsToDevice */
542     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
543     dibdrv_SetDeviceGammaRamp,          /* pSetDeviceGammaRamp */
544     NULL,                               /* pSetLayout */
545     NULL,                               /* pSetMapMode */
546     NULL,                               /* pSetMapperFlags */
547     dibdrv_SetPixel,                    /* pSetPixel */
548     NULL,                               /* pSetPixelFormat */
549     NULL,                               /* pSetPolyFillMode */
550     NULL,                               /* pSetROP2 */
551     NULL,                               /* pSetRelAbs */
552     NULL,                               /* pSetStretchBltMode */
553     NULL,                               /* pSetTextAlign */
554     NULL,                               /* pSetTextCharacterExtra */
555     NULL,                               /* pSetTextColor */
556     NULL,                               /* pSetTextJustification */
557     NULL,                               /* pSetViewportExt */
558     NULL,                               /* pSetViewportOrg */
559     NULL,                               /* pSetWindowExt */
560     NULL,                               /* pSetWindowOrg */
561     NULL,                               /* pSetWorldTransform */
562     NULL,                               /* pStartDoc */
563     NULL,                               /* pStartPage */
564     dibdrv_StretchBlt,                  /* pStretchBlt */
565     NULL,                               /* pStretchDIBits */
566     NULL,                               /* pStrokeAndFillPath */
567     NULL,                               /* pStrokePath */
568     NULL,                               /* pSwapBuffers */
569     NULL,                               /* pUnrealizePalette */
570     NULL,                               /* pWidenPath */
571     NULL,                               /* pwglCopyContext */
572     NULL,                               /* pwglCreateContext */
573     NULL,                               /* pwglCreateContextAttribsARB */
574     NULL,                               /* pwglDeleteContext */
575     NULL,                               /* pwglGetProcAddress */
576     NULL,                               /* pwglMakeContextCurrentARB */
577     NULL,                               /* pwglMakeCurrent */
578     NULL,                               /* pwglSetPixelFormatWINE */
579     NULL,                               /* pwglShareLists */
580     NULL,                               /* pwglUseFontBitmapsA */
581     NULL,                               /* pwglUseFontBitmapsW */
582     GDI_PRIORITY_DIB_DRV                /* priority */
583 };