~hikiko/mir/mir.unity8-desktop-session

« back to all changes in this revision

Viewing changes to tests/unit-tests/graphics/mesa/test_kms_page_flipper.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Daniel van Vugt, Ubuntu daily release
  • Date: 2014-01-08 02:04:38 UTC
  • mfrom: (1.1.54)
  • Revision ID: package-import@ubuntu.com-20140108020438-ikbu7qqm9v2l026y
Tags: 0.1.3+14.04.20140108-0ubuntu1
[ Daniel van Vugt ]
* Preparing for release 0.1.3

[ Ubuntu daily release ]
* Automatic snapshot from revision 1170

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 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: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "src/platform/graphics/mesa/kms_page_flipper.h"
 
20
 
 
21
#include "mir_test_doubles/mock_drm.h"
 
22
#include "mir_test_doubles/mock_display_report.h"
 
23
#include "mir/graphics/null_display_report.h"
 
24
#include "mir_test/fake_shared.h"
 
25
 
 
26
#include <gtest/gtest.h>
 
27
#include <gmock/gmock.h>
 
28
 
 
29
#include <stdexcept>
 
30
#include <atomic>
 
31
#include <thread>
 
32
#include <unordered_set>
 
33
 
 
34
#include <sys/time.h>
 
35
 
 
36
namespace mg  = mir::graphics;
 
37
namespace mgm = mir::graphics::mesa;
 
38
namespace mt  = mir::test;
 
39
namespace mtd = mir::test::doubles;
 
40
 
 
41
namespace
 
42
{
 
43
 
 
44
class KMSPageFlipperTest : public ::testing::Test
 
45
{
 
46
public:
 
47
    KMSPageFlipperTest()
 
48
        : page_flipper{mock_drm.fake_drm.fd()}
 
49
    {
 
50
    }
 
51
 
 
52
    testing::NiceMock<mtd::MockDRM> mock_drm;
 
53
    mgm::KMSPageFlipper page_flipper;
 
54
};
 
55
 
 
56
ACTION_P(InvokePageFlipHandler, param)
 
57
{
 
58
    int const dont_care{0};
 
59
    char dummy;
 
60
 
 
61
    arg1->page_flip_handler(dont_care, dont_care, dont_care, dont_care, *param);
 
62
    ASSERT_EQ(1, read(arg0, &dummy, 1));
 
63
}
 
64
 
 
65
}
 
66
 
 
67
TEST_F(KMSPageFlipperTest, schedule_flip_calls_drm_page_flip)
 
68
{
 
69
    using namespace testing;
 
70
 
 
71
    uint32_t const crtc_id{10};
 
72
    uint32_t const fb_id{101};
 
73
 
 
74
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(),
 
75
                                          crtc_id, fb_id, _, _))
 
76
        .Times(1);
 
77
 
 
78
    page_flipper.schedule_flip(crtc_id, fb_id);
 
79
}
 
80
 
 
81
TEST_F(KMSPageFlipperTest, double_schedule_flip_throws)
 
82
{
 
83
    using namespace testing;
 
84
 
 
85
    uint32_t const crtc_id{10};
 
86
    uint32_t const fb_id{101};
 
87
 
 
88
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(),
 
89
                                          crtc_id, fb_id, _, _))
 
90
        .Times(1);
 
91
 
 
92
    page_flipper.schedule_flip(crtc_id, fb_id);
 
93
 
 
94
    EXPECT_THROW({
 
95
        page_flipper.schedule_flip(crtc_id, fb_id);
 
96
    }, std::logic_error);
 
97
}
 
98
 
 
99
TEST_F(KMSPageFlipperTest, wait_for_flip_handles_drm_event)
 
100
{
 
101
    using namespace testing;
 
102
 
 
103
    uint32_t const crtc_id{10};
 
104
    uint32_t const fb_id{101};
 
105
    void* user_data{nullptr};
 
106
 
 
107
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(),
 
108
                                          crtc_id, fb_id, _, _))
 
109
        .Times(1)
 
110
        .WillOnce(DoAll(SaveArg<4>(&user_data), Return(0)));
 
111
 
 
112
    EXPECT_CALL(mock_drm, drmHandleEvent(mock_drm.fake_drm.fd(), _))
 
113
        .Times(1)
 
114
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data), Return(0)));
 
115
 
 
116
    page_flipper.schedule_flip(crtc_id, fb_id);
 
117
 
 
118
    /* Fake a DRM event */
 
119
    EXPECT_EQ(1, write(mock_drm.fake_drm.write_fd(), "a", 1));
 
