在android opengl es 2.0上进行帧率断断续续

时间:2013-03-26 21:30:57

标签: java android opengl-es

最近我开始开发一些简单的opengl es 2.0 android应用程序,我的动画速度有问题。平均fps不断等于60(每100帧测量一次),但有时动画会在很短的时间内出现断断续续的情况。即使呈现单个三角形也会发生这种情况。当动画继续进行时,我没有进行任何分配,并且在动画期间logcat中没有GC事件。

在渲染循环中执行的代码:

public void draw( GLFloatBuffer positionBuffer, int positionBufferOffset, int trianglesCount )
{
    GLES20.glUseProgram( activeGLProgram.glProgramId );

    // Set mvpMatrixValue
    int u_MvpMatrixLocation = GLES20.glGetUniformLocation( activeGLProgram.glProgramId, U_MVP_MATRIX_NAME );
    GLES20.glUniformMatrix4fv( u_MvpMatrixLocation, 1, false, mvpMatrix, 0 );

    // Set vertex position stream
    int a_VertexPositionLocation = GLES20.glGetAttribLocation( activeGLProgram.glProgramId, A_VERTEX_POSITION_NAME );
    GLES20.glBindBuffer( GLES20.GL_ARRAY_BUFFER, positionBuffer.glBufferId );
    GLES20.glEnableVertexAttribArray( a_VertexPositionLocation );

    GLES20.glVertexAttribPointer( a_VertexPositionLocation, POSITION_COORDINATES_PER_VERTEX,
                                  GLES20.GL_FLOAT, false,
                                  0, positionBufferOffset * GLUtilities.BYTES_PER_FLOAT );

    // Draw shape
    GLES20.glDrawArrays( GLES20.GL_TRIANGLES, 0, trianglesCount * 3 );

    // Clear state
    GLES20.glDisableVertexAttribArray( a_VertexPositionLocation );
    GLES20.glBindBuffer( GLES20.GL_ARRAY_BUFFER, 0 );
}

其余代码是简单的GLSurfaceView和GLSurfaceView.Renderer

测量

调试输出格式为:

D / FRAMERATE:过去100帧的平均fps

D / FRAMERATE:最后100帧的最慢帧渲染时间

D / FRAMERATE:最后100帧的最快帧渲染时间

测量Galaxy S II:

D/FRAMERATE:(24052): 59.018566
D/FRAMERATE:(24052): maxTimeDiff = 77ms <----
D/FRAMERATE:(24052): minTimeDiff = 3ms
D/FRAMERATE:(24052): 59.900166
D/FRAMERATE:(24052): maxTimeDiff = 24ms
D/FRAMERATE:(24052): minTimeDiff = 10ms
D/FRAMERATE:(24052): 59.780804
D/FRAMERATE:(24052): maxTimeDiff = 28ms
D/FRAMERATE:(24052): minTimeDiff = 10ms
D/FRAMERATE:(24052): 59.4882
D/FRAMERATE:(24052): maxTimeDiff = 32ms
D/FRAMERATE:(24052): minTimeDiff = 3ms
D/FRAMERATE:(24052): 59.840427
D/FRAMERATE:(24052): maxTimeDiff = 24ms
D/FRAMERATE:(24052): minTimeDiff = 9ms
D/FRAMERATE:(24052): 59.920105
D/FRAMERATE:(24052): maxTimeDiff = 22ms
D/FRAMERATE:(24052): minTimeDiff = 12ms
.
.
.
D/FRAMERATE:(24052): 59.116573
D/FRAMERATE:(24052): maxTimeDiff = 68 <--- Here it happend again
D/FRAMERATE:(24052): minTimeDiff = 8

看起来问题并没有出现在HTC Desire Z上:

D/FRAMERATE: (28788): maxTimeDiff = 20ms
D/FRAMERATE: (28788): minTimeDiff = 15ms
D/FRAMERATE: (28788): 58.27506
D/FRAMERATE: (28788): maxTimeDiff = 22ms
D/FRAMERATE: (28788): minTimeDiff = 12ms
D/FRAMERATE: (28788): 58.27506
D/FRAMERATE: (28788): maxTimeDiff = 22ms
D/FRAMERATE: (28788): minTimeDiff = 14ms
D/FRAMERATE: (28788): 58.27506
D/FRAMERATE: (28788): maxTimeDiff = 20ms
D/FRAMERATE: (28788): minTimeDiff = 14ms

除了编写本地ndk代码之外,是否有人可以帮助寻找解决方案?

0 个答案:

没有答案