Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "SurfaceView" is slower than a custom "view"?

I am developing an application that is almost completed. But I have some problems with graphics:

Initially I used a custom "View", but had trouble handling the time in different resolutions of devices. Also the animation was not smooth.

So there reading some examples and reviews, I decided to use "SurfaceView". Now the animation is smooth, but everything is slower than with a "View". I do not know why, since I did not change practically nothing when migrated from "View" to "SurfaceView":

In "View" (+90 fps):

@Override
public void run(){      
    for(;;){
        if(gameState == PLAYING)
            updatePhisic();                             

        updateSprites();

        this.postInvalidate();
        try{
            Thread.sleep(10);
        }catch(InterruptedException e){}
    }
}

In "Surfaceview" ("Lienzo" is the main class <"Surfaceview">) (Maybe 30 fps).

@Override
public void run() {
    Canvas canvas;
while(running){
    canvas = null;
    try{
    canvas = holder.lockCanvas(null);
    synchronized(holder){
        lienzo.updatePhisic();
        lienzo.draw(canvas);
    }
    }
        finally{    
        if(canvas != null)
        holder.unlockCanvasAndPost(canvas);
    }
}
}

Physics and graphics drawing is the same in both cases.

Why?, I mean, "SurfaceView" was the apparent solution to everything, and now "OpenGL" is the solution.

Thanks.

like image 221
txuy Avatar asked Oct 21 '22 07:10

txuy


1 Answers

I too have experienced a significant FPS increase when switching an application from using SurfaceView to a single View subclass. My belief is that this is due to hardware acceleration.

Switching away from SurfaceView is something I had to do ever since the serious bug with SurfaceView was introduced in Android 4.3. See https://code.google.com/p/android/issues/detail?id=58385. During a discussion about this on the Android Developers mailing list, Romain Guy actually suggested that using a normal View might be faster anyway, due to hardware acceleration. This turned out to be true - at least on devices I'm developing on (HTC One X and the old Nexus 7). It could be true, however, that the graphics are more susceptible to 'choppy' animation. For me, this FPS increase sweetened the initial pain and frustration of having to deal with the SurfaceView bug and refactor my application to not use SurfaceView.

like image 135
Trevor Avatar answered Oct 23 '22 03:10

Trevor