summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/mozilla/gecko/gfx/RenderControllerThread.java
blob: 06f82f158366bb645c791f95fc6b7ae512bd4dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.mozilla.gecko.gfx;

import android.opengl.GLSurfaceView;

import java.util.concurrent.LinkedBlockingQueue;

import javax.microedition.khronos.opengles.GL10;

/**
 * Thread which controls the rendering to OpenGL context. Render commands are queued and
 * processed and delegated by this thread.
 */
public class RenderControllerThread extends Thread implements LayerView.Listener {
    private LinkedBlockingQueue<RenderCommand> queue = new LinkedBlockingQueue<RenderCommand>();
    private GLController controller;
    private boolean renderQueued = false;
    private int width;
    private int height;

    public RenderControllerThread(GLController controller) {
        this.controller = controller;
    }

    @Override
    public void run() {
        while (true) {
            RenderCommand command;
            try {
                command = queue.take();
                execute(command);
                if (command == RenderCommand.SHUTDOWN) {
                    return;
                }
            } catch (InterruptedException exception) {
                throw new RuntimeException(exception);
            }
        }
    }

    void execute(RenderCommand command) {
        switch (command) {
            case SHUTDOWN:
                doShutdown();
                break;
            case RENDER_FRAME:
                doRenderFrame();
                break;
            case SIZE_CHANGED:
                doSizeChanged();
                break;
            case SURFACE_CREATED:
                doSurfaceCreated();
                break;
            case SURFACE_DESTROYED:
                doSurfaceDestroyed();
                break;
        }
    }

    public void shutdown() {
        queue.add(RenderCommand.SHUTDOWN);
    }

    @Override
    public void compositorCreated() {

    }

    @Override
    public void renderRequested() {
        synchronized (this) {
            if (!renderQueued) {
                queue.add(RenderCommand.RENDER_FRAME);
                renderQueued = true;
            }
        }
    }

    @Override
    public void compositionPauseRequested() {
        queue.add(RenderCommand.SURFACE_DESTROYED);
    }

    @Override
    public void compositionResumeRequested(int width, int height) {

    }

    @Override
    public void surfaceChanged(int width, int height) {
        this.width = width;
        this.height = height;
        queue.add(RenderCommand.SIZE_CHANGED);
    }

    public void surfaceCreated() {
        queue.add(RenderCommand.SURFACE_CREATED);
    }

    private GLSurfaceView.Renderer getRenderer() {
        return controller.getView().getRenderer();
    }

    private void doShutdown() {
        controller.disposeGLContext();
        controller = null;
    }

    private void doRenderFrame() {
        synchronized (this) {
            renderQueued = false;
        }
        if (controller.getEGLSurface() == null) {
            return;
        }
        GLSurfaceView.Renderer renderer = getRenderer();
        if (renderer != null) {
            renderer.onDrawFrame((GL10) controller.getGL());
        }
        controller.swapBuffers();
    }

    private void doSizeChanged() {
        GLSurfaceView.Renderer renderer = getRenderer();
        if (renderer != null) {
            renderer.onSurfaceChanged((GL10) controller.getGL(), width, height);
        }
    }

    private void doSurfaceCreated() {
        if (!controller.hasSurface()) {
            controller.initGLContext();
        }
    }

    private void doSurfaceDestroyed() {
        controller.disposeGLContext();
    }

    public enum RenderCommand {
        SHUTDOWN,
        RECREATE_SURFACE,
        RENDER_FRAME,
        SIZE_CHANGED,
        SURFACE_CREATED,
        SURFACE_DESTROYED,
    }
}