120
 
 
121
    page_flipper.wait_for_flip(crtc_id);
 
122
}
 
123
 
 
124
TEST_F(KMSPageFlipperTest, wait_for_non_scheduled_page_flip_doesnt_block)
 
125
{
 
126
    using namespace testing;
 
127
 
 
128
    uint32_t const crtc_id{10};
 
129
 
 
130
    EXPECT_CALL(mock_drm, drmModePageFlip(_, _, _, _, _))
 
131
        .Times(0);
 
132
 
 
133
    EXPECT_CALL(mock_drm, drmHandleEvent(_, _))
 
134
        .Times(0);
 
135
 
 
136
    page_flipper.wait_for_flip(crtc_id);
 
137
}
 
138
 
 
139
TEST_F(KMSPageFlipperTest, failure_in_wait_for_flip_throws)
 
140
{
 
141
    using namespace testing;
 
142
 
 
143
    uint32_t const crtc_id{10};
 
144
    uint32_t const fb_id{101};
 
145
    void* user_data{nullptr};
 
146
 
 
147
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(),
 
148
                                          crtc_id, fb_id, _, _))
 
149
        .Times(1)
 
150
        .WillOnce(DoAll(SaveArg<4>(&user_data), Return(0)));
 
151
 
 
152
    EXPECT_CALL(mock_drm, drmHandleEvent(_, _))
 
153
        .Times(0);
 
154
 
 
155
    page_flipper.schedule_flip(crtc_id, fb_id);
 
156
 
 
157
    /* Cause a failure in wait_for_flip */
 
158
    close(mock_drm.fake_drm.fd());
 
159
 
 
160
    EXPECT_THROW({
 
161
        page_flipper.wait_for_flip(crtc_id);
 
162
    }, std::runtime_error);
 
163
}
 
164
 
 
165
TEST_F(KMSPageFlipperTest, wait_for_flips_interleaved)
 
166
{
 
167
    using namespace testing;
 
168
 
 
169
    uint32_t const fb_id{101};
 
170
    std::vector<uint32_t> const crtc_ids{10, 11, 12};
 
171
    std::vector<void*> user_data{nullptr, nullptr, nullptr};
 
172
 
 
173
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(),
 
174
                                          _, fb_id, _, _))
 
175
        .Times(3)
 
176
        .WillOnce(DoAll(SaveArg<4>(&user_data[0]), Return(0)))
 
177
        .WillOnce(DoAll(SaveArg<4>(&user_data[1]), Return(0)))
 
178
        .WillOnce(DoAll(SaveArg<4>(&user_data[2]), Return(0)));
 
179
 
 
180
    EXPECT_CALL(mock_drm, drmHandleEvent(mock_drm.fake_drm.fd(), _))
 
181
        .Times(3)
 
182
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[1]), Return(0)))
 
183
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[2]), Return(0)))
 
184
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[0]), Return(0)));
 
185
 
 
186
    for (auto crtc_id : crtc_ids)
 
187
        page_flipper.schedule_flip(crtc_id, fb_id);
 
188
 
 
189
    /* Fake 3 DRM events */
 
190
    EXPECT_EQ(3, write(mock_drm.fake_drm.write_fd(), "abc", 3));
 
191
 
 
192
    for (auto crtc_id : crtc_ids)
 
193
        page_flipper.wait_for_flip(crtc_id);
 
194
}
 
195
 
 
196
namespace
 
197
{
 
198
 
 
199
class PageFlippingFunctor
 
200
{
 
201
public:
 
202
    PageFlippingFunctor(mgm::KMSPageFlipper& page_flipper,
 
203
                        uint32_t crtc_id)
 
204
        : page_flipper(page_flipper), crtc_id{crtc_id}, done{false},
 
205
          num_page_flips{0}, num_waits{0}
 
206
    {
 
207
    }
 
208
 
 
209
    void operator()()
 
210
    {
 
211
        while (!done)
 
212
        {
 
213
            page_flipper.schedule_flip(crtc_id, 0);
 
214
            num_page_flips++;
 
215
            std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
216
            page_flipper.wait_for_flip(crtc_id);
 
217
            num_waits++;
 
218
        }
 
219
    }
 
220
 
 
221
    int page_flip_count()
 
222
    {
 
223
        return num_page_flips;
 
224
    }
 
225
 
 
226
    int wait_count()
 
227
    {
 
228
        return num_waits;
 
229
    }
 
230
 
 
231
    void stop()
 
232
    {
 
233
        done = true;
 
234
    }
 
235
 
 
236
private:
 
237
    mgm::KMSPageFlipper& page_flipper;
 
238
    uint32_t const crtc_id;
 
239
    std::atomic<bool> done;
 
240
    std::atomic<int> num_page_flips;
 
241
    std::atomic<int> num_waits;
 
242
};
 
243
 
 
244
}
 
