Update git submodules
[LibreOffice.git] / android / source / src / java / org / mozilla / gecko / gfx / RenderControllerThread.java
blob4a205123ed4b95bc7eca76a4cd8440cc2aa99641
1 package org.mozilla.gecko.gfx;
3 import android.opengl.GLSurfaceView;
5 import java.util.concurrent.LinkedBlockingQueue;
7 /**
8 * Thread which controls the rendering to OpenGL context. Render commands are queued and
9 * processed and delegated by this thread.
11 public class RenderControllerThread extends Thread implements LayerView.Listener {
12 private LinkedBlockingQueue<RenderCommand> queue = new LinkedBlockingQueue<RenderCommand>();
13 private GLController controller;
14 private boolean renderQueued = false;
15 private int width;
16 private int height;
18 public RenderControllerThread(GLController controller) {
19 this.controller = controller;
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;
32 } catch (InterruptedException exception) {
33 throw new RuntimeException(exception);
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;
58 public void shutdown() {
59 queue.add(RenderCommand.SHUTDOWN);
62 @Override
63 public void compositorCreated() {
67 @Override
68 public void renderRequested() {
69 synchronized (this) {
70 if (!renderQueued) {
71 queue.add(RenderCommand.RENDER_FRAME);
72 renderQueued = true;
77 @Override
78 public void compositionPauseRequested() {
79 queue.add(RenderCommand.SURFACE_DESTROYED);
82 @Override
83 public void surfaceChanged(int width, int height) {
84 this.width = width;
85 this.height = height;
86 queue.add(RenderCommand.SIZE_CHANGED);
89 public void surfaceCreated() {
90 queue.add(RenderCommand.SURFACE_CREATED);
93 private GLSurfaceView.Renderer getRenderer() {
94 return controller.getView().getRenderer();
97 private void doShutdown() {
98 controller.disposeGLContext();
99 controller = null;
102 private void doRenderFrame() {
103 synchronized (this) {
104 renderQueued = false;
106 if (controller.getEGLSurface() == null) {
107 return;
109 GLSurfaceView.Renderer renderer = getRenderer();
110 if (renderer != null) {
111 renderer.onDrawFrame(controller.getGL());
113 controller.swapBuffers();
116 private void doSizeChanged() {
117 GLSurfaceView.Renderer renderer = getRenderer();
118 if (renderer != null) {
119 renderer.onSurfaceChanged(controller.getGL(), width, height);
123 private void doSurfaceCreated() {
124 if (!controller.hasSurface()) {
125 controller.initGLContext();
129 private void doSurfaceDestroyed() {
130 controller.disposeGLContext();
133 public enum RenderCommand {
134 SHUTDOWN,
135 RECREATE_SURFACE,
136 RENDER_FRAME,
137 SIZE_CHANGED,
138 SURFACE_CREATED,
139 SURFACE_DESTROYED,