xref: /aoo41x/main/vcl/unx/generic/gdi/xrender_peer.hxx (revision ebfcd9af)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _SV_XRENDER_PEER_HXX
25 #define _SV_XRENDER_PEER_HXX
26 
27 #include <tools/prex.h>
28 struct _XTrap; // on some older systems this is not declared within Xrender.h
29 #include <X11/extensions/Xrender.h>
30 #include <tools/postx.h>
31 
32 #include <vcl/salgtype.hxx>
33 #include <osl/module.h>
34 
35 class XRenderPeer
36 {
37 public:
38     static XRenderPeer& GetInstance();
39     int                 GetVersion() const;
40 
41     sal_uInt32          InitRenderText();
42 
43 protected:
44                         XRenderPeer();
45                         ~XRenderPeer();
46     void                InitRenderLib();
47 
48     Display*            mpDisplay;
49     XRenderPictFormat*  mpStandardFormatA8;
50     int                 mnRenderVersion;
51     oslModule           mpRenderLib;
52 
53 public:
54     XRenderPictFormat* GetStandardFormatA8() const;
55     XRenderPictFormat* FindStandardFormat(int nFormat) const;
56 
57     // the methods below are thin wrappers for the XRENDER API
58     XRenderPictFormat* FindVisualFormat( Visual* ) const;
59     XRenderPictFormat* FindPictureFormat( unsigned long nMask,
60         const XRenderPictFormat& ) const;
61     Picture     CreatePicture( Drawable, const XRenderPictFormat*,
62                     unsigned long nDrawable, const XRenderPictureAttributes* ) const;
63     void        ChangePicture( Picture, unsigned long nValueMask,
64                     const XRenderPictureAttributes* ) const;
65     void        SetPictureClipRegion( Picture, XLIB_Region ) const;
66     void        CompositePicture( int nOp, Picture aSrc, Picture aMask, Picture aDst,
67                     int nXSrc, int nYSrc, int nXMask, int nYMask,
68                     int nXDst, int nYDst, unsigned nWidth, unsigned nHeight ) const;
69     void        FreePicture( Picture ) const;
70 
71     GlyphSet    CreateGlyphSet() const;
72     void        FreeGlyphSet( GlyphSet ) const;
73     void        AddGlyph( GlyphSet, Glyph nGlyphId, const XGlyphInfo&,
74                     const char* pBuffer, int nBufSize ) const;
75     void        FreeGlyph( GlyphSet, Glyph nGlyphId ) const;
76     void        CompositeString32( Picture aSrc, Picture aDst, GlyphSet,
77                     int nDstX, int nDstY, const unsigned* pText, int nTextLen ) const;
78     void        FillRectangle( int nOp, Picture aDst, const XRenderColor*,
79                                int nX, int nY, unsigned nW, unsigned nH ) const;
80     void        CompositeTrapezoids( int nOp, Picture aSrc, Picture aDst,
81                     const XRenderPictFormat*, int nXSrc, int nYSrc,
82                     const XTrapezoid*, int nCount ) const;
83     bool        AddTraps( Picture aDst, int nXOfs, int nYOfs,
84                     const _XTrap*, int nCount ) const;
85 
86     bool        AreTrapezoidsSupported() const
87 #ifdef XRENDER_LINK
88                     { return true; }
89 #else
90                     { return mpXRenderCompositeTrapezoids!=NULL; }
91 
92 private:
93     XRenderPictFormat* (*mpXRenderFindFormat)(Display*,unsigned long,
94         const XRenderPictFormat*,int);
95     XRenderPictFormat* (*mpXRenderFindVisualFormat)(Display*,Visual*);
96     XRenderPictFormat* (*mpXRenderFindStandardFormat)(Display*,int);
97     Bool        (*mpXRenderQueryExtension)(Display*,int*,int*);
98     void        (*mpXRenderQueryVersion)(Display*,int*,int*);
99 
100     Picture     (*mpXRenderCreatePicture)(Display*,Drawable, const XRenderPictFormat*,
101                     unsigned long,const XRenderPictureAttributes*);
102     void        (*mpXRenderChangePicture)(Display*,Picture,
103                     unsigned long,const XRenderPictureAttributes*);
104     void        (*mpXRenderSetPictureClipRegion)(Display*,Picture,XLIB_Region);
105     void        (*mpXRenderFreePicture)(Display*,Picture);
106     void        (*mpXRenderComposite)(Display*,int,Picture,Picture,Picture,
107                     int,int,int,int,int,int,unsigned,unsigned);
108 
109     GlyphSet    (*mpXRenderCreateGlyphSet)(Display*, const XRenderPictFormat*);
110     void        (*mpXRenderFreeGlyphSet)(Display*,GlyphSet);
111     void        (*mpXRenderAddGlyphs)(Display*,GlyphSet,Glyph*,
112                     const XGlyphInfo*,int,const char*,int);
113     void        (*mpXRenderFreeGlyphs)(Display*,GlyphSet,Glyph*,int);
114     void        (*mpXRenderCompositeString32)(Display*,int,Picture,Picture,
115                     const XRenderPictFormat*,GlyphSet,int,int,int,int,const unsigned*,int);
116     void        (*mpXRenderFillRectangle)(Display*,int,Picture,
117                     const XRenderColor*,int,int,unsigned int,unsigned int);
118     void        (*mpXRenderCompositeTrapezoids)(Display*,int,Picture,Picture,
119                     const XRenderPictFormat*,int,int,const XTrapezoid*,int);
120     void        (*mpXRenderAddTraps)(Display*,Picture,int,int,const _XTrap*,int);
121 #endif // XRENDER_LINK
122 };
123 
124 //=====================================================================
125 
126 class ScopedPic
127 {
128 public:
129                ScopedPic( XRenderPeer& rPeer, Picture& rPic );
130                ~ScopedPic();
131     Picture&   Get();
132 
133 private:
134     XRenderPeer& mrRenderPeer;
135     Picture      maPicture;
136 
137 private: // prevent copy and assignmet
138            ScopedPic( const ScopedPic& );
139     void   operator=( const ScopedPic& );
140 };
141 
142 //=====================================================================
143 
144 inline int XRenderPeer::GetVersion() const
145 {
146     return mnRenderVersion;
147 }
148 
149 inline XRenderPictFormat* XRenderPeer::GetStandardFormatA8() const
150 {
151     return mpStandardFormatA8;
152 }
153 
154 inline XRenderPictFormat* XRenderPeer::FindStandardFormat(int nFormat) const
155 {
156 #ifdef XRENDER_LINK
157     return XRenderFindStandardFormat(mpDisplay, nFormat);
158 #else
159     return (*mpXRenderFindStandardFormat)(mpDisplay, nFormat);
160 #endif
161 }
162 
163 inline XRenderPictFormat* XRenderPeer::FindVisualFormat( Visual* pVisual ) const
164 {
165 #ifdef XRENDER_LINK
166     return XRenderFindVisualFormat ( mpDisplay, pVisual );
167 #else
168     return (*mpXRenderFindVisualFormat)( mpDisplay, pVisual );
169 #endif
170 }
171 
172 inline XRenderPictFormat* XRenderPeer::FindPictureFormat( unsigned long nFormatMask,
173     const XRenderPictFormat& rFormatAttr ) const
174 {
175 #ifdef XRENDER_LINK
176     return XRenderFindFormat( mpDisplay, nFormatMask, &rFormatAttr, 0 );
177 #else
178     return (*mpXRenderFindFormat)( mpDisplay, nFormatMask, &rFormatAttr, 0 );
179 #endif
180 }
181 
182 inline Picture XRenderPeer::CreatePicture( Drawable aDrawable,
183     const XRenderPictFormat* pVisFormat, unsigned long nValueMask,
184     const XRenderPictureAttributes* pRenderAttr ) const
185 {
186 #ifdef XRENDER_LINK
187     return XRenderCreatePicture( mpDisplay, aDrawable, pVisFormat,
188                                  nValueMask, pRenderAttr );
189 #else
190     return (*mpXRenderCreatePicture)( mpDisplay, aDrawable, pVisFormat,
191         nValueMask, pRenderAttr );
192 #endif
193 }
194 
195 inline void XRenderPeer::ChangePicture( Picture aPicture,
196     unsigned long nValueMask, const XRenderPictureAttributes* pRenderAttr ) const
197 {
198 #ifdef XRENDER_LINK
199     XRenderChangePicture( mpDisplay, aPicture, nValueMask, pRenderAttr );
200 #else
201     (*mpXRenderChangePicture)( mpDisplay, aPicture, nValueMask, pRenderAttr );
202 #endif
203 }
204 
205 inline void XRenderPeer::SetPictureClipRegion( Picture aPicture,
206     XLIB_Region aXlibRegion ) const
207 {
208 #ifdef XRENDER_LINK
209     XRenderSetPictureClipRegion( mpDisplay, aPicture, aXlibRegion );
210 #else
211     (*mpXRenderSetPictureClipRegion)( mpDisplay, aPicture, aXlibRegion );
212 #endif
213 }
214 
215 inline void XRenderPeer::CompositePicture( int nXRenderOp,
216     Picture aSrcPic, Picture aMaskPic, Picture aDstPic,
217     int nSrcX, int nSrcY, int nMaskX, int nMaskY, int nDstX, int nDstY,
218     unsigned nWidth, unsigned nHeight ) const
219 {
220 #ifdef XRENDER_LINK
221     XRenderComposite( mpDisplay, nXRenderOp, aSrcPic, aMaskPic, aDstPic,
222                       nSrcX, nSrcY, nMaskX, nMaskY, nDstX, nDstY, nWidth, nHeight );
223 #else
224     (*mpXRenderComposite)( mpDisplay, nXRenderOp, aSrcPic, aMaskPic, aDstPic,
225         nSrcX, nSrcY, nMaskX, nMaskY, nDstX, nDstY, nWidth, nHeight );
226 #endif
227 }
228 
229 inline void XRenderPeer::FreePicture( Picture aPicture ) const
230 {
231 #ifdef XRENDER_LINK
232     XRenderFreePicture( mpDisplay, aPicture );
233 #else
234     (*mpXRenderFreePicture)( mpDisplay, aPicture );
235 #endif
236 }
237 
238 inline GlyphSet XRenderPeer::CreateGlyphSet() const
239 {
240 #ifdef XRENDER_LINK
241     return XRenderCreateGlyphSet( mpDisplay, mpStandardFormatA8 );
242 #else
243     return (*mpXRenderCreateGlyphSet)( mpDisplay, mpStandardFormatA8 );
244 #endif
245 }
246 
247 inline void XRenderPeer::FreeGlyphSet( GlyphSet aGS ) const
248 {
249 #ifdef XRENDER_LINK
250     XRenderFreeGlyphSet( mpDisplay, aGS );
251 #else
252     (*mpXRenderFreeGlyphSet)( mpDisplay, aGS );
253 #endif
254 }
255 
256 inline void XRenderPeer::AddGlyph( GlyphSet aGS, Glyph nGlyphId,
257     const XGlyphInfo& rGI, const char* pBuffer, int nBufSize ) const
258 {
259 #ifdef XRENDER_LINK
260     XRenderAddGlyphs( mpDisplay, aGS, &nGlyphId, &rGI, 1,
261                       const_cast<char*>(pBuffer), nBufSize );
262 #else
263     (*mpXRenderAddGlyphs)( mpDisplay, aGS, &nGlyphId, &rGI, 1,
264         const_cast<char*>(pBuffer), nBufSize );
265 #endif
266 }
267 
268 inline void XRenderPeer::FreeGlyph( GlyphSet aGS, Glyph nGlyphId ) const
269 {
270     (void)aGS; (void)nGlyphId;
271 
272     // XRenderFreeGlyphs not implemented yet for version<=0.2
273     // #108209# disabled because of crash potential,
274     // the glyph leak is not too bad because they will
275     // be cleaned up when the glyphset is released
276 #if 0 // TODO: reenable when it works without problems
277     if( mnRenderVersion >= 0x05 )
278     {
279 #ifdef XRENDER_LINK
280         XRenderFreeGlyphs( mpDisplay, aGS, &nGlyphId, 1 );
281 #else
282         (*mpXRenderFreeGlyphs)( mpDisplay, aGS, &nGlyphId, 1 );
283 #endif
284     }
285 #endif
286 }
287 
288 inline void XRenderPeer::CompositeString32( Picture aSrc, Picture aDst,
289     GlyphSet aGlyphSet, int nDstX, int nDstY,
290     const unsigned* pText, int nTextLen ) const
291 {
292 #ifdef XRENDER_LINK
293     XRenderCompositeString32( mpDisplay, PictOpOver, aSrc, aDst, NULL,
294                               aGlyphSet, 0, 0, nDstX, nDstY, pText, nTextLen );
295 #else
296     (*mpXRenderCompositeString32)( mpDisplay, PictOpOver, aSrc, aDst, NULL,
297         aGlyphSet, 0, 0, nDstX, nDstY, pText, nTextLen );
298 #endif
299 }
300 
301 inline void XRenderPeer::FillRectangle( int a, Picture b, const XRenderColor* c,
302     int d, int e, unsigned int f, unsigned int g) const
303 {
304 #ifdef XRENDER_LINK
305     XRenderFillRectangle( mpDisplay, a, b, c, d, e, f, g );
306 #else
307     (*mpXRenderFillRectangle)( mpDisplay, a, b, c, d, e, f, g );
308 #endif
309 }
310 
311 
312 inline void XRenderPeer::CompositeTrapezoids( int nOp,
313     Picture aSrc, Picture aDst, const XRenderPictFormat* pXRPF,
314     int nXSrc, int nYSrc, const XTrapezoid* pXT, int nCount ) const
315 {
316 #ifdef XRENDER_LINK
317     XRenderCompositeTrapezoids( mpDisplay, nOp, aSrc, aDst, pXRPF,
318         nXSrc, nYSrc, pXT, nCount );
319 #else
320     (*mpXRenderCompositeTrapezoids)( mpDisplay, nOp, aSrc, aDst, pXRPF,
321         nXSrc, nYSrc, pXT, nCount );
322 #endif
323 }
324 
325 inline bool XRenderPeer::AddTraps( Picture aDst, int nXOfs, int nYOfs,
326     const _XTrap* pTraps, int nCount ) const
327 {
328 #ifdef XRENDER_LINK
329     XRenderAddTraps( mpDisplay, aDst, nXOfs, nYOfs, pTraps, nCount );
330 #else
331     if( !mpXRenderAddTraps )
332         return false;
333     (*mpXRenderAddTraps)( mpDisplay, aDst, nXOfs, nYOfs, pTraps, nCount );
334 #endif
335     return true;
336 }
337 
338 //=====================================================================
339 
340 inline ScopedPic::ScopedPic( XRenderPeer& rPeer, Picture& rPic )
341 :   mrRenderPeer( rPeer)
342 ,   maPicture( rPic )
343 {}
344 
345 inline ScopedPic::~ScopedPic()
346 {
347     if( maPicture )
348         mrRenderPeer.FreePicture( maPicture );
349 }
350 
351 inline Picture& ScopedPic::Get()
352 {
353     return maPicture;
354 }
355 
356 //=====================================================================
357 
358 inline XRenderColor GetXRenderColor( const SalColor& rSalColor, double fTransparency = 0.0 )
359 {
360 	XRenderColor aRetVal;
361 	// convert the SalColor
362 	aRetVal.red   = SALCOLOR_RED(   rSalColor ); aRetVal.red   |= (aRetVal.red   << 8);
363 	aRetVal.green = SALCOLOR_GREEN( rSalColor ); aRetVal.green |= (aRetVal.green << 8);
364 	aRetVal.blue  = SALCOLOR_BLUE(  rSalColor ); aRetVal.blue  |= (aRetVal.blue  << 8);
365 
366 	// handle transparency
367 	aRetVal.alpha = 0xFFFF; // default to opaque
368 	if( fTransparency != 0 )
369 	{
370 		const double fAlpha = 1.0 - fTransparency;
371 		aRetVal.alpha = static_cast<sal_uInt16>(fAlpha * 0xFFFF + 0.5);
372 		// xrender wants pre-multiplied colors
373 		aRetVal.red   = static_cast<sal_uInt16>(fAlpha * aRetVal.red + 0.5);
374 		aRetVal.green = static_cast<sal_uInt16>(fAlpha * aRetVal.green + 0.5);
375 		aRetVal.blue  = static_cast<sal_uInt16>(fAlpha * aRetVal.blue + 0.5);
376 	}
377 
378 	return aRetVal;
379 }
380 
381 //=====================================================================
382 
383 #endif // _SV_XRENDER_PEER_HXX
384