multithreading - Android- How defer main MessageQueue events during custom GLSurfaceView.Renderer.onDrawFrame(GL10 gl)? -
multithreading - Android- How defer main MessageQueue events during custom GLSurfaceView.Renderer.onDrawFrame(GL10 gl)? -
situation: android
class glrenderer implements glsurfaceview.renderer .. void ondrawframe(gl10 gl) {..} class mainglsurfaceview extends glsurfaceview .. setrenderer(new glrenderer()); class mainactivity .. .. boolean ontouchevent(motionevent event) {..}
mainactivity.ontouchevent receives , processes events, of alter state used ondrawframe(gl).
q 1: there way "put message queue on hold" until ondrawframe returns?
note: ondrawframe might take ~1/3 sec on slow phone. if necessary, can alter if doesn't have info needs, can start fetching in background, homecoming promptly, , draw new frame on later draw request (triggered timer).
q 1b: perhaps events interrupting draw, because of i'm doing fetch data. can events interrupt @ moment (in middle of ondrawframe), or when custom ondrawframe logic makes (system?) calls?
a 1b: unfortunately, breakpoint caught event interrupting in middle of computation (a vm "init" of new instance of little class used hold temporary value required, "interruptible"; java code might do). need cope interrupting events, can't side-step them.
q 2: or improve examine incoming messages, , somehow decide ones should handled immediately, , ?do what? other messages, process them after ondrawframe returns?
q 3: i've made effort @ q 2, putting messages on internal queue. tried processing them @ end of ondrawframe method. worked okay until message tried open confirmation dialog. result: runtimeexception: can't create handler within thread has not called looper.prepare(). yeah, didn't think should doing way. can somehow shove messages on main message queue?
(i didn't want create yet thread, @ end of ondrawframe tried "new handler().postdelayed(new runnable() .." within of going events. oops - has same problem - can't create handler on thread ondrawframe running on.)
so basic thought seek way not pull rug out under current draw frame. i'd rather not have create event-triggered logic work on 1 set of data, "freeze" info (copy it), draw frame can work on frozen set.
q&a's looked @ before asking this:
message queue in android
this talks creating looper , handler, including link article. might adapt create handler attached main thread's looper. instead of having inject messagequeue, need pass secondary queue handler, , start running. i'm on wrong thread @ time want start handler, not sure how proceed. hmm, maybe create custom event, somehow trigger on main thread?
how pause activity?
shows how utilize flag pause worker thread. since have multiple types of events wish defer, instead of approach, easier hold (copies of) events on separate queue. need know how "inject" them main messagequeue. i'm trying avoid creating thread, minimize scheme resources.
how pause thread's message queue in android?
alternate approaches (not using looper) when creating one's own thread, e.g. worker thread. doesn't help situation, ui events coming in existing looper.
and in case there different way tackle this:
isn't situation everyone uses glsurfaceview rendering encounter eventually?
is there illustration of robust way deal gl drawing , asynchronous gui events?
the final piece solution "q 3":
public class mainactivity ... // phone call this, if not on ui thread. public static void processqueuedeventsonuithread() { seek { runnable runnable = new runnable() { @override public void run() { ... process deferred ui events, have stored on private queue ... } }; mainactivity.mmainactivity.runonuithread(runnable); } grab (exception e) { log.e("mainactivity", "processqueuedeventsonuithread", e); } }
the lastly statement in glrenderer.ondrawframe() now
mainactivity.processqueuedeventsonuithread();
the exceptions no longer occur, if processed events cause dialog window (with own handler) open. activity.runonuithread(runnable)
essential step.
android multithreading event-handling drawing
Comments
Post a Comment