LibreOffice Module canvas (master)  1
cairo_canvas.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 #include <sal/log.hxx>
22 
23 #include <com/sun/star/awt/Rectangle.hpp>
24 #include <com/sun/star/lang/NoSupportException.hpp>
25 #include <osl/mutex.hxx>
26 #include <tools/diagnose_ex.h>
27 #include <vcl/sysdata.hxx>
28 #include <vcl/skia/SkiaHelper.hxx>
30 
31 #include "cairo_canvas.hxx"
32 
33 using namespace ::cairo;
34 using namespace ::com::sun::star;
35 
36 namespace cairocanvas
37 {
38  Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
39  const uno::Reference< uno::XComponentContext >& /*rxContext*/ ) :
40  maArguments(aArguments)
41  {
42  }
43 
45  {
46  // #i64742# Only perform initialization when not in probe mode
47  if( !maArguments.hasElements() )
48  return;
49 
50  assert( !SkiaHelper::isVCLSkiaEnabled() );
51 
52  /* maArguments:
53  0: ptr to creating instance (Window or VirtualDevice)
54  1: current bounds of creating instance
55  2: bool, denoting always on top state for Window (always false for VirtualDevice)
56  3: XWindow for creating Window (or empty for VirtualDevice)
57  4: SystemGraphicsData as a streamed Any
58  */
59  SAL_INFO("canvas.cairo","Canvas created " << this);
60 
61  ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
62  maArguments[0].getValueTypeClass() == uno::TypeClass_HYPER &&
63  maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
64  "Canvas::initialize: wrong number of arguments, or wrong types" );
65 
66  // We expect a single Any here, containing a pointer to a valid
67  // VCL output device, on which to output (mostly needed for text)
68  sal_Int64 nPtr = 0;
69  maArguments[0] >>= nPtr;
70  OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
71  ENSURE_ARG_OR_THROW( pOutDev != nullptr,
72  "Canvas::initialize: invalid OutDev pointer" );
73 
74  awt::Rectangle aBounds;
75  maArguments[1] >>= aBounds;
76 
77  uno::Sequence<sal_Int8> aSeq;
78  maArguments[4] >>= aSeq;
79 
80  const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
81  if( !pSysData || !pSysData->nSize )
82  throw lang::NoSupportException( "Passed SystemGraphicsData invalid!" );
83 
84  bool bHasCairo = pOutDev->SupportsCairo();
85  ENSURE_ARG_OR_THROW(bHasCairo, "SpriteCanvas::SpriteCanvas: No Cairo capability");
86 
87  // setup helper
88  maDeviceHelper.init( *this, *pOutDev );
89 
90  maCanvasHelper.init( basegfx::B2ISize(aBounds.Width, aBounds.Height), *this, this );
91 
92  // forward surface to render on to canvashelper
93  maCanvasHelper.setSurface( maDeviceHelper.getSurface(), false );
94 
95  maArguments.realloc(0);
96  }
97 
99  {
100  SAL_INFO("canvas.cairo", "CairoCanvas destroyed" );
101  }
102 
104  {
105  ::osl::MutexGuard aGuard( m_aMutex );
106 
107  // forward to parent
108  CanvasBaseT::disposeThis();
109  }
110 
111  OUString SAL_CALL Canvas::getServiceName( )
112  {
113  return "com.sun.star.rendering.Canvas.Cairo";
114  }
115 
116  // XServiceInfo
117  sal_Bool Canvas::supportsService(const OUString& sServiceName)
118  {
119  return cppu::supportsService(this, sServiceName);
120 
121  }
123  {
124  return "com.sun.star.comp.rendering.Canvas.Cairo";
125  }
126  css::uno::Sequence< OUString > Canvas::getSupportedServiceNames()
127  {
128  return { getServiceName() };
129  }
130 
131  bool Canvas::repaint( const SurfaceSharedPtr& pSurface,
132  const rendering::ViewState& viewState,
133  const rendering::RenderState& renderState )
134  {
135  return maCanvasHelper.repaint( pSurface, viewState, renderState );
136  }
137 
139  {
140  return maDeviceHelper.getSurface();
141  }
142 
143  SurfaceSharedPtr Canvas::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
144  {
145  return maDeviceHelper.createSurface( rSize, aContent );
146  }
147 
149  {
150  SurfaceSharedPtr pSurface;
151 
153  if( rBitmap.GetSystemData( aData ) ) {
154  const Size& rSize = rBitmap.GetSizePixel();
155 
156  pSurface = maDeviceHelper.createSurface( aData, rSize );
157  }
158 
159  return pSurface;
160  }
161 
163  {
164  // non-modifiable surface here
165  return SurfaceSharedPtr();
166  }
167 
169  {
170  return maDeviceHelper.getOutputDevice();
171  }
172 }
173 
174 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
176  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
177 {
179  p->acquire();
180  try {
181  p->initialize();
182  } catch (css::uno::Exception&) {
183  p->dispose();
184  p->release();
185  throw;
186  }
187  return static_cast<cppu::OWeakObject*>(p.get());
188 }
189 
190 
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Canvas(const css::uno::Sequence< css::uno::Any > &aArguments, const css::uno::Reference< css::uno::XComponentContext > &rxContext)
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
bool SupportsCairo() const
Size GetSizePixel() const
virtual void disposeThis() override
Dispose all internal references.
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_rendering_Canvas_Cairo_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::mutex m_aMutex
Product of this component's factory.
virtual ~Canvas() override
For resource tracking.
constexpr OUStringLiteral aData
std::shared_ptr< Surface > SurfaceSharedPtr
virtual ::cairo::SurfaceSharedPtr changeSurface() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
unsigned char sal_Bool
bool GetSystemData(BitmapSystemData &rData) const
#define ENSURE_ARG_OR_THROW(c, m)
virtual OutputDevice * getOutputDevice() override
virtual OUString SAL_CALL getImplementationName() override
VCL_DLLPUBLIC bool isVCLSkiaEnabled()
#define SAL_INFO(area, stream)
css::uno::Sequence< css::uno::Any > maArguments
void * p
Sequence< sal_Int8 > aSeq
virtual ::cairo::SurfaceSharedPtr getSurface() override
virtual ::cairo::SurfaceSharedPtr createSurface(const ::basegfx::B2ISize &rSize, int aContent) override
virtual OUString SAL_CALL getServiceName() override
virtual bool repaint(const ::cairo::SurfaceSharedPtr &pSurface, const css::rendering::ViewState &viewState, const css::rendering::RenderState &renderState) override
bool m_bDetectedRangeSegmentation false