1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef INCLUDED_SLIDESHOW_VIEWSHAPE_HXX 25 #define INCLUDED_SLIDESHOW_VIEWSHAPE_HXX 26 27 #include <cppcanvas/renderer.hxx> 28 #include <cppcanvas/bitmap.hxx> 29 30 #include <basegfx/range/b2drectangle.hxx> 31 #include <basegfx/polygon/b2dpolygon.hxx> 32 33 #include <boost/shared_ptr.hpp> 34 #include <boost/utility.hpp> 35 36 #include "tools.hxx" 37 #include "shapeattributelayer.hxx" 38 #include "animatedsprite.hxx" 39 #include "viewlayer.hxx" 40 #include "doctreenode.hxx" 41 42 #include <vector> 43 44 45 namespace slideshow 46 { 47 namespace internal 48 { 49 /** This class is the viewable representation of a draw 50 document's XShape, associated to a specific View 51 52 The class is able to render the associated XShape on 53 View implementations. 54 */ 55 class ViewShape : private boost::noncopyable 56 { 57 public: 58 /** Create a ViewShape for the given View 59 60 @param rView 61 The associated View object. 62 */ 63 explicit ViewShape( const ViewLayerSharedPtr& rViewLayer ); 64 65 /** Query the associated view layer of this shape 66 */ 67 ViewLayerSharedPtr getViewLayer() const; 68 69 /** Query dimension of a safety border around the shape for AA 70 71 If the view performs antialiasing, this method 72 calculates a safety border around the shape, in the 73 shape coordinate system, which is guaranteed to 74 include every pixel touched when rendering the shape. 75 */ 76 ::basegfx::B2DSize getAntialiasingBorder() const; 77 78 79 // animation methods 80 //------------------------------------------------------------------ 81 82 /** Notify the ViewShape that an animation starts now 83 84 This method enters animation mode on the associate 85 target view. The shape can be animated in parallel on 86 different views. 87 88 @return whether the mode change finished successfully. 89 */ 90 bool enterAnimationMode(); 91 92 /** Notify the ViewShape that it is no longer animated 93 94 This methods ends animation mode on the associate 95 target view 96 */ 97 void leaveAnimationMode(); 98 99 /** Query whether the ViewShape is currently animated 100 101 This method checks whether the ViewShape is currently in 102 animation mode. 103 */ isBackgroundDetached() const104 bool isBackgroundDetached() const { return mbAnimationMode; } 105 106 107 // render methods 108 //------------------------------------------------------------------ 109 110 enum UpdateFlags 111 { 112 NONE= 0, 113 TRANSFORMATION= 1, 114 CLIP= 2, 115 ALPHA= 4, 116 POSITION= 8, 117 CONTENT= 16, 118 FORCE= 32 119 }; 120 121 struct RenderArgs 122 { 123 /** Create render argument struct 124 125 @param rOrigBounds 126 The initial shape bounds 127 128 @param rUpdateBounds 129 The area covered by the shape 130 131 @param rBounds 132 The current shape bounds 133 134 @param rAttr 135 The current shape attribute set. Can be NULL, for 136 default attributes. Attention: stored as a reference, 137 thus, parameter object must stay valid! 138 139 @param rSubsets 140 Vector of subset rendering ranges. Attention: 141 stored as a reference, thus, parameter object must 142 stay valid! 143 144 @param nPrio 145 Shape priority 146 */ RenderArgsslideshow::internal::ViewShape::RenderArgs147 RenderArgs( const ::basegfx::B2DRectangle& rOrigBounds, 148 const ::basegfx::B2DRectangle& rUpdateBounds, 149 const ::basegfx::B2DRectangle& rBounds, 150 const ::basegfx::B2DRectangle& rUnitBounds, 151 const ShapeAttributeLayerSharedPtr& rAttr, 152 const VectorOfDocTreeNodes& rSubsets, 153 double nPrio ) : 154 maOrigBounds( rOrigBounds ), 155 maUpdateBounds( rUpdateBounds ), 156 maBounds( rBounds ), 157 maUnitBounds( rUnitBounds ), 158 mrAttr( rAttr ), 159 mrSubsets( rSubsets ), 160 mnShapePriority( nPrio ) 161 { 162 } 163 164 const ::basegfx::B2DRectangle maOrigBounds; 165 const ::basegfx::B2DRectangle maUpdateBounds; 166 const ::basegfx::B2DRectangle maBounds; 167 const ::basegfx::B2DRectangle maUnitBounds; 168 const ShapeAttributeLayerSharedPtr& mrAttr; 169 const VectorOfDocTreeNodes& mrSubsets; 170 const double mnShapePriority; 171 }; 172 173 /** Update the ViewShape 174 175 This method updates the ViewShape on the associated 176 view. If the shape is currently animated, the render 177 target is the sprite, otherwise the view's 178 canvas. This method does not render anything, if the 179 update flags are 0. 180 181 @param rMtf 182 The metafile representation of the shape 183 184 @param rArgs 185 Parameter structure, containing all necessary arguments 186 187 @param nUpdateFlags 188 Bitmask of things to update. Use FORCE to force a repaint. 189 190 @param bIsVisible 191 When false, the shape is fully invisible (and possibly 192 don't need to be painted) 193 194 @return whether the rendering finished successfully. 195 */ 196 bool update( const GDIMetaFileSharedPtr& rMtf, 197 const RenderArgs& rArgs, 198 int nUpdateFlags, 199 bool bIsVisible ) const; 200 201 /** Retrieve renderer for given canvas and metafile. 202 203 If necessary, the renderer is created or updated for 204 the metafile and attribute layer. 205 206 @return a renderer that renders to the given 207 destination canvas 208 */ 209 ::cppcanvas::RendererSharedPtr getRenderer( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas, 210 const GDIMetaFileSharedPtr& rMtf, 211 const ShapeAttributeLayerSharedPtr& rAttr ) const; 212 213 214 private: 215 struct RendererCacheEntry 216 { RendererCacheEntryslideshow::internal::ViewShape::RendererCacheEntry217 RendererCacheEntry() : 218 mpDestinationCanvas(), 219 mpRenderer(), 220 mpMtf(), 221 mpLastBitmap(), 222 mpLastBitmapCanvas() 223 { 224 } 225 getDestinationCanvasslideshow::internal::ViewShape::RendererCacheEntry226 ::cppcanvas::CanvasSharedPtr getDestinationCanvas() 227 { 228 return mpDestinationCanvas; 229 } 230 231 ::cppcanvas::CanvasSharedPtr mpDestinationCanvas; 232 ::cppcanvas::RendererSharedPtr mpRenderer; 233 GDIMetaFileSharedPtr mpMtf; 234 ::cppcanvas::BitmapSharedPtr mpLastBitmap; 235 ::cppcanvas::BitmapCanvasSharedPtr mpLastBitmapCanvas; 236 }; 237 238 typedef ::std::vector< RendererCacheEntry > RendererCacheVector; 239 240 241 /** Prefetch Renderer for given canvas 242 */ 243 bool prefetch( RendererCacheEntry& io_rCacheEntry, 244 const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas, 245 const GDIMetaFileSharedPtr& rMtf, 246 const ShapeAttributeLayerSharedPtr& rAttr ) const; 247 248 /** Draw with prefetched Renderer to stored canvas 249 250 This method draws prefetched Renderer to its 251 associated canvas (which happens to be mpLastCanvas). 252 */ 253 bool draw( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas, 254 const GDIMetaFileSharedPtr& rMtf, 255 const ShapeAttributeLayerSharedPtr& rAttr, 256 const ::basegfx::B2DHomMatrix& rTransform, 257 const ::basegfx::B2DPolyPolygon* pClip, 258 const VectorOfDocTreeNodes& rSubsets ) const; 259 260 /** Render shape to an active sprite 261 */ 262 bool renderSprite( const ViewLayerSharedPtr& rViewLayer, 263 const GDIMetaFileSharedPtr& rMtf, 264 const ::basegfx::B2DRectangle& rOrigBounds, 265 const ::basegfx::B2DRectangle& rBounds, 266 const ::basegfx::B2DRectangle& rUnitBounds, 267 int nUpdateFlags, 268 const ShapeAttributeLayerSharedPtr& pAttr, 269 const VectorOfDocTreeNodes& rSubsets, 270 double nPrio, 271 bool bIsVisible ) const; 272 273 /** Render shape to given canvas 274 */ 275 bool render( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas, 276 const GDIMetaFileSharedPtr& rMtf, 277 const ::basegfx::B2DRectangle& rBounds, 278 const ::basegfx::B2DRectangle& rUpdateBounds, 279 int nUpdateFlags, 280 const ShapeAttributeLayerSharedPtr& pAttr, 281 const VectorOfDocTreeNodes& rSubsets, 282 bool bIsVisible ) const; 283 284 /** Calc sprite size in pixel 285 286 Converts user coordinate system to device pixel, and 287 adds antialiasing border. 288 289 @param rUserSize 290 Size of the sprite in user coordinate system (doc coordinates) 291 */ 292 ::basegfx::B2DSize calcSpriteSizePixel( const ::basegfx::B2DSize& rUserSize ) const; 293 294 enum{ MAX_RENDER_CACHE_ENTRIES=2 }; 295 296 /** Retrieve a valid iterator to renderer cache entry 297 298 This method ensures that an internal limit of 299 MAX_RENDER_CACHE_ENTRIES is not exceeded. 300 301 @param rDestinationCanvas 302 Destination canvas to retrieve cache entry for 303 304 @return a valid iterator to a renderer cache entry for 305 the given canvas. The entry might be 306 default-constructed (if newly added) 307 */ 308 RendererCacheVector::iterator getCacheEntry( const ::cppcanvas::CanvasSharedPtr& rDestinationCanvas ) const; 309 310 void invalidateRenderer() const; 311 312 /** The view layer this object is part of. 313 314 Needed for sprite creation 315 */ 316 ViewLayerSharedPtr mpViewLayer; 317 318 /// A set of cached mtf/canvas combinations 319 mutable RendererCacheVector maRenderers; 320 321 /// The sprite object 322 mutable AnimatedSpriteSharedPtr mpSprite; 323 324 /// If true, render() calls go to the sprite 325 mutable bool mbAnimationMode; 326 327 /// If true, shape needs full repaint (and the sprite a setup, if any) 328 mutable bool mbForceUpdate; 329 }; 330 331 typedef ::boost::shared_ptr< ViewShape > ViewShapeSharedPtr; 332 333 } 334 } 335 336 #endif /* INCLUDED_SLIDESHOW_VIEWSHAPE_HXX */ 337