From 7c99a5273bea6f071efcd8441bedc1b768cc1d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Thu, 10 Mar 2011 12:03:45 +0100 Subject: Added automatic graphicssystem switching on meego when app is minimized. When all top-level widgets are minimized we switch to raster to reduce GPU memory consumption. We switch back to graphicssystem meego when at least one top-level widget is shown normally again. The switching only applies when the runtime graphicssystem is being used. The switching only applies when the runtime graphicssystem is being used. Task-number: QTBUG-18013 Reviewed-by: Armin Berres --- src/gui/painting/qgraphicssystem_runtime.cpp | 5 +- .../graphicssystems/meego/qmeegographicssystem.cpp | 170 +++++++++++++++++++-- .../graphicssystems/meego/qmeegographicssystem.h | 18 ++- .../qmeegographicssystemhelper.cpp | 44 +----- .../qmeegographicssystemhelper.h | 17 ++- tools/qmeegographicssystemhelper/qmeegoruntime.cpp | 49 +++++- tools/qmeegographicssystemhelper/qmeegoruntime.h | 4 + 7 files changed, 249 insertions(+), 58 deletions(-) diff --git a/src/gui/painting/qgraphicssystem_runtime.cpp b/src/gui/painting/qgraphicssystem_runtime.cpp index 5841d40..33652ee 100644 --- a/src/gui/painting/qgraphicssystem_runtime.cpp +++ b/src/gui/painting/qgraphicssystem_runtime.cpp @@ -394,7 +394,10 @@ void QRuntimeGraphicsSystem::setGraphicsSystem(const QString &name) if(m_windowSurfaceDestroyPolicy == DestroyAfterFirstFlush) proxy->m_pendingWindowSurface.reset(proxy->m_windowSurface.take()); - proxy->m_windowSurface.reset(m_graphicsSystem->createWindowSurface(widget)); + QWindowSurface *newWindowSurface = m_graphicsSystem->createWindowSurface(widget); + newWindowSurface->setGeometry(proxy->geometry()); + + proxy->m_windowSurface.reset(newWindowSurface); qt_widget_private(widget)->invalidateBuffer(widget->rect()); } diff --git a/src/plugins/graphicssystems/meego/qmeegographicssystem.cpp b/src/plugins/graphicssystems/meego/qmeegographicssystem.cpp index 13eab7f..18a0944 100644 --- a/src/plugins/graphicssystems/meego/qmeegographicssystem.cpp +++ b/src/plugins/graphicssystems/meego/qmeegographicssystem.cpp @@ -63,6 +63,8 @@ bool QMeeGoGraphicsSystem::surfaceWasCreated = false; QHash QMeeGoGraphicsSystem::liveTexturePixmaps; +QList QMeeGoGraphicsSystem::switchCallbacks; + QMeeGoGraphicsSystem::QMeeGoGraphicsSystem() { qDebug("Using the meego graphics system"); @@ -74,6 +76,78 @@ QMeeGoGraphicsSystem::~QMeeGoGraphicsSystem() qt_destroy_gl_share_widget(); } +class QMeeGoGraphicsSystemSwitchHandler : public QObject +{ + Q_OBJECT +public: + QMeeGoGraphicsSystemSwitchHandler(); + + void addWidget(QWidget *widget); + bool eventFilter(QObject *, QEvent *); + +private slots: + void removeWidget(QObject *object); + +private: + int visibleWidgets() const; + +private: + QList m_widgets; +}; + +QMeeGoGraphicsSystemSwitchHandler::QMeeGoGraphicsSystemSwitchHandler() +{ +} + +void QMeeGoGraphicsSystemSwitchHandler::addWidget(QWidget *widget) +{ + if (!m_widgets.contains(widget)) { + widget->installEventFilter(this); + connect(widget, SIGNAL(destroyed(QObject *)), this, SLOT(removeWidget(QObject *))); + m_widgets << widget; + } +} + +void QMeeGoGraphicsSystemSwitchHandler::removeWidget(QObject *object) +{ + m_widgets.removeOne(static_cast(object)); +} + +int QMeeGoGraphicsSystemSwitchHandler::visibleWidgets() const +{ + int count = 0; + for (int i = 0; i < m_widgets.size(); ++i) + count += m_widgets.at(i)->isVisible() && !(m_widgets.at(i)->windowState() & Qt::WindowMinimized); + return count; +} + +bool QMeeGoGraphicsSystemSwitchHandler::eventFilter(QObject *object, QEvent *event) +{ + if (event->type() == QEvent::WindowStateChange) { + QWindowStateChangeEvent *change = static_cast(event); + QWidget *widget = static_cast(object); + + Qt::WindowStates current = widget->windowState(); + Qt::WindowStates old = change->oldState(); + + // did minimized flag change? + if ((current ^ old) & Qt::WindowMinimized) { + if (current & Qt::WindowMinimized) { + if (visibleWidgets() == 0) + QMeeGoGraphicsSystem::switchToRaster(); + } else { + if (visibleWidgets() == 1) + QMeeGoGraphicsSystem::switchToMeeGo(); + } + } + } + + // resume processing of event + return false; +} + +Q_GLOBAL_STATIC(QMeeGoGraphicsSystemSwitchHandler, switch_handler) + QWindowSurface* QMeeGoGraphicsSystem::createWindowSurface(QWidget *widget) const { QGLWidget *shareWidget = qt_gl_share_widget(); @@ -83,6 +157,9 @@ QWindowSurface* QMeeGoGraphicsSystem::createWindowSurface(QWidget *widget) const QGLShareContextScope ctx(shareWidget->context()); + if (QApplicationPrivate::instance()->graphics_system_name == QLatin1String("runtime")) + switch_handler()->addWidget(widget); + QMeeGoGraphicsSystem::surfaceWasCreated = true; QWindowSurface *surface = new QGLWindowSurface(widget); return surface; @@ -203,18 +280,7 @@ QPixmapData *QMeeGoGraphicsSystem::pixmapDataWithGLTexture(int w, int h) bool QMeeGoGraphicsSystem::meeGoRunning() { - if (! QApplicationPrivate::instance()) { - qWarning("Application not running just yet... hard to know what system running!"); - return false; - } - - QString name = QApplicationPrivate::instance()->graphics_system_name; - if (name == "runtime") { - QRuntimeGraphicsSystem *rsystem = (QRuntimeGraphicsSystem *) QApplicationPrivate::instance()->graphics_system; - name = rsystem->graphicsSystemName(); - } - - return (name == "meego"); + return runningGraphicsSystemName() == "meego"; } QPixmapData* QMeeGoGraphicsSystem::pixmapDataWithNewLiveTexture(int w, int h, QImage::Format format) @@ -259,6 +325,69 @@ void QMeeGoGraphicsSystem::destroyFenceSync(void *fenceSync) QMeeGoExtensions::eglDestroySyncKHR(QEgl::display(), fenceSync); } +QString QMeeGoGraphicsSystem::runningGraphicsSystemName() +{ + if (!QApplicationPrivate::instance()) { + qWarning("Querying graphics system but application not running yet!"); + return QString(); + } + + QString name = QApplicationPrivate::instance()->graphics_system_name; + if (name == QLatin1String("runtime")) { + QRuntimeGraphicsSystem *rsystem = (QRuntimeGraphicsSystem *) QApplicationPrivate::instance()->graphics_system; + name = rsystem->graphicsSystemName(); + } + + return name; +} + +void QMeeGoGraphicsSystem::switchToMeeGo() +{ + if (meeGoRunning()) + return; + + if (QApplicationPrivate::instance()->graphics_system_name != QLatin1String("runtime")) + qWarning("Can't switch to meego - switching only supported with 'runtime' graphics system."); + else { + triggerSwitchCallbacks(0, "meego"); + + QApplication *app = static_cast(QCoreApplication::instance()); + app->setGraphicsSystem(QLatin1String("meego")); + + triggerSwitchCallbacks(1, "meego"); + } +} + +void QMeeGoGraphicsSystem::switchToRaster() +{ + if (runningGraphicsSystemName() == QLatin1String("raster")) + return; + + if (QApplicationPrivate::instance()->graphics_system_name != QLatin1String("runtime")) + qWarning("Can't switch to raster - switching only supported with 'runtime' graphics system."); + else { + triggerSwitchCallbacks(0, "raster"); + + QApplication *app = static_cast(QCoreApplication::instance()); + app->setGraphicsSystem(QLatin1String("raster")); + + QMeeGoLivePixmapData::invalidateSurfaces(); + + triggerSwitchCallbacks(1, "raster"); + } +} + +void QMeeGoGraphicsSystem::registerSwitchCallback(QMeeGoSwitchCallback callback) +{ + switchCallbacks << callback; +} + +void QMeeGoGraphicsSystem::triggerSwitchCallbacks(int type, const char *name) +{ + for (int i = 0; i < switchCallbacks.size(); ++i) + switchCallbacks.at(i)(type, name); +} + /* C API */ int qt_meego_image_to_egl_shared_image(const QImage &image) @@ -340,3 +469,20 @@ void qt_meego_invalidate_live_surfaces(void) { return QMeeGoLivePixmapData::invalidateSurfaces(); } + +void qt_meego_switch_to_raster(void) +{ + QMeeGoGraphicsSystem::switchToRaster(); +} + +void qt_meego_switch_to_meego(void) +{ + QMeeGoGraphicsSystem::switchToMeeGo(); +} + +void qt_meego_register_switch_callback(QMeeGoSwitchCallback callback) +{ + QMeeGoGraphicsSystem::registerSwitchCallback(callback); +} + +#include "qmeegographicssystem.moc" diff --git a/src/plugins/graphicssystems/meego/qmeegographicssystem.h b/src/plugins/graphicssystems/meego/qmeegographicssystem.h index 27a4e7a..ecc85b2 100644 --- a/src/plugins/graphicssystems/meego/qmeegographicssystem.h +++ b/src/plugins/graphicssystems/meego/qmeegographicssystem.h @@ -47,6 +47,8 @@ #include #include +extern "C" typedef void (*QMeeGoSwitchCallback)(int type, const char *name); + class QMeeGoGraphicsSystem : public QGraphicsSystem { public: @@ -76,13 +78,22 @@ public: static void* createFenceSync(); static void destroyFenceSync(void* fenceSync); + static void switchToRaster(); + static void switchToMeeGo(); + static QString runningGraphicsSystemName(); + + static void registerSwitchCallback(QMeeGoSwitchCallback callback); + private: static bool meeGoRunning(); static EGLSurface getSurfaceForLiveTexturePixmap(QPixmap *pixmap); static void destroySurfaceForLiveTexturePixmap(QPixmapData* pmd); + static void triggerSwitchCallbacks(int type, const char *name); static bool surfaceWasCreated; - static QHash liveTexturePixmaps; + static QHash liveTexturePixmaps; + static QList switchCallbacks; + }; /* C api */ @@ -95,7 +106,7 @@ extern "C" { Q_DECL_EXPORT bool qt_meego_destroy_egl_shared_image(Qt::HANDLE handle); Q_DECL_EXPORT void qt_meego_set_surface_fixed_size(int width, int height); Q_DECL_EXPORT void qt_meego_set_surface_scaling(int x, int y, int width, int height); - Q_DECL_EXPORT void qt_meego_set_translucent(bool translucent); + Q_DECL_EXPORT void qt_meego_set_translucent(bool translucent); Q_DECL_EXPORT QPixmapData* qt_meego_pixmapdata_with_new_live_texture(int w, int h, QImage::Format format); Q_DECL_EXPORT QPixmapData* qt_meego_pixmapdata_from_live_texture_handle(Qt::HANDLE handle); Q_DECL_EXPORT QImage* qt_meego_live_texture_lock(QPixmap *pixmap, void *fenceSync); @@ -104,6 +115,9 @@ extern "C" { Q_DECL_EXPORT void* qt_meego_create_fence_sync(void); Q_DECL_EXPORT void qt_meego_destroy_fence_sync(void* fs); Q_DECL_EXPORT void qt_meego_invalidate_live_surfaces(void); + Q_DECL_EXPORT void qt_meego_switch_to_raster(void); + Q_DECL_EXPORT void qt_meego_switch_to_meego(void); + Q_DECL_EXPORT void qt_meego_register_switch_callback(QMeeGoSwitchCallback callback); } #endif diff --git a/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.cpp b/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.cpp index ac32995..3f39bda 100644 --- a/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.cpp +++ b/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.cpp @@ -47,7 +47,6 @@ #include #include #include "qmeegoruntime.h" -#include "qmeegoswitchevent.h" QString QMeeGoGraphicsSystemHelper::runningGraphicsSystemName() { @@ -77,46 +76,12 @@ bool QMeeGoGraphicsSystemHelper::isRunningRuntime() void QMeeGoGraphicsSystemHelper::switchToMeeGo() { - if (isRunningMeeGo()) - return; - - if (QApplicationPrivate::instance()->graphics_system_name != QLatin1String("runtime")) - qWarning("Can't switch to meego - switching only supported with 'runtime' graphics system."); - else { - QMeeGoSwitchEvent willSwitchEvent(QLatin1String("meego"), QMeeGoSwitchEvent::WillSwitch); - foreach (QWidget *widget, QApplication::topLevelWidgets()) - QCoreApplication::sendEvent(widget, &willSwitchEvent); - - QApplication *app = static_cast(QCoreApplication::instance()); - app->setGraphicsSystem(QLatin1String("meego")); - - QMeeGoSwitchEvent didSwitchEvent(QLatin1String("meego"), QMeeGoSwitchEvent::DidSwitch); - foreach (QWidget *widget, QApplication::topLevelWidgets()) - QCoreApplication::sendEvent(widget, &didSwitchEvent); - } + QMeeGoRuntime::switchToMeeGo(); } void QMeeGoGraphicsSystemHelper::switchToRaster() { - if (runningGraphicsSystemName() == QLatin1String("raster")) - return; - - if (QApplicationPrivate::instance()->graphics_system_name != QLatin1String("runtime")) - qWarning("Can't switch to raster - switching only supported with 'runtime' graphics system."); - else { - QMeeGoSwitchEvent willSwitchEvent(QLatin1String("raster"), QMeeGoSwitchEvent::WillSwitch); - foreach (QWidget *widget, QApplication::topLevelWidgets()) - QCoreApplication::sendEvent(widget, &willSwitchEvent); - - QApplication *app = static_cast(QCoreApplication::instance()); - app->setGraphicsSystem(QLatin1String("raster")); - - QMeeGoRuntime::invalidateLiveSurfaces(); - - QMeeGoSwitchEvent didSwitchEvent(QLatin1String("raster"), QMeeGoSwitchEvent::DidSwitch); - foreach (QWidget *widget, QApplication::topLevelWidgets()) - QCoreApplication::sendEvent(widget, &didSwitchEvent); - } + QMeeGoRuntime::switchToRaster(); } Qt::HANDLE QMeeGoGraphicsSystemHelper::imageToEGLSharedImage(const QImage &image) @@ -170,3 +135,8 @@ void QMeeGoGraphicsSystemHelper::setSwapBehavior(SwapMode mode) else if (mode == KillSwap) QGLWindowSurface::swapBehavior = QGLWindowSurface::KillSwap; } + +void QMeeGoGraphicsSystemHelper::enableSwitchEvents() +{ + QMeeGoRuntime::enableSwitchEvents(); +} diff --git a/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.h b/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.h index 5a3b57e..9e50652 100644 --- a/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.h +++ b/tools/qmeegographicssystemhelper/qmeegographicssystemhelper.h @@ -97,14 +97,21 @@ public: */ static bool isRunningRuntime(); + //! Enables the sending of QMeeGoSwitchEvent's when the graphicssystem switches. + /*! + An application that wishes to start receive QMeegoSwitchEvents must call this function. + */ + static void enableSwitchEvents(); + //! Switches to meego graphics system. /*! When running with the 'runtime' graphics system, sets the currently active system to 'meego'. The window surface and all the resources are automatically migrated to OpenGL. Will fail if the active graphics system is not 'runtime'. Calling this function will emit QMeeGoSwitchEvent to the top level widgets. - Two events will be emitted for each switch -- one before the switch (QMeeGoSwitchEvent::WillSwitch) - and one after the switch (QMeeGoSwitchEvent::DidSwitch). + If switch events are enabled, two events will be emitted for each switch -- + one before the switch (QMeeGoSwitchEvent::WillSwitch) and one after the + switch (QMeeGoSwitchEvent::DidSwitch). */ static void switchToMeeGo(); @@ -114,9 +121,9 @@ public: system to 'raster'. The window surface and the graphics resources (including the EGL shared image resources) are automatically migrated back to the CPU. All OpenGL resources (surface, context, cache, font cache) are automaticall anihilated. - Calling this function will emit QMeeGoSwitchEvent to the top level widgets. - Two events will be emitted for each switch -- one before the switch (QMeeGoSwitchEvent::WillSwitch) - and one after the switch (QMeeGoSwitchEvent::DidSwitch). + Calling this function will emit QMeeGoSwitchEvent to the top level widgets. If switch + events are enabled, two events will be emitted for each switch -- one before the + switch (QMeeGoSwitchEvent::WillSwitch) and one after the switch (QMeeGoSwitchEvent::DidSwitch). */ static void switchToRaster(); diff --git a/tools/qmeegographicssystemhelper/qmeegoruntime.cpp b/tools/qmeegographicssystemhelper/qmeegoruntime.cpp index 7c81d51..15f9cdf 100644 --- a/tools/qmeegographicssystemhelper/qmeegoruntime.cpp +++ b/tools/qmeegographicssystemhelper/qmeegoruntime.cpp @@ -41,6 +41,11 @@ #include "qmeegoruntime.h" +#include "qmeegoswitchevent.h" + +#include +#include + #include #include #include @@ -49,6 +54,7 @@ #define ENSURE_INITIALIZED {if (!initialized) initialize();} bool QMeeGoRuntime::initialized = false; +bool QMeeGoRuntime::switchEventsEnabled = false; typedef int (*QMeeGoImageToEglSharedImageFunc) (const QImage&); typedef QPixmapData* (*QMeeGoPixmapDataFromEglSharedImageFunc) (Qt::HANDLE handle, const QImage&); @@ -66,6 +72,9 @@ typedef Qt::HANDLE (*QMeeGoLiveTextureGetHandleFunc) (QPixmap*); typedef void* (*QMeeGoCreateFenceSyncFunc) (void); typedef void (*QMeeGoDestroyFenceSyncFunc) (void *fs); typedef void (*QMeeGoInvalidateLiveSurfacesFunc) (void); +typedef void (*QMeeGoSwitchToRasterFunc) (void); +typedef void (*QMeeGoSwitchToMeeGoFunc) (void); +typedef void (*QMeeGoRegisterSwitchCallbackFunc) (void (*callback)(int type, const char *name)); static QMeeGoImageToEglSharedImageFunc qt_meego_image_to_egl_shared_image = NULL; static QMeeGoPixmapDataFromEglSharedImageFunc qt_meego_pixmapdata_from_egl_shared_image = NULL; @@ -83,6 +92,16 @@ static QMeeGoLiveTextureGetHandleFunc qt_meego_live_texture_get_handle = NULL; static QMeeGoCreateFenceSyncFunc qt_meego_create_fence_sync = NULL; static QMeeGoDestroyFenceSyncFunc qt_meego_destroy_fence_sync = NULL; static QMeeGoInvalidateLiveSurfacesFunc qt_meego_invalidate_live_surfaces = NULL; +static QMeeGoSwitchToRasterFunc qt_meego_switch_to_raster = NULL; +static QMeeGoSwitchToMeeGoFunc qt_meego_switch_to_meego = NULL; +static QMeeGoRegisterSwitchCallbackFunc qt_meego_register_switch_callback = NULL; + +extern "C" void handleSwitch(int type, const char *name) +{ + QMeeGoSwitchEvent switchEvent((QLatin1String(name)), QMeeGoSwitchEvent::State(type)); + foreach (QWidget *widget, QApplication::topLevelWidgets()) + QCoreApplication::sendEvent(widget, &switchEvent); +} void QMeeGoRuntime::initialize() { @@ -112,13 +131,17 @@ void QMeeGoRuntime::initialize() qt_meego_create_fence_sync = (QMeeGoCreateFenceSyncFunc) library.resolve("qt_meego_create_fence_sync"); qt_meego_destroy_fence_sync = (QMeeGoDestroyFenceSyncFunc) library.resolve("qt_meego_destroy_fence_sync"); qt_meego_invalidate_live_surfaces = (QMeeGoInvalidateLiveSurfacesFunc) library.resolve("qt_meego_invalidate_live_surfaces"); + qt_meego_switch_to_raster = (QMeeGoSwitchToRasterFunc) library.resolve("qt_meego_switch_to_raster"); + qt_meego_switch_to_meego = (QMeeGoSwitchToMeeGoFunc) library.resolve("qt_meego_switch_to_meego"); + qt_meego_register_switch_callback = (QMeeGoRegisterSwitchCallbackFunc) library.resolve("qt_meego_register_switch_callback"); if (qt_meego_image_to_egl_shared_image && qt_meego_pixmapdata_from_egl_shared_image && qt_meego_pixmapdata_with_gl_texture && qt_meego_destroy_egl_shared_image && qt_meego_update_egl_shared_image_pixmap && qt_meego_set_surface_fixed_size && qt_meego_set_surface_scaling && qt_meego_set_translucent && qt_meego_pixmapdata_with_new_live_texture && qt_meego_pixmapdata_from_live_texture_handle && qt_meego_live_texture_lock && qt_meego_live_texture_release && qt_meego_live_texture_get_handle && - qt_meego_create_fence_sync && qt_meego_destroy_fence_sync && qt_meego_invalidate_live_surfaces) + qt_meego_create_fence_sync && qt_meego_destroy_fence_sync && qt_meego_invalidate_live_surfaces && + qt_meego_switch_to_raster && qt_meego_switch_to_meego && qt_meego_register_switch_callback) { qDebug("Successfully resolved MeeGo graphics system: %s %s\n", qPrintable(libraryPrivate->fileName), qPrintable(libraryPrivate->fullVersion)); } else { @@ -242,3 +265,27 @@ void QMeeGoRuntime::invalidateLiveSurfaces() Q_ASSERT(qt_meego_invalidate_live_surfaces); qt_meego_invalidate_live_surfaces(); } + +void QMeeGoRuntime::switchToRaster() +{ + ENSURE_INITIALIZED; + Q_ASSERT(qt_meego_switch_to_raster); + qt_meego_switch_to_raster(); +} + +void QMeeGoRuntime::switchToMeeGo() +{ + ENSURE_INITIALIZED; + Q_ASSERT(qt_meego_switch_to_meego); + qt_meego_switch_to_meego(); +} + +void QMeeGoRuntime::enableSwitchEvents() +{ + ENSURE_INITIALIZED; + if (!switchEventsEnabled) { + Q_ASSERT(qt_meego_register_switch_callback); + qt_meego_register_switch_callback(handleSwitch); + switchEventsEnabled = true; + } +} diff --git a/tools/qmeegographicssystemhelper/qmeegoruntime.h b/tools/qmeegographicssystemhelper/qmeegoruntime.h index b91efae..6279b4c 100644 --- a/tools/qmeegographicssystemhelper/qmeegoruntime.h +++ b/tools/qmeegographicssystemhelper/qmeegoruntime.h @@ -63,7 +63,11 @@ public: static void* createFenceSync(); static void destroyFenceSync(void *fs); static void invalidateLiveSurfaces(); + static void switchToRaster(); + static void switchToMeeGo(); + static void enableSwitchEvents(); private: static bool initialized; + static bool switchEventsEnabled; }; -- cgit v0.12