~alan-griffiths/mir/migrate-tests-to-miral

« back to all changes in this revision

Viewing changes to examples/miral-kiosk/sw_splash.cpp

  • Committer: Christopher James Halse Rogers
  • Date: 2017-09-07 05:58:13 UTC
  • mfrom: (4242 development-branch)
  • mto: (4243.1.1 mir3)
  • mto: This revision was merged to the branch mainline in revision 4244.
  • Revision ID: christopher.halse.rogers@canonical.com-20170907055813-4qsg25nirybc8jj3
Merge trunk, resolving conflict

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 2 or 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include "sw_splash.h"
 
20
 
 
21
#include <mir/client/display_config.h>
 
22
#include <mir/client/surface.h>
 
23
#include <mir/client/window.h>
 
24
#include <mir/client/window_spec.h>
 
25
 
 
26
#include <mir_toolkit/mir_buffer_stream.h>
 
27
 
 
28
#include <chrono>
 
29
#include <cstring>
 
30
#include <thread>
 
31
#include <mutex>
 
32
 
 
33
namespace
 
34
{
 
35
MirPixelFormat find_8888_format(MirConnection* connection)
 
36
{
 
37
    unsigned int const num_formats = 32;
 
38
    MirPixelFormat pixel_formats[num_formats];
 
39
    unsigned int valid_formats;
 
40
    mir_connection_get_available_surface_formats(connection, pixel_formats, num_formats, &valid_formats);
 
41
 
 
42
    for (unsigned int i = 0; i < num_formats; ++i)
 
43
    {
 
44
        MirPixelFormat cur_pf = pixel_formats[i];
 
45
        if (cur_pf == mir_pixel_format_abgr_8888 ||
 
46
            cur_pf == mir_pixel_format_argb_8888)
 
47
        {
 
48
            return cur_pf;
 
49
        }
 
50
    }
 
51
 
 
52
    for (unsigned int i = 0; i < num_formats; ++i)
 
53
    {
 
54
        MirPixelFormat cur_pf = pixel_formats[i];
 
55
        if (cur_pf == mir_pixel_format_xbgr_8888 ||
 
56
            cur_pf == mir_pixel_format_xrgb_8888)
 
57
        {
 
58
            return cur_pf;
 
59
        }
 
60
    }
 
61
 
 
62
    return *pixel_formats;
 
63
}
 
64
 
 
65
auto create_window(MirConnection* connection, mir::client::Surface const& surface) -> mir::client::Window
 
66
{
 
67
    int id = 0;
 
68
    int width = 0;
 
69
    int height = 0;
 
70
 
 
71
    mir::client::DisplayConfig{connection}.for_each_output([&](MirOutput const* output)
 
72
        {
 
73
            if (mir_output_get_connection_state(output) == mir_output_connection_state_connected &&
 
74
                mir_output_is_enabled(output))
 
75
            {
 
76
                id = mir_output_get_id(output);
 
77
 
 
78
                MirOutputMode const* mode = mir_output_get_current_mode(output);
 
79
                width = mir_output_mode_get_width(mode);
 
80
                height = mir_output_mode_get_height(mode);
 
81
            }
 
82
        });
 
83
 
 
84
    return mir::client::WindowSpec::for_normal_window(connection, width, height)
 
85
        .set_name("splash")
 
86
        .set_fullscreen_on_output(id)
 
87
        .add_surface(surface, width, height, 0, 0)
 
88
        .create_window();
 
89
}
 
90
 
 
91
void render_pattern(MirGraphicsRegion *region, uint8_t pattern[])
 
92
{
 
93
    char *row = region->vaddr;
 
94
 
 
95
    for (int j = 0; j < region->height; j++)
 
96
    {
 
97
        uint32_t *pixel = (uint32_t*)row;
 
98
 
 
99
        for (int i = 0; i < region->width; i++)
 
100
            memcpy(pixel+i, pattern, sizeof pixel[i]);
 
101
 
 
102
        row += region->stride;
 
103
    }
 
104
}
 
105
}
 
106
 
 
107
struct SwSplash::Self
 
108
{
 
109
    std::mutex mutex;
 
110
    std::weak_ptr<mir::scene::Session> session;
 
111
};
 
112
 
 
113
SwSplash::SwSplash() : self{std::make_shared<Self>()} {}
 
114
 
 
115
SwSplash::~SwSplash() = default;
 
116
 
 
117
void SwSplash::operator()(std::weak_ptr<mir::scene::Session> const& session)
 
118
{
 
119
    std::lock_guard<decltype(self->mutex)> lock{self->mutex};
 
120
    self->session = session;
 
121
}
 
122
 
 
123
auto SwSplash::session() const -> std::weak_ptr<mir::scene::Session>
 
124
{
 
125
    std::lock_guard<decltype(self->mutex)> lock{self->mutex};
 
126
    return self->session;
 
127
}
 
128
 
 
129
void SwSplash::operator()(MirConnection* connection)
 
130
{
 
131
    MirPixelFormat pixel_format = find_8888_format(connection);
 
132
 
 
133
    uint8_t pattern[4] = { 0x14, 0x48, 0xDD, 0xFF };
 
134
 
 
135
    switch(pixel_format)
 
136
    {
 
137
    case mir_pixel_format_abgr_8888:
 
138
    case mir_pixel_format_xbgr_8888:
 
139
        std::swap(pattern[2],pattern[0]);
 
140
        break;
 
141
 
 
142
    case mir_pixel_format_argb_8888:
 
143
    case mir_pixel_format_xrgb_8888:
 
144
        break;
 
145
 
 
146
    default:
 
147
        return;
 
148
    };
 
149
 
 
150
 
 
151
    mir::client::Surface surface{mir_connection_create_render_surface_sync(connection, 42, 42)};
 
152
    MirBufferStream* buffer_stream = mir_render_surface_get_buffer_stream(surface, 42, 42, pixel_format);
 
153
 
 
154
    auto const window = create_window(connection, surface);
 
155
 
 
156
    MirGraphicsRegion graphics_region;
 
157
 
 
158
    auto const time_limit = std::chrono::steady_clock::now() + std::chrono::seconds(2);
 
159
 
 
160
    do
 
161
    {
 
162
        mir_buffer_stream_get_graphics_region(buffer_stream, &graphics_region);
 
163
 
 
164
        render_pattern(&graphics_region, pattern);
 
165
        mir_buffer_stream_swap_buffers_sync(buffer_stream);
 
166
 
 
167
        for (auto& x : pattern)
 
168
            x =  3*x/4;
 
169
 
 
170
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
 
171
    }
 
172
    while (std::chrono::steady_clock::now() < time_limit);
 
173
}