Re: Offscreen rendering - performance issue

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Offscreen rendering - performance issue

chinnaswamyg
Hi All,
         I am developing a stand-alone CAD app for a numerical analysis using java2D which works fine. In my program, I am not using any animator class like (Java) Timer to display the graphics contents in a loop; instead, whenever there is change in  graphics objects / contents, the image is recomposed and drawn;

When the size of my graphics content ( amesh) becomes large, I faced the performance issue with java2D and began to use JOGL where offscreen rendering procedure is used to compose and write to a BufferedImage.   First I wrote a method to create a GLOffscreenAutoDrawable object  and the same is called everytime, whenever there is a change in the graphics content of the application as shown in the second code block below.

This also works fine but it is not faster. Is this because GLOffscreenAutoDrawable object is created every time whenever I tried to compose my java2D graphics?  or is there any other way like make the drawable as blank and redraw on the same area.  Please help to resolve the performance issue.


Many thanks in advance.

 
     
        // method for creating GLOffscreenAutoDrawable
        public static  GLOffscreenAutoDrawable getDrawable(boolean performance, int width, int height) {
                GLOffscreenAutoDrawable drawable  = null;
                        if(performance) {  // if performance is true, then it well generate an offscreen drawable
                        GLProfile glp = GLProfile.getDefault();
                        GLCapabilities caps = new GLCapabilities(glp);
                        caps.setHardwareAccelerated(true);
                        caps.setOnscreen(false);
                        GLDrawableFactory factory = GLDrawableFactory.getFactory(glp);

                        // TODO: IF I USE OFFSCREEN DRAWABLE, THIS DOESN'T DRAW ANYTHING
                        drawable = factory.createOffscreenAutoDrawable(null,caps,null,width,height);
                        drawable.display();
                        // drawable.getContext().makeCurrent();
                        }
                         return drawable;
                }

 
                //
                // Draw function in the java2D app to call GLOffscreenAutoDrawable drawable
                //
                public void drawForGL(Graphics2D g2d) {
                                Stroke stkOld = g2d.getStroke();
                                g2d.setColor(this.drawColor);
                                point2D ptBuf = sShape.getLeftTopCornerPt();
                                int x = (int) ptBuf.getX();
                                int y = (int) ptBuf.getY();
                                int width = sShape.getWidthInt();
                                int height = sShape.getHeightInt();
                               
                                // Preparing gl2 Object for further drawings.
                                GLOffscreenAutoDrawable drawable = CadListsnerConstants.getDrawable(performance,  width,  height);
                                drawable.addGLEventListener(new OffscreenJOGL());
                                 GL2 gl = drawable.getGL().getGL2();
                        gl.glViewport(0, 0, width, height);
                        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                        glRGB rgb = commonMeths.getGLRGB(drawColor);
                        gl.glClearColor(rgb.getGLRed(), rgb.getRGB().getGLGreen(), rgb.getRGB().getGLBlue(), 0.5f);    
                        gl.glClear(GL.GL_COLOR_BUFFER_BIT);  
                        gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);
                       
                /*
                 *  seperateout the layer objects from cobj5 and draw them using openGL and
                 */
                cobj5.remLayerShapes() ;
                 for(int i=0; i < shapes.size(); i++) {
                LayerNew l1 = (LayerNew)shapes.get(i);
                l1.drawGL(gl, g2d);
                }
                 gl.glDisableClientState(GL2.GL_VERTEX_ARRAY);
                BufferedImage im = new AWTGLReadBufferUtil(drawable.getGLProfile(), true).readPixelsToBufferedImage(drawable.getGL(), 0, 0, width, height, true);
                g2d.drawImage(im, null,  x, y );      
}



Chinnaswamy
Singapore










Reply | Threaded
Open this post in threaded view
|

Re: Offscreen rendering - performance issue

gouessej
Administrator
Hello

It's not efficient because you create a new BufferedImage at each call instead of using a single com.jogamp.opengl.util.GLPixelBuffer and because you use Java2D to render.

Personally, I would switch to a completely different approach. Use a com.jogamp.opengl.util.awt.Overlay and call createGraphics() to get a Graphics2D instance to draw your Java2D shapes. Use a canvas or window (drawable) provided by JOGL in onscreen rendering, put this drawable into the overlay, use an animator. The performance will be much better that way.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Offscreen rendering - performance issue

Chinnaswamy
Many thanks, Gouessej.

I will try with your suggested method and hope to overcome the performance issue.  However,  I am using a  number of mouse listeners in my Java2D app  and can they be still used as it is by choosing GLCanvas.  

Thanks,
Chinnaswamy
Reply | Threaded
Open this post in threaded view
|

Re: Offscreen rendering - performance issue

gouessej
Administrator
Yes, why not? GLCanvas is an heavyweight AWT canvas after all, it should work, it depends on what you do in these listeners.

Your previous approach was a dead end because the performance mainly depended on Java2D and not on JOGL. You could have used the internal NIO buffer of the BufferedImage to transfer the content from JOGL to Java2D or you could have used the source code of GLJPanel as a source of inspiration but it would have been a lot more cumbersome than my suggestion.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Offscreen rendering - performance issue

gouessej
Administrator
In reply to this post by Chinnaswamy
By the way, rewriting all your rendering code with JOGL would help you to get rid of the overlay and Java2D. Maybe this project can help you:
https://github.com/brandonborkholder/glg2d
Julien Gouesse | Personal blog | Website