File: test_basic_screen_shooter.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 (299 lines) | stat: -rw-r--r-- 10,954 bytes parent folder | download
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * 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 "mir/executor.h"
#include "mir/graphics/platform.h"
#include "mir/graphics/display_sink.h"
#include "mir/renderer/gl/gl_surface.h"
#include "mir/test/doubles/stub_gl_rendering_provider.h"
#include "src/server/compositor/basic_screen_shooter.h"

#include "mir/renderer/renderer_factory.h"
#include "mir/test/doubles/mock_scene.h"
#include "mir/test/doubles/mock_renderer.h"
#include "mir/test/doubles/mock_gl_rendering_provider.h"
#include "mir/test/doubles/stub_gl_rendering_provider.h"
#include "mir/test/doubles/advanceable_clock.h"
#include "mir/test/doubles/explicit_executor.h"
#include "mir/test/doubles/stub_buffer.h"
#include "mir/test/doubles/stub_scene_element.h"
#include "mir/test/doubles/stub_renderable.h"
#include "mir/test/doubles/stub_gl_config.h"
#include "mir/test/doubles/stub_buffer_allocator.h"

#include <gtest/gtest.h>

namespace mc = mir::compositor;
namespace mr = mir::renderer;
namespace mg = mir::graphics;
namespace geom = mir::geometry;
namespace mtd = mir::test::doubles;

using namespace testing;
using namespace std::chrono_literals;

namespace
{
class MockRendererFactory : public mr::RendererFactory
{
public:
    MOCK_METHOD(
        std::unique_ptr<mr::Renderer>,
        create_renderer_for,
        (std::unique_ptr<mg::gl::OutputSurface>, std::shared_ptr<mg::GLRenderingProvider>),
        (const override));
};

struct BasicScreenShooter : Test
{
    BasicScreenShooter()
    {
        ON_CALL(*scene, scene_elements_for(_)).WillByDefault(Return(scene_elements));
        ON_CALL(*renderer_factory, create_renderer_for(_,_)).WillByDefault(
                [this](auto output_surface, auto)
                {
                    ON_CALL(*next_renderer, render(_))
                        .WillByDefault(
                            [surface = std::shared_ptr<mg::gl::OutputSurface>(std::move(output_surface))]()
                            {
                                return surface->commit();
                            });

                    return std::move(next_renderer);
                });
        ON_CALL(*gl_provider, as_texture(_))
            .WillByDefault(
                [this](auto buffer)
                {
                    return default_gl_behaviour_provider.as_texture(std::move(buffer));
                });
        ON_CALL(*gl_provider, surface_for_sink(_, _))
            .WillByDefault(
                [](mg::DisplaySink& sink, auto const&)
                    -> std::unique_ptr<mg::gl::OutputSurface>
                {
                    if (auto cpu_provider = sink.acquire_compatible_allocator<mg::CPUAddressableDisplayAllocator>())
                    {
                        auto surface = std::make_unique<testing::NiceMock<mtd::MockOutputSurface>>();
                        auto format = cpu_provider->supported_formats().front();
                        ON_CALL(*surface, commit())
                            .WillByDefault(
                                [cpu_provider, format]()
                                {
                                    return cpu_provider->alloc_fb(format);
                                });
                        return surface;
                    }
                    BOOST_THROW_EXCEPTION((std::runtime_error{"CPU output support not available?!"}));
                });
        ON_CALL(*gl_provider, suitability_for_allocator(_))
            .WillByDefault(Return(mg::probe::supported));
        ON_CALL(*gl_provider, suitability_for_display(_))
            .WillByDefault(Return(mg::probe::supported));

        shooter = std::make_unique<mc::BasicScreenShooter>(
            scene,
            clock,
            executor,
            gl_providers,
            renderer_factory,
            buffer_allocator,
            std::make_shared<mtd::StubGLConfig>());
    }

    std::unique_ptr<mtd::MockRenderer> next_renderer{std::make_unique<testing::NiceMock<mtd::MockRenderer>>()};

    std::shared_ptr<mtd::MockScene> scene{std::make_shared<NiceMock<mtd::MockScene>>()};
    mg::RenderableList renderables{[]()
        {
            mg::RenderableList renderables;
            for (int i = 0; i < 6; i++)
            {
                renderables.push_back(std::make_shared<mtd::StubRenderable>());
            }
            return renderables;
        }()};
    mc::SceneElementSequence scene_elements{[&]()
        {
            mc::SceneElementSequence elements;
            for (auto& renderable : renderables)
            {
                elements.push_back(std::make_shared<mtd::StubSceneElement>(renderable));
            }
            return elements;
        }()};
    mtd::StubGlRenderingProvider default_gl_behaviour_provider;
    std::shared_ptr<mtd::MockGlRenderingProvider> gl_provider{std::make_shared<testing::NiceMock<mtd::MockGlRenderingProvider>>()};
    std::vector<std::shared_ptr<mg::GLRenderingProvider>> gl_providers{gl_provider};
    std::shared_ptr<MockRendererFactory> renderer_factory{std::make_shared<testing::NiceMock<MockRendererFactory>>()};
    std::shared_ptr<mtd::StubBufferAllocator> buffer_allocator;
    std::shared_ptr<mtd::AdvanceableClock> clock{std::make_shared<mtd::AdvanceableClock>()};
    mtd::ExplicitExecutor executor;
    std::unique_ptr<mc::BasicScreenShooter> shooter;
    std::shared_ptr<mtd::StubBuffer> buffer{std::make_shared<mtd::StubBuffer>(geom::Size{800, 600})};
    geom::Rectangle const viewport_rect{{20, 30}, {40, 50}};
    StrictMock<MockFunction<void(std::optional<mir::time::Timestamp>)>> callback;
    std::optional<mir::time::Timestamp> const nullopt_time{};
};

}

