[PATCH] enable double buffering on GtkCanvasPeer

Scott Gilbertson Onsite sgilbertson-onsite@cogeco.ca
Fri Jul 18 18:51:00 GMT 2003


From: "graydon hoare" <graydon@redhat.com>
> ... if it's
> seriously the case that most AWT programmers will write this:
>
> class myComponent extends Canvas
> {
>         void paint (Graphics g)
>         {
>         Rectangle clip = g.getBounds ();
>         Image im = this.createImage (clip.getWidth (),
>                                     clip.getHeight ());
>         Graphics buf = im.getGraphics ();
>         buf.draw( ... );
>         im.flush ();
>         }
> }
>
> when they want double buffering. it seems completely loopy to me, but
> maybe that's how it's done....

The normal approach is a little less loopy:
 1. createImage and getGraphics are only called if the window size changes
(or once at startup)
 2. the in-memory image is only manipulated if something visible has changed
 3. the in-memory image is copied to the on-screen graphics (using
drawImage) every paint
(as done in
http://gcc.gnu.org/ml/java-patches/2003-q2/msg00512/DoubleBuffer.java)

#1 is accomplished by inspecting the window size and in-memory buffer fields
in the paint method.

#2 can be done several ways.  A simple technique is to set a "dirty" flag in
the repaint method.  Another is to have a "set dirty flag" method and call
it only when something visible changes.  For example, in a text field, you'd
set the dirty flag when the text, font, color or alignment changed.  The
paint method re-draws onto the in-memory image only if the flag is set, and
clears the flag.

#3 is simply a matter of calling Graphics.drawImage from paint.

> ...in that case I could recognize the incoming "special"
> GtkOffscreenImage using instanceof, and just pop the Gdk drawable
> stack.

I'm guessing you mean you'd do this in drawImage? If so, that sounds like
what I did in XGraphics.drawImage for the xlib peers.

> my main interest is in keeping RENDER drawing on the server, moving
> between server-side pixmaps, not doing a pixel-copy back and forth
> with the client.

My xlib patch uses pixmaps for the off-screen images (XOffScreenImage
contains a Pixmap), and you render to them using server-side functionality.
The drawImage method is also server-side-only (it calls XCopyArea).  The
same trick should work with gtk.

If you decide to implement this approach in the gtk peers you could, of
course, use the program from my patch as a test case.  You'd also need to
incorporate my patch, unless it's been committed before you start, because
it includes a change to java.awt.Component.createImage (int width, int
height).



More information about the Java-patches mailing list