245
 
 
246
TEST_F(KMSPageFlipperTest, threads_switch_worker)
 
247
{
 
248
    using namespace testing;
 
249
 
 
250
    size_t const worker_index{0};
 
251
    size_t const other_index{1};
 
252
    std::vector<uint32_t> const crtc_ids{10, 11};
 
253
    std::vector<void*> user_data{nullptr, nullptr};
 
254
    std::vector<std::unique_ptr<PageFlippingFunctor>> page_flipping_functors;
 
255
    std::vector<std::thread> page_flipping_threads;
 
256
    std::thread::id tid;
 
257
 
 
258
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(), _, _, _, _))
 
259
        .Times(2)
 
260
        .WillOnce(DoAll(SaveArg<4>(&user_data[worker_index]), Return(0)))
 
261
        .WillOnce(DoAll(SaveArg<4>(&user_data[other_index]), Return(0)));
 
262
 
 
263
    /*
 
264
     * The first event releases the original worker, hence we expect that
 
265
     * then the other thread will become the worker.
 
266
     */
 
267
    EXPECT_CALL(mock_drm, drmHandleEvent(mock_drm.fake_drm.fd(), _))
 
268
        .Times(2)
 
269
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[worker_index]), Return(0)))
 
270
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[other_index]), Return(0)));
 
271
 
 
272
    /* Start the page-flipping threads */
 
273
    for (auto crtc_id : crtc_ids)
 
274
    {
 
275
        auto pf = std::unique_ptr<PageFlippingFunctor>(new PageFlippingFunctor{page_flipper, crtc_id});
 
276
        page_flipping_functors.push_back(std::move(pf));
 
277
        page_flipping_threads.push_back(std::thread{std::ref(*page_flipping_functors.back())});
 
278
 
 
279
        /* Wait for page-flip request and tell flipper to stop after this iteration */
 
280
        while (page_flipping_functors.back()->page_flip_count() == 0)
 
281
            std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
282
        page_flipping_functors.back()->stop();
 
283
 
 
284
        /* Wait until the (first) thread has become the worker */
 
285
        while (tid == std::thread::id())
 
286
        {
 
287
            std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
288
            tid = page_flipper.debug_get_worker_tid();
 
289
        }
 
290
    }
 
291
 
 
292
    EXPECT_EQ(page_flipping_threads[worker_index].get_id(), tid);
 
293
 
 
294
    /* Fake a DRM event */
 
295
    EXPECT_EQ(1, write(mock_drm.fake_drm.write_fd(), "a", 1));
 
296
 
 
297
    page_flipping_threads[worker_index].join();
 
298
 
 
299
    /* Wait for the worker to switch */
 
300
    while (tid != page_flipping_threads[other_index].get_id())
 
301
    {
 
302
        std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
303
        tid = page_flipper.debug_get_worker_tid();
 
304
    }
 
305
 
 
306
    /* Fake another DRM event to unblock the remaining thread */
 
307
    EXPECT_EQ(1, write(mock_drm.fake_drm.write_fd(), "a", 1));
 
308
 
 
309
    page_flipping_threads[other_index].join();
 
310
}
 
311
 
 
312
TEST_F(KMSPageFlipperTest, threads_worker_notifies_non_worker)
 
313
{
 
314
    using namespace testing;
 
315
 
 
316
    size_t const worker_index{0};
 
317
    size_t const other_index{1};
 
318
    std::vector<uint32_t> const crtc_ids{10, 11};
 
319
    std::vector<void*> user_data{nullptr, nullptr};
 
320
    std::vector<std::unique_ptr<PageFlippingFunctor>> page_flipping_functors;
 
321
    std::vector<std::thread> page_flipping_threads;
 
322
    std::thread::id tid;
 
323
 
 
324
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(), _, _, _, _))
 
325
        .Times(2)
 
326
        .WillOnce(DoAll(SaveArg<4>(&user_data[worker_index]), Return(0)))
 
327
        .WillOnce(DoAll(SaveArg<4>(&user_data[other_index]), Return(0)));
 
328
 
 
329
    /*
 
330
     * The first event releases the non-worker thread, hence we expect that
 
331
     * original worker not change.
 
332
     */
 
333
    EXPECT_CALL(mock_drm, drmHandleEvent(mock_drm.fake_drm.fd(), _))
 
