LibreOffice Module canvas (master)  1
dx_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 
31 #include <rtl/math.hxx>
32 #include <tools/diagnose_ex.h>
33 
34 #include <canvas/canvastools.hxx>
35 
37 #include "dx_impltools.hxx"
38 #include "dx_spritehelper.hxx"
39 
40 using namespace ::com::sun::star;
41 
42 namespace dxcanvas
43 {
46  mpBitmap(),
47  mbTextureDirty( true ),
48  mbShowSpriteBounds( false )
49  {
50  }
51 
52  void SpriteHelper::init( const geometry::RealSize2D& rSpriteSize,
53  const SpriteCanvasRef& rSpriteCanvas,
54  const IDXRenderModuleSharedPtr& rRenderModule,
55  const DXSurfaceBitmapSharedPtr& rBitmap,
56  bool bShowSpriteBounds )
57  {
58  ENSURE_OR_THROW( rSpriteCanvas.get() &&
59  rRenderModule &&
60  rBitmap,
61  "SpriteHelper::init(): Invalid device, sprite canvas or surface" );
62 
63  mpSpriteCanvas = rSpriteCanvas;
64  mpBitmap = rBitmap;
65  mbTextureDirty = true;
66  mbShowSpriteBounds = bShowSpriteBounds;
67 
68  // also init base class
69  CanvasCustomSpriteHelper::init( rSpriteSize,
70  rSpriteCanvas.get() );
71  }
72 
74  {
75  mpBitmap.reset();
76  mpSpriteCanvas.clear();
77 
78  // forward to parent
79  CanvasCustomSpriteHelper::disposing();
80  }
81 
82  ::basegfx::B2DPolyPolygon SpriteHelper::polyPolygonFromXPolyPolygon2D( uno::Reference< rendering::XPolyPolygon2D >& xPoly ) const
83  {
85  }
86 
88  {
89  if( !mpBitmap ||
90  !mpSpriteCanvas.get() )
91  {
92  return false; // we're disposed, no redraw necessary
93  }
94 
95  if( !isActive() ||
97  {
98  return false; // sprite is invisible
99  }
100 
101  return true;
102  }
103 
104  void SpriteHelper::redraw( bool& io_bSurfaceDirty ) const
105  {
106  if( !mpBitmap ||
107  !mpSpriteCanvas.get() )
108  {
109  return; // we're disposed
110  }
111 
112  const ::basegfx::B2DPoint& rPos( getPosPixel() );
113  const double fAlpha( getAlpha() );
114 
115  if( isActive() &&
116  !::basegfx::fTools::equalZero( fAlpha ) )
117  {
118 
119  // TODO(Q2): For the time being, Device does not take a target
120  // surface, but always unconditionally renders to the
121  // background buffer.
122 
123  // log output pos in device pixel
124  SAL_INFO("canvas.directx", "SpriteHelper::redraw(): output pos is (" <<
125  rPos.getX() << "," << rPos.getY() << ")" );
126 
127  const ::basegfx::B2DVector& rSize( getSizePixel() );
128  const ::basegfx::B2DHomMatrix& rTransform( getTransformation() );
129  const uno::Reference< rendering::XPolyPolygon2D >& xClip( getClip() );
130 
131  mbTextureDirty = false;
132  io_bSurfaceDirty = false; // state taken, and processed.
133 
134  ::basegfx::B2DPolyPolygon aClipPath; // empty for no clip
135  bool bIsClipRectangular( false ); // false, if no
136  // clip, or clip
137  // is complex
138 
139  // setup and apply clip (if any)
140  // =================================
141 
142  if( xClip.is() )
143  {
144  aClipPath = tools::polyPolygonFromXPolyPolygon2D( xClip );
145 
146  const sal_Int32 nNumClipPolygons( aClipPath.count() );
147  if( nNumClipPolygons )
148  {
149  // TODO(P2): hold rectangle attribute directly
150  // at the XPolyPolygon2D
151 
152  // check whether the clip is rectangular
153  if( nNumClipPolygons == 1 )
154  if( ::basegfx::utils::isRectangle( aClipPath.getB2DPolygon( 0 ) ) )
155  bIsClipRectangular = true;
156  }
157  }
158 
159  const ::basegfx::B2DRectangle aSourceRect( 0.0,
160  0.0,
161  rSize.getX(),
162  rSize.getY() );
163 
164  // draw simple rectangular area if no clip is set.
165  if( !aClipPath.count() )
166  {
167  mpBitmap->draw(fAlpha,rPos,rTransform);
168  }
169  else if( bIsClipRectangular )
170  {
171  // apply a simple rect clip
172  // ========================
173 
174  ::basegfx::B2DRectangle aClipBounds(
175  ::basegfx::utils::getRange( aClipPath ) );
176  aClipBounds.intersect( aSourceRect );
177 
178  mpBitmap->draw(fAlpha,rPos,aClipBounds,rTransform);
179  }
180  else
181  {
182  // apply clip the hard way
183  // =======================
184 
185  mpBitmap->draw(fAlpha,rPos,aClipPath,rTransform);
186  }
187 
188  if( mbShowSpriteBounds )
189  {
190  if( aClipPath.count() )
191  {
192  // TODO(F2): Re-enable debug output
193  }
194  }
195  }
196  }
197 }
198 
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ::basegfx::B2DHomMatrix & getTransformation() const
double getAlpha() const
Retrieve current alpha value.
std::shared_ptr< IDXRenderModule > IDXRenderModuleSharedPtr
const cppcanvas::SpriteCanvasSharedPtr mpSpriteCanvas
SpriteCanvasRef mpSpriteCanvas
const ::basegfx::B2DPoint & getPosPixel() const
B2DPolygon const & getB2DPolygon(sal_uInt32 nIndex) const
void init(const css::geometry::RealSize2D &rSpriteSize, const SpriteCanvasRef &rSpriteCanvas, const IDXRenderModuleSharedPtr &rRenderModule, const DXSurfaceBitmapSharedPtr &rBitmap, bool bShowSpriteBounds)
Late-init the sprite helper.
::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(const uno::Reference< rendering::XPolyPolygon2D > &xPoly)
bool needRedraw() const
Returns true, if the sprite really needs redraw.
const css::uno::Reference< css::rendering::XPolyPolygon2D > & getClip() const
Retrieve current clip.
static bool equalZero(const double &rfVal)
const ::basegfx::B2DVector & getSizePixel() const
std::shared_ptr< DXSurfaceBitmap > DXSurfaceBitmapSharedPtr
virtual ::basegfx::B2DPolyPolygon polyPolygonFromXPolyPolygon2D(css::uno::Reference< css::rendering::XPolyPolygon2D > &xPoly) const override
Called to convert an API polygon to a basegfx polygon.
void intersect(const B2DRange &rRange)
B2DRange getRange(const B2DPolygon &rCandidate)
#define ENSURE_OR_THROW(c, m)
sal_uInt32 count() const
#define SAL_INFO(area, stream)
SpriteHelper()
Create sprite helper.
bool isActive() const
Retrieve current activation state.
bool isRectangle(const B2DPolygon &rPoly)
void redraw(bool &io_bSurfaceDirty) const
Repaint sprite content via hardware to associated sprite canvas.
DXSurfaceBitmapSharedPtr mpBitmap