LibreOffice Module canvas (master)  1
cairo_canvasbitmap.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 
24 #include <tools/diagnose_ex.h>
25 #include <vcl/bitmapex.hxx>
26 #include <vcl/BitmapTools.hxx>
27 
28 #include <cairo.h>
29 
30 #include "cairo_canvasbitmap.hxx"
31 
32 using namespace ::cairo;
33 using namespace ::com::sun::star;
34 
35 namespace cairocanvas
36 {
37  CanvasBitmap::CanvasBitmap( const ::basegfx::B2ISize& rSize,
38  const SurfaceProviderRef& rSurfaceProvider,
39  rendering::XGraphicDevice* pDevice,
40  bool bHasAlpha ) :
41  mpSurfaceProvider( rSurfaceProvider ),
42  mpBufferSurface(),
43  mpBufferCairo(),
44  maSize(rSize),
45  mbHasAlpha(bHasAlpha)
46  {
47  ENSURE_OR_THROW( mpSurfaceProvider.is(),
48  "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
49 
50  SAL_INFO(
51  "canvas.cairo",
52  "bitmap size: " << rSize.getX() << "x" << rSize.getY());
53 
54  mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
55  mpBufferCairo = mpBufferSurface->getCairo();
56 
57  maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
58  maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
59 
60  // clear bitmap to 100% transparent
61  maCanvasHelper.clear();
62  }
63 
65  {
66  mpSurfaceProvider.clear();
67 
68  mpBufferCairo.reset();
69  mpBufferSurface.reset();
70 
71  // forward to parent
72  CanvasBitmap_Base::disposeThis();
73  }
74 
76  {
77  return mpBufferSurface;
78  }
79 
80  SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
81  {
82  return mpSurfaceProvider->createSurface(rSize,aContent);
83  }
84 
86  {
87  return mpSurfaceProvider->createSurface(rBitmap);
88  }
89 
91  {
92  // non-modifiable surface here
93  return SurfaceSharedPtr();
94  }
95 
97  {
98  return mpSurfaceProvider->getOutputDevice();
99  }
100 
101  bool CanvasBitmap::repaint( const SurfaceSharedPtr& pSurface,
102  const rendering::ViewState& viewState,
103  const rendering::RenderState& renderState )
104  {
105  return maCanvasHelper.repaint( pSurface, viewState, renderState );
106  }
107 
108  uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )
109  {
110  uno::Any aRV( sal_Int32(0) );
111  // 0 ... get BitmapEx
112  // 1 ... get Pixbuf with bitmap RGB content
113  // 2 ... return nothing (empty Any)
114  switch( nHandle )
115  {
116  case 0:
117  {
118  aRV <<= reinterpret_cast<sal_Int64>( nullptr );
119  if ( !mbHasAlpha )
120  break;
121 
122  BitmapEx* pBitmapEx = vcl::bitmap::CreateFromCairoSurface(
123  ::Size( maSize.getX(), maSize.getY() ),
124  getSurface()->getCairoSurface().get());
125  if (pBitmapEx)
126  aRV <<= reinterpret_cast<sal_Int64>( pBitmapEx );
127 
128  break;
129  }
130  case 1:
131  {
132  aRV = getOutputDevice()->GetNativeSurfaceHandle(mpBufferSurface, maSize);
133  break;
134  }
135  case 2:
136  {
137  // Always return nothing - for the RGB surface support.
138  // Alpha code paths go via the above case 0.
139  aRV = uno::Any();
140  break;
141  }
142  }
143 
144  return aRV;
145  }
146 
148  {
149  return "CairoCanvas.CanvasBitmap";
150  }
151 
152  sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
153  {
154  return cppu::supportsService( this, ServiceName );
155  }
156 
157  uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
158  {
159  return { "com.sun.star.rendering.CanvasBitmap" };
160  }
161 
162 }
163 
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void disposeThis() override
Dispose all internal references.
const ::basegfx::B2ISize maSize
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Any SAL_CALL getFastPropertyValue(sal_Int32 nHandle) override
virtual ::cairo::SurfaceSharedPtr getSurface() override
virtual ::cairo::SurfaceSharedPtr createSurface(const ::basegfx::B2ISize &rSize, int aContent) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
CanvasBitmap(const ::basegfx::B2ISize &rSize, const SurfaceProviderRef &rDevice, css::rendering::XGraphicDevice *pDevice, bool bHasAlpha)
Create a canvas bitmap for the given surface.
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
std::shared_ptr< Surface > SurfaceSharedPtr
virtual bool repaint(const ::cairo::SurfaceSharedPtr &pSurface, const css::rendering::ViewState &viewState, const css::rendering::RenderState &renderState) override
css::uno::Any GetNativeSurfaceHandle(cairo::SurfaceSharedPtr &rSurface, const basegfx::B2ISize &rSize) const
sal_Int32 getX() const
unsigned char sal_Bool
::cairo::SurfaceSharedPtr mpBufferSurface
SurfaceProviderRef mpSurfaceProvider
#define ENSURE_OR_THROW(c, m)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
#define SAL_INFO(area, stream)
sal_Int32 getY() const
virtual ::cairo::SurfaceSharedPtr changeSurface() override
::rtl::Reference< SurfaceProvider > SurfaceProviderRef
virtual OutputDevice * getOutputDevice() override
::basegfx::B2IVector maSize
Definition: dx_9rm.cxx:116
::cairo::CairoSharedPtr mpBufferCairo