~raof/mir/wayland-clients-close-their-sessions-on-quit

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
/*
 * Copyright © 2016 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 2 or 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: Kevin DuBois <kevin.dubois@canonical.com>
 */

#include "diamond.h"
#include "mir_egl_platform_shim.h"
#include <assert.h>
#include <stdio.h>
#include <unistd.h>

int const num_vertices = 4; 
GLfloat const vertices[] =
{
    1.0f, 0.0f,
    0.0f, 1.0f,
    0.0f, -1.0f,
    -1.0f, 0.0f,
};

GLfloat const texcoords[] =
{
    0.0f, 0.0f,
    1.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 1.0f,
};

static GLuint load_shader(const char *src, GLenum type)
{
    GLuint shader = glCreateShader(type);
    assert(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;
        assert(-1);
    }
    return shader;
}

void render_diamond(Diamond* info, int width, int height)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glViewport(0, 0, width, height);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, info->num_vertices);
}

Diamond setup_diamond_common()
{
    glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
    char const vertex_shader_src[] =
        "attribute vec2 pos;                                \n"
        "attribute vec2 texcoord;                           \n"
        "varying vec2 v_texcoord;                           \n"
        "void main()                                        \n"
        "{                                                  \n"
        "    gl_Position = vec4(pos.x, pos.y, 0.0, 1.0);    \n"
        "    v_texcoord = texcoord;                         \n"
        "}                                                  \n";
    char const fragment_shader_src[] =
        "precision mediump float;                       \n"
        "varying vec2 v_texcoord;                       \n"
        "uniform sampler2D tex;                         \n"
        "void main()                                    \n"
        "{                                              \n"
        "   gl_FragColor = texture2D(tex, v_texcoord);  \n"
        "}                                              \n";

    GLint linked = 0;
    Diamond info;

    info.vertex_shader = load_shader(vertex_shader_src, GL_VERTEX_SHADER);
    info.fragment_shader = load_shader(fragment_shader_src, GL_FRAGMENT_SHADER);
    info.program = glCreateProgram();
    assert(info.program);

    glAttachShader(info.program, info.vertex_shader);
    glAttachShader(info.program, info.fragment_shader);
    glLinkProgram(info.program);
    glGetProgramiv(info.program, GL_LINK_STATUS, &linked);
    if (!linked)
    {
        GLchar log[1024];
        glGetProgramInfoLog(info.program, sizeof log - 1, NULL, log);
        log[sizeof log - 1] = '\0';
        printf("Link failed: %s\n", log);
        assert(-1);
    }

    glUseProgram(info.program);

    info.pos = glGetAttribLocation(info.program, "pos");
    info.texuniform = glGetUniformLocation(info.program, "tex");
    info.texcoord = glGetAttribLocation(info.program, "texcoord");
    info.num_vertices = num_vertices;

    glUniform1i(info.pos, 0);
    glUniform1i(info.texuniform, 0);
    glVertexAttribPointer(info.pos, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glVertexAttribPointer(info.texcoord, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
    glEnableVertexAttribArray(info.pos);
    glEnableVertexAttribArray(info.texcoord);

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &info.texid);
    glBindTexture(GL_TEXTURE_2D, info.texid);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    return info;
}

Diamond setup_diamond_import(EGLImageKHR img, int use_shim)
{
    Diamond diamond = setup_diamond_common();
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    if (use_shim)
    {
        future_driver_glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, img);
    }
    else
    {
        PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES =
            (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) eglGetProcAddress("glEGLImageTargetTexture2DOES");
        glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, img);
    }
    return diamond;
}

Diamond setup_diamond()
{
    Diamond diamond = setup_diamond_common();
    static unsigned char data[] = {
        0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0x00, 0x00, 0xFF,
        0xFF, 0x00, 0x00, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF
    };
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(
        GL_TEXTURE_2D, 0, GL_RGBA,
        2, 2, 0,
        GL_RGBA, GL_UNSIGNED_BYTE,
        data);
    return diamond;
}

void destroy_diamond(Diamond* info)
{
    glDeleteTextures(1, &info->texid);
    glDisableVertexAttribArray(info->pos);
    glDisableVertexAttribArray(info->texcoord);
    glDeleteShader(info->vertex_shader);
    glDeleteShader(info->fragment_shader);
    glDeleteProgram(info->program);
}