1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <X11/Xutil.h>
30 #include <X11/extensions/shape.h>
32 #include "x11_display.hpp"
33 #include "x11_graphics.hpp"
34 #include "x11_window.hpp"
35 #include "../src/generic_bitmap.hpp"
38 X11Graphics::X11Graphics( intf_thread_t *pIntf, X11Display &rDisplay,
39 int width, int height ):
40 OSGraphics( pIntf ), m_rDisplay( rDisplay ), m_width( width ),
43 // Get the display paramaters
44 int screen = DefaultScreen( XDISPLAY );
45 int depth = DefaultDepth( XDISPLAY, screen );
47 // X11 doesn't accept that !
48 if( width == 0 || height == 0 )
50 // Avoid a X11 Bad Value error
52 msg_Err( getIntf(), "Invalid image size (null width or height)" );
56 m_pixmap = XCreatePixmap( XDISPLAY, DefaultRootWindow( XDISPLAY ),
57 width, height, depth);
59 // Create the transparency mask (everything is transparent initially)
60 m_mask = XCreateRegion();
62 // Create a Graphics Context that does not generate GraphicsExpose events
64 xgcvalues.graphics_exposures = False;
65 m_gc = XCreateGC( XDISPLAY, m_pixmap, GCGraphicsExposures, &xgcvalues );
69 X11Graphics::~X11Graphics()
71 XFreeGC( XDISPLAY, m_gc );
72 XDestroyRegion( m_mask );
73 XFreePixmap( XDISPLAY, m_pixmap );
77 void X11Graphics::clear()
79 // Clear the transparency mask
80 XDestroyRegion( m_mask );
81 m_mask = XCreateRegion();
85 void X11Graphics::drawGraphics( const OSGraphics &rGraphics, int xSrc,
86 int ySrc, int xDest, int yDest, int width,
91 width = rGraphics.getWidth();
95 height = rGraphics.getHeight();
99 Drawable src = ((X11Graphics&)rGraphics).getDrawable();
101 // Create the mask for transparency
102 Region voidMask = XCreateRegion();
107 rect.height = height;
108 Region clipMask = XCreateRegion();
109 XUnionRectWithRegion( &rect, voidMask, clipMask );
110 Region mask = XCreateRegion();
111 XIntersectRegion( ((X11Graphics&)rGraphics).getMask(), clipMask, mask );
112 XDestroyRegion( clipMask );
113 XDestroyRegion( voidMask );
114 XOffsetRegion( mask, xDest - xSrc, yDest - ySrc );
117 XSetRegion( XDISPLAY, m_gc, mask );
118 XCopyArea( XDISPLAY, src, m_pixmap, m_gc, xSrc, ySrc, width, height,
121 // Add the source mask to the mask of the graphics
122 Region newMask = XCreateRegion();
123 XUnionRegion( m_mask, mask, newMask );
124 XDestroyRegion( mask );
125 XDestroyRegion( m_mask );
130 void X11Graphics::drawBitmap( const GenericBitmap &rBitmap, int xSrc,
131 int ySrc, int xDest, int yDest, int width,
132 int height, bool blend )
134 // Get the bitmap size if necessary
137 width = rBitmap.getWidth();
139 else if( width > rBitmap.getWidth() )
141 msg_Dbg( getIntf(), "Bitmap width too small!" );
142 width = rBitmap.getWidth();
146 height = rBitmap.getHeight();
148 else if( height > rBitmap.getHeight() )
150 msg_Dbg( getIntf(), "Bitmap height too small!" );
151 height = rBitmap.getHeight();
154 // Nothing to draw if width or height is null
155 if( width == 0 || height == 0 )
160 // Safety check for debugging purpose
161 if( xDest + width > m_width || yDest + height > m_height )
163 msg_Dbg( getIntf(), "Bitmap too large !" );
167 // Get a buffer on the image data
168 uint8_t *pBmpData = rBitmap.getData();
169 if( pBmpData == NULL )
175 // Get the image from the pixmap
176 XImage *pImage = XGetImage( XDISPLAY, m_pixmap, xDest, yDest, width,
177 height, AllPlanes, ZPixmap );
180 msg_Dbg( getIntf(), "XGetImage returned NULL" );
183 char *pData = pImage->data;
185 // Get the padding of this image
186 int pad = pImage->bitmap_pad >> 3;
187 int shift = ( pad - ( (width * XPIXELSIZE) % pad ) ) % pad;
189 // Mask for transparency
190 Region mask = XCreateRegion();
192 // Get a pointer on the right X11Display::makePixel method
193 X11Display::MakePixelFunc_t makePixelFunc = ( blend ?
194 m_rDisplay.getBlendPixel() : m_rDisplay.getPutPixel() );
196 // Skip the first lines of the image
197 pBmpData += 4 * ySrc * rBitmap.getWidth();
199 // Copy the bitmap on the image and compute the mask
200 for( int y = 0; y < height; y++ )
202 // Skip uninteresting bytes at the beginning of the line
203 pBmpData += 4 * xSrc;
204 // Flag to say whether the previous pixel on the line was visible
205 bool wasVisible = false;
206 // Beginning of the current visible segment on the line
207 int visibleSegmentStart = 0;
208 for( int x = 0; x < width; x++ )
210 uint8_t b = *(pBmpData++);
211 uint8_t g = *(pBmpData++);
212 uint8_t r = *(pBmpData++);
213 uint8_t a = *(pBmpData++);
215 (m_rDisplay.*makePixelFunc)( (uint8_t*)pData, r, g, b, a );
222 // Beginning of a visible segment
223 visibleSegmentStart = x;
229 // Pixel is transparent
232 // End of a visible segment: add it to the mask
233 addHSegmentInRegion( mask, visibleSegmentStart, x, y );
240 // End of a visible segment: add it to the mask
241 addHSegmentInRegion( mask, visibleSegmentStart, width, y );
244 // Skip uninteresting bytes at the end of the line
245 pBmpData += 4 * (rBitmap.getWidth() - width - xSrc);
248 // Apply the mask to the graphics context
249 XOffsetRegion( mask, xDest, yDest );
250 XSetRegion( XDISPLAY, m_gc, mask );
251 // Copy the image on the pixmap
252 XPutImage( XDISPLAY, m_pixmap, m_gc, pImage, 0, 0, xDest, yDest, width,
254 XDestroyImage( pImage );
256 // Add the bitmap mask to the global graphics mask
257 Region newMask = XCreateRegion();
258 XUnionRegion( mask, m_mask, newMask );
259 XDestroyRegion( m_mask );
262 XDestroyRegion( mask );
266 void X11Graphics::fillRect( int left, int top, int width, int height,
269 // Update the mask with the rectangle area
270 Region newMask = XCreateRegion();
275 rect.height = height;
276 XUnionRectWithRegion( &rect, m_mask, newMask );
277 XDestroyRegion( m_mask );
280 // Draw the rectangle
282 gcVal.foreground = m_rDisplay.getPixelValue( color >> 16, color >> 8, color );
283 XChangeGC( XDISPLAY, m_gc, GCForeground, &gcVal );
284 XSetRegion( XDISPLAY, m_gc, m_mask );
285 XFillRectangle( XDISPLAY, m_pixmap, m_gc, left, top, width, height );
289 void X11Graphics::drawRect( int left, int top, int width, int height,
292 // Update the mask with the rectangle
293 addHSegmentInRegion( m_mask, left, left + width, top );
294 addHSegmentInRegion( m_mask, left, left + width, top + height );
295 addVSegmentInRegion( m_mask, top, top + height, left );
296 addVSegmentInRegion( m_mask, top, top + height, left + width );
298 // Draw the rectangle
300 gcVal.foreground = m_rDisplay.getPixelValue( color >> 16, color >> 8, color );
301 XChangeGC( XDISPLAY, m_gc, GCForeground, &gcVal );
302 XSetRegion( XDISPLAY, m_gc, m_mask );
303 XDrawRectangle( XDISPLAY, m_pixmap, m_gc, left, top, width - 1, height - 1 );
307 void X11Graphics::applyMaskToWindow( OSWindow &rWindow )
309 // Get the target window
310 Window win = ((X11Window&)rWindow).getDrawable();
312 // Change the shape of the window
313 XShapeCombineRegion( XDISPLAY, win, ShapeBounding, 0, 0, m_mask,
318 void X11Graphics::copyToWindow( OSWindow &rWindow, int xSrc, int ySrc,
319 int width, int height, int xDest, int yDest )
321 // Destination window
322 Drawable dest = ((X11Window&)rWindow).getDrawable();
324 XCopyArea( XDISPLAY, m_pixmap, dest, XGC, xSrc, ySrc, width, height,
329 bool X11Graphics::hit( int x, int y ) const
331 return XPointInRegion( m_mask, x, y );
335 inline void X11Graphics::addHSegmentInRegion( Region &rMask, int xStart,
341 rect.width = xEnd - xStart;
343 Region newMask = XCreateRegion();
344 XUnionRectWithRegion( &rect, rMask, newMask );
345 XDestroyRegion( rMask );
350 inline void X11Graphics::addVSegmentInRegion( Region &rMask, int yStart,
357 rect.height = yEnd - yStart;
358 Region newMask = XCreateRegion();
359 XUnionRectWithRegion( &rect, rMask, newMask );
360 XDestroyRegion( rMask );