From 6d609e009f4836418bbe5b404be8ae03d29ef8cb Mon Sep 17 00:00:00 2001 From: Robert Bieber Date: Wed, 7 Jul 2010 09:33:47 +0000 Subject: Theme Editor: Implemented caching for rendered text, added profiling info to debug build, added a 500msec delay when rendering after code changes to prevent editor from hanging on large themes git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27332 a1c6a512-1295-4272-9138-f99709370657 --- utils/themeeditor/graphics/rbfont.cpp | 19 +++++++++++---- utils/themeeditor/graphics/rbfontcache.cpp | 21 ++++++++++++++++ utils/themeeditor/graphics/rbfontcache.h | 2 ++ utils/themeeditor/graphics/rbtext.cpp | 14 +++++------ utils/themeeditor/graphics/rbtext.h | 4 +-- utils/themeeditor/graphics/rbtextcache.cpp | 35 +++++++++++++++++++++++++++ utils/themeeditor/graphics/rbtextcache.h | 39 ++++++++++++++++++++++++++++++ 7 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 utils/themeeditor/graphics/rbtextcache.cpp create mode 100644 utils/themeeditor/graphics/rbtextcache.h (limited to 'utils/themeeditor/graphics') diff --git a/utils/themeeditor/graphics/rbfont.cpp b/utils/themeeditor/graphics/rbfont.cpp index cd68af99fe..3b7397283c 100644 --- a/utils/themeeditor/graphics/rbfont.cpp +++ b/utils/themeeditor/graphics/rbfont.cpp @@ -21,6 +21,7 @@ #include "rbfont.h" #include "rbfontcache.h" +#include "rbtextcache.h" #include #include @@ -166,6 +167,13 @@ RBFont::~RBFont() RBText* RBFont::renderText(QString text, QColor color, int viewWidth, QGraphicsItem *parent) { + + /* Checking for a cache hit first */ + QImage* image = RBTextCache::lookup(header.value("filename").toString() + + text); + if(image) + return new RBText(image, viewWidth, parent); + int firstChar = header.value("firstchar").toInt(); int height = header.value("height").toInt(); int maxWidth = header.value("maxwidth").toInt(); @@ -184,10 +192,10 @@ RBText* RBFont::renderText(QString text, QColor color, int viewWidth, for(int i = 0; i < widths.count(); i++) totalWidth += widths[i]; - QImage image(totalWidth, height, QImage::Format_Indexed8); + image = new QImage(totalWidth, height, QImage::Format_Indexed8); - image.setColor(0, qRgba(0,0,0,0)); - image.setColor(1, color.rgb()); + image->setColor(0, qRgba(0,0,0,0)); + image->setColor(1, color.rgb()); /* Drawing the text */ int startX = 0; @@ -214,9 +222,9 @@ RBText* RBFont::renderText(QString text, QColor color, int viewWidth, for(int bit = 0; bit < 8; bit++) { if(mask & data) - image.setPixel(x, y, 1); + image->setPixel(x, y, 1); else - image.setPixel(x, y, 0); + image->setPixel(x, y, 0); y++; mask <<= 1; @@ -230,6 +238,7 @@ RBText* RBFont::renderText(QString text, QColor color, int viewWidth, startX += widths[i]; } + RBTextCache::insert(header.value("filename").toString() + text, image); return new RBText(image, viewWidth, parent); } diff --git a/utils/themeeditor/graphics/rbfontcache.cpp b/utils/themeeditor/graphics/rbfontcache.cpp index 3b6d56fd58..7ec77e11a1 100644 --- a/utils/themeeditor/graphics/rbfontcache.cpp +++ b/utils/themeeditor/graphics/rbfontcache.cpp @@ -22,3 +22,24 @@ #include "rbfontcache.h" QHash RBFontCache::cache; + +void RBFontCache::clearCache() +{ + QHash::iterator i; + for(i = cache.begin(); i != cache.end(); i++) + { + CacheInfo* c = *i; + if(c->imageData) + delete c->imageData; + + if(c->offsetData) + delete c->offsetData; + + if(c->widthData) + delete c->widthData; + + delete c; + } + + cache.clear(); +} diff --git a/utils/themeeditor/graphics/rbfontcache.h b/utils/themeeditor/graphics/rbfontcache.h index 50a6d2ec48..62d82b7188 100644 --- a/utils/themeeditor/graphics/rbfontcache.h +++ b/utils/themeeditor/graphics/rbfontcache.h @@ -23,6 +23,7 @@ #define RBFONTCACHE_H #include +#include class RBFontCache { @@ -39,6 +40,7 @@ public: static CacheInfo* lookup(QString key){ return cache.value(key, 0); } static void insert(QString key, CacheInfo* data){ cache.insert(key, data); } + static void clearCache(); private: static QHash cache; diff --git a/utils/themeeditor/graphics/rbtext.cpp b/utils/themeeditor/graphics/rbtext.cpp index d7fe542ab1..4666f9ae99 100644 --- a/utils/themeeditor/graphics/rbtext.cpp +++ b/utils/themeeditor/graphics/rbtext.cpp @@ -23,24 +23,24 @@ #include -RBText::RBText(const QImage &image, int maxWidth, QGraphicsItem *parent) +RBText::RBText(QImage* image, int maxWidth, QGraphicsItem *parent) :QGraphicsItem(parent), image(image), maxWidth(maxWidth) { } QRectF RBText::boundingRect() const { - if(image.width() < maxWidth) - return QRectF(0, 0, image.width(), image.height()); + if(image->width() < maxWidth) + return QRectF(0, 0, image->width(), image->height()); else - return QRectF(0, 0, maxWidth, image.height()); + return QRectF(0, 0, maxWidth, image->height()); } void RBText::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - if(image.width() < maxWidth) - painter->drawImage(0, 0, image, 0, 0, image.width(), image.height()); + if(image->width() < maxWidth) + painter->drawImage(0, 0, *image, 0, 0, image->width(), image->height()); else - painter->drawImage(0, 0, image, 0, 0, maxWidth, image.height()); + painter->drawImage(0, 0, *image, 0, 0, maxWidth, image->height()); } diff --git a/utils/themeeditor/graphics/rbtext.h b/utils/themeeditor/graphics/rbtext.h index d03505491e..936a809f6c 100644 --- a/utils/themeeditor/graphics/rbtext.h +++ b/utils/themeeditor/graphics/rbtext.h @@ -28,14 +28,14 @@ class RBText : public QGraphicsItem { public: - RBText(const QImage& image, int maxWidth, QGraphicsItem* parent); + RBText(QImage* image, int maxWidth, QGraphicsItem* parent); QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); private: - QImage image; + QImage* image; int maxWidth; }; diff --git a/utils/themeeditor/graphics/rbtextcache.cpp b/utils/themeeditor/graphics/rbtextcache.cpp new file mode 100644 index 0000000000..0116b80311 --- /dev/null +++ b/utils/themeeditor/graphics/rbtextcache.cpp @@ -0,0 +1,35 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2010 Robert Bieber + * + * 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. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include "rbtextcache.h" + +QHash RBTextCache::cache; + +void RBTextCache::clearCache() +{ + QHash::iterator i; + for(i = cache.begin(); i != cache.end(); i++) + { + delete (*i); + } + + cache.clear(); +} diff --git a/utils/themeeditor/graphics/rbtextcache.h b/utils/themeeditor/graphics/rbtextcache.h new file mode 100644 index 0000000000..a0c0e42e8d --- /dev/null +++ b/utils/themeeditor/graphics/rbtextcache.h @@ -0,0 +1,39 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2010 Robert Bieber + * + * 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. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#ifndef RBTEXTCACHE_H +#define RBTEXTCACHE_H + +#include +#include + +class RBTextCache +{ +public: + static QImage* lookup(QString key){ return cache.value(key, 0); } + static void insert(QString key, QImage* im){ cache.insert(key, im); } + static void clearCache(); + +private: + static QHash cache; +}; + +#endif // RBTEXTCACHE_H -- cgit v1.2.3