Debian Codegraph @

android/source/src/java/org/mozilla/gecko/gfx/RenderControllerThread.java

raw
1 package org.mozilla.gecko.gfx;
2
3 import android.opengl.GLSurfaceView;
4
5 import java.util.concurrent.LinkedBlockingQueue;
6
7 /**
8 * Thread which controls the rendering to OpenGL context. Render commands are queued and
9 * processed and delegated by this thread.
10 */
11 public class RenderControllerThread extends Thread implements LayerView.Listener {
12 private final LinkedBlockingQueue<RenderCommand> queue = new LinkedBlockingQueue<RenderCommand>();
13 private GLController controller;
14 private boolean renderQueued = false;
15 private int width;
16 private int height;
17
18 public RenderControllerThread(GLController controller) {
19 this.controller = controller;
20 }
21
22 @Override
23 public void run() {
24 while (true) {
25 RenderCommand command;
26 try {
27 command = queue.take();
28 execute(command);
29 if (command == RenderCommand.SHUTDOWN) {
30 return;
31 }
32 } catch (InterruptedException exception) {
33 throw new RuntimeException(exception);
34 }
35 }
36 }
37
38 void execute(RenderCommand command) {
39 switch (command) {
40 case SHUTDOWN:
41 doShutdown();
42 break;
43 case RENDER_FRAME:
44 doRenderFrame();
45 break;
46 case SIZE_CHANGED:
47 doSizeChanged();
48 break;
49 case SURFACE_CREATED:
50 doSurfaceCreated();
51 break;
52 case SURFACE_DESTROYED:
53 doSurfaceDestroyed();
54 break;
55 }
56 }
57
58 public void shutdown() {
59 queue.add(RenderCommand.SHUTDOWN);
60 }
61
62 @Override
63 public void renderRequested() {
64 synchronized (this) {
65 if (!renderQueued) {
66 queue.add(RenderCommand.RENDER_FRAME);
67 renderQueued = true;
68 }
69 }
70 }
71
72 @Override
73 public void compositionPauseRequested() {
74 queue.add(RenderCommand.SURFACE_DESTROYED);
75 }
76
77 @Override
78 public void surfaceChanged(int width, int height) {
79 this.width = width;
80 this.height = height;
81 queue.add(RenderCommand.SIZE_CHANGED);
82 }
83
84 public void surfaceCreated() {
85 queue.add(RenderCommand.SURFACE_CREATED);
86 }
87
88 private GLSurfaceView.Renderer getRenderer() {
89 return controller.getView().getRenderer();
90 }
91
92 private void doShutdown() {
93 controller.disposeGLContext();
94 controller = null;
95 }
96
97 private void doRenderFrame() {
98 synchronized (this) {
99 renderQueued = false;
100 }
101 if (controller.getEGLSurface() == null) {
102 return;
103 }
104 GLSurfaceView.Renderer renderer = getRenderer();
105 if (renderer != null) {
106 renderer.onDrawFrame(controller.getGL());
107 }
108 controller.swapBuffers();
109 }
110
111 private void doSizeChanged() {
112 GLSurfaceView.Renderer renderer = getRenderer();
113 if (renderer != null) {
114 renderer.onSurfaceChanged(controller.getGL(), width, height);
115 }
116 }
117
118 private void doSurfaceCreated() {
119 if (!controller.hasSurface()) {
120 controller.initGLContext();
121 }
122 }
123
124 private void doSurfaceDestroyed() {
125 controller.disposeGLContext();
126 }
127
128 public enum RenderCommand {
129 SHUTDOWN,
130 RECREATE_SURFACE,
131 RENDER_FRAME,
132 SIZE_CHANGED,
133 SURFACE_CREATED,
134 SURFACE_DESTROYED,
135 }
136 }