[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    [Panel-devel] Problem with caching applet backgrounds
From:       Thomas Georgiou <tageorgiou () gmail ! com>
Date:       2007-07-29 3:47:00
Message-ID: 200707282347.00599.TAGeorgiou () gmail ! com
[Download RAW message or body]

I implemented dynamic shadows and they are in svn currently.
I now tried to implement caching of the background in order to avoid redrawing 
it every time it paints.  I've done most of it, except that it only paints 
the background if it regenerates it.  I've been trying to debug it for the 
past hour, but now i am ready to sleep so I am asking for others to examine 
it.

P.S. The shadow from the background SVG should be removed now since there are 
dynamic shadows.

The patch is attached.

["appletbackgroundcache.diff" (text/x-diff)]

Index: applet.cpp
===================================================================
--- applet.cpp	(revision 693788)
+++ applet.cpp	(working copy)
@@ -67,7 +67,9 @@
           immutable(false),
           hasConfigurationInterface(false),
           failed(false),
-          canMove(true)
+          canMove(true),
+          cachedContents(0),
+          cachedBackground(0)
     {
         if (appletId == 0) {
             appletId = nextId();
@@ -85,6 +87,8 @@
         }
         delete background;
         delete package;
+        delete cachedBackground;
+        delete cachedContents;
     }
 
     void init(Applet* applet)
@@ -139,90 +143,103 @@
         //TODO: we should cache this background rather that repaint it over and over
         //TODO: get shadow removed from svg
         QSize contents = contentSize(q).toSize();
-        const int contentWidth = contents.width();
-        const int contentHeight = contents.height();
+        int leftO;
+        int topO;
+        if (!cachedContents || (*cachedContents) != contents) {
+            const int contentWidth = contents.width();
+            const int contentHeight = contents.height();
+            if (cachedContents)
+                delete cachedContents;
+            cachedContents = new QSize(contentWidth, contentHeight);
 
+            background->resize();
 
-        background->resize();
+            const int topHeight = background->elementSize("top").height();
+            const int topWidth = background->elementSize("top").width();
+            const int leftWidth = background->elementSize("left").width();
+            const int leftHeight = background->elementSize("left").height();
+            const int rightWidth = background->elementSize("right").width();
+            const int bottomHeight = background->elementSize("bottom").height();
+            //const int lrWidths = leftWidth + rightWidth;
+            //const int tbHeights = topHeight + bottomHeight;
 
-        const int topHeight = background->elementSize("top").height();
-        const int topWidth = background->elementSize("top").width();
-        const int leftWidth = background->elementSize("left").width();
-        const int leftHeight = background->elementSize("left").height();
-        const int rightWidth = background->elementSize("right").width();
-        const int bottomHeight = background->elementSize("bottom").height();
-        //const int lrWidths = leftWidth + rightWidth;
-        //const int tbHeights = topHeight + bottomHeight;
+            // contents top-left corner is (0,0).  We need to draw up and left of \
that +            const int topOffset = 0 - topHeight;
+            const int leftOffset = 0 - leftWidth;
+            const int rightOffset = contentWidth;
+            const int bottomOffset = contentHeight;
+            const int contentTop = 0;
+            const int contentLeft = 0;
+            leftO = leftOffset;
+            topO = topOffset;
 
-        // contents top-left corner is (0,0).  We need to draw up and left of that
-        const int topOffset = 0 - topHeight;
-        const int leftOffset = 0 - leftWidth;
-        const int rightOffset = contentWidth;
-        const int bottomOffset = contentHeight;
-        const int contentTop = 0;
-        const int contentLeft = 0;
+            QPixmap* backgroundPixmap = new QPixmap(leftWidth + contentWidth + \
rightWidth, topHeight + contentHeight + bottomHeight); +            \
backgroundPixmap->fill(Qt::transparent); +            QPainter p(backgroundPixmap);
+            p.translate(leftWidth, topHeight);
+            p.setCompositionMode(QPainter::CompositionMode_Source);
 
-        QPixmap backgroundPixmap(leftWidth + contentWidth + rightWidth, topHeight + \
                contentHeight + bottomHeight);
-        backgroundPixmap.fill(Qt::transparent);
-        QPainter p(&backgroundPixmap);
-        p.translate(leftWidth, topHeight);
-        p.setCompositionMode(QPainter::CompositionMode_Source);
+            p.setRenderHint(QPainter::SmoothPixmapTransform);
 
-        p.setRenderHint(QPainter::SmoothPixmapTransform);
+            background->paint(&p, QRect(leftOffset,  topOffset,    leftWidth,  \
topHeight),    "topleft"); +            background->paint(&p, QRect(rightOffset, \
topOffset,    rightWidth, topHeight),    "topright"); +            \
background->paint(&p, QRect(leftOffset,  bottomOffset, leftWidth,  bottomHeight), \
"bottomleft"); +            background->paint(&p, QRect(rightOffset, bottomOffset, \
rightWidth, bottomHeight), "bottomright");  
-        background->paint(&p, QRect(leftOffset,  topOffset,    leftWidth,  \
                topHeight),    "topleft");
-        background->paint(&p, QRect(rightOffset, topOffset,    rightWidth, \
                topHeight),    "topright");
-        background->paint(&p, QRect(leftOffset,  bottomOffset, leftWidth,  \
                bottomHeight), "bottomleft");
-        background->paint(&p, QRect(rightOffset, bottomOffset, rightWidth, \
bottomHeight), "bottomright"); +            QPixmap left(leftWidth, leftHeight);
+            left.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&left);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "left");
+            }
+            p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, \
contentHeight), left);  
-        QPixmap left(leftWidth, leftHeight);
-        left.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&left);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "left");
-        }
-        p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight), \
left); +            QPixmap right(rightWidth, leftHeight);
+            right.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&right);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "right");
+            }
+            p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, \
contentHeight), right);  
-        QPixmap right(rightWidth, leftHeight);
-        right.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&right);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "right");
-        }
-        p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, contentHeight), \
right); +            QPixmap top(topWidth, topHeight);
+            top.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&top);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "top");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, \
topHeight), top);  
+            QPixmap bottom(topWidth, bottomHeight);
+            bottom.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&bottom);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, \
bottomHeight), bottom);  
-        QPixmap top(topWidth, topHeight);
-        top.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&top);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "top");
-        }
-        p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, topHeight), \
top); +            background->paint(&p, QRect(contentLeft, contentTop, contentWidth \
+ 1, contentHeight + 1), "center");  
-        QPixmap bottom(topWidth, bottomHeight);
-        bottom.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&bottom);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            QImage shadowImage = backgroundPixmap->toImage();
+            shadowBlur(shadowImage, 5);
+            QPixmap* shadowPixmap = new QPixmap(QPixmap::fromImage(shadowImage));
+            QPainter pShadow(shadowPixmap);
+            if (shadowColor != Qt::black) {
+                pShadow.setCompositionMode(QPainter::CompositionMode_SourceAtop);
+                pShadow.fillRect(0, 0, shadowImage.size().width(), \
shadowImage.size().height(), QBrush(shadowColor)); +            }
+            //p2->setRenderHint(QPainter::SmoothPixmapTransform);
+            pShadow.setCompositionMode(QPainter::CompositionMode_SourceOver);
+            pShadow.drawPixmap(0, 0, *backgroundPixmap);
+            if (cachedBackground)
+                delete cachedBackground;
+            cachedBackground = shadowPixmap;
         }
