1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _SV_XRENDER_PEER_HXX
32 #define _SV_XRENDER_PEER_HXX
34 #include <tools/prex.h>
35 #include <X11/extensions/Xrender.h>
36 #include <tools/postx.h>
38 #include <vcl/salgtype.hxx>
39 #include <osl/module.h>
44 static XRenderPeer
& GetInstance();
45 int GetVersion() const;
47 sal_uInt32
InitRenderText( int nMaxDepth
);
55 XRenderPictFormat
* mpStandardFormatA8
;
57 oslModule mpRenderLib
;
60 XRenderPictFormat
* GetStandardFormatA8() const;
61 XRenderPictFormat
* FindStandardFormat(int nFormat
) const;
63 // the methods below are thin wrappers for the XRENDER API
64 XRenderPictFormat
* FindVisualFormat( Visual
* ) const;
65 XRenderPictFormat
* FindPictureFormat( unsigned long nMask
,
66 const XRenderPictFormat
& ) const;
67 Picture
CreatePicture( Drawable
, const XRenderPictFormat
*,
68 unsigned long nDrawable
, const XRenderPictureAttributes
* ) const;
69 void SetPictureClipRegion( Picture
, XLIB_Region
) const;
70 void CompositePicture( int nOp
, Picture aSrc
, Picture aMask
, Picture aDst
,
71 int nXSrc
, int nYSrc
, int nXMask
, int nYMask
,
72 int nXDst
, int nYDst
, unsigned nWidth
, unsigned nHeight
) const;
73 void FreePicture( Picture
) const;
75 GlyphSet
CreateGlyphSet() const;
76 void FreeGlyphSet( GlyphSet
) const;
77 void AddGlyph( GlyphSet
, Glyph nGlyphId
, const XGlyphInfo
&,
78 const char* pBuffer
, int nBufSize
) const;
79 void FreeGlyph( GlyphSet
, Glyph nGlyphId
) const;
80 void CompositeString32( Picture aSrc
, Picture aDst
, GlyphSet
,
81 int nDstX
, int nDstY
, const unsigned* pText
, int nTextLen
) const;
82 void FillRectangle( int nOp
, Picture aDst
, const XRenderColor
*,
83 int nX
, int nY
, unsigned nW
, unsigned nH
) const;
84 void CompositeTrapezoids( int nOp
, Picture aSrc
, Picture aDst
,
85 const XRenderPictFormat
*, int nXSrc
, int nYSrc
,
86 const XTrapezoid
*, int nCount
) const;
87 bool AddTraps( Picture aDst
, int nXOfs
, int nYOfs
,
88 const XTrap
*, int nCount
) const;
90 bool AreTrapezoidsSupported() const
94 { return mpXRenderCompositeTrapezoids
!=NULL
; }
97 XRenderPictFormat
* (*mpXRenderFindFormat
)(Display
*,unsigned long,
98 const XRenderPictFormat
*,int);
99 XRenderPictFormat
* (*mpXRenderFindVisualFormat
)(Display
*,Visual
*);
100 XRenderPictFormat
* (*mpXRenderFindStandardFormat
)(Display
*,int);
101 Bool (*mpXRenderQueryExtension
)(Display
*,int*,int*);
102 void (*mpXRenderQueryVersion
)(Display
*,int*,int*);
104 Picture (*mpXRenderCreatePicture
)(Display
*,Drawable
, const XRenderPictFormat
*,
105 unsigned long,const XRenderPictureAttributes
*);
106 void (*mpXRenderSetPictureClipRegion
)(Display
*,Picture
,XLIB_Region
);
107 void (*mpXRenderFreePicture
)(Display
*,Picture
);
108 void (*mpXRenderComposite
)(Display
*,int,Picture
,Picture
,Picture
,
109 int,int,int,int,int,int,unsigned,unsigned);
111 GlyphSet (*mpXRenderCreateGlyphSet
)(Display
*, const XRenderPictFormat
*);
112 void (*mpXRenderFreeGlyphSet
)(Display
*,GlyphSet
);
113 void (*mpXRenderAddGlyphs
)(Display
*,GlyphSet
,Glyph
*,
114 const XGlyphInfo
*,int,const char*,int);
115 void (*mpXRenderFreeGlyphs
)(Display
*,GlyphSet
,Glyph
*,int);
116 void (*mpXRenderCompositeString32
)(Display
*,int,Picture
,Picture
,
117 const XRenderPictFormat
*,GlyphSet
,int,int,int,int,const unsigned*,int);
118 void (*mpXRenderFillRectangle
)(Display
*,int,Picture
,
119 const XRenderColor
*,int,int,unsigned int,unsigned int);
120 void (*mpXRenderCompositeTrapezoids
)(Display
*,int,Picture
,Picture
,
121 const XRenderPictFormat
*,int,int,const XTrapezoid
*,int);
122 void (*mpXRenderAddTraps
)(Display
*,Picture
,int,int,const XTrap
*,int);
123 #endif // XRENDER_LINK
126 //=====================================================================
131 ScopedPic( XRenderPeer
& rPeer
, Picture
& rPic
);
136 XRenderPeer
& mrRenderPeer
;
139 private: // prevent copy and assignmet
140 ScopedPic( const ScopedPic
& );
141 void operator=( const ScopedPic
& );
144 //=====================================================================
146 inline int XRenderPeer::GetVersion() const
148 return mnRenderVersion
;
151 inline XRenderPictFormat
* XRenderPeer::GetStandardFormatA8() const
153 return mpStandardFormatA8
;
156 inline XRenderPictFormat
* XRenderPeer::FindStandardFormat(int nFormat
) const
159 return XRenderFindStandardFormat(mpDisplay
, nFormat
);
161 return (*mpXRenderFindStandardFormat
)(mpDisplay
, nFormat
);
165 inline XRenderPictFormat
* XRenderPeer::FindVisualFormat( Visual
* pVisual
) const
168 return XRenderFindVisualFormat ( mpDisplay
, pVisual
);
170 return (*mpXRenderFindVisualFormat
)( mpDisplay
, pVisual
);
174 inline XRenderPictFormat
* XRenderPeer::FindPictureFormat( unsigned long nFormatMask
,
175 const XRenderPictFormat
& rFormatAttr
) const
178 return XRenderFindFormat( mpDisplay
, nFormatMask
, &rFormatAttr
, 0 );
180 return (*mpXRenderFindFormat
)( mpDisplay
, nFormatMask
, &rFormatAttr
, 0 );
184 inline Picture
XRenderPeer::CreatePicture( Drawable aDrawable
,
185 const XRenderPictFormat
* pVisFormat
, unsigned long nValueMask
,
186 const XRenderPictureAttributes
* pRenderAttr
) const
189 return XRenderCreatePicture( mpDisplay
, aDrawable
, pVisFormat
,
190 nValueMask
, pRenderAttr
);
192 return (*mpXRenderCreatePicture
)( mpDisplay
, aDrawable
, pVisFormat
,
193 nValueMask
, pRenderAttr
);
197 inline void XRenderPeer::SetPictureClipRegion( Picture aPicture
,
198 XLIB_Region aXlibRegion
) const
201 XRenderSetPictureClipRegion( mpDisplay
, aPicture
, aXlibRegion
);
203 (*mpXRenderSetPictureClipRegion
)( mpDisplay
, aPicture
, aXlibRegion
);
207 inline void XRenderPeer::CompositePicture( int nXRenderOp
,
208 Picture aSrcPic
, Picture aMaskPic
, Picture aDstPic
,
209 int nSrcX
, int nSrcY
, int nMaskX
, int nMaskY
, int nDstX
, int nDstY
,
210 unsigned nWidth
, unsigned nHeight
) const
213 XRenderComposite( mpDisplay
, nXRenderOp
, aSrcPic
, aMaskPic
, aDstPic
,
214 nSrcX
, nSrcY
, nMaskX
, nMaskY
, nDstX
, nDstY
, nWidth
, nHeight
);
216 (*mpXRenderComposite
)( mpDisplay
, nXRenderOp
, aSrcPic
, aMaskPic
, aDstPic
,
217 nSrcX
, nSrcY
, nMaskX
, nMaskY
, nDstX
, nDstY
, nWidth
, nHeight
);
221 inline void XRenderPeer::FreePicture( Picture aPicture
) const
224 XRenderFreePicture( mpDisplay
, aPicture
);
226 (*mpXRenderFreePicture
)( mpDisplay
, aPicture
);
230 inline GlyphSet
XRenderPeer::CreateGlyphSet() const
233 return XRenderCreateGlyphSet( mpDisplay
, mpStandardFormatA8
);
235 return (*mpXRenderCreateGlyphSet
)( mpDisplay
, mpStandardFormatA8
);
239 inline void XRenderPeer::FreeGlyphSet( GlyphSet aGS
) const
242 XRenderFreeGlyphSet( mpDisplay
, aGS
);
244 (*mpXRenderFreeGlyphSet
)( mpDisplay
, aGS
);
248 inline void XRenderPeer::AddGlyph( GlyphSet aGS
, Glyph nGlyphId
,
249 const XGlyphInfo
& rGI
, const char* pBuffer
, int nBufSize
) const
252 XRenderAddGlyphs( mpDisplay
, aGS
, &nGlyphId
, &rGI
, 1,
253 const_cast<char*>(pBuffer
), nBufSize
);
255 (*mpXRenderAddGlyphs
)( mpDisplay
, aGS
, &nGlyphId
, &rGI
, 1,
256 const_cast<char*>(pBuffer
), nBufSize
);
260 inline void XRenderPeer::FreeGlyph( GlyphSet aGS
, Glyph nGlyphId
) const
262 (void)aGS
; (void)nGlyphId
;
264 // XRenderFreeGlyphs not implemented yet for version<=0.2
265 // #108209# disabled because of crash potential,
266 // the glyph leak is not too bad because they will
267 // be cleaned up when the glyphset is released
268 #if 0 // TODO: reenable when it works without problems
269 if( mnRenderVersion
>= 0x05 )
272 XRenderFreeGlyphs( mpDisplay
, aGS
, &nGlyphId
, 1 );
274 (*mpXRenderFreeGlyphs
)( mpDisplay
, aGS
, &nGlyphId
, 1 );
280 inline void XRenderPeer::CompositeString32( Picture aSrc
, Picture aDst
,
281 GlyphSet aGlyphSet
, int nDstX
, int nDstY
,
282 const unsigned* pText
, int nTextLen
) const
285 XRenderCompositeString32( mpDisplay
, PictOpOver
, aSrc
, aDst
, NULL
,
286 aGlyphSet
, 0, 0, nDstX
, nDstY
, pText
, nTextLen
);
288 (*mpXRenderCompositeString32
)( mpDisplay
, PictOpOver
, aSrc
, aDst
, NULL
,
289 aGlyphSet
, 0, 0, nDstX
, nDstY
, pText
, nTextLen
);
293 inline void XRenderPeer::FillRectangle( int a
, Picture b
, const XRenderColor
* c
,
294 int d
, int e
, unsigned int f
, unsigned int g
) const
297 XRenderFillRectangle( mpDisplay
, a
, b
, c
, d
, e
, f
, g
);
299 (*mpXRenderFillRectangle
)( mpDisplay
, a
, b
, c
, d
, e
, f
, g
);
304 inline void XRenderPeer::CompositeTrapezoids( int nOp
,
305 Picture aSrc
, Picture aDst
, const XRenderPictFormat
* pXRPF
,
306 int nXSrc
, int nYSrc
, const XTrapezoid
* pXT
, int nCount
) const
309 XRenderCompositeTrapezoids( mpDisplay
, nOp
, aSrc
, aDst
, pXRPF
,
310 nXSrc
, nYSrc
, pXT
, nCount
);
312 (*mpXRenderCompositeTrapezoids
)( mpDisplay
, nOp
, aSrc
, aDst
, pXRPF
,
313 nXSrc
, nYSrc
, pXT
, nCount
);
317 inline bool XRenderPeer::AddTraps( Picture aDst
, int nXOfs
, int nYOfs
,
318 const XTrap
* pTraps
, int nCount
) const
321 XRenderAddTraps( mpDisplay
, aDst
, nXOfs
, nYOfs
, pTraps
, nCount
);
323 if( !mpXRenderAddTraps
)
325 (*mpXRenderAddTraps
)( mpDisplay
, aDst
, nXOfs
, nYOfs
, pTraps
, nCount
);
330 //=====================================================================
332 inline ScopedPic::ScopedPic( XRenderPeer
& rPeer
, Picture
& rPic
)
333 : mrRenderPeer( rPeer
)
337 inline ScopedPic::~ScopedPic()
340 mrRenderPeer
.FreePicture( maPicture
);
343 inline Picture
& ScopedPic::Get()
348 //=====================================================================
350 inline XRenderColor
GetXRenderColor( const SalColor
& rSalColor
, double fTransparency
= 0.0 )
352 XRenderColor aRetVal
;
353 // convert the SalColor
354 aRetVal
.red
= SALCOLOR_RED( rSalColor
); aRetVal
.red
|= (aRetVal
.red
<< 8);
355 aRetVal
.green
= SALCOLOR_GREEN( rSalColor
); aRetVal
.green
|= (aRetVal
.green
<< 8);
356 aRetVal
.blue
= SALCOLOR_BLUE( rSalColor
); aRetVal
.blue
|= (aRetVal
.blue
<< 8);
358 // handle transparency
359 aRetVal
.alpha
= 0xFFFF; // default to opaque
360 if( fTransparency
!= 0 )
362 const double fAlpha
= 1.0 - fTransparency
;
363 aRetVal
.alpha
= static_cast<sal_uInt16
>(fAlpha
* 0xFFFF + 0.5);
364 // xrender wants pre-multiplied colors
365 aRetVal
.red
= static_cast<sal_uInt16
>(fAlpha
* aRetVal
.red
+ 0.5);
366 aRetVal
.green
= static_cast<sal_uInt16
>(fAlpha
* aRetVal
.green
+ 0.5);
367 aRetVal
.blue
= static_cast<sal_uInt16
>(fAlpha
* aRetVal
.blue
+ 0.5);
373 //=====================================================================
375 #endif // _SV_XRENDER_PEER_HXX