From 569c9b84cecdef0d7fb7cc3af0a007662a97cece Mon Sep 17 00:00:00 2001 From: Fabio D'Urso Date: Mon, 2 Jul 2012 08:22:51 +0200 Subject: [PATCH] Before preloading pixmaps check that they fit in cache --- core/document.cpp | 25 ++++++++++++++++++++++++- core/generator.cpp | 14 ++++++++++++++ core/generator_p.h | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/document.cpp b/core/document.cpp index 3200ebdb3..f6bf699a7 100644 --- a/core/document.cpp +++ b/core/document.cpp @@ -11,6 +11,7 @@ #include "document.h" #include "document_p.h" +#include #ifdef Q_OS_WIN #define _WIN32_WINNT 0x0500 #include @@ -973,6 +974,20 @@ void DocumentPrivate::slotTimedMemoryCheck() void DocumentPrivate::sendGeneratorRequest() { + /* If the pixmap cache will have to be cleaned in order to make room for the + * next request, get the distance from the current viewport of the page + * whose pixmap will be removed. We will ignore preload requests for pages + * that are at the same distance or farther */ + const qulonglong memoryToFree = calculateMemoryToFree(); + const int currentViewportPage = (*m_viewportIterator).pageNumber; + int maxDistance = INT_MAX; // Default: No maximum + if ( memoryToFree ) + { + AllocatedPixmap *pixmapToReplace = searchLowestPriorityUnloadablePixmap(); + if ( pixmapToReplace ) + maxDistance = qAbs( pixmapToReplace->page - currentViewportPage ); + } + // find a request PixmapRequest * request = 0; m_pixmapRequestsMutex.lock(); @@ -1000,8 +1015,16 @@ void DocumentPrivate::sendGeneratorRequest() } delete r; } + else if ( !r->d->mForce && r->d->isPreload() && qAbs( r->pageNumber() - currentViewportPage ) >= maxDistance ) + { + m_pixmapRequestsStack.pop_back(); + //kDebug() << "Ignoring request that doesn't fit in cache"; + delete r; + } else + { request = r; + } } // if no request found (or already generated), return @@ -1014,7 +1037,7 @@ void DocumentPrivate::sendGeneratorRequest() // [MEM] preventive memory freeing qulonglong pixmapBytes = 4 * request->width() * request->height(); if ( pixmapBytes > (1024 * 1024) ) - cleanupPixmapMemory(); + cleanupPixmapMemory( memoryToFree /* previously calculated value */ ); // submit the request to the generator if ( m_generator->canGeneratePixmap() ) diff --git a/core/generator.cpp b/core/generator.cpp index 17b5e6309..0073943a0 100644 --- a/core/generator.cpp +++ b/core/generator.cpp @@ -10,6 +10,7 @@ #include "generator.h" #include "generator_p.h" +#include "observer.h" #include #include @@ -477,6 +478,19 @@ void PixmapRequestPrivate::swap() qSwap( mWidth, mHeight ); } +bool PixmapRequestPrivate::isPreload() const +{ + switch ( mPriority ) + { + case PAGEVIEW_PRELOAD_PRIO: + case THUMBNAILS_PRELOAD_PRIO: + case PRESENTATION_PRELOAD_PRIO: + return true; + default: + return false; + } +} + class Okular::ExportFormatPrivate : public QSharedData { public: diff --git a/core/generator_p.h b/core/generator_p.h index f2d746a19..775da7f5b 100644 --- a/core/generator_p.h +++ b/core/generator_p.h @@ -70,6 +70,7 @@ class PixmapRequestPrivate { public: void swap(); + bool isPreload() const; int mId; int mPageNumber;