UI引擎机制系列(一)GLRender线程处理接口设计

GLSurfaceView的线程队列,有以下缺陷:
1)在GLSurfaceView转入后台,放进此GLSurfaceView消息队列的代码依然会立即执行,但执行时可能生成纹理失败。原因是没有gl环境.

myGLSurfaceView.queueEvent(
    new Runnable() {
        // This method will be called on the rendering thread:
        public void run() {
              doSomeThing();
        }
    }
);

2)没有去除重复消息的功能。
3)没有实现每帧执行一个消息功能。


设计两个消息处理接口:插入消息队列,插入消息集合。都满足需求一:GL消失停止时,停止运行消息。
1)插入消息队列接口queueEvent(Runnable r)
兼容原GLSurfaceView消息队列,放入此消息队列的消息,在onDraw之前依次全部执行。
使用案例:按键和鼠标事件,放入GLRender线程后,要求顺次全部执行。应该使用此消息队列。
2)插入消息集合接口setEvent(String tag,Runnable r)
满足需求二、三:a.去除重复消息,b.单帧执行。
放入此消息集合的消息,依标签(tag)为身份唯一标示,重复消息仅保留最新,去除旧消息。每次onDraw之前,仅执行一条消息。随机执行,不保证先放入先执行。
使用案例:a.状态栏标示改变事件,当有多条消息时,仅最后一条消息有效,可以使用此消息集合。b.连续生成海报等耗时操作,需要在Render线程,但不希望同一帧内太多任务阻塞GLRender线程。可以使用此消息集合。


消息队列图示:



消息集合图示:




实现代码:

1)定义消息队列、实现插入消息队列:

public class HimalayaView extends GLSurfaceView {
…
    //仿照原GLSurfaceView写线程队列。
    private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
    @Override
    public void queueEvent(Runnable r) {
        if (r == null) {
            throw new IllegalArgumentException("r must not be null");
        }
        synchronized(HimalayaView.this) {
            mEventQueue.add(r);
            HimalayaView.this.notifyAll();
        }
}
…
}

2)定义消息集合、实现插入消息集合:
public class HimalayaView extends GLSurfaceView {
…
    //事件Set
    private Map<String,Runnable> mEventSet = new HashMap<String,Runnable>();
    
    public void setEvent(String tag,Runnable r) {//如果不需要自建的线程队列,仅仅注释掉此方法。
        if (r == null) {
            throw new IllegalArgumentException("r must not be null");
        }
        synchronized(mEventSet) {
        	mEventSet.put(tag, r);
        	mEventSet.notifyAll();
        }
    }
…
}

3)执行消息队列和消息集合
	public class HimalayaRenderer implements GLSurfaceView.Renderer {
		public void onDrawFrame(final GL10 gl) {
…
			//线程事件队列的逻辑
			Runnable event = null;
			while (true) {
			 synchronized (mEventQueue) {
		           if (! mEventQueue.isEmpty()) {
		               event = mEventQueue.remove(0);
					
		           }else{
		        	   break;
		           }
			 }// end of synchronized(
	            if (event != null) {
	                event.run();
	                event = null;
	            }
		   };
			//线程事件集合
			synchronized (mEventSet) {
			        if (! mEventSet.isEmpty()) {
			        	String key = mEventSet.keySet().iterator().next();
			        	event = mEventSet.remove(key);
			        }else{
			        	//break;
			        }
			}// end of synchronized(
	            if (event != null) {
	                event.run();
	                event = null;
	            }
                     //onDraw
			GL2JNILib.native_gl_render();
		}
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值