334
        .Times(2)
 
335
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[other_index]), Return(0)))
 
336
        .WillOnce(DoAll(InvokePageFlipHandler(&user_data[worker_index]), Return(0)));
 
337
 
 
338
    /* Start the page-flipping threads */
 
339
    for (auto crtc_id : crtc_ids)
 
340
    {
 
341
        auto pf = std::unique_ptr<PageFlippingFunctor>(new PageFlippingFunctor{page_flipper, crtc_id});
 
342
        page_flipping_functors.push_back(std::move(pf));
 
343
        page_flipping_threads.push_back(std::thread{std::ref(*page_flipping_functors.back())});
 
344
 
 
345
        /* Wait for page-flip request and tell flipper to stop after this iteration */
 
346
        while (page_flipping_functors.back()->page_flip_count() == 0)
 
347
            std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
348
        page_flipping_functors.back()->stop();
 
349
 
 
350
        /* Wait until the (first) thread has become the worker */
 
351
        while (tid == std::thread::id())
 
352
        {
 
353
            std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
354
            tid = page_flipper.debug_get_worker_tid();
 
355
        }
 
356
    }
 
357
 
 
358
    EXPECT_EQ(page_flipping_threads[worker_index].get_id(), tid);
 
359
 
 
360
    /* Fake a DRM event */
 
361
    EXPECT_EQ(1, write(mock_drm.fake_drm.write_fd(), "a", 1));
 
362
 
 
363
    /* Wait for the non-worker thread to exit */
 
364
    page_flipping_threads[other_index].join();
 
365
 
 
366
    /* Check that the worker hasn't changed */
 
367
    EXPECT_EQ(page_flipping_threads[worker_index].get_id(),
 
368
              page_flipper.debug_get_worker_tid());
 
369
 
 
370
    /* Fake another DRM event to unblock the remaining thread */
 
371
    EXPECT_EQ(1, write(mock_drm.fake_drm.write_fd(), "a", 1));
 
372
 
 
373
    page_flipping_threads[worker_index].join();
 
374
}
 
375
 
 
376
namespace
 
377
{
 
378
 
 
379
class PageFlipCounter
 
380
{
 
381
public:
 
382
    void add_flip(uint32_t crtc_id, void* user_data)
 
383
    {
 
384
        std::lock_guard<std::mutex> lock{data_mutex};
 
385
 
 
386
        data.push_back({CountType::flip, crtc_id});
 
387
        pending_flips.insert(user_data);
 
388
    }
 
389
 
 
390
    void add_handle_event(uint32_t crtc_id)
 
391
    {
 
392
        std::lock_guard<std::mutex> lock{data_mutex};
 
393
 
 
394
        data.push_back({CountType::handle_event, crtc_id});
 
395
    }
 
396
 
 
397
    int count_flips()
 
398
    {
 
399
        std::lock_guard<std::mutex> lock{data_mutex};
 
400
 
 
401
        return std::count_if(begin(data), end(data), [](CountElement& e) -> bool
 
402
        {
 
403
            return e.type == CountType::flip;
 
404
        });
 
405
    }
 
406
 
 
407
    int count_handle_events()
 
408
    {
 
409
        std::lock_guard<std::mutex> lock{data_mutex};
 
410
 
 
411
        return std::count_if(begin(data), end(data), [](CountElement& e) -> bool
 
412
        {
 
413
            return e.type == CountType::handle_event;
 
414
        });
 
415
    }
 
416
 
 
417
    bool no_consecutive_flips_for_same_crtc_id()
 
418
    {
 
419
        std::lock_guard<std::mutex> lock{data_mutex};
 
420
 
 
421
        std::unordered_set<uint32_t> pending_crtc_ids;
 
422
 
 
423
        for (auto& e : data)
 
424
        {
 
425
            if (e.type == CountType::flip)
 
426
            {
 
427
                if (pending_crtc_ids.find(e.crtc_id) != pending_crtc_ids.end())
 
428
                    return false;
 
429
 
 
430
                pending_crtc_ids.insert(e.crtc_id);
 
431
            }
 
432
            else if (e.type == CountType::handle_event)
 
433
            {
 
434
                if (pending_crtc_ids.find(e.crtc_id) == pending_crtc_ids.end())
 
435
                    return false;
 
436
                pending_crtc_ids.erase(e.crtc_id);
 
437
            }
 
438
        }
 
439
 
 
440
        return true;
 
441
    }
 
442
 
 
443
    void* get_pending_flip_data()
 
444
    {
 
445
        std::lock_guard<std::mutex> lock{data_mutex};
 
446
 
 
447
        auto iter = pending_flips.begin();
 
448
        if (iter == pending_flips.end())
 
449
        {
 
450
            return 0;
 
451
        }
 
452
        else
 
453
        {
 
454
            auto d = *iter;
 
455
            pending_flips.erase(iter);
 
456
            return d;
 
457
        }
 
458
    }
 
459
 
 
460
private:
 
461
    enum class CountType {flip, handle_event};
 
462
    struct CountElement
 
463
    {
 
464
        CountType type;
 
465
        uint32_t crtc_id;
 
466
    };
 
467
 
 
468
    std::vector<CountElement> data;
 
469
    std::unordered_set<void*> pending_flips;
 
470
    std::mutex data_mutex;
 
471
};
 
472
 
 
473
ACTION_P(InvokePageFlipHandlerWithPendingData, counter)
 
474
{
 
475
    int const dont_care{0};
 
476
    int const drm_fd{arg0};
 
477
    char dummy;
 
478
 
 
479
    auto user_data = static_cast<mgm::PageFlipEventData*>(counter->get_pending_flip_data());
 
480
    uint32_t const crtc_id{user_data->crtc_id};
 
481
 
 
482
    /* Remove the event from the drm event queue */
 
483
    ASSERT_EQ(1, read(drm_fd, &dummy, 1));
 
484
    /* Call the page flip handler */
 
485
    arg1->page_flip_handler(dont_care, dont_care, dont_care, dont_care, user_data);
 
486
    /* Record this call */
 
487
    counter->add_handle_event(crtc_id);
 
488
}
 
489
 
 
490
ACTION_P2(AddPageFlipEvent, counter, write_drm_fd)
 
491
{
 
492
    uint32_t const crtc_id{arg0};
 
493
    void* const user_data{arg1};
 
494
 
 
495
    /* Record this call */
 
496
    counter->add_flip(crtc_id, user_data);
 
497
    /* Add an event to the drm event queue */
 
498
    EXPECT_EQ(1, write(write_drm_fd, "a", 1));
 
499
}
 
500
 
 
501
}
 
502
 
 
503
TEST_F(KMSPageFlipperTest, threads_concurrent_page_flips_dont_deadlock)
 
504
{
 
505
    using namespace testing;
 
506
 
 
507
    std::vector<uint32_t> const crtc_ids{10, 11, 12};
 
508
    std::vector<std::unique_ptr<PageFlippingFunctor>> page_flipping_functors;
 
509
    std::vector<std::thread> page_flipping_threads;
 
510
    PageFlipCounter counter;
 
511
 
 
512
    EXPECT_CALL(mock_drm, drmModePageFlip(mock_drm.fake_drm.fd(), _, _, _, _))
 
513
        .WillRepeatedly(DoAll(WithArgs<1,4>(AddPageFlipEvent(&counter, mock_drm.fake_drm.write_fd())),
 
514
                              Return(0)));
 
515
 
 
516
    EXPECT_CALL(mock_drm, drmHandleEvent(mock_drm.fake_drm.fd(), _))
 
517
        .WillRepeatedly(DoAll(InvokePageFlipHandlerWithPendingData(&counter),
 
518
                              Return(0)));
 
519
 
 
520
    /* Set up threads */
 
521
    for (auto crtc_id : crtc_ids)
 
522
    {
 
523
        auto pf = std::unique_ptr<PageFlippingFunctor>(new PageFlippingFunctor{page_flipper, crtc_id});
 
524
        page_flipping_functors.push_back(std::move(pf));
 
525
        page_flipping_threads.push_back(std::thread{std::ref(*page_flipping_functors.back())});
 
526
    }
 
527
 
 
528
    /* Wait for at least min_flips page-flips to be processed */
 
529
    int const min_flips{100};
 
530
 
 
531
    while (counter.count_flips() < min_flips)
 
532
        std::this_thread::sleep_for(std::chrono::milliseconds{1});
 
533
 
 
534
    /* Tell the flippers to stop and wait for them to finish */
 
535
    for (auto& pf : page_flipping_functors)
 
536
        pf->stop();
 
537
 
 
538
    for (auto& pf_thread : page_flipping_threads)
 
539
        pf_thread.join();
 
540
 
 
541
    /* Sanity checks */
 
542
    EXPECT_EQ(counter.count_flips(), counter.count_handle_events());
 
543
    EXPECT_TRUE(counter.no_consecutive_flips_for_same_crtc_id());
 
544
}