imagehlp: Remove unused variable.
[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 )
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     if(!ret) return ERROR_BAD_FORMAT;
238
239     /* update coordinates, the destination rectangle is always stored at 0,0 */
240     src->x -= src->visrect.left;
241     src->y -= src->visrect.top;
242     offset_rect( &src->visrect, -src->visrect.left, -src->visrect.top );
243     return ERROR_SUCCESS;
244 }
245
246 int clip_rect_to_dib( const dib_info *dib, RECT *rc )
247 {
248     RECT rect;
249
250     rect.left   = max( 0, -dib->rect.left );
251     rect.top    = max( 0, -dib->rect.top );
252     rect.right  = min( dib->rect.right, dib->width ) - dib->rect.left;
253     rect.bottom = min( dib->rect.bottom, dib->height ) - dib->rect.top;
254     if (is_rect_empty( &rect )) return 0;
255     return intersect_rect( rc, &rect, rc );
256 }
257
258 int get_clipped_rects( const dib_info *dib, const RECT *rc, HRGN clip, struct clipped_rects *clip_rects )
259 {
260     const WINEREGION *region;
261     RECT rect, *out = clip_rects->buffer;
262     int i;
263
264     init_clipped_rects( clip_rects );
265
266     rect.left   = max( 0, -dib->rect.left );
267     rect.top    = max( 0, -dib->rect.top );
268     rect.right  = min( dib->rect.right, dib->width ) - dib->rect.left;
269     rect.bottom = min( dib->rect.bottom, dib->height ) - dib->rect.top;
270     if (is_rect_empty( &rect )) return 0;
271     if (rc && !intersect_rect( &rect, &rect, rc )) return 0;
272
273     if (!clip)
274     {
275         *out = rect;
276         clip_rects->count = 1;
277         return 1;
278     }
279
280     if (!(region = get_wine_region( clip ))) return 0;
281
282     for (i = 0; i < region->numRects; i++)
283     {
284         if (region->rects[i].top >= rect.bottom) break;
285         if (!intersect_rect( out, &rect, &region->rects[i] )) continue;
286         out++;
287         if (out == &clip_rects->buffer[sizeof(clip_rects->buffer) / sizeof(RECT)])
288         {
289             clip_rects->rects = HeapAlloc( GetProcessHeap(), 0, region->numRects * sizeof(RECT) );
290             if (!clip_rects->rects) return 0;
291             memcpy( clip_rects->rects, clip_rects->buffer, (out - clip_rects->buffer) * sizeof(RECT) );
292             out = clip_rects->rects + (out - clip_rects->buffer);
293         }
294     }
295     release_wine_region( clip );
296     clip_rects->count = out - clip_rects->rects;
297     return clip_rects->count;
298 }
299
300 void add_clipped_bounds( dibdrv_physdev *dev, const RECT *rect, HRGN clip )
301 {
302     const WINEREGION *region;
303     RECT rc;
304
305     if (!dev->bounds) return;
306     if (clip)
307     {
308         if (!(region = get_wine_region( clip ))) return;
309         if (!rect) rc = region->extents;
310         else intersect_rect( &rc, rect, &region->extents );
311         release_wine_region( clip );
312     }
313     else rc = *rect;
314
315     if (is_rect_empty( &rc )) return;
316     offset_rect( &rc, dev->dib.rect.left, dev->dib.rect.top );
317     add_bounds_rect( dev->bounds, &rc );
318 }
319
320 /**********************************************************************
321  *           dibdrv_CreateDC
322  */
323 static BOOL dibdrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
324                              LPCWSTR output, const DEVMODEW *data )
325 {
326     dibdrv_physdev *pdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pdev) );
327
328     if (!pdev) return FALSE;
329     clear_dib_info(&pdev->dib);
330     clear_dib_info(&pdev->brush.dib);
331     clear_dib_info(&pdev->pen_brush.dib);
332     push_dc_driver( dev, &pdev->dev, &dib_driver );
333     return TRUE;
334 }
335
336 /***********************************************************************
337  *           dibdrv_DeleteDC
338  */
339 static BOOL dibdrv_DeleteDC( PHYSDEV dev )
340 {
341     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
342     TRACE("(%p)\n", dev);
343     free_pattern_brush( &pdev->brush );
344     free_pattern_brush( &pdev->pen_brush );
345     HeapFree( GetProcessHeap(), 0, pdev );
346     return TRUE;
347 }
348
349 /***********************************************************************
350  *           dibdrv_SelectBitmap
351  */
352 static HBITMAP dibdrv_SelectBitmap( PHYSDEV dev, HBITMAP bitmap )
353 {
354     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
355     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
356     dib_info dib;
357
358     TRACE("(%p, %p)\n", dev, bitmap);
359
360     if (!bmp) return 0;
361
362     if (!init_dib_info_from_bitmapobj(&dib, bmp))
363     {
364         GDI_ReleaseObj( bitmap );
365         return 0;
366     }
367     pdev->dib = dib;
368     GDI_ReleaseObj( bitmap );
369
370     return bitmap;
371 }
372
373 /***********************************************************************
374  *           dibdrv_SetDeviceClipping
375  */
376 static void dibdrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
377 {
378     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
379     TRACE("(%p, %p)\n", dev, rgn);
380
381     pdev->clip = rgn;
382 }
383
384 /***********************************************************************
385  *           dibdrv_SetBoundsRect
386  */
387 static UINT dibdrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
388 {
389     dibdrv_physdev *pdev = get_dibdrv_pdev( dev );
390
391     if (flags & DCB_DISABLE) pdev->bounds = NULL;
392     else if (flags & DCB_ENABLE) pdev->bounds = rect;
393     return DCB_RESET;  /* we don't have device-specific bounds */
394 }
395
396 const struct gdi_dc_funcs dib_driver =
397 {
398     NULL,                               /* pAbortDoc */
399     NULL,                               /* pAbortPath */
400     dibdrv_AlphaBlend,                  /* pAlphaBlend */
401     NULL,                               /* pAngleArc */
402     dibdrv_Arc,                         /* pArc */
403     dibdrv_ArcTo,                       /* pArcTo */
404     NULL,                               /* pBeginPath */
405     dibdrv_BlendImage,                  /* pBlendImage */
406     dibdrv_Chord,                       /* pChord */
407     NULL,                               /* pCloseFigure */
408     NULL,                               /* pCreateCompatibleDC */
409     dibdrv_CreateDC,                    /* pCreateDC */
410     dibdrv_DeleteDC,                    /* pDeleteDC */
411     NULL,                               /* pDeleteObject */
412     NULL,                               /* pDeviceCapabilities */
413     dibdrv_Ellipse,                     /* pEllipse */
414     NULL,                               /* pEndDoc */
415     NULL,                               /* pEndPage */
416     NULL,                               /* pEndPath */
417     NULL,                               /* pEnumFonts */
418     NULL,                               /* pEnumICMProfiles */
419     NULL,                               /* pExcludeClipRect */
420     NULL,                               /* pExtDeviceMode */
421     NULL,                               /* pExtEscape */
422     dibdrv_ExtFloodFill,                /* pExtFloodFill */
423     NULL,                               /* pExtSelectClipRgn */
424     dibdrv_ExtTextOut,                  /* pExtTextOut */
425     NULL,                               /* pFillPath */
426     NULL,                               /* pFillRgn */
427     NULL,                               /* pFlattenPath */
428     NULL,                               /* pFontIsLinked */
429     NULL,                               /* pFrameRgn */
430     NULL,                               /* pGdiComment */
431     NULL,                               /* pGdiRealizationInfo */
432     NULL,                               /* pGetBoundsRect */
433     NULL,                               /* pGetCharABCWidths */
434     NULL,                               /* pGetCharABCWidthsI */
435     NULL,                               /* pGetCharWidth */
436     NULL,                               /* pGetDeviceCaps */
437     NULL,                               /* pGetDeviceGammaRamp */
438     NULL,                               /* pGetFontData */
439     NULL,                               /* pGetFontUnicodeRanges */
440     NULL,                               /* pGetGlyphIndices */
441     NULL,                               /* pGetGlyphOutline */
442     NULL,                               /* pGetICMProfile */
443     dibdrv_GetImage,                    /* pGetImage */
444     NULL,                               /* pGetKerningPairs */
445     dibdrv_GetNearestColor,             /* pGetNearestColor */
446     NULL,                               /* pGetOutlineTextMetrics */
447     dibdrv_GetPixel,                    /* pGetPixel */
448     NULL,                               /* pGetSystemPaletteEntries */
449     NULL,                               /* pGetTextCharsetInfo */
450     NULL,                               /* pGetTextExtentExPoint */
451     NULL,                               /* pGetTextExtentExPointI */
452     NULL,                               /* pGetTextFace */
453     NULL,                               /* pGetTextMetrics */
454     dibdrv_GradientFill,                /* pGradientFill */
455     NULL,                               /* pIntersectClipRect */
456     NULL,                               /* pInvertRgn */
457     dibdrv_LineTo,                      /* pLineTo */
458     NULL,                               /* pModifyWorldTransform */
459     NULL,                               /* pMoveTo */
460     NULL,                               /* pOffsetClipRgn */
461     NULL,                               /* pOffsetViewportOrg */
462     NULL,                               /* pOffsetWindowOrg */
463     dibdrv_PaintRgn,                    /* pPaintRgn */
464     dibdrv_PatBlt,                      /* pPatBlt */
465     dibdrv_Pie,                         /* pPie */
466     NULL,                               /* pPolyBezier */
467     NULL,                               /* pPolyBezierTo */
468     NULL,                               /* pPolyDraw */
469     dibdrv_PolyPolygon,                 /* pPolyPolygon */
470     dibdrv_PolyPolyline,                /* pPolyPolyline */
471     dibdrv_Polygon,                     /* pPolygon */
472     dibdrv_Polyline,                    /* pPolyline */
473     NULL,                               /* pPolylineTo */
474     dibdrv_PutImage,                    /* pPutImage */
475     NULL,                               /* pRealizeDefaultPalette */
476     NULL,                               /* pRealizePalette */
477     dibdrv_Rectangle,                   /* pRectangle */
478     NULL,                               /* pResetDC */
479     NULL,                               /* pRestoreDC */
480     dibdrv_RoundRect,                   /* pRoundRect */
481     NULL,                               /* pSaveDC */
482     NULL,                               /* pScaleViewportExt */
483     NULL,                               /* pScaleWindowExt */
484     dibdrv_SelectBitmap,                /* pSelectBitmap */
485     dibdrv_SelectBrush,                 /* pSelectBrush */
486     NULL,                               /* pSelectClipPath */
487     dibdrv_SelectFont,                  /* pSelectFont */
488     NULL,                               /* pSelectPalette */
489     dibdrv_SelectPen,                   /* pSelectPen */
490     NULL,                               /* pSetArcDirection */
491     NULL,                               /* pSetBkColor */
492     NULL,                               /* pSetBkMode */
493     dibdrv_SetBoundsRect,               /* pSetBoundsRect */
494     dibdrv_SetDCBrushColor,             /* pSetDCBrushColor */
495     dibdrv_SetDCPenColor,               /* pSetDCPenColor */
496     NULL,                               /* pSetDIBitsToDevice */
497     dibdrv_SetDeviceClipping,           /* pSetDeviceClipping */
498     NULL,                               /* pSetDeviceGammaRamp */
499     NULL,                               /* pSetLayout */
500     NULL,                               /* pSetMapMode */
501     NULL,                               /* pSetMapperFlags */
502     dibdrv_SetPixel,                    /* pSetPixel */
503     NULL,                               /* pSetPolyFillMode */
504     NULL,                               /* pSetROP2 */
505     NULL,                               /* pSetRelAbs */
506     NULL,                               /* pSetStretchBltMode */
507     NULL,                               /* pSetTextAlign */
508     NULL,                               /* pSetTextCharacterExtra */
509     NULL,                               /* pSetTextColor */
510     NULL,                               /* pSetTextJustification */
511     NULL,                               /* pSetViewportExt */
512     NULL,                               /* pSetViewportOrg */
513     NULL,                               /* pSetWindowExt */
514     NULL,                               /* pSetWindowOrg */
515     NULL,                               /* pSetWorldTransform */
516     NULL,                               /* pStartDoc */
517     NULL,                               /* pStartPage */
518     dibdrv_StretchBlt,                  /* pStretchBlt */
519     NULL,                               /* pStretchDIBits */
520     NULL,                               /* pStrokeAndFillPath */
521     NULL,                               /* pStrokePath */
522     NULL,                               /* pUnrealizePalette */
523     NULL,                               /* pWidenPath */
524     dibdrv_wine_get_wgl_driver,         /* wine_get_wgl_driver */
525     GDI_PRIORITY_DIB_DRV                /* priority */
526 };
527
528
529 /***********************************************************************
530  * Driver for window surfaces.
531  *
532  * It uses the DIB engine but needs extra locking since multiple DCs
533  * can paint to the same window.
534  */
535
536 struct windrv_physdev
537 {
538     struct gdi_physdev     dev;
539     struct dibdrv_physdev *dibdrv;
540     struct window_surface *surface;
541 };
542
543 static const struct gdi_dc_funcs window_driver;
544
545 static inline struct windrv_physdev *get_windrv_physdev( PHYSDEV dev )
546 {
547     return (struct windrv_physdev *)dev;
548 }
549
550 static inline void lock_surface( struct window_surface *surface )
551 {
552     GDI_CheckNotLock();
553     surface->funcs->lock( surface );
554 }
555
556 static inline void unlock_surface( struct window_surface *surface )
557 {
558     surface->funcs->unlock( surface );
559 }
560
561 static void unlock_bits_surface( struct gdi_image_bits *bits )
562 {
563     unlock_surface( bits->param );
564 }
565
566 void dibdrv_set_window_surface( DC *dc, struct window_surface *surface )
567 {
568     char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
569     BITMAPINFO *info = (BITMAPINFO *)buffer;
570     RECT rect;
571     void *bits;
572     PHYSDEV windev;
573     struct windrv_physdev *physdev;
574     struct dibdrv_physdev *dibdrv;
575
576     TRACE( "%p %p\n", dc->hSelf, surface );
577
578     windev = pop_dc_driver( dc, &window_driver );
579
580     if (surface)
581     {
582         if (windev) push_dc_driver( &dc->physDev, windev, windev->funcs );
583         else
584         {
585             if (!window_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL )) return;
586             windev = find_dc_driver( dc, &window_driver );
587         }
588
589         physdev = get_windrv_physdev( windev );
590         window_surface_add_ref( surface );
591         if (physdev->surface) window_surface_release( physdev->surface );
592         physdev->surface = surface;
593
594         dibdrv = physdev->dibdrv;
595         bits = surface->funcs->get_info( surface, info );
596         init_dib_info_from_bitmapinfo( &dibdrv->dib, info, bits );
597         /* clip the device rect to the surface */
598         rect = surface->rect;
599         offset_rect( &rect, dc->device_rect.left, dc->device_rect.top );
600         intersect_rect( &dc->device_rect, &dc->device_rect, &rect );
601         dibdrv->dib.rect = dc->vis_rect;
602         offset_rect( &dibdrv->dib.rect, -rect.left, -rect.top );
603         dibdrv->bounds = surface->funcs->get_bounds( surface );
604         DC_InitDC( dc );
605     }
606     else if (windev)
607     {
608         dib_driver.pDeleteDC( pop_dc_driver( dc, &dib_driver ));
609         windev->funcs->pDeleteDC( windev );
610         DC_InitDC( dc );
611     }
612 }
613
614 static BOOL windrv_AlphaBlend( PHYSDEV dst_dev, struct bitblt_coords *dst,
615                                PHYSDEV src_dev, struct bitblt_coords *src, BLENDFUNCTION func )
616 {
617     struct windrv_physdev *physdev = get_windrv_physdev( dst_dev );
618     BOOL ret;
619
620     lock_surface( physdev->surface );
621     dst_dev = GET_NEXT_PHYSDEV( dst_dev, pAlphaBlend );
622     ret = dst_dev->funcs->pAlphaBlend( dst_dev, dst, src_dev, src, func );
623     unlock_surface( physdev->surface );
624     return ret;
625 }
626
627 static BOOL windrv_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
628                         INT xstart, INT ystart, INT xend, INT yend )
629 {
630     struct windrv_physdev *physdev = get_windrv_physdev( dev );
631     BOOL ret;
632
633     lock_surface( physdev->surface );
634     dev = GET_NEXT_PHYSDEV( dev, pArc );
635     ret = dev->funcs->pArc( dev, left, top, right, bottom, xstart, ystart, xend, yend );
636     unlock_surface( physdev->surface );
637     return ret;
638 }
639
640 static BOOL windrv_ArcTo( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
641                           INT xstart, INT ystart, INT xend, INT yend )
642 {
643     struct windrv_physdev *physdev = get_windrv_physdev( dev );
644     BOOL ret;
645
646     lock_surface( physdev->surface );
647     dev = GET_NEXT_PHYSDEV( dev, pArc );
648     ret = dev->funcs->pArcTo( dev, left, top, right, bottom, xstart, ystart, xend, yend );
649     unlock_surface( physdev->surface );
650     return ret;
651 }
652
653 static DWORD windrv_BlendImage( PHYSDEV dev, BITMAPINFO *info, const struct gdi_image_bits *bits,
654                                 struct bitblt_coords *src, struct bitblt_coords *dst, BLENDFUNCTION blend )
655 {
656     struct windrv_physdev *physdev = get_windrv_physdev( dev );
657     DWORD ret;
658
659     lock_surface( physdev->surface );
660     dev = GET_NEXT_PHYSDEV( dev, pBlendImage );
661     ret = dev->funcs->pBlendImage( dev, info, bits, src, dst, blend );
662     unlock_surface( physdev->surface );
663     return ret;
664 }
665
666 static BOOL windrv_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
667                           INT xstart, INT ystart, INT xend, INT yend )
668 {
669     struct windrv_physdev *physdev = get_windrv_physdev( dev );
670     BOOL ret;
671
672     lock_surface( physdev->surface );
673     dev = GET_NEXT_PHYSDEV( dev, pChord );
674     ret = dev->funcs->pChord( dev, left, top, right, bottom, xstart, ystart, xend, yend );
675     unlock_surface( physdev->surface );
676     return ret;
677 }
678
679 static BOOL windrv_CreateDC( PHYSDEV *dev, LPCWSTR driver, LPCWSTR device,
680                              LPCWSTR output, const DEVMODEW *devmode )
681 {
682     struct windrv_physdev *physdev = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*physdev) );
683
684     if (!physdev) return FALSE;
685
686     if (!dib_driver.pCreateDC( dev, NULL, NULL, NULL, NULL ))
687     {
688         HeapFree( GetProcessHeap(), 0, physdev );
689         return FALSE;
690     }
691     physdev->dibdrv = get_dibdrv_pdev( *dev );
692     push_dc_driver( dev, &physdev->dev, &window_driver );
693     return TRUE;
694 }
695
696 static BOOL windrv_DeleteDC( PHYSDEV dev )
697 {
698     struct windrv_physdev *physdev = get_windrv_physdev( dev );
699
700     window_surface_release( physdev->surface );
701     HeapFree( GetProcessHeap(), 0, physdev );
702     return TRUE;
703 }
704
705 static BOOL windrv_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
706 {
707     struct windrv_physdev *physdev = get_windrv_physdev( dev );
708     BOOL ret;
709
710     lock_surface( physdev->surface );
711     dev = GET_NEXT_PHYSDEV( dev, pEllipse );
712     ret = dev->funcs->pEllipse( dev, left, top, right, bottom );
713     unlock_surface( physdev->surface );
714     return ret;
715 }
716
717 static BOOL windrv_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT type )
718 {
719     struct windrv_physdev *physdev = get_windrv_physdev( dev );
720     BOOL ret;
721
722     lock_surface( physdev->surface );
723     dev = GET_NEXT_PHYSDEV( dev, pExtFloodFill );
724     ret = dev->funcs->pExtFloodFill( dev, x, y, color, type );
725     unlock_surface( physdev->surface );
726     return ret;
727 }
728
729 static BOOL windrv_ExtTextOut( PHYSDEV dev, INT x, INT y, UINT flags, const RECT *rect,
730                                LPCWSTR str, UINT count, const INT *dx )
731 {
732     struct windrv_physdev *physdev = get_windrv_physdev( dev );
733     BOOL ret;
734
735     lock_surface( physdev->surface );
736     dev = GET_NEXT_PHYSDEV( dev, pExtTextOut );
737     ret = dev->funcs->pExtTextOut( dev, x, y, flags, rect, str, count, dx );
738     unlock_surface( physdev->surface );
739     return ret;
740 }
741
742 static DWORD windrv_GetImage( PHYSDEV dev, BITMAPINFO *info,
743                               struct gdi_image_bits *bits, struct bitblt_coords *src )
744 {
745     struct windrv_physdev *physdev = get_windrv_physdev( dev );
746     DWORD ret;
747
748     lock_surface( physdev->surface );
749     dev = GET_NEXT_PHYSDEV( dev, pGetImage );
750     ret = dev->funcs->pGetImage( dev, info, bits, src );
751
752     /* don't return alpha if original surface doesn't support it */
753     if (info->bmiHeader.biBitCount == 32 &&
754         info->bmiHeader.biCompression == BI_RGB &&
755         physdev->dibdrv->dib.compression == BI_BITFIELDS)
756     {
757         DWORD *colors = (DWORD *)info->bmiColors;
758         colors[0] = 0xff0000;
759         colors[1] = 0x00ff00;
760         colors[2] = 0x0000ff;
761         info->bmiHeader.biCompression = BI_BITFIELDS;
762     }
763
764     if (!bits->is_copy)
765     {
766         /* use the freeing callback to unlock the surface */
767         assert( !bits->free );
768         bits->free = unlock_bits_surface;
769         bits->param = physdev->surface;
770     }
771     else unlock_surface( physdev->surface );
772     return ret;
773 }
774
775 static COLORREF windrv_GetPixel( PHYSDEV dev, INT x, INT y )
776 {
777     struct windrv_physdev *physdev = get_windrv_physdev( dev );
778     COLORREF ret;
779
780     lock_surface( physdev->surface );
781     dev = GET_NEXT_PHYSDEV( dev, pGetPixel );
782     ret = dev->funcs->pGetPixel( dev, x, y );
783     unlock_surface( physdev->surface );
784     return ret;
785 }
786
787 static BOOL windrv_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
788                                  void * grad_array, ULONG ngrad, ULONG mode )
789 {
790     struct windrv_physdev *physdev = get_windrv_physdev( dev );
791     BOOL ret;
792
793     lock_surface( physdev->surface );
794     dev = GET_NEXT_PHYSDEV( dev, pGradientFill );
795     ret = dev->funcs->pGradientFill( dev, vert_array, nvert, grad_array, ngrad, mode );
796     unlock_surface( physdev->surface );
797     return ret;
798 }
799
800 static BOOL windrv_LineTo( PHYSDEV dev, INT x, INT y )
801 {
802     struct windrv_physdev *physdev = get_windrv_physdev( dev );
803     BOOL ret;
804
805     lock_surface( physdev->surface );
806     dev = GET_NEXT_PHYSDEV( dev, pLineTo );
807     ret = dev->funcs->pLineTo( dev, x, y );
808     unlock_surface( physdev->surface );
809     return ret;
810 }
811
812 static BOOL windrv_PaintRgn( PHYSDEV dev, HRGN rgn )
813 {
814     struct windrv_physdev *physdev = get_windrv_physdev( dev );
815     BOOL ret;
816
817     lock_surface( physdev->surface );
818     dev = GET_NEXT_PHYSDEV( dev, pPaintRgn );
819     ret = dev->funcs->pPaintRgn( dev, rgn );
820     unlock_surface( physdev->surface );
821     return ret;
822 }
823
824 static BOOL windrv_PatBlt( PHYSDEV dev, struct bitblt_coords *dst, DWORD rop )
825 {
826     struct windrv_physdev *physdev = get_windrv_physdev( dev );
827     BOOL ret;
828
829     lock_surface( physdev->surface );
830     dev = GET_NEXT_PHYSDEV( dev, pPatBlt );
831     ret = dev->funcs->pPatBlt( dev, dst, rop );
832     unlock_surface( physdev->surface );
833     return ret;
834 }
835
836 static BOOL windrv_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
837                         INT xstart, INT ystart, INT xend, INT yend )
838 {
839     struct windrv_physdev *physdev = get_windrv_physdev( dev );
840     BOOL ret;
841
842     lock_surface( physdev->surface );
843     dev = GET_NEXT_PHYSDEV( dev, pPie );
844     ret = dev->funcs->pPie( dev, left, top, right, bottom, xstart, ystart, xend, yend );
845     unlock_surface( physdev->surface );
846     return ret;
847 }
848
849 static BOOL windrv_PolyPolygon( PHYSDEV dev, const POINT *points, const INT *counts, UINT polygons )
850 {
851     struct windrv_physdev *physdev = get_windrv_physdev( dev );
852     BOOL ret;
853
854     lock_surface( physdev->surface );
855     dev = GET_NEXT_PHYSDEV( dev, pPolyPolygon );
856     ret = dev->funcs->pPolyPolygon( dev, points, counts, polygons );
857     unlock_surface( physdev->surface );
858     return ret;
859 }
860
861 static BOOL windrv_PolyPolyline( PHYSDEV dev, const POINT *points, const DWORD *counts, DWORD lines )
862 {
863     struct windrv_physdev *physdev = get_windrv_physdev( dev );
864     BOOL ret;
865
866     lock_surface( physdev->surface );
867     dev = GET_NEXT_PHYSDEV( dev, pPolyPolyline );
868     ret = dev->funcs->pPolyPolyline( dev, points, counts, lines );
869     unlock_surface( physdev->surface );
870     return ret;
871 }
872
873 static BOOL windrv_Polygon( PHYSDEV dev, const POINT *points, INT count )
874 {
875     struct windrv_physdev *physdev = get_windrv_physdev( dev );
876     BOOL ret;
877
878     lock_surface( physdev->surface );
879     dev = GET_NEXT_PHYSDEV( dev, pPolygon );
880     ret = dev->funcs->pPolygon( dev, points, count );
881     unlock_surface( physdev->surface );
882     return ret;
883 }
884
885 static BOOL windrv_Polyline( PHYSDEV dev, const POINT *points, INT count )
886 {
887     struct windrv_physdev *physdev = get_windrv_physdev( dev );
888     BOOL ret;
889
890     lock_surface( physdev->surface );
891     dev = GET_NEXT_PHYSDEV( dev, pPolyline );
892     ret = dev->funcs->pPolyline( dev, points, count );
893     unlock_surface( physdev->surface );
894     return ret;
895 }
896
897 static DWORD windrv_PutImage( PHYSDEV dev, HRGN clip, BITMAPINFO *info,
898                               const struct gdi_image_bits *bits, struct bitblt_coords *src,
899                               struct bitblt_coords *dst, DWORD rop )
900 {
901     struct windrv_physdev *physdev = get_windrv_physdev( dev );
902     DWORD ret;
903
904     lock_surface( physdev->surface );
905     dev = GET_NEXT_PHYSDEV( dev, pPutImage );
906     ret = dev->funcs->pPutImage( dev, clip, info, bits, src, dst, rop );
907     unlock_surface( physdev->surface );
908     return ret;
909 }
910
911 static BOOL windrv_Rectangle( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
912 {
913     struct windrv_physdev *physdev = get_windrv_physdev( dev );
914     BOOL ret;
915
916     lock_surface( physdev->surface );
917     dev = GET_NEXT_PHYSDEV( dev, pRectangle );
918     ret = dev->funcs->pRectangle( dev, left, top, right, bottom );
919     unlock_surface( physdev->surface );
920     return ret;
921 }
922
923 static BOOL windrv_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
924                                INT ell_width, INT ell_height )
925 {
926     struct windrv_physdev *physdev = get_windrv_physdev( dev );
927     BOOL ret;
928
929     lock_surface( physdev->surface );
930     dev = GET_NEXT_PHYSDEV( dev, pRoundRect );
931     ret = dev->funcs->pRoundRect( dev, left, top, right, bottom, ell_width, ell_height );
932     unlock_surface( physdev->surface );
933     return ret;
934 }
935
936 static UINT windrv_SetBoundsRect( PHYSDEV dev, RECT *rect, UINT flags )
937 {
938     /* do nothing, we use the dibdrv bounds tracking for our own purpose */
939     return DCB_RESET;
940 }
941
942 static INT windrv_SetDIBitsToDevice( PHYSDEV dev, INT x_dst, INT y_dst, DWORD cx, DWORD cy,
943                                      INT x_src, INT y_src, UINT startscan, UINT lines,
944                                      const void *bits, BITMAPINFO *src_info, UINT coloruse )
945 {
946     struct windrv_physdev *physdev = get_windrv_physdev( dev );
947     INT ret;
948
949     lock_surface( physdev->surface );
950     dev = GET_NEXT_PHYSDEV( dev, pSetDIBitsToDevice );
951     ret = dev->funcs->pSetDIBitsToDevice( dev, x_dst, y_dst, cx, cy,
952                                           x_src, y_src, startscan, lines, bits, src_info, coloruse );
953     unlock_surface( physdev->surface );
954     return ret;
955 }
956
957 static void windrv_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
958 {
959     dev = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
960     dev->funcs->pSetDeviceClipping( dev, rgn );
961     /* also forward to the graphics driver for the OpenGL case */
962     if (dev->funcs == &dib_driver)
963     {
964         dev = GET_NEXT_PHYSDEV( dev, pSetDeviceClipping );
965         dev->funcs->pSetDeviceClipping( dev, rgn );
966     }
967 }
968
969 static COLORREF windrv_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
970 {
971     struct windrv_physdev *physdev = get_windrv_physdev( dev );
972     COLORREF ret;
973
974     lock_surface( physdev->surface );
975     dev = GET_NEXT_PHYSDEV( dev, pSetPixel );
976     ret = dev->funcs->pSetPixel( dev, x, y, color );
977     unlock_surface( physdev->surface );
978     return ret;
979 }
980
981 static BOOL windrv_StretchBlt( PHYSDEV dst_dev, struct bitblt_coords *dst,
982                                PHYSDEV src_dev, struct bitblt_coords *src, DWORD rop )
983 {
984     struct windrv_physdev *physdev = get_windrv_physdev( dst_dev );
985     BOOL ret;
986
987     lock_surface( physdev->surface );
988     dst_dev = GET_NEXT_PHYSDEV( dst_dev, pStretchBlt );
989     ret = dst_dev->funcs->pStretchBlt( dst_dev, dst, src_dev, src, rop );
990     unlock_surface( physdev->surface );
991     return ret;
992 }
993
994 static INT windrv_StretchDIBits( PHYSDEV dev, INT x_dst, INT y_dst, INT width_dst, INT height_dst,
995                                  INT x_src, INT y_src, INT width_src, INT height_src, const void *bits,
996                                  BITMAPINFO *src_info, UINT coloruse, DWORD rop )
997 {
998     struct windrv_physdev *physdev = get_windrv_physdev( dev );
999     INT ret;
1000
1001     lock_surface( physdev->surface );
1002     dev = GET_NEXT_PHYSDEV( dev, pStretchDIBits );
1003     ret = dev->funcs->pStretchDIBits( dev, x_dst, y_dst, width_dst, height_dst,
1004                                       x_src, y_src, width_src, height_src, bits, src_info, coloruse, rop );
1005     unlock_surface( physdev->surface );
1006     return ret;
1007 }
1008
1009 static struct opengl_funcs *windrv_wine_get_wgl_driver( PHYSDEV dev, UINT version )
1010 {
1011     dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
1012     if (dev->funcs == &dib_driver) dev = GET_NEXT_PHYSDEV( dev, wine_get_wgl_driver );
1013     return dev->funcs->wine_get_wgl_driver( dev, version );
1014 }
1015
1016 static const struct gdi_dc_funcs window_driver =
1017 {
1018     NULL,                               /* pAbortDoc */
1019     NULL,                               /* pAbortPath */
1020     windrv_AlphaBlend,                  /* pAlphaBlend */
1021     NULL,                               /* pAngleArc */
1022     windrv_Arc,                         /* pArc */
1023     windrv_ArcTo,                       /* pArcTo */
1024     NULL,                               /* pBeginPath */
1025     windrv_BlendImage,                  /* pBlendImage */
1026     windrv_Chord,                       /* pChord */
1027     NULL,                               /* pCloseFigure */
1028     NULL,                               /* pCreateCompatibleDC */
1029     windrv_CreateDC,                    /* pCreateDC */
1030     windrv_DeleteDC,                    /* pDeleteDC */
1031     NULL,                               /* pDeleteObject */
1032     NULL,                               /* pDeviceCapabilities */
1033     windrv_Ellipse,                     /* pEllipse */
1034     NULL,                               /* pEndDoc */
1035     NULL,                               /* pEndPage */
1036     NULL,                               /* pEndPath */
1037     NULL,                               /* pEnumFonts */
1038     NULL,                               /* pEnumICMProfiles */
1039     NULL,                               /* pExcludeClipRect */
1040     NULL,                               /* pExtDeviceMode */
1041     NULL,                               /* pExtEscape */
1042     windrv_ExtFloodFill,                /* pExtFloodFill */
1043     NULL,                               /* pExtSelectClipRgn */
1044     windrv_ExtTextOut,                  /* pExtTextOut */
1045     NULL,                               /* pFillPath */
1046     NULL,                               /* pFillRgn */
1047     NULL,                               /* pFlattenPath */
1048     NULL,                               /* pFontIsLinked */
1049     NULL,                               /* pFrameRgn */
1050     NULL,                               /* pGdiComment */
1051     NULL,                               /* pGdiRealizationInfo */
1052     NULL,                               /* pGetBoundsRect */
1053     NULL,                               /* pGetCharABCWidths */
1054     NULL,                               /* pGetCharABCWidthsI */
1055     NULL,                               /* pGetCharWidth */
1056     NULL,                               /* pGetDeviceCaps */
1057     NULL,                               /* pGetDeviceGammaRamp */
1058     NULL,                               /* pGetFontData */
1059     NULL,                               /* pGetFontUnicodeRanges */
1060     NULL,                               /* pGetGlyphIndices */
1061     NULL,                               /* pGetGlyphOutline */
1062     NULL,                               /* pGetICMProfile */
1063     windrv_GetImage,                    /* pGetImage */
1064     NULL,                               /* pGetKerningPairs */
1065     NULL,                               /* pGetNearestColor */
1066     NULL,                               /* pGetOutlineTextMetrics */
1067     windrv_GetPixel,                    /* pGetPixel */
1068     NULL,                               /* pGetSystemPaletteEntries */
1069     NULL,                               /* pGetTextCharsetInfo */
1070     NULL,                               /* pGetTextExtentExPoint */
1071     NULL,                               /* pGetTextExtentExPointI */
1072     NULL,                               /* pGetTextFace */
1073     NULL,                               /* pGetTextMetrics */
1074     windrv_GradientFill,                /* pGradientFill */
1075     NULL,                               /* pIntersectClipRect */
1076     NULL,                               /* pInvertRgn */
1077     windrv_LineTo,                      /* pLineTo */
1078     NULL,                               /* pModifyWorldTransform */
1079     NULL,                               /* pMoveTo */
1080     NULL,                               /* pOffsetClipRgn */
1081     NULL,                               /* pOffsetViewportOrg */
1082     NULL,                               /* pOffsetWindowOrg */
1083     windrv_PaintRgn,                    /* pPaintRgn */
1084     windrv_PatBlt,                      /* pPatBlt */
1085     windrv_Pie,                         /* pPie */
1086     NULL,                               /* pPolyBezier */
1087     NULL,                               /* pPolyBezierTo */
1088     NULL,                               /* pPolyDraw */
1089     windrv_PolyPolygon,                 /* pPolyPolygon */
1090     windrv_PolyPolyline,                /* pPolyPolyline */
1091     windrv_Polygon,                     /* pPolygon */
1092     windrv_Polyline,                    /* pPolyline */
1093     NULL,                               /* pPolylineTo */
1094     windrv_PutImage,                    /* pPutImage */
1095     NULL,                               /* pRealizeDefaultPalette */
1096     NULL,                               /* pRealizePalette */
1097     windrv_Rectangle,                   /* pRectangle */
1098     NULL,                               /* pResetDC */
1099     NULL,                               /* pRestoreDC */
1100     windrv_RoundRect,                   /* pRoundRect */
1101     NULL,                               /* pSaveDC */
1102     NULL,                               /* pScaleViewportExt */
1103     NULL,                               /* pScaleWindowExt */
1104     NULL,                               /* pSelectBitmap */
1105     NULL,                               /* pSelectBrush */
1106     NULL,                               /* pSelectClipPath */
1107     NULL,                               /* pSelectFont */
1108     NULL,                               /* pSelectPalette */
1109     NULL,                               /* pSelectPen */
1110     NULL,                               /* pSetArcDirection */
1111     NULL,                               /* pSetBkColor */
1112     NULL,                               /* pSetBkMode */
1113     windrv_SetBoundsRect,               /* pSetBoundsRect */
1114     NULL,                               /* pSetDCBrushColor */
1115     NULL,                               /* pSetDCPenColor */
1116     windrv_SetDIBitsToDevice,           /* pSetDIBitsToDevice */
1117     windrv_SetDeviceClipping,           /* pSetDeviceClipping */
1118     NULL,                               /* pSetDeviceGammaRamp */
1119     NULL,                               /* pSetLayout */
1120     NULL,                               /* pSetMapMode */
1121     NULL,                               /* pSetMapperFlags */
1122     windrv_SetPixel,                    /* pSetPixel */
1123     NULL,                               /* pSetPolyFillMode */
1124     NULL,                               /* pSetROP2 */
1125     NULL,                               /* pSetRelAbs */
1126     NULL,                               /* pSetStretchBltMode */
1127     NULL,                               /* pSetTextAlign */
1128     NULL,                               /* pSetTextCharacterExtra */
1129     NULL,                               /* pSetTextColor */
1130     NULL,                               /* pSetTextJustification */
1131     NULL,                               /* pSetViewportExt */
1132     NULL,                               /* pSetViewportOrg */
1133     NULL,                               /* pSetWindowExt */
1134     NULL,                               /* pSetWindowOrg */
1135     NULL,                               /* pSetWorldTransform */
1136     NULL,                               /* pStartDoc */
1137     NULL,                               /* pStartPage */
1138     windrv_StretchBlt,                  /* pStretchBlt */
1139     windrv_StretchDIBits,               /* pStretchDIBits */
1140     NULL,                               /* pStrokeAndFillPath */
1141     NULL,                               /* pStrokePath */
1142     NULL,                               /* pUnrealizePalette */
1143     NULL,                               /* pWidenPath */
1144     windrv_wine_get_wgl_driver,         /* wine_get_wgl_driver */
1145     GDI_PRIORITY_DIB_DRV + 10           /* priority */
1146 };