Improving Graphics Performance Android

One of the most common reasons to use the Android NDK is to leverage the OpenGL ES 1.1 and 2.0 native libraries, to perform complex math calculations that would benefit from native code, and speed up the porting process. Although you can use the Java APIs within the Android SDK to provide OpenGL ES support, some developers with core graphics libraries built in C or C++ might prefer to use the NDK. Here are some tips for developing and using graphics libraries provided with the Android NDK:

  • OpenGL ES 1.1 native libraries are guaranteed on Android 1.6 (API Level 4) and higher; OpenGL ES 2.0 native libraries are guaranteed on Android 2.0 (API Level 5) and higher. Make sure you include ”GLES2/gl2.h” and, optionally, include GLES2/gl2ext.h” to get access to the functions. They are named in the standard OpenGL way (for example, glClearColor).
  • Use the <uses-sdk> manifest tag to enforce the minimum SDK supported by the OpenGL ES version your application leverages.
  • Use the <uses-feature> manifest tag to specify which version of OpenGL ES your application leverages so that the Android Market can filter your application and provide it only to compatible devices.

For example, the following block of code is how the drawFrame() method from the OpenGL chapter would look in the NDK. You can find this code in the SimpleNDK project:

const GLfloat gVertices[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
}; void Java_com_androidbook_simplendk_NativeOpenGL2Activity_drawFrame (JNIEnv * env, jobject this, jint shaderProgram)
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, gVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

This is called from the onDrawFrame() method of our CustomRenderer class. Because this is the code that runs in a tight loop, it makes sense to implement it with native code. Of course, this particular implementation doesn’t benefit at all, but if we had done a bunch of complex math, transformations, and other algorithmically heavy code, it could possibly be faster. Only testing on actual handsets can determine for each case what is or isn’t faster, though.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics