src/gui/painting/qgraphicssystem_runtime.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
--- a/src/gui/painting/qgraphicssystem_runtime.cpp	Fri Sep 17 08:34:18 2010 +0300
+++ b/src/gui/painting/qgraphicssystem_runtime.cpp	Mon Oct 04 01:19:32 2010 +0300
@@ -53,10 +53,8 @@
 static int qt_pixmap_serial = 0;
 
 #define READBACK(f)                                         \
-    m_graphicsSystem->decreaseMemoryUsage(memoryUsage());   \
     f                                                       \
-    readBackInfo();                                         \
-    m_graphicsSystem->increaseMemoryUsage(memoryUsage());   \
+    readBackInfo();
 
 
 class QDeferredGraphicsSystemChange : public QObject
@@ -252,16 +250,8 @@
     return m_data;
 }
 
-uint QRuntimePixmapData::memoryUsage() const
-{
-    if(is_null || d == 0)
-        return 0;
-    return w * h * (d / 8);
-}
-
-
 QRuntimeWindowSurface::QRuntimeWindowSurface(const QRuntimeGraphicsSystem *gs, QWidget *window)
-    : QWindowSurface(window), m_windowSurface(0), m_pendingWindowSurface(0), m_graphicsSystem(gs)
+    : QWindowSurface(window), m_graphicsSystem(gs)
 {
 
 }
@@ -269,7 +259,6 @@
 QRuntimeWindowSurface::~QRuntimeWindowSurface()
 {
     m_graphicsSystem->removeWindowSurface(this);
-    delete m_windowSurface;
 }
 
 QPaintDevice *QRuntimeWindowSurface::paintDevice()
@@ -288,16 +277,13 @@
 #ifdef QT_DEBUG
         qDebug() << "QRuntimeWindowSurface::flush() - destroy pending window surface";
 #endif
-        delete m_pendingWindowSurface;
-        m_pendingWindowSurface = 0;
+        m_pendingWindowSurface.reset();
     }
 }
 
 void QRuntimeWindowSurface::setGeometry(const QRect &rect)
 {
-    m_graphicsSystem->decreaseMemoryUsage(memoryUsage());
     m_windowSurface->setGeometry(rect);
-    m_graphicsSystem->increaseMemoryUsage(memoryUsage());
 }
 
 bool QRuntimeWindowSurface::scroll(const QRegion &area, int dx, int dy)
@@ -330,27 +316,21 @@
     return m_windowSurface->offset(widget);
 }
 
-uint QRuntimeWindowSurface::memoryUsage() const
-{
-    QPaintDevice *pdev = m_windowSurface->paintDevice();
-    if (pdev && pdev->depth() != 0)
-        return pdev->width() * pdev->height() * (pdev->depth()/8);
-
-    return 0;
-}
-
 QRuntimeGraphicsSystem::QRuntimeGraphicsSystem()
