-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathdisplay_buffer.cpp
506 lines (443 loc) · 15.5 KB
/
display_buffer.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "display_sink.h"
#include "kms_cpu_addressable_display_provider.h"
#include "kms_output.h"
#include "cpu_addressable_fb.h"
#include "gbm_display_allocator.h"
#include "mir/fd.h"
#include "mir/graphics/display_report.h"
#include "mir/graphics/drm_formats.h"
#include "mir/graphics/platform.h"
#include "mir/log.h"
#include "mir/graphics/dmabuf_buffer.h"
#include <algorithm>
#include <boost/throw_exception.hpp>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <cstdint>
#include <drm_fourcc.h>
#include <drm_mode.h>
#include <gbm.h>
#include <memory>
#include <stdexcept>
#include <chrono>
#include <xf86drm.h>
#include <xf86drmMode.h>
namespace mg = mir::graphics;
namespace mga = mir::graphics::atomic;
namespace geom = mir::geometry;
namespace mgk = mir::graphics::kms;
mga::DisplaySink::DisplaySink(
mir::Fd drm_fd,
std::shared_ptr<struct gbm_device> gbm,
std::shared_ptr<mgk::DRMEventHandler> event_handler,
mga::BypassOption,
std::shared_ptr<DisplayReport> const& listener,
std::vector<std::shared_ptr<KMSOutput>> const& outputs,
geom::Rectangle const& area,
glm::mat2 const& transformation)
: DmaBufDisplayAllocator(gbm, drm_fd),
gbm{std::move(gbm)},
listener(listener),
outputs(outputs),
event_handler{std::move(event_handler)},
area(area),
transform{transformation},
needs_set_crtc{false}
{
listener->report_successful_setup_of_native_resources();
// If any of the outputs have a CRTC mismatch, we will want to set all of them
// so that they're all showing the same buffer.
bool has_crtc_mismatch = false;
for (auto& output : outputs)
{
has_crtc_mismatch = output->has_crtc_mismatch();
if (has_crtc_mismatch)
break;
}
if (has_crtc_mismatch)
{
mir::log_info("Clearing screen due to differing encountered and target modes");
// TODO: Pull a supported format out of KMS rather than assuming XRGB8888
auto initial_fb = std::make_shared<graphics::CPUAddressableFB>(
std::move(drm_fd),
false,
DRMFormat{DRM_FORMAT_XRGB8888},
area.size);
auto mapping = initial_fb->map_writeable();
::memset(mapping->data(), 24, mapping->len());
visible_fb = std::move(initial_fb);
for (auto &output: outputs) {
output->set_crtc(*visible_fb);
}
listener->report_successful_drm_mode_set_crtc_on_construction();
}
listener->report_successful_display_construction();
}
mga::DisplaySink::~DisplaySink() = default;
geom::Rectangle mga::DisplaySink::view_area() const
{
return area;
}
glm::mat2 mga::DisplaySink::transformation() const
{
return transform;
}
void mga::DisplaySink::set_transformation(glm::mat2 const& t, geometry::Rectangle const& a)
{
transform = t;
area = a;
}
bool mga::DisplaySink::overlay(std::vector<DisplayElement> const& renderable_list)
{
// TODO: implement more than the most basic case.
if (renderable_list.size() != 1)
{
return false;
}
if (renderable_list[0].screen_positon != view_area())
{
return false;
}
if (renderable_list[0].source_position.top_left != geom::PointF {0,0} ||
renderable_list[0].source_position.size.width.as_value() != view_area().size.width.as_int() ||
renderable_list[0].source_position.size.height.as_value() != view_area().size.height.as_int())
{
return false;
}
if (auto fb = std::dynamic_pointer_cast<graphics::FBHandle>(renderable_list[0].buffer))
{
next_swap = std::move(fb);
return true;
}
return false;
}
void mga::DisplaySink::for_each_display_sink(std::function<void(graphics::DisplaySink&)> const& f)
{
f(*this);
}
void mga::DisplaySink::set_crtc(FBHandle const& forced_frame)
{
for (auto& output : outputs)
{
/*
* Note that failure to set the CRTC is not a fatal error. This can
* happen under normal conditions when resizing VirtualBox (which
* actually removes and replaces the virtual output each time so
* sometimes it's really not there). Xorg often reports similar
* errors, and it's not fatal.
*/
if (!output->set_crtc(forced_frame))
mir::log_error("Failed to set DRM CRTC. "
"Screen contents may be incomplete. "
"Try plugging the monitor in again.");
}
}
void mga::DisplaySink::post()
{
/*
* We might not have waited for the previous frame to page flip yet.
* This is good because it maximizes the time available to spend rendering
* each frame. Just remember wait_for_page_flip() must be called at some
* point before the next schedule_page_flip().
*/
wait_for_page_flip();
if (!next_swap)
{
// Hey! No one has given us a next frame yet, so we don't have to change what's onscreen.
// Sweet! We can just bail.
return;
}
/*
* Otherwise, pull the next frame into the pending slot
*/
scheduled_fb = std::move(next_swap);
next_swap = nullptr;
/*
* Try to schedule a page flip as first preference to avoid tearing.
* [will complete in a background thread]
*/
if (!needs_set_crtc && !schedule_page_flip(*scheduled_fb))
needs_set_crtc = true;
/*
* Fallback blitting: Not pretty, since it may tear. VirtualBox seems
* to need to do this on every frame. [will complete in this thread]
*/
if (needs_set_crtc)
{
set_crtc(*scheduled_fb);
// SetCrtc is immediate, so the FB is now visible and we have nothing pending
visible_fb = std::move(scheduled_fb);
scheduled_fb = nullptr;
needs_set_crtc = false;
}
using namespace std::chrono_literals; // For operator""ms()
// Predicted worst case render time for the next frame...
auto predicted_render_time = 50ms;
if (holding_client_buffers)
{
/*
* For composited frames we defer wait_for_page_flip till just before
* the next frame, but not for bypass frames. Deferring the flip of
* bypass frames would increase the time we held
* visible_bypass_frame unacceptably, resulting in client stuttering
* unless we allocate more buffers (which I'm trying to avoid).
* Also, bypass does not need the deferred page flip because it has
* no compositing/rendering step for which to save time for.
*/
wait_for_page_flip();
// It's very likely the next frame will be bypassed like this one so
// we only need time for kernel page flip scheduling...
predicted_render_time = 5ms;
}
else
{
/*
* Not in clone mode? We can afford to wait for the page flip then,
* making us double-buffered (noticeably less laggy than the triple
* buffering that clone mode requires).
*/
if (outputs.size() == 1)
wait_for_page_flip();
/*
* TODO: If you're optimistic about your GPU performance and/or
* measure it carefully you may wish to set predicted_render_time
* to a lower value here for lower latency.
*
*predicted_render_time = 9ms; // e.g. about the same as Weston
*/
}
recommend_sleep = 0ms;
if (outputs.size() == 1)
{
auto const& output = outputs.front();
auto const min_frame_interval = 1000ms / output->max_refresh_rate();
if (predicted_render_time < min_frame_interval)
recommend_sleep = min_frame_interval - predicted_render_time;
}
}
std::chrono::milliseconds mga::DisplaySink::recommended_sleep() const
{
return recommend_sleep;
}
bool mga::DisplaySink::schedule_page_flip(FBHandle const& bufobj)
{
/*
* Schedule the current front buffer object for display. Note that
* the page flip is asynchronous and synchronized with vertical refresh.
*/
/* TODO: This works badly if *some* outputs successfully flipped and
* others did not. We should instead have exactly one KMSOutput per DisplaySink
*/
for (auto& output : outputs)
{
if (output->schedule_page_flip(bufobj))
{
pending_flips.push_back(output.get());
}
}
return !pending_flips.empty();
}
void mga::DisplaySink::wait_for_page_flip()
{
if (!pending_flips.empty())
{
for (auto pending_flip : pending_flips)
{
pending_flip->wait_for_page_flip();
}
pending_flips.clear();
// The previously-scheduled FB has been page-flipped, and is now visible
visible_fb = std::move(scheduled_fb);
scheduled_fb = nullptr;
}
}
void mga::DisplaySink::schedule_set_crtc()
{
needs_set_crtc = true;
}
auto mga::DisplaySink::drm_fd() const -> mir::Fd
{
return mir::Fd{mir::IntOwnedFd{outputs.front()->drm_fd()}};
}
auto mga::DisplaySink::gbm_device() const -> std::shared_ptr<struct gbm_device>
{
return gbm;
}
void mga::DisplaySink::set_next_image(std::unique_ptr<Framebuffer> content)
{
std::vector<DisplayElement> const single_buffer = {
DisplayElement {
view_area(),
geom::RectangleF{
{0, 0},
{view_area().size.width.as_value(), view_area().size.height.as_value()}},
std::move(content)
}
};
if (!overlay(single_buffer))
{
// Oh, oh! We should be *guaranteed* to “overlay” a single Framebuffer; this is likely a programming error
BOOST_THROW_EXCEPTION((std::runtime_error{"Failed to post buffer to display"}));
}
}
namespace {
auto const MAX_PLANES = 4zu;
auto get_import_buffers(std::shared_ptr<mg::DMABufBuffer> buffer)
-> std::tuple<std::array<int, 4>, std::array<int, 4>, std::array<int, 4>, std::array<uint64_t, 4>>
{
auto const plane_descriptors = buffer->planes();
assert(plane_descriptors.size() <= MAX_PLANES);
// std::array becuase we can't really return int[4]
std::array<int, 4> dmabuf_fds = {0};
std::array<int, 4> pitches = {0};
std::array<int, 4> offsets = {0};
std::array<uint64_t, 4> modifiers = {0};
for (std::size_t i = 0; i < std::min(MAX_PLANES, plane_descriptors.size()); i++)
{
dmabuf_fds[i] = plane_descriptors[i].dma_buf;
pitches[i] = plane_descriptors[i].stride;
offsets[i] = plane_descriptors[i].offset;
modifiers[i] = buffer->modifier().value_or(DRM_FORMAT_MOD_INVALID);
}
return std::make_tuple(dmabuf_fds, pitches, offsets, modifiers);
}
auto import_gbm_bo(
std::shared_ptr<struct gbm_device> gbm,
std::shared_ptr<mg::DMABufBuffer> buffer,
std::array<int, 4> dmabuf_fds,
std::array<int, 4> pitches,
std::array<int, 4> offsets) -> std::shared_ptr<struct gbm_bo>
{
auto const plane_descriptors = buffer->planes();
gbm_import_fd_modifier_data import_data = {
.width = buffer->size().width.as_uint32_t(),
.height = buffer->size().height.as_uint32_t(),
.format = buffer->format(),
.num_fds = static_cast<uint32_t>(std::min(plane_descriptors.size(), MAX_PLANES)),
.fds = {dmabuf_fds[0], dmabuf_fds[1], dmabuf_fds[2], dmabuf_fds[3]},
.strides = {pitches[0], pitches[1], pitches[2], pitches[3]},
.offsets = {offsets[0], offsets[1], offsets[2], offsets[3]},
.modifier = buffer->modifier().value_or(DRM_FORMAT_MOD_NONE),
};
auto* gbm_bo = gbm_bo_import(gbm.get(), GBM_BO_IMPORT_FD_MODIFIER, (void*)&import_data, GBM_BO_USE_SCANOUT);
return std::shared_ptr<struct gbm_bo>(gbm_bo, &gbm_bo_destroy);
}
auto drm_fb_id_from_dma_buffer(
mir::Fd drm_fd, std::shared_ptr<struct gbm_device> const gbm, std::shared_ptr<mg::DMABufBuffer> buffer)
-> std::shared_ptr<uint32_t>
{
auto [dmabuf_fds, pitches, offsets, modifiers] = get_import_buffers(buffer);
auto gbm_bo = import_gbm_bo(gbm, buffer, dmabuf_fds, pitches, offsets);
if (!gbm_bo)
{
mir::log_warning("Failed to import buffer");
return {};
}
auto const plane_descriptors = buffer->planes();
uint32_t gem_handles[MAX_PLANES] = {0};
for (std::size_t i = 0; i < std::min(MAX_PLANES, plane_descriptors.size()); i++)
gem_handles[i] = gbm_bo_get_handle_for_plane(gbm_bo.get(), i).u32;
// Note that we capture `gbm_bo` so that its lifetime matches that of the fb_id
auto fb_id = std::shared_ptr<uint32_t>{
new uint32_t{0},
[drm_fd, gbm_bo](uint32_t* fb_id)
{
if (*fb_id)
drmModeRmFB(drm_fd, *fb_id);
delete fb_id;
}};
auto [width, height] = buffer->size();
int ret = drmModeAddFB2WithModifiers(
drm_fd,
width.as_uint32_t(),
height.as_uint32_t(),
buffer->format(),
gem_handles,
reinterpret_cast<uint32_t*>(pitches.data()),
reinterpret_cast<uint32_t*>(offsets.data()),
modifiers.data(),
fb_id.get(),
DRM_MODE_FB_MODIFIERS);
if (ret)
{
mir::log_warning("drmModeAddFB2WithModifiers returned an error: %d", ret);
return {};
}
return fb_id;
}
}
auto mga::DmaBufDisplayAllocator::framebuffer_for(std::shared_ptr<DMABufBuffer> buffer) -> std::unique_ptr<Framebuffer>
{
auto fb_id = drm_fb_id_from_dma_buffer(drm_fd(), gbm, buffer);
if (!fb_id)
{
return {};
}
struct AtomicKmsFbHandle : public mg::FBHandle
{
AtomicKmsFbHandle(std::shared_ptr<uint32_t> fb_handle, geometry::Size size) :
kms_fb_id{fb_handle},
size_{size}
{
}
virtual auto size() const -> geometry::Size override
{
return size_;
}
virtual operator uint32_t() const override
{
return *kms_fb_id;
}
private:
std::shared_ptr<uint32_t> kms_fb_id;
geometry::Size size_;
};
buffer->on_consumed();
return std::make_unique<AtomicKmsFbHandle>(fb_id, buffer->size());
}
auto mga::DisplaySink::maybe_create_allocator(DisplayAllocator::Tag const& type_tag)
-> DisplayAllocator*
{
if (dynamic_cast<mg::CPUAddressableDisplayAllocator::Tag const*>(&type_tag))
{
if (!kms_allocator)
{
kms_allocator = kms::CPUAddressableDisplayAllocator::create_if_supported(drm_fd(), outputs.front()->size());
}
return kms_allocator.get();
}
if (dynamic_cast<mg::GBMDisplayAllocator::Tag const*>(&type_tag))
{
if (!gbm_allocator)
{
gbm_allocator = std::make_unique<GBMDisplayAllocator>(drm_fd(), gbm, outputs.front()->size());
}
return gbm_allocator.get();
}
if(dynamic_cast<DmaBufDisplayAllocator::Tag const*>(&type_tag))
{
if (!bypass_allocator)
{
bypass_allocator = std::make_shared<DmaBufDisplayAllocator>(gbm, drm_fd());
}
return bypass_allocator.get();
}
return nullptr;
}