-        p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, \
                bottomHeight), bottom);
-
-        background->paint(&p, QRect(contentLeft, contentTop, contentWidth + 1, \
                contentHeight + 1), "center");
-
-        QImage shadowImage = backgroundPixmap.toImage();
-        shadowBlur(shadowImage, 5);
-        QPainter pShadow(&shadowImage);
-        pShadow.setCompositionMode(QPainter::CompositionMode_SourceAtop);
-        pShadow.fillRect(0, 0, shadowImage.size().width(), \
                shadowImage.size().height(), QBrush(shadowColor));
-        QPixmap shadowPixmap = QPixmap::fromImage(shadowImage);
-        //p2->setRenderHint(QPainter::SmoothPixmapTransform);
-        p2->drawPixmap(leftOffset, topOffset, shadowPixmap);
-        p2->drawPixmap(leftOffset, topOffset, backgroundPixmap);
+        p2->drawPixmap(leftO, topO, *cachedBackground);
     }
 
     void paintHover(QPainter* painter, Applet* q)
@@ -267,6 +284,10 @@
     bool hasConfigurationInterface : 1;
     bool failed : 1;
     bool canMove : 1;
+
+    private:
+        QSize* cachedContents;
+        QPixmap* cachedBackground;
 };
 
 uint Applet::Private::s_maxAppletId = 0;



_______________________________________________
Panel-devel mailing list
Panel-devel@kde.org
https://mail.kde.org/mailman/listinfo/panel-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic