From 032f03a0e84551a4e1b5366d1eb90b3b6de77a0a Mon Sep 17 00:00:00 2001 From: Mailson Menezes Date: Mon, 16 Jul 2012 12:57:51 -0300 Subject: [PATCH] Track memory usage of tiles --- core/document.cpp | 20 ++++++++++++++++---- core/page.cpp | 12 +++++++++--- core/tilesmanager.cpp | 14 ++++++++++++++ core/tilesmanager.h | 1 + 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/core/document.cpp b/core/document.cpp index aad045fce..dafcd5a6c 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -923,6 +923,7 @@ void DocumentPrivate::sendGeneratorRequest() // create new tiles manager tilesManager = new TilesManager( r->width(), r->height() ); } + r->page()->deletePixmap( r->id() ); r->page()->setTilesManager( r->id(), tilesManager ); request = r; @@ -933,6 +934,7 @@ void DocumentPrivate::sendGeneratorRequest() << " (" << r->width() << "x" << r->height() << " px);"; // page is too small. stop using tiles. + r->page()->deletePixmap( r->id() ); r->setNormalizedRect( NormalizedRect() ); request = r; @@ -961,8 +963,13 @@ void DocumentPrivate::sendGeneratorRequest() } // [MEM] preventive memory freeing - const QRect requestRect = !request->normalizedRect().isNull() ? request->normalizedRect().geometry( request->width(), request->height() ) : QRect(0, 0, request->width(), request->height() ); - qulonglong pixmapBytes = 4 * requestRect.width() * requestRect.height(); + qulonglong pixmapBytes = 0; + const TilesManager * tm = request->page()->tilesManager( request->id() ); + if ( tm ) + pixmapBytes = tm->totalMemory(); + else + pixmapBytes = 4 * request->width() * request->height(); + if ( pixmapBytes > (1024 * 1024) ) cleanupPixmapMemory( pixmapBytes ); @@ -3816,8 +3823,13 @@ void DocumentPrivate::requestDone( PixmapRequest * req ) if ( itObserver != m_observers.constEnd() ) { // [MEM] 1.2 append memory allocation descriptor to the FIFO - const QRect requestRect = !req->normalizedRect().isNull() ? req->normalizedRect().geometry( req->width(), req->height() ) : QRect(0, 0, req->width(), req->height() ); - qulonglong memoryBytes = 4 * requestRect.width() * requestRect.height(); + qulonglong memoryBytes = 0; + const TilesManager *tm = req->page()->tilesManager( req->id() ); + if ( tm ) + memoryBytes = tm->totalMemory(); + else + memoryBytes = 4 * req->width() * req->height(); + AllocatedPixmap * memoryPage = new AllocatedPixmap( req->id(), req->pageNumber(), memoryBytes ); m_allocatedPixmapsFifo.append( memoryPage ); m_allocatedPixmapsTotalMemory += memoryBytes; diff --git a/core/page.cpp b/core/page.cpp index ee67e711a..cbc7ca493 100644 --- a/core/page.cpp +++ b/core/page.cpp @@ -135,7 +135,6 @@ Page::Page( uint page, double w, double h, Rotation o ) Page::~Page() { deletePixmaps(); - qDeleteAll( d->m_tilesManagers ); deleteRects(); d->deleteHighlights(); deleteAnnotations(); @@ -688,8 +687,14 @@ void Page::setFormFields( const QLinkedList< FormField * >& fields ) void Page::deletePixmap( int id ) { - PagePrivate::PixmapObject object = d->m_pixmaps.take( id ); - delete object.m_pixmap; + TilesManager *tm = d->m_tilesManagers.take( id ); + if ( tm ) + delete tm; + else + { + PagePrivate::PixmapObject object = d->m_pixmaps.take( id ); + delete object.m_pixmap; + } } void Page::deletePixmaps() @@ -701,6 +706,7 @@ void Page::deletePixmaps() } d->m_pixmaps.clear(); + qDeleteAll( d->m_tilesManagers ); } void Page::deleteRects() diff --git a/core/tilesmanager.cpp b/core/tilesmanager.cpp index d572ae910..3bc780bdc 100644 --- a/core/tilesmanager.cpp +++ b/core/tilesmanager.cpp @@ -132,6 +132,20 @@ QList TilesManager::tilesAt( const NormalizedRect &rect ) const return result; } +long TilesManager::totalMemory() const +{ + long totalPixels = 0; + + for ( int i = 0; i < 16; i++ ) + { + QPixmap *pixmap = m_tiles[ i ].pixmap; + if ( pixmap ) + totalPixels += pixmap->width() * pixmap->height(); + } + + return 4*totalPixels; +} + Tile::Tile() : pixmap( 0 ) , dirty ( true ) diff --git a/core/tilesmanager.h b/core/tilesmanager.h index ca14664f4..2ed3ff0ef 100644 --- a/core/tilesmanager.h +++ b/core/tilesmanager.h @@ -37,6 +37,7 @@ class OKULAR_EXPORT TilesManager void setPixmap( const QPixmap *pixmap, const NormalizedRect &rect ); bool hasPixmap( const NormalizedRect &rect ); QList tilesAt( const NormalizedRect &rect ) const; + long totalMemory() const; inline int width() const { return m_width; } void setWidth( int width );