File indexing completed on 2024-05-19 16:09:58

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2011 Boudewijn Rempt <boud@kogmbh.com>
0003  * Copyright (C) 2011 Marijn Kruisselbrink <mkruisselbrink@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KWPageCacheManager.h"
0022 
0023 #include <QImage>
0024 
0025 static const int MAX_TILE_SIZE = 1024;
0026 
0027 /*
0028 KWPageCache::KWPageCache(KWPageCacheManager *manager, QImage *img)
0029     : m_manager(manager), cache(img), allExposed(true)
0030 {
0031     cache->fill(0xffff);
0032 }*/
0033 
0034 KWPageCache::KWPageCache(KWPageCacheManager *manager, int w, int h)
0035     : m_manager(manager), m_tilesx(1), m_tilesy(1), m_size(w, h), allExposed(true)
0036 {
0037     if (w > MAX_TILE_SIZE || h > MAX_TILE_SIZE) {
0038         m_tilesx = w / MAX_TILE_SIZE;
0039         if (w % MAX_TILE_SIZE != 0) m_tilesx++;
0040         m_tilesy = h / MAX_TILE_SIZE;
0041         if (h % MAX_TILE_SIZE != 0) m_tilesy++;
0042 
0043         for (int x = 0; x < m_tilesx; x++) {
0044             for (int y = 0; y < m_tilesy; y++) {
0045                 int tilew = qMin(MAX_TILE_SIZE, w - x * MAX_TILE_SIZE);
0046                 int tileh = qMin(MAX_TILE_SIZE, h - y * MAX_TILE_SIZE);
0047                 cache.push_back(QImage(tilew, tileh, QImage::Format_RGB16));
0048             }
0049         }
0050     } else {
0051         cache.push_back(QImage(w, h, QImage::Format_RGB16));
0052     }
0053 }
0054 
0055 
0056 KWPageCache::~KWPageCache()
0057 {
0058 }
0059 
0060 KWPageCacheManager::KWPageCacheManager(int cacheSize)
0061     : m_cache(cacheSize)
0062 {
0063 }
0064 
0065 KWPageCacheManager::~KWPageCacheManager()
0066 {
0067     clear();
0068 }
0069 
0070 KWPageCache *KWPageCacheManager::take(const KWPage &page)
0071 {
0072     KWPageCache *cache = 0;
0073     if (m_cache.contains(page)) {
0074         cache = m_cache.take(page);
0075     }
0076     return cache;
0077 }
0078 
0079 void KWPageCacheManager::insert(const KWPage &page, KWPageCache *cache)
0080 {
0081     QSize size = cache->m_size;
0082     // make sure always at least two pages can be cached
0083     m_cache.insert(page, cache, qMin(m_cache.maxCost() / 2, size.width() * size.height()));
0084 }
0085 
0086 KWPageCache *KWPageCacheManager::cache(const QSize &size)
0087 {
0088     KWPageCache *cache = 0;
0089     if (!cache){
0090         cache = new KWPageCache(this, size.width(), size.height());
0091     }
0092     return cache;
0093 }
0094 
0095 void KWPageCacheManager::clear()
0096 {
0097     m_cache.clear();
0098 }
0099