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>
29 #include <vcl/skia/SkiaHelper.hxx>
31 
32 #include "cairo_canvas.hxx"
33 
34 using namespace ::cairo;
35 using namespace ::com::sun::star;
36 
37 namespace cairocanvas
38 {
39  Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
40  const uno::Reference< uno::XComponentContext >& /*rxContext*/ ) :
41  maArguments(aArguments)
42  {
43  }
44 
46  {
47  // #i64742# Only perform initialization when not in probe mode
48  if( !maArguments.hasElements() )
49  return;
50 
51  // tdf#93870 - force VCL canvas in OpenGL mode for now.
54 
55  /* maArguments:
56  0: ptr to creating instance (Window or VirtualDevice)
57  1: current bounds of creating instance
58  2: bool, denoting always on top state for Window (always false for VirtualDevice)
59  3: XWindow for creating Window (or empty for VirtualDevice)
60  4: SystemGraphicsData as a streamed Any
61  */
62  SAL_INFO("canvas.cairo","Canvas created " << this);
63 
64  ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
65  maArguments[0].getValueTypeClass() == uno::TypeClass_HYPER &&
66  maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
67  "Canvas::initialize: wrong number of arguments, or wrong types" );
68 
69  // We expect a single Any here, containing a pointer to a valid
70  // VCL output device, on which to output (mostly needed for text)
71  sal_Int64 nPtr = 0;
72  maArguments[0] >>= nPtr;
73  OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
74 
75  ENSURE_ARG_OR_THROW( pOutDev != nullptr,
76  "Canvas::initialize: invalid OutDev pointer" );
77 
78  awt::Rectangle aBounds;
79  maArguments[1] >>= aBounds;
80 
81  uno::Sequence<sal_Int8> aSeq;
82  maArguments[4] >>= aSeq;
83 
84  const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
85  if( !pSysData || !pSysData->nSize )
86  throw lang::NoSupportException( "Passed SystemGraphicsData invalid!" );
87 
88  bool bHasCairo = pOutDev->SupportsCairo();
89  ENSURE_ARG_OR_THROW(bHasCairo, "SpriteCanvas::SpriteCanvas: No Cairo capability");
90 
91  // setup helper
92  maDeviceHelper.init( *this, *pOutDev );
93 
94  maCanvasHelper.init( basegfx::B2ISize(aBounds.Width, aBounds.Height), *this, this );
95 
96  // forward surface to render on to canvashelper
97  maCanvasHelper.setSurface( maDeviceHelper.getSurface(), false );
98 
99  maArguments.realloc(0);
100  }
101 
103  {
104  SAL_INFO("canvas.cairo", "CairoCanvas destroyed" );
105  }
106 
108  {
109  ::osl::MutexGuard aGuard( m_aMutex );
110 
111  // forward to parent
112  CanvasBaseT::disposeThis();
113  }
114 
115  OUString SAL_CALL Canvas::getServiceName( )
116  {
117  return "com.sun.star.rendering.Canvas.Cairo";
118  }
119 
120  // XServiceInfo
121  sal_Bool Canvas::supportsService(const OUString& sServiceName)
122  {
123  return cppu::supportsService(this, sServiceName);
124 
125  }
127  {
128  return "com.sun.star.comp.rendering.Canvas.Cairo";
129  }
130  css::uno::Sequence< OUString > Canvas::getSupportedServiceNames()
131  {
132  return { getServiceName() };
133  }
134 
135  bool Canvas::repaint( const SurfaceSharedPtr& pSurface,
136  const rendering::ViewState& viewState,
137  const rendering::RenderState& renderState )
138  {
139  return maCanvasHelper.repaint( pSurface, viewState, renderState );
140  }
141 
143  {
144  return maDeviceHelper.getSurface();
145  }
146 
147  SurfaceSharedPtr Canvas::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
148  {
149  return maDeviceHelper.createSurface( rSize, aContent );
150  }
151 
153  {
154  SurfaceSharedPtr pSurface;
155 
157  if( rBitmap.GetSystemData( aData ) ) {
158  const Size& rSize = rBitmap.GetSizePixel();
159 
160  pSurface = maDeviceHelper.createSurface( aData, rSize );
161  }
162 
163  return pSurface;
164  }
165 
167  {
168  // non-modifiable surface here
169  return SurfaceSharedPtr();
170  }
171 
173  {
174  return maDeviceHelper.getOutputDevice();
175  }
176 }
177 
178 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
180  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
181 {
182  auto p = new cairocanvas::Canvas(args, context);
183  p->acquire();
184  try {
185  p->initialize();
186  } catch (css::uno::Exception&) {
187  p->dispose();
188  p->release();
189  throw;
190  }
191  return static_cast<cppu::OWeakObject*>(p);
192 }
193 
194 
195 /* 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
const char aData[]
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)
static bool isVCLOpenGLEnabled()
Product of this component's factory.
virtual ~Canvas() override
For resource tracking.
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
osl::Mutex m_aMutex
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