You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

577 lines
15 KiB

/***************************************************************************
* Copyright (C) 2012 by Mailson Menezes <mailson@gmail.com> *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "tilesmanager_p.h"
#include <QPixmap>
#include <QtCore/qmath.h>
#include <QList>
#define MISSCOUNTER_MAX INT_MAX/2
#define MISSCOUNTER_MIN INT_MIN/2
#define TILES_MAXSIZE 2000000
using namespace Okular;
static bool rankedTilesLessThan( Tile *t1, Tile *t2 )
{
// Order tiles by its dirty state and miss counter. That is: dirty tiles
// will be evicted first then tiles with higher miss values.
if ( t1->dirty == t2->dirty )
return t1->miss < t2->miss;
return !t1->dirty;
}
class TilesManager::Private
{
public:
Private();
bool hasPixmap( const NormalizedRect &rect, const Tile &tile ) const;
void tilesAt( const NormalizedRect &rect, Tile &tile, QList<Tile> &result, bool allowEmpty );
void setPixmap( const QPixmap *pixmap, const NormalizedRect &rect, Tile &tile );
/**
* Mark @p tile and all its children as dirty
*/
static void markDirty( Tile &tile );
/**
* Deletes all tiles, recursively
*/
void deleteTiles( const Tile &tile );
void markParentDirty( const Tile &tile );
void rankTiles( Tile &tile, QList<Tile*> &rankedTiles );
/**
* Since the tile can be large enough to occupy a significant amount of
* space, they may be split in more tiles. This operation is performed
* when the tiles of a certain region is requested and they are bigger
* than an arbitrary value. Only tiles intersecting the desired region
* are split. There's no need to do this for the entire page.
*/
void split( Tile &tile, const NormalizedRect &rect );
/**
* Checks whether the tile's size is bigger than an arbitrary value and
* performs the split operation returning true.
* Otherwise it just returns false, without performing any operation.
*/
bool splitBigTiles( Tile &tile, const NormalizedRect &rect );
// The page is split in a 4x4 grid of tiles
Tile tiles[16];
int width;
int height;
long totalPixels;
Rotation rotation;
NormalizedRect visibleRect;
NormalizedRect requestRect;
int requestWidth;
int requestHeight;
};
TilesManager::Private::Private()
: width( 0 )
, height( 0 )
, totalPixels( 0 )
, rotation( Rotation0 )
, requestRect( NormalizedRect() )
, requestWidth( 0 )
, requestHeight( 0 )
{
}
TilesManager::TilesManager( int width, int height, Rotation rotation )
: d( new Private )
{
d->width = width;
d->height = height;
d->rotation = rotation;
// The page is split in a 4x4 grid of tiles
const double dim = 0.25;
for ( int i = 0; i < 16; ++i )
{
int x = i % 4;
int y = i / 4;
d->tiles[ i ].rect = NormalizedRect( x*dim, y*dim, x*dim+dim, y*dim+dim );
}
}
TilesManager::~TilesManager()
{
for ( int i = 0; i < 16; ++i )
d->deleteTiles( d->tiles[ i ] );
delete d;
}
void TilesManager::Private::deleteTiles( const Tile &tile )
{
if ( tile.pixmap )
{
totalPixels -= tile.pixmap->width()*tile.pixmap->height();
delete tile.pixmap;
}
if ( tile.nTiles > 0 )
{
for ( int i = 0; i < tile.nTiles; ++i )
deleteTiles( tile.tiles[ i ] );
delete [] tile.tiles;
}
}
void TilesManager::setWidth( int width )
{
if ( width == d->width )
return;
d->width = width;
markDirty();
}
int TilesManager::width() const
{
return d->width;
}
void TilesManager::setHeight( int height )
{
if ( height == d->height )
return;
d->height = height;
}
int TilesManager::height() const
{
return d->height;
}
void TilesManager::setRotation( Rotation rotation )
{
if ( rotation == d->rotation )
return;
d->rotation = rotation;
markDirty();
}
Rotation TilesManager::rotation() const
{
return d->rotation;
}
void TilesManager::markDirty()
{
for ( int i = 0; i < 16; ++i )
{
TilesManager::Private::markDirty( d->tiles[ i ] );
}
}
void TilesManager::Private::markDirty( Tile &tile )
{
tile.dirty = true;
for ( int i = 0; i < tile.nTiles; ++i )
{
markDirty( tile.tiles[ i ] );
}
}
void TilesManager::setVisibleRect( const NormalizedRect &rect )
{
if ( d->visibleRect == rect )
return;
d->visibleRect = rect;
}
NormalizedRect TilesManager::visibleRect() const
{
return d->visibleRect;
}
void TilesManager::setPixmap( const QPixmap *pixmap, const NormalizedRect &rect )
{
if ( !d->requestRect.isNull() )
{
if ( !(d->requestRect == rect) || rect.geometry( width(), height() ).size() != pixmap->size() )
return;
d->requestRect = NormalizedRect();
}
for ( int i = 0; i < 16; ++i )
{
d->setPixmap( pixmap, fromRotatedRect( rect, d->rotation ), d->tiles[ i ] );
}
}
void TilesManager::Private::setPixmap( const QPixmap *pixmap, const NormalizedRect &rect, Tile &tile )
{
QRect pixmapRect = TilesManager::toRotatedRect( rect, rotation ).geometry( width, height );
// Exclude tiles outside the viewport
if ( !tile.rect.intersects( rect ) )
return;
// if the tile is not entirely within the viewport (the tile intersects an
// edged of the viewport), attempt to set the pixmap in the children tiles
if ( !((tile.rect & rect) == tile.rect) )
{
// paint children tiles
if ( tile.nTiles > 0 )
{
for ( int i = 0; i < tile.nTiles; ++i )
setPixmap( pixmap, rect, tile.tiles[ i ] );
delete tile.pixmap;
tile.pixmap = 0;
}
return;
}
// the tile lies entirely within the viewport
if ( tile.nTiles == 0 )
{
tile.dirty = false;
// check whether the tile size is big and split it if necessary
if ( !splitBigTiles( tile, rect ) )
{
if ( tile.pixmap )
{
totalPixels -= tile.pixmap->width()*tile.pixmap->height();
delete tile.pixmap;
}
NormalizedRect rotatedRect = TilesManager::toRotatedRect( tile.rect, rotation );
tile.pixmap = new QPixmap( pixmap->copy( rotatedRect.geometry( width, height ).translated( -pixmapRect.topLeft() ) ) );
totalPixels += tile.pixmap->width()*tile.pixmap->height();
}
else
{
if ( tile.pixmap )
{
totalPixels -= tile.pixmap->width()*tile.pixmap->height();
delete tile.pixmap;
tile.pixmap = 0;
}
for ( int i = 0; i < tile.nTiles; ++i )
setPixmap( pixmap, rect, tile.tiles[ i ] );
}
}
else
{
QRect tileRect = tile.rect.geometry( width, height );
// sets the pixmap of the children tiles. if the tile's size is too
// small, discards the children tiles and use the current one
if ( tileRect.width()*tileRect.height() >= TILES_MAXSIZE )
{
tile.dirty = false;
if ( tile.pixmap )
{
totalPixels -= tile.pixmap->width()*tile.pixmap->height();
delete tile.pixmap;
tile.pixmap = 0;
}
for ( int i = 0; i < tile.nTiles; ++i )
setPixmap( pixmap, rect, tile.tiles[ i ] );
}
else
{
// remove children tiles
for ( int i = 0; i < tile.nTiles; ++i )
{
deleteTiles( tile.tiles[ i ] );
tile.tiles[ i ].pixmap = 0;
}
delete [] tile.tiles;
tile.tiles = 0;
tile.nTiles = 0;
// paint tile
if ( tile.pixmap )
{
totalPixels -= tile.pixmap->width()*tile.pixmap->height();
delete tile.pixmap;
}
tile.pixmap = new QPixmap( pixmap->copy( tile.rect.geometry( width, height ).translated( -pixmapRect.topLeft() ) ) );
totalPixels += tile.pixmap->width()*tile.pixmap->height();
tile.dirty = false;
}
}
}
bool TilesManager::hasPixmap( const NormalizedRect &rect )
{
NormalizedRect rotatedRect = fromRotatedRect( rect, d->rotation );
for ( int i = 0; i < 16; ++i )
{
if ( !d->hasPixmap( rotatedRect, d->tiles[ i ] ) )
return false;
}
return true;
}
bool TilesManager::Private::hasPixmap( const NormalizedRect &rect, const Tile &tile ) const
{
if ( !tile.rect.intersects( rect ) )
return true;
if ( tile.nTiles == 0 )
return tile.isValid();
// all children tiles are clean. doesn't need to go deeper
if ( !tile.dirty )
return true;
for ( int i = 0; i < tile.nTiles; ++i )
{
if ( !hasPixmap( rect, tile.tiles[ i ] ) )
return false;
}
return true;
}
QList<Tile> TilesManager::tilesAt( const NormalizedRect &rect, bool allowEmpty )
{
QList<Tile> result;
NormalizedRect rotatedRect = fromRotatedRect( rect, d->rotation );
for ( int i = 0; i < 16; ++i )
{
d->tilesAt( rotatedRect, d->tiles[ i ], result, allowEmpty );
}
return result;
}
void TilesManager::Private::tilesAt( const NormalizedRect &rect, Tile &tile, QList<Tile> &result, bool allowEmpty )
{
if ( !tile.rect.intersects( rect ) )
{
tile.miss = qMin( tile.miss+1, MISSCOUNTER_MAX );
return;
}
// split big tiles before the requests are made, otherwise we would end up
// requesting huge areas unnecessarily
splitBigTiles( tile, rect );
if ( ( allowEmpty && tile.nTiles == 0 ) || ( !allowEmpty && tile.pixmap ) )
{
tile.miss = qMax( tile.miss-1, MISSCOUNTER_MIN );
Tile newTile = tile;
if ( rotation != Rotation0 )
newTile.rect = TilesManager::toRotatedRect( tile.rect, rotation );
result.append( newTile );
}
else
{
for ( int i = 0; i < tile.nTiles; ++i )
tilesAt( rect, tile.tiles[ i ], result, allowEmpty );
}
}
long TilesManager::totalMemory() const
{
return 4*d->totalPixels;
}
void TilesManager::cleanupPixmapMemory( qulonglong numberOfBytes )
{
QList<Tile*> rankedTiles;
for ( int i = 0; i < 16; ++i )
{
d->rankTiles( d->tiles[ i ], rankedTiles );
}
qSort( rankedTiles.begin(), rankedTiles.end(), rankedTilesLessThan );
while ( numberOfBytes > 0 && !rankedTiles.isEmpty() )
{
Tile *tile = rankedTiles.takeLast();
if ( !tile->pixmap )
continue;
// do not evict visible pixmaps
if ( tile->rect.intersects( d->visibleRect ) )
continue;
qulonglong pixels = tile->pixmap->width()*tile->pixmap->height();
d->totalPixels -= pixels;
if ( numberOfBytes < 4*pixels )
numberOfBytes = 0;
else
numberOfBytes -= 4*pixels;
tile->miss = 0;
delete tile->pixmap;
tile->pixmap = 0;
d->markParentDirty( *tile );
}
}
void TilesManager::Private::markParentDirty( const Tile &tile )
{
if ( !tile.parent )
return;
if ( !tile.parent->dirty )
{
tile.parent->dirty = true;
markParentDirty( *tile.parent );
}
}
void TilesManager::Private::rankTiles( Tile &tile, QList<Tile*> &rankedTiles )
{
if ( tile.parent )
tile.miss = qBound( MISSCOUNTER_MIN, tile.miss + tile.parent->miss, MISSCOUNTER_MAX );
if ( tile.pixmap )
{
rankedTiles.append( &tile );
}
else
{
for ( int i = 0; i < tile.nTiles; ++i )
{
rankTiles( tile.tiles[ i ], rankedTiles );
}
if ( tile.nTiles > 0 )
tile.miss = 0;
}
}
bool TilesManager::isRequesting( const NormalizedRect &rect, int pageWidth, int pageHeight ) const
{
return rect == d->requestRect && pageWidth == d->requestWidth && pageHeight == d->requestHeight;
}
void TilesManager::setRequest( const NormalizedRect &rect, int pageWidth, int pageHeight )
{
d->requestRect = rect;
d->requestWidth = pageWidth;
d->requestHeight = pageHeight;
}
bool TilesManager::Private::splitBigTiles( Tile &tile, const NormalizedRect &rect )
{
QRect tileRect = tile.rect.geometry( width, height );
if ( tileRect.width()*tileRect.height() < TILES_MAXSIZE )
return false;
split( tile, rect );
return true;
}
void TilesManager::Private::split( Tile &tile, const NormalizedRect &rect )
{
if ( tile.nTiles != 0 )
return;
if ( rect.isNull() || !tile.rect.intersects( rect ) )
return;
tile.nTiles = 4;
tile.tiles = new Tile[4];
double hCenter = (tile.rect.left + tile.rect.right)/2;
double vCenter = (tile.rect.top + tile.rect.bottom)/2;
tile.tiles[0].rect = NormalizedRect( tile.rect.left, tile.rect.top, hCenter, vCenter );
tile.tiles[1].rect = NormalizedRect( hCenter, tile.rect.top, tile.rect.right, vCenter );
tile.tiles[2].rect = NormalizedRect( tile.rect.left, vCenter, hCenter, tile.rect.bottom );
tile.tiles[3].rect = NormalizedRect( hCenter, vCenter, tile.rect.right, tile.rect.bottom );
for ( int i = 0; i < tile.nTiles; ++i )
{
tile.tiles[ i ].parent = &tile;
splitBigTiles( tile.tiles[ i ], rect );
}
}
NormalizedRect TilesManager::fromRotatedRect( const NormalizedRect &rect, Rotation rotation )
{
if ( rotation == Rotation0 )
return rect;
NormalizedRect newRect;
switch ( rotation )
{
case Rotation90:
newRect = NormalizedRect( rect.top, 1 - rect.right, rect.bottom, 1 - rect.left );
break;
case Rotation180:
newRect = NormalizedRect( 1 - rect.right, 1 - rect.bottom, 1 - rect.left, 1 - rect.top );
break;
case Rotation270:
newRect = NormalizedRect( 1 - rect.bottom, rect.left, 1 - rect.top, rect.right );
break;
default:
newRect = rect;
break;
}
return newRect;
}
NormalizedRect TilesManager::toRotatedRect( const NormalizedRect &rect, Rotation rotation )
{
if ( rotation == Rotation0 )
return rect;
NormalizedRect newRect;
switch ( rotation )
{
case Rotation90:
newRect = NormalizedRect( 1 - rect.bottom, rect.left, 1 - rect.top, rect.right );
break;
case Rotation180:
newRect = NormalizedRect( 1 - rect.right, 1 - rect.bottom, 1 - rect.left, 1 - rect.top );
break;
case Rotation270:
newRect = NormalizedRect( rect.top, 1 - rect.right, rect.bottom, 1 - rect.left );
break;
default:
newRect = rect;
break;
}
return newRect;
}
Tile::Tile()
: pixmap( 0 )
, dirty ( true )
, tiles( 0 )
, nTiles( 0 )
, parent( 0 )
, miss( 0 )
{
}
bool Tile::isValid() const
{
return pixmap && !dirty;
}