Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.6k views
in Technique[技术] by (71.8m points)

Fast Screen Flicker while NOT drawing on Android OpenGL

I wanted to save battery life time. My app only needs to be drawn sometimes. So I added this code to my Renderer in the onDraw method:

boolean dirty = true;

public void onDrawFrame(GL10 arg0) {
    if (!dirty) return;
    dirty = false;

    ..... draw images ....
}

So my app only gets drawn when I want it. But what happens is that if I dont draw my app on every frame it flickers really fast. It looks like it will be drawn every 2. frame or so and in all the other frames only a black screen will be drawn.

I know that i could set the render mode to RENDERMODE_WHEN_DIRTY. But I dont want to create another thread to check if its dirty or not.

My question is why does it flicker? i dont call any methods or GLES20 calls before i do my check: if (!dirty) return; and i am sure that the boolean dirty doesnt change and is always false except for the first frame.

EDIT:

I changed my code to this:

int dirty = 0;

public void onDrawFrame(GL10 arg0) {
    if (dirty > 1) return;
    dirty++;

    ..... draw images ....
}

This stops the flickering! Looks like you have to draw atleast 2 times so you dont have this wierd screen flickering. Anyways I will now try to use the more clean way and create a thread that calls requestRender() when i want to draw something and set my render mode to RENDERMODE_WHEN_DIRTY

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would guess that windowing system will still assume you've updated the frame, and put the buffer to screen - i.e. how is it supposed to know that you've not drawn anything? What ends up on screen will be whatever happens to be in that memory buffer (typically the frame from N-2 frames ago).

Use a separate thread to sleep the rendering until the state changes to dirty, so you only send frames where you have actually rendered something to the OS. This way you not only save GPU load, you also stop the CPU thread spinning around wasting power doing nothing polling the dirty state.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...