Reducing Calls to OpenGl Esjni Methods

The first suspects are the many OpenGL ES calls we issue per frame when we render the model for each Bob. First of all, we have four matrix operations per Bob. If we don't need rotation or scaling, we can bring that down to two calls. Here are the FPS numbers for each device when we only use glLoadIdentity() and glTranslatef() in the inner loop:

Hero:

12-10 04:57:49.610: DEBUG/FPSCounter(766): fps: 27

Hero:

12-10 04:57:49.610: DEBUG/FPSCounter(766): fps: 27

12-

-10

04:

: 57 :

:49.

.610:

DEBUG/FPSCounter(766):

fps:

27

12-

-10

04:

: 57 :

50

.650:

DEBUG/FPSCounter(766):

fps:

28

12-

-10

04:

: 57 :

50

.650:

DEBUG/FPSCounter(766):

fps:

28

12-

-10

04:

: 57 :

51

530:

DEBUG/dalvikvm(766): GC freed 22

Droid:

12

-10

05:

: 08 :

: 38.

.604:

DEBUG/FPSCounter(1702):

: fps:

56

12

10

05:

: 08 :

:39.

.620:

DEBUG/FPSCounter(1702):

: fps:

57

12

10

05:

: 08 :

40

.628:

DEBUG/FPSCounter(1702):

: fps:

58

12

10

05:

: 08 :

41

.644:

DEBUG/FPSCounter(1702):

: fps:

57

Nexus

One:

12

10

04:

: 58:

01

.277:

DEBUG/FPSCounter(2509):

: fps:

54

12

10

04:

: 58:

02

.287:

DEBUG/FPSCounter(2509)

: fps:

54

12

10

04:

: 58:

03

.307:

DEBUG/FPSCounter(2509)

: fps:

55

12

10

04:

: 58:

04

.317:

DEBUG/FPSCounter(2509)

: fps:

55

Well, it improved the performance on the Hero quite a bit, and the Droid and Nexus One also benefited a little from removing the two matrix operations. Of course, there's a little bit of cheating involved: if we need to rotate and scale our Bobs, there's no way around issuing those two additional calls. However, when all we do is 2D rendering, there's a neat little trick we can use that will get rid of all matrix operations (we'll look into this in the next chapter).

OpenGL ES is a C API provided to Java via a JNI wrapper. This means that any OpenGL ES method we call has to cross that JNI wrapper to call the actual C native function. This is somewhat costly on earlier Android versions, but has gotten better with more recent versions. As shown, the impact is not all that huge, especially if the actual operations take up more time than issuing the call itself.

0 0

Post a comment