forked from kalyankondapally/external-drm_hwcomposer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drmdisplaycompositor.cpp
951 lines (808 loc) · 27.5 KB
/
drmdisplaycompositor.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
#define LOG_TAG "hwc-drm-display-compositor"
#include "drmdisplaycompositor.h"
#include <pthread.h>
#include <sched.h>
#include <stdlib.h>
#include <time.h>
#include <sstream>
#include <vector>
#include <cutils/log.h>
#include <drm/drm_mode.h>
#include <sync/sync.h>
#include <utils/Trace.h>
#include "autolock.h"
#include "drmcrtc.h"
#include "drmplane.h"
#include "drmresources.h"
#include "glworker.h"
namespace android {
void SquashState::Init(DrmHwcLayer *layers, size_t num_layers) {
generation_number_++;
valid_history_ = 0;
regions_.clear();
last_handles_.clear();
std::vector<DrmHwcRect<int>> in_rects;
for (size_t i = 0; i < num_layers; i++) {
DrmHwcLayer *layer = &layers[i];
in_rects.emplace_back(layer->display_frame);
last_handles_.push_back(layer->sf_handle);
}
std::vector<separate_rects::RectSet<uint64_t, int>> out_regions;
separate_rects::separate_rects_64(in_rects, &out_regions);
for (const separate_rects::RectSet<uint64_t, int> &out_region : out_regions) {
regions_.emplace_back();
Region ®ion = regions_.back();
region.rect = out_region.rect;
region.layer_refs = out_region.id_set.getBits();
}
}
void SquashState::GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
std::vector<bool> &changed_regions) const {
changed_regions.resize(regions_.size());
if (num_layers != last_handles_.size()) {
ALOGE("SquashState::GenerateHistory expected %zu layers but got %zu layers",
last_handles_.size(), num_layers);
return;
}
std::bitset<kMaxLayers> changed_layers;
for (size_t i = 0; i < last_handles_.size(); i++) {
DrmHwcLayer *layer = &layers[i];
// Protected layers can't be squashed so we treat them as constantly
// changing.
if (layer->protected_usage() || last_handles_[i] != layer->sf_handle)
changed_layers.set(i);
}
for (size_t i = 0; i < regions_.size(); i++) {
changed_regions[i] = (regions_[i].layer_refs & changed_layers).any();
}
}
void SquashState::StableRegionsWithMarginalHistory(
const std::vector<bool> &changed_regions,
std::vector<bool> &stable_regions) const {
stable_regions.resize(regions_.size());
for (size_t i = 0; i < regions_.size(); i++) {
stable_regions[i] = !changed_regions[i] && is_stable(i);
}
}
void SquashState::RecordHistory(DrmHwcLayer *layers, size_t num_layers,
const std::vector<bool> &changed_regions) {
if (num_layers != last_handles_.size()) {
ALOGE("SquashState::RecordHistory expected %zu layers but got %zu layers",
last_handles_.size(), num_layers);
return;
}
if (changed_regions.size() != regions_.size()) {
ALOGE("SquashState::RecordHistory expected %zu regions but got %zu regions",
regions_.size(), changed_regions.size());
return;
}
for (size_t i = 0; i < last_handles_.size(); i++) {
DrmHwcLayer *layer = &layers[i];
last_handles_[i] = layer->sf_handle;
}
for (size_t i = 0; i < regions_.size(); i++) {
regions_[i].change_history <<= 1;
regions_[i].change_history.set(/* LSB */ 0, changed_regions[i]);
}
valid_history_++;
}
bool SquashState::RecordAndCompareSquashed(
const std::vector<bool> &squashed_regions) {
if (squashed_regions.size() != regions_.size()) {
ALOGE(
"SquashState::RecordAndCompareSquashed expected %zu regions but got "
"%zu regions",
regions_.size(), squashed_regions.size());
return false;
}
bool changed = false;
for (size_t i = 0; i < regions_.size(); i++) {
if (regions_[i].squashed != squashed_regions[i]) {
regions_[i].squashed = squashed_regions[i];
changed = true;
}
}
return changed;
}
void SquashState::Dump(std::ostringstream *out) const {
*out << "----SquashState generation=" << generation_number_
<< " history=" << valid_history_ << "\n"
<< " Regions: count=" << regions_.size() << "\n";
for (size_t i = 0; i < regions_.size(); i++) {
const Region ®ion = regions_[i];
*out << " [" << i << "]"
<< " history=" << region.change_history << " rect";
region.rect.Dump(out);
*out << " layers=(";
bool first = true;
for (size_t layer_index = 0; layer_index < kMaxLayers; layer_index++) {
if ((region.layer_refs &
std::bitset<kMaxLayers>((size_t)1 << layer_index))
.any()) {
if (!first)
*out << " ";
first = false;
*out << layer_index;
}
}
*out << ")";
if (region.squashed)
*out << " squashed";
*out << "\n";
}
}
static bool UsesSquash(const std::vector<DrmCompositionPlane> &comp_planes) {
return std::any_of(comp_planes.begin(), comp_planes.end(),
[](const DrmCompositionPlane &plane) {
return plane.type() == DrmCompositionPlane::Type::kSquash;
});
}
DrmDisplayCompositor::DrmDisplayCompositor()
: drm_(NULL),
display_(-1),
initialized_(false),
active_(false),
use_hw_overlays_(true),
framebuffer_index_(0),
squash_framebuffer_index_(0),
dump_frames_composited_(0),
dump_last_timestamp_ns_(0) {
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
return;
dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
}
DrmDisplayCompositor::~DrmDisplayCompositor() {
if (!initialized_)
return;
int ret = pthread_mutex_lock(&lock_);
if (ret)
ALOGE("Failed to acquire compositor lock %d", ret);
if (mode_.blob_id)
drm_->DestroyPropertyBlob(mode_.blob_id);
if (mode_.old_blob_id)
drm_->DestroyPropertyBlob(mode_.old_blob_id);
active_composition_.reset();
ret = pthread_mutex_unlock(&lock_);
if (ret)
ALOGE("Failed to acquire compositor lock %d", ret);
pthread_mutex_destroy(&lock_);
}
int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
drm_ = drm;
display_ = display;
int ret = pthread_mutex_init(&lock_, NULL);
if (ret) {
ALOGE("Failed to initialize drm compositor lock %d\n", ret);
return ret;
}
initialized_ = true;
return 0;
}
std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
const {
return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
}
std::tuple<uint32_t, uint32_t, int>
DrmDisplayCompositor::GetActiveModeResolution() {
DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
if (connector == NULL) {
ALOGE("Failed to determine display mode: no connector for display %d",
display_);
return std::make_tuple(0, 0, -ENODEV);
}
const DrmMode &mode = connector->active_mode();
return std::make_tuple(mode.h_display(), mode.v_display(), 0);
}
int DrmDisplayCompositor::PrepareFramebuffer(
DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
int ret = fb.WaitReleased(-1);
if (ret) {
ALOGE("Failed to wait for framebuffer release %d", ret);
return ret;
}
uint32_t width, height;
std::tie(width, height, ret) = GetActiveModeResolution();
if (ret) {
ALOGE(
"Failed to allocate framebuffer because the display resolution could "
"not be determined %d",
ret);
return ret;
}
fb.set_release_fence_fd(-1);
if (!fb.Allocate(width, height)) {
ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
return -ENOMEM;
}
display_comp->layers().emplace_back();
DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
pre_comp_layer.sf_handle = fb.buffer()->handle;
pre_comp_layer.blending = DrmHwcBlending::kPreMult;
pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
display_comp->importer());
if (ret) {
ALOGE("Failed to import framebuffer for display %d", ret);
return ret;
}
return ret;
}
int DrmDisplayCompositor::ApplySquash(DrmDisplayComposition *display_comp) {
int ret = 0;
DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
ret = PrepareFramebuffer(fb, display_comp);
if (ret) {
ALOGE("Failed to prepare framebuffer for squash %d", ret);
return ret;
}
std::vector<DrmCompositionRegion> ®ions = display_comp->squash_regions();
ret = pre_compositor_->Composite(display_comp->layers().data(),
regions.data(), regions.size(), fb.buffer(),
display_comp->importer());
pre_compositor_->Finish();
if (ret) {
ALOGE("Failed to squash layers");
return ret;
}
ret = display_comp->CreateNextTimelineFence();
if (ret <= 0) {
ALOGE("Failed to create squash framebuffer release fence %d", ret);
return ret;
}
fb.set_release_fence_fd(ret);
display_comp->SignalSquashDone();
return 0;
}
int DrmDisplayCompositor::ApplyPreComposite(
DrmDisplayComposition *display_comp) {
int ret = 0;
DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
ret = PrepareFramebuffer(fb, display_comp);
if (ret) {
ALOGE("Failed to prepare framebuffer for pre-composite %d", ret);
return ret;
}
std::vector<DrmCompositionRegion> ®ions = display_comp->pre_comp_regions();
ret = pre_compositor_->Composite(display_comp->layers().data(),
regions.data(), regions.size(), fb.buffer(),
display_comp->importer());
pre_compositor_->Finish();
if (ret) {
ALOGE("Failed to pre-composite layers");
return ret;
}
ret = display_comp->CreateNextTimelineFence();
if (ret <= 0) {
ALOGE("Failed to create pre-composite framebuffer release fence %d", ret);
return ret;
}
fb.set_release_fence_fd(ret);
display_comp->SignalPreCompDone();
return 0;
}
int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
if (!pset) {
ALOGE("Failed to allocate property set");
return -ENOMEM;
}
int ret;
std::vector<DrmCompositionPlane> &comp_planes =
display_comp->composition_planes();
for (DrmCompositionPlane &comp_plane : comp_planes) {
DrmPlane *plane = comp_plane.plane();
ret = plane->Disable(pset);
if (ret) {
drmModeAtomicFree(pset);
return ret;
}
}
ret = drmModeAtomicCommit(drm_->fd(), pset, 0, drm_);
if (ret) {
ALOGE("Failed to commit pset ret=%d\n", ret);
drmModeAtomicFree(pset);
return ret;
}
drmModeAtomicFree(pset);
return 0;
}
int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) {
int ret = 0;
std::vector<DrmHwcLayer> &layers = display_comp->layers();
std::vector<DrmCompositionPlane> &comp_planes =
display_comp->composition_planes();
std::vector<DrmCompositionRegion> &squash_regions =
display_comp->squash_regions();
std::vector<DrmCompositionRegion> &pre_comp_regions =
display_comp->pre_comp_regions();
if (!pre_compositor_) {
pre_compositor_.reset(new GLWorkerCompositor());
int ret = pre_compositor_->Init();
if (ret) {
ALOGE("Failed to initialize OpenGL compositor %d", ret);
return ret;
}
}
int squash_layer_index = -1;
if (squash_regions.size() > 0) {
squash_framebuffer_index_ = (squash_framebuffer_index_ + 1) % 2;
ret = ApplySquash(display_comp);
if (ret)
return ret;
squash_layer_index = layers.size() - 1;
} else {
if (UsesSquash(comp_planes)) {
DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
layers.emplace_back();
squash_layer_index = layers.size() - 1;
DrmHwcLayer &squash_layer = layers.back();
ret = squash_layer.buffer.ImportBuffer(fb.buffer()->handle,
display_comp->importer());
if (ret) {
ALOGE("Failed to import old squashed framebuffer %d", ret);
return ret;
}
squash_layer.sf_handle = fb.buffer()->handle;
squash_layer.blending = DrmHwcBlending::kPreMult;
squash_layer.source_crop = DrmHwcRect<float>(
0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
squash_layer.display_frame = DrmHwcRect<int>(
0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
ret = display_comp->CreateNextTimelineFence();
if (ret <= 0) {
ALOGE("Failed to create squash framebuffer release fence %d", ret);
return ret;
}
fb.set_release_fence_fd(ret);
ret = 0;
}
}
bool do_pre_comp = pre_comp_regions.size() > 0;
int pre_comp_layer_index = -1;
if (do_pre_comp) {
ret = ApplyPreComposite(display_comp);
if (ret)
return ret;
pre_comp_layer_index = layers.size() - 1;
framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
}
for (DrmCompositionPlane &comp_plane : comp_planes) {
std::vector<size_t> &source_layers = comp_plane.source_layers();
switch (comp_plane.type()) {
case DrmCompositionPlane::Type::kSquash:
if (source_layers.size())
ALOGE("Squash source_layers is expected to be empty (%zu/%d)",
source_layers[0], squash_layer_index);
source_layers.push_back(squash_layer_index);
break;
case DrmCompositionPlane::Type::kPrecomp:
if (!do_pre_comp) {
ALOGE(
"Can not use pre composite framebuffer with no pre composite "
"regions");
return -EINVAL;
}
// Replace source_layers with the output of the precomposite
source_layers.clear();
source_layers.push_back(pre_comp_layer_index);
break;
default:
break;
}
}
return ret;
}
int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
bool test_only) {
ATRACE_CALL();
int ret = 0;
std::vector<DrmHwcLayer> &layers = display_comp->layers();
std::vector<DrmCompositionPlane> &comp_planes =
display_comp->composition_planes();
std::vector<DrmCompositionRegion> &pre_comp_regions =
display_comp->pre_comp_regions();
uint64_t out_fences[drm_->GetCrtcCount()];
DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
if (!connector) {
ALOGE("Could not locate connector for display %d", display_);
return -ENODEV;
}
DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
if (!crtc) {
ALOGE("Could not locate crtc for display %d", display_);
return -ENODEV;
}
drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
if (!pset) {
ALOGE("Failed to allocate property set");
return -ENOMEM;
}
if (crtc->out_fence_ptr_property().id() != 0) {
ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->out_fence_ptr_property().id(),
(uint64_t) &out_fences[0]);
if (ret < 0) {
ALOGE("Failed to add OUT_FENCE_PTR property to pset: %d", ret);
drmModeAtomicFree(pset);
return ret;
}
}
if (mode_.needs_modeset) {
ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(),
mode_.blob_id) < 0 ||
drmModeAtomicAddProperty(pset, connector->id(),
connector->crtc_id_property().id(),
crtc->id()) < 0;
if (ret) {
ALOGE("Failed to add blob %d to pset", mode_.blob_id);
drmModeAtomicFree(pset);
return ret;
}
}
for (DrmCompositionPlane &comp_plane : comp_planes) {
DrmPlane *plane = comp_plane.plane();
std::vector<size_t> &source_layers = comp_plane.source_layers();
int fb_id = -1;
if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
if (source_layers.size() > 1) {
ALOGE("Can't handle more than one source layer sz=%zu type=%d",
source_layers.size(), comp_plane.type());
continue;
}
if (source_layers.empty() || source_layers.front() >= layers.size()) {
ALOGE("Source layer index %zu out of bounds %zu type=%d",
source_layers.front(), layers.size(), comp_plane.type());
break;
}
DrmHwcLayer &layer = layers[source_layers.front()];
if (!layer.buffer) {
ALOGE("Expected a valid framebuffer for pset");
break;
}
fb_id = layer.buffer->fb_id;
if (fb_id == 0) {
ret = layer.buffer.CreateFrameBuffer(plane->type());
if (ret) {
ALOGE("Failed to Create Framebuffer.");
break;
}
fb_id = layer.buffer->fb_id;
}
if (fb_id > 0) {
ret = plane->UpdateProperties(pset, crtc->id(), layer);
if (ret) {
ALOGE("Failed to update Plane.");
break;
}
}
}
// Disable the plane if there's no framebuffer
if (fb_id < 0) {
ret = plane->Disable(pset);
if (ret)
break;
continue;
}
if (ret)
break;
}
out:
if (!ret) {
uint32_t flags = 0;
if (test_only) {
flags |= DRM_MODE_ATOMIC_TEST_ONLY;
} else if (mode_.needs_modeset) {
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
#ifndef USE_DISABLE_OVERLAY_USAGE
} else {
flags |= DRM_MODE_ATOMIC_NONBLOCK;
#endif
}
ret = drmModeAtomicCommit(drm_->fd(), pset, flags, drm_);
if (ret) {
if (test_only)
ALOGI("Commit test pset failed ret=%d\n", ret);
else {
// TODO(kalyank): The only reason we should be
// failing here is driver reporting EBUSY. We
// should keep track of Atomic Events and update
// the commits Asynchronously. For now do a
// blocking commit in case the non-blocking commit
// failed.
if (!mode_.needs_modeset)
ret = drmModeAtomicCommit(drm_->fd(), pset, 0, drm_);
if (ret) {
ALOGE("Failed to commit pset ret=%d\n", ret);
}
}
drmModeAtomicFree(pset);
return ret;
}
}
if (pset)
drmModeAtomicFree(pset);
if (!test_only && mode_.needs_modeset) {
ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
if (ret) {
ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
mode_.old_blob_id, ret);
return ret;
}
/* TODO: Add dpms to the pset when the kernel supports it */
ret = ApplyDpms(display_comp);
if (ret) {
ALOGE("Failed to apply DPMS after modeset %d\n", ret);
return ret;
}
connector->set_active_mode(mode_.mode);
mode_.old_blob_id = mode_.blob_id;
mode_.blob_id = 0;
mode_.needs_modeset = false;
}
if (crtc->out_fence_ptr_property().id()) {
display_comp->set_out_fence((int) out_fences[crtc->pipe()]);
close((int) out_fences[crtc->pipe()]);
}
return ret;
}
int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
if (!conn) {
ALOGE("Failed to get DrmConnector for display %d", display_);
return -ENODEV;
}
const DrmProperty &prop = conn->dpms_property();
int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
display_comp->dpms_mode());
if (ret) {
ALOGE("Failed to set DPMS property for connector %d", conn->id());
return ret;
}
return 0;
}
std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
const DrmMode &mode) {
struct drm_mode_modeinfo drm_mode;
memset(&drm_mode, 0, sizeof(drm_mode));
mode.ToDrmModeModeInfo(&drm_mode);
uint32_t id = 0;
int ret = drm_->CreatePropertyBlob(&drm_mode,
sizeof(struct drm_mode_modeinfo), &id);
if (ret) {
ALOGE("Failed to create mode property blob %d", ret);
return std::make_tuple(ret, 0);
}
ALOGE("Create blob_id %" PRIu32 "\n", id);
return std::make_tuple(ret, id);
}
void DrmDisplayCompositor::ClearDisplay() {
AutoLock lock(&lock_, "compositor");
int ret = lock.Lock();
if (ret)
return;
if (!active_composition_)
return;
if (DisablePlanes(active_composition_.get()))
return;
active_composition_->SignalCompositionDone();
active_composition_.reset(NULL);
}
void DrmDisplayCompositor::ApplyFrame(
std::unique_ptr<DrmDisplayComposition> composition, int status) {
int ret = status;
if (!ret)
ret = CommitFrame(composition.get(), false);
if (ret) {
ALOGE("Composite failed for display %d", display_);
// Disable the hw used by the last active composition. This allows us to
// signal the release fences from that composition to avoid hanging.
ClearDisplay();
return;
}
++dump_frames_composited_;
if (active_composition_)
active_composition_->SignalCompositionDone();
ret = pthread_mutex_lock(&lock_);
if (ret)
ALOGE("Failed to acquire lock for active_composition swap");
active_composition_.swap(composition);
if (!ret)
ret = pthread_mutex_unlock(&lock_);
if (ret)
ALOGE("Failed to release lock for active_composition swap");
}
int DrmDisplayCompositor::ApplyComposition(
std::unique_ptr<DrmDisplayComposition> composition) {
int ret = 0;
switch (composition->type()) {
case DRM_COMPOSITION_TYPE_FRAME:
ret = PrepareFrame(composition.get());
if (ret) {
ALOGE("Failed to prepare frame for display %d", display_);
return ret;
}
ApplyFrame(std::move(composition), false);
break;
case DRM_COMPOSITION_TYPE_DPMS:
active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
ret = ApplyDpms(composition.get());
if (ret)
ALOGE("Failed to apply dpms for display %d", display_);
return ret;
case DRM_COMPOSITION_TYPE_MODESET:
mode_.mode = composition->display_mode();
if (mode_.blob_id)
drm_->DestroyPropertyBlob(mode_.blob_id);
std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
if (ret) {
ALOGE("Failed to create mode blob for display %d", display_);
return ret;
}
mode_.needs_modeset = true;
return 0;
default:
ALOGE("Unknown composition type %d", composition->type());
return -EINVAL;
}
return ret;
}
int DrmDisplayCompositor::SquashAll() {
AutoLock lock(&lock_, "compositor");
int ret = lock.Lock();
if (ret)
return ret;
if (!active_composition_)
return 0;
std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
ret = SquashFrame(active_composition_.get(), comp.get());
// ApplyFrame needs the lock
lock.Unlock();
if (!ret)
ApplyFrame(std::move(comp), 0);
return ret;
}
// Returns:
// - 0 if src is successfully squashed into dst
// - -EALREADY if the src is already squashed
// - Appropriate error if the squash fails
int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
DrmDisplayComposition *dst) {
if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
return -ENOTSUP;
std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
std::vector<DrmHwcLayer> &src_layers = src->layers();
// Make sure there is more than one layer to squash.
size_t src_planes_with_layer = std::count_if(
src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
return p.type() != DrmCompositionPlane::Type::kDisable;
});
if (src_planes_with_layer <= 1)
return -EALREADY;
int pre_comp_layer_index;
int ret = dst->Init(drm_, src->crtc(), src->importer(), src->planner(),
src->frame_no());
if (ret) {
ALOGE("Failed to init squash all composition %d", ret);
return ret;
}
DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kPrecomp, NULL,
src->crtc());
std::vector<DrmHwcLayer> dst_layers;
for (DrmCompositionPlane &comp_plane : src_planes) {
// Composition planes without DRM planes should never happen
if (comp_plane.plane() == NULL) {
ALOGE("Skipping squash all because of NULL plane");
ret = -EINVAL;
goto move_layers_back;
}
if (comp_plane.type() == DrmCompositionPlane::Type::kDisable) {
dst->AddPlaneDisable(comp_plane.plane());
continue;
}
for (auto i : comp_plane.source_layers()) {
DrmHwcLayer &layer = src_layers[i];
// Squashing protected layers is impossible.
if (layer.protected_usage()) {
ret = -ENOTSUP;
goto move_layers_back;
}
// The OutputFds point to freed memory after hwc_set returns. They are
// returned to the default to prevent DrmDisplayComposition::Plan from
// filling the OutputFds.
layer.release_fence = OutputFd();
dst_layers.emplace_back(std::move(layer));
squashed_comp.source_layers().push_back(
squashed_comp.source_layers().size());
}
if (comp_plane.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
squashed_comp.set_plane(comp_plane.plane());
else
dst->AddPlaneDisable(comp_plane.plane());
}
ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
if (ret) {
ALOGE("Failed to set layers for squash all composition %d", ret);
goto move_layers_back;
}
ret = dst->AddPlaneComposition(std::move(squashed_comp));
if (ret) {
ALOGE("Failed to add squashed plane composition %d", ret);
goto move_layers_back;
}
ret = dst->FinalizeComposition();
if (ret) {
ALOGE("Failed to plan for squash all composition %d", ret);
goto move_layers_back;
}
ret = ApplyPreComposite(dst);
if (ret) {
ALOGE("Failed to pre-composite for squash all composition %d", ret);
goto move_layers_back;
}
pre_comp_layer_index = dst->layers().size() - 1;
framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
for (DrmCompositionPlane &plane : dst->composition_planes()) {
if (plane.type() == DrmCompositionPlane::Type::kPrecomp) {
// Replace source_layers with the output of the precomposite
plane.source_layers().clear();
plane.source_layers().push_back(pre_comp_layer_index);
break;
}
}
return 0;
// TODO(zachr): think of a better way to transfer ownership back to the active
// composition.
move_layers_back:
for (size_t plane_index = 0;
plane_index < src_planes.size() && plane_index < dst_layers.size();) {
if (src_planes[plane_index].source_layers().empty()) {
plane_index++;
continue;
}
for (auto i : src_planes[plane_index].source_layers())
src_layers[i] = std::move(dst_layers[plane_index++]);
}
return ret;
}
void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
int ret = pthread_mutex_lock(&lock_);
if (ret)
return;
uint64_t num_frames = dump_frames_composited_;
dump_frames_composited_ = 0;
struct timespec ts;
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
if (ret) {
pthread_mutex_unlock(&lock_);
return;
}
uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
*out << "--DrmDisplayCompositor[" << display_
<< "]: num_frames=" << num_frames << " num_ms=" << num_ms
<< " fps=" << fps << "\n";
dump_last_timestamp_ns_ = cur_ts;
if (active_composition_)
active_composition_->Dump(out);
squash_state_.Dump(out);
pthread_mutex_unlock(&lock_);
}
}