~ci-train-bot/mir/mir-ubuntu-yakkety-2783

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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright © 2014 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: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 */

#include "mir_toolkit/mir_client_library.h"
#include "mir_toolkit/debug/surface.h"

#include "mir/compositor/compositor.h"
#include "mir/scene/surface.h"
#include "mir/scene/surface_factory.h"
#include "mir/scene/null_surface_observer.h"
#include "mir/renderer/renderer_factory.h"
#include "mir/graphics/renderable.h"
#include "mir/graphics/buffer.h"
#include "mir/graphics/buffer_id.h"

#include "mir_test_framework/stubbed_server_configuration.h"
#include "mir_test_framework/basic_client_server_fixture.h"
#include "mir_test_framework/any_surface.h"
#include "mir/test/doubles/stub_renderer.h"

#include <gtest/gtest.h>
#include <gmock/gmock.h>

#include <mutex>
#include <condition_variable>

using namespace std::chrono_literals;
namespace mtf = mir_test_framework;
namespace mtd = mir::test::doubles;
namespace mc = mir::compositor;
namespace mg = mir::graphics;
namespace geom = mir::geometry;

namespace
{

struct StubRenderer : mtd::StubRenderer
{
    void render(mg::RenderableList const& renderables) const override
    {
        std::lock_guard<std::mutex> lock{mutex};
        for (auto const& r : renderables)
            rendered_buffers_.push_back(r->buffer()->id());

        if (renderables.size() > 0)
            new_rendered_buffer_cv.notify_all();
    }

    std::vector<mg::BufferID> rendered_buffers()
    {
        std::lock_guard<std::mutex> lock{mutex};
        return rendered_buffers_;
    }

    std::vector<mg::BufferID> wait_for_new_rendered_buffers()
    {
        std::unique_lock<std::mutex> lock{mutex};

        new_rendered_buffer_cv.wait_for(
            lock, std::chrono::seconds{2},
            [this] { return rendered_buffers_.size() != 0; });

        auto const rendered = std::move(rendered_buffers_);
        return rendered;
    }

    mutable std::mutex mutex;
    mutable std::condition_variable new_rendered_buffer_cv;
    mutable std::vector<mg::BufferID> rendered_buffers_;
};

class StubRendererFactory : public mir::renderer::RendererFactory
{
public:
    std::unique_ptr<mir::renderer::Renderer> create_renderer_for(
        mg::DisplayBuffer&) override
    {
        std::lock_guard<std::mutex> lock{mutex};
        renderer_ = new StubRenderer();
        renderer_created_cv.notify_all();
        return std::unique_ptr<mir::renderer::Renderer>{renderer_};
    }

    StubRenderer* renderer()
    {
        std::unique_lock<std::mutex> lock{mutex};

        renderer_created_cv.wait_for(
            lock, std::chrono::seconds{2},
            [this] { return renderer_ != nullptr; });

        return renderer_;
    }

    void clear_renderer()
    {
        std::lock_guard<std::mutex> lock{mutex};
        renderer_ = nullptr;
    }

    std::mutex mutex;
    std::condition_variable renderer_created_cv;
    StubRenderer* renderer_ = nullptr;
};

class PostObserver : public mir::scene::NullSurfaceObserver
{
public:
    PostObserver(std::function<void(int)> cb) : cb{cb}
    {
    }

    void frame_posted(int count, geom::Size const&) override
    {
        cb(count);
    }

private:
    std::function<void(int)> const cb;
};

class PublicSurfaceFactory : public mir::scene::SurfaceFactory
{
public:
    using Surface = mir::scene::Surface;

    PublicSurfaceFactory(std::shared_ptr<mir::scene::SurfaceFactory> const& real) : real_surface_factory{real} {}

    std::shared_ptr<Surface> create_surface(
        std::list<mir::scene::StreamInfo> const& streams,
        mir::scene::SurfaceCreationParameters const& params) override
    {
        latest_surface = real_surface_factory->create_surface(streams, params);
        return latest_surface;
    }

    std::shared_ptr<Surface> const latest() const
    {
        return latest_surface;
    }

    void forget_latest()
    {
        latest_surface.reset();
    }

private:
    std::shared_ptr<mir::scene::SurfaceFactory> const real_surface_factory;
    std::shared_ptr<Surface> latest_surface;
};

struct StubServerConfig : mtf::StubbedServerConfiguration
{
    std::shared_ptr<PublicSurfaceFactory> the_public_surface_factory()
    {
        return public_surface_factory(
                   [this]{ return std::make_shared<PublicSurfaceFactory>(
                           mtf::StubbedServerConfiguration::the_surface_factory()); });
    }

    std::shared_ptr<mir::scene::SurfaceFactory> the_surface_factory() override
    {
        return the_public_surface_factory();
    }

    std::shared_ptr<StubRendererFactory> the_stub_renderer_factory()
    {
        return stub_renderer_factory(
            [] { return std::make_shared<StubRendererFactory>(); });
    }

