1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
33*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
34*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <vcl/canvastools.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <comphelper/scopeguard.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <basegfx/range/b2drectangle.hxx>
41*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <boost/cast.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include "cairo_spritecanvashelper.hxx"
46*cdf0e10cSrcweir #include "cairo_canvascustomsprite.hxx"
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace ::cairo;
49*cdf0e10cSrcweir using namespace ::com::sun::star;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir namespace cairocanvas
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir     namespace
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         /** Sprite redraw at original position
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir             Used to repaint the whole canvas (background and all
58*cdf0e10cSrcweir             sprites)
59*cdf0e10cSrcweir          */
60*cdf0e10cSrcweir         void spriteRedraw( const CairoSharedPtr& pCairo,
61*cdf0e10cSrcweir                            const ::canvas::Sprite::Reference& rSprite )
62*cdf0e10cSrcweir         {
63*cdf0e10cSrcweir             // downcast to derived cairocanvas::Sprite interface, which
64*cdf0e10cSrcweir             // provides the actual redraw methods.
65*cdf0e10cSrcweir             ::boost::polymorphic_downcast< Sprite* >(rSprite.get())->redraw( pCairo, true);
66*cdf0e10cSrcweir         }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         void repaintBackground( const CairoSharedPtr&      pCairo,
69*cdf0e10cSrcweir                                 const SurfaceSharedPtr&    pBackgroundSurface,
70*cdf0e10cSrcweir                                 const ::basegfx::B2DRange& rArea )
71*cdf0e10cSrcweir         {
72*cdf0e10cSrcweir             cairo_save( pCairo.get() );
73*cdf0e10cSrcweir             cairo_rectangle( pCairo.get(), ceil( rArea.getMinX() ), ceil( rArea.getMinY() ),
74*cdf0e10cSrcweir                              floor( rArea.getWidth() ), floor( rArea.getHeight() ) );
75*cdf0e10cSrcweir             cairo_clip( pCairo.get() );
76*cdf0e10cSrcweir             cairo_set_source_surface( pCairo.get(), pBackgroundSurface->getCairoSurface().get(), 0, 0 );
77*cdf0e10cSrcweir             cairo_set_operator( pCairo.get(), CAIRO_OPERATOR_SOURCE );
78*cdf0e10cSrcweir             cairo_paint( pCairo.get() );
79*cdf0e10cSrcweir             cairo_restore( pCairo.get() );
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir         void opaqueUpdateSpriteArea( const ::canvas::Sprite::Reference& rSprite,
83*cdf0e10cSrcweir                                      const CairoSharedPtr&              pCairo,
84*cdf0e10cSrcweir                                      const ::basegfx::B2IRange&         rArea )
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             // clip output to actual update region (otherwise a)
87*cdf0e10cSrcweir             // wouldn't save much render time, and b) will clutter
88*cdf0e10cSrcweir             // scrolled sprite content outside this area)
89*cdf0e10cSrcweir             cairo_save( pCairo.get() );
90*cdf0e10cSrcweir             cairo_rectangle( pCairo.get(), rArea.getMinX(), rArea.getMinY(),
91*cdf0e10cSrcweir                              sal::static_int_cast<sal_Int32>(rArea.getWidth()),
92*cdf0e10cSrcweir                              sal::static_int_cast<sal_Int32>(rArea.getHeight()) );
93*cdf0e10cSrcweir             cairo_clip( pCairo.get() );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir             // repaint affected sprite directly to output device (at
96*cdf0e10cSrcweir             // the actual screen output position)
97*cdf0e10cSrcweir             // rendering directly to device buffer
98*cdf0e10cSrcweir             ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir             cairo_restore( pCairo.get() );
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         /** Repaint sprite at original position
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir             Used for opaque updates, which render directly to the
106*cdf0e10cSrcweir             device buffer.
107*cdf0e10cSrcweir          */
108*cdf0e10cSrcweir         void spriteRedrawStub( const CairoSharedPtr& pCairo,
109*cdf0e10cSrcweir                                const ::canvas::Sprite::Reference& rSprite )
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir             if( rSprite.is() )
112*cdf0e10cSrcweir             {
113*cdf0e10cSrcweir                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
114*cdf0e10cSrcweir             }
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         /** Repaint sprite at given position
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir             Used for generic update, which renders into device buffer.
120*cdf0e10cSrcweir          */
121*cdf0e10cSrcweir         void spriteRedrawStub2( const CairoSharedPtr& pCairo,
122*cdf0e10cSrcweir                                 const ::canvas::Sprite::Reference&	rSprite )
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             if( rSprite.is() )
125*cdf0e10cSrcweir             {
126*cdf0e10cSrcweir                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, true );;
127*cdf0e10cSrcweir             }
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         /** Repaint sprite at original position
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir             Used for opaque updates from scrollUpdate(), which render
133*cdf0e10cSrcweir             directly to the front buffer.
134*cdf0e10cSrcweir          */
135*cdf0e10cSrcweir         void spriteRedrawStub3( const CairoSharedPtr& pCairo,
136*cdf0e10cSrcweir                                 const ::canvas::SpriteRedrawManager::AreaComponent& rComponent )
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             const ::canvas::Sprite::Reference& rSprite( rComponent.second.getSprite() );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             if( rSprite.is() )
141*cdf0e10cSrcweir                 ::boost::polymorphic_downcast< Sprite* >( rSprite.get() )->redraw( pCairo, false );
142*cdf0e10cSrcweir         }
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     SpriteCanvasHelper::SpriteCanvasHelper() :
146*cdf0e10cSrcweir         mpRedrawManager( NULL ),
147*cdf0e10cSrcweir         mpOwningSpriteCanvas( NULL ),
148*cdf0e10cSrcweir         mpCompositingSurface(),
149*cdf0e10cSrcweir         maCompositingSurfaceSize()
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     void SpriteCanvasHelper::init( ::canvas::SpriteRedrawManager& rManager,
154*cdf0e10cSrcweir                                    SpriteCanvas&                  rDevice,
155*cdf0e10cSrcweir                                    const ::basegfx::B2ISize&      rSize )
156*cdf0e10cSrcweir     {
157*cdf0e10cSrcweir         mpRedrawManager = &rManager;
158*cdf0e10cSrcweir         mpOwningSpriteCanvas = &rDevice;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         CanvasHelper::init( rSize, rDevice, &rDevice );
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     void SpriteCanvasHelper::disposing()
164*cdf0e10cSrcweir     {
165*cdf0e10cSrcweir         mpCompositingSurface.reset();
166*cdf0e10cSrcweir         mpOwningSpriteCanvas = NULL;
167*cdf0e10cSrcweir         mpRedrawManager = NULL;
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         // forward to base
170*cdf0e10cSrcweir         CanvasHelper::disposing();
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromAnimation(
174*cdf0e10cSrcweir         const uno::Reference< rendering::XAnimation >& )
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         return uno::Reference< rendering::XAnimatedSprite >();
177*cdf0e10cSrcweir     }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     uno::Reference< rendering::XAnimatedSprite > SpriteCanvasHelper::createSpriteFromBitmaps(
180*cdf0e10cSrcweir         const uno::Sequence< uno::Reference< rendering::XBitmap > >& /*animationBitmaps*/,
181*cdf0e10cSrcweir         sal_Int8                                                     /*interpolationMode*/ )
182*cdf0e10cSrcweir     {
183*cdf0e10cSrcweir         return uno::Reference< rendering::XAnimatedSprite >();
184*cdf0e10cSrcweir     }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     uno::Reference< rendering::XCustomSprite > SpriteCanvasHelper::createCustomSprite( const geometry::RealSize2D& spriteSize )
187*cdf0e10cSrcweir     {
188*cdf0e10cSrcweir         if( !mpRedrawManager )
189*cdf0e10cSrcweir             return uno::Reference< rendering::XCustomSprite >(); // we're disposed
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir         return uno::Reference< rendering::XCustomSprite >(
192*cdf0e10cSrcweir             new CanvasCustomSprite( spriteSize,
193*cdf0e10cSrcweir                                     mpOwningSpriteCanvas ) );
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     uno::Reference< rendering::XSprite > SpriteCanvasHelper::createClonedSprite(
197*cdf0e10cSrcweir         const uno::Reference< rendering::XSprite >& )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         return uno::Reference< rendering::XSprite >();
200*cdf0e10cSrcweir     }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir     sal_Bool SpriteCanvasHelper::updateScreen( const ::basegfx::B2IRange& /*rCurrArea*/,
203*cdf0e10cSrcweir                                                sal_Bool                   bUpdateAll,
204*cdf0e10cSrcweir                                                bool&                      io_bSurfaceDirty )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         if( !mpRedrawManager ||
207*cdf0e10cSrcweir             !mpOwningSpriteCanvas ||
208*cdf0e10cSrcweir             !mpOwningSpriteCanvas->getWindowSurface() ||
209*cdf0e10cSrcweir             !mpOwningSpriteCanvas->getBufferSurface() )
210*cdf0e10cSrcweir         {
211*cdf0e10cSrcweir             return sal_False; // disposed, or otherwise dysfunctional
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir         OSL_TRACE("SpriteCanvasHelper::updateScreen called");
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir         // force compositing surface to be available before using it
219*cdf0e10cSrcweir         // inside forEachSpriteArea
220*cdf0e10cSrcweir         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
221*cdf0e10cSrcweir         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
222*cdf0e10cSrcweir         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
223*cdf0e10cSrcweir         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir         // TODO(P1): Might be worthwile to track areas of background
226*cdf0e10cSrcweir         // changes, too.
227*cdf0e10cSrcweir         if( !bUpdateAll && !io_bSurfaceDirty )
228*cdf0e10cSrcweir         {
229*cdf0e10cSrcweir             // background has not changed, so we're free to optimize
230*cdf0e10cSrcweir             // repaint to areas where a sprite has changed
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir             // process each independent area of overlapping sprites
233*cdf0e10cSrcweir             // separately.
234*cdf0e10cSrcweir             mpRedrawManager->forEachSpriteArea( *this );
235*cdf0e10cSrcweir         }
236*cdf0e10cSrcweir         else
237*cdf0e10cSrcweir         {
238*cdf0e10cSrcweir             OSL_TRACE("SpriteCanvasHelper::updateScreen update ALL");
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir             // background has changed, so we currently have no choice
241*cdf0e10cSrcweir             // but repaint everything (or caller requested that)
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir             cairo_rectangle( pCompositingCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
244*cdf0e10cSrcweir             cairo_clip( pCompositingCairo.get() );
245*cdf0e10cSrcweir             cairo_save( pCompositingCairo.get() );
246*cdf0e10cSrcweir             cairo_set_source_surface( pCompositingCairo.get(),
247*cdf0e10cSrcweir                                       mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
248*cdf0e10cSrcweir                                       0, 0 );
249*cdf0e10cSrcweir             cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
250*cdf0e10cSrcweir             cairo_paint( pCompositingCairo.get() );
251*cdf0e10cSrcweir             cairo_restore( pCompositingCairo.get() );
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir             // repaint all active sprites on top of background into
254*cdf0e10cSrcweir             // VDev.
255*cdf0e10cSrcweir             mpRedrawManager->forEachSprite(
256*cdf0e10cSrcweir                 ::boost::bind(
257*cdf0e10cSrcweir                     &spriteRedraw,
258*cdf0e10cSrcweir                     boost::cref(pCompositingCairo),
259*cdf0e10cSrcweir                     _1 ) );
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir             // flush to screen
262*cdf0e10cSrcweir             cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
263*cdf0e10cSrcweir             cairo_clip( pWindowCairo.get() );
264*cdf0e10cSrcweir             cairo_set_source_surface( pWindowCairo.get(),
265*cdf0e10cSrcweir                                       pCompositingSurface->getCairoSurface().get(),
266*cdf0e10cSrcweir                                       0, 0 );
267*cdf0e10cSrcweir             cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
268*cdf0e10cSrcweir             cairo_paint( pWindowCairo.get() );
269*cdf0e10cSrcweir         }
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir         // change record vector must be cleared, for the next turn of
272*cdf0e10cSrcweir         // rendering and sprite changing
273*cdf0e10cSrcweir         mpRedrawManager->clearChangeRecords();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir         io_bSurfaceDirty = false;
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir         // commit to screen
278*cdf0e10cSrcweir         mpOwningSpriteCanvas->flush();
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir         return sal_True;
281*cdf0e10cSrcweir     }
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir     void SpriteCanvasHelper::backgroundPaint( const ::basegfx::B2DRange& rUpdateRect )
284*cdf0e10cSrcweir     {
285*cdf0e10cSrcweir         if( mpOwningSpriteCanvas && mpCompositingSurface )
286*cdf0e10cSrcweir             repaintBackground( mpCompositingSurface->getCairo(),
287*cdf0e10cSrcweir                                mpOwningSpriteCanvas->getBufferSurface(),
288*cdf0e10cSrcweir                                rUpdateRect );
289*cdf0e10cSrcweir     }
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir     void SpriteCanvasHelper::scrollUpdate( const ::basegfx::B2DRange& 						rMoveStart,
292*cdf0e10cSrcweir                                            const ::basegfx::B2DRange& 						rMoveEnd,
293*cdf0e10cSrcweir                                            const ::canvas::SpriteRedrawManager::UpdateArea& rUpdateArea )
294*cdf0e10cSrcweir     {
295*cdf0e10cSrcweir         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
296*cdf0e10cSrcweir                           mpOwningSpriteCanvas->getBufferSurface(),
297*cdf0e10cSrcweir                           "SpriteCanvasHelper::scrollUpdate(): NULL device pointer " );
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir         OSL_TRACE("SpriteCanvasHelper::scrollUpdate called");
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
302*cdf0e10cSrcweir         const ::basegfx::B2IRange  aOutputBounds( 0,0,
303*cdf0e10cSrcweir                                                   rSize.getX(),
304*cdf0e10cSrcweir                                                   rSize.getY() );
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
307*cdf0e10cSrcweir         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
308*cdf0e10cSrcweir         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
309*cdf0e10cSrcweir         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir         // round rectangles to integer pixel. Note: have to be
312*cdf0e10cSrcweir         // extremely careful here, to avoid off-by-one errors for
313*cdf0e10cSrcweir         // the destination area: otherwise, the next scroll update
314*cdf0e10cSrcweir         // would copy pixel that are not supposed to be part of
315*cdf0e10cSrcweir         // the sprite.
316*cdf0e10cSrcweir         ::basegfx::B2IRange aSourceRect(
317*cdf0e10cSrcweir             ::canvas::tools::spritePixelAreaFromB2DRange( rMoveStart ) );
318*cdf0e10cSrcweir         const ::basegfx::B2IRange& rDestRect(
319*cdf0e10cSrcweir             ::canvas::tools::spritePixelAreaFromB2DRange( rMoveEnd ) );
320*cdf0e10cSrcweir         ::basegfx::B2IPoint aDestPos( rDestRect.getMinimum() );
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir         ::std::vector< ::basegfx::B2IRange > aUnscrollableAreas;
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir         // TODO(E3): This is plain buggy (but copies the behaviour of
325*cdf0e10cSrcweir         // the old Impress slideshow) - the scrolled area might
326*cdf0e10cSrcweir         // actually lie _below_ another window!
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir         // clip to output bounds (cannot properly scroll stuff
329*cdf0e10cSrcweir         // _outside_ our screen area)
330*cdf0e10cSrcweir         if( !::canvas::tools::clipScrollArea( aSourceRect,
331*cdf0e10cSrcweir                                               aDestPos,
332*cdf0e10cSrcweir                                               aUnscrollableAreas,
333*cdf0e10cSrcweir                                               aOutputBounds ) )
334*cdf0e10cSrcweir         {
335*cdf0e10cSrcweir             // fully clipped scroll area: cannot simply scroll
336*cdf0e10cSrcweir             // then. Perform normal opaque update (can use that, since
337*cdf0e10cSrcweir             // one of the preconditions for scrollable update is
338*cdf0e10cSrcweir             // opaque sprite content)
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir             // repaint all affected sprites directly to output device
341*cdf0e10cSrcweir             ::std::for_each( rUpdateArea.maComponentList.begin(),
342*cdf0e10cSrcweir                              rUpdateArea.maComponentList.end(),
343*cdf0e10cSrcweir                              ::boost::bind(
344*cdf0e10cSrcweir                                  &spriteRedrawStub3,
345*cdf0e10cSrcweir                                  boost::cref(pCompositingCairo),
346*cdf0e10cSrcweir                                  _1 ) );
347*cdf0e10cSrcweir         }
348*cdf0e10cSrcweir         else
349*cdf0e10cSrcweir         {
350*cdf0e10cSrcweir             const ::basegfx::B2IVector aSourceUpperLeftPos( aSourceRect.getMinimum() );
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir             // clip dest area (which must be inside rDestBounds)
353*cdf0e10cSrcweir             ::basegfx::B2IRange aDestRect( rDestRect );
354*cdf0e10cSrcweir             aDestRect.intersect( aOutputBounds );
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir             cairo_save( pCompositingCairo.get() );
357*cdf0e10cSrcweir             // scroll content in device back buffer
358*cdf0e10cSrcweir             cairo_set_source_surface( pCompositingCairo.get(),
359*cdf0e10cSrcweir                                       mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
360*cdf0e10cSrcweir                                       aDestPos.getX() - aSourceUpperLeftPos.getX(),
361*cdf0e10cSrcweir                                       aDestPos.getY() - aSourceUpperLeftPos.getY() );
362*cdf0e10cSrcweir             cairo_rectangle( pCompositingCairo.get(),
363*cdf0e10cSrcweir                              aDestPos.getX(), aDestPos.getY(),
364*cdf0e10cSrcweir                              sal::static_int_cast<sal_Int32>(aDestRect.getWidth()),
365*cdf0e10cSrcweir                              sal::static_int_cast<sal_Int32>(aDestRect.getHeight()) );
366*cdf0e10cSrcweir             cairo_clip( pCompositingCairo.get() );
367*cdf0e10cSrcweir             cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
368*cdf0e10cSrcweir             cairo_paint( pCompositingCairo.get() );
369*cdf0e10cSrcweir             cairo_restore( pCompositingCairo.get() );
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir             const ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
372*cdf0e10cSrcweir                 aFirst( rUpdateArea.maComponentList.begin() );
373*cdf0e10cSrcweir             ::canvas::SpriteRedrawManager::SpriteConnectedRanges::ComponentListType::const_iterator
374*cdf0e10cSrcweir                   aSecond( aFirst ); ++aSecond;
375*cdf0e10cSrcweir 
376*cdf0e10cSrcweir             ENSURE_OR_THROW( aFirst->second.getSprite().is(),
377*cdf0e10cSrcweir                              "VCLCanvas::scrollUpdate(): no sprite" );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir             // repaint uncovered areas from sprite. Need to actually
380*cdf0e10cSrcweir             // clip here, since we're only repainting _parts_ of the
381*cdf0e10cSrcweir             // sprite
382*cdf0e10cSrcweir             ::std::for_each( aUnscrollableAreas.begin(),
383*cdf0e10cSrcweir                              aUnscrollableAreas.end(),
384*cdf0e10cSrcweir                              ::boost::bind( &opaqueUpdateSpriteArea,
385*cdf0e10cSrcweir                                             ::boost::cref(aFirst->second.getSprite()),
386*cdf0e10cSrcweir                                             boost::cref(pCompositingCairo),
387*cdf0e10cSrcweir                                             _1 ) );
388*cdf0e10cSrcweir         }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir         // repaint uncovered areas from backbuffer - take the
391*cdf0e10cSrcweir         // _rounded_ rectangles from above, to have the update
392*cdf0e10cSrcweir         // consistent with the scroll above.
393*cdf0e10cSrcweir         ::std::vector< ::basegfx::B2DRange > aUncoveredAreas;
394*cdf0e10cSrcweir         ::basegfx::computeSetDifference( aUncoveredAreas,
395*cdf0e10cSrcweir                                          rUpdateArea.maTotalBounds,
396*cdf0e10cSrcweir                                          ::basegfx::B2DRange( rDestRect ) );
397*cdf0e10cSrcweir         ::std::for_each( aUncoveredAreas.begin(),
398*cdf0e10cSrcweir                          aUncoveredAreas.end(),
399*cdf0e10cSrcweir                          ::boost::bind( &repaintBackground,
400*cdf0e10cSrcweir                                         boost::cref(pCompositingCairo),
401*cdf0e10cSrcweir                                         boost::cref(mpOwningSpriteCanvas->getBufferSurface()),
402*cdf0e10cSrcweir                                         _1 ) );
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir         cairo_rectangle( pWindowCairo.get(), 0, 0, rSize.getX(), rSize.getY() );
405*cdf0e10cSrcweir         cairo_clip( pWindowCairo.get() );
406*cdf0e10cSrcweir         cairo_set_source_surface( pWindowCairo.get(),
407*cdf0e10cSrcweir                                   pCompositingSurface->getCairoSurface().get(),
408*cdf0e10cSrcweir                                   0, 0 );
409*cdf0e10cSrcweir         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
410*cdf0e10cSrcweir         cairo_paint( pWindowCairo.get() );
411*cdf0e10cSrcweir     }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir     void SpriteCanvasHelper::opaqueUpdate( const ::basegfx::B2DRange&                          rTotalArea,
414*cdf0e10cSrcweir                                            const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
415*cdf0e10cSrcweir     {
416*cdf0e10cSrcweir         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
417*cdf0e10cSrcweir                           mpOwningSpriteCanvas->getBufferSurface(),
418*cdf0e10cSrcweir                           "SpriteCanvasHelper::opaqueUpdate(): NULL device pointer " );
419*cdf0e10cSrcweir 
420*cdf0e10cSrcweir         OSL_TRACE("SpriteCanvasHelper::opaqueUpdate called");
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir         const ::basegfx::B2ISize& rDeviceSize = mpOwningSpriteCanvas->getSizePixel();
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rDeviceSize);
425*cdf0e10cSrcweir         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
426*cdf0e10cSrcweir         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
427*cdf0e10cSrcweir         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir         cairo_rectangle( pCompositingCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
430*cdf0e10cSrcweir         cairo_clip( pCompositingCairo.get() );
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir         ::basegfx::B2DVector aPos( ceil( rTotalArea.getMinX() ), ceil( rTotalArea.getMinY() ) );
433*cdf0e10cSrcweir         ::basegfx::B2DVector aSize( floor( rTotalArea.getMaxX() - aPos.getX() ), floor( rTotalArea.getMaxY() - aPos.getY() ) );
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir         cairo_rectangle( pCompositingCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
436*cdf0e10cSrcweir         cairo_clip( pCompositingCairo.get() );
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir         // repaint all affected sprites directly to output device
439*cdf0e10cSrcweir         ::std::for_each( rSortedUpdateSprites.begin(),
440*cdf0e10cSrcweir                          rSortedUpdateSprites.end(),
441*cdf0e10cSrcweir                          ::boost::bind(
442*cdf0e10cSrcweir                              &spriteRedrawStub,
443*cdf0e10cSrcweir                              boost::cref(pCompositingCairo),
444*cdf0e10cSrcweir                              _1 ) );
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir         // flush to screen
447*cdf0e10cSrcweir         cairo_rectangle( pWindowCairo.get(), 0, 0, rDeviceSize.getX(), rDeviceSize.getY() );
448*cdf0e10cSrcweir         cairo_clip( pWindowCairo.get() );
449*cdf0e10cSrcweir         cairo_rectangle( pWindowCairo.get(), aPos.getX(), aPos.getY(), aSize.getX(), aSize.getY() );
450*cdf0e10cSrcweir         cairo_clip( pWindowCairo.get() );
451*cdf0e10cSrcweir         cairo_set_source_surface( pWindowCairo.get(),
452*cdf0e10cSrcweir                                   pCompositingSurface->getCairoSurface().get(),
453*cdf0e10cSrcweir                                   0, 0 );
454*cdf0e10cSrcweir         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
455*cdf0e10cSrcweir         cairo_paint( pWindowCairo.get() );
456*cdf0e10cSrcweir     }
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir     void SpriteCanvasHelper::genericUpdate( const ::basegfx::B2DRange&                          rRequestedArea,
459*cdf0e10cSrcweir                                             const ::std::vector< ::canvas::Sprite::Reference >& rSortedUpdateSprites )
460*cdf0e10cSrcweir     {
461*cdf0e10cSrcweir         // TODO
462*cdf0e10cSrcweir         OSL_TRACE("SpriteCanvasHelper::genericUpdate called");
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir         ENSURE_OR_THROW( mpOwningSpriteCanvas &&
465*cdf0e10cSrcweir                          mpOwningSpriteCanvas->getBufferSurface(),
466*cdf0e10cSrcweir                          "SpriteCanvasHelper::genericUpdate(): NULL device pointer " );
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir         // limit size of update VDev to target outdev's size
469*cdf0e10cSrcweir         const ::basegfx::B2ISize& rSize = mpOwningSpriteCanvas->getSizePixel();
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir         SurfaceSharedPtr pCompositingSurface = getCompositingSurface(rSize);
472*cdf0e10cSrcweir         SurfaceSharedPtr pWindowSurface = mpOwningSpriteCanvas->getWindowSurface();
473*cdf0e10cSrcweir         CairoSharedPtr pCompositingCairo = pCompositingSurface->getCairo();
474*cdf0e10cSrcweir         CairoSharedPtr pWindowCairo = pWindowSurface->getCairo();
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir         // round output position towards zero. Don't want to truncate
477*cdf0e10cSrcweir         // a fraction of a sprite pixel...  Clip position at origin,
478*cdf0e10cSrcweir         // otherwise, truncation of size below might leave visible
479*cdf0e10cSrcweir         // areas uncovered by VDev.
480*cdf0e10cSrcweir         const Point aOutputPosition(
481*cdf0e10cSrcweir             ::std::max( sal_Int32( 0 ),
482*cdf0e10cSrcweir                         static_cast< sal_Int32 >(rRequestedArea.getMinX()) ),
483*cdf0e10cSrcweir             ::std::max( sal_Int32( 0 ),
484*cdf0e10cSrcweir                         static_cast< sal_Int32 >(rRequestedArea.getMinY()) ) );
485*cdf0e10cSrcweir         // round output size towards +infty. Don't want to truncate a
486*cdf0e10cSrcweir         // fraction of a sprite pixel... Limit size of VDev to output
487*cdf0e10cSrcweir         // device's area.
488*cdf0e10cSrcweir         const Size  aOutputSize(
489*cdf0e10cSrcweir             ::std::min( rSize.getX(),
490*cdf0e10cSrcweir                         ::canvas::tools::roundUp( rRequestedArea.getMaxX() - aOutputPosition.X()) ),
491*cdf0e10cSrcweir             ::std::min( rSize.getY(),
492*cdf0e10cSrcweir                         ::canvas::tools::roundUp( rRequestedArea.getMaxY() - aOutputPosition.Y()) ) );
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir         cairo_rectangle( pCompositingCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
495*cdf0e10cSrcweir         cairo_clip( pCompositingCairo.get() );
496*cdf0e10cSrcweir 
497*cdf0e10cSrcweir         // paint background
498*cdf0e10cSrcweir         cairo_save( pCompositingCairo.get() );
499*cdf0e10cSrcweir         cairo_set_source_surface( pCompositingCairo.get(),
500*cdf0e10cSrcweir                                   mpOwningSpriteCanvas->getBufferSurface()->getCairoSurface().get(),
501*cdf0e10cSrcweir                                   0, 0 );
502*cdf0e10cSrcweir         cairo_set_operator( pCompositingCairo.get(), CAIRO_OPERATOR_SOURCE );
503*cdf0e10cSrcweir         cairo_paint( pCompositingCairo.get() );
504*cdf0e10cSrcweir         cairo_restore( pCompositingCairo.get() );
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir         // repaint all affected sprites on top of background into
507*cdf0e10cSrcweir         // VDev.
508*cdf0e10cSrcweir         ::std::for_each( rSortedUpdateSprites.begin(),
509*cdf0e10cSrcweir                          rSortedUpdateSprites.end(),
510*cdf0e10cSrcweir                          ::boost::bind( &spriteRedrawStub2,
511*cdf0e10cSrcweir                                         boost::cref(pCompositingCairo),
512*cdf0e10cSrcweir                                         _1 ) );
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir         // flush to screen
515*cdf0e10cSrcweir         cairo_rectangle( pWindowCairo.get(), aOutputPosition.X(), aOutputPosition.Y(), aOutputSize.Width(), aOutputSize.Height() );
516*cdf0e10cSrcweir         cairo_clip( pWindowCairo.get() );
517*cdf0e10cSrcweir         cairo_set_source_surface( pWindowCairo.get(),
518*cdf0e10cSrcweir                                   pCompositingSurface->getCairoSurface().get(),
519*cdf0e10cSrcweir                                   0, 0 );
520*cdf0e10cSrcweir         cairo_set_operator( pWindowCairo.get(), CAIRO_OPERATOR_SOURCE );
521*cdf0e10cSrcweir         cairo_paint( pWindowCairo.get() );
522*cdf0e10cSrcweir     }
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir     ::cairo::SurfaceSharedPtr SpriteCanvasHelper::getCompositingSurface( const ::basegfx::B2ISize& rNeededSize )
525*cdf0e10cSrcweir     {
526*cdf0e10cSrcweir         if( rNeededSize.getX() < maCompositingSurfaceSize.getX() ||
527*cdf0e10cSrcweir             rNeededSize.getY() < maCompositingSurfaceSize.getY() )
528*cdf0e10cSrcweir         {
529*cdf0e10cSrcweir             // need to give buffer more size
530*cdf0e10cSrcweir             mpCompositingSurface.reset();
531*cdf0e10cSrcweir         }
532*cdf0e10cSrcweir 
533*cdf0e10cSrcweir         if( !mpCompositingSurface )
534*cdf0e10cSrcweir         {
535*cdf0e10cSrcweir             mpCompositingSurface =
536*cdf0e10cSrcweir                 mpOwningSpriteCanvas->getWindowSurface()->getSimilar(
537*cdf0e10cSrcweir                     CAIRO_CONTENT_COLOR,
538*cdf0e10cSrcweir                     rNeededSize.getX(), rNeededSize.getY() );
539*cdf0e10cSrcweir             maCompositingSurfaceSize = rNeededSize;
540*cdf0e10cSrcweir         }
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir         return mpCompositingSurface;
543*cdf0e10cSrcweir     }
544*cdf0e10cSrcweir }
545