winex11.drv: Remove remaining pixmap prefix to fix compilation without libxrender.
[wine] / dlls / winex11.drv / brush.c
1 /*
2  * X11DRV brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
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 "config.h"
22
23 #include <stdlib.h>
24
25 #include "wine/winbase16.h"
26 #include "x11drv.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
30
31 static const char HatchBrushes[][8] =
32 {
33     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
34     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
35     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL  */
36     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL  */
37     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
38     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS  */
39 };
40
41   /* Levels of each primary for dithering */
42 #define PRIMARY_LEVELS  3
43 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
44
45  /* Dithering matrix size  */
46 #define MATRIX_SIZE     8
47 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
48
49   /* Total number of possible levels for a dithered primary color */
50 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
51
52   /* Dithering matrix */
53 static const int dither_matrix[MATRIX_SIZE_2] =
54 {
55      0, 32,  8, 40,  2, 34, 10, 42,
56     48, 16, 56, 24, 50, 18, 58, 26,
57     12, 44,  4, 36, 14, 46,  6, 38,
58     60, 28, 52, 20, 62, 30, 54, 22,
59      3, 35, 11, 43,  1, 33,  9, 41,
60     51, 19, 59, 27, 49, 17, 57, 25,
61     15, 47,  7, 39, 13, 45,  5, 37,
62     63, 31, 55, 23, 61, 29, 53, 21
63 };
64
65   /* Mapping between (R,G,B) triples and EGA colors */
66 static const int EGAmapping[TOTAL_LEVELS] =
67 {
68     0,  /* 000000 -> 000000 */
69     4,  /* 00007f -> 000080 */
70     12, /* 0000ff -> 0000ff */
71     2,  /* 007f00 -> 008000 */
72     6,  /* 007f7f -> 008080 */
73     6,  /* 007fff -> 008080 */
74     10, /* 00ff00 -> 00ff00 */
75     6,  /* 00ff7f -> 008080 */
76     14, /* 00ffff -> 00ffff */
77     1,  /* 7f0000 -> 800000 */
78     5,  /* 7f007f -> 800080 */
79     5,  /* 7f00ff -> 800080 */
80     3,  /* 7f7f00 -> 808000 */
81     8,  /* 7f7f7f -> 808080 */
82     7,  /* 7f7fff -> c0c0c0 */
83     3,  /* 7fff00 -> 808000 */
84     7,  /* 7fff7f -> c0c0c0 */
85     7,  /* 7fffff -> c0c0c0 */
86     9,  /* ff0000 -> ff0000 */
87     5,  /* ff007f -> 800080 */
88     13, /* ff00ff -> ff00ff */
89     3,  /* ff7f00 -> 808000 */
90     7,  /* ff7f7f -> c0c0c0 */
91     7,  /* ff7fff -> c0c0c0 */
92     11, /* ffff00 -> ffff00 */
93     7,  /* ffff7f -> c0c0c0 */
94     15  /* ffffff -> ffffff */
95 };
96
97 #define PIXEL_VALUE(r,g,b) \
98     X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
99
100 static const COLORREF BLACK = RGB(0, 0, 0);
101 static const COLORREF WHITE = RGB(0xff, 0xff, 0xff);
102
103 /***********************************************************************
104  *           BRUSH_DitherColor
105  */
106 static Pixmap BRUSH_DitherColor( COLORREF color, int depth)
107 {
108     /* X image for building dithered pixmap */
109     static XImage *ditherImage = NULL;
110     static COLORREF prevColor = 0xffffffff;
111     unsigned int x, y;
112     Pixmap pixmap;
113     GC gc = get_bitmap_gc(depth);
114
115     if (!ditherImage)
116     {
117         ditherImage = X11DRV_DIB_CreateXImage( MATRIX_SIZE, MATRIX_SIZE, depth );
118         if (!ditherImage) 
119         {
120             ERR("Could not create dither image\n");
121             return 0;
122         }
123     }
124
125     wine_tsx11_lock();
126     if (color != prevColor)
127     {
128         int r = GetRValue( color ) * DITHER_LEVELS;
129         int g = GetGValue( color ) * DITHER_LEVELS;
130         int b = GetBValue( color ) * DITHER_LEVELS;
131         const int *pmatrix = dither_matrix;
132
133         for (y = 0; y < MATRIX_SIZE; y++)
134         {
135             for (x = 0; x < MATRIX_SIZE; x++)
136             {
137                 int d  = *pmatrix++ * 256;
138                 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
139                 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
140                 int db = ((b + d) / MATRIX_SIZE_2) / 256;
141                 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
142             }
143         }
144         prevColor = color;
145     }
146
147     pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, depth );
148     XPutImage( gdi_display, pixmap, gc, ditherImage, 0, 0,
149                0, 0, MATRIX_SIZE, MATRIX_SIZE );
150     wine_tsx11_unlock();
151
152     return pixmap;
153 }
154
155
156 /***********************************************************************
157  *           BRUSH_DitherMono
158  */
159 static Pixmap BRUSH_DitherMono( COLORREF color )
160 {
161     /* This makes the spray work in Win 3.11 pbrush.exe */
162     /* FIXME. Extend this basic selection of dither patterns */
163     static const char gray_dither[][2] = {{ 0x1, 0x0 }, /* DKGRAY */
164                                           { 0x2, 0x1 }, /* GRAY */
165                                           { 0x1, 0x3 }, /* LTGRAY */
166     };                                      
167     int gray = (30 * GetRValue(color) + 59 * GetGValue(color) + 11 * GetBValue(color)) / 100;
168     int idx = gray * (sizeof gray_dither/sizeof gray_dither[0] + 1)/256 - 1;
169     Pixmap pixmap;
170
171     TRACE("color=%06x -> gray=%x\n", color, gray);
172
173     wine_tsx11_lock();
174     pixmap = XCreateBitmapFromData( gdi_display, root_window, 
175                                     gray_dither[idx],
176                                     2, 2 );
177     wine_tsx11_unlock();
178     return pixmap;
179 }
180
181 /***********************************************************************
182  *           BRUSH_SelectSolidBrush
183  */
184 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
185 {
186     COLORREF colorRGB = X11DRV_PALETTE_GetColor( physDev, color );
187     if ((physDev->depth > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
188     {
189           /* Dithered brush */
190         physDev->brush.pixmap = BRUSH_DitherColor( colorRGB, physDev->depth );
191         physDev->brush.fillStyle = FillTiled;
192         physDev->brush.pixel = 0;
193     }
194     else if (physDev->depth == 1 && colorRGB != WHITE && colorRGB != BLACK)
195     {
196         physDev->brush.pixel = 0;
197         physDev->brush.pixmap = BRUSH_DitherMono( colorRGB );
198         physDev->brush.fillStyle = FillTiled;
199     }
200     else
201     {
202           /* Solid brush */
203         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
204         physDev->brush.fillStyle = FillSolid;
205     }
206 }
207
208
209 /***********************************************************************
210  *           BRUSH_SelectPatternBrush
211  */
212 static BOOL BRUSH_SelectPatternBrush( X11DRV_PDEVICE *physDev, HBITMAP hbitmap )
213 {
214     BITMAP bitmap;
215     X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( hbitmap );
216
217     if (!physBitmap || !GetObjectW( hbitmap, sizeof(bitmap), &bitmap )) return FALSE;
218
219     X11DRV_DIB_Lock( physBitmap, DIB_Status_GdiMod );
220
221     if ((physDev->depth == 1) && (physBitmap->depth != 1))
222     {
223         wine_tsx11_lock();
224         /* Special case: a color pattern on a monochrome DC */
225         physDev->brush.pixmap = XCreatePixmap( gdi_display, root_window,
226                                                bitmap.bmWidth, bitmap.bmHeight, 1);
227         /* FIXME: should probably convert to monochrome instead */
228         XCopyPlane( gdi_display, physBitmap->pixmap, physDev->brush.pixmap,
229                     get_bitmap_gc(1), 0, 0, bitmap.bmWidth, bitmap.bmHeight, 0, 0, 1 );
230         wine_tsx11_unlock();
231     }
232     else
233     {
234         /* XRender is needed because of possible depth conversion */
235         X11DRV_XRender_CopyBrush(physDev, physBitmap, bitmap.bmWidth, bitmap.bmHeight);
236     }
237
238     X11DRV_DIB_Unlock( physBitmap, TRUE );
239
240     if (physBitmap->depth > 1)
241     {
242         physDev->brush.fillStyle = FillTiled;
243         physDev->brush.pixel = 0;  /* Ignored */
244     }
245     else
246     {
247         physDev->brush.fillStyle = FillOpaqueStippled;
248         physDev->brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
249     }
250     return TRUE;
251 }
252
253
254 /***********************************************************************
255  *           BRUSH_SelectDIBPatternBrush
256  */
257 static BOOL BRUSH_SelectDIBPatternBrush( X11DRV_PDEVICE *physDev, HGLOBAL mem )
258 {
259     BOOL ret;
260     HDC memdc;
261     BITMAPINFO *info = GlobalLock( mem );
262     HBITMAP bitmap = CreateDIBitmap( physDev->dev.hdc, &info->bmiHeader, CBM_INIT,
263                                      (LPBYTE)info + bitmap_info_size( info, DIB_RGB_COLORS ),
264                                      info, DIB_RGB_COLORS );
265
266     /* make sure it's owned by x11drv */
267     memdc = CreateCompatibleDC( physDev->dev.hdc );
268     SelectObject( memdc, bitmap );
269     DeleteDC( memdc );
270
271     if ((ret = BRUSH_SelectPatternBrush( physDev, bitmap )))
272     {
273         X_PHYSBITMAP *physBitmap = X11DRV_get_phys_bitmap( bitmap );
274         physBitmap->pixmap = 0;  /* so it doesn't get freed */
275     }
276     DeleteObject( bitmap );
277     GlobalUnlock( mem );
278     return ret;
279 }
280
281
282 /***********************************************************************
283  *           SelectBrush   (X11DRV.@)
284  */
285 HBRUSH X11DRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
286 {
287     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
288     LOGBRUSH logbrush;
289
290     if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
291
292     TRACE("hdc=%p hbrush=%p\n", dev->hdc, hbrush);
293
294     if (physDev->brush.pixmap)
295     {
296         wine_tsx11_lock();
297         XFreePixmap( gdi_display, physDev->brush.pixmap );
298         wine_tsx11_unlock();
299         physDev->brush.pixmap = 0;
300     }
301     physDev->brush.style = logbrush.lbStyle;
302     if (hbrush == GetStockObject( DC_BRUSH ))
303         logbrush.lbColor = GetDCBrushColor( dev->hdc );
304
305     switch(logbrush.lbStyle)
306     {
307       case BS_NULL:
308         TRACE("BS_NULL\n" );
309         break;
310
311       case BS_SOLID:
312         TRACE("BS_SOLID\n" );
313         BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
314         break;
315
316       case BS_HATCHED:
317         TRACE("BS_HATCHED\n" );
318         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
319         wine_tsx11_lock();
320         physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
321                                                        HatchBrushes[logbrush.lbHatch], 8, 8 );
322         wine_tsx11_unlock();
323         physDev->brush.fillStyle = FillStippled;
324         break;
325
326       case BS_PATTERN:
327         TRACE("BS_PATTERN\n");
328         if (!BRUSH_SelectPatternBrush( physDev, (HBITMAP)logbrush.lbHatch )) return 0;
329         break;
330
331       case BS_DIBPATTERN:
332         TRACE("BS_DIBPATTERN\n");
333         if (!BRUSH_SelectDIBPatternBrush( physDev, (HGLOBAL)logbrush.lbHatch )) return 0;
334         break;
335     }
336     return hbrush;
337 }
338
339
340 /***********************************************************************
341  *           SetDCBrushColor (X11DRV.@)
342  */
343 COLORREF X11DRV_SetDCBrushColor( PHYSDEV dev, COLORREF crColor )
344 {
345     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
346
347     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
348         BRUSH_SelectSolidBrush( physDev, crColor );
349
350     return crColor;
351 }