xref: /trunk/main/vcl/unx/generic/gdi/xrender_peer.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org 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 Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <stdio.h>
32 
33 #include <rtl/ustring.hxx>
34 #include <osl/module.h>
35 
36 #include <unx/salunx.h>
37 #include <unx/saldata.hxx>
38 #include <unx/saldisp.hxx>
39 
40 #include <xrender_peer.hxx>
41 
42 using namespace rtl;
43 
44 // ---------------------------------------------------------------------------
45 
46 XRenderPeer::XRenderPeer()
47 :   mpDisplay( GetX11SalData()->GetDisplay()->GetDisplay() ),
48     mpStandardFormatA8( NULL ),
49     mnRenderVersion( 0 ),
50     mpRenderLib( NULL )
51 #ifndef XRENDER_LINK
52 ,   mpXRenderCompositeTrapezoids( NULL )
53 ,   mpXRenderAddTraps( NULL )
54 #endif // XRENDER_LINK
55 {
56     InitRenderLib();
57 }
58 
59 // ---------------------------------------------------------------------------
60 
61 XRenderPeer::~XRenderPeer()
62 {
63     osl_unloadModule( mpRenderLib );
64 }
65 
66 // ---------------------------------------------------------------------------
67 
68 XRenderPeer& XRenderPeer::GetInstance()
69 {
70     static XRenderPeer aPeer;
71     return aPeer;
72 }
73 
74 // ---------------------------------------------------------------------------
75 
76 void XRenderPeer::InitRenderLib()
77 {
78     int nDummy;
79     if( !XQueryExtension( mpDisplay, "RENDER", &nDummy, &nDummy, &nDummy ) )
80         return;
81 
82 #ifndef XRENDER_LINK
83     // we don't know if we are running on a system with xrender library
84     // we don't want to install system libraries ourselves
85     // => load them dynamically when they are there
86     const OUString aLibName( RTL_CONSTASCII_USTRINGPARAM( "libXrender.so.1" ));
87     mpRenderLib = osl_loadModule( aLibName.pData, SAL_LOADMODULE_DEFAULT );
88     if( !mpRenderLib ) {
89 #ifdef DEBUG
90         fprintf( stderr, "Display can do XRender, but no %s installed.\n"
91             "Please install for improved display performance\n", OUStringToOString( aLibName.getStr(),
92                                                                                     osl_getThreadTextEncoding() ).getStr() );
93 #endif
94         return;
95     }
96 
97     oslGenericFunction pFunc;
98     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderQueryExtension" );
99     if( !pFunc ) return;
100     mpXRenderQueryExtension = (Bool(*)(Display*,int*,int*))pFunc;
101 
102     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderQueryVersion" );
103     if( !pFunc ) return;
104     mpXRenderQueryVersion = (void(*)(Display*,int*,int*))pFunc;
105 
106     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFindVisualFormat" );
107     if( !pFunc ) return;
108     mpXRenderFindVisualFormat = (XRenderPictFormat*(*)(Display*,Visual*))pFunc;
109 
110     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFindStandardFormat" );
111     if( !pFunc ) return;
112     mpXRenderFindStandardFormat = (XRenderPictFormat*(*)(Display*,int))pFunc;
113 
114     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFindFormat" );
115     if( !pFunc ) return;
116     mpXRenderFindFormat = (XRenderPictFormat*(*)(Display*,unsigned long,
117         const XRenderPictFormat*,int))pFunc;
118 
119     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderCreateGlyphSet" );
120     if( !pFunc ) return;
121     mpXRenderCreateGlyphSet = (GlyphSet(*)(Display*,const XRenderPictFormat*))pFunc;
122 
123     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFreeGlyphSet" );
124     if( !pFunc ) return;
125     mpXRenderFreeGlyphSet = (void(*)(Display*,GlyphSet))pFunc;
126 
127     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderAddGlyphs" );
128     if( !pFunc ) return;
129     mpXRenderAddGlyphs = (void(*)(Display*,GlyphSet,Glyph*,const XGlyphInfo*,
130         int,const char*,int))pFunc;
131 
132     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFreeGlyphs" );
133     if( !pFunc ) return;
134     mpXRenderFreeGlyphs = (void(*)(Display*,GlyphSet,Glyph*,int))pFunc;
135 
136     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderCompositeString32" );
137     if( !pFunc ) return;
138     mpXRenderCompositeString32 = (void(*)(Display*,int,Picture,Picture,
139         const XRenderPictFormat*,GlyphSet,int,int,int,int,const unsigned*,int))pFunc;
140 
141     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderCreatePicture" );
142     if( !pFunc ) return;
143     mpXRenderCreatePicture = (Picture(*)(Display*,Drawable,const XRenderPictFormat*,
144         unsigned long,const XRenderPictureAttributes*))pFunc;
145 
146     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderChangePicture" );
147     if( !pFunc ) return;
148     mpXRenderChangePicture = (void(*)(Display*,Picture,unsigned long,const XRenderPictureAttributes*))pFunc;
149 
150     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderSetPictureClipRegion" );
151     if( !pFunc ) return;
152     mpXRenderSetPictureClipRegion = (void(*)(Display*,Picture,XLIB_Region))pFunc;
153 
154     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFreePicture" );
155     if( !pFunc ) return;
156     mpXRenderFreePicture = (void(*)(Display*,Picture))pFunc;
157 
158     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderComposite" );
159     if( !pFunc ) return;
160     mpXRenderComposite = (void(*)(Display*,int,Picture,Picture,Picture,
161         int,int,int,int,int,int,unsigned,unsigned))pFunc;
162 
163     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderFillRectangle" );
164     if( !pFunc ) return;
165     mpXRenderFillRectangle = (void(*)(Display*,int,Picture,const XRenderColor*,
166         int,int,unsigned int,unsigned int))pFunc;
167 
168     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderCompositeTrapezoids" );
169 #if 0 // not having trapezoid support is supported
170     if( !pFunc ) return;
171 #endif
172     mpXRenderCompositeTrapezoids = (void(*)(Display*,int,Picture,Picture,
173         const XRenderPictFormat*,int,int,const XTrapezoid*,int))pFunc;
174 
175     pFunc = osl_getAsciiFunctionSymbol( mpRenderLib, "XRenderAddTraps" );
176 #if 0 // not having trapezoid support is supported
177     if( !pFunc ) return;
178 #endif
179     mpXRenderAddTraps = (void(*)(Display*,Picture,int,int,const _XTrap*,int))pFunc;
180 
181 #endif // XRENDER_LINK
182 
183     // needed to initialize libXrender internals, we already know its there
184 #ifdef XRENDER_LINK
185     XRenderQueryExtension( mpDisplay, &nDummy, &nDummy );
186 #else
187     (*mpXRenderQueryExtension)( mpDisplay, &nDummy, &nDummy );
188 #endif
189 
190     int nMajor, nMinor;
191 #ifdef XRENDER_LINK
192     XRenderQueryVersion( mpDisplay, &nMajor, &nMinor );
193 #else
194     (*mpXRenderQueryVersion)( mpDisplay, &nMajor, &nMinor );
195 #endif
196     mnRenderVersion = 16*nMajor + nMinor;
197 
198     // the 8bit alpha mask format must be there
199     XRenderPictFormat aPictFormat={0,0,8,{0,0,0,0,0,0,0,0xFF},0};
200     mpStandardFormatA8 = FindPictureFormat( PictFormatAlphaMask|PictFormatDepth, aPictFormat );
201 }
202 
203 // ---------------------------------------------------------------------------
204 
205 // return mask of screens capable of XRENDER text
206 sal_uInt32 XRenderPeer::InitRenderText()
207 {
208     if( mnRenderVersion < 0x01 )
209         return 0;
210 
211     // #93033# disable XRENDER for old RENDER versions if XINERAMA is present
212     int nDummy;
213     if( XQueryExtension( mpDisplay, "XINERAMA", &nDummy, &nDummy, &nDummy ) )
214         if( mnRenderVersion < 0x02 )
215             return 0;
216 
217     if( !mpStandardFormatA8 )
218         return 0;
219 
220     // and the visual must be supported too on at least one screen
221     sal_uInt32 nRetMask = 0;
222     SalDisplay* pSalDisp = GetX11SalData()->GetDisplay();
223     const int nScreenCount = pSalDisp->GetScreenCount();
224     XRenderPictFormat* pVisualFormat = NULL;
225     int nMaxDepth = 0;
226     for( int nScreen = 0; nScreen < nScreenCount; ++nScreen )
227     {
228         Visual* pXVisual = pSalDisp->GetVisual( nScreen ).GetVisual();
229         pVisualFormat = FindVisualFormat( pXVisual );
230         if( pVisualFormat != NULL )
231         {
232             int nVDepth = pSalDisp->GetVisual( nScreen ).GetDepth();
233             if( nVDepth > nMaxDepth )
234                 nMaxDepth = nVDepth;
235             nRetMask |= 1U << nScreen;
236         }
237     }
238 
239     // #97763# disable XRENDER on <15bit displays for XFree<=4.2.0
240     if( mnRenderVersion <= 0x02 )
241         if( nMaxDepth < 15 )
242             nRetMask = 0;
243 
244     return nRetMask;
245 }
246 
247 // ---------------------------------------------------------------------------
248