TEST_F(BasicScreenShooter, calls_callback_from_executor)
{
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    clock->advance_by(1s);
    EXPECT_CALL(callback, Call(std::make_optional(clock->now())));
    executor.execute();
}

TEST_F(BasicScreenShooter, renders_scene_elements)
{
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    InSequence seq;
    EXPECT_CALL(*scene, scene_elements_for(_)).WillOnce(Return(scene_elements));
    EXPECT_THAT(renderables.size(), Gt(0));
    EXPECT_CALL(*next_renderer, render(Eq(renderables)));
    EXPECT_CALL(callback, Call(std::make_optional(clock->now())));
    executor.execute();
}

TEST_F(BasicScreenShooter, sets_viewport_correctly_before_render)
{
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    Sequence a, b;
    EXPECT_CALL(*next_renderer, set_viewport(Eq(viewport_rect))).InSequence(b);
    EXPECT_CALL(*next_renderer, render(_)).InSequence(a, b);
    EXPECT_CALL(callback, Call(std::make_optional(clock->now()))).InSequence(a, b);
    executor.execute();
}

TEST_F(BasicScreenShooter, graceful_failure_on_zero_sized_buffer)
{
    auto broken_buffer = std::make_shared<mtd::StubBuffer>(geom::Size{0, 0});
    shooter->capture(broken_buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    EXPECT_CALL(callback, Call(nullopt_time));
    executor.execute();
}

TEST_F(BasicScreenShooter, throw_in_scene_elements_for_causes_graceful_failure)
{
    ON_CALL(*scene, scene_elements_for(_)).WillByDefault(Invoke([](auto) -> mc::SceneElementSequence
        {
            throw std::runtime_error{"throw in scene_elements_for()!"};
        }));
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    EXPECT_CALL(callback, Call(nullopt_time));
    executor.execute();
}

TEST_F(BasicScreenShooter, throw_in_surface_for_output_handled_gracefully)
{
    ON_CALL(*gl_provider, surface_for_sink).WillByDefault(
        [](auto&, auto&) -> std::unique_ptr<mg::gl::OutputSurface>
        {
            BOOST_THROW_EXCEPTION((std::runtime_error{"Throw in surface_for_sink"}));
        });
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    EXPECT_CALL(callback, Call(nullopt_time));
    executor.execute();
}

TEST_F(BasicScreenShooter, throw_in_render_causes_graceful_failure)
{
    EXPECT_CALL(*next_renderer, render(_)).WillOnce([](auto) -> std::unique_ptr<mg::Framebuffer>
        {
            throw std::runtime_error{"throw in render()!"};
        });
    shooter->capture(buffer, viewport_rect, [&](auto time)
        {
            callback.Call(time);
        });
    EXPECT_CALL(callback, Call(nullopt_time));
    executor.execute();
}

TEST_F(BasicScreenShooter, ensures_renderer_is_current_on_only_one_thread)
{
    thread_local bool is_current_here = false;
    std::atomic<bool> is_current = false;

    auto shooter = std::make_unique<mc::BasicScreenShooter>(
        scene,
        clock,
        mir::thread_pool_executor,
        gl_providers,
        renderer_factory,
        buffer_allocator,
        std::make_shared<mtd::StubGLConfig>());

    ON_CALL(*next_renderer, render(_))
        .WillByDefault(
            [&](auto) -> std::unique_ptr<mg::Framebuffer>
            {
                /* It's OK if we're being made current again on the same thread
                 * without having been released previously.
                 * We just need to ensure that, if we are current *anywhere* then
                 * it's *this* thread that we're current on.
                 */
                EXPECT_THAT(is_current_here, Eq(is_current.load()));
                is_current = true;
                is_current_here = true;
                return {};
            });
    ON_CALL(*next_renderer, suspend())
        .WillByDefault(
            [&]()
            {
                EXPECT_THAT(is_current_here, Eq(is_current.load()));
                is_current = false;
                is_current_here = false;
            });

    // This doesn't actually have to be atomic
    std::atomic<int> call_count = 0;
    auto const spawn_a_capture =
        [&]()
        {
            shooter->capture(buffer, viewport_rect, [&](auto) { call_count++; });
        };

    auto const expected_call_count = 20;
    for (auto i = 0; i < expected_call_count; ++i)
    {
        mir::thread_pool_executor.spawn(spawn_a_capture);
    }

    mir::ThreadPoolExecutor::quiesce();
    EXPECT_THAT(call_count, Eq(expected_call_count));
}