Low FPS with Android SurfaceView Support

I have some difficulties with my frame rate using SurfaceView. I am doing typical material that I found in some textbooks (they all said the same thing), but I can’t achieve a decent frame rate on my Galaxy S-samsung S (old, i9000).

Here's the code i for the thread loop. FPS is initialized at level 30.

@Override public void run() { long ticksPS = 1000/FPS; long startTime; long sleepTime; //fps checker long contms=0; long lasttimecheck = System.currentTimeMillis(); int fps=0; while (running) { long time = System.currentTimeMillis(); if(contms>1000) { Log.v("FPS",String.valueOf(fps)); contms=time-lasttimecheck; fps=1; } else { fps++; contms+=time-lasttimecheck; } lasttimecheck = time; Canvas c = null; startTime =time; try { c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { view.onDraw(c); } } finally { if (c != null) { view.getHolder().unlockCanvasAndPost(c); } } sleepTime = ticksPS-(System.currentTimeMillis() - startTime); try { if (sleepTime > 10) sleep(sleepTime); else { Log.w("LOWFPS",String.valueOf(contms)); sleep(10); } } catch (Exception e) {} } } 

In surfaceView, I initialize the holder with holder.setFormat (PixelFormat.RGBA_8888); but I don’t know if I need to do something with bitmaps to avoid processor uselessness (I save bitmaps in local variables and then draw them). The game is simple, it should work much faster.

The frame rate is quite random, sometimes it works with smoothing, sometimes it does not work, but always under 30FPS.

Any ideas ???

EDIT WITH LONELINE

 @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bg, 0, 0, null); //1 stars.draw(canvas,dx,dy); //2 if(playing.on()) reactors.draw_reaccio(canvas,dx,dy); //3 gotes.draw(canvas,dx,dy); //4 reactors.draw(canvas,dx,dy); //5 sg.draw(canvas); //6 sr.draw(canvas); //7 eraser.draw(canvas); //8 playing.draw(canvas); //9 opcions.draw(canvas); //10 } 

1) Drawing a background (480x800) 2) it is a class that contains a list of basic getHeight () objects (stars) with its coordinates (x, y) and an identifier with an associated image (about 9 images of different stars) 3) it draws n * 2 a circle, one with a bay, and the other with a stroke on a related object (about 20 or so) 4) He draws the main object of the game, small drops with animation. There are 9 different kinds of drops, and each of them has 5 related animation images (should I place 5 images in 1, maybe?) 5) the same as drops, but without animation From 6 to 10) it doesn’t matter, he just draws an image

I assume that the slowness is due to: (2) due to the number of stars (4) associated with the animation, the witch changes every 2-3 frames to another image and maybe this is too much for memory, I think I should combine all the images in just 1.

Frame rate around 20-22 FPS with S. Galaxy S i9000

+4
source share
2 answers

If you turn off the drawing, how many frames will you achieve? Just to check how the display pipeline works.

I tried following create 5 raster images in size of 720 x 480 pixels in RGB565 format to display them on SurfaceView from a cycle similar to yours. The only difference is that I had “prepared bitmaps” and did not draw them in a loop. This is what has been achieved on the Nexus-S phone.

FPS: 55 (I tried to start the cycle so quickly, without adjustment)

CPU utilization: 85%

This is when I decided to display my SurfaceView from JNI :) !!

Carry out a similar experiment and see how much your device can throttle without drawing operations. If it looks good, then you can configure whether you can fit your drawing operations into the budget.

+3
source

Things to keep in mind: how long have you been gaining a routine?

If you have a problem with it running under 30 FPS, I would change my mind if it sleeps. This is not an exact thing and can sleep less or more according to the API, so I would remove it for now and let it work as quickly as possible.

I agree with the above, please write the drawing code or comment on his call to the drawing procedure and see what FPS you use.

In general, in terms of sampling, you can see that it is trembling in value if it measures more than 1 second.

Many back ends only work on even divisors of 60. So if you do a lot of things, maybe one or two additional calls will take you from 60/1 = 60 frames per second to 60/2 = 30 frames per second.

0
source

Source: https://habr.com/ru/post/1383421/


All Articles