RenderView
gallery3d 的渲染从 RenderView 开始。RenderView 从 GLSurfaceView 继承而来,采用了通知型绘制模式,即通过调用 requestRender 通知 RenderView 重绘屏幕。
RenderView 将所有需要绘制的对象都保存一个 Lists中,Lists 包含了5个ArrayList,其定义如下所示:
public final ArrayList<Layer> updateList = new ArrayList<Layer>(); public final ArrayList<Layer> opaqueList = new ArrayList<Layer>(); public final ArrayList<Layer> blendedList = new ArrayList<Layer>(); public final ArrayList<Layer> hitTestList = new ArrayList<Layer>(); public final ArrayList<Layer> systemList = new ArrayList<Layer>();
RenderView 的 onDrawFrame 接口完成每一帧的绘制操作,绘制时遍历 lists 里每个 list 的每一个成员并调用其 renderXXX 函数。主要代码如下所示:
... final Lists lists = sLists; final ArrayList<Layer> updateList = lists.updateList; boolean isDirty = false; for (int i = 0, size = updateList.size(); i != size; ++i) { boolean retVal = updateList.get(i).update(this, mFrameInterval); isDirty |= retVal; } if (isDirty) { requestRender(); } // Clear the depth buffer. gl.glClear(GL11.GL_DEPTH_BUFFER_BIT); gl.glEnable(GL11.GL_SCISSOR_TEST); gl.glScissor(0, 0, getWidth(), getHeight()); // Run the opaque pass. gl.glDisable(GL11.GL_BLEND); final ArrayList<Layer> opaqueList = lists.opaqueList; for (int i = opaqueList.size() - 1; i >= 0; --i) { final Layer layer = opaqueList.get(i); if (!layer.mHidden) { layer.renderOpaque(this, gl); } } // Run the blended pass. gl.glEnable(GL11.GL_BLEND); final ArrayList<Layer> blendedList = lists.blendedList; for (int i = 0, size = blendedList.size(); i != size; ++i) { final Layer layer = blendedList.get(i); if (!layer.mHidden) { layer.renderBlended(this, gl); } } gl.glDisable(GL11.GL_BLEND);
lists 的各个 list 里包含的各个 layer 如下所示:
lists |------------------|-----------------|-----------------|---------------| updateList opaqueList blendedList systemList hitTestList | | | | | GridLayer GridLayer GridLayer GridLayer GridLayer BackgroudLayer BackgroudLayer BackgroudLayer HudLayer HudLayer HudLayer HudLayer TimeBar TimeBar TimeBar PathBar PathBar PathBar XXXButton XXXButton XXXButton XXXMenu XXXMenu XXXMenu
Layer 类提供了 update(...), renderOpaque (...), renderBlended (...) 接口,从上面 RenderView 的 onDrawFrame 绘制代码可以看到,这些接口被调用。
public abstract class Layer { ... ... public abstract void generate(RenderView view, RenderView.Lists lists); public boolean update(RenderView view, float frameInterval) { return false; } public void renderOpaque(RenderView view, GL11 gl) { } public void renderBlended(RenderView view, GL11 gl) { } ... ... }
GridLayer
GridLayer 中有个 GridDrawManager,专门负责绘制。
下面是 GridDrawManager 的构造函数,从其参数里可以看出些门道。