    std::shared_ptr<mir::renderer::RendererFactory> the_renderer_factory() override
    {
        return the_stub_renderer_factory();
    }

    mir::CachedPtr<StubRendererFactory> stub_renderer_factory;
    mir::CachedPtr<PublicSurfaceFactory> public_surface_factory;
};

using BasicFixture = mtf::BasicClientServerFixture<StubServerConfig>;

struct StaleFrames : BasicFixture,
                     ::testing::WithParamInterface<int>
{
    StaleFrames()
        : post_observer(std::make_shared<PostObserver>(
            [this](int n){frame_posted(n);}))
    {
    }

    void SetUp()
    {
        BasicFixture::SetUp();

        client_create_surface();
        auto pub = server_configuration.the_public_surface_factory();
        auto surface = pub->latest();
        ASSERT_TRUE(!!surface);
        surface->add_observer(post_observer);
        pub->forget_latest();
    }

    void TearDown()
    {
        mir_window_release_sync(window);

        BasicFixture::TearDown();
    }

    void client_create_surface()
    {
        window = mtf::make_any_surface(connection);
        ASSERT_TRUE(mir_window_is_valid(window));
    }

    std::vector<mg::BufferID> wait_for_new_rendered_buffers()
    {
        return server_configuration.the_stub_renderer_factory()->renderer()->wait_for_new_rendered_buffers();
    }

    void stop_compositor()
    {
        server_configuration.the_compositor()->stop();
        server_configuration.the_stub_renderer_factory()->clear_renderer();
    }

    void start_compositor()
    {
        server_configuration.the_compositor()->start();
    }

    /*
     * NOTE that we wait for surface buffer posts as opposed to display posts.
     * The difference is that surface buffer posts will precisely match the
     * number of client swaps for any swap interval, but display posts may be
     * fewer than the number of swaps if the client was quick and using
     * interval zero.
     */
    void frame_posted(int count)
    {
        std::unique_lock<std::mutex> lock(mutex);
        posts += count;
        posted.notify_all();
    }

    bool wait_for_posts(int count, std::chrono::seconds timeout)
    {
        std::unique_lock<std::mutex> lock(mutex);
        auto const deadline = std::chrono::steady_clock::now() + timeout;
        while (posts < count)
        {
            if (posted.wait_until(lock, deadline) == std::cv_status::timeout)
                return false;
        }
        posts -= count;
        return true;
    }

    MirWindow* window;

private:
    std::shared_ptr<PostObserver> post_observer;
    std::mutex mutex;
    std::condition_variable posted;
    int posts = 0;
};

}

TEST_P(StaleFrames, are_dropped_when_restarting_compositor)
{
    using namespace testing;

    stop_compositor();

    std::set<mg::BufferID> stale_buffers;

    stale_buffers.emplace(mir_debug_window_current_buffer_id(window));

    auto bs = mir_window_get_buffer_stream(window);
    mir_buffer_stream_set_swapinterval(bs, GetParam());
    mir_buffer_stream_swap_buffers_sync(bs);

    stale_buffers.emplace(mir_debug_window_current_buffer_id(window));
    mir_buffer_stream_swap_buffers_sync(bs);

    EXPECT_THAT(stale_buffers.size(), Eq(2u));

    auto const fresh_buffer = mg::BufferID{mir_debug_window_current_buffer_id(window)};
    mir_buffer_stream_swap_buffers_sync(bs);

    ASSERT_TRUE(wait_for_posts(3, 60s));
    start_compositor();

    // Note first stale buffer and fresh_buffer may be equal when defaulting to double buffers
    stale_buffers.erase(fresh_buffer);

    auto const new_buffers = wait_for_new_rendered_buffers();
    ASSERT_THAT(new_buffers.size(), Eq(1u));
    EXPECT_THAT(stale_buffers, Not(Contains(new_buffers[0])));
}

TEST_P(StaleFrames, only_fresh_frames_are_used_after_restarting_compositor)
{
    using namespace testing;

    stop_compositor();

    auto bs = mir_window_get_buffer_stream(window);
    mir_buffer_stream_set_swapinterval(bs, GetParam());
    mir_buffer_stream_swap_buffers_sync(bs);
    mir_buffer_stream_swap_buffers_sync(bs);

    auto const fresh_buffer = mg::BufferID{mir_debug_window_current_buffer_id(window)};
    mir_buffer_stream_swap_buffers_sync(bs);

    ASSERT_TRUE(wait_for_posts(3, 60s));
    start_compositor();

    auto const new_buffers = wait_for_new_rendered_buffers();
    ASSERT_THAT(new_buffers.size(), Eq(1u));
    EXPECT_THAT(new_buffers[0], Eq(fresh_buffer));
}

INSTANTIATE_TEST_CASE_P(PerSwapInterval, StaleFrames, ::testing::Values(0,1));