~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
/*
 * 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>

float distance(int x0, int y0, int x1, int y1)
{
    float dx = x1 - x0;
    float dy = y1 - y0; 
    return sqrt((dx * dx +  dy * dy));
}

void fill_buffer_with_centered_circle_abgr(
    MirBuffer* buffer, float radius, 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;
    int const center_x = region.width / 2;
    int const center_y = region.height / 2; 
    unsigned char* vaddr = (unsigned char*) region.vaddr;
    for(int i = 0; i < region.height; i++)
    {
        unsigned int* pixel = (unsigned int*) vaddr;
        for(int j = 0; j < region.width ; j++)
        {
            int const centered_i = i - center_y;
            int const centered_j = j - center_x; 
            if (distance(0,0, centered_i, centered_j) > radius)
                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 int 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;
        }
    }

    signal(SIGTERM, shutdown);
    signal(SIGINT, shutdown);

    int displacement_x = 0;
    int displacement_y = 0;
    unsigned int fg = 0xFF1448DD;
    unsigned int bg = 0xFF6F2177;
    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;
    }

    MirPresentationChain* chain =  mir_connection_create_presentation_chain_sync(connection);
    if (!mir_presentation_chain_is_valid(chain))
    {
        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
    }

    MirSurfaceSpec* spec = mir_connection_create_spec_for_normal_surface(connection, width, height, format);
    mir_surface_spec_add_presentation_chain(
        spec, width, height, displacement_x, displacement_y, chain);
    MirSurface* surface = mir_surface_create_sync(spec);
    if (!mir_surface_is_valid(surface))
    {
        printf("could not create MirSurface\n");
        return -1;
    }

    mir_surface_spec_release(spec);

    int num_prerendered_frames = 20;
    MirBufferUsage usage = mir_buffer_usage_software;
    SubmissionInfo buffer_available[num_prerendered_frames];

    for (int i = 0u; i < num_prerendered_frames; 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);

        float max_radius = distance(0, 0, width, height) / 2.0f;
        float radius_i = ((float) i + 1) / num_prerendered_frames * max_radius;
        fill_buffer_with_centered_circle_abgr(buffer_available[i].buffer, radius_i, fg, bg);

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

    int i = 0;
    int inc = -1;
    while (rendering)
    {
        MirBuffer* b;
        pthread_mutex_lock(&buffer_available[i].lock);
        while(!buffer_available[i].available)
            pthread_cond_wait(&buffer_available[i].cv, &buffer_available[i].lock);
        buffer_available[i].available = 0;
        b = buffer_available[i].buffer;
        pthread_mutex_unlock(&buffer_available[i].lock);

        mir_presentation_chain_submit_buffer(chain, b);

        if ((i == num_prerendered_frames - 1) || (i == 0))
            inc *= -1; 
        i = i + inc;
    }

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