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 
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 
72  ENSURE_ARG_OR_THROW( pOutDev != nullptr,
73  "Canvas::initialize: invalid OutDev pointer" );
74 
75  awt::Rectangle aBounds;
76  maArguments[1] >>= aBounds;
77 
78  uno::Sequence<sal_Int8> aSeq;
79  maArguments[4] >>= aSeq;
80 
81  const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
82  if( !pSysData || !pSysData->nSize )
83  throw lang::NoSupportException( "Passed SystemGraphicsData invalid!" );
84 
85  bool bHasCairo = pOutDev->SupportsCairo();
86  ENSURE_ARG_OR_THROW(bHasCairo, "SpriteCanvas::SpriteCanvas: No Cairo capability");
87 
88  // setup helper
89  maDeviceHelper.init( *this, *pOutDev );
90 
91  maCanvasHelper.init( basegfx::B2ISize(aBounds.Width, aBounds.Height), *this, this );
92 
93  // forward surface to render on to canvashelper
94  maCanvasHelper.setSurface( maDeviceHelper.getSurface(), false );
95 
96  maArguments.realloc(0);
97  }
98 
100  {
101  SAL_INFO("canvas.cairo", "CairoCanvas destroyed" );
102  }
103 
105  {
106  ::osl::MutexGuard aGuard( m_aMutex );
107 
108  // forward to parent
109  CanvasBaseT::disposeThis();
110  }
111 
112  OUString SAL_CALL Canvas::getServiceName( )
113  {
114  return "com.sun.star.rendering.Canvas.Cairo";
115  }
116 
117  // XServiceInfo
118  sal_Bool Canvas::supportsService(const OUString& sServiceName)
119  {
120  return cppu::supportsService(this, sServiceName);
121 
122  }
124  {
125  return "com.sun.star.comp.rendering.Canvas.Cairo";
126  }
127  css::uno::Sequence< OUString > Canvas::getSupportedServiceNames()
128  {
129  return { getServiceName() };
130  }
131 
132  bool Canvas::repaint( const SurfaceSharedPtr& pSurface,
133  const rendering::ViewState& viewState,
134  const rendering::RenderState& renderState )
135  {
136  return maCanvasHelper.repaint( pSurface, viewState, renderState );
137  }
138 
140  {
141  return maDeviceHelper.getSurface();
142  }
143 
144  SurfaceSharedPtr Canvas::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
145  {
146  return maDeviceHelper.createSurface( rSize, aContent );
147  }
148 
150  {
151  SurfaceSharedPtr pSurface;
152 
154  if( rBitmap.GetSystemData( aData ) ) {
155  const Size& rSize = rBitmap.GetSizePixel();
156 
157  pSurface = maDeviceHelper.createSurface( aData, rSize );
158  }
159 
160  return pSurface;
161  }
162 
164  {
165  // non-modifiable surface here
166  return SurfaceSharedPtr();
167  }
168 
170  {
171  return maDeviceHelper.getOutputDevice();
172  }
173 }
174 
175 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
177  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
178 {
180  p->acquire();
181  try {
182  p->initialize();
183  } catch (css::uno::Exception&) {
184  p->dispose();
185  p->release();
186  throw;
187  }
188  return static_cast<cppu::OWeakObject*>(p.get());
189 }
190 
191 
192 /* 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
osl::Mutex m_aMutex
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)
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
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