From 4efa523375788a0e70797d5c83108bfd45712a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= Date: Thu, 16 Nov 2006 22:46:14 +0000 Subject: [PATCH] A simple attempt at a showfps effect. OpenGL-only, only lame graphs, still many TODO's. svn path=/branches/work/kwin_composite/; revision=605497 --- CMakeLists.txt | 1 + COMPOSITE_TODO | 2 +- effects.cpp | 2 + effects/showfps.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ effects/showfps.h | 44 +++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 effects/showfps.cpp create mode 100644 effects/showfps.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bdd0e0161..85db33cf59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ set(kwin_KDEINIT_SRCS effects/shiftworkspaceup.cpp effects/howto.cpp effects/dialogparent.cpp + effects/showfps.cpp ) kde4_automoc(kwin ${kwin_KDEINIT_SRCS}) diff --git a/COMPOSITE_TODO b/COMPOSITE_TODO index 18843fffc4..c4fc66823d 100644 --- a/COMPOSITE_TODO +++ b/COMPOSITE_TODO @@ -234,7 +234,7 @@ Effects TODO + - needs support for new window types from the current draft of the EWMH WM spec + - needs patching Qt, netwm* classes in kdecore -+ showfps effect +/ showfps effect - for debugging, just shows transparent fps in some corner - just painting the number in paintScreen() should do, with glPushMatrix() and glLoadIdentity() to avoid all transformations diff --git a/effects.cpp b/effects.cpp index da0afd6ac3..2ff4276079 100644 --- a/effects.cpp +++ b/effects.cpp @@ -21,6 +21,7 @@ License. See the file "COPYING" for the exact licensing terms. #include "effects/scalein.h" #include "effects/shakymove.h" #include "effects/shiftworkspaceup.h" +#include "effects/showfps.h" namespace KWinInternal { @@ -89,6 +90,7 @@ EffectsHandler::EffectsHandler( Workspace* ws ) { if( !compositing()) return; +// effects.append( new ShowFpsEffect( ws )); // effects.append( new HowtoEffect ); // effects.append( new MakeTransparentEffect ); // effects.append( new ShakyMoveEffect ); diff --git a/effects/showfps.cpp b/effects/showfps.cpp new file mode 100644 index 0000000000..ccada94457 --- /dev/null +++ b/effects/showfps.cpp @@ -0,0 +1,114 @@ +/***************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2006 Lubos Lunak + +You can Freely distribute this program under the GNU General Public +License. See the file "COPYING" for the exact licensing terms. +******************************************************************/ + +#include "showfps.h" + +#include + +#include + +namespace KWinInternal +{ + +ShowFpsEffect::ShowFpsEffect( Workspace* ws ) + : wspace( ws ) + , paints_pos( 0 ) + , frames_pos( 0 ) + { + for( int i = 0; + i < NUM_PAINTS; + ++i ) + paints[ i ] = 0; + for( int i = 0; + i < MAX_FPS; + ++i ) + frames[ i ] = 0; + } + +void ShowFpsEffect::prePaintScreen( int* mask, QRegion* region, int time ) + { + *mask &= ~Scene::PAINT_SCREEN_REGION; + if( time == 0 ) + ; // TODO optimized away + t.start(); + frames[ frames_pos ] = t.second() * 1000 + t.msec(); + if( ++frames_pos == MAX_FPS ) + frames_pos = 0; + effects->prePaintScreen( mask, region, time ); + } + +void ShowFpsEffect::paintScreen( int mask, QRegion region, ScreenPaintData& data ) + { + effects->paintScreen( mask, region, data ); + glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT ); + glEnable( GL_BLEND ); + glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); + const double ALPHA = 0.5; + const int MAX_TIME = 100; + int x = 100; + int y = 100; + int FPS_WIDTH = 10; + glColor4f( 1, 1, 1, ALPHA ); // white + glBegin( GL_QUADS ); + glVertex2i( x, y ); + glVertex2i( x + NUM_PAINTS + FPS_WIDTH, y ); + glVertex2i( x + NUM_PAINTS + FPS_WIDTH, y + MAX_TIME ); + glVertex2i( x, y + MAX_TIME ); + glEnd(); + y += MAX_TIME; + glBegin( GL_LINES ); + for( int i = 0; + i < NUM_PAINTS; + ++i ) + { + int value = paints[ ( i + paints_pos ) % NUM_PAINTS ]; + if( value > MAX_TIME ) + value = MAX_TIME; // limit + if( value <= 10 ) + glColor4f( 0, 1, 0, ALPHA ); // green + else if( value <= 20 ) + glColor4f( 1, 1, 0, ALPHA ); // yellow + else if( value <= 50 ) + glColor4f( 1, 0, 0, ALPHA ); // red + else + glColor4f( 0, 0, 0, ALPHA ); // black + glVertex2i( x + i, y ); + glVertex2i( x + i, y - value ); + } + glEnd(); + int fps = 0; + for( int i = 0; + i < MAX_FPS; + ++i ) + if( t.second() * 1000 + t.msec() - frames[ i ] < 1000 ) + ++fps; // count all frames in the last second + if( fps > MAX_TIME ) + fps = MAX_TIME; // keep it the same height (TODO change later) + glBegin( GL_QUADS ); + x += NUM_PAINTS; // paint to the right + glColor4f( 0, 0, 1, ALPHA ); // blue + glVertex2i( x, y ); + glVertex2i( x + FPS_WIDTH, y ); + glVertex2i( x + FPS_WIDTH, y - fps ); + glVertex2i( x, y - fps ); + glEnd(); + glPopAttrib(); + } + +void ShowFpsEffect::postPaintScreen() + { + effects->postPaintScreen(); + paints[ paints_pos ] = t.elapsed(); + if( ++paints_pos == NUM_PAINTS ) + paints_pos = 0; + wspace->addDamageFull(); + } + +} // namespace diff --git a/effects/showfps.h b/effects/showfps.h new file mode 100644 index 0000000000..c662c0552d --- /dev/null +++ b/effects/showfps.h @@ -0,0 +1,44 @@ +/***************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2006 Lubos Lunak + +You can Freely distribute this program under the GNU General Public +License. See the file "COPYING" for the exact licensing terms. +******************************************************************/ + +// TODO MIT or some other licence, perhaps move to some lib + +#ifndef KWIN_SHOWFPS_H +#define KWIN_SHOWFPS_H + +#include + +#include + +namespace KWinInternal +{ + +class ShowFpsEffect + : public Effect + { + public: + ShowFpsEffect( Workspace* ws ); + virtual void prePaintScreen( int* mask, QRegion* region, int time ); + virtual void paintScreen( int mask, QRegion region, ScreenPaintData& data ); + virtual void postPaintScreen(); + private: + Workspace* wspace; + QTime t; + enum { NUM_PAINTS = 100 }; // remember time needed to paint this many paints + int paints[ NUM_PAINTS ]; // time needed to paint + int paints_pos; // position in the queue + enum { MAX_FPS = 200 }; + int frames[ MAX_FPS ]; // (sec*1000+msec) of the time the frame was done + int frames_pos; // position in the queue + }; + +} // namespace + +#endif