Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <canvas/debug.hxx>
22 : #include <tools/diagnose_ex.h>
23 : #include <canvas/spriteredrawmanager.hxx>
24 :
25 : #include <basegfx/range/b2drectangle.hxx>
26 : #include <basegfx/tools/canvastools.hxx>
27 : #include <basegfx/vector/b2dsize.hxx>
28 :
29 : #include <algorithm>
30 : #include <o3tl/compat_functional.hxx>
31 : #include <boost/bind.hpp>
32 :
33 :
34 : namespace canvas
35 : {
36 : namespace
37 : {
38 : /** Helper class to condense sprite updates into a single action
39 :
40 : This class tracks the sprite changes over the recorded
41 : change list, and generates a single update action from
42 : that (note that per screen update, several moves,
43 : visibility changes and content updates might happen)
44 : */
45 0 : class SpriteTracer
46 : {
47 : public:
48 0 : SpriteTracer( const Sprite::Reference& rAffectedSprite ) :
49 : mpAffectedSprite(rAffectedSprite),
50 : maMoveStartArea(),
51 : maMoveEndArea(),
52 : mbIsMove( false ),
53 0 : mbIsGenericUpdate( false )
54 : {
55 0 : }
56 :
57 0 : void operator()( const SpriteRedrawManager::SpriteChangeRecord& rSpriteRecord )
58 : {
59 : // only deal with change events from the currently
60 : // affected sprite
61 0 : if( rSpriteRecord.mpAffectedSprite == mpAffectedSprite )
62 : {
63 0 : switch( rSpriteRecord.meChangeType )
64 : {
65 : case SpriteRedrawManager::SpriteChangeRecord::move:
66 0 : if( !mbIsMove )
67 : {
68 : // no move yet - this must be the first one
69 : maMoveStartArea = ::basegfx::B2DRectangle(
70 : rSpriteRecord.maOldPos,
71 0 : rSpriteRecord.maOldPos + rSpriteRecord.maUpdateArea.getRange() );
72 0 : mbIsMove = true;
73 : }
74 :
75 0 : maMoveEndArea = rSpriteRecord.maUpdateArea;
76 0 : break;
77 :
78 : case SpriteRedrawManager::SpriteChangeRecord::update:
79 : // update end update area of the
80 : // sprite. Thus, every update() action
81 : // _after_ the last move will correctly
82 : // update the final repaint area. And this
83 : // does not interfere with subsequent
84 : // moves, because moves always perform a
85 : // hard set of maMoveEndArea to their
86 : // stored value
87 0 : maMoveEndArea.expand( rSpriteRecord.maUpdateArea );
88 0 : mbIsGenericUpdate = true;
89 0 : break;
90 :
91 : default:
92 0 : ENSURE_OR_THROW( false,
93 : "Unexpected case in SpriteUpdater::operator()" );
94 : break;
95 : }
96 : }
97 0 : }
98 :
99 0 : void commit( SpriteRedrawManager::SpriteConnectedRanges& rUpdateCollector ) const
100 : {
101 0 : if( mbIsMove )
102 : {
103 0 : if( !maMoveStartArea.isEmpty() ||
104 0 : !maMoveEndArea.isEmpty() )
105 : {
106 : // if mbIsGenericUpdate is false, this is a
107 : // pure move (i.e. no other update
108 : // operations). Pass that information on to
109 : // the SpriteInfo
110 0 : const bool bIsPureMove( !mbIsGenericUpdate );
111 :
112 : // ignore the case that start and end update
113 : // area overlap - the b2dconnectedranges
114 : // handle that, anyway. doing it this way
115 : // ensures that we have both old and new area
116 : // stored
117 :
118 : // round all given range up to enclosing
119 : // integer rectangle - since the whole thing
120 : // here is about
121 :
122 : // first, draw the new sprite position
123 : rUpdateCollector.addRange(
124 0 : ::basegfx::unotools::b2DSurroundingIntegerRangeFromB2DRange( maMoveEndArea ),
125 : SpriteRedrawManager::SpriteInfo(
126 : mpAffectedSprite,
127 : maMoveEndArea,
128 : true,
129 0 : bIsPureMove ) );
130 :
131 : // then, clear the old place (looks smoother
132 : // this way)
133 : rUpdateCollector.addRange(
134 0 : ::basegfx::unotools::b2DSurroundingIntegerRangeFromB2DRange( maMoveStartArea ),
135 : SpriteRedrawManager::SpriteInfo(
136 : Sprite::Reference(),
137 : maMoveStartArea,
138 : true,
139 0 : bIsPureMove ) );
140 : }
141 : }
142 0 : else if( mbIsGenericUpdate &&
143 0 : !maMoveEndArea.isEmpty() )
144 : {
145 : rUpdateCollector.addRange(
146 0 : ::basegfx::unotools::b2DSurroundingIntegerRangeFromB2DRange( maMoveEndArea ),
147 : SpriteRedrawManager::SpriteInfo(
148 : mpAffectedSprite,
149 : maMoveEndArea,
150 0 : true ) );
151 : }
152 0 : }
153 :
154 : private:
155 : Sprite::Reference mpAffectedSprite;
156 : ::basegfx::B2DRectangle maMoveStartArea;
157 : ::basegfx::B2DRectangle maMoveEndArea;
158 :
159 : /// True, if at least one move was encountered
160 : bool mbIsMove;
161 :
162 : /// True, if at least one generic update was encountered
163 : bool mbIsGenericUpdate;
164 : };
165 :
166 :
167 : /** SpriteChecker functor, which for every sprite checks the
168 : given update vector for necessary screen updates
169 : */
170 : class SpriteUpdater
171 : {
172 : public:
173 : /** Generate update area list
174 :
175 : @param rUpdater
176 : Reference to an updater object, which will receive the
177 : update areas.
178 :
179 : @param rChangeContainer
180 : Container with all sprite change requests
181 :
182 : */
183 0 : SpriteUpdater( SpriteRedrawManager::SpriteConnectedRanges& rUpdater,
184 : const SpriteRedrawManager::VectorOfChangeRecords& rChangeContainer ) :
185 : mrUpdater( rUpdater ),
186 0 : mrChangeContainer( rChangeContainer )
187 : {
188 0 : }
189 :
190 : /** Call this method for every sprite on your screen
191 :
192 : This method scans the change container, collecting all
193 : update info for the given sprite into one or two
194 : update operations, which in turn are inserted into the
195 : connected ranges processor.
196 :
197 : @param rSprite
198 : Current sprite to collect update info for.
199 : */
200 0 : void operator()( const Sprite::Reference& rSprite )
201 : {
202 : const SpriteTracer aSpriteTracer(
203 : ::std::for_each( mrChangeContainer.begin(),
204 : mrChangeContainer.end(),
205 0 : SpriteTracer( rSprite ) ) );
206 :
207 0 : aSpriteTracer.commit( mrUpdater );
208 0 : }
209 :
210 : private:
211 : SpriteRedrawManager::SpriteConnectedRanges& mrUpdater;
212 : const SpriteRedrawManager::VectorOfChangeRecords& mrChangeContainer;
213 : };
214 : }
215 :
216 0 : void SpriteRedrawManager::setupUpdateAreas( SpriteConnectedRanges& rUpdateAreas ) const
217 : {
218 : // TODO(T3): This is NOT thread safe at all. This only works
219 : // under the assumption that NOBODY changes ANYTHING
220 : // concurrently, while this method is on the stack. We should
221 : // really rework the canvas::Sprite interface, in such a way
222 : // that it dumps ALL its state with a single, atomic
223 : // call. Then, we store that state locally. This prolly goes
224 : // in line with the problem of having sprite state available
225 : // for the frame before the last frame; plus, it avoids
226 : // frequent locks of the object mutices
227 : SpriteWeakOrder aSpriteComparator;
228 :
229 : // put all sprites that have changed content into update areas
230 0 : ListOfSprites::const_iterator aCurrSprite( maSprites.begin() );
231 0 : const ListOfSprites::const_iterator aEndSprite ( maSprites.end() );
232 0 : while( aCurrSprite != aEndSprite )
233 : {
234 0 : if( (*aCurrSprite)->isContentChanged() )
235 0 : const_cast<SpriteRedrawManager*>(this)->updateSprite( *aCurrSprite,
236 0 : (*aCurrSprite)->getPosPixel(),
237 0 : (*aCurrSprite)->getUpdateArea() );
238 0 : ++aCurrSprite;
239 : }
240 :
241 : // sort sprites after prio
242 0 : VectorOfSprites aSortedSpriteVector;
243 : ::std::copy( maSprites.begin(),
244 : maSprites.end(),
245 0 : ::std::back_insert_iterator< VectorOfSprites >(aSortedSpriteVector) );
246 : ::std::sort( aSortedSpriteVector.begin(),
247 : aSortedSpriteVector.end(),
248 0 : aSpriteComparator );
249 :
250 : // extract all referenced sprites from the maChangeRecords
251 : // (copy sprites, make the list unique, regarding the
252 : // sprite pointer). This assumes that, until this scope
253 : // ends, nobody changes the maChangeRecords vector!
254 0 : VectorOfSprites aUpdatableSprites;
255 0 : VectorOfChangeRecords::const_iterator aCurrRecord( maChangeRecords.begin() );
256 0 : const VectorOfChangeRecords::const_iterator aEndRecords( maChangeRecords.end() );
257 0 : while( aCurrRecord != aEndRecords )
258 : {
259 0 : const Sprite::Reference& rSprite( aCurrRecord->getSprite() );
260 0 : if( rSprite.is() )
261 0 : aUpdatableSprites.push_back( rSprite );
262 0 : ++aCurrRecord;
263 0 : }
264 :
265 : ::std::sort( aUpdatableSprites.begin(),
266 : aUpdatableSprites.end(),
267 0 : aSpriteComparator );
268 :
269 : VectorOfSprites::iterator aEnd=
270 : ::std::unique( aUpdatableSprites.begin(),
271 0 : aUpdatableSprites.end() );
272 :
273 : // for each unique sprite, check the change event vector,
274 : // calculate the update operation from that, and add the
275 : // result to the aUpdateArea.
276 : ::std::for_each( aUpdatableSprites.begin(),
277 : aEnd,
278 : SpriteUpdater( rUpdateAreas,
279 0 : maChangeRecords) );
280 :
281 : // TODO(P2): Implement your own output iterator adapter, to
282 : // avoid that totally superfluous temp aUnchangedSprites
283 : // vector.
284 :
285 : // add all sprites to rUpdateAreas, that are _not_ already
286 : // contained in the uniquified vector of changed ones
287 : // (i.e. the difference between aSortedSpriteVector and
288 : // aUpdatableSprites).
289 0 : VectorOfSprites aUnchangedSprites;
290 : ::std::set_difference( aSortedSpriteVector.begin(),
291 : aSortedSpriteVector.end(),
292 : aUpdatableSprites.begin(),
293 : aEnd,
294 : ::std::back_insert_iterator< VectorOfSprites >(aUnchangedSprites),
295 0 : aSpriteComparator );
296 :
297 : // add each remaining unchanged sprite to connected ranges,
298 : // marked as "don't need update"
299 0 : VectorOfSprites::const_iterator aCurr( aUnchangedSprites.begin() );
300 0 : const VectorOfSprites::const_iterator aEnd2( aUnchangedSprites.end() );
301 0 : while( aCurr != aEnd2 )
302 : {
303 0 : const ::basegfx::B2DRange& rUpdateArea( (*aCurr)->getUpdateArea() );
304 : rUpdateAreas.addRange(
305 : ::basegfx::unotools::b2DSurroundingIntegerRangeFromB2DRange( rUpdateArea ),
306 0 : SpriteInfo(*aCurr,
307 : rUpdateArea,
308 0 : false) );
309 0 : ++aCurr;
310 0 : }
311 0 : }
312 :
313 : #if OSL_DEBUG_LEVEL > 0
314 : bool impIsEqualB2DRange(const basegfx::B2DRange& rRangeA, const basegfx::B2DRange& rRangeB, double fSmallValue)
315 : {
316 : return fabs(rRangeB.getMinX() - rRangeA.getMinX()) <= fSmallValue
317 : && fabs(rRangeB.getMinY() - rRangeA.getMinY()) <= fSmallValue
318 : && fabs(rRangeB.getMaxX() - rRangeA.getMaxX()) <= fSmallValue
319 : && fabs(rRangeB.getMaxY() - rRangeA.getMaxY()) <= fSmallValue;
320 : }
321 :
322 : bool impIsEqualB2DVector(const basegfx::B2DVector& rVecA, const basegfx::B2DVector& rVecB, double fSmallValue)
323 : {
324 : return fabs(rVecB.getX() - rVecA.getX()) <= fSmallValue
325 : && fabs(rVecB.getY() - rVecA.getY()) <= fSmallValue;
326 : }
327 : #endif
328 :
329 0 : bool SpriteRedrawManager::isAreaUpdateScroll( ::basegfx::B2DRectangle& o_rMoveStart,
330 : ::basegfx::B2DRectangle& o_rMoveEnd,
331 : const UpdateArea& rUpdateArea,
332 : ::std::size_t nNumSprites ) const
333 : {
334 : // check for a solitary move, which consists of exactly two
335 : // pure-move entries, the first with valid, the second with
336 : // invalid sprite (see SpriteTracer::commit()). Note that we
337 : // cannot simply store some flag in SpriteTracer::commit()
338 : // above and just check that here, since during the connected
339 : // range calculations, other sprites might get merged into the
340 : // same region (thus spoiling the scrolling move
341 : // optimization).
342 0 : if( nNumSprites != 2 )
343 0 : return false;
344 :
345 : const SpriteConnectedRanges::ComponentListType::const_iterator aFirst(
346 0 : rUpdateArea.maComponentList.begin() );
347 : SpriteConnectedRanges::ComponentListType::const_iterator aSecond(
348 0 : aFirst ); ++aSecond;
349 :
350 0 : if( !aFirst->second.isPureMove() ||
351 0 : !aSecond->second.isPureMove() ||
352 0 : !aFirst->second.getSprite().is() ||
353 : // use _true_ update area, not the rounded version
354 0 : !aFirst->second.getSprite()->isAreaUpdateOpaque( aFirst->second.getUpdateArea() ) ||
355 0 : aSecond->second.getSprite().is() )
356 : {
357 : // either no move update, or incorrect sprite, or sprite
358 : // content not fully opaque over update region.
359 0 : return false;
360 : }
361 :
362 0 : o_rMoveStart = aSecond->second.getUpdateArea();
363 0 : o_rMoveEnd = aFirst->second.getUpdateArea();
364 :
365 : #if OSL_DEBUG_LEVEL > 0
366 : ::basegfx::B2DRectangle aTotalBounds( o_rMoveStart );
367 : aTotalBounds.expand( o_rMoveEnd );
368 :
369 : SAL_WARN_IF(!impIsEqualB2DRange(rUpdateArea.maTotalBounds, basegfx::unotools::b2DSurroundingIntegerRangeFromB2DRange(aTotalBounds), 0.5),
370 : "canvas",
371 : "SpriteRedrawManager::isAreaUpdateScroll(): sprite area and total area mismatch");
372 : SAL_WARN_IF(!impIsEqualB2DVector(o_rMoveStart.getRange(), o_rMoveEnd.getRange(), 0.5),
373 : "canvas",
374 : "SpriteRedrawManager::isAreaUpdateScroll(): scroll start and end area have mismatching size");
375 : #endif
376 :
377 0 : return true;
378 : }
379 :
380 0 : bool SpriteRedrawManager::isAreaUpdateNotOpaque( const ::basegfx::B2DRectangle& rUpdateRect,
381 : const AreaComponent& rComponent ) const
382 : {
383 0 : const Sprite::Reference& pAffectedSprite( rComponent.second.getSprite() );
384 :
385 0 : if( !pAffectedSprite.is() )
386 0 : return true; // no sprite, no opaque update!
387 :
388 0 : return !pAffectedSprite->isAreaUpdateOpaque( rUpdateRect );
389 : }
390 :
391 0 : bool SpriteRedrawManager::isAreaUpdateOpaque( const UpdateArea& rUpdateArea,
392 : ::std::size_t nNumSprites ) const
393 : {
394 : // check whether the sprites in the update area's list will
395 : // fully cover the given area _and_ do that in an opaque way
396 : // (i.e. no alpha, no non-rectangular sprite content).
397 :
398 : // TODO(P1): Come up with a smarter early-exit criterion here
399 : // (though, I think, the case that _lots_ of sprites _fully_
400 : // cover a rectangular area _without_ any holes is extremely
401 : // improbable)
402 :
403 : // avoid checking large number of sprites (and probably fail,
404 : // anyway). Note: the case nNumSprites < 1 should normally not
405 : // happen, as handleArea() calls backgroundPaint() then.
406 0 : if( nNumSprites > 3 || nNumSprites < 1 )
407 0 : return false;
408 :
409 : // now, calc the _true_ update area, by merging all sprite's
410 : // true update areas into one rectangle
411 0 : ::basegfx::B2DRange aTrueArea( rUpdateArea.maComponentList.begin()->second.getUpdateArea() );
412 : ::std::for_each( rUpdateArea.maComponentList.begin(),
413 : rUpdateArea.maComponentList.end(),
414 : ::boost::bind( (void (basegfx::B2DRange::*)(const basegfx::B2DRange&))(
415 : &basegfx::B2DRange::expand),
416 : aTrueArea,
417 : ::boost::bind( &SpriteInfo::getUpdateArea,
418 : ::boost::bind( ::o3tl::select2nd<AreaComponent>(),
419 0 : _1 ) ) ) );
420 :
421 : const SpriteConnectedRanges::ComponentListType::const_iterator aEnd(
422 0 : rUpdateArea.maComponentList.end() );
423 :
424 : // and check whether _any_ of the sprites tells that its area
425 : // update will not be opaque.
426 : return (::std::find_if( rUpdateArea.maComponentList.begin(),
427 : aEnd,
428 : ::boost::bind( &SpriteRedrawManager::isAreaUpdateNotOpaque,
429 : this,
430 : ::boost::cref(aTrueArea),
431 0 : _1 ) ) == aEnd );
432 : }
433 :
434 0 : bool SpriteRedrawManager::areSpritesChanged( const UpdateArea& rUpdateArea ) const
435 : {
436 : // check whether SpriteInfo::needsUpdate returns false for
437 : // all elements of this area's contained sprites
438 :
439 : // if not a single changed sprite found - just ignore this
440 : // component (return false)
441 : const SpriteConnectedRanges::ComponentListType::const_iterator aEnd(
442 0 : rUpdateArea.maComponentList.end() );
443 : return (::std::find_if( rUpdateArea.maComponentList.begin(),
444 : aEnd,
445 : ::boost::bind( &SpriteInfo::needsUpdate,
446 : ::boost::bind(
447 : ::o3tl::select2nd<SpriteConnectedRanges::ComponentType>(),
448 0 : _1 ) ) ) != aEnd );
449 : }
450 :
451 0 : SpriteRedrawManager::SpriteRedrawManager() :
452 : maSprites(),
453 0 : maChangeRecords()
454 : {
455 0 : }
456 :
457 0 : void SpriteRedrawManager::disposing()
458 : {
459 : // drop all references
460 0 : maChangeRecords.clear();
461 :
462 : // dispose all sprites - the spritecanvas, and by delegation,
463 : // this object, is the owner of the sprites. After all, a
464 : // sprite without a canvas to render into makes not terribly
465 : // much sense.
466 :
467 : // TODO(Q3): Once boost 1.33 is in, change back to for_each
468 : // with ::boost::mem_fn. For the time being, explicit loop due
469 : // to cdecl declaration of all UNO methods.
470 0 : ListOfSprites::reverse_iterator aCurr( maSprites.rbegin() );
471 0 : ListOfSprites::reverse_iterator aEnd( maSprites.rend() );
472 0 : while( aCurr != aEnd )
473 0 : (*aCurr++)->dispose();
474 :
475 0 : maSprites.clear();
476 0 : }
477 :
478 0 : void SpriteRedrawManager::clearChangeRecords()
479 : {
480 0 : maChangeRecords.clear();
481 0 : }
482 :
483 0 : void SpriteRedrawManager::showSprite( const Sprite::Reference& rSprite )
484 : {
485 0 : maSprites.push_back( rSprite );
486 0 : }
487 :
488 0 : void SpriteRedrawManager::hideSprite( const Sprite::Reference& rSprite )
489 : {
490 0 : maSprites.remove( rSprite );
491 0 : }
492 :
493 0 : void SpriteRedrawManager::moveSprite( const Sprite::Reference& rSprite,
494 : const ::basegfx::B2DPoint& rOldPos,
495 : const ::basegfx::B2DPoint& rNewPos,
496 : const ::basegfx::B2DVector& rSpriteSize )
497 : {
498 : maChangeRecords.push_back( SpriteChangeRecord( rSprite,
499 : rOldPos,
500 : rNewPos,
501 0 : rSpriteSize ) );
502 0 : }
503 :
504 0 : void SpriteRedrawManager::updateSprite( const Sprite::Reference& rSprite,
505 : const ::basegfx::B2DPoint& rPos,
506 : const ::basegfx::B2DRange& rUpdateArea )
507 : {
508 : maChangeRecords.push_back( SpriteChangeRecord( rSprite,
509 : rPos,
510 0 : rUpdateArea ) );
511 0 : }
512 :
513 3 : }
514 :
515 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|