LibreOffice Module canvas (master)  1
cairo_spritehelper.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 
27 #include <rtl/math.hxx>
28 #include <tools/diagnose_ex.h>
29 
30 #include <cairo.h>
31 
32 #include "cairo_spritehelper.hxx"
33 
34 using namespace ::cairo;
35 using namespace ::com::sun::star;
36 
37 namespace cairocanvas
38 {
41  mpBufferSurface(),
42  mbTextureDirty(true)
43  {}
44 
45  void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
46  const SpriteCanvasRef& rSpriteCanvas )
47  {
48  ENSURE_OR_THROW( rSpriteCanvas,
49  "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
50 
51  mpSpriteCanvas = rSpriteCanvas;
52  mbTextureDirty = true;
53 
54  // also init base class
55  CanvasCustomSpriteHelper::init( rSpriteSize,
56  rSpriteCanvas.get() );
57  }
58 
59  void SpriteHelper::setSurface( const SurfaceSharedPtr& pBufferSurface )
60  {
61  mpBufferSurface = pBufferSurface;
62  }
63 
65  {
66  mpBufferSurface.reset();
67  mpSpriteCanvas.clear();
68 
69  // forward to parent
70  CanvasCustomSpriteHelper::disposing();
71  }
72 
73  void SpriteHelper::redraw( const CairoSharedPtr& pCairo,
74  const ::basegfx::B2DPoint& rPos,
75  bool& /*io_bSurfacesDirty*/,
76  bool /*bBufferedUpdate*/ ) const
77  {
78 #ifdef CAIRO_CANVAS_PERF_TRACE
79  struct timespec aTimer;
80  mxDevice->startPerfTrace( &aTimer );
81 #endif
82 
83  const double fAlpha( getAlpha() );
84  const ::basegfx::B2DHomMatrix aTransform( getTransformation() );
85 
86  if( !isActive() || ::basegfx::fTools::equalZero( fAlpha ) )
87  return;
88 
89  SAL_INFO( "canvas.cairo", "CanvasCustomSprite::redraw called");
90  if( !pCairo )
91  return;
92 
94  cairo_save( pCairo.get() );
95 
96  double fX, fY;
97 
98  fX = rPos.getX();
99  fY = rPos.getY();
100 
101  if( !aTransform.isIdentity() )
102  {
103  cairo_matrix_t aMatrix, aInverseMatrix;
104  cairo_matrix_init( &aMatrix,
105  aTransform.get( 0, 0 ), aTransform.get( 1, 0 ), aTransform.get( 0, 1 ),
106  aTransform.get( 1, 1 ), aTransform.get( 0, 2 ), aTransform.get( 1, 2 ) );
107 
108  aMatrix.x0 = basegfx::fround( aMatrix.x0 );
109  aMatrix.y0 = basegfx::fround( aMatrix.y0 );
110 
111  cairo_matrix_init( &aInverseMatrix, aMatrix.xx, aMatrix.yx, aMatrix.xy, aMatrix.yy, aMatrix.x0, aMatrix.y0 );
112  cairo_matrix_invert( &aInverseMatrix );
113  cairo_matrix_transform_distance( &aInverseMatrix, &fX, &fY );
114 
115  cairo_set_matrix( pCairo.get(), &aMatrix );
116  }
117 
118  fX = basegfx::fround( fX );
119  fY = basegfx::fround( fY );
120 
121  cairo_matrix_t aOrigMatrix;
122  cairo_get_matrix( pCairo.get(), &aOrigMatrix );
123  cairo_translate( pCairo.get(), fX, fY );
124 
125  if( getClip().is() )
126  {
127  const uno::Reference<rendering::XPolyPolygon2D>& rClip( getClip() );
128 
129  ::basegfx::B2DPolyPolygon aClipPoly(
131  rClip ));
132 
133  doPolyPolygonImplementation( aClipPoly, Clip, pCairo.get(),
134  nullptr, SurfaceProviderRef(mpSpriteCanvas.get()),
135  rClip->getFillRule() );
136  }
137 
138  SAL_INFO( "canvas.cairo","aSize " << aSize.getX() << " x " << aSize.getY() << " position: " << fX << "," << fY );
139  cairo_rectangle( pCairo.get(), 0, 0, floor( aSize.getX() ), floor( aSize.getY() ) );
140  cairo_clip( pCairo.get() );
141  cairo_set_matrix( pCairo.get(), &aOrigMatrix );
142 
143  if( isContentFullyOpaque() )
144  cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
145  cairo_set_source_surface( pCairo.get(),
146  mpBufferSurface->getCairoSurface().get(),
147  fX, fY );
148  if( ::rtl::math::approxEqual( fAlpha, 1.0 ) )
149  cairo_paint( pCairo.get() );
150  else
151  cairo_paint_with_alpha( pCairo.get(), fAlpha );
152 
153  cairo_restore( pCairo.get() );
154 
155 #ifdef CAIRO_CANVAS_PERF_TRACE
156  mxDevice->stopPerfTrace( &aTimer, "sprite redraw" );
157 #endif
158  }
159 
160  ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
161  {
162  return ::basegfx::unotools::b2DPolyPolygonFromXPolyPolygon2D(xPoly);
163  }
164 }
165 
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ::basegfx::B2DHomMatrix & getTransformation() const
bool isContentFullyOpaque() const
Returns true, if sprite content bitmap is fully opaque.
virtual ::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(css::uno::Reference< css::rendering::XPolyPolygon2D > &xPoly) const override
Called to convert an API polygon to a basegfx polygon.
double getAlpha() const
Retrieve current alpha value.
const cppcanvas::SpriteCanvasSharedPtr mpSpriteCanvas
double getX() const
double getY() const
void setSurface(const ::cairo::SurfaceSharedPtr &pBufferSurface)
std::shared_ptr< cairo_t > CairoSharedPtr
void init(const css::geometry::RealSize2D &rSpriteSize, const SpriteCanvasRef &rSpriteCanvas)
Late-init the sprite helper.
B2IRange fround(const B2DRange &rRange)
const css::uno::Reference< css::rendering::XPolyPolygon2D > & getClip() const
Retrieve current clip.
std::shared_ptr< Surface > SurfaceSharedPtr
void doPolyPolygonImplementation(const ::basegfx::B2DPolyPolygon &aPolyPolygon, Operation aOperation, cairo_t *pCairo, const uno::Sequence< rendering::Texture > *pTextures, const SurfaceProviderRef &pDevice, rendering::FillRule eFillrule)
static bool equalZero(const double &rfVal)
const ::basegfx::B2DVector & getSizePixel() const
::basegfx::B2DPolyPolygon b2DPolyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
#define ENSURE_OR_THROW(c, m)
#define SAL_INFO(area, stream)
void redraw(const ::cairo::CairoSharedPtr &pCairo, const ::basegfx::B2DPoint &rPos, bool &bSurfacesDirty, bool bBufferedUpdate) const
Repaint sprite content to associated sprite canvas.
bool isActive() const
Retrieve current activation state.
SpriteHelper()
Create sprite helper.
::rtl::Reference< SurfaceProvider > SurfaceProviderRef
::cairo::SurfaceSharedPtr mpBufferSurface