Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / factory / cf_service.cxx
blob64451299f437e206b6e5f9cf9508788b6b4f84f9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <osl/mutex.hxx>
22 #include <osl/process.h>
23 #include <cppuhelper/implementationentry.hxx>
24 #include <cppuhelper/factory.hxx>
25 #include <cppuhelper/implbase3.hxx>
26 #include <cppuhelper/supportsservice.hxx>
28 #include <com/sun/star/uno/XComponentContext.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 #include <com/sun/star/configuration/theDefaultProvider.hpp>
33 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
34 #include <com/sun/star/container/XNameAccess.hpp>
35 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
36 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <boost/bind.hpp>
39 #include <vector>
40 #include <utility>
41 #include <o3tl/compat_functional.hxx>
42 #include <algorithm>
43 #include <vcl/opengl/OpenGLWrapper.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
50 namespace
53 class CanvasFactory
54 : public ::cppu::WeakImplHelper3< lang::XServiceInfo,
55 lang::XMultiComponentFactory,
56 lang::XMultiServiceFactory >
58 typedef std::pair<OUString,Sequence<OUString> > AvailPair;
59 typedef std::pair<OUString,OUString> CachePair;
60 typedef std::vector< AvailPair > AvailVector;
61 typedef std::vector< CachePair > CacheVector;
64 mutable ::osl::Mutex m_mutex;
65 Reference<XComponentContext> m_xContext;
66 Reference<container::XNameAccess> m_xCanvasConfigNameAccess;
67 AvailVector m_aAvailableImplementations;
68 AvailVector m_aAcceleratedImplementations;
69 AvailVector m_aAAImplementations;
70 mutable CacheVector m_aCachedImplementations;
71 mutable bool m_bCacheHasForcedLastImpl;
72 mutable bool m_bCacheHasUseAcceleratedEntry;
73 mutable bool m_bCacheHasUseAAEntry;
75 void checkConfigFlag( bool& r_bFlag,
76 bool& r_CacheFlag,
77 const OUString& nodeName ) const;
78 Reference<XInterface> use(
79 OUString const & serviceName,
80 Sequence<Any> const & args,
81 Reference<XComponentContext> const & xContext ) const;
82 Reference<XInterface> lookupAndUse(
83 OUString const & serviceName, Sequence<Any> const & args,
84 Reference<XComponentContext> const & xContext ) const;
86 public:
87 virtual ~CanvasFactory();
88 CanvasFactory( Reference<XComponentContext> const & xContext );
90 // XServiceInfo
91 virtual OUString SAL_CALL getImplementationName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
92 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
93 throw (RuntimeException, std::exception) SAL_OVERRIDE;
94 virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
95 throw (RuntimeException, std::exception) SAL_OVERRIDE;
97 // XMultiComponentFactory
98 virtual Sequence<OUString> SAL_CALL getAvailableServiceNames()
99 throw (RuntimeException, std::exception) SAL_OVERRIDE;
100 virtual Reference<XInterface> SAL_CALL createInstanceWithContext(
101 OUString const & name,
102 Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
103 virtual Reference<XInterface> SAL_CALL
104 createInstanceWithArgumentsAndContext(
105 OUString const & name,
106 Sequence<Any> const & args,
107 Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
109 // XMultiServiceFactory
110 virtual Reference<XInterface> SAL_CALL createInstance(
111 OUString const & name )
112 throw (Exception, std::exception) SAL_OVERRIDE;
113 virtual Reference<XInterface> SAL_CALL createInstanceWithArguments(
114 OUString const & name, Sequence<Any> const & args )
115 throw (Exception, std::exception) SAL_OVERRIDE;
118 CanvasFactory::CanvasFactory( Reference<XComponentContext> const & xContext ) :
119 m_mutex(),
120 m_xContext(xContext),
121 m_xCanvasConfigNameAccess(),
122 m_aAvailableImplementations(),
123 m_aAcceleratedImplementations(),
124 m_aAAImplementations(),
125 m_aCachedImplementations(),
126 m_bCacheHasForcedLastImpl(),
127 m_bCacheHasUseAcceleratedEntry(),
128 m_bCacheHasUseAAEntry()
132 // read out configuration for preferred services:
133 Reference<lang::XMultiServiceFactory> xConfigProvider(
134 configuration::theDefaultProvider::get( m_xContext ) );
136 Any propValue(
137 makeAny( beans::PropertyValue(
138 OUString("nodepath"), -1,
139 makeAny( OUString("/org.openoffice.Office.Canvas") ),
140 beans::PropertyState_DIRECT_VALUE ) ) );
142 m_xCanvasConfigNameAccess.set(
143 xConfigProvider->createInstanceWithArguments(
144 OUString("com.sun.star.configuration.ConfigurationAccess"),
145 Sequence<Any>( &propValue, 1 ) ),
146 UNO_QUERY_THROW );
148 propValue = makeAny(
149 beans::PropertyValue(
150 OUString("nodepath"), -1,
151 makeAny( OUString("/org.openoffice.Office.Canvas/CanvasServiceList") ),
152 beans::PropertyState_DIRECT_VALUE ) );
154 Reference<container::XNameAccess> xNameAccess(
155 xConfigProvider->createInstanceWithArguments(
156 OUString("com.sun.star.configuration.ConfigurationAccess"),
157 Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
158 Reference<container::XHierarchicalNameAccess> xHierarchicalNameAccess(
159 xNameAccess, UNO_QUERY_THROW);
161 Sequence<OUString> serviceNames = xNameAccess->getElementNames();
162 const OUString* pCurr = serviceNames.getConstArray();
163 const OUString* const pEnd = pCurr + serviceNames.getLength();
164 while( pCurr != pEnd )
166 Reference<container::XNameAccess> xEntryNameAccess(
167 xHierarchicalNameAccess->getByHierarchicalName(*pCurr),
168 UNO_QUERY );
170 if( xEntryNameAccess.is() )
172 Sequence<OUString> implementationList;
173 if( (xEntryNameAccess->getByName("PreferredImplementations") >>= implementationList) )
175 m_aAvailableImplementations.push_back( std::make_pair(*pCurr,implementationList) );
177 if( (xEntryNameAccess->getByName("AcceleratedImplementations") >>= implementationList) )
179 m_aAcceleratedImplementations.push_back( std::make_pair(*pCurr,implementationList) );
181 if( (xEntryNameAccess->getByName("AntialiasingImplementations") >>= implementationList) )
183 m_aAAImplementations.push_back( std::make_pair(*pCurr,implementationList) );
188 ++pCurr;
191 catch (const RuntimeException &)
193 throw;
195 catch (const Exception&)
199 if( m_aAvailableImplementations.empty() )
201 // Ugh. Looks like configuration is borked. Fake minimal
202 // setup.
203 Sequence<OUString> aServices(1);
204 aServices[0] = "com.sun.star.comp.rendering.Canvas.VCL";
205 m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.Canvas"),
206 aServices) );
208 aServices[0] = "com.sun.star.comp.rendering.SpriteCanvas.VCL";
209 m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.SpriteCanvas"),
210 aServices) );
214 CanvasFactory::~CanvasFactory()
219 // XServiceInfo
220 OUString CanvasFactory::getImplementationName() throw (RuntimeException, std::exception)
222 return OUString("com.sun.star.comp.rendering.CanvasFactory");
225 sal_Bool CanvasFactory::supportsService( OUString const & serviceName )
226 throw (RuntimeException, std::exception)
228 return cppu::supportsService(this, serviceName);
231 Sequence<OUString> CanvasFactory::getSupportedServiceNames()
232 throw (RuntimeException, std::exception)
234 OUString name("com.sun.star.rendering.CanvasFactory");
235 return Sequence<OUString>(&name, 1);
238 // XMultiComponentFactory
239 Sequence<OUString> CanvasFactory::getAvailableServiceNames()
240 throw (RuntimeException, std::exception)
242 Sequence<OUString> aServiceNames(m_aAvailableImplementations.size());
243 std::transform(m_aAvailableImplementations.begin(),
244 m_aAvailableImplementations.end(),
245 aServiceNames.getArray(),
246 o3tl::select1st<AvailPair>());
247 return aServiceNames;
250 Reference<XInterface> CanvasFactory::createInstanceWithContext(
251 OUString const & name, Reference<XComponentContext> const & xContext )
252 throw (Exception, std::exception)
254 return createInstanceWithArgumentsAndContext(
255 name, Sequence<Any>(), xContext );
259 Reference<XInterface> CanvasFactory::use(
260 OUString const & serviceName,
261 Sequence<Any> const & args,
262 Reference<XComponentContext> const & xContext ) const
264 try {
265 return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
266 serviceName, args, xContext);
268 catch (css::lang::IllegalArgumentException &)
270 return Reference<XInterface>();
272 catch (const RuntimeException &)
274 throw;
276 catch (const Exception &)
278 return Reference<XInterface>();
283 void CanvasFactory::checkConfigFlag( bool& r_bFlag,
284 bool& r_CacheFlag,
285 const OUString& nodeName ) const
287 if( m_xCanvasConfigNameAccess.is() )
289 m_xCanvasConfigNameAccess->getByName( nodeName ) >>= r_bFlag;
291 if( r_CacheFlag != r_bFlag )
293 // cache is invalid, because of different order of
294 // elements
295 r_CacheFlag = r_bFlag;
296 m_aCachedImplementations.clear();
302 Reference<XInterface> CanvasFactory::lookupAndUse(
303 OUString const & serviceName, Sequence<Any> const & args,
304 Reference<XComponentContext> const & xContext ) const
306 ::osl::MutexGuard guard(m_mutex);
308 // forcing last entry from impl list, if config flag set
309 bool bForceLastEntry(false);
310 checkConfigFlag( bForceLastEntry,
311 m_bCacheHasForcedLastImpl,
312 OUString("ForceSafeServiceImpl") );
314 // tdf#93870 - force VCL canvas in OpenGL mode for now.
315 if( OpenGLWrapper::isVCLOpenGLEnabled() )
316 bForceLastEntry = true;
318 // use anti-aliasing canvas, if config flag set (or not existing)
319 bool bUseAAEntry(true);
320 checkConfigFlag( bUseAAEntry,
321 m_bCacheHasUseAAEntry,
322 OUString("UseAntialiasingCanvas") );
324 // use accelerated canvas, if config flag set (or not existing)
325 bool bUseAcceleratedEntry(true);
326 checkConfigFlag( bUseAcceleratedEntry,
327 m_bCacheHasUseAcceleratedEntry,
328 OUString("UseAcceleratedCanvas") );
330 // try to reuse last working implementation for given service name
331 const CacheVector::iterator aEnd(m_aCachedImplementations.end());
332 CacheVector::iterator aMatch;
333 if( (aMatch=std::find_if(m_aCachedImplementations.begin(),
334 aEnd,
335 boost::bind(&OUString::equals,
336 boost::cref(serviceName),
337 boost::bind(
338 o3tl::select1st<CachePair>(),
339 _1)))) != aEnd )
341 Reference<XInterface> xCanvas( use( aMatch->second, args, xContext ) );
342 if(xCanvas.is())
343 return xCanvas;
346 // lookup in available service list
347 const AvailVector::const_iterator aAvailEnd(m_aAvailableImplementations.end());
348 AvailVector::const_iterator aAvailImplsMatch;
349 if( (aAvailImplsMatch=std::find_if(m_aAvailableImplementations.begin(),
350 aAvailEnd,
351 boost::bind(&OUString::equals,
352 boost::cref(serviceName),
353 boost::bind(
354 o3tl::select1st<AvailPair>(),
355 _1)))) == aAvailEnd )
357 return Reference<XInterface>();
360 const AvailVector::const_iterator aAAEnd(m_aAAImplementations.end());
361 AvailVector::const_iterator aAAImplsMatch;
362 if( (aAAImplsMatch=std::find_if(m_aAAImplementations.begin(),
363 aAAEnd,
364 boost::bind(&OUString::equals,
365 boost::cref(serviceName),
366 boost::bind(
367 o3tl::select1st<AvailPair>(),
368 _1)))) == aAAEnd )
370 return Reference<XInterface>();
373 const AvailVector::const_iterator aAccelEnd(m_aAcceleratedImplementations.end());
374 AvailVector::const_iterator aAccelImplsMatch;
375 if( (aAccelImplsMatch=std::find_if(m_aAcceleratedImplementations.begin(),
376 aAccelEnd,
377 boost::bind(&OUString::equals,
378 boost::cref(serviceName),
379 boost::bind(
380 o3tl::select1st<AvailPair>(),
381 _1)))) == aAccelEnd )
383 return Reference<XInterface>();
386 const Sequence<OUString> aPreferredImpls( aAvailImplsMatch->second );
387 const OUString* pCurrImpl = aPreferredImpls.getConstArray();
388 const OUString* const pEndImpl = pCurrImpl + aPreferredImpls.getLength();
390 const Sequence<OUString> aAAImpls( aAAImplsMatch->second );
391 const OUString* const pFirstAAImpl = aAAImpls.getConstArray();
392 const OUString* const pEndAAImpl = pFirstAAImpl + aAAImpls.getLength();
394 const Sequence<OUString> aAccelImpls( aAccelImplsMatch->second );
395 const OUString* const pFirstAccelImpl = aAccelImpls.getConstArray();
396 const OUString* const pEndAccelImpl = pFirstAccelImpl + aAccelImpls.getLength();
398 // force last entry from impl list, if config flag set
399 if( bForceLastEntry )
400 pCurrImpl = pEndImpl-1;
402 while( pCurrImpl != pEndImpl )
404 const OUString aCurrName(pCurrImpl->trim());
406 // check whether given canvas service is listed in the
407 // sequence of "accelerated canvas implementations"
408 const bool bIsAcceleratedImpl(
409 std::any_of(pFirstAccelImpl,
410 pEndAccelImpl,
411 boost::bind(&OUString::equals,
412 boost::cref(aCurrName),
413 boost::bind(
414 &OUString::trim,
415 _1))) );
417 // check whether given canvas service is listed in the
418 // sequence of "antialiasing canvas implementations"
419 const bool bIsAAImpl(
420 std::any_of(pFirstAAImpl,
421 pEndAAImpl,
422 boost::bind(&OUString::equals,
423 boost::cref(aCurrName),
424 boost::bind(
425 &OUString::trim,
426 _1))) );
428 // try to instantiate canvas *only* if either accel and AA
429 // property match preference, *or*, if there's a mismatch, only
430 // go for a less capable canvas (that effectively let those
431 // pour canvas impls still work as fallbacks, should an
432 // accelerated/AA one fail). Property implies configuration:
433 // http://en.wikipedia.org/wiki/Truth_table#Logical_implication
434 if( (!bIsAAImpl || bUseAAEntry) && (!bIsAcceleratedImpl || bUseAcceleratedEntry) )
436 Reference<XInterface> xCanvas(
437 use( pCurrImpl->trim(), args, xContext ) );
439 if(xCanvas.is())
441 if( aMatch != aEnd )
443 // cache entry exists, replace dysfunctional
444 // implementation name
445 aMatch->second = pCurrImpl->trim();
447 else
449 // new service name, add new cache entry
450 m_aCachedImplementations.push_back(std::make_pair(serviceName,
451 pCurrImpl->trim()));
454 return xCanvas;
458 ++pCurrImpl;
461 return Reference<XInterface>();
465 Reference<XInterface> CanvasFactory::createInstanceWithArgumentsAndContext(
466 OUString const & preferredOne, Sequence<Any> const & args,
467 Reference<XComponentContext> const & xContext ) throw (Exception, std::exception)
469 Reference<XInterface> xCanvas(
470 lookupAndUse( preferredOne, args, xContext ) );
471 if(xCanvas.is())
472 return xCanvas;
474 // last resort: try service name directly
475 return use( preferredOne, args, xContext );
478 // XMultiServiceFactory
480 Reference<XInterface> CanvasFactory::createInstance( OUString const & name )
481 throw (Exception, std::exception)
483 return createInstanceWithArgumentsAndContext(
484 name, Sequence<Any>(), m_xContext );
488 Reference<XInterface> CanvasFactory::createInstanceWithArguments(
489 OUString const & name, Sequence<Any> const & args ) throw (Exception, std::exception)
491 return createInstanceWithArgumentsAndContext(
492 name, args, m_xContext );
495 } // anon namespace
498 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
499 com_sun_star_comp_rendering_CanvasFactory_get_implementation(::com::sun::star::uno::XComponentContext* context,
500 ::com::sun::star::uno::Sequence<css::uno::Any> const &)
502 return cppu::acquire(new CanvasFactory(context));
506 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */