~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
/*
 * Copyright © 2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Kevin DuBois <kevin.dubois@canonical.com>
 *
 */

#include <mir_toolkit/mir_connection.h>
#include <mir_toolkit/mir_buffer_stream.h>
#include <mir_toolkit/mir_surface.h>
#include <mir_toolkit/mir_presentation_chain.h>
#include <mir_toolkit/mir_buffer.h>
#include <mir_toolkit/version.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <assert.h>

#define PALETTE_SIZE 5

void fill_buffer_diagonal_stripes(
    MirBuffer* buffer, unsigned int fg, unsigned int bg)
{
    MirGraphicsRegion region = mir_buffer_get_graphics_region(buffer, mir_read_write);
    if ((!region.vaddr) || (region.pixel_format != mir_pixel_format_abgr_8888))
        return;

    unsigned char* vaddr = (unsigned char*) region.vaddr;
    int const num_stripes = 10;
    int const stripes_thickness = region.width / num_stripes;
    for(int i = 0; i < region.height; i++)
    {
        unsigned int* pixel = (unsigned int*) vaddr;
        for(int j = 0; j < region.width ; j++)
        {
            if ((((i + j) / stripes_thickness) % stripes_thickness) % 2)
                pixel[j] = bg;
            else
                pixel[j] = fg;
        }
        vaddr += region.stride; 
    }
}

typedef struct SubmissionInfo
{
    int available;
    MirBuffer* buffer;
    pthread_mutex_t lock;
    pthread_cond_t cv;
} SubmissionInfo;

static void available_callback(MirBuffer* buffer, void* client_context)
{
    SubmissionInfo* info = (SubmissionInfo*) client_context;
    pthread_mutex_lock(&info->lock);
    info->available = 1;
    info->buffer = buffer;
    pthread_cond_broadcast(&info->cv);
    pthread_mutex_unlock(&info->lock);
}

volatile sig_atomic_t rendering = 1;
static void shutdown(int signum)
{
    if ((signum == SIGTERM) || (signum == SIGINT))
        rendering = 0;
}

