gdi32: Re-create the brush bits only when the ROP has really changed.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / dc.c
blob7001b464611822a3b07c4ed8dfdc0cb737d03e77
1 /*
2 * DIB driver initialization and DC functions.
4 * Copyright 2011 Huw Davies
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.
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.
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
21 #include <assert.h>
23 #include "gdi_private.h"
24 #include "dibdrv.h"
26 #include "wine/exception.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
31 static const DWORD bit_fields_888[3] = {0xff0000, 0x00ff00, 0x0000ff};
32 static const DWORD bit_fields_555[3] = {0x7c00, 0x03e0, 0x001f};
34 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
36 int s, l;
38 if(!mask)
40 *shift = *len = 0;
41 return;
44 s = 0;
45 while ((mask & 1) == 0)
47 mask >>= 1;
48 s++;
50 l = 0;
51 while ((mask & 1) == 1)
53 mask >>= 1;
54 l++;
56 *shift = s;
57 *len = l;
60 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
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);
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)
73 dib->bit_count = bi->biBitCount;
74 dib->width = bi->biWidth;
75 dib->height = bi->biHeight;
76 dib->compression = bi->biCompression;
77 dib->stride = get_dib_stride( dib->width, dib->bit_count );
78 dib->bits.ptr = bits;
79 dib->bits.is_copy = FALSE;
80 dib->bits.free = NULL;
81 dib->bits.param = NULL;
83 if(dib->height < 0) /* top-down */
85 dib->height = -dib->height;
87 else /* bottom-up */
89 /* bits always points to the top-left corner and the stride is -ve */
90 dib->bits.ptr = (BYTE*)dib->bits.ptr + (dib->height - 1) * dib->stride;
91 dib->stride = -dib->stride;
94 dib->funcs = &funcs_null;
96 switch(dib->bit_count)
98 case 32:
99 if(bi->biCompression == BI_RGB)
100 bit_fields = bit_fields_888;
102 init_bit_fields(dib, bit_fields);
104 if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
105 dib->funcs = &funcs_8888;
106 else
107 dib->funcs = &funcs_32;
108 break;
110 case 24:
111 dib->funcs = &funcs_24;
112 break;
114 case 16:
115 if(bi->biCompression == BI_RGB)
116 bit_fields = bit_fields_555;
118 init_bit_fields(dib, bit_fields);
120 if(dib->red_mask == 0x7c00 && dib->green_mask == 0x03e0 && dib->blue_mask == 0x001f)
121 dib->funcs = &funcs_555;
122 else
123 dib->funcs = &funcs_16;
124 break;
126 case 8:
127 dib->funcs = &funcs_8;
128 break;
130 case 4:
131 dib->funcs = &funcs_4;
132 break;
134 case 1:
135 dib->funcs = &funcs_1;
136 break;
139 if (color_table && bi->biClrUsed)
141 dib->color_table = color_table;
142 dib->color_table_size = bi->biClrUsed;
144 else if (flags & default_color_table)
146 dib->color_table = get_default_color_table( dib->bit_count );
147 dib->color_table_size = dib->color_table ? (1 << dib->bit_count) : 0;
149 else
151 dib->color_table = NULL;
152 dib->color_table_size = 0;
156 void init_dib_info_from_bitmapinfo(dib_info *dib, const BITMAPINFO *info, void *bits, enum dib_info_flags flags)
158 init_dib_info( dib, &info->bmiHeader, (const DWORD *)info->bmiColors, info->bmiColors, bits, flags );
161 BOOL init_dib_info_from_bitmapobj(dib_info *dib, BITMAPOBJ *bmp, enum dib_info_flags flags)
163 if (!bmp->dib)
165 BITMAPINFO info;
167 get_ddb_bitmapinfo( bmp, &info );
168 if (!bmp->bitmap.bmBits)
170 int width_bytes = get_dib_stride( bmp->bitmap.bmWidth, bmp->bitmap.bmBitsPixel );
171 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
172 bmp->bitmap.bmHeight * width_bytes );
173 if (!bmp->bitmap.bmBits) return FALSE;
175 init_dib_info_from_bitmapinfo( dib, &info, bmp->bitmap.bmBits, flags );
177 else init_dib_info( dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields,
178 bmp->color_table, bmp->dib->dsBm.bmBits, flags );
179 return TRUE;
182 static void clear_dib_info(dib_info *dib)
184 dib->color_table = NULL;
185 dib->bits.ptr = NULL;
186 dib->bits.free = NULL;
187 dib->bits.param = NULL;
190 /**********************************************************************
191 * free_dib_info
193 * Free the resources associated with a dib and optionally the bits
195 void free_dib_info(dib_info *dib)
197 if (dib->bits.free) dib->bits.free( &dib->bits );
198 clear_dib_info( dib );
201 void copy_dib_color_info(dib_info *dst, const dib_info *src)
203 dst->bit_count = src->bit_count;
204 dst->red_mask = src->red_mask;
205 dst->green_mask = src->green_mask;
206 dst->blue_mask = src->blue_mask;
207 dst->red_len = src->red_len;
208 dst->green_len = src->green_len;
209 dst->blue_len = src->blue_len;
210 dst->red_shift = src->red_shift;
211 dst->green_shift = src->green_shift;
212 dst->blue_shift = src->blue_shift;
213 dst->funcs = src->funcs;
214 dst->color_table_size = src->color_table_size;
215 dst->color_table = src->color_table;
218 DWORD convert_bitmapinfo( const BITMAPINFO *src_info, void *src_bits, struct bitblt_coords *src,
219 const BITMAPINFO *dst_info, void *dst_bits, BOOL add_alpha )
221 dib_info src_dib, dst_dib;
222 DWORD ret;
224 init_dib_info_from_bitmapinfo( &src_dib, src_info, src_bits, default_color_table );
225 init_dib_info_from_bitmapinfo( &dst_dib, dst_info, dst_bits, default_color_table );
227 __TRY
229 dst_dib.funcs->convert_to( &dst_dib, &src_dib, &src->visrect );
230 ret = TRUE;
232 __EXCEPT_PAGE_FAULT
234 WARN( "invalid bits pointer %p\n", src_bits );
235 ret = FALSE;
237 __ENDTRY
239 /* We shared the color tables, so there's no need to free the dib_infos here */
240 if(!ret) return ERROR_BAD_FORMAT;
242 /* update coordinates, the destination rectangle is always stored at 0,0 */
243 src->x -= src->visrect.left;
244 src->y -= src->visrect.top;
245 offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
247 if (add_alpha && dst_dib.funcs == &funcs_8888 && src_dib.funcs != &funcs_8888)
249 DWORD *pixel = dst_dib.bits.ptr;
250 int x, y;
252 for (y = src->visrect.top; y < src->visrect.bottom; y++, pixel += dst_dib.stride / 4)
253 for (x = src->visrect.left; x < src->visrect.right; x++)
254 pixel[x] |= 0xff000000;
257 return ERROR_SUCCESS;
260 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
262 const WINEREGION *region;
263 RECT rect, *out = clip_rects->buffer;
264 int i;
266 init_clipped_rects( clip_rects );
268 rect.left = 0;
269 rect.top = 0;
270 rect.right = dib->width;
271 rect.bottom = dib->height;
272 if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
274 if (!clip)
276 *out = rect;
277 clip_rects->count = 1;
278 return 1;
281 if (!(region = get_wine_region( clip ))) return 0;
283 for (i = 0; i < region->numRects; i++)
285 if (region->rects[i].top >= rect.bottom) break;
286 if (!intersect_rect( out, &rect, &region->rects[i] )) continue;
287 out++;
288 if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
290 clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
291 if (!clip_rects->rects) return 0;
292 memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
293 out = clip_rects->rects + (out - clip_rects->buffer);
296 release_wine_region( clip );
297 clip_rects->count = out - clip_rects->rects;
298 return clip_rects->count;
301 /**********************************************************************
302 * dibdrv_CreateDC
304 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
305 LPCWSTR output, const DEVMODEW *data )
307 dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
309 if (!pdev) return FALSE;
310 clear_dib_info(&pdev->dib);
311 clear_dib_info(&pdev->brush_dib);
312 push_dc_driver( dev, &pdev->dev, &dib_driver );
313 return TRUE;
316 /***********************************************************************
317 * dibdrv_DeleteDC
319 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
321 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
322 TRACE("(%p)\n", dev);
323 free_pattern_brush(pdev);
324 HeapFree( GetProcessHeap(), 0, pdev );
325 return TRUE;
328 /***********************************************************************
329 * dibdrv_CopyBitmap
331 static BOOL dibdrv_CopyBitmap( HBITMAP src, HBITMAP dst )
333 return nulldrv_CopyBitmap( src, dst );
336 /***********************************************************************
337 * dibdrv_DeleteBitmap
339 static BOOL dibdrv_DeleteBitmap( HBITMAP bitmap )
341 return TRUE;
344 /***********************************************************************
345 * dibdrv_SelectBitmap
347 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
349 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
350 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
351 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
352 dib_info dib;
354 TRACE("(%p, %p)\n", dev, bitmap);
356 if (!bmp) return 0;
358 if(!init_dib_info_from_bitmapobj(&dib, bmp, default_color_table))
360 GDI_ReleaseObj( bitmap );
361 return 0;
363 pdev->dib = dib;
364 pdev->defer = 0;
365 GDI_ReleaseObj( bitmap );
367 return next->funcs->pSelectBitmap( next, bitmap );
370 /***********************************************************************
371 * dibdrv_SetDeviceClipping
373 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
375 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
376 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
377 TRACE("(%p, %p)\n", dev, rgn);
379 pdev->clip = rgn;
380 return next->funcs->pSetDeviceClipping( next, rgn );
383 /***********************************************************************
384 * dibdrv_SetDIBColorTable
386 static UINT dibdrv_SetDIBColorTable( PHYSDEV dev, UINT pos, UINT count, const RGBQUAD *colors )
388 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDIBColorTable );
389 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
390 TRACE("(%p, %d, %d, %p)\n", dev, pos, count, colors);
392 if (pdev->dib.color_table) pdev->brush_rop = -1; /* force re-creating the brush bits */
394 return next->funcs->pSetDIBColorTable( next, pos, count, colors );
398 const struct gdi_dc_funcs dib_driver =
400 NULL, /* pAbortDoc */
401 NULL, /* pAbortPath */
402 dibdrv_AlphaBlend, /* pAlphaBlend */
403 NULL, /* pAngleArc */
404 NULL, /* pArc */
405 NULL, /* pArcTo */
406 NULL, /* pBeginPath */
407 dibdrv_BlendImage, /* pBlendImage */
408 NULL, /* pChoosePixelFormat */
409 NULL, /* pChord */
410 NULL, /* pCloseFigure */
411 dibdrv_CopyBitmap, /* pCopyBitmap */
412 NULL, /* pCreateBitmap */
413 NULL, /* pCreateCompatibleDC */
414 dibdrv_CreateDC, /* pCreateDC */
415 NULL, /* pCreateDIBSection */
416 dibdrv_DeleteBitmap, /* pDeleteBitmap */
417 dibdrv_DeleteDC, /* pDeleteDC */
418 NULL, /* pDeleteObject */
419 NULL, /* pDescribePixelFormat */
420 NULL, /* pDeviceCapabilities */
421 NULL, /* pEllipse */
422 NULL, /* pEndDoc */
423 NULL, /* pEndPage */
424 NULL, /* pEndPath */
425 NULL, /* pEnumFonts */
426 NULL, /* pEnumICMProfiles */
427 NULL, /* pExcludeClipRect */
428 NULL, /* pExtDeviceMode */
429 NULL, /* pExtEscape */
430 NULL, /* pExtFloodFill */
431 NULL, /* pExtSelectClipRgn */
432 dibdrv_ExtTextOut, /* pExtTextOut */
433 NULL, /* pFillPath */
434 NULL, /* pFillRgn */
435 NULL, /* pFlattenPath */
436 NULL, /* pFontIsLinked */
437 NULL, /* pFrameRgn */
438 NULL, /* pGdiComment */
439 NULL, /* pGdiRealizationInfo */
440 NULL, /* pGetCharABCWidths */
441 NULL, /* pGetCharABCWidthsI */
442 NULL, /* pGetCharWidth */
443 NULL, /* pGetDeviceCaps */
444 NULL, /* pGetDeviceGammaRamp */
445 NULL, /* pGetFontData */
446 NULL, /* pGetFontUnicodeRanges */
447 NULL, /* pGetGlyphIndices */
448 NULL, /* pGetGlyphOutline */
449 NULL, /* pGetICMProfile */
450 dibdrv_GetImage, /* pGetImage */
451 NULL, /* pGetKerningPairs */
452 dibdrv_GetNearestColor, /* pGetNearestColor */
453 NULL, /* pGetOutlineTextMetrics */
454 dibdrv_GetPixel, /* pGetPixel */
455 NULL, /* pGetPixelFormat */
456 NULL, /* pGetSystemPaletteEntries */
457 NULL, /* pGetTextCharsetInfo */
458 NULL, /* pGetTextExtentExPoint */
459 NULL, /* pGetTextExtentExPointI */
460 NULL, /* pGetTextFace */
461 NULL, /* pGetTextMetrics */
462 dibdrv_GradientFill, /* pGradientFill */
463 NULL, /* pIntersectClipRect */
464 NULL, /* pInvertRgn */
465 dibdrv_LineTo, /* pLineTo */
466 NULL, /* pModifyWorldTransform */
467 NULL, /* pMoveTo */
468 NULL, /* pOffsetClipRgn */
469 NULL, /* pOffsetViewportOrg */
470 NULL, /* pOffsetWindowOrg */
471 dibdrv_PaintRgn, /* pPaintRgn */
472 dibdrv_PatBlt, /* pPatBlt */
473 NULL, /* pPie */
474 NULL, /* pPolyBezier */
475 NULL, /* pPolyBezierTo */
476 NULL, /* pPolyDraw */
477 NULL, /* pPolyPolygon */
478 dibdrv_PolyPolyline, /* pPolyPolyline */
479 NULL, /* pPolygon */
480 dibdrv_Polyline, /* pPolyline */
481 NULL, /* pPolylineTo */
482 dibdrv_PutImage, /* pPutImage */
483 NULL, /* pRealizeDefaultPalette */
484 NULL, /* pRealizePalette */
485 dibdrv_Rectangle, /* pRectangle */
486 NULL, /* pResetDC */
487 NULL, /* pRestoreDC */
488 NULL, /* pRoundRect */
489 NULL, /* pSaveDC */
490 NULL, /* pScaleViewportExt */
491 NULL, /* pScaleWindowExt */
492 dibdrv_SelectBitmap, /* pSelectBitmap */
493 dibdrv_SelectBrush, /* pSelectBrush */
494 NULL, /* pSelectClipPath */
495 NULL, /* pSelectFont */
496 NULL, /* pSelectPalette */
497 dibdrv_SelectPen, /* pSelectPen */
498 NULL, /* pSetArcDirection */
499 NULL, /* pSetBkColor */
500 NULL, /* pSetBkMode */
501 dibdrv_SetDCBrushColor, /* pSetDCBrushColor */
502 dibdrv_SetDCPenColor, /* pSetDCPenColor */
503 dibdrv_SetDIBColorTable, /* pSetDIBColorTable */
504 NULL, /* pSetDIBitsToDevice */
505 dibdrv_SetDeviceClipping, /* pSetDeviceClipping */
506 NULL, /* pSetDeviceGammaRamp */
507 NULL, /* pSetLayout */
508 NULL, /* pSetMapMode */
509 NULL, /* pSetMapperFlags */
510 dibdrv_SetPixel, /* pSetPixel */
511 NULL, /* pSetPixelFormat */
512 NULL, /* pSetPolyFillMode */
513 NULL, /* pSetROP2 */
514 NULL, /* pSetRelAbs */
515 NULL, /* pSetStretchBltMode */
516 NULL, /* pSetTextAlign */
517 NULL, /* pSetTextCharacterExtra */
518 NULL, /* pSetTextColor */
519 NULL, /* pSetTextJustification */
520 NULL, /* pSetViewportExt */
521 NULL, /* pSetViewportOrg */
522 NULL, /* pSetWindowExt */
523 NULL, /* pSetWindowOrg */
524 NULL, /* pSetWorldTransform */
525 NULL, /* pStartDoc */
526 NULL, /* pStartPage */
527 dibdrv_StretchBlt, /* pStretchBlt */
528 NULL, /* pStretchDIBits */
529 NULL, /* pStrokeAndFillPath */
530 NULL, /* pStrokePath */
531 NULL, /* pSwapBuffers */
532 NULL, /* pUnrealizePalette */
533 NULL, /* pWidenPath */
534 NULL, /* pwglCopyContext */
535 NULL, /* pwglCreateContext */
536 NULL, /* pwglCreateContextAttribsARB */
537 NULL, /* pwglDeleteContext */
538 NULL, /* pwglGetPbufferDCARB */
539 NULL, /* pwglGetProcAddress */
540 NULL, /* pwglMakeContextCurrentARB */
541 NULL, /* pwglMakeCurrent */
542 NULL, /* pwglSetPixelFormatWINE */
543 NULL, /* pwglShareLists */
544 NULL, /* pwglUseFontBitmapsA */
545 NULL /* pwglUseFontBitmapsW */