mmdevapi/tests: Add tests for volume control interfaces.
[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 /***********************************************************************
31  *           dibdrv_DeleteDC
32  */
33 static BOOL CDECL dibdrv_DeleteDC( PHYSDEV dev )
34 {
35     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
36     TRACE("(%p)\n", dev);
37     DeleteObject(pdev->clip);
38     return 0;
39 }
40
41 static void calc_shift_and_len(DWORD mask, int *shift, int *len)
42 {
43     int s, l;
44
45     if(!mask)
46     {
47         *shift = *len = 0;
48         return;
49     }
50
51     s = 0;
52     while ((mask & 1) == 0)
53     {
54         mask >>= 1;
55         s++;
56     }
57     l = 0;
58     while ((mask & 1) == 1)
59     {
60         mask >>= 1;
61         l++;
62     }
63     *shift = s;
64     *len = l;
65 }
66
67 static void init_bit_fields(dib_info *dib, const DWORD *bit_fields)
68 {
69     dib->red_mask    = bit_fields[0];
70     dib->green_mask  = bit_fields[1];
71     dib->blue_mask   = bit_fields[2];
72     calc_shift_and_len(dib->red_mask,   &dib->red_shift,   &dib->red_len);
73     calc_shift_and_len(dib->green_mask, &dib->green_shift, &dib->green_len);
74     calc_shift_and_len(dib->blue_mask,  &dib->blue_shift,  &dib->blue_len);
75 }
76
77 static BOOL init_dib(dib_info *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields, void *bits)
78 {
79     dib->bit_count = bi->biBitCount;
80     dib->width     = bi->biWidth;
81     dib->height    = bi->biHeight;
82     dib->stride    = ((dib->width * dib->bit_count + 31) >> 3) & ~3;
83     dib->bits      = bits;
84
85     if(dib->height < 0) /* top-down */
86     {
87         dib->height = -dib->height;
88     }
89     else /* bottom-up */
90     {
91         /* bits always points to the top-left corner and the stride is -ve */
92         dib->bits    = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
93         dib->stride  = -dib->stride;
94     }
95
96     dib->funcs = &funcs_null;
97
98     switch(dib->bit_count)
99     {
100     case 32:
101         if(bi->biCompression == BI_RGB)
102             dib->funcs = &funcs_8888;
103         else
104         {
105             init_bit_fields(dib, bit_fields);
106             if(dib->red_mask == 0xff0000 && dib->green_mask == 0x00ff00 && dib->blue_mask == 0x0000ff)
107                 dib->funcs = &funcs_8888;
108             else
109                 dib->funcs = &funcs_32;
110         }
111         break;
112
113     default:
114         TRACE("bpp %d not supported, will forward to graphics driver.\n", dib->bit_count);
115         return FALSE;
116     }
117
118     return TRUE;
119 }
120
121 /***********************************************************************
122  *           dibdrv_SelectBitmap
123  */
124 static HBITMAP CDECL dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
125 {
126     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBitmap );
127     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
128     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
129     TRACE("(%p, %p)\n", dev, bitmap);
130
131     if (!bmp) return 0;
132     assert(bmp->dib);
133
134     pdev->clip = CreateRectRgn(0, 0, 0, 0);
135     pdev->defer = 0;
136
137     if(!init_dib(&pdev->dib, &bmp->dib->dsBmih, bmp->dib->dsBitfields, bmp->dib->dsBm.bmBits))
138         pdev->defer |= DEFER_FORMAT;
139
140     GDI_ReleaseObj( bitmap );
141
142     return next->funcs->pSelectBitmap( next, bitmap );
143 }
144
145 /***********************************************************************
146  *           dibdrv_SetBkColor
147  */
148 static COLORREF CDECL dibdrv_SetBkColor( PHYSDEV dev, COLORREF color )
149 {
150     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkColor );
151     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
152
153     pdev->bkgnd_color = pdev->dib.funcs->colorref_to_pixel( &pdev->dib, color );
154
155     if( GetBkMode(dev->hdc) == OPAQUE )
156         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
157     else
158     {
159         pdev->bkgnd_and = ~0u;
160         pdev->bkgnd_xor = 0;
161     }
162
163     return next->funcs->pSetBkColor( next, color );
164 }
165
166 /***********************************************************************
167  *           dibdrv_SetBkMode
168  */
169 static INT CDECL dibdrv_SetBkMode( PHYSDEV dev, INT mode )
170 {
171     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetBkMode );
172     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
173
174     if( mode == OPAQUE )
175         calc_and_xor_masks( GetROP2(dev->hdc), pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor );
176     else
177     {
178         pdev->bkgnd_and = ~0u;
179         pdev->bkgnd_xor = 0;
180     }
181
182     return next->funcs->pSetBkMode( next, mode );
183 }
184
185 /***********************************************************************
186  *           dibdrv_SetDeviceClipping
187  */
188 static void CDECL dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
189 {
190     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
191     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
192     TRACE("(%p, %p, %p)\n", dev, vis_rgn, clip_rgn);
193
194     CombineRgn( pdev->clip, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
195     return next->funcs->pSetDeviceClipping( next, vis_rgn, clip_rgn);
196 }
197
198 /***********************************************************************
199  *           dibdrv_SetROP2
200  */
201 static INT CDECL dibdrv_SetROP2( PHYSDEV dev, INT rop )
202 {
203     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetROP2 );
204     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
205
206     calc_and_xor_masks(rop, pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
207     update_brush_rop(pdev, rop);
208     if( GetBkMode(dev->hdc) == OPAQUE )
209         calc_and_xor_masks(rop, pdev->bkgnd_color, &pdev->bkgnd_and, &pdev->bkgnd_xor);
210
211     return next->funcs->pSetROP2( next, rop );
212 }
213
214 const DC_FUNCTIONS dib_driver =
215 {
216     NULL,                               /* pAbortDoc */
217     NULL,                               /* pAbortPath */
218     NULL,                               /* pAlphaBlend */
219     NULL,                               /* pAngleArc */
220     NULL,                               /* pArc */
221     NULL,                               /* pArcTo */
222     NULL,                               /* pBeginPath */
223     NULL,                               /* pChoosePixelFormat */
224     NULL,                               /* pChord */
225     NULL,                               /* pCloseFigure */
226     NULL,                               /* pCreateBitmap */
227     NULL,                               /* pCreateDC */
228     NULL,                               /* pCreateDIBSection */
229     NULL,                               /* pDeleteBitmap */
230     dibdrv_DeleteDC,                    /* pDeleteDC */
231     NULL,                               /* pDeleteObject */
232     NULL,                               /* pDescribePixelFormat */
233     NULL,                               /* pDeviceCapabilities */
234     NULL,                               /* pEllipse */
235     NULL,                               /* pEndDoc */
236     NULL,                               /* pEndPage */
237     NULL,                               /* pEndPath */
238     NULL,                               /* pEnumDeviceFonts */
239     NULL,                               /* pEnumICMProfiles */
240     NULL,                               /* pExcludeClipRect */
241     NULL,                               /* pExtDeviceMode */
242     NULL,                               /* pExtEscape */
243     NULL,                               /* pExtFloodFill */
244     NULL,                               /* pExtSelectClipRgn */
245     NULL,                               /* pExtTextOut */
246     NULL,                               /* pFillPath */
247     NULL,                               /* pFillRgn */
248     NULL,                               /* pFlattenPath */
249     NULL,                               /* pFrameRgn */
250     NULL,                               /* pGdiComment */
251     NULL,                               /* pGetBitmapBits */
252     NULL,                               /* pGetCharWidth */
253     NULL,                               /* pGetDIBits */
254     NULL,                               /* pGetDeviceCaps */
255     NULL,                               /* pGetDeviceGammaRamp */
256     NULL,                               /* pGetICMProfile */
257     NULL,                               /* pGetNearestColor */
258     NULL,                               /* pGetPixel */
259     NULL,                               /* pGetPixelFormat */
260     NULL,                               /* pGetSystemPaletteEntries */
261     NULL,                               /* pGetTextExtentExPoint */
262     NULL,                               /* pGetTextMetrics */
263     NULL,                               /* pIntersectClipRect */
264     NULL,                               /* pInvertRgn */
265     dibdrv_LineTo,                      /* pLineTo */
266     NULL,                               /* pModifyWorldTransform */
267     NULL,                               /* pMoveTo */
268     NULL,                               /* pOffsetClipRgn */
269     NULL,                               /* pOffsetViewportOrg */
270     NULL,                               /* pOffsetWindowOrg */
271     NULL,                               /* pPaintRgn */
272     dibdrv_PatBlt,                      /* pPatBlt */
273     NULL,                               /* pPie */
274     NULL,                               /* pPolyBezier */
275     NULL,                               /* pPolyBezierTo */
276     NULL,                               /* pPolyDraw */
277     NULL,                               /* pPolyPolygon */
278     NULL,                               /* pPolyPolyline */
279     NULL,                               /* pPolygon */
280     NULL,                               /* pPolyline */
281     NULL,                               /* pPolylineTo */
282     NULL,                               /* pRealizeDefaultPalette */
283     NULL,                               /* pRealizePalette */
284     NULL,                               /* pRectangle */
285     NULL,                               /* pResetDC */
286     NULL,                               /* pRestoreDC */
287     NULL,                               /* pRoundRect */
288     NULL,                               /* pSaveDC */
289     NULL,                               /* pScaleViewportExt */
290     NULL,                               /* pScaleWindowExt */
291     dibdrv_SelectBitmap,                /* pSelectBitmap */
292     dibdrv_SelectBrush,                 /* pSelectBrush */
293     NULL,                               /* pSelectClipPath */
294     NULL,                               /* pSelectFont */
295     NULL,                               /* pSelectPalette */
296     dibdrv_SelectPen,                   /* pSelectPen */
297     NULL,                               /* pSetArcDirection */
298     NULL,                               /* pSetBitmapBits */
299     dibdrv_SetBkColor,                  /* pSetBkColor */
300     dibdrv_SetBkMode,                   /* pSetBkMode */
301     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
302     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
303     NULL,                               /* pSetDIBColorTable */
304     NULL,                               /* pSetDIBits */
305     NULL,                               /* pSetDIBitsToDevice */
306     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
307     NULL,                               /* pSetDeviceGammaRamp */
308     NULL,                               /* pSetLayout */
309     NULL,                               /* pSetMapMode */
310     NULL,                               /* pSetMapperFlags */
311     NULL,                               /* pSetPixel */
312     NULL,                               /* pSetPixelFormat */
313     NULL,                               /* pSetPolyFillMode */
314     dibdrv_SetROP2,                     /* pSetROP2 */
315     NULL,                               /* pSetRelAbs */
316     NULL,                               /* pSetStretchBltMode */
317     NULL,                               /* pSetTextAlign */
318     NULL,                               /* pSetTextCharacterExtra */
319     NULL,                               /* pSetTextColor */
320     NULL,                               /* pSetTextJustification */
321     NULL,                               /* pSetViewportExt */
322     NULL,                               /* pSetViewportOrg */
323     NULL,                               /* pSetWindowExt */
324     NULL,                               /* pSetWindowOrg */
325     NULL,                               /* pSetWorldTransform */
326     NULL,                               /* pStartDoc */
327     NULL,                               /* pStartPage */
328     NULL,                               /* pStretchBlt */
329     NULL,                               /* pStretchDIBits */
330     NULL,                               /* pStrokeAndFillPath */
331     NULL,                               /* pStrokePath */
332     NULL,                               /* pSwapBuffers */
333     NULL,                               /* pUnrealizePalette */
334     NULL,                               /* pWidenPath */
335     NULL,                               /* pwglCopyContext */
336     NULL,                               /* pwglCreateContext */
337     NULL,                               /* pwglCreateContextAttribsARB */
338     NULL,                               /* pwglDeleteContext */
339     NULL,                               /* pwglGetProcAddress */
340     NULL,                               /* pwglGetPbufferDCARB */
341     NULL,                               /* pwglMakeCurrent */
342     NULL,                               /* pwglMakeContextCurrentARB */
343     NULL,                               /* pwglSetPixelFormatWINE */
344     NULL,                               /* pwglShareLists */
345     NULL,                               /* pwglUseFontBitmapsA */
346     NULL                                /* pwglUseFontBitmapsW */
347 };