~albaguirre/mir/possibly-fix-yakkety-build-failure

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Daniel van Vugt <daniel.van.vugt@canonical.com>
 *         Alberto Aguirre <alberto.aguirre@canonical.com>
 */

#include "eglapp.h"
#include <assert.h>
#include <stdio.h>
#include <GLES2/gl2.h>

#include <vector>
#include <stdexcept>
#include <iostream>
#include <algorithm>

static GLuint load_shader(const char *src, GLenum type)
{
    GLuint shader = glCreateShader(type);
    if (shader)
    {
        GLint compiled;
        glShaderSource(shader, 1, &src, NULL);
        glCompileShader(shader);
        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
        if (!compiled)
        {
            GLchar log[1024];
            glGetShaderInfoLog(shader, sizeof log - 1, NULL, log);
            log[sizeof log - 1] = '\0';
            printf("load_shader compile failed: %s\n", log);
            glDeleteShader(shader);
            shader = 0;
        }
    }
    return shader;
}

class DrawableDigit
{
public:
    DrawableDigit(std::initializer_list<std::vector<GLubyte>> list)
    {
        for (auto const& arg : list)
        {
            indices.insert(indices.end(), arg.begin(), arg.end());
        }
    }

    void draw()
    {
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_BYTE, indices.data());
    }

private:
    DrawableDigit() = delete;
    std::vector<GLubyte> indices;
};

class TwoDigitCounter
{
public:
    TwoDigitCounter(GLuint prog, float thickness, unsigned int max_count)
        : count{0}, max_count{std::min(max_count, 99u)}, program{prog},
          vertices{create_vertices(thickness)}, digit_drawables{create_digits()}
    {
        vpos = glGetAttribLocation(program, "vPosition");
        if (vpos == -1)
            throw std::runtime_error("Failed to obtain vPosition attribute");

        xoff = glGetUniformLocation(program, "xOffset");
        if (xoff == -1)
            throw std::runtime_error("Failed to obtain xoff uniform");

        xscale = glGetUniformLocation(program, "xScale");
        if (xscale == -1)
            throw std::runtime_error("Failed to obtain xscale uniform");

        glUniform1f(xscale, 0.4f);
        glVertexAttribPointer(vpos, 2, GL_FLOAT, GL_FALSE, 0, vertices.data());
        glEnableVertexAttribArray(0);
    }

    void draw()
    {
        drawLeftDigit();
        drawRightDigit();
        nextCount();
    }

private:
    void draw(int digit)
    {
        if (static_cast<size_t>(digit) > digit_drawables.size())
            throw std::logic_error("invalid digit to draw");

        digit_drawables[digit].draw();
    }

    void drawRightDigit()
    {
        glUniform1f(xoff, 0.5f);
        draw(count % 10);
    }

    void drawLeftDigit()
    {
        glUniform1f(xoff, -0.5f);
        draw(count/10);
    }

    void nextCount()
    {
        count++;
        if (count > max_count) count = 0;
    }

    static std::vector<GLfloat> create_vertices(GLfloat t)
    {
        return std::vector<GLfloat>{
            -1.0f,   -1.0f,
            -1.0f+t, -1.0f,
             1.0f-t, -1.0f,

             1.0f,   -1.0f,
            -1.0f,   -1.0f+t,
             1.0f,   -1.0f+t,

            -1.0f,   -t/2.0f,
            -1.0f+t, -t/2.0f,
             1.0f-t, -t/2.0f,

             1.0f,   -t/2.0f,
            -1.0f,    t/2.0f,
             1.0f,    t/2.0f,

            -1.0f,    1.0f-t,
             1.0f,    1.0f-t,
            -1.0f,    1.0f,

            -1.0f+t,  1.0f,
             1.0f-t,  1.0f,
             1.0f,    1.0f,
        };
    }

    static std::vector<DrawableDigit> create_digits()
    {
        std::vector<DrawableDigit> digits;
        /*
         * Vertex indices for quads which can be used to make
         * simple number "font" shapes
         */
        std::vector<GLubyte> top{12, 14, 13, 13, 14, 17};
        std::vector<GLubyte> bottom{0, 4, 3, 3, 4, 5};
        std::vector<GLubyte> middle{6, 10, 9, 9, 10, 11};
        std::vector<GLubyte> left{0, 14, 1, 1, 14, 15};
        std::vector<GLubyte> right{2, 16, 3, 3, 16, 17};

        std::vector<GLubyte> top_left{6, 14, 7, 7, 14, 15};
        std::vector<GLubyte> top_right{8, 16, 9, 9, 16, 17};
        std::vector<GLubyte> bottom_left{0, 6, 1, 1, 6, 7};
        std::vector<GLubyte> bottom_right{2, 8, 3, 3, 8, 9};

        digits.push_back({top, bottom, left, right});
        digits.push_back({right});
        digits.push_back({top, middle, bottom, top_right, bottom_left});
        digits.push_back({top, middle, bottom, right});
        digits.push_back({top_left, middle, right});
        digits.push_back({top, middle, bottom, top_left, bottom_right});
        digits.push_back({top, middle, bottom, left, bottom_right});
        digits.push_back({top, right});
        digits.push_back({top, middle, bottom, left, right});
        digits.push_back({top, middle, right, top_left});
        return digits;
    }

    unsigned int count;
    unsigned int const max_count;

    GLint vpos;
    GLint xoff;
    GLint xscale;
    GLuint program;

    std::vector<GLfloat> const vertices;
    std::vector<DrawableDigit> digit_drawables;
};

/* Colours from http://design.ubuntu.com/brand/colour-palette */
#define MID_AUBERGINE(x) (x)*0.368627451f, (x)*0.152941176f, (x)*0.31372549f
#define ORANGE        0.866666667f, 0.282352941f, 0.141414141f

int main(int argc, char *argv[])
{
    const char vshadersrc[] =
        "attribute vec4 vPosition;            \n"
        "uniform float xOffset;               \n"
        "uniform float xScale;                \n"
        "void main()                          \n"
        "{                                    \n"
        "    vec4 vertex;                     \n"
        "    vertex = vPosition;              \n"
        "    vertex.x *= xScale;              \n"
        "    vertex.x += xOffset;             \n"
        "    gl_Position = vertex;            \n"
        "}                                    \n";

    const char fshadersrc[] =
        "precision mediump float;             \n"
        "uniform vec4 col;                    \n"
        "void main()                          \n"
        "{                                    \n"
        "    gl_FragColor = col;              \n"
        "}                                    \n";

    GLuint vshader, fshader, prog;
    GLint linked, col;
    unsigned int width = 512, height = 256;

    if (!mir_eglapp_init(argc, argv, &width, &height))
        return 1;

    vshader = load_shader(vshadersrc, GL_VERTEX_SHADER);
    assert(vshader);
    fshader = load_shader(fshadersrc, GL_FRAGMENT_SHADER);
    assert(fshader);
    prog = glCreateProgram();
    assert(prog);
    glAttachShader(prog, vshader);
    glAttachShader(prog, fshader);
    glLinkProgram(prog);

    glGetProgramiv(prog, GL_LINK_STATUS, &linked);
    if (!linked)
    {
        GLchar log[1024];
        glGetProgramInfoLog(prog, sizeof log - 1, NULL, log);
        log[sizeof log - 1] = '\0';
        printf("Link failed: %s\n", log);
        return 2;
    }

    float const opacity = mir_eglapp_background_opacity;
    glClearColor(MID_AUBERGINE(opacity), opacity);
    glViewport(0, 0, width, height);

    glUseProgram(prog);

    col = glGetUniformLocation(prog, "col");
    glUniform4f(col, ORANGE, 1.0f);

    float thickness = 0.2f;
    unsigned int max_counter = 60;
    TwoDigitCounter counter(prog, thickness, max_counter);

    while (mir_eglapp_running())
    {
        glClear(GL_COLOR_BUFFER_BIT);
        counter.draw();
        mir_eglapp_swap_buffers();
    }

    mir_eglapp_shutdown();

    return 0;
}