xref: /trunk/main/slideshow/source/engine/slideview.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 #include "precompiled_slideshow.hxx"
25 
26 #include <canvas/debug.hxx>
27 #include <tools/diagnose_ex.h>
28 #include <canvas/canvastools.hxx>
29 
30 #include "eventqueue.hxx"
31 #include "eventmultiplexer.hxx"
32 #include "slideview.hxx"
33 #include "delayevent.hxx"
34 #include "unoview.hxx"
35 
36 #include <rtl/instance.hxx>
37 #include <cppuhelper/basemutex.hxx>
38 #include <cppuhelper/compbase2.hxx>
39 #include <cppuhelper/implementationentry.hxx>
40 #include <cppuhelper/interfacecontainer.h>
41 #include <comphelper/make_shared_from_uno.hxx>
42 
43 #include <cppcanvas/spritecanvas.hxx>
44 #include <cppcanvas/customsprite.hxx>
45 #include <cppcanvas/vclfactory.hxx>
46 #include <cppcanvas/basegfxfactory.hxx>
47 
48 #include <tools/debug.hxx>
49 
50 #include <basegfx/range/b1drange.hxx>
51 #include <basegfx/range/b2drange.hxx>
52 #include <basegfx/range/b2irange.hxx>
53 #include <basegfx/point/b2dpoint.hxx>
54 #include <basegfx/polygon/b2dpolygon.hxx>
55 #include <basegfx/matrix/b2dhommatrix.hxx>
56 #include <basegfx/polygon/b2dpolygontools.hxx>
57 #include <basegfx/polygon/b2dpolypolygontools.hxx>
58 #include <basegfx/tools/canvastools.hxx>
59 #include <basegfx/polygon/b2dpolygonclipper.hxx>
60 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
61 
62 #include <com/sun/star/presentation/XSlideShow.hpp>
63 
64 #include <boost/noncopyable.hpp>
65 #include <boost/bind.hpp>
66 #include <boost/weak_ptr.hpp>
67 
68 #include <vector>
69 #include <iterator>
70 #include <algorithm>
71 
72 using namespace com::sun::star;
73 
74 namespace slideshow {
75 namespace internal {
76 
77 namespace {
78 
79 struct StaticUnitRectPoly : public rtl::StaticWithInit<basegfx::B2DPolygon, StaticUnitRectPoly>
80 {
operator ()slideshow::internal::__anon6e2ed7a90111::StaticUnitRectPoly81     basegfx::B2DPolygon operator()()
82     {
83         return basegfx::tools::createUnitPolygon();
84     }
85 };
86 
87 /** Sprite entry, to store sprite plus priority
88 
89     The operator<() defines a strict weak ordering of sprites, sort
90     key is the sprite priority.
91  */
92 struct SpriteEntry
93 {
SpriteEntryslideshow::internal::__anon6e2ed7a90111::SpriteEntry94     SpriteEntry( const cppcanvas::CustomSpriteSharedPtr& rSprite,
95                  double                                  nPrio ) :
96         mpSprite( rSprite ),
97         mnPriority( nPrio )
98     {
99     }
100 
operator <slideshow::internal::__anon6e2ed7a90111::SpriteEntry101     bool operator<(const SpriteEntry& rRHS) const
102     {
103         return mnPriority < rRHS.mnPriority;
104     }
105 
106     boost::weak_ptr< cppcanvas::CustomSprite > mpSprite;
107     double                                     mnPriority;
108 };
109 
110 typedef std::vector< SpriteEntry > SpriteVector;
111 
112 
113 /** Create a clip polygon for slide views
114 
115     @param rClip
116     Clip to set (can be empty)
117 
118     @param rCanvas
119     Canvas to create the clip polygon for
120 
121     @param rUserSize
122     The size of the view. Note that the returned clip will
123     <em>always</em> clip to at least the rect defined herein.
124 
125     @return the view clip polygon, in view coordinates, which is
126     guaranteed to at least clip to the view size.
127  */
createClipPolygon(const basegfx::B2DPolyPolygon & rClip,const cppcanvas::CanvasSharedPtr &,const basegfx::B2DSize & rUserSize)128 basegfx::B2DPolyPolygon createClipPolygon( const basegfx::B2DPolyPolygon&    rClip,
129                                            const cppcanvas::CanvasSharedPtr& /*rCanvas*/,
130                                            const basegfx::B2DSize&           rUserSize )
131 {
132     // setup canvas clipping
133     // =====================
134 
135     // AW: Simplified
136     const basegfx::B2DRange aClipRange(0, 0, rUserSize.getX(), rUserSize.getY());
137 
138     if(rClip.count())
139     {
140         return basegfx::tools::clipPolyPolygonOnRange(rClip, aClipRange, true, false);
141     }
142     else
143     {
144         return basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aClipRange));
145     }
146 }
147 
148 /** Prepare given clip polygon to be stored as the current clip
149 
150     Note that this is separate from createClipPolygon(), to allow
151     SlideView implementations to store this intermediate result
152     (createClipPolygon() has to be called every time the view size
153     changes)
154  */
prepareClip(const basegfx::B2DPolyPolygon & rClip)155 basegfx::B2DPolyPolygon prepareClip( const basegfx::B2DPolyPolygon& rClip )
156 {
157     basegfx::B2DPolyPolygon aClip( rClip );
158 
159     // TODO(P2): unnecessary, once XCanvas is correctly handling this
160     // AW: Should be no longer necessary; tools are now bezier-safe
161     if( aClip.areControlPointsUsed() )
162         aClip = basegfx::tools::adaptiveSubdivideByAngle( aClip );
163 
164     // normalize polygon, preparation for clipping
165     // in updateCanvas()
166     aClip = basegfx::tools::correctOrientations(aClip);
167     aClip = basegfx::tools::solveCrossovers(aClip);
168     aClip = basegfx::tools::stripNeutralPolygons(aClip);
169     aClip = basegfx::tools::stripDispensablePolygons(aClip, false);
170 
171     return aClip;
172 }
173 
174 
clearRect(::cppcanvas::CanvasSharedPtr const & pCanvas,basegfx::B2IRange const & rArea)175 void clearRect( ::cppcanvas::CanvasSharedPtr const& pCanvas,
176                 basegfx::B2IRange const&            rArea )
177 {
178     // convert clip polygon to device coordinate system
179     ::basegfx::B2DPolyPolygon const* pClipPoly( pCanvas->getClip() );
180     if( pClipPoly )
181     {
182         ::basegfx::B2DPolyPolygon aClipPoly( *pClipPoly );
183         aClipPoly.transform( pCanvas->getTransformation() );
184         pCanvas->setClip( aClipPoly );
185     }
186 
187     // set transformation to identity (->device pixel)
188     pCanvas->setTransformation( ::basegfx::B2DHomMatrix() );
189 
190     // #i42440# Fill the _full_ background in
191     // black. Since we had to extend the bitmap by one
192     // pixel, and the bitmap is initialized white,
193     // depending on the slide content a one pixel wide
194     // line will show to the bottom and the right.
195     const ::basegfx::B2DPolygon aPoly(
196         ::basegfx::tools::createPolygonFromRect(
197             basegfx::B2DRange(rArea)));
198 
199     ::cppcanvas::PolyPolygonSharedPtr pPolyPoly(
200         ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( pCanvas,
201                                                                       aPoly ) );
202 
203     if( pPolyPoly )
204     {
205         pPolyPoly->setCompositeOp( cppcanvas::CanvasGraphic::SOURCE );
206         pPolyPoly->setRGBAFillColor( 0x00000000U );
207         pPolyPoly->draw();
208     }
209 
210 #if defined(VERBOSE) && defined(DBG_UTIL)
211     ::cppcanvas::CanvasSharedPtr pCliplessCanvas( pCanvas->clone() );
212     pCliplessCanvas->setClip();
213 
214     if( pCanvas->getClip() )
215     {
216         ::cppcanvas::PolyPolygonSharedPtr pPolyPoly2(
217             ::cppcanvas::BaseGfxFactory::getInstance().createPolyPolygon( pCliplessCanvas,
218                                                                           *(pCanvas->getClip()) ));
219         if( pPolyPoly2 )
220         {
221             pPolyPoly2->setRGBALineColor( 0x008000FFU );
222             pPolyPoly2->draw();
223         }
224     }
225 #endif
226 }
227 
228 /** Get bounds in pixel
229 
230     @param rLayerBounds
231     Bound rect, in user space coordinates
232 
233     @param rTransformation
234     User space to device pixel transformation
235 
236     @return the layer bounds in pixel, extended by one pixel to the
237     right and bottom
238  */
getLayerBoundsPixel(basegfx::B2DRange const & rLayerBounds,basegfx::B2DHomMatrix const & rTransformation)239 basegfx::B2IRange getLayerBoundsPixel( basegfx::B2DRange const&     rLayerBounds,
240                                        basegfx::B2DHomMatrix const& rTransformation )
241 {
242     ::basegfx::B2DRange aTmpRect;
243     ::canvas::tools::calcTransformedRectBounds( aTmpRect,
244                                                 rLayerBounds,
245                                                 rTransformation );
246 
247     if( aTmpRect.isEmpty() )
248         return ::basegfx::B2IRange();
249 
250     // #i42440# Returned layer size is one pixel too small, as
251     // rendering happens one pixel to the right and below the
252     // actual bound rect.
253     return ::basegfx::B2IRange( ::basegfx::fround(aTmpRect.getMinX()),
254                                 ::basegfx::fround(aTmpRect.getMinY()),
255                                 ::basegfx::fround(aTmpRect.getMaxX()) + 1,
256                                 ::basegfx::fround(aTmpRect.getMaxY()) + 1 );
257 }
258 
259 
260 // ----------------------------------------------------------------
261 
262 /** Container class for sprites issued by a ViewLayer
263 
264     This class handles the sprite prioritization issues, that are
265     needed for layer sprites (e.g. the need to re-prioritize sprites
266     when the layer changes prio).
267  */
268 class LayerSpriteContainer
269 {
270     /** Max fill level of maSprites, before we try to prune it from
271         deceased sprites
272     */
273     enum{ SPRITE_ULLAGE=256 };
274 
275     /** All sprites that have been issued by this container (pruned
276         from time to time, for invalid references). This vector is
277         kept sorted with increasing sprite priority.
278     */
279     SpriteVector       maSprites;
280 
281     // Priority of this layer, relative to other view layers
282     basegfx::B1DRange maLayerPrioRange;
283 
getSpritePriority(std::size_t nSpriteNum) const284     double getSpritePriority( std::size_t nSpriteNum ) const
285     {
286         // divide the available layer range equally between all
287         // sprites, assign upper bound of individual sprite range as
288         // sprite prio (the layer itself gets assigned the lower bound
289         // of sprite 0's individual range):
290         //
291         // | layer 0                    | layer 1                    | ...
292         // |    sprite 0 |    sprite 1  |    sprite 0 |    sprite 1  | ...
293         return maLayerPrioRange.getMinimum() + maLayerPrioRange.getRange()*(nSpriteNum+1)/(maSprites.size()+1);
294     }
295 
296     /** Rescan sprite vector, and remove deceased sprites (and reset
297         sprite prio)
298 
299         @param aBegin
300         Iterator to the first entry to rescan
301      */
updateSprites()302     void updateSprites()
303     {
304         SpriteVector aValidSprites;
305 
306         // check all sprites for validity and set new priority
307         SpriteVector::iterator       aCurrSprite( maSprites.begin() );
308         const SpriteVector::iterator aEnd( maSprites.end() );
309         while( aCurrSprite != aEnd )
310         {
311             cppcanvas::CustomSpriteSharedPtr pCurrSprite( aCurrSprite->mpSprite.lock() );
312 
313             if( pCurrSprite )
314             {
315                 // only copy still valid sprites over to the refreshed
316                 // sprite vector.
317                 aValidSprites.push_back( *aCurrSprite );
318 
319                 pCurrSprite->setPriority(
320                     getSpritePriority( aValidSprites.size()-1 ));
321             }
322 
323             ++aCurrSprite;
324         }
325 
326         // replace sprite list with pruned one
327         maSprites.swap( aValidSprites );
328     }
329 
330 public:
LayerSpriteContainer()331     LayerSpriteContainer() :
332         maSprites(),
333         maLayerPrioRange()
334     {
335     }
336 
getLayerPriority() const337     basegfx::B1DRange getLayerPriority() const
338     {
339         return maLayerPrioRange;
340     }
341 
setLayerPriority(const basegfx::B1DRange & rRange)342     void setLayerPriority( const basegfx::B1DRange& rRange )
343     {
344         if( rRange != maLayerPrioRange )
345         {
346             maLayerPrioRange = rRange;
347 
348             // prune and recalc sprite prios
349             updateSprites();
350         }
351     }
352 
addSprite(const cppcanvas::CustomSpriteSharedPtr & pSprite,double nPriority)353     void addSprite( const cppcanvas::CustomSpriteSharedPtr& pSprite,
354                     double                                  nPriority )
355     {
356         if( !pSprite )
357             return;
358 
359         SpriteEntry aEntry( pSprite,nPriority );
360 
361         // insert new sprite, such that vector stays sorted
362         SpriteVector::iterator aInsertPos(
363             maSprites.insert(
364                 std::lower_bound( maSprites.begin(),
365                                   maSprites.end(),
366                                   aEntry ),
367                 aEntry ));
368 
369         const std::size_t nNumSprites( maSprites.size() );
370         if( nNumSprites > SPRITE_ULLAGE ||
371             maSprites.end() - aInsertPos > 1 )
372         {
373             // updateSprites() also updates all sprite prios
374             updateSprites();
375         }
376         else
377         {
378             // added sprite to the end, and not too many sprites in
379             // vector - perform optimized update (only need to set
380             // prio). This basically caters for the common case of
381             // iterated character animations, which generate lots of
382             // sprites, all added to the end.
383             pSprite->setPriority(
384                 getSpritePriority( nNumSprites-1 ));
385         }
386     }
387 
clear()388     void clear()
389     {
390         maSprites.clear();
391     }
392 };
393 
394 
395 // ----------------------------------------------------------------
396 
397 
398 /** This class provides layers for a slide view
399 
400     Layers are used to render animations with the correct z order -
401     because sprites are always in front of the static canvas
402     background, shapes that must appear <em<before</em> an animation
403     must also be displayed as a sprite.
404 
405     Each layer has a priority assigned to it (valid range [0,1]), which
406     also affects all sprites created for this specific layer - i.e. if
407     the layer priority changes, the sprites change z order together
408     with their parent.
409  */
410 class SlideViewLayer : public ViewLayer,
411                        private boost::noncopyable
412 {
413     // Smart container for all sprites issued by this layer
414     mutable LayerSpriteContainer             maSpriteContainer;
415 
416     // Bounds of this layer in user space coordinates
417     basegfx::B2DRange                        maLayerBounds;
418 
419     // Bounds of this layer in device pixel
420     mutable basegfx::B2IRange                maLayerBoundsPixel;
421 
422     // Current clip polygon in user coordinates
423     basegfx::B2DPolyPolygon                  maClip;
424 
425     // Current size of the view in user coordinates
426     basegfx::B2DSize                         maUserSize;
427 
428     // Current overall view transformation
429     basegfx::B2DHomMatrix                    maTransformation;
430 
431     // 'parent' canvas, this viewlayer is associated with
432     const cppcanvas::SpriteCanvasSharedPtr   mpSpriteCanvas;
433 
434     /** output surface (necessarily a sprite, won't otherwise be able
435         to display anything <em>before</em> other sprites)
436     */
437     mutable cppcanvas::CustomSpriteSharedPtr mpSprite;
438 
439     // actual output canvas retrieved from a sprite
440     mutable cppcanvas::CanvasSharedPtr       mpOutputCanvas;
441 
442     // ptr back to owning view. needed for isOnView() method
443     View const* const                        mpParentView;
444 
445 public:
446     /** Create a new layer
447 
448         @param pCanvas
449         Sprite canvas to create the layer on
450 
451         @param rTransform
452         Initial overall canvas transformation
453 
454         @param rLayerBounds
455         Initial layer bounds, in view coordinate system
456      */
SlideViewLayer(const cppcanvas::SpriteCanvasSharedPtr & pCanvas,const basegfx::B2DHomMatrix & rTransform,const basegfx::B2DRange & rLayerBounds,const basegfx::B2DSize & rUserSize,View const * const pParentView)457     SlideViewLayer( const cppcanvas::SpriteCanvasSharedPtr& pCanvas,
458                     const basegfx::B2DHomMatrix&            rTransform,
459                     const basegfx::B2DRange&                rLayerBounds,
460                     const basegfx::B2DSize&                 rUserSize,
461                     View const* const                       pParentView) :
462         maSpriteContainer(),
463         maLayerBounds(rLayerBounds),
464         maLayerBoundsPixel(),
465         maClip(),
466         maUserSize(rUserSize),
467         maTransformation(rTransform),
468         mpSpriteCanvas(pCanvas),
469         mpSprite(),
470         mpOutputCanvas(),
471         mpParentView(pParentView)
472     {
473     }
474 
updateView(const basegfx::B2DHomMatrix & rMatrix,const basegfx::B2DSize & rUserSize)475     void updateView( const basegfx::B2DHomMatrix& rMatrix,
476                      const basegfx::B2DSize&      rUserSize )
477     {
478         maTransformation = rMatrix;
479         maUserSize = rUserSize;
480 
481         // limit layer bounds to visible screen
482         maLayerBounds.intersect( basegfx::B2DRange(0.0,
483                                                    0.0,
484                                                    maUserSize.getX(),
485                                                    maUserSize.getY()) );
486 
487         basegfx::B2IRange const& rNewLayerPixel(
488             getLayerBoundsPixel(maLayerBounds,
489                                 maTransformation) );
490         if( rNewLayerPixel != maLayerBoundsPixel )
491         {
492             // re-gen sprite with new size
493             mpOutputCanvas.reset();
494             mpSprite.reset();
495         }
496     }
497 
498 private:
499     // ViewLayer interface
500     // ----------------------------------------------
501 
createSprite(const::basegfx::B2DSize & rSpriteSizePixel,double nPriority) const502     virtual cppcanvas::CustomSpriteSharedPtr createSprite(
503         const ::basegfx::B2DSize& rSpriteSizePixel,
504         double                    nPriority ) const
505     {
506         cppcanvas::CustomSpriteSharedPtr pSprite(
507             mpSpriteCanvas->createCustomSprite( rSpriteSizePixel ) );
508 
509         maSpriteContainer.addSprite( pSprite,
510                                      nPriority );
511 
512         return pSprite;
513     }
514 
setPriority(const basegfx::B1DRange & rRange)515     virtual void setPriority( const basegfx::B1DRange& rRange )
516     {
517         OSL_ENSURE( !rRange.isEmpty() &&
518                     rRange.getMinimum() >= 1.0,
519                     "SlideViewLayer::setPriority(): prio MUST be larger than 1.0 (because "
520                     "the background layer already lies there)" );
521 
522         maSpriteContainer.setLayerPriority( rRange );
523 
524         if( mpSprite )
525             mpSprite->setPriority( rRange.getMinimum() );
526     }
527 
getTransformation() const528     virtual basegfx::B2DHomMatrix getTransformation() const
529     {
530         // Offset given transformation by left, top border of given
531         // range (after transformation through given transformation)
532         basegfx::B2DRectangle aTmpRect;
533         canvas::tools::calcTransformedRectBounds( aTmpRect,
534                                                   maLayerBounds,
535                                                   maTransformation );
536 
537         basegfx::B2DHomMatrix aMatrix( maTransformation );
538 
539         // Add translation according to the origin of aTmpRect. Ignore the
540         // translation when aTmpRect was not properly initialized.
541         if ( ! aTmpRect.isEmpty())
542         {
543             aMatrix.translate( -basegfx::fround(aTmpRect.getMinX()),
544                                -basegfx::fround(aTmpRect.getMinY()) );
545         }
546 
547         return aMatrix;
548     }
549 
getSpriteTransformation() const550     virtual basegfx::B2DHomMatrix getSpriteTransformation() const
551     {
552         return maTransformation;
553     }
554 
clear() const555     virtual void clear() const
556     {
557         // keep layer clip
558         clearRect(getCanvas()->clone(),
559                   maLayerBoundsPixel);
560     }
561 
clearAll() const562     virtual void clearAll() const
563     {
564         ::cppcanvas::CanvasSharedPtr pCanvas( getCanvas()->clone() );
565 
566         // clear layer clip, to clear whole area
567         pCanvas->setClip();
568 
569         clearRect(pCanvas,
570                   maLayerBoundsPixel);
571     }
572 
isOnView(boost::shared_ptr<View> const & rView) const573     virtual bool isOnView(boost::shared_ptr<View> const& rView) const
574     {
575         return rView.get() == mpParentView;
576     }
577 
getCanvas() const578     virtual cppcanvas::CanvasSharedPtr getCanvas() const
579     {
580         if( !mpOutputCanvas )
581         {
582             if( !mpSprite )
583             {
584                 maLayerBoundsPixel = getLayerBoundsPixel(maLayerBounds,
585                                                          maTransformation);
586 
587                 // HACK: ensure at least 1x1 pixel size. clients might
588                 // need an actual canvas (e.g. for bound rect
589                 // calculations) without rendering anything. Better
590                 // solution: introduce something like a reference
591                 // canvas for ViewLayers, which is always available.
592                 if( maLayerBoundsPixel.isEmpty() )
593                     maLayerBoundsPixel = basegfx::B2IRange(0,0,1,1);
594 
595                 const basegfx::B2I64Tuple& rSpriteSize(maLayerBoundsPixel.getRange());
596                 mpSprite = mpSpriteCanvas->createCustomSprite(
597                     basegfx::B2DVector(sal::static_int_cast<sal_Int32>(rSpriteSize.getX()),
598                                        sal::static_int_cast<sal_Int32>(rSpriteSize.getY())) );
599 
600                 mpSprite->setPriority(
601                     maSpriteContainer.getLayerPriority().getMinimum() );
602 
603 #if defined(VERBOSE) && defined(DBG_UTIL)
604                 mpSprite->movePixel(
605                     basegfx::B2DPoint(maLayerBoundsPixel.getMinimum()) +
606                     basegfx::B2DPoint(10,10) );
607 
608                 mpSprite->setAlpha(0.5);
609 #else
610                 mpSprite->movePixel(
611                     basegfx::B2DPoint(maLayerBoundsPixel.getMinimum()) );
612 
613                 mpSprite->setAlpha(1.0);
614 #endif
615                 mpSprite->show();
616             }
617 
618             ENSURE_OR_THROW( mpSprite,
619                               "SlideViewLayer::getCanvas(): no layer sprite" );
620 
621             mpOutputCanvas = mpSprite->getContentCanvas();
622 
623             ENSURE_OR_THROW( mpOutputCanvas,
624                               "SlideViewLayer::getCanvas(): sprite doesn't yield a canvas" );
625 
626             // new canvas retrieved - setup transformation and clip
627             mpOutputCanvas->setTransformation( getTransformation() );
628             mpOutputCanvas->setClip(
629                 createClipPolygon( maClip,
630                                    mpOutputCanvas,
631                                    maUserSize ));
632         }
633 
634         return mpOutputCanvas;
635     }
636 
setClip(const basegfx::B2DPolyPolygon & rClip)637     virtual void setClip( const basegfx::B2DPolyPolygon& rClip )
638     {
639         basegfx::B2DPolyPolygon aNewClip = prepareClip( rClip );
640 
641         if( aNewClip != maClip )
642         {
643             maClip = aNewClip;
644 
645             if(mpOutputCanvas )
646                 mpOutputCanvas->setClip(
647                     createClipPolygon( maClip,
648                                        mpOutputCanvas,
649                                        maUserSize ));
650         }
651     }
652 
resize(const::basegfx::B2DRange & rArea)653     virtual bool resize( const ::basegfx::B2DRange& rArea )
654     {
655         const bool bRet( maLayerBounds != rArea );
656         maLayerBounds = rArea;
657         updateView( maTransformation,
658                     maUserSize );
659 
660         return bRet;
661     }
662 };
663 
664 
665 // ---------------------------------------------------------
666 
667 typedef cppu::WeakComponentImplHelper2<
668     ::com::sun::star::util::XModifyListener,
669       ::com::sun::star::awt::XPaintListener> SlideViewBase;
670 
671 /** SlideView class
672 
673     This class implements the View interface, encapsulating
674     <em>one</em> view a slideshow is displayed on.
675  */
676 class SlideView : private cppu::BaseMutex,
677                   public SlideViewBase,
678                   public UnoView
679 {
680 public:
681     SlideView( const uno::Reference<presentation::XSlideShowView>& xView,
682                EventQueue&                                         rEventQueue,
683                EventMultiplexer&                                   rEventMultiplexer );
684     void updateCanvas();
685 
686 private:
687     // View:
688     virtual ViewLayerSharedPtr createViewLayer( const basegfx::B2DRange& rLayerBounds ) const;
689     virtual bool updateScreen() const;
690     virtual bool paintScreen() const;
691     virtual void setViewSize( const ::basegfx::B2DSize& );
692     virtual void setCursorShape( sal_Int16 nPointerShape );
693 
694     // ViewLayer interface
695     virtual bool isOnView(boost::shared_ptr<View> const& rView) const;
696     virtual void clear() const;
697     virtual void clearAll() const;
698     virtual cppcanvas::CanvasSharedPtr getCanvas() const;
699     virtual cppcanvas::CustomSpriteSharedPtr createSprite( const ::basegfx::B2DSize& rSpriteSizePixel,
700                                                            double                    nPriority ) const;
701     virtual void setPriority( const basegfx::B1DRange& rRange );
702     virtual ::basegfx::B2DHomMatrix getTransformation() const;
703     virtual basegfx::B2DHomMatrix getSpriteTransformation() const;
704     virtual void setClip( const ::basegfx::B2DPolyPolygon& rClip );
705     virtual bool resize( const ::basegfx::B2DRange& rArea );
706 
707     // UnoView:
708     virtual void _dispose();
709     virtual uno::Reference<presentation::XSlideShowView> getUnoView()const;
710     virtual void setIsSoundEnabled (const bool bValue);
711     virtual bool isSoundEnabled (void) const;
712 
713     // XEventListener:
714     virtual void SAL_CALL disposing( lang::EventObject const& evt );
715     // XModifyListener:
716     virtual void SAL_CALL modified( const lang::EventObject& aEvent );
717     // XPaintListener:
718     virtual void SAL_CALL windowPaint( const awt::PaintEvent& e );
719 
720     // WeakComponentImplHelperBase:
721     virtual void SAL_CALL disposing();
722 
723     void updateClip();
724 
725 private:
726     typedef std::vector< boost::weak_ptr<SlideViewLayer> > ViewLayerVector;
727 
728     // Prune viewlayers from deceased ones, optionally update them
729     void pruneLayers( bool bWithViewLayerUpdate=false ) const;
730 
731     /** Max fill level of maViewLayers, before we try to prune it from
732         deceased layers
733     */
734     enum{ LAYER_ULLAGE=8 };
735 
736     uno::Reference<presentation::XSlideShowView>              mxView;
737     cppcanvas::SpriteCanvasSharedPtr                          mpCanvas;
738 
739     EventMultiplexer&                                         mrEventMultiplexer;
740     EventQueue&                                               mrEventQueue;
741 
742     mutable LayerSpriteContainer                              maSprites;
743     mutable ViewLayerVector                                   maViewLayers;
744 
745     basegfx::B2DPolyPolygon                                   maClip;
746 
747     basegfx::B2DHomMatrix                                     maViewTransform;
748     basegfx::B2DSize                                          maUserSize;
749     bool mbIsSoundEnabled;
750 };
751 
752 
SlideView(const uno::Reference<presentation::XSlideShowView> & xView,EventQueue & rEventQueue,EventMultiplexer & rEventMultiplexer)753 SlideView::SlideView( const uno::Reference<presentation::XSlideShowView>& xView,
754                       EventQueue&                                         rEventQueue,
755                       EventMultiplexer&                                   rEventMultiplexer ) :
756     SlideViewBase( m_aMutex ),
757     mxView( xView ),
758     mpCanvas(),
759     mrEventMultiplexer( rEventMultiplexer ),
760     mrEventQueue( rEventQueue ),
761     maSprites(),
762     maViewLayers(),
763     maClip(),
764     maViewTransform(),
765     maUserSize( 1.0, 1.0 ), // default size: one-by-one rectangle
766     mbIsSoundEnabled(true)
767 {
768     // take care not constructing any UNO references to this _inside_
769     // ctor, shift that code to createSlideView()!
770     ENSURE_OR_THROW( mxView.is(),
771                       "SlideView::SlideView(): Invalid view" );
772 
773     mpCanvas = cppcanvas::VCLFactory::getInstance().createSpriteCanvas(
774         xView->getCanvas() );
775     ENSURE_OR_THROW( mpCanvas,
776                       "Could not create cppcanvas" );
777 
778     geometry::AffineMatrix2D aViewTransform(
779         xView->getTransformation() );
780 
781     if( basegfx::fTools::equalZero(
782             basegfx::B2DVector(aViewTransform.m00,
783                                aViewTransform.m10).getLength()) ||
784         basegfx::fTools::equalZero(
785             basegfx::B2DVector(aViewTransform.m01,
786                                aViewTransform.m11).getLength()) )
787     {
788         OSL_ENSURE( false,
789                     "SlideView::SlideView(): Singular matrix!" );
790 
791         canvas::tools::setIdentityAffineMatrix2D(aViewTransform);
792     }
793 
794     basegfx::unotools::homMatrixFromAffineMatrix(
795         maViewTransform, aViewTransform );
796 
797     // once and forever: set fixed prio to this 'layer' (we're always
798     // the background layer)
799     maSprites.setLayerPriority( basegfx::B1DRange(0.0,1.0) );
800 }
801 
disposing()802 void SlideView::disposing()
803 {
804     osl::MutexGuard aGuard( m_aMutex );
805 
806     maViewLayers.clear();
807     maSprites.clear();
808     mpCanvas.reset();
809 
810     // additionally, also de-register from XSlideShowView
811     if (mxView.is())
812     {
813         mxView->removeTransformationChangedListener( this );
814         mxView->removePaintListener( this );
815         mxView.clear();
816     }
817 }
818 
createViewLayer(const basegfx::B2DRange & rLayerBounds) const819 ViewLayerSharedPtr SlideView::createViewLayer( const basegfx::B2DRange& rLayerBounds ) const
820 {
821     osl::MutexGuard aGuard( m_aMutex );
822 
823     ENSURE_OR_THROW( mpCanvas,
824                       "SlideView::createViewLayer(): Disposed" );
825 
826     const std::size_t nNumLayers( maViewLayers.size() );
827 
828     // avoid filling up layer vector with lots of deceased layer weak
829     // ptrs
830     if( nNumLayers > LAYER_ULLAGE )
831         pruneLayers();
832 
833     boost::shared_ptr<SlideViewLayer> pViewLayer( new SlideViewLayer(mpCanvas,
834                                                                      getTransformation(),
835                                                                      rLayerBounds,
836                                                                      maUserSize,
837                                                                      this) );
838     maViewLayers.push_back( pViewLayer );
839 
840     return pViewLayer;
841 }
842 
updateScreen() const843 bool SlideView::updateScreen() const
844 {
845     osl::MutexGuard aGuard( m_aMutex );
846 
847     ENSURE_OR_RETURN_FALSE( mpCanvas.get(),
848                        "SlideView::updateScreen(): Disposed" );
849 
850     return mpCanvas->updateScreen( false );
851 }
852 
paintScreen() const853 bool SlideView::paintScreen() const
854 {
855     osl::MutexGuard aGuard( m_aMutex );
856 
857     ENSURE_OR_RETURN_FALSE( mpCanvas.get(),
858                        "SlideView::paintScreen(): Disposed" );
859 
860     return mpCanvas->updateScreen( true );
861 }
862 
clear() const863 void SlideView::clear() const
864 {
865     osl::MutexGuard aGuard( m_aMutex );
866 
867     OSL_ENSURE( mxView.is() && mpCanvas,
868                 "SlideView::clear(): Disposed" );
869     if( !mxView.is() || !mpCanvas )
870         return;
871 
872     // keep layer clip
873     clearRect(getCanvas()->clone(),
874               getLayerBoundsPixel(
875                   basegfx::B2DRange(0,0,
876                                     maUserSize.getX(),
877                                     maUserSize.getY()),
878                   getTransformation()));
879 }
880 
clearAll() const881 void SlideView::clearAll() const
882 {
883     osl::MutexGuard aGuard( m_aMutex );
884 
885     OSL_ENSURE( mxView.is() && mpCanvas,
886                 "SlideView::clear(): Disposed" );
887     if( !mxView.is() || !mpCanvas )
888         return;
889 
890     // clear whole view
891     mxView->clear();
892 }
893 
setViewSize(const basegfx::B2DSize & rSize)894 void SlideView::setViewSize( const basegfx::B2DSize& rSize )
895 {
896     osl::MutexGuard aGuard( m_aMutex );
897 
898     maUserSize = rSize;
899     updateCanvas();
900 }
901 
setCursorShape(sal_Int16 nPointerShape)902 void SlideView::setCursorShape( sal_Int16 nPointerShape )
903 {
904     osl::MutexGuard const guard( m_aMutex );
905 
906     if (mxView.is())
907         mxView->setMouseCursor( nPointerShape );
908 }
909 
isOnView(boost::shared_ptr<View> const & rView) const910 bool SlideView::isOnView(boost::shared_ptr<View> const& rView) const
911 {
912     return rView.get() == this;
913 }
914 
getCanvas() const915 cppcanvas::CanvasSharedPtr SlideView::getCanvas() const
916 {
917     osl::MutexGuard aGuard( m_aMutex );
918 
919     ENSURE_OR_THROW( mpCanvas,
920                       "SlideView::getCanvas(): Disposed" );
921 
922     return mpCanvas;
923 }
924 
createSprite(const basegfx::B2DSize & rSpriteSizePixel,double nPriority) const925 cppcanvas::CustomSpriteSharedPtr SlideView::createSprite(
926     const basegfx::B2DSize& rSpriteSizePixel,
927     double                  nPriority ) const
928 {
929     osl::MutexGuard aGuard( m_aMutex );
930 
931     ENSURE_OR_THROW( mpCanvas, "SlideView::createSprite(): Disposed" );
932 
933     cppcanvas::CustomSpriteSharedPtr pSprite(
934         mpCanvas->createCustomSprite( rSpriteSizePixel ) );
935 
936     maSprites.addSprite( pSprite,
937                          nPriority );
938 
939     return pSprite;
940 }
941 
setPriority(const basegfx::B1DRange &)942 void SlideView::setPriority( const basegfx::B1DRange& /*rRange*/ )
943 {
944     osl::MutexGuard aGuard( m_aMutex );
945 
946     OSL_ENSURE( false,
947                 "SlideView::setPriority() is a NOOP for slide view - "
948                 "content will always be shown in the background" );
949 }
950 
getTransformation() const951 basegfx::B2DHomMatrix SlideView::getTransformation() const
952 {
953     osl::MutexGuard aGuard( m_aMutex );
954 
955     basegfx::B2DHomMatrix aMatrix;
956     aMatrix.scale( 1.0/maUserSize.getX(), 1.0/maUserSize.getY() );
957 
958     return maViewTransform * aMatrix;
959 }
960 
getSpriteTransformation() const961 basegfx::B2DHomMatrix SlideView::getSpriteTransformation() const
962 {
963     return getTransformation();
964 }
965 
setClip(const basegfx::B2DPolyPolygon & rClip)966 void SlideView::setClip( const basegfx::B2DPolyPolygon& rClip )
967 {
968     osl::MutexGuard aGuard( m_aMutex );
969 
970     basegfx::B2DPolyPolygon aNewClip = prepareClip( rClip );
971 
972     if( aNewClip != maClip )
973     {
974         maClip = aNewClip;
975 
976         updateClip();
977     }
978 }
979 
resize(const::basegfx::B2DRange &)980 bool SlideView::resize( const ::basegfx::B2DRange& /*rArea*/ )
981 {
982     osl::MutexGuard aGuard( m_aMutex );
983 
984     OSL_ENSURE( false,
985                 "SlideView::resize(): ignored for the View, can't change size "
986                 "effectively, anyway" );
987 
988     return false;
989 }
990 
getUnoView() const991 uno::Reference<presentation::XSlideShowView> SlideView::getUnoView() const
992 {
993     osl::MutexGuard aGuard( m_aMutex );
994     return mxView;
995 }
996 
setIsSoundEnabled(const bool bValue)997 void SlideView::setIsSoundEnabled (const bool bValue)
998 {
999     mbIsSoundEnabled = bValue;
1000 }
1001 
isSoundEnabled(void) const1002 bool SlideView::isSoundEnabled (void) const
1003 {
1004     return mbIsSoundEnabled;
1005 }
1006 
_dispose()1007 void SlideView::_dispose()
1008 {
1009     dispose();
1010 }
1011 
1012 // XEventListener
disposing(lang::EventObject const & evt)1013 void SlideView::disposing( lang::EventObject const& evt )
1014 {
1015     (void)evt;
1016 
1017     // no deregistration necessary anymore, XView has left:
1018     osl::MutexGuard const guard( m_aMutex );
1019 
1020     if (mxView.is())
1021     {
1022         OSL_ASSERT( evt.Source == mxView );
1023         mxView.clear();
1024     }
1025 
1026     dispose();
1027 }
1028 
1029 // XModifyListener
modified(const lang::EventObject &)1030 void SlideView::modified( const lang::EventObject& /*aEvent*/ )
1031 {
1032     osl::MutexGuard const guard( m_aMutex );
1033 
1034     OSL_ENSURE( mxView.is(), "SlideView::modified(): "
1035                 "Disposed, but event received from XSlideShowView?!");
1036 
1037     if( !mxView.is() )
1038         return;
1039 
1040     geometry::AffineMatrix2D aViewTransform(
1041         mxView->getTransformation() );
1042 
1043     if( basegfx::fTools::equalZero(
1044             basegfx::B2DVector(aViewTransform.m00,
1045                                aViewTransform.m10).getLength()) ||
1046         basegfx::fTools::equalZero(
1047             basegfx::B2DVector(aViewTransform.m01,
1048                                aViewTransform.m11).getLength()) )
1049     {
1050         OSL_ENSURE( false,
1051                     "SlideView::modified(): Singular matrix!" );
1052 
1053         canvas::tools::setIdentityAffineMatrix2D(aViewTransform);
1054     }
1055 
1056     // view transformation really changed?
1057     basegfx::B2DHomMatrix aNewTransform;
1058     basegfx::unotools::homMatrixFromAffineMatrix(
1059         aNewTransform,
1060         aViewTransform );
1061 
1062     if( aNewTransform == maViewTransform )
1063         return; // No change, nothing to do
1064 
1065     maViewTransform = aNewTransform;
1066 
1067     updateCanvas();
1068 
1069     // notify view change. Don't call EventMultiplexer directly, this
1070     // might not be the main thread!
1071     mrEventQueue.addEvent(
1072         makeEvent( boost::bind( (bool (EventMultiplexer::*)(
1073                                      const uno::Reference<presentation::XSlideShowView>&))
1074                                 &EventMultiplexer::notifyViewChanged,
1075                                 boost::ref(mrEventMultiplexer), mxView ),
1076                    "EventMultiplexer::notifyViewChanged"));
1077 }
1078 
1079 // XPaintListener
windowPaint(const awt::PaintEvent &)1080 void SlideView::windowPaint( const awt::PaintEvent& /*e*/ )
1081 {
1082     osl::MutexGuard aGuard( m_aMutex );
1083 
1084     OSL_ENSURE( mxView.is() && mpCanvas, "Disposed, but event received?!" );
1085 
1086     // notify view clobbering. Don't call EventMultiplexer directly,
1087     // this might not be the main thread!
1088     mrEventQueue.addEvent(
1089         makeEvent( boost::bind( &EventMultiplexer::notifyViewClobbered,
1090                                 boost::ref(mrEventMultiplexer), mxView ),
1091                    "EventMultiplexer::notifyViewClobbered") );
1092 }
1093 
updateCanvas()1094 void SlideView::updateCanvas()
1095 {
1096     OSL_ENSURE( mpCanvas,
1097                 "SlideView::updateCanvasTransform(): Disposed" );
1098 
1099     if( !mpCanvas || !mxView.is())
1100         return;
1101 
1102     mpCanvas->clear(); // this is unnecessary, strictly speaking. but
1103                        // it makes the SlideView behave exactly like a
1104                        // sprite-based SlideViewLayer, because those
1105                        // are created from scratch after a resize
1106     clearAll();
1107     mpCanvas->setTransformation( getTransformation() );
1108     mpCanvas->setClip(
1109         createClipPolygon( maClip,
1110                            mpCanvas,
1111                            maUserSize ));
1112 
1113     // forward update to viewlayers
1114     pruneLayers( true );
1115 }
1116 
updateClip()1117 void SlideView::updateClip()
1118 {
1119     OSL_ENSURE( mpCanvas,
1120                 "SlideView::updateClip(): Disposed" );
1121 
1122     if( !mpCanvas )
1123         return;
1124 
1125     mpCanvas->setClip(
1126         createClipPolygon( maClip,
1127                            mpCanvas,
1128                            maUserSize ));
1129 
1130     pruneLayers( false );
1131 }
1132 
pruneLayers(bool bWithViewLayerUpdate) const1133 void SlideView::pruneLayers( bool bWithViewLayerUpdate ) const
1134 {
1135     ViewLayerVector aValidLayers;
1136 
1137     const basegfx::B2DHomMatrix& rCurrTransform(
1138         getTransformation() );
1139 
1140     // check all layers for validity, and retain only the live ones
1141     ViewLayerVector::const_iterator       aCurr( maViewLayers.begin() );
1142     const ViewLayerVector::const_iterator aEnd( maViewLayers.end() );
1143     while( aCurr != aEnd )
1144     {
1145         boost::shared_ptr< SlideViewLayer > pCurrLayer( aCurr->lock() );
1146 
1147         if( pCurrLayer )
1148         {
1149             aValidLayers.push_back( pCurrLayer );
1150 
1151             if( bWithViewLayerUpdate )
1152                 pCurrLayer->updateView( rCurrTransform,
1153                                         maUserSize );
1154         }
1155 
1156         ++aCurr;
1157     }
1158 
1159     // replace layer list with pruned one
1160     maViewLayers.swap( aValidLayers );
1161 }
1162 
1163 } // anonymous namespace
1164 
createSlideView(uno::Reference<presentation::XSlideShowView> const & xView,EventQueue & rEventQueue,EventMultiplexer & rEventMultiplexer)1165 UnoViewSharedPtr createSlideView( uno::Reference< presentation::XSlideShowView> const& xView,
1166                                   EventQueue&                                          rEventQueue,
1167                                   EventMultiplexer&                                    rEventMultiplexer )
1168 {
1169     boost::shared_ptr<SlideView> const that(
1170         comphelper::make_shared_from_UNO(
1171             new SlideView(xView,
1172                           rEventQueue,
1173                           rEventMultiplexer)));
1174 
1175     // register listeners with XSlideShowView
1176     xView->addTransformationChangedListener( that.get() );
1177     xView->addPaintListener( that.get() );
1178 
1179     // set new transformation
1180     that->updateCanvas();
1181 
1182     return that;
1183 }
1184 
1185 } // namespace internal
1186 } // namespace slideshow
1187