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