~vanvugt/mir/gpu-load-balance

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
/*
 * Copyright © 2015, 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 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/>.
 *
 * Authored By: Kevin DuBois <kevin.dubois@canonical.com>
 */

#include "server_example_custom_compositor.h"
#include "server_example_adorning_compositor.h"

#include "mir/abnormal_exit.h"
#include "mir/server.h"
#include "mir/options/option.h"
#include "mir/compositor/display_buffer_compositor_factory.h"

namespace me = mir::examples;
namespace mg = mir::graphics;
namespace mc = mir::compositor;

///\example server_example_custom_compositor.cpp
/// Demonstrate writing an alternative GL rendering code

namespace
{
char const* const compositor_option = "custom-compositor";
char const* const compositor_description = "Select an alterative compositor [{adorning|default}]";
char const* const compositor_adorning = "adorning";
char const* const compositor_default = "default";

char const* const background_option = "background-color";
char const* const background_description =
    "fill the background of the adorning compositor with a color [{purple|blue|grey|black}]";
char const* const background_purple = "purple";
char const* const background_blue = "blue";
char const* const background_grey = "grey";
char const* const background_black = "black";

class AdorningDisplayBufferCompositorFactory : public mc::DisplayBufferCompositorFactory
{
public:
    AdorningDisplayBufferCompositorFactory(std::tuple<float, float, float> const& rgb_color,
        std::shared_ptr<mc::CompositorReport> const& report) :
        color(rgb_color), report(report)
    {
    }

    std::unique_ptr<mc::DisplayBufferCompositor> create_compositor_for(
        mg::DisplayBuffer& display_buffer) override
    {
        return std::make_unique<me::AdorningDisplayBufferCompositor>(display_buffer, color, report);
    }
private:
    std::tuple<float, float, float> const color;
    std::shared_ptr<mc::CompositorReport> const report;
};
}

void me::add_custom_compositor_option_to(Server& server)
{
    server.add_configuration_option(compositor_option, compositor_description, compositor_default);
    server.add_configuration_option(background_option, background_description, background_black);

    server.wrap_display_buffer_compositor_factory(
        [server](std::shared_ptr<mc::DisplayBufferCompositorFactory> const& factory)
        -> std::shared_ptr<mc::DisplayBufferCompositorFactory>
    {
        auto const selection = server.get_options()->get<std::string>(compositor_option);

        auto const color_name = server.get_options()->get<std::string>(background_option);
        if (selection == compositor_adorning)
        {
            std::tuple<float, float, float> color;
            if (color_name == background_blue)
                color = std::make_tuple(0.4, 0.5, 1.0);
            else if (color_name == background_grey)
                color = std::make_tuple(0.3, 0.3, 0.3);
            else if (color_name == background_purple)
                color = std::make_tuple(0.8, 0.5, 0.8);
            else if (color_name == background_black)
                color = std::make_tuple(0.0, 0.0, 0.0);
            else
                throw mir::AbnormalExit("Unknown color selection: " + color_name);
            return std::make_shared<AdorningDisplayBufferCompositorFactory>(color, server.the_compositor_report());
        }
        else if (selection == compositor_default)
        {
            if (color_name != background_black)
                throw mir::AbnormalExit("default compositor can only set background color to black");
            return factory;
        }

        throw mir::AbnormalExit("Unknown compositor selection: " + selection);
    });
}