Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / mozilla / gecko / gfx / RenderControllerThread.java
blob5c74d56a004b9e6c6a4b0e88b1868550de257709
1 package org.mozilla.gecko.gfx;
3 import android.opengl.GLSurfaceView;
5 import java.util.concurrent.LinkedBlockingQueue;
7 import javax.microedition.khronos.opengles.GL10;
9 /**
10 * Thread which controls the rendering to OpenGL context. Render commands are queued and
11 * processed and delegated by this thread.
13 public class RenderControllerThread extends Thread implements LayerView.Listener {
14 private LinkedBlockingQueue<RenderCommand> queue = new LinkedBlockingQueue<RenderCommand>();
15 private GLController controller;
16 private boolean renderQueued = false;
17 private int width;
18 private int height;
20 public RenderControllerThread(GLController controller) {
21 this.controller = controller;
24 @Override
25 public void run() {
26 while (true) {
27 RenderCommand command;
28 try {
29 command = queue.take();
30 execute(command);
31 if (command == RenderCommand.SHUTDOWN) {
32 return;
34 } catch (InterruptedException exception) {
35 throw new RuntimeException(exception);
40 void execute(RenderCommand command) {
41 switch (command) {
42 case SHUTDOWN:
43 doShutdown();
44 break;
45 case RENDER_FRAME:
46 doRenderFrame();
47 break;
48 case SIZE_CHANGED:
49 doSizeChanged();
50 break;
51 case SURFACE_CREATED:
52 doSurfaceCreated();
53 break;
54 case SURFACE_DESTROYED:
55 doSurfaceDestroyed();
56 break;
60 public void shutdown() {
61 queue.add(RenderCommand.SHUTDOWN);
64 @Override
65 public void compositorCreated() {
69 @Override
70 public void renderRequested() {
71 synchronized (this) {
72 if (!renderQueued) {
73 queue.add(RenderCommand.RENDER_FRAME);
74 renderQueued = true;
79 @Override
80 public void compositionPauseRequested() {
81 queue.add(RenderCommand.SURFACE_DESTROYED);
84 @Override
85 public void surfaceChanged(int width, int height) {
86 this.width = width;
87 this.height = height;
88 queue.add(RenderCommand.SIZE_CHANGED);
91 public void surfaceCreated() {
92 queue.add(RenderCommand.SURFACE_CREATED);
95 private GLSurfaceView.Renderer getRenderer() {
96 return controller.getView().getRenderer();
99 private void doShutdown() {
100 controller.disposeGLContext();
101 controller = null;
104 private void doRenderFrame() {
105 synchronized (this) {
106 renderQueued = false;
108 if (controller.getEGLSurface() == null) {
109 return;
111 GLSurfaceView.Renderer renderer = getRenderer();
112 if (renderer != null) {
113 renderer.onDrawFrame(controller.getGL());
115 controller.swapBuffers();
118 private void doSizeChanged() {
119 GLSurfaceView.Renderer renderer = getRenderer();
120 if (renderer != null) {
121 renderer.onSurfaceChanged(controller.getGL(), width, height);
125 private void doSurfaceCreated() {
126 if (!controller.hasSurface()) {
127 controller.initGLContext();
131 private void doSurfaceDestroyed() {
132 controller.disposeGLContext();
135 public enum RenderCommand {
136 SHUTDOWN,
137 RECREATE_SURFACE,
138 RENDER_FRAME,
139 SIZE_CHANGED,
140 SURFACE_CREATED,
141 SURFACE_DESTROYED,