Keep track of whether or not anything was painted each frame and avoid the

extra effort of copying the back buffer to the screen if nothing actually
changed.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2322 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-03-25 19:06:54 +00:00
parent 0dd5966c9a
commit b66e0d2584
4 changed files with 56 additions and 21 deletions
@@ -1,5 +1,5 @@
//
// $Id: FrameManager.java,v 1.32 2003/01/15 02:21:07 shaper Exp $
// $Id: FrameManager.java,v 1.33 2003/03/25 19:06:54 mdb Exp $
package com.threerings.media;
@@ -422,12 +422,18 @@ public class FrameManager
// paint our frame participants (which want to be handled
// specially)
_participantPaintOp.setGraphics(_bgfx);
_participantPaintOp.init(_bgfx);
_participants.apply(_participantPaintOp);
boolean ppart = _participantPaintOp.paintedSomething();
// repaint any widgets that have declared they need to be
// repainted since the last tick
_remgr.paintComponents(_bgfx, this);
boolean pcomp = _remgr.paintComponents(_bgfx, this);
// if we didn't paint anything, get the fork out of dodge
if (!(ppart || pcomp)) {
return;
}
if (_displayPerf && _perfLabel != null) {
// render the current performance status
@@ -683,9 +689,19 @@ public class FrameManager
* Sets the graphics context to which the frame participants
* render themselves.
*/
public void setGraphics (Graphics g)
public void init (Graphics g)
{
_g = g;
_painted = 0;
}
/**
* Returns true if we painted at least one component in our last
* application.
*/
public boolean paintedSomething ()
{
return (_painted > 0);
}
// documentation inherited
@@ -693,7 +709,7 @@ public class FrameManager
{
FrameParticipant part = (FrameParticipant)observer;
Component pcomp = part.getComponent();
if (pcomp == null) {
if (pcomp == null || !part.needsPaint()) {
return true;
}
@@ -726,6 +742,7 @@ public class FrameManager
_g.translate(_bounds.x, _bounds.y);
pcomp.paint(_g);
_g.translate(-_bounds.x, -_bounds.y);
_painted++;
} catch (Throwable t) {
String ptos = StringUtil.safeToString(part);
@@ -754,6 +771,9 @@ public class FrameManager
/** The graphics context to which the participants render. */
protected Graphics _g;
/** The number of participants that were actually painted. */
protected int _painted;
/** A handy rectangle that we reuse time and again to avoid having
* to instantiate a new rectangle in the midst of the core
* rendering loop. */
@@ -1,5 +1,5 @@
//
// $Id: FrameParticipant.java,v 1.2 2002/06/18 22:25:33 mdb Exp $
// $Id: FrameParticipant.java,v 1.3 2003/03/25 19:06:54 mdb Exp $
package com.threerings.media;
@@ -20,6 +20,13 @@ public interface FrameParticipant
*/
public void tick (long tickStamp);
/**
* Called immediately prior to {@link #getComponent} and then {@link
* Component#paint} on said component, to determine whether or not
* this frame participant needs to be painted.
*/
public boolean needsPaint ();
/**
* If a frame participant wishes also to be actively rendered every
* frame rather than use passive rendering (which for Swing, at least,
@@ -1,5 +1,5 @@
//
// $Id: FrameRepaintManager.java,v 1.16 2002/12/09 05:00:48 shaper Exp $
// $Id: FrameRepaintManager.java,v 1.17 2003/03/25 19:06:54 mdb Exp $
package com.threerings.media;
@@ -216,13 +216,15 @@ public class FrameRepaintManager extends RepaintManager
/**
* Paints the components that have become dirty since the last tick.
*
* @return true if any components were painted.
*/
public void paintComponents (Graphics g, FrameManager fmgr)
public boolean paintComponents (Graphics g, FrameManager fmgr)
{
synchronized (this) {
// exit now if there are no dirty rectangles to paint
if (_dirty.isEmpty()) {
return;
return false;
}
// otherwise, swap our hashmaps
@@ -416,6 +418,8 @@ public class FrameRepaintManager extends RepaintManager
// clear out the mapping of dirty components
_spare.clear();
return true;
}
/**
+16 -12
View File
@@ -1,5 +1,5 @@
//
// $Id: MediaPanel.java,v 1.28 2003/02/04 03:11:43 mdb Exp $
// $Id: MediaPanel.java,v 1.29 2003/03/25 19:06:54 mdb Exp $
package com.threerings.media;
@@ -223,9 +223,21 @@ public class MediaPanel extends JComponent
{
}
/**
* Returns this component, as we want to be painted with the tick.
*/
// documentation inherited from interface
public boolean needsPaint ()
{
// compute our average dirty regions per tick
if (_tick++ == 99) {
_tick = 0;
int dirty = IntListUtil.sum(_dirty);
Arrays.fill(_dirty, 0);
_dirtyPerTick = (float)dirty/100;
}
return (!_tickPaintPending || _remgr.haveDirtyRegions());
}
// documentation inherited from interface
public Component getComponent ()
{
return this;
@@ -271,14 +283,6 @@ public class MediaPanel extends JComponent
_tickPaintPending = false;
}
// compute our average dirty regions per tick
if (_tick++ == 99) {
_tick = 0;
int dirty = IntListUtil.sum(_dirty);
Arrays.fill(_dirty, 0);
_dirtyPerTick = (float)dirty/100;
}
// if we have no invalid rects, there's no need to repaint
if (!_remgr.haveDirtyRegions()) {
return;