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