]> git.sesse.net Git - vlc/blob - modules/gui/skins2/win32/win32_graphics.cpp
Make Zorglub less unhappy
[vlc] / modules / gui / skins2 / win32 / win32_graphics.cpp
1 /*****************************************************************************
2  * win32_graphics.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #ifdef WIN32_SKINS
26
27 #define WINVER 0x500
28 #ifndef AC_SRC_ALPHA
29 #define AC_SRC_ALPHA 1
30 #endif
31
32 #include "win32_factory.hpp"
33 #include "win32_graphics.hpp"
34 #include "win32_window.hpp"
35 #include "../src/generic_bitmap.hpp"
36
37 Win32Graphics::Win32Graphics( intf_thread_t *pIntf, int width, int height ):
38     OSGraphics( pIntf ), m_width( width ), m_height( height ), m_hDC( NULL )
39 {
40     HBITMAP hBmp;
41     HDC hDC = GetDC( NULL );
42     hBmp = CreateCompatibleBitmap( hDC, m_width, m_height );
43     ReleaseDC( NULL, hDC );
44
45     m_hDC = CreateCompatibleDC( NULL );
46     SelectObject( m_hDC, hBmp );
47     DeleteObject( hBmp );
48
49     // Create the mask
50     m_mask = CreateRectRgn( 0, 0, 0, 0 );
51 }
52
53
54 Win32Graphics::~Win32Graphics()
55 {
56     DeleteDC( m_hDC );
57     DeleteObject( m_mask );
58 }
59
60
61 void Win32Graphics::clear()
62 {
63     // Clear the transparency mask
64     DeleteObject( m_mask );
65     m_mask = CreateRectRgn( 0, 0, 0, 0 );
66 }
67
68
69 void Win32Graphics::drawBitmap( const GenericBitmap &rBitmap,
70                                 int xSrc, int ySrc, int xDest, int yDest,
71                                 int width, int height, bool blend )
72 {
73     // Get the bitmap size if necessary
74     if( width == -1 )
75     {
76         width = rBitmap.getWidth();
77     }
78     if( height == -1 )
79     {
80         height = rBitmap.getHeight();
81     }
82
83     if( xDest + width > m_width || yDest + height > m_height )
84     {
85         msg_Err( getIntf(), "Bitmap too large !" );
86         return;
87     }
88
89     // Get a buffer on the image data
90     uint8_t *pBmpData = rBitmap.getData();
91     if( pBmpData == NULL )
92     {
93         // Nothing to draw
94         return;
95     }
96
97     void *pBits;     // pointer to DIB section
98     // Fill a BITMAPINFO structure
99     BITMAPINFO bmpInfo;
100     memset( &bmpInfo, 0, sizeof( bmpInfo ) );
101     bmpInfo.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
102     bmpInfo.bmiHeader.biWidth = width;
103     bmpInfo.bmiHeader.biHeight = -height;
104     bmpInfo.bmiHeader.biPlanes = 1;
105     bmpInfo.bmiHeader.biBitCount = 32;
106     bmpInfo.bmiHeader.biCompression = BI_RGB;
107     bmpInfo.bmiHeader.biSizeImage = width * height * 4;
108
109     // Create a DIB (Device Independant Bitmap) and associate it with
110     // a temporary DC
111     HDC hDC = CreateCompatibleDC( m_hDC );
112     HBITMAP hBmp = CreateDIBSection( hDC, &bmpInfo, DIB_RGB_COLORS,
113                                      &pBits, NULL, 0 );
114     SelectObject( hDC, hBmp );
115
116     // Mask for transparency
117     HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
118
119     // Skip the first lines of the image
120     pBmpData += 4 * ySrc * rBitmap.getWidth();
121
122     // Copy the bitmap on the image and compute the mask
123     for( int y = 0; y < height; y++ )
124     {
125         // Skip uninteresting bytes at the beginning of the line
126         pBmpData += 4 * xSrc;
127         // Flag to say whether the previous pixel on the line was visible
128         bool wasVisible = false;
129         // Beginning of the current visible segment on the line
130         int visibleSegmentStart = 0;
131         for( int x = 0; x < width; x++ )
132         {
133             uint8_t b = *(pBmpData++);
134             uint8_t g = *(pBmpData++);
135             uint8_t r = *(pBmpData++);
136             uint8_t a = *(pBmpData++);
137
138             // Draw the pixel
139             ((UINT32 *)pBits)[x + y * width] =
140                 (a << 24) | (r << 16) | (g << 8) | b;
141
142             if( a > 0 )
143             {
144                 // Pixel is visible
145                 if( ! wasVisible )
146                 {
147                     // Beginning of a visible segment
148                     visibleSegmentStart = x;
149                 }
150                 wasVisible = true;
151             }
152             else
153             {
154                 // Pixel is transparent
155                 if( wasVisible )
156                 {
157                     // End of a visible segment: add it to the mask
158                     addSegmentInRegion( mask, visibleSegmentStart, x, y );
159                 }
160                 wasVisible = false;
161             }
162         }
163         if( wasVisible )
164         {
165             // End of a visible segment: add it to the mask
166             addSegmentInRegion( mask, visibleSegmentStart, width, y );
167         }
168         // Skip uninteresting bytes at the end of the line
169         pBmpData += 4 * (rBitmap.getWidth() - width - xSrc);
170     }
171
172     // Apply the mask to the internal DC
173     OffsetRgn( mask, xDest, yDest );
174     SelectClipRgn( m_hDC, mask );
175
176     BLENDFUNCTION bf;      // structure for alpha blending
177     bf.BlendOp = AC_SRC_OVER;
178     bf.BlendFlags = 0;
179     bf.SourceConstantAlpha = 0xff;  // don't use constant alpha
180     bf.AlphaFormat = AC_SRC_ALPHA;
181
182     // Blend the image onto the internal DC
183     BOOL (WINAPI *AlphaBlend)( HDC, int, int, int, int, HDC, int, int,
184                                int, int, BLENDFUNCTION );
185     AlphaBlend = ((Win32Factory*)OSFactory::instance( getIntf() ))->AlphaBlend;
186     if( AlphaBlend &&
187         !AlphaBlend( m_hDC, xDest, yDest, width, height, hDC, 0, 0,
188                      width, height, bf ) )
189     {
190         msg_Err( getIntf(), "AlphaBlend() failed" );
191     }
192     else if( !AlphaBlend )
193     {
194         // Copy the image onto the internal DC
195         BitBlt( m_hDC, xDest, yDest, width, height, hDC, 0, 0, SRCCOPY );
196     }
197
198     // Add the bitmap mask to the global graphics mask
199     CombineRgn( m_mask, m_mask, mask, RGN_OR );
200
201     // Do cleanup
202     DeleteObject( hBmp );
203     DeleteObject( mask );
204     DeleteDC( hDC );
205 }
206
207
208 void Win32Graphics::drawGraphics( const OSGraphics &rGraphics, int xSrc,
209                                   int ySrc, int xDest, int yDest, int width,
210                                   int height )
211 {
212     if( width == -1 )
213     {
214         width = rGraphics.getWidth();
215     }
216     if( height == -1 )
217     {
218         height = rGraphics.getHeight();
219     }
220
221     // Create the mask for transparency
222     HRGN mask = CreateRectRgn( xSrc, ySrc, xSrc + width, ySrc + height );
223     CombineRgn( mask, ((Win32Graphics&)rGraphics).getMask(), mask, RGN_AND );
224     OffsetRgn( mask, xDest - xSrc, yDest - ySrc );
225
226     // Copy the image
227     HDC srcDC = ((Win32Graphics&)rGraphics).getDC();
228     SelectClipRgn( m_hDC, mask );
229     BitBlt( m_hDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
230
231     // Add the source mask to the mask of the graphics
232     CombineRgn( m_mask, mask, m_mask, RGN_OR );
233     DeleteObject( mask );
234 }
235
236
237 void Win32Graphics::fillRect( int left, int top, int width, int height,
238                               uint32_t color )
239 {
240     // Update the mask with the rectangle area
241     HRGN newMask = CreateRectRgn( left, top, left + width, top + height );
242     CombineRgn( m_mask, m_mask, newMask, RGN_OR );
243     SelectClipRgn( m_hDC, m_mask );
244     DeleteObject( newMask );
245
246     // Create a brush with the color
247     int red = (color & 0xff0000) >> 16;
248     int green = (color & 0xff00) >> 8;
249     int blue = color & 0xff;
250     HBRUSH hBrush = CreateSolidBrush( RGB( red, green, blue ) );
251
252     // Draw the rectangle
253     RECT r;
254     r.left = left;
255     r.top = top;
256     r.right = left + width;
257     r.bottom = top + height;
258     FillRect( m_hDC, &r, hBrush );
259     DeleteObject( hBrush );
260 }
261
262
263 void Win32Graphics::drawRect( int left, int top, int width, int height,
264                               uint32_t color )
265 {
266     // Update the mask with the rectangle
267     HRGN l1 = CreateRectRgn( left, top, left + width, top + 1 );
268     HRGN l2 = CreateRectRgn( left + width - 1, top,
269                              left + width, top + height );
270     HRGN l3 = CreateRectRgn( left, top + height - 1,
271                              left + width, top + height );
272     HRGN l4 = CreateRectRgn( left, top, left + 1, top + height );
273     CombineRgn( m_mask, m_mask, l1, RGN_OR );
274     CombineRgn( m_mask, m_mask, l2, RGN_OR );
275     CombineRgn( m_mask, m_mask, l3, RGN_OR );
276     CombineRgn( m_mask, m_mask, l4, RGN_OR );
277     DeleteObject( l1 );
278     DeleteObject( l2 );
279     DeleteObject( l3 );
280     DeleteObject( l4 );
281
282     SelectClipRgn( m_hDC, m_mask );
283
284     // Create a pen with the color
285     int red = (color & 0xff0000) >> 16;
286     int green = (color & 0xff00) >> 8;
287     int blue = color & 0xff;
288     HPEN hPen = CreatePen( PS_SOLID, 0, RGB( red, green, blue ) );
289     SelectObject( m_hDC, hPen );
290
291     // Draw the rectangle
292     MoveToEx( m_hDC, left, top, NULL );
293     LineTo( m_hDC, left + width - 1, top );
294     LineTo( m_hDC, left + width - 1, top + height - 1 );
295     LineTo( m_hDC, left, top + height - 1 );
296     LineTo( m_hDC, left, top );
297
298     // Delete the pen
299     DeleteObject( hPen );
300 }
301
302
303 void Win32Graphics::applyMaskToWindow( OSWindow &rWindow )
304 {
305     // Get window handle
306     HWND hWnd = ((Win32Window&)rWindow).getHandle();
307
308     // Apply the mask
309     // We need to copy the mask, because SetWindowRgn modifies it in our back
310     HRGN mask = CreateRectRgn( 0, 0, 0, 0 );
311     CombineRgn( mask, m_mask, NULL, RGN_COPY );
312     SetWindowRgn( hWnd, mask, TRUE );
313 }
314
315
316 void Win32Graphics::copyToWindow( OSWindow &rWindow, int xSrc, int ySrc,
317                                   int width, int height, int xDest, int yDest )
318 {
319     // Initialize painting
320     HWND hWnd = ((Win32Window&)rWindow).getHandle();
321     HDC wndDC = GetWindowDC( hWnd );
322     HDC srcDC = m_hDC;
323
324     // Draw image on window
325     BitBlt( wndDC, xDest, yDest, width, height, srcDC, xSrc, ySrc, SRCCOPY );
326
327     // Release window device context
328     ReleaseDC( hWnd, wndDC );
329 }
330
331
332 bool Win32Graphics::hit( int x, int y ) const
333 {
334     return PtInRegion( m_mask, x, y );
335 }
336
337
338 void Win32Graphics::addSegmentInRegion( HRGN &rMask, int start,
339                                         int end, int line )
340 {
341     HRGN buffer = CreateRectRgn( start, line, end, line + 1 );
342     CombineRgn( rMask, buffer, rMask, RGN_OR );
343     DeleteObject( buffer );
344 }
345
346 #endif