简体   繁体   中英

3 Android Threads, how to structure

I'm building an openGL ES 2 game, with Java on Android.

I currently have two threads. The Android UI and openGL.

As far as I understand it, to get the best performance, I need to have another thread for game logic, so the openGL thread can render as fast as possible.

This would leave me with 3. The Android UI, openGL and Game Logic. (Let's call them UI, OGL and GL.)

Correct me if I'm wrong:

UI starts, makes OGL, and GL. OGL and GL run, querying UI for input. GL sends draw calls to the queue in OGL.

There are two cases here. If GL takes longer to loop than OGL, the next loop of OGL needs to wait for GL to finish before starting it's next loop, as to avoid only rendering half of the calls for last frame. If OGL takes longer than GL, GL needs to wait until OGL is done to start again/send more draw calls.

To accomplish this:

When GL is done with it's loop, it checks OGL to see if it's done drawing the current queue of draw calls. If so, it starts both GL and OGL's next loop. If not, it waits.

When OGL is done, it checks GL. If GL is done, it starts both loops again. If not, it waits for GL, which when done will check OGL, and start both again.

Sorry if this is a little confusingly worded..

To illistrate my idea:

diagram http://img840.imageshack.us/img840/2816/stackoverflowthredpictu.jpg

To clarify me question: -Will laying things out this way work? --If not, what would work better?

-What specific subjects should I look up to accomplish this? --ie I don't know how to make threads or send information between them.

This is my first time asking a question here, hope I did it correctly. :) Thanks!

-Will laying things out this way work? --If not, what would work better?

Yes, but this strategy may create more complexity than necessary. It's easy to deadlock two threads when they have to wait for one another.

I think you could use RENDERMODE_WHEN_DIRTY :

 setRenderMode(RENDERMODE_WHEN_DIRTY);

In that mode, your renderer will render a frame only when (1) the surface is created or (2) you call requestRender() . So, your game logic thread would call queueEvent() (or similar) to push drawing calls to the renderer, and then call requestRender() at the end of the game logic loop.

If you still find that the game logic thread needs to wait for the renderer to finish, you could create a Semaphore in the game logic thread that will block it until the renderer signals (by calling release() on the Semaphore ) that it's finished rendering the frame.

Disclaimer: I'm not a game developer, but hopefully this gives you some paths to explore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM