~mir-team/miral/release

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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Copyright © 2013-2015 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/>.
 *
 * Authors: Daniel van Vugt <daniel.van.vugt@canonical.com>
 *          Mirco Müller <mirco.mueller@canonical.com>
 *          Alan Griffiths <alan@octopull.co.uk>
 *          Kevin DuBois <kevin.dubois@canonical.com>
 */

#include "splash.h"

#include <chrono>

#include "eglapp.h"
#include "miregl.h"
#include <assert.h>
#include <glib.h>
#include <string.h>
#include <GLES2/gl2.h>
#include <sys/stat.h>
#include <signal.h>
#include <atomic>
#include <mutex>

#include <mir_toolkit/mir_client_library.h>

#include "spinner_glow.h"
#include "spinner_logo.h"

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;
}

// Colours from http://design.ubuntu.com/brand/colour-palette
//#define MID_AUBERGINE   0.368627451f, 0.152941176f, 0.31372549f
//#define ORANGE          0.866666667f, 0.282352941f, 0.141414141f
//#define WARM_GREY       0.682352941f, 0.654901961f, 0.623529412f
//#define COOL_GREY       0.2f,         0.2f,         0.2f
#define LIGHT_AUBERGINE 0.466666667f, 0.297297297f, 0.435294118f
//#define DARK_AUBERGINE  0.17254902f,  0.0f,         0.117647059f
//#define BLACK           0.0f,         0.0f,         0.0f
//#define WHITE           1.0f,         1.0f,         1.0f

template <typename Image>
void uploadTexture (GLuint id, Image& image)
{
    glBindTexture(GL_TEXTURE_2D, id);

    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 image.width,
                 image.height,
                 0,
                 GL_RGBA,
                 GL_UNSIGNED_BYTE,
                 image.pixel_data);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glBindTexture(GL_TEXTURE_2D, 0);
}

GLuint createShaderProgram(const char* vertexShaderSrc, const char* fragmentShaderSrc)
{
    if (!vertexShaderSrc || !fragmentShaderSrc)
        return 0;

    GLuint vShaderId = 0;
    vShaderId = load_shader(vertexShaderSrc, GL_VERTEX_SHADER);
    assert(vShaderId);

    GLuint fShaderId = 0;
    fShaderId = load_shader(fragmentShaderSrc, GL_FRAGMENT_SHADER);
    assert(fShaderId);

    GLuint progId = 0;
    progId = glCreateProgram();
    assert(progId);
    glAttachShader(progId, vShaderId);
    glAttachShader(progId, fShaderId);
    glLinkProgram(progId);

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

    return progId;
}

typedef struct _AnimationValues
{
    double lastTimeStamp;
    GLfloat angle;
    GLfloat fadeBackground;
    GLfloat fadeLogo;
    GLfloat fadeGlow;
} AnimationValues;

bool updateAnimation (GTimer* timer, AnimationValues* anim)
{
    if (!timer || !anim)
        return false;

    //1.) 0.0   - 0.6:   logo fades in fully
    //2.) 0.0   - 6.0:   logo does one full spin 360°
    //3.) 6.0   - 6.833: glow fades in fully, black-background fades out to 50%
    //4.) 6.833 - 7.666: glow fades out fully, black-background fades out to 0% 
    //5.) 7.666 - 8.266: logo fades out fully
    //8.266..:       now spinner can be closed as all its elements are faded out

    // Hacked to run three times as fast
    double elapsed = 3*g_timer_elapsed (timer, NULL);
    double dt = elapsed - anim->lastTimeStamp;
    anim->lastTimeStamp = elapsed;

    // step 1.)
    if (elapsed < 0.6f)
        anim->fadeLogo += 1.6f * dt;

    // step 2.)
    anim->angle -= (0.017453292519943f * 360.0f / 18.0f) * dt;

    // step 3.) glow
    if (elapsed > 6.0f && elapsed < 6.833f)
        anim->fadeGlow += 1.2f * dt;

    // step 3.) background
    if (elapsed > 0.6f && elapsed < 6.833f)
    { if (anim->fadeBackground > 0) anim->fadeBackground -= 0.15f * dt; else anim->fadeBackground = 0; }

    // step 4.) glow
    if (elapsed > 7.0f)
        anim->fadeGlow -= 0.6f * dt;

    // step 5.)
    if (elapsed > 6.833f)
        anim->fadeLogo -= 1.6f * dt;

    return elapsed < 8.266f;
}

namespace
{
const char vShaderSrcSpinner[] =
    "attribute vec4 vPosition;                       \n"
    "attribute vec2 aTexCoords;                      \n"
    "uniform float theta;                            \n"
    "varying vec2 vTexCoords;                        \n"
    "void main()                                     \n"
    "{                                               \n"
    "    float c = cos(theta);                       \n"
    "    float s = sin(theta);                       \n"
    "    mat2 m;                                     \n"
    "    m[0] = vec2(c, s);                          \n"
    "    m[1] = vec2(-s, c);                         \n"
    "    vTexCoords = m * aTexCoords + vec2 (0.5, 0.5); \n"
    "    gl_Position = vec4(vPosition.xy, -1.0, 1.0); \n"
    "}                                               \n";

const char fShaderSrcGlow[] =
    "precision mediump float;                             \n"
    "varying vec2 vTexCoords;                             \n"
    "uniform sampler2D uSampler;                          \n"
    "uniform float uFadeGlow;                             \n"
    "void main()                                          \n"
    "{                                                    \n"
    "    vec4 col = texture2D(uSampler, vTexCoords);      \n"
    "    col = col * uFadeGlow;                           \n"
    "    gl_FragColor = col;                              \n"
    "}                                                    \n";

const char fShaderSrcLogo[] =
    "precision mediump float;                             \n"
    "varying vec2 vTexCoords;                             \n"
    "uniform sampler2D uSampler;                          \n"
    "uniform float uFadeLogo;                             \n"
    "void main()                                          \n"
    "{                                                    \n"
    "    vec4 col = texture2D(uSampler, vTexCoords);      \n"
    "    col = col * uFadeLogo;                           \n"
    "    gl_FragColor = col;                              \n"
    "}                                                    \n";

std::atomic<bool> dying{false};
void lifecycle_event_callback(MirConnection* /*connection*/, MirLifecycleState state, void* context)
{
    if (state == mir_lifecycle_connection_lost)
        static_cast<decltype(dying)*>(context)->store(true);
}
}

struct SpinnerSplash::Self
{
    std::mutex mutex;
    std::weak_ptr<mir::scene::Session> session;
};

SpinnerSplash::SpinnerSplash() : self{std::make_shared<Self>()} {}

SpinnerSplash::~SpinnerSplash() = default;

void SpinnerSplash::operator()(std::weak_ptr<mir::scene::Session> const& session)
{
    std::lock_guard<decltype(self->mutex)> lock{self->mutex};
    self->session = session;
}

auto SpinnerSplash::session() const
-> std::shared_ptr<mir::scene::Session>
{
    std::lock_guard<decltype(self->mutex)> lock{self->mutex};
    return self->session.lock();
}

void SpinnerSplash::operator()(MirConnection* const connection)
try
{
    GLuint prog[2];
    GLuint texture[2];
    GLint vpos[2];
    GLint theta;
    GLint fadeGlow;
    GLint fadeLogo;
    GLint aTexCoords[2];
    GLint sampler[2];

    mir_connection_set_lifecycle_event_callback(connection, &lifecycle_event_callback, &dying);
    auto const windows = mir_eglapp_init(connection);

    if (!windows.size()) return;

    double pixelSize = 10 * 11.18;
    const GLfloat texCoordsSpinner[] =
    {
        -0.5f, 0.5f,
        -0.5f, -0.5f,
        0.5f, 0.5f,
        0.5f, -0.5f,
    };

    prog[0] = createShaderProgram(vShaderSrcSpinner, fShaderSrcGlow);
    prog[1] = createShaderProgram(vShaderSrcSpinner, fShaderSrcLogo);

    // setup proper GL-blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquation(GL_FUNC_ADD);

    // get locations of shader-attributes/uniforms
    vpos[0] = glGetAttribLocation(prog[0], "vPosition");
    aTexCoords[0] = glGetAttribLocation(prog[0], "aTexCoords");
    theta = glGetUniformLocation(prog[0], "theta");
    sampler[0] = glGetUniformLocation(prog[0], "uSampler");
    fadeGlow = glGetUniformLocation(prog[0], "uFadeGlow");
    vpos[1] = glGetAttribLocation(prog[1], "vPosition");
    aTexCoords[1] = glGetAttribLocation(prog[1], "aTexCoords");
    sampler[1] = glGetUniformLocation(prog[1], "uSampler");
    fadeLogo = glGetUniformLocation(prog[1], "uFadeLogo");

    // create and upload spinner-artwork
    // note that the embedded image data has pre-multiplied alpha
    glGenTextures(2, texture);
    uploadTexture(texture[0], spinner_glow);
    uploadTexture(texture[1], spinner_logo);

    // bunch of shader-attributes to enable
    glVertexAttribPointer(aTexCoords[0], 2, GL_FLOAT, GL_FALSE, 0, texCoordsSpinner);
    glVertexAttribPointer(aTexCoords[1], 2, GL_FLOAT, GL_FALSE, 0, texCoordsSpinner);
    glEnableVertexAttribArray(vpos[0]);
    glEnableVertexAttribArray(vpos[1]);
    glEnableVertexAttribArray(aTexCoords[0]);
    glEnableVertexAttribArray(aTexCoords[1]);
    glActiveTexture(GL_TEXTURE0);

    AnimationValues anim = {0.0, 0.0, 1.0, 0.0, 0.0};
    GTimer* timer = g_timer_new();

    do
    {
        for (auto const& surface : windows)
            surface->paint([&](unsigned int width, unsigned int height)
            {
                GLfloat halfRealWidth = ((2.0 / width) * pixelSize) / 2.0;
                GLfloat halfRealHeight = ((2.0 / height) * pixelSize) / 2.0;

                const GLfloat vertices[] =
                    {
                        halfRealWidth,  halfRealHeight,
                        halfRealWidth, -halfRealHeight,
                        -halfRealWidth, halfRealHeight,
                        -halfRealWidth,-halfRealHeight,
                    };

                glVertexAttribPointer(vpos[0], 2, GL_FLOAT, GL_FALSE, 0, vertices);
                glVertexAttribPointer(vpos[1], 2, GL_FLOAT, GL_FALSE, 0, vertices);

                glViewport(0, 0, width, height);

                GLfloat color[] = {LIGHT_AUBERGINE};
                for (auto& c : color) { c*= anim.fadeBackground; }

                glClearColor(color[0], color[1], color[2], anim.fadeBackground);
                glClear(GL_COLOR_BUFFER_BIT);

                // draw glow
                glUseProgram(prog[0]);
                glBindTexture(GL_TEXTURE_2D, texture[0]);
                glUniform1i(sampler[0], 0);
                glUniform1f(theta, anim.angle);
                glUniform1f(fadeGlow, anim.fadeGlow);
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

                // draw logo
                glUseProgram(prog[1]);
                glBindTexture(GL_TEXTURE_2D, texture[1]);
                glUniform1i(sampler[1], 0);
                glUniform1f(theta, anim.angle);
                glUniform1f(fadeLogo, anim.fadeLogo);
                glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
            });

        if (dying.load())
            throw std::runtime_error("Server disconnected");
    }
    while (updateAnimation(timer, &anim));

    glDeleteTextures(2, texture);
    g_timer_destroy (timer);
}
catch (std::exception const& x)
{
    printf("%s\n", x.what());
}