merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / gdi / xrender_peer.hxx
blob3a29aa6880ab73a0ac4b94f1144b764bc5530907
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile$
10 * $Revision$
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>
41 class XRenderPeer
43 public:
44 static XRenderPeer& GetInstance();
45 int GetVersion() const;
47 sal_uInt32 InitRenderText( int nMaxDepth );
49 protected:
50 XRenderPeer();
51 ~XRenderPeer();
52 void InitRenderLib();
54 Display* mpDisplay;
55 XRenderPictFormat* mpStandardFormatA8;
56 int mnRenderVersion;
57 oslModule mpRenderLib;
59 public:
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
91 #ifdef XRENDER_LINK
92 { return true; }
93 #else
94 { return mpXRenderCompositeTrapezoids!=NULL; }
96 private:
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 //=====================================================================
128 class ScopedPic
130 public:
131 ScopedPic( XRenderPeer& rPeer, Picture& rPic );
132 ~ScopedPic();
133 Picture& Get();
135 private:
136 XRenderPeer& mrRenderPeer;
137 Picture maPicture;
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
158 #ifdef XRENDER_LINK
159 return XRenderFindStandardFormat(mpDisplay, nFormat);
160 #else
161 return (*mpXRenderFindStandardFormat)(mpDisplay, nFormat);
162 #endif
165 inline XRenderPictFormat* XRenderPeer::FindVisualFormat( Visual* pVisual ) const
167 #ifdef XRENDER_LINK
168 return XRenderFindVisualFormat ( mpDisplay, pVisual );
169 #else
170 return (*mpXRenderFindVisualFormat)( mpDisplay, pVisual );
171 #endif
174 inline XRenderPictFormat* XRenderPeer::FindPictureFormat( unsigned long nFormatMask,
175 const XRenderPictFormat& rFormatAttr ) const
177 #ifdef XRENDER_LINK
178 return XRenderFindFormat( mpDisplay, nFormatMask, &rFormatAttr, 0 );
179 #else
180 return (*mpXRenderFindFormat)( mpDisplay, nFormatMask, &rFormatAttr, 0 );
181 #endif
184 inline Picture XRenderPeer::CreatePicture( Drawable aDrawable,
185 const XRenderPictFormat* pVisFormat, unsigned long nValueMask,
186 const XRenderPictureAttributes* pRenderAttr ) const
188 #ifdef XRENDER_LINK
189 return XRenderCreatePicture( mpDisplay, aDrawable, pVisFormat,
190 nValueMask, pRenderAttr );
191 #else
192 return (*mpXRenderCreatePicture)( mpDisplay, aDrawable, pVisFormat,
193 nValueMask, pRenderAttr );
194 #endif
197 inline void XRenderPeer::SetPictureClipRegion( Picture aPicture,
198 XLIB_Region aXlibRegion ) const
200 #ifdef XRENDER_LINK
201 XRenderSetPictureClipRegion( mpDisplay, aPicture, aXlibRegion );
202 #else
203 (*mpXRenderSetPictureClipRegion)( mpDisplay, aPicture, aXlibRegion );
204 #endif
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
212 #ifdef XRENDER_LINK
213 XRenderComposite( mpDisplay, nXRenderOp, aSrcPic, aMaskPic, aDstPic,
214 nSrcX, nSrcY, nMaskX, nMaskY, nDstX, nDstY, nWidth, nHeight );
215 #else
216 (*mpXRenderComposite)( mpDisplay, nXRenderOp, aSrcPic, aMaskPic, aDstPic,
217 nSrcX, nSrcY, nMaskX, nMaskY, nDstX, nDstY, nWidth, nHeight );
218 #endif
221 inline void XRenderPeer::FreePicture( Picture aPicture ) const
223 #ifdef XRENDER_LINK
224 XRenderFreePicture( mpDisplay, aPicture );
225 #else
226 (*mpXRenderFreePicture)( mpDisplay, aPicture );
227 #endif
230 inline GlyphSet XRenderPeer::CreateGlyphSet() const
232 #ifdef XRENDER_LINK
233 return XRenderCreateGlyphSet( mpDisplay, mpStandardFormatA8 );
234 #else
235 return (*mpXRenderCreateGlyphSet)( mpDisplay, mpStandardFormatA8 );
236 #endif
239 inline void XRenderPeer::FreeGlyphSet( GlyphSet aGS ) const
241 #ifdef XRENDER_LINK
242 XRenderFreeGlyphSet( mpDisplay, aGS );
243 #else
244 (*mpXRenderFreeGlyphSet)( mpDisplay, aGS );
245 #endif
248 inline void XRenderPeer::AddGlyph( GlyphSet aGS, Glyph nGlyphId,
249 const XGlyphInfo& rGI, const char* pBuffer, int nBufSize ) const
251 #ifdef XRENDER_LINK
252 XRenderAddGlyphs( mpDisplay, aGS, &nGlyphId, &rGI, 1,
253 const_cast<char*>(pBuffer), nBufSize );
254 #else
255 (*mpXRenderAddGlyphs)( mpDisplay, aGS, &nGlyphId, &rGI, 1,
256 const_cast<char*>(pBuffer), nBufSize );
257 #endif
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 )
271 #ifdef XRENDER_LINK
272 XRenderFreeGlyphs( mpDisplay, aGS, &nGlyphId, 1 );
273 #else
274 (*mpXRenderFreeGlyphs)( mpDisplay, aGS, &nGlyphId, 1 );
275 #endif
277 #endif
280 inline void XRenderPeer::CompositeString32( Picture aSrc, Picture aDst,
281 GlyphSet aGlyphSet, int nDstX, int nDstY,
282 const unsigned* pText, int nTextLen ) const
284 #ifdef XRENDER_LINK
285 XRenderCompositeString32( mpDisplay, PictOpOver, aSrc, aDst, NULL,
286 aGlyphSet, 0, 0, nDstX, nDstY, pText, nTextLen );
287 #else
288 (*mpXRenderCompositeString32)( mpDisplay, PictOpOver, aSrc, aDst, NULL,
289 aGlyphSet, 0, 0, nDstX, nDstY, pText, nTextLen );
290 #endif
293 inline void XRenderPeer::FillRectangle( int a, Picture b, const XRenderColor* c,
294 int d, int e, unsigned int f, unsigned int g) const
296 #ifdef XRENDER_LINK
297 XRenderFillRectangle( mpDisplay, a, b, c, d, e, f, g );
298 #else
299 (*mpXRenderFillRectangle)( mpDisplay, a, b, c, d, e, f, g );
300 #endif
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
308 #ifdef XRENDER_LINK
309 XRenderCompositeTrapezoids( mpDisplay, nOp, aSrc, aDst, pXRPF,
310 nXSrc, nYSrc, pXT, nCount );
311 #else
312 (*mpXRenderCompositeTrapezoids)( mpDisplay, nOp, aSrc, aDst, pXRPF,
313 nXSrc, nYSrc, pXT, nCount );
314 #endif
317 inline bool XRenderPeer::AddTraps( Picture aDst, int nXOfs, int nYOfs,
318 const XTrap* pTraps, int nCount ) const
320 #ifdef XRENDER_LINK
321 XRenderAddTraps( mpDisplay, aDst, nXOfs, nYOfs, pTraps, nCount );
322 #else
323 if( !mpXRenderAddTraps )
324 return false;
325 (*mpXRenderAddTraps)( mpDisplay, aDst, nXOfs, nYOfs, pTraps, nCount );
326 #endif
327 return true;
330 //=====================================================================
332 inline ScopedPic::ScopedPic( XRenderPeer& rPeer, Picture& rPic )
333 : mrRenderPeer( rPeer)
334 , maPicture( rPic )
337 inline ScopedPic::~ScopedPic()
339 if( maPicture )
340 mrRenderPeer.FreePicture( maPicture );
343 inline Picture& ScopedPic::Get()
345 return maPicture;
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);
370 return aRetVal;
373 //=====================================================================
375 #endif // _SV_XRENDER_PEER_HXX