title: Why is OpenGL rendering so slow with SDL2 on Android?
body: <p>I am porting a C++ 2D game to Android with the help of the NDK and the SDL; and I use OpenGL to render the sprites. The performances are quite disappointing.</p>
  
  <p>After some investigations, I have found that the bottleneck is in the OpenGL calls during the rendering procedure. <em>Wait a second!</em> Before pasting the classic answer telling not change GL states, not to bind already bound textures and so on, please read the following.</p>
  
  <p>Actually, even if I display a single 364x353 rgba texture in a middle of a black screen, it happens that the rendering takes almost 20 ms. The procedure can be summarized like this:</p>
  
  <p><strong>Edit</strong> The code below used to call <code>glFlush()</code>. As thokra pointed out in the comments, this is not necessary. Nevertheless, removing it did not improve the performances.</p>
  
  <pre><code>glClear( GL_COLOR_BUFFER_BIT );
  
  glBindTexture( the_texture );
  glEnable(GL_BLEND);
  
  glEnableClientState( GL_COLOR_ARRAY );
  glColorPointer( 4, GL_FLOAT, 0, colors );
  
  glEnableClientState( GL_VERTEX_ARRAY );
  glVertexPointer( 2, GL_FLOAT, 0, positions );
  
  glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  glTexCoordPointer( 2, GL_FLOAT, 0, texture_positions );
  
  glDrawArrays( GL_TRIANGLE_FAN, 0, vertex_count );
  
  glDisableClientState( GL_TEXTURE_COORD_ARRAY );
  glDisableClientState( GL_VERTEX_ARRAY );
  glDisableClientState( GL_COLOR_ARRAY );
  glDisable(GL_BLEND);
  
  //glFlush();
  SDL_GL_SwapWindow( window );
  </code></pre>
  
  <p>The execution of the last two lines of this function (flush and swap) is very irregular in duration. It can take from 4 to 12 ms!</p>
  
  <p>The best hints I could find on the web are <a href="https://stackoverflow.com/questions/5165470/tips-for-improving-opengl-es-fill-rate-on-android">this question on StackOverflow</a> and this <a href="https://groups.google.com/forum/#!topic/android-developers/dXkcKsv8fJM" rel="nofollow noreferrer">thread on Google Groups</a>.</p>
  
  <p>The answers to the questions on StackOverflow are of no help. According to the author of the question:</p>
  
  <blockquote>
    <p>clearing vs not clearing frame with glClear doesn't affect fps for me. Neither does enabling/disabling blending.</p>
  </blockquote>
  
  <p>and</p>
  
  <blockquote>
    <p>[…] just want to see if using compressed textures improves fill rate. It seems it doesn't […]</p>
  </blockquote>
  
  <p>The Google Groups thread is very similar to my problem but it ended when the author gave up:</p>
  
  <blockquote>
    <p>So basically, there's no way to draw a 480x854
    image on screen with OpenGL at 60fps because of the texel reading
    bottleneck.</p>
  </blockquote>
  
  <p>Seriously, no way? Why the call to both <code>glFlush()</code> and <code>SDL_GL_SwapWindow()</code> takes so long? And why is it so irregular? Can't I do anything about it?</p>
  
  <p>I did a quick search in SDL2's source code and <a href="http://hg.libsdl.org/SDL/file/54c06b0f65d9/src/core/android/SDL_android.c#l390" rel="nofollow noreferrer">found</a> that <code>SDL_GL_SwapWindow()</code> uses the Java Native Interface to call a Java method which will do the work. Can it be the cause of the irregularity? What can I do about this?</p>