Flicker

Hi,

I’ve got a JOGL project that is displaying an animated, flickering box. Unfortunately, sometimes (typically about 30 seconds into the program) the flickering stops for a second or so (it should change about every 1/8 of a second) before picking up again.

specifying -verbose:gc indicates that it isn’t the garbage collection. What else could cause this? How can I check these causes?

Many thanks!

You’re giving us very little info :slight_smile:

Basically the animation is interrupted, and then continues, and it’s not the GC… oh well! :slight_smile:

Maybe you’re using System.nanoTime(), which is known to go back in time every once in a while, due to CPUs throttling their frequency.

Other than that, we need much much more information, like a self contained (!) testcase, so that we can run it, or if you’re lucky: look through it and point you are potential bugs!

Fair enough.

Interestingly, your nanoTime comment might be it! I was using it because I wanted accuracy better than currentTimeMillis, but if it’s an issue, maybe that’s a good trade-off (no flicker for worse accuracy). Is there a better timer to use to get millisecond accuracy (not just precision) than nanoTime?

Anyway, here is the basic loop for the image presentation:


      :
      while( !m_stop_running && stimulus_iterator.hasNext() )
      {
         TimedStimulus item = stimulus_iterator.next();

         // Load image into ByteBuffer
         //
         loadStimulus( item );
         
         // Determine how long to wait before display
         //
         // Time to present - (elapsed time since last stimulus) - (typical time to render)
         //
         sleep_time = item.getTime() +
               ( m_start - System.nanoTime() ) - 
               ( m_average_image_render_time );
         
         // If we have plenty of time, then sleep until we're ready
         //
         if( sleep_time > 0 )
         {
            try
            {
               Thread.sleep( sleep_time / Constants.NANOSECONDS_PER_MILLISECOND );
            }
            catch( InterruptedException ex )
            {
               continue;
            }
         }
         
         // Skip the image if it's over 5 seconds late
         //
         else if( sleep_time < -5 * Constants.NANOSECONDS_PER_SECOND )
         {
            log.warn( "Image display late " + sleep_time + " nsec. Skipping." );
            continue;
         }
         
         // Present the stimulus
         //
         try
         {
            m_gl_canvas.display();
         }
         catch( GLException glx )
         {
            log.warn( "Error displaying image: " + glx.getLocalizedMessage() );
         }
         
      } // end loop over timed stimulus items in the task
      :

The display method looks like this:


      :
      // Draw to the buffer as RGB bytes
      // m_buffer populated in the loadImage() call in main loop
      //
      GL gl = drawable.getGL();
      gl.glPushAttrib( GL.GL_COLOR_BUFFER_BIT );
      gl.glDrawPixels( 800, 600, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, m_buffer );
      gl.glPopAttrib(); 
      gl.glFlush();
      :

Thanks for your help!

This might be your problem; the JVM shares CPU with the OS, so you should always call sleep() even it’s with a low value, otherwise the OS gets a backlog of tasks which may take a while to clear.

I disagree with you. Calling sleep even a vanishingly small time decreases the frame rate noticeably and is not necessary. I removed it in TUER and the frame rate is twice better. I have almost no problem of timing, I use currentTimeMillis for the moment.

I disagree with your disagreement! :wink:

My tests showed a 10% performance drop without sleep() - maybe it’s because I’m using software rendering…

please sign some Non-Agreement Disclosure