Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kwin / scene.cpp
blobbab07ccaa0641a8fdc686b64df73998d6cadc63e
1 /********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
5 Copyright (C) 2006 Lubos Lunak <l.lunak@kde.org>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
22 (NOTE: The compositing code is work in progress. As such this design
23 documentation may get outdated in some areas.)
25 The base class for compositing, implementing shared functionality
26 between the OpenGL and XRender backends.
28 Design:
30 When compositing is turned on, XComposite extension is used to redirect
31 drawing of windows to pixmaps and XDamage extension is used to get informed
32 about damage (changes) to window contents. This code is mostly in composite.cpp .
34 Workspace::performCompositing() starts one painting pass. Painting is done
35 by painting the screen, which in turn paints every window. Painting can be affected
36 using effects, which are chained. E.g. painting a screen means that actually
37 paintScreen() of the first effect is called, which possibly does modifications
38 and calls next effect's paintScreen() and so on, until Scene::finalPaintScreen()
39 is called.
41 There are 3 phases of every paint (not necessarily done together):
42 The pre-paint phase, the paint phase and the post-paint phase.
44 The pre-paint phase is used to find out about how the painting will be actually
45 done (i.e. what the effects will do). For example when only a part of the screen
46 needs to be updated and no effect will do any transformation it is possible to use
47 an optimized paint function. How the painting will be done is controlled
48 by the mask argument, see PAINT_WINDOW_* and PAINT_SCREEN_* flags in scene.h .
49 For example an effect that decides to paint a normal windows as translucent
50 will need to modify the mask in its prePaintWindow() to include
51 the PAINT_WINDOW_TRANSLUCENT flag. The paintWindow() function will then get
52 the mask with this flag turned on and will also paint using transparency.
54 The paint pass does the actual painting, based on the information collected
55 using the pre-paint pass. After running through the effects' paintScreen()
56 either paintGenericScreen() or optimized paintSimpleScreen() are called.
57 Those call paintWindow() on windows (not necessarily all), possibly using
58 clipping to optimize performance and calling paintWindow() first with only
59 PAINT_WINDOW_OPAQUE to paint the opaque parts and then later
60 with PAINT_WINDOW_TRANSLUCENT to paint the transparent parts. Function
61 paintWindow() again goes through effects' paintWindow() until
62 finalPaintWindow() is called, which calls the window's performPaint() to
63 do the actual painting.
65 The post-paint can be used for cleanups and is also used for scheduling
66 repaints during the next painting pass for animations. Effects wanting to
67 repaint certain parts can manually damage them during post-paint and repaint
68 of these parts will be done during the next paint pass.
72 #include "scene.h"
74 #include <X11/extensions/shape.h>
76 #include "client.h"
77 #include "deleted.h"
78 #include "effects.h"
80 namespace KWin
83 //****************************************
84 // Scene
85 //****************************************
87 Scene* scene;
89 Scene::Scene( Workspace* ws )
90 : wspace( ws ),
91 has_waitSync( false )
95 Scene::~Scene()
99 // returns mask and possibly modified region
100 void Scene::paintScreen( int* mask, QRegion* region )
102 *mask = ( *region == QRegion( 0, 0, displayWidth(), displayHeight()))
103 ? 0 : PAINT_SCREEN_REGION;
104 updateTimeDiff();
105 // preparation step
106 static_cast<EffectsHandlerImpl*>(effects)->startPaint();
107 ScreenPrePaintData pdata;
108 pdata.mask = *mask;
109 pdata.paint = *region;
110 effects->prePaintScreen( pdata, time_diff );
111 *mask = pdata.mask;
112 *region = pdata.paint;
113 if( *mask & ( PAINT_SCREEN_TRANSFORMED | PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS ))
114 { // Region painting is not possible with transformations,
115 // because screen damage doesn't match transformed positions.
116 *mask &= ~PAINT_SCREEN_REGION;
117 *region = infiniteRegion();
119 else if( *mask & PAINT_SCREEN_REGION )
120 { // make sure not to go outside visible screen
121 *region &= QRegion( 0, 0, displayWidth(), displayHeight());
123 else
124 { // whole screen, not transformed, force region to be full
125 *region = QRegion( 0, 0, displayWidth(), displayHeight());
127 painted_region = *region;
128 if( *mask & PAINT_SCREEN_BACKGROUND_FIRST )
129 paintBackground( *region );
130 ScreenPaintData data;
131 effects->paintScreen( *mask, *region, data );
132 foreach( Window* w, stacking_order )
133 effects->postPaintWindow( effectWindow( w ));
134 effects->postPaintScreen();
135 *region |= painted_region;
136 // make sure not to go outside of the screen area
137 *region &= QRegion( 0, 0, displayWidth(), displayHeight());
138 // make sure all clipping is restored
139 Q_ASSERT( !PaintClipper::clip());
142 // Compute time since the last painting pass.
143 void Scene::updateTimeDiff()
145 if( last_time.isNull())
147 // Painting has been idle (optimized out) for some time,
148 // which means time_diff would be huge and would break animations.
149 // Simply set it to one (zero would mean no change at all and could
150 // cause problems).
151 time_diff = 1;
153 else
154 time_diff = last_time.elapsed();
155 if( time_diff < 0 ) // check time rollback
156 time_diff = 1;
157 last_time.start();;
160 // Painting pass is optimized away.
161 void Scene::idle()
163 // Don't break time since last paint for the next pass.
164 last_time = QTime();
167 // the function that'll be eventually called by paintScreen() above
168 void Scene::finalPaintScreen( int mask, QRegion region, ScreenPaintData& data )
170 if( mask & ( PAINT_SCREEN_TRANSFORMED | PAINT_SCREEN_WITH_TRANSFORMED_WINDOWS ))
171 paintGenericScreen( mask, data );
172 else
173 paintSimpleScreen( mask, region );
176 // The generic painting code that can handle even transformations.
177 // It simply paints bottom-to-top.
178 void Scene::paintGenericScreen( int orig_mask, ScreenPaintData )
180 if( !( orig_mask & PAINT_SCREEN_BACKGROUND_FIRST ))
181 paintBackground( infiniteRegion());
182 QList< Phase2Data > phase2;
183 foreach( Window* w, stacking_order ) // bottom to top
185 WindowPrePaintData data;
186 data.mask = orig_mask | ( w->isOpaque() ? PAINT_WINDOW_OPAQUE : PAINT_WINDOW_TRANSLUCENT );
187 w->resetPaintingEnabled();
188 data.paint = infiniteRegion(); // no clipping, so doesn't really matter
189 data.clip = QRegion();
190 data.quads = w->buildQuads();
191 // preparation step
192 effects->prePaintWindow( effectWindow( w ), data, time_diff );
193 #ifndef NDEBUG
194 foreach( WindowQuad q, data.quads )
195 if( q.isTransformed())
196 kFatal( 1212 ) << "Pre-paint calls are not allowed to transform quads!" ;
197 #endif
198 if( !w->isPaintingEnabled())
199 continue;
200 phase2.append( Phase2Data( w, infiniteRegion(), data.clip, data.mask, data.quads ));
203 foreach( Phase2Data d, phase2 )
204 paintWindow( d.window, d.mask, d.region, d.quads );
207 // The optimized case without any transformations at all.
208 // It can paint only the requested region and can use clipping
209 // to reduce painting and improve performance.
210 void Scene::paintSimpleScreen( int orig_mask, QRegion region )
212 // TODO PAINT_WINDOW_* flags don't belong here, that's why it's in the assert,
213 // perhaps the two enums should be separated
214 assert(( orig_mask & ( PAINT_WINDOW_TRANSFORMED | PAINT_SCREEN_TRANSFORMED
215 | PAINT_WINDOW_TRANSLUCENT | PAINT_WINDOW_OPAQUE )) == 0 );
216 QHash< Window*, Phase2Data > phase2data;
217 // Draw each opaque window top to bottom, subtracting the bounding rect of
218 // each window from the clip region after it's been drawn.
219 for( int i = stacking_order.count() - 1; // top to bottom
220 i >= 0;
221 --i )
223 Window* w = stacking_order[ i ];
224 WindowPrePaintData data;
225 data.mask = orig_mask | ( w->isOpaque() ? PAINT_WINDOW_OPAQUE : PAINT_WINDOW_TRANSLUCENT );
226 w->resetPaintingEnabled();
227 data.paint = region;
228 data.clip = w->isOpaque() ? w->shape().translated( w->x(), w->y()) : QRegion();
229 data.quads = w->buildQuads();
230 // preparation step
231 effects->prePaintWindow( effectWindow( w ), data, time_diff );
232 #ifndef NDEBUG
233 foreach( WindowQuad q, data.quads )
234 if( q.isTransformed())
235 kFatal( 1212 ) << "Pre-paint calls are not allowed to transform quads!" ;
236 #endif
237 if( !w->isPaintingEnabled())
238 continue;
239 if( data.paint != region ) // prepaint added area to draw
240 painted_region |= data.paint; // make sure it makes it to the screen
241 // Schedule the window for painting
242 phase2data[w] = Phase2Data( w, data.paint, data.clip, data.mask, data.quads );
244 // Do the actual painting
245 // First opaque windows, top to bottom
246 // This also calculates correct paint regions for windows, also taking
247 // care of clipping
248 QRegion allclips;
249 for( int i = stacking_order.count() - 1; i >= 0; --i )
251 Window* w = stacking_order[ i ];
252 if( !phase2data.contains( w ))
253 continue;
254 Phase2Data d = phase2data[w];
255 // Calculate correct paint region and take the clip region into account
256 d.region = painted_region - allclips;
257 allclips |= d.clip;
258 if( d.mask & PAINT_WINDOW_TRANSLUCENT )
260 // For translucent windows, the paint region must contain the
261 // entire painted area, except areas clipped by opaque windows
262 // above the translucent window
263 phase2data[w].region = d.region;
265 else
267 // Paint the opaque window
268 paintWindow( d.window, d.mask, d.region, d.quads );
271 // Fill any areas of the root window not covered by windows
272 if( !( orig_mask & PAINT_SCREEN_BACKGROUND_FIRST ))
273 paintBackground( painted_region - allclips );
274 // Now walk the list bottom to top, drawing translucent windows.
275 for( int i = 0; i < stacking_order.count(); i++ )
277 Window* w = stacking_order[ i ];
278 if( !phase2data.contains( w ))
279 continue;
280 Phase2Data d = phase2data[w];
281 if( d.mask & PAINT_WINDOW_TRANSLUCENT )
282 paintWindow( d.window, d.mask, d.region, d.quads );
286 void Scene::paintWindow( Window* w, int mask, QRegion region, WindowQuadList quads )
288 // no painting outside visible screen (and no transformations)
289 region &= QRect( 0, 0, displayWidth(), displayHeight());
290 if( region.isEmpty()) // completely clipped
291 return;
293 WindowPaintData data( w->window()->effectWindow());
294 data.quads = quads;
295 effects->paintWindow( effectWindow( w ), mask, region, data );
298 // the function that'll be eventually called by paintWindow() above
299 void Scene::finalPaintWindow( EffectWindowImpl* w, int mask, QRegion region, WindowPaintData& data )
301 effects->drawWindow( w, mask, region, data );
304 // will be eventually called from drawWindow()
305 void Scene::finalDrawWindow( EffectWindowImpl* w, int mask, QRegion region, WindowPaintData& data )
307 w->sceneWindow()->performPaint( mask, region, data );
310 //****************************************
311 // Scene::Window
312 //****************************************
314 Scene::Window::Window( Toplevel * c )
315 : toplevel( c )
316 , filter( ImageFilterFast )
317 , disable_painting( 0 )
318 , shape_valid( false )
322 Scene::Window::~Window()
326 void Scene::Window::discardShape()
328 // it is created on-demand and cached, simply
329 // reset the flag
330 shape_valid = false;
333 // Find out the shape of the window using the XShape extension
334 // or if shape is not set then simply it's the window geometry.
335 QRegion Scene::Window::shape() const
337 if( !shape_valid )
339 Client* c = dynamic_cast< Client* >( toplevel );
340 if( toplevel->shape() || ( c != NULL && !c->mask().isEmpty()))
342 int count, order;
343 XRectangle* rects = XShapeGetRectangles( display(), toplevel->frameId(),
344 ShapeBounding, &count, &order );
345 if(rects)
347 shape_region = QRegion();
348 for( int i = 0;
349 i < count;
350 ++i )
351 shape_region += QRegion( rects[ i ].x, rects[ i ].y,
352 rects[ i ].width, rects[ i ].height );
353 XFree(rects);
355 else
356 shape_region = QRegion( 0, 0, width(), height());
358 else
359 shape_region = QRegion( 0, 0, width(), height());
360 shape_valid = true;
362 return shape_region;
365 bool Scene::Window::isVisible() const
367 if( dynamic_cast< Deleted* >( toplevel ) != NULL )
368 return false;
369 if( !toplevel->isOnCurrentDesktop())
370 return false;
371 if( Client* c = dynamic_cast< Client* >( toplevel ))
372 return c->isShown( true );
373 return true; // Unmanaged is always visible
374 // TODO there may be transformations, so ignore this for now
375 return !toplevel->geometry()
376 .intersected( QRect( 0, 0, displayWidth(), displayHeight()))
377 .isEmpty();
380 bool Scene::Window::isOpaque() const
382 return toplevel->opacity() == 1.0 && !toplevel->hasAlpha();
385 bool Scene::Window::isPaintingEnabled() const
387 return !disable_painting;
390 void Scene::Window::resetPaintingEnabled()
392 disable_painting = 0;
393 if( dynamic_cast< Deleted* >( toplevel ) != NULL )
394 disable_painting |= PAINT_DISABLED_BY_DELETE;
395 if( !toplevel->isOnCurrentDesktop())
396 disable_painting |= PAINT_DISABLED_BY_DESKTOP;
397 if( Client* c = dynamic_cast< Client* >( toplevel ))
399 if( c->isMinimized() )
400 disable_painting |= PAINT_DISABLED_BY_MINIMIZE;
401 if( c->isHiddenInternal())
402 disable_painting |= PAINT_DISABLED;
406 void Scene::Window::enablePainting( int reason )
408 disable_painting &= ~reason;
411 void Scene::Window::disablePainting( int reason )
413 disable_painting |= reason;
416 WindowQuadList Scene::Window::buildQuads() const
418 if( toplevel->clientPos() == QPoint( 0, 0 ) && toplevel->clientSize() == toplevel->size())
419 return makeQuads( WindowQuadContents, shape()); // has no decoration
420 QRegion contents = shape() & QRect( toplevel->clientPos(), toplevel->clientSize());
421 QRegion decoration = shape() - contents;
422 WindowQuadList ret = makeQuads( WindowQuadContents, contents );
423 ret += makeQuads( WindowQuadDecoration, decoration );
424 return ret;
427 WindowQuadList Scene::Window::makeQuads( WindowQuadType type, const QRegion& reg ) const
429 WindowQuadList ret;
430 foreach( QRect r, reg.rects())
432 WindowQuad quad( type );
433 // TODO asi mam spatne pravy dolni roh - bud tady, nebo v jinych castech
434 quad[ 0 ] = WindowVertex( r.x(), r.y(), r.x(), r.y());
435 quad[ 1 ] = WindowVertex( r.x() + r.width(), r.y(), r.x() + r.width(), r.y());
436 quad[ 2 ] = WindowVertex( r.x() + r.width(), r.y() + r.height(), r.x() + r.width(), r.y() + r.height());
437 quad[ 3 ] = WindowVertex( r.x(), r.y() + r.height(), r.x(), r.y() + r.height());
438 ret.append( quad );
440 return ret;
443 } // namespace