brandonborkholder / glg2d

Graphics2D wrapper for JOGL
77 stars 31 forks source link

Image cache cleaning #16

Closed dyorgio closed 11 years ago

dyorgio commented 11 years ago

Sometimes image cache shouldn't be cleared... I use your nice API to easylly program 2d graphics on openGL with java (because I know a lot of Graphics2D and I like it :)), if I remove cache.clear() invoke from AbstractImageHelper.setG2D() my FPS increases (from 130 to 450, macbook retina :P).

I don't understand why you create a own WeakHashMap, Java 1.4+ already have one.

Anyways... Maybe cache cleaning could be a parameter, RenderingHint...

dyorgio commented 11 years ago

Possible solution for image update problem:

on GLG2DImageHelper:

void updateImage(Image image, int x, int y, int width, int height, Color bgColor, Buffer buffer);

on AbstractImageHelper:

public void updateImage(Image image, int x, int y, int width, int height, Color bgColor, Buffer buffer) {
        Texture texture = cache.get(image);
        if (texture != null) {
            begin(texture, null, bgColor);
            g2d.getGLContext().getGL().glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, image.getWidth(null), image.getHeight(null), GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, buffer);
            drawImage(image, x, y, width, height, bgColor, null);
            end(texture);
        }
    }

The code is compatible with GL2 and ES2 (glTexSubImage2D), I use for video update on textures..

brandonborkholder commented 11 years ago

My custom WeakHashMap is a holdover from when I had some additional functionality, I think I can expunge it.

There are two reasons I've stuck with clearing the cache on every paint.

But I do like your idea of using a RenderingHint to enable developers to keep the cache from being cleared after each draw. Another option might be a multi-level cache, where Images that do have all the data are kept across draws and the cache has some maximum size.

dyorgio commented 11 years ago

Is a big problem a new texture generation for every video frame :(

brandonborkholder commented 11 years ago

I fixed this with 831147a02dd93b6e258e49bf92aa877e7f642334. The new default cache policy is to never clear the cache. You can control this with the new GLG2DRenderingHints class. So if you draw the same icon each frame, it won't generate a new texture each time.