-    : m_memoryUsage(0), m_windowSurfaceDestroyPolicy(DestroyImmediately),
-	m_graphicsSystem(0), m_graphicsSystemChangeMemoryLimit(0)
+    : m_windowSurfaceDestroyPolicy(DestroyImmediately),
+      m_graphicsSystem(0)
 {
     QApplicationPrivate::graphics_system_name = QLatin1String("runtime");
     QApplicationPrivate::runtime_graphics_system = true;
 
+#ifdef QT_DEFAULT_RUNTIME_SYSTEM
+    m_graphicsSystemName = QLatin1String(QT_DEFAULT_RUNTIME_SYSTEM);
+    if (m_graphicsSystemName.isNull())
+#endif
+        m_graphicsSystemName = QLatin1String("raster");
+
 #ifdef Q_OS_SYMBIAN
-    m_graphicsSystemName = QLatin1String("openvg");
     m_windowSurfaceDestroyPolicy = DestroyAfterFirstFlush;
-#else
-    m_graphicsSystemName = QLatin1String("raster");
 #endif
 
     m_graphicsSystem = QGraphicsSystemFactory::create(m_graphicsSystemName);
@@ -373,51 +353,30 @@
 {
     Q_ASSERT(m_graphicsSystem);
     QRuntimeWindowSurface *rtSurface = new QRuntimeWindowSurface(this, widget);
-    rtSurface->m_windowSurface = m_graphicsSystem->createWindowSurface(widget);
+    rtSurface->m_windowSurface.reset(m_graphicsSystem->createWindowSurface(widget));
     widget->setWindowSurface(rtSurface);
     m_windowSurfaces << rtSurface;
-    increaseMemoryUsage(rtSurface->memoryUsage());
     return rtSurface;
 }
 
-/*!
-    Sets graphics system when resource memory consumption is under /a memoryUsageLimit.
-*/
-void QRuntimeGraphicsSystem::setGraphicsSystem(const QString &name, uint memoryUsageLimit)
-{
-#ifdef QT_DEBUG
-    qDebug() << "QRuntimeGraphicsSystem::setGraphicsSystem( "<< name <<", " << memoryUsageLimit << ")";
-    qDebug() << "        current approximated graphics system memory usage " << memoryUsage() << " bytes";
-#endif
-    if (memoryUsage() >= memoryUsageLimit) {
-        m_graphicsSystemChangeMemoryLimit = memoryUsageLimit;
-        m_pendingGraphicsSystemName = name;
-    } else {
-        setGraphicsSystem(name);
-    }
-}
-
 void QRuntimeGraphicsSystem::setGraphicsSystem(const QString &name)
 {
     if (m_graphicsSystemName == name)
         return;
 #ifdef QT_DEBUG
     qDebug() << "QRuntimeGraphicsSystem::setGraphicsSystem( " << name << " )";
-    qDebug() << "        current approximated graphics system memory usage "<< memoryUsage() << " bytes";
 #endif
-    delete m_graphicsSystem;
+    QGraphicsSystem *oldSystem = m_graphicsSystem;
     m_graphicsSystem = QGraphicsSystemFactory::create(name);
     m_graphicsSystemName = name;
 
     Q_ASSERT(m_graphicsSystem);
 
-    m_graphicsSystemChangeMemoryLimit = 0;
     m_pendingGraphicsSystemName = QString();
 
     for (int i = 0; i < m_pixmapDatas.size(); ++i) {
         QRuntimePixmapData *proxy = m_pixmapDatas.at(i);
         QPixmapData *newData = m_graphicsSystem->createPixmapData(proxy->m_data);
-        // ### TODO Optimize. Openvg and s60raster graphics systems could switch internal ARGB32_PRE QImage buffers.
         newData->fromImage(proxy->m_data->toImage(), Qt::NoOpaqueDetection);
         delete proxy->m_data;
         proxy->m_data = newData;
@@ -428,58 +387,26 @@
         QRuntimeWindowSurface *proxy = m_windowSurfaces.at(i);
         QWidget *widget = proxy->m_windowSurface->window();
 
-        if(m_windowSurfaceDestroyPolicy == DestroyImmediately) {
-            delete proxy->m_windowSurface;
-            proxy->m_pendingWindowSurface = 0;
-        } else {
-            proxy->m_pendingWindowSurface = proxy->m_windowSurface;
-        }
+        if(m_windowSurfaceDestroyPolicy == DestroyAfterFirstFlush)
+            proxy->m_pendingWindowSurface.reset(proxy->m_windowSurface.take());
 
-        proxy->m_windowSurface = m_graphicsSystem->createWindowSurface(widget);
+        proxy->m_windowSurface.reset(m_graphicsSystem->createWindowSurface(widget));
         qt_widget_private(widget)->invalidateBuffer(widget->rect());
     }
+
+    delete oldSystem;
 }
 
 void QRuntimeGraphicsSystem::removePixmapData(QRuntimePixmapData *pixmapData) const
 {
     int index = m_pixmapDatas.lastIndexOf(pixmapData);
     m_pixmapDatas.removeAt(index);
-    decreaseMemoryUsage(pixmapData->memoryUsage(), true);
 }
 
 void QRuntimeGraphicsSystem::removeWindowSurface(QRuntimeWindowSurface *windowSurface) const
 {
     int index = m_windowSurfaces.lastIndexOf(windowSurface);
     m_windowSurfaces.removeAt(index);
-    decreaseMemoryUsage(windowSurface->memoryUsage(), true);
-}
-
-void QRuntimeGraphicsSystem::increaseMemoryUsage(uint amount) const
-{
-    m_memoryUsage += amount;
-
-    if (m_graphicsSystemChangeMemoryLimit &&
-        m_memoryUsage < m_graphicsSystemChangeMemoryLimit) {
-
-        QRuntimeGraphicsSystem *gs = const_cast<QRuntimeGraphicsSystem*>(this);
-        QDeferredGraphicsSystemChange *deferredChange =
-                    new QDeferredGraphicsSystemChange(gs, m_pendingGraphicsSystemName);
-        deferredChange->launch();
-    }
-}
-
-void QRuntimeGraphicsSystem::decreaseMemoryUsage(uint amount, bool persistent) const
-{
-    m_memoryUsage -= amount;
-
-    if (persistent && m_graphicsSystemChangeMemoryLimit &&
-        m_memoryUsage < m_graphicsSystemChangeMemoryLimit) {
-
-        QRuntimeGraphicsSystem *gs = const_cast<QRuntimeGraphicsSystem*>(this);
-        QDeferredGraphicsSystemChange *deferredChange =
-                    new QDeferredGraphicsSystemChange(gs, m_pendingGraphicsSystemName);
-        deferredChange->launch();
-    }
 }
 
 #include "qgraphicssystem_runtime.moc"