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