Jump to content

  • Log In with Google      Sign In   
  • Create Account

Android + OpenGL ES...double buffered? threaded? gameloop?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Holland   Members   -  Reputation: 155

Like
0Likes
Like

Posted 30 April 2012 - 05:11 PM

3 questions...but first, here's my setup

My activity class:
...useless stuff...
import android.opengl.GLSurfaceView;

public class MyApp extends Activity
{
	private GLSurfaceView mSurfaceView;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); //fullscreen
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		glSurfaceView = new GLSurfaceView(this);
		glSurfaceView.setRenderer(new MyRenderer());
		setContentView(glSurfaceView);
	}
}

My Renderer class
...unimportant stuff...
import android.opengl.GLSurfaceView.Renderer;
public class MyRenderer implements Renderer
{
	@Override
	public void onDrawFrame(GL10 gl)
	{
		...iterate over draw objects and call .draw() on them...
	}
}
...more unimportant stuff...

1) Is OpenGL ES double buffered by default?
Right now, I've created a Renderer class that extends GLSurfaceView.Renderer. From that, I've got an override for onDrawFrame...like you can see above. Is this already double buffered? I've done a little bit of OpenGL coding for PC and I remember having to specifically tell it to swap buffers...

2) Given my current setup, is the renderer already on its own thread? Is that something handled by OpenGL ES (or the GLViewPort class)?

3) Right now I'm doing everything inside of the onDrawScene(GL10 gl) function. Is there a better way of creating a game loop? Seems like (assuming rendering is currently on its own thread), onDrawScene should just iterate of my drawable objects and draw them, but I should probably have a loop in another thread somewhere polling input, updating drawable object positions, etc. Any thoughts?

Thanks, in advance, for the help!

[Edit: spelling error]

Edited by Holland, 30 April 2012 - 05:18 PM.


Sponsor:

#2 Mihai Moldovan   Members   -  Reputation: 127

Like
0Likes
Like

Posted 30 April 2012 - 06:15 PM

AFAIK Android uses EGL. So it all depends on what the Java crap is doing under the hood to initialize your EGL content.

You could either dig into the Android codebase (ugly, full of hacks) or just ditch Java and take control by doing all the initialization with your own code in C++.

#3 mame82   Members   -  Reputation: 102

Like
1Likes
Like

Posted 06 May 2012 - 05:22 PM

1) OpenGL ES isn't double buffered by default, but GLSurfaceView uses double buffering in your configutration (eglSwapBuffer is called automatically). you can change this behaviour if you call setRenderMode(RENDERMODE_WHEN_DIRTY), now you can force swapbuffer yourself by calling requestRender() (Look at GLSurfaceView on the reference page)

Tripple Buffering is also possible, look for the video about OpenGL developing from the last google IO. The guy explains how to use triple buffering for benchmarking purposes (with double buffering most devices have a fixed output of 30 / 60 fps)

2) The renderer is indeed on its own thread, GLSurfaceView starts the Thread after setRenderer() get's called. I've rewritten this class for Wallpaper-rendering with OpenGL ES 2.0 and took a deeper look. GLSurfaceView does all the EGL things with the more complicated android stuff for your, handling the RenderThread, Managnig the EGLContext (important because of the different behaviours on different devices, when other activities need a lock on the EGLContext - not trivial to manage this on your own)

3) I've choosen the way to build up two extra Threads for my current developments (one for logic, one for animations, several others are launched by android like SensorThread for polling accelerometer). In general I could say these things:
- don't do heavy work on androids UIThread, which isn't the case if you do it in onDrawFram() because now its done by the renderThread
- if you do heavy work like IO-operations use a ASyncTask
- its hard to implement extra threads in the java manner (new Thread and calling run() ) because you have to take care of the whole synchronizing and more care on
bringing your Threads to idle-state if your activity is paused (or you app is a battery drainer). This isn't the preferable way as long as you haven't performance problems while using the render-thread

Avoid using native code, except you have to (ready c / c++ implemantations) - the dalvik vm is really fast, especially since it uses a JIT-Compiler (since API-Level 8 I think).
You still have to call your c/c++ written wrapper methods through JAVA (with JNI / NDK), nothing else the Android-API is doing So you don't have a performance increase, except your logic is handled here and you make use of special ARM CPU features like the NEON-platform or similar things

Look a christmas 3D on android market, if your device can handle it you'll see 2 things
first: all resource are loaded in the render-thread, not with a async task (very long load time, user can't see anything)
second: camera handling, matrix operations, particle calculation are also handled in the render task, but with less performance penalty than thread handling -->
the need of an extra thread depends on what logic work you have to do




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS