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  maSize(rSize),
43  mbHasAlpha(bHasAlpha)
44  {
45  ENSURE_OR_THROW( mpSurfaceProvider.is(),
46  "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
47 
48  SAL_INFO(
49  "canvas.cairo",
50  "bitmap size: " << rSize.getX() << "x" << rSize.getY());
51 
52  mpBufferSurface = mpSurfaceProvider->createSurface( rSize, bHasAlpha ? CAIRO_CONTENT_COLOR_ALPHA : CAIRO_CONTENT_COLOR );
53  mpBufferCairo = mpBufferSurface->getCairo();
54 
55  maCanvasHelper.init( rSize, *mpSurfaceProvider, pDevice );
56  maCanvasHelper.setSurface( mpBufferSurface, bHasAlpha );
57 
58  // clear bitmap to 100% transparent
59  maCanvasHelper.clear();
60  }
61 
63  {
64  mpSurfaceProvider.clear();
65 
66  mpBufferCairo.reset();
67  mpBufferSurface.reset();
68 
69  // forward to parent
70  CanvasBitmap_Base::disposeThis();
71  }
72 
74  {
75  return mpBufferSurface;
76  }
77 
78  SurfaceSharedPtr CanvasBitmap::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
79  {
80  return mpSurfaceProvider->createSurface(rSize,aContent);
81  }
82 
84  {
85  return mpSurfaceProvider->createSurface(rBitmap);
86  }
87 
89  {
90  // non-modifiable surface here
91  return SurfaceSharedPtr();
92  }
93 
95  {
96  return mpSurfaceProvider->getOutputDevice();
97  }
98 
99  bool CanvasBitmap::repaint( const SurfaceSharedPtr& pSurface,
100  const rendering::ViewState& viewState,
101  const rendering::RenderState& renderState )
102  {
103  return maCanvasHelper.repaint( pSurface, viewState, renderState );
104  }
105 
106  uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )
107  {
108  uno::Any aRV( sal_Int32(0) );
109  // 0 ... get BitmapEx
110  // 1 ... get Pixbuf with bitmap RGB content
111  // 2 ... return nothing (empty Any)
112  switch( nHandle )
113  {
114  case 0:
115  {
116  aRV <<= reinterpret_cast<sal_Int64>( nullptr );
117  if ( !mbHasAlpha )
118  break;
119 
120  BitmapEx* pBitmapEx = vcl::bitmap::CreateFromCairoSurface(
121  ::Size( maSize.getX(), maSize.getY() ),
122  getSurface()->getCairoSurface().get());
123  if (pBitmapEx)
124  aRV <<= reinterpret_cast<sal_Int64>( pBitmapEx );
125 
126  break;
127  }
128  case 1:
129  {
130  aRV = getOutputDevice()->GetNativeSurfaceHandle(mpBufferSurface, maSize);
131  break;
132  }
133  case 2:
134  {
135  // Always return nothing - for the RGB surface support.
136  // Alpha code paths go via the above case 0.
137  aRV = uno::Any();
138  break;
139  }
140  }
141 
142  return aRV;
143  }
144 
146  {
147  return "CairoCanvas.CanvasBitmap";
148  }
149 
150  sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
151  {
152  return cppu::supportsService( this, ServiceName );
153  }
154 
155  uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
156  {
157  return { "com.sun.star.rendering.CanvasBitmap" };
158  }
159 
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 getY() const
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
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)
virtual ::cairo::SurfaceSharedPtr changeSurface() override
::rtl::Reference< SurfaceProvider > SurfaceProviderRef
sal_Int32 getX() const
virtual OutputDevice * getOutputDevice() override
::basegfx::B2IVector maSize
Definition: dx_9rm.cxx:116
::cairo::CairoSharedPtr mpBufferCairo