File: sw_splash.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (154 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright © 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/>.
 */

#include "sw_splash.h"
#include "wayland_app.h"
#include "wayland_surface.h"
#include "wayland_shm.h"
#include <wayland-client.h>

#include <chrono>
#include <cstring>
#include <thread>
#include <mutex>

using namespace mir::geometry;

namespace
{
struct DrawContext
{
    struct wl_display* display;
    std::unique_ptr<WaylandShm> shm;
    std::unique_ptr<WaylandSurface> surface;

    uint8_t pattern[4] = { 0x14, 0x48, 0xDD, 0xFF };
};

void draw_new_stuff(DrawContext* ctx)
{
    Size const size{ctx->surface->configured_size()};
    auto const width = size.width.as_int();
    auto const height = size.height.as_int();
    auto const stride = width * 4;

    auto const buffer = ctx->shm->get_buffer(size, Stride{stride});
    char* row = static_cast<decltype(row)>(buffer->data());

    for (int j = 0; j < height; j++)
    {
        uint32_t* pixel = (uint32_t*)row;

        for (int i = 0; i < width; i++)
            memcpy(pixel + i, ctx->pattern, sizeof pixel[i]);

        row += stride;
    }

    ctx->surface->add_frame_callback([ctx]()
        {
            draw_new_stuff(ctx);
        });
    ctx->surface->attach_buffer(buffer->use(), 1);
    ctx->surface->commit();
}
}

struct SwSplash::Self : SplashSession
{
    Self()
    {
    }

    bool show_splash=true;

    DrawContext ctx;

    std::mutex mutable mutex;
    std::weak_ptr<mir::scene::Session> session_;

    std::shared_ptr<mir::scene::Session> session() const override
    {
        std::lock_guard lock{mutex};
        return session_.lock();
    }

    void operator()(struct wl_display* display);
};

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

SwSplash::~SwSplash() = default;

void SwSplash::enable (bool show_splash_opt){
    self->show_splash = show_splash_opt;
}

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

SwSplash::operator std::shared_ptr<SplashSession>() const
{
    return self;
}

void SwSplash::Self::operator()(struct wl_display* display)
{
    if (!show_splash)
        return;

    WaylandApp app{display};

    ctx.display = display;
    ctx.shm = std::make_unique<WaylandShm>(app.shm());
    ctx.surface = std::make_unique<WaylandSurface>(&app);
    ctx.surface->set_fullscreen(nullptr);
    ctx.surface->commit();
    app.roundtrip();

    WaylandCallback::create(wl_display_sync(display), [this]()
        {
            draw_new_stuff(&ctx);
        });

    auto const time_limit = std::chrono::steady_clock::now() + std::chrono::seconds(2);

    //this is the splash screen
    do
    {
        wl_display_dispatch(display);

        for (auto& x : ctx.pattern)
        {
            x =  3*x/4;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
    while (std::chrono::steady_clock::now() < time_limit);

    app.roundtrip();
    ctx.surface.reset();
    ctx.shm.reset();
}

void SwSplash::operator()(struct wl_display* display)
{
    (*self)(display);
}