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