int main(int argc, char** argv)
{
    static char const *socket_file = NULL;
    int arg = -1;
    int width = 400;
    int height = 400;
    while ((arg = getopt (argc, argv, "m:s:h:")) != -1)
    {
        switch (arg)
        {
        case 'm':
            socket_file = optarg;
            break;
        case 's':
        {
            unsigned int w, h;
            if (sscanf(optarg, "%ux%u", &w, &h) == 2)
            {
                width = w;
                height = h;
            }
            else
            {
                printf("Invalid size: %s, using default size\n", optarg);
            }
            break;
        }
        case 'h':
        case '?':
        default:
            puts(argv[0]);
            printf("Usage:\n");
            printf("    -m <Mir server socket>\n");
            printf("    -s WIDTHxHEIGHT of window\n");
            printf("    -h help dialog\n");
            return -1;
        }
    }


    int const chain_width = width / 2;
    int const chain_height = height / 2;

    sigset_t signal_set;
    sigemptyset(&signal_set);
    sigaddset(&signal_set, SIGALRM);
    sigprocmask(SIG_BLOCK, &signal_set, NULL);

    struct sigaction action;
    action.sa_handler = shutdown;
    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    sigaction(SIGINT, &action, NULL);
    sigaction(SIGTERM, &action, NULL);


    int displacement_x = 0;
    int displacement_y = 0;

    MirPixelFormat format = mir_pixel_format_abgr_8888;

    MirConnection* connection = mir_connect_sync(socket_file, "prerendered_frames");
    if (!mir_connection_is_valid(connection))
    {
        printf("could not connect to server file at: %s\n", socket_file);
        return -1;
    }

    unsigned int const num_chains = 4;
    unsigned int const num_buffers = num_chains + 1;
    unsigned int const fg[PALETTE_SIZE] = {
        0xFF14BEA0,
        0xFF000000,
        0xFF1111FF,
        0xFFAAAAAA,
        0xFFB00076
    };
    unsigned int const bg[PALETTE_SIZE] = {
        0xFFDF2111,
        0xFFFFFFFF,
        0xFF11DDDD,
        0xFF404040,
        0xFFFFFF00
    };
    unsigned int spare_buffer = 0;

    MirPresentationChain* chain[num_chains];
    for(unsigned int i = 0u; i < num_chains; i++)
    {
        chain[i] =  mir_connection_create_presentation_chain_sync(connection);
        if (!mir_presentation_chain_is_valid(chain[i]))
        {
            printf("could not create MirPresentationChain\n");

            // TODO this is a frig to pass smoke tests until we support NBS by default
#if (MIR_CLIENT_VERSION <= MIR_VERSION_NUMBER(3, 3, 0))
            printf("This is currently an unreleased API - likely server support is switched off\n");
            return 0;
#else
            return -1;
#endif
        }
    }

    //Arrange a 2x2 grid of chains within surface
    MirSurfaceSpec* spec = mir_connection_create_spec_for_normal_surface(connection, width, height, format);
    mir_surface_spec_add_presentation_chain(
        spec, chain_width, chain_height, displacement_x, displacement_y, chain[0]);
    mir_surface_spec_add_presentation_chain(
        spec, chain_width, chain_height, chain_width, displacement_y, chain[1]);
    mir_surface_spec_add_presentation_chain(
        spec, chain_width, chain_height, displacement_x, chain_height, chain[2]);
    mir_surface_spec_add_presentation_chain(
        spec, chain_width, chain_height, chain_width, chain_height, chain[3]);
    MirSurface* surface = mir_surface_create_sync(spec);
    mir_surface_spec_release(spec);

    MirBufferUsage usage = mir_buffer_usage_software;
    SubmissionInfo buffer_available[num_buffers];

    //prerender the frames
    for (unsigned int i = 0u; i < num_buffers; i++)
    {
        pthread_cond_init(&buffer_available[i].cv, NULL);
        pthread_mutex_init(&buffer_available[i].lock, NULL);
        buffer_available[i].available = 0;
        buffer_available[i].buffer = NULL;

        mir_connection_allocate_buffer(
            connection, width, height, format, usage, available_callback, &buffer_available[i]);

        pthread_mutex_lock(&buffer_available[i].lock);
        while(!buffer_available[i].buffer)
            pthread_cond_wait(&buffer_available[i].cv, &buffer_available[i].lock);

        fill_buffer_diagonal_stripes(buffer_available[i].buffer,
            fg[i % PALETTE_SIZE], bg[i % PALETTE_SIZE]);

        pthread_mutex_unlock(&buffer_available[i].lock);
    }

    while (rendering)
    {
        for(unsigned int i = 0u; i < num_chains; i++)
        {
            MirBuffer* b;
            pthread_mutex_lock(&buffer_available[spare_buffer].lock);
            while(!buffer_available[spare_buffer].available)
                pthread_cond_wait(&buffer_available[spare_buffer].cv, &buffer_available[spare_buffer].lock);
            buffer_available[spare_buffer].available = 0;
            b = buffer_available[spare_buffer].buffer;
            pthread_mutex_unlock(&buffer_available[spare_buffer].lock);

            mir_presentation_chain_submit_buffer(chain[i], b);

            //just looks like a blur if we don't slow things down
            ualarm(500000, 0);
            int sig;
            sigwait(&signal_set, &sig);
            if (!rendering) break;

            if (++spare_buffer > num_chains)
                spare_buffer = 0;
        }
    }

    for (unsigned int i = 0u; i < num_buffers; i++)
        mir_buffer_release(buffer_available[i].buffer);
    for (unsigned int i = 0u; i < num_chains; i++)
        mir_presentation_chain_release(chain[i]);
    mir_surface_release_sync(surface);
    mir_connection_release(connection);
    return 0;
}