summaryrefslogtreecommitdiff
path: root/libs/vr/libvrflinger/hardware_composer.cpp
blob: 70f303b2085ff84531fe0ad2e1916a187797c155 (plain)
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
#include "hardware_composer.h"

#include <binder/IServiceManager.h>
#include <cutils/properties.h>
#include <cutils/sched_policy.h>
#include <fcntl.h>
#include <log/log.h>
#include <poll.h>
#include <stdint.h>
#include <sync/sync.h>
#include <sys/eventfd.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/system_properties.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <utils/Trace.h>

#include <algorithm>
#include <chrono>
#include <functional>
#include <map>
#include <sstream>
#include <string>
#include <tuple>

#include <dvr/dvr_display_types.h>
#include <dvr/performance_client_api.h>
#include <private/dvr/clock_ns.h>
#include <private/dvr/ion_buffer.h>

using android::hardware::Return;
using android::hardware::Void;
using android::pdx::ErrorStatus;
using android::pdx::LocalHandle;
using android::pdx::Status;
using android::pdx::rpc::EmptyVariant;
using android::pdx::rpc::IfAnyOf;

using namespace std::chrono_literals;

namespace android {
namespace dvr {

namespace {

const char kDvrPerformanceProperty[] = "sys.dvr.performance";

const char kRightEyeOffsetProperty[] = "dvr.right_eye_offset_ns";

// Surface flinger uses "VSYNC-sf" and "VSYNC-app" for its version of these
// events. Name ours similarly.
const char kVsyncTraceEventName[] = "VSYNC-vrflinger";

// How long to wait after boot finishes before we turn the display off.
constexpr int kBootFinishedDisplayOffTimeoutSec = 10;

constexpr int kDefaultDisplayWidth = 1920;
constexpr int kDefaultDisplayHeight = 1080;
constexpr int64_t kDefaultVsyncPeriodNs = 16666667;
// Hardware composer reports dpi as dots per thousand inches (dpi * 1000).
constexpr int kDefaultDpi = 400000;

// Get time offset from a vsync to when the pose for that vsync should be
// predicted out to. For example, if scanout gets halfway through the frame
// at the halfway point between vsyncs, then this could be half the period.
// With global shutter displays, this should be changed to the offset to when
// illumination begins. Low persistence adds a frame of latency, so we predict
// to the center of the next frame.
inline int64_t GetPosePredictionTimeOffset(int64_t vsync_period_ns) {
  return (vsync_period_ns * 150) / 100;
}

// Attempts to set the scheduler class and partiton for the current thread.
// Returns true on success or false on failure.
bool SetThreadPolicy(const std::string& scheduler_class,
                     const std::string& partition) {
  int error = dvrSetSchedulerClass(0, scheduler_class.c_str());
  if (error < 0) {
    ALOGE(
        "SetThreadPolicy: Failed to set scheduler class \"%s\" for "
        "thread_id=%d: %s",
        scheduler_class.c_str(), gettid(), strerror(-error));
    return false;
  }
  error = dvrSetCpuPartition(0, partition.c_str());
  if (error < 0) {
    ALOGE(
        "SetThreadPolicy: Failed to set cpu partiton \"%s\" for thread_id=%d: "
        "%s",
        partition.c_str(), gettid(), strerror(-error));
    return false;
  }
  return true;
}

// Utility to generate scoped tracers with arguments.
// TODO(eieio): Move/merge this into utils/Trace.h?
class TraceArgs {
 public:
  template <typename... Args>
  explicit TraceArgs(const char* format, Args&&... args) {
    std::array<char, 1024> buffer;
    snprintf(buffer.data(), buffer.size(), format, std::forward<Args>(args)...);
    atrace_begin(ATRACE_TAG, buffer.data());
  }

  ~TraceArgs() { atrace_end(ATRACE_TAG); }

 private:
  TraceArgs(const TraceArgs&) = delete;
  void operator=(const TraceArgs&) = delete;
};

// Macro to define a scoped tracer with arguments. Uses PASTE(x, y) macro
// defined in utils/Trace.h.
#define TRACE_FORMAT(format, ...) \
  TraceArgs PASTE(__tracer, __LINE__) { format, ##__VA_ARGS__ }

// Returns "primary" or "external". Useful for writing more readable logs.
const char* GetDisplayName(bool is_primary) {
  return is_primary ? "primary" : "external";
}

}  // anonymous namespace

HardwareComposer::HardwareComposer()
    : initialized_(false), request_display_callback_(nullptr) {}

HardwareComposer::~HardwareComposer(void) {
  UpdatePostThreadState(PostThreadState::Quit, true);
  if (post_thread_.joinable())
    post_thread_.join();
  composer_callback_->SetVsyncService(nullptr);
}

void HardwareComposer::UpdateEdidData(Hwc2::Composer* composer,
                                      hwc2_display_t hw_id) {
  const auto error = composer->getDisplayIdentificationData(
      hw_id, &display_port_, &display_identification_data_);
  if (error != android::hardware::graphics::composer::V2_1::Error::NONE) {
    if (error !=
        android::hardware::graphics::composer::V2_1::Error::UNSUPPORTED) {
      ALOGI("hardware_composer: identification data error\n");
    } else {
      ALOGI("hardware_composer: identification data unsupported\n");
    }
  }
}

bool HardwareComposer::Initialize(
    Hwc2::Composer* composer, hwc2_display_t primary_display_id,
    RequestDisplayCallback request_display_callback) {
  if (initialized_) {
    ALOGE("HardwareComposer::Initialize: already initialized.");
    return false;
  }

  request_display_callback_ = request_display_callback;

  primary_display_ = GetDisplayParams(composer, primary_display_id, true);

  vsync_service_ = new VsyncService;
  sp<IServiceManager> sm(defaultServiceManager());
  auto result = sm->addService(String16(VsyncService::GetServiceName()),
      vsync_service_, false);
  LOG_ALWAYS_FATAL_IF(result != android::OK,
      "addService(%s) failed", VsyncService::GetServiceName());

  post_thread_event_fd_.Reset(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
  LOG_ALWAYS_FATAL_IF(
      !post_thread_event_fd_,
      "HardwareComposer: Failed to create interrupt event fd : %s",
      strerror(errno));

  UpdateEdidData(composer, primary_display_id);

  post_thread_ = std::thread(&HardwareComposer::PostThread, this);

  initialized_ = true;

  return initialized_;
}

void HardwareComposer::Enable() {
  UpdatePostThreadState(PostThreadState::Suspended, false);
}

void HardwareComposer::Disable() {
  UpdatePostThreadState(PostThreadState::Suspended, true);

  std::unique_lock<std::mutex> lock(post_thread_mutex_);
  post_thread_ready_.wait(lock, [this] {
    return !post_thread_resumed_;
  });
}

void HardwareComposer::OnBootFinished() {
  std::lock_guard<std::mutex> lock(post_thread_mutex_);
  if (boot_finished_)
    return;
  boot_finished_ = true;
  post_thread_wait_.notify_one();
}

// Update the post thread quiescent state based on idle and suspended inputs.
void HardwareComposer::UpdatePostThreadState(PostThreadStateType state,
                                             bool suspend) {
  std::unique_lock<std::mutex> lock(post_thread_mutex_);

  // Update the votes in the state variable before evaluating the effective
  // quiescent state. Any bits set in post_thread_state_ indicate that the post
  // thread should be suspended.
  if (suspend) {
    post_thread_state_ |= state;
  } else {
    post_thread_state_ &= ~state;
  }

  const bool quit = post_thread_state_ & PostThreadState::Quit;
  const bool effective_suspend = post_thread_state_ != PostThreadState::Active;
  if (quit) {
    post_thread_quiescent_ = true;
    eventfd_write(post_thread_event_fd_.Get(), 1);
    post_thread_wait_.notify_one();
  } else if (effective_suspend && !post_thread_quiescent_) {
    post_thread_quiescent_ = true;
    eventfd_write(post_thread_event_fd_.Get(), 1);
  } else if (!effective_suspend && post_thread_quiescent_) {
    post_thread_quiescent_ = false;
    eventfd_t value;
    eventfd_read(post_thread_event_fd_.Get(), &value);
    post_thread_wait_.notify_one();
  }
}

void HardwareComposer::CreateComposer() {
  if (composer_)
    return;
  composer_.reset(new Hwc2::impl::Composer("default"));
  composer_callback_ = new ComposerCallback;
  composer_->registerCallback(composer_callback_);
  LOG_ALWAYS_FATAL_IF(!composer_callback_->GotFirstHotplug(),
      "Registered composer callback but didn't get hotplug for primary"
      " display");
  composer_callback_->SetVsyncService(vsync_service_);
}

void HardwareComposer::OnPostThreadResumed() {
  ALOGI("OnPostThreadResumed");
  EnableDisplay(*target_display_, true);

  // Trigger target-specific performance mode change.
  property_set(kDvrPerformanceProperty, "performance");
}

void HardwareComposer::OnPostThreadPaused() {
  ALOGI("OnPostThreadPaused");
  retire_fence_fds_.clear();
  layers_.clear();

  // Phones create a new composer client on resume and destroy it on pause.
  if (composer_callback_ != nullptr) {
    composer_callback_->SetVsyncService(nullptr);
    composer_callback_ = nullptr;
  }
  composer_.reset(nullptr);

  // Trigger target-specific performance mode change.
  property_set(kDvrPerformanceProperty, "idle");
}

bool HardwareComposer::PostThreadCondWait(std::unique_lock<std::mutex>& lock,
                                          int timeout_sec,
                                          const std::function<bool()>& pred) {
  auto pred_with_quit = [&] {
    return pred() || (post_thread_state_ & PostThreadState::Quit);
  };
  if (timeout_sec >= 0) {
    post_thread_wait_.wait_for(lock, std::chrono::seconds(timeout_sec),
                               pred_with_quit);
  } else {
    post_thread_wait_.wait(lock, pred_with_quit);
  }
  if (post_thread_state_ & PostThreadState::Quit) {
    ALOGI("HardwareComposer::PostThread: Quitting.");
    return true;
  }
  return false;
}

HWC::Error HardwareComposer::Validate(hwc2_display_t display) {
  uint32_t num_types;
  uint32_t num_requests;
  HWC::Error error =
      composer_->validateDisplay(display, &num_types, &num_requests);

  if (error == HWC2_ERROR_HAS_CHANGES) {
    ALOGE("Hardware composer has requested composition changes, "
          "which we don't support.");
    // Accept the changes anyway and see if we can get something on the screen.
    error = composer_->acceptDisplayChanges(display);
  }

  return error;
}

bool HardwareComposer::EnableVsync(const DisplayParams& display, bool enabled) {
  HWC::Error error = composer_->setVsyncEnabled(display.id,
      (Hwc2::IComposerClient::Vsync)(enabled ? HWC2_VSYNC_ENABLE
                                             : HWC2_VSYNC_DISABLE));
  if (error != HWC::Error::None) {
    ALOGE("Error attempting to %s vsync on %s display: %s",
        enabled ? "enable" : "disable", GetDisplayName(display.is_primary),
        error.to_string().c_str());
  }
  return error == HWC::Error::None;
}

bool HardwareComposer::SetPowerMode(const DisplayParams& display, bool active) {
  ALOGI("Turning %s display %s", GetDisplayName(display.is_primary),
      active ? "on" : "off");
  HWC::PowerMode power_mode = active ? HWC::PowerMode::On : HWC::PowerMode::Off;
  HWC::Error error = composer_->setPowerMode(display.id,
      power_mode.cast<Hwc2::IComposerClient::PowerMode>());
  if (error != HWC::Error::None) {
    ALOGE("Error attempting to turn %s display %s: %s",
          GetDisplayName(display.is_primary), active ? "on" : "off",
        error.to_string().c_str());
  }
  return error == HWC::Error::None;
}

bool HardwareComposer::EnableDisplay(const DisplayParams& display,
                                     bool enabled) {
  bool power_result;
  bool vsync_result;
  // When turning a display on, we set the power state then set vsync. When
  // turning a display off we do it in the opposite order.
  if (enabled) {
    power_result = SetPowerMode(display, enabled);
    vsync_result = EnableVsync(display, enabled);
  } else {
    vsync_result = EnableVsync(display, enabled);
    power_result = SetPowerMode(display, enabled);
  }
  return power_result && vsync_result;
}

HWC::Error HardwareComposer::Present(hwc2_display_t display) {
  int32_t present_fence;
  HWC::Error error = composer_->presentDisplay(display, &present_fence);

  // According to the documentation, this fence is signaled at the time of
  // vsync/DMA for physical displays.
  if (error == HWC::Error::None) {
    retire_fence_fds_.emplace_back(present_fence);
  } else {
    ATRACE_INT("HardwareComposer: PresentResult", error);
  }

  return error;
}

DisplayParams HardwareComposer::GetDisplayParams(
    Hwc2::Composer* composer, hwc2_display_t display, bool is_primary) {
  DisplayParams params;
  params.id = display;
  params.is_primary = is_primary;

  Hwc2::Config config;
  HWC::Error error = composer->getActiveConfig(display, &config);

  if (error == HWC::Error::None) {
    auto get_attr = [&](hwc2_attribute_t attr, const char* attr_name)
        -> std::optional<int32_t> {
      int32_t val;
      HWC::Error error = composer->getDisplayAttribute(
          display, config, (Hwc2::IComposerClient::Attribute)attr, &val);
      if (error != HWC::Error::None) {
        ALOGE("Failed to get %s display attr %s: %s",
            GetDisplayName(is_primary), attr_name,
            error.to_string().c_str());
        return std::nullopt;
      }
      return val;
    };

    auto width = get_attr(HWC2_ATTRIBUTE_WIDTH, "width");
    auto height = get_attr(HWC2_ATTRIBUTE_HEIGHT, "height");

    if (width && height) {
      params.width = *width;
      params.height = *height;
    } else {
      ALOGI("Failed to get width and/or height for %s display. Using default"
          " size %dx%d.", GetDisplayName(is_primary), kDefaultDisplayWidth,
          kDefaultDisplayHeight);
      params.width = kDefaultDisplayWidth;
      params.height = kDefaultDisplayHeight;
    }

    auto vsync_period = get_attr(HWC2_ATTRIBUTE_VSYNC_PERIOD, "vsync period");
    if (vsync_period) {
      params.vsync_period_ns = *vsync_period;
    } else {
      ALOGI("Failed to get vsync period for %s display. Using default vsync"
          " period %.2fms", GetDisplayName(is_primary),
          static_cast<float>(kDefaultVsyncPeriodNs) / 1000000);
      params.vsync_period_ns = kDefaultVsyncPeriodNs;
    }

    auto dpi_x = get_attr(HWC2_ATTRIBUTE_DPI_X, "DPI X");
    auto dpi_y = get_attr(HWC2_ATTRIBUTE_DPI_Y, "DPI Y");
    if (dpi_x && dpi_y) {
      params.dpi.x = *dpi_x;
      params.dpi.y = *dpi_y;
    } else {
      ALOGI("Failed to get dpi_x and/or dpi_y for %s display. Using default"
          " dpi %d.", GetDisplayName(is_primary), kDefaultDpi);
      params.dpi.x = kDefaultDpi;
      params.dpi.y = kDefaultDpi;
    }
  } else {
    ALOGE("HardwareComposer: Failed to get current %s display config: %d."
        " Using default display values.",
        GetDisplayName(is_primary), error.value);
    params.width = kDefaultDisplayWidth;
    params.height = kDefaultDisplayHeight;
    params.dpi.x = kDefaultDpi;
    params.dpi.y = kDefaultDpi;
    params.vsync_period_ns = kDefaultVsyncPeriodNs;
  }

  ALOGI(
      "HardwareComposer: %s display attributes: width=%d height=%d "
      "vsync_period_ns=%d DPI=%dx%d",
      GetDisplayName(is_primary),
      params.width,
      params.height,
      params.vsync_period_ns,
      params.dpi.x,
      params.dpi.y);

  return params;
}

std::string HardwareComposer::Dump() {
  std::unique_lock<std::mutex> lock(post_thread_mutex_);
  std::ostringstream stream;

  auto print_display_metrics = [&](const DisplayParams& params) {
    stream << GetDisplayName(params.is_primary)
           << " display metrics:     " << params.width << "x"
           << params.height << " " << (params.dpi.x / 1000.0)
           << "x" << (params.dpi.y / 1000.0) << " dpi @ "
           << (1000000000.0 / params.vsync_period_ns) << " Hz"
           << std::endl;
  };

  print_display_metrics(primary_display_);
  if (external_display_)
    print_display_metrics(*external_display_);

  stream << "Post thread resumed: " << post_thread_resumed_ << std::endl;
  stream << "Active layers:       " << layers_.size() << std::endl;
  stream << std::endl;

  for (size_t i = 0; i < layers_.size(); i++) {
    stream << "Layer " << i << ":";
    stream << " type=" << layers_[i].GetCompositionType().to_string();
    stream << " surface_id=" << layers_[i].GetSurfaceId();
    stream << " buffer_id=" << layers_[i].GetBufferId();
    stream << std::endl;
  }
  stream << std::endl;

  if (post_thread_resumed_) {
    stream << "Hardware Composer Debug Info:" << std::endl;
    stream << composer_->dumpDebugInfo();
  }

  return stream.str();
}

void HardwareComposer::PostLayers(hwc2_display_t display) {
  ATRACE_NAME("HardwareComposer::PostLayers");

  // Setup the hardware composer layers with current buffers.
  for (auto& layer : layers_) {
    layer.Prepare();
  }

  // Now that we have taken in a frame from the application, we have a chance
  // to drop the frame before passing the frame along to HWC.
  // If the display driver has become backed up, we detect it here and then
  // react by skipping this frame to catch up latency.
  while (!retire_fence_fds_.empty() &&
         (!retire_fence_fds_.front() ||
          sync_wait(retire_fence_fds_.front().Get(), 0) == 0)) {
    // There are only 2 fences in here, no performance problem to shift the
    // array of ints.
    retire_fence_fds_.erase(retire_fence_fds_.begin());
  }

  const bool is_fence_pending = static_cast<int32_t>(retire_fence_fds_.size()) >
                                post_thread_config_.allowed_pending_fence_count;

  if (is_fence_pending) {
    ATRACE_INT("frame_skip_count", ++frame_skip_count_);

    ALOGW_IF(is_fence_pending,
             "Warning: dropping a frame to catch up with HWC (pending = %zd)",
             retire_fence_fds_.size());

    for (auto& layer : layers_) {
      layer.Drop();
    }
    return;
  } else {
    // Make the transition more obvious in systrace when the frame skip happens
    // above.
    ATRACE_INT("frame_skip_count", 0);
  }

#if TRACE > 1
  for (size_t i = 0; i < layers_.size(); i++) {
    ALOGI("HardwareComposer::PostLayers: layer=%zu buffer_id=%d composition=%s",
          i, layers_[i].GetBufferId(),
          layers_[i].GetCompositionType().to_string().c_str());
  }
#endif

  HWC::Error error = Validate(display);
  if (error != HWC::Error::None) {
    ALOGE("HardwareComposer::PostLayers: Validate failed: %s display=%" PRIu64,
          error.to_string().c_str(), display);
    return;
  }

  error = Present(display);
  if (error != HWC::Error::None) {
    ALOGE("HardwareComposer::PostLayers: Present failed: %s",
          error.to_string().c_str());
    return;
  }

  std::vector<Hwc2::Layer> out_layers;
  std::vector<int> out_fences;
  error = composer_->getReleaseFences(display,
                                      &out_layers, &out_fences);
  ALOGE_IF(error != HWC::Error::None,
           "HardwareComposer::PostLayers: Failed to get release fences: %s",
           error.to_string().c_str());

  // Perform post-frame bookkeeping.
  uint32_t num_elements = out_layers.size();
  for (size_t i = 0; i < num_elements; ++i) {
    for (auto& layer : layers_) {
      if (layer.GetLayerHandle() == out_layers[i]) {
        layer.Finish(out_fences[i]);
      }
    }
  }
}

void HardwareComposer::SetDisplaySurfaces(
    std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces) {
  ALOGI("HardwareComposer::SetDisplaySurfaces: surface count=%zd",
        surfaces.size());
  const bool display_idle = surfaces.size() == 0;
  {
    std::unique_lock<std::mutex> lock(post_thread_mutex_);
    surfaces_ = std::move(surfaces);
    surfaces_changed_ = true;
  }

  if (request_display_callback_)
    request_display_callback_(!display_idle);

  // Set idle state based on whether there are any surfaces to handle.
  UpdatePostThreadState(PostThreadState::Idle, display_idle);
}

int HardwareComposer::OnNewGlobalBuffer(DvrGlobalBufferKey key,
                                        IonBuffer& ion_buffer) {
  if (key == DvrGlobalBuffers::kVsyncBuffer) {
    vsync_ring_ = std::make_unique<CPUMappedBroadcastRing<DvrVsyncRing>>(
        &ion_buffer, CPUUsageMode::WRITE_OFTEN);

    if (vsync_ring_->IsMapped() == false) {
      return -EPERM;
    }
  }

  if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
    return MapConfigBuffer(ion_buffer);
  }

  return 0;
}

void HardwareComposer::OnDeletedGlobalBuffer(DvrGlobalBufferKey key) {
  if (key == DvrGlobalBuffers::kVrFlingerConfigBufferKey) {
    ConfigBufferDeleted();
  }
}

int HardwareComposer::MapConfigBuffer(IonBuffer& ion_buffer) {
  std::lock_guard<std::mutex> lock(shared_config_mutex_);
  shared_config_ring_ = DvrConfigRing();

  if (ion_buffer.width() < DvrConfigRing::MemorySize()) {
    ALOGE("HardwareComposer::MapConfigBuffer: invalid buffer size.");
    return -EINVAL;
  }

  void* buffer_base = 0;
  int result = ion_buffer.Lock(ion_buffer.usage(), 0, 0, ion_buffer.width(),
                               ion_buffer.height(), &buffer_base);
  if (result != 0) {
    ALOGE(
        "HardwareComposer::MapConfigBuffer: Failed to map vrflinger config "
        "buffer.");
    return -EPERM;
  }

  shared_config_ring_ = DvrConfigRing::Create(buffer_base, ion_buffer.width());
  ion_buffer.Unlock();

  return 0;
}

void HardwareComposer::ConfigBufferDeleted() {
  std::lock_guard<std::mutex> lock(shared_config_mutex_);
  shared_config_ring_ = DvrConfigRing();
}

void HardwareComposer::UpdateConfigBuffer() {
  std::lock_guard<std::mutex> lock(shared_config_mutex_);
  if (!shared_config_ring_.is_valid())
    return;
  // Copy from latest record in shared_config_ring_ to local copy.
  DvrConfig record;
  if (shared_config_ring_.GetNewest(&shared_config_ring_sequence_, &record)) {
    ALOGI("DvrConfig updated: sequence %u, post offset %d",
          shared_config_ring_sequence_, record.frame_post_offset_ns);
    ++shared_config_ring_sequence_;
    post_thread_config_ = record;
  }
}

int HardwareComposer::PostThreadPollInterruptible(
    const pdx::LocalHandle& event_fd, int requested_events, int timeout_ms) {
  pollfd pfd[2] = {
      {
          .fd = event_fd.Get(),
          .events = static_cast<short>(requested_events),
          .revents = 0,
      },
      {
          .fd = post_thread_event_fd_.Get(),
          .events = POLLPRI | POLLIN,
          .revents = 0,
      },
  };
  int ret, error;
  do {
    ret = poll(pfd, 2, timeout_ms);
    error = errno;
    ALOGW_IF(ret < 0,
             "HardwareComposer::PostThreadPollInterruptible: Error during "
             "poll(): %s (%d)",
             strerror(error), error);
  } while (ret < 0 && error == EINTR);

  if (ret < 0) {
    return -error;
  } else if (ret == 0) {
    return -ETIMEDOUT;
  } else if (pfd[0].revents != 0) {
    return 0;
  } else if (pfd[1].revents != 0) {
    ALOGI("VrHwcPost thread interrupted: revents=%x", pfd[1].revents);
    return kPostThreadInterrupted;
  } else {
    return 0;
  }
}

// Sleep until the next predicted vsync, returning the predicted vsync
// timestamp.
Status<int64_t> HardwareComposer::WaitForPredictedVSync() {
  const int64_t predicted_vsync_time = last_vsync_timestamp_ +
      (target_display_->vsync_period_ns * vsync_prediction_interval_);
  const int error = SleepUntil(predicted_vsync_time);
  if (error < 0) {
    ALOGE("HardwareComposer::WaifForVSync:: Failed to sleep: %s",
          strerror(-error));
    return error;
  }
  return {predicted_vsync_time};
}

int HardwareComposer::SleepUntil(int64_t wakeup_timestamp) {
  const int timer_fd = vsync_sleep_timer_fd_.Get();
  const itimerspec wakeup_itimerspec = {
      .it_interval = {.tv_sec = 0, .tv_nsec = 0},
      .it_value = NsToTimespec(wakeup_timestamp),
  };
  int ret =
      timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &wakeup_itimerspec, nullptr);
  int error = errno;
  if (ret < 0) {
    ALOGE("HardwareComposer::SleepUntil: Failed to set timerfd: %s",
          strerror(error));
    return -error;
  }

  return PostThreadPollInterruptible(vsync_sleep_timer_fd_, POLLIN,
                                     /*timeout_ms*/ -1);
}

void HardwareComposer::PostThread() {
  // NOLINTNEXTLINE(runtime/int)
  prctl(PR_SET_NAME, reinterpret_cast<unsigned long>("VrHwcPost"), 0, 0, 0);

  // Set the scheduler to SCHED_FIFO with high priority. If this fails here
  // there may have been a startup timing issue between this thread and
  // performanced. Try again later when this thread becomes active.
  bool thread_policy_setup =
      SetThreadPolicy("graphics:high", "/system/performance");

  // Create a timerfd based on CLOCK_MONOTINIC.
  vsync_sleep_timer_fd_.Reset(timerfd_create(CLOCK_MONOTONIC, 0));
  LOG_ALWAYS_FATAL_IF(
      !vsync_sleep_timer_fd_,
      "HardwareComposer: Failed to create vsync sleep timerfd: %s",
      strerror(errno));

  struct VsyncEyeOffsets { int64_t left_ns, right_ns; };
  bool was_running = false;

  auto get_vsync_eye_offsets = [this]() -> VsyncEyeOffsets {
    VsyncEyeOffsets offsets;
    offsets.left_ns =
        GetPosePredictionTimeOffset(target_display_->vsync_period_ns);

    // TODO(jbates) Query vblank time from device, when such an API is
    // available. This value (6.3%) was measured on A00 in low persistence mode.
    int64_t vblank_ns = target_display_->vsync_period_ns * 63 / 1000;
    offsets.right_ns = (target_display_->vsync_period_ns - vblank_ns) / 2;

    // Check property for overriding right eye offset value.
    offsets.right_ns =
        property_get_int64(kRightEyeOffsetProperty, offsets.right_ns);

    return offsets;
  };

  VsyncEyeOffsets vsync_eye_offsets = get_vsync_eye_offsets();

  while (1) {
    ATRACE_NAME("HardwareComposer::PostThread");

    // Check for updated config once per vsync.
    UpdateConfigBuffer();

    while (post_thread_quiescent_) {
      std::unique_lock<std::mutex> lock(post_thread_mutex_);
      ALOGI("HardwareComposer::PostThread: Entering quiescent state.");

      if (was_running) {
        vsync_trace_parity_ = false;
        ATRACE_INT(kVsyncTraceEventName, 0);
      }

      // Tear down resources.
      OnPostThreadPaused();
      was_running = false;
      post_thread_resumed_ = false;
      post_thread_ready_.notify_all();

      if (PostThreadCondWait(lock, -1,
                             [this] { return !post_thread_quiescent_; })) {
        // A true return value means we've been asked to quit.
        return;
      }

      post_thread_resumed_ = true;
      post_thread_ready_.notify_all();

      ALOGI("HardwareComposer::PostThread: Exiting quiescent state.");
    }

    if (!composer_)
      CreateComposer();

    bool target_display_changed = UpdateTargetDisplay();
    bool just_resumed_running = !was_running;
    was_running = true;

    if (target_display_changed)
      vsync_eye_offsets = get_vsync_eye_offsets();

    if (just_resumed_running) {
      OnPostThreadResumed();

      // Try to setup the scheduler policy if it failed during startup. Only
      // attempt to do this on transitions from inactive to active to avoid
      // spamming the system with RPCs and log messages.
      if (!thread_policy_setup) {
        thread_policy_setup =
            SetThreadPolicy("graphics:high", "/system/performance");
      }
    }

    if (target_display_changed || just_resumed_running) {
      // Initialize the last vsync timestamp with the current time. The
      // predictor below uses this time + the vsync interval in absolute time
      // units for the initial delay. Once the driver starts reporting vsync the
      // predictor will sync up with the real vsync.
      last_vsync_timestamp_ = GetSystemClockNs();
      vsync_prediction_interval_ = 1;
      retire_fence_fds_.clear();
    }

    int64_t vsync_timestamp = 0;
    {
      TRACE_FORMAT("wait_vsync|vsync=%u;last_timestamp=%" PRId64
                   ";prediction_interval=%d|",
                   vsync_count_ + 1, last_vsync_timestamp_,
                   vsync_prediction_interval_);

      auto status = WaitForPredictedVSync();
      ALOGE_IF(
          !status,
          "HardwareComposer::PostThread: Failed to wait for vsync event: %s",
          status.GetErrorMessage().c_str());

      // If there was an error either sleeping was interrupted due to pausing or
      // there was an error getting the latest timestamp.
      if (!status)
        continue;

      // Predicted vsync timestamp for this interval. This is stable because we
      // use absolute time for the wakeup timer.
      vsync_timestamp = status.get();
    }

    vsync_trace_parity_ = !vsync_trace_parity_;
    ATRACE_INT(kVsyncTraceEventName, vsync_trace_parity_ ? 1 : 0);

    // Advance the vsync counter only if the system is keeping up with hardware
    // vsync to give clients an indication of the delays.
    if (vsync_prediction_interval_ == 1)
      ++vsync_count_;

    UpdateLayerConfig();

    // Publish the vsync event.
    if (vsync_ring_) {
      DvrVsync vsync;
      vsync.vsync_count = vsync_count_;
      vsync.vsync_timestamp_ns = vsync_timestamp;
      vsync.vsync_left_eye_offset_ns = vsync_eye_offsets.left_ns;
      vsync.vsync_right_eye_offset_ns = vsync_eye_offsets.right_ns;
      vsync.vsync_period_ns = target_display_->vsync_period_ns;

      vsync_ring_->Publish(vsync);
    }

    {
      // Sleep until shortly before vsync.
      ATRACE_NAME("sleep");

      const int64_t display_time_est_ns =
          vsync_timestamp + target_display_->vsync_period_ns;
      const int64_t now_ns = GetSystemClockNs();
      const int64_t sleep_time_ns = display_time_est_ns - now_ns -
                                    post_thread_config_.frame_post_offset_ns;
      const int64_t wakeup_time_ns =
          display_time_est_ns - post_thread_config_.frame_post_offset_ns;

      ATRACE_INT64("sleep_time_ns", sleep_time_ns);
      if (sleep_time_ns > 0) {
        int error = SleepUntil(wakeup_time_ns);
        ALOGE_IF(error < 0 && error != kPostThreadInterrupted,
                 "HardwareComposer::PostThread: Failed to sleep: %s",
                 strerror(-error));
        // If the sleep was interrupted (error == kPostThreadInterrupted),
        // we still go through and present this frame because we may have set
        // layers earlier and we want to flush the Composer's internal command
        // buffer by continuing through to validate and present.
      }
    }

    {
      auto status = composer_callback_->GetVsyncTime(target_display_->id);

      // If we failed to read vsync there might be a problem with the driver.
      // Since there's nothing we can do just behave as though we didn't get an
      // updated vsync time and let the prediction continue.
      const int64_t current_vsync_timestamp =
          status ? status.get() : last_vsync_timestamp_;

      const bool vsync_delayed =
          last_vsync_timestamp_ == current_vsync_timestamp;
      ATRACE_INT("vsync_delayed", vsync_delayed);

      // If vsync was delayed advance the prediction interval and allow the
      // fence logic in PostLayers() to skip the frame.
      if (vsync_delayed) {
        ALOGW(
            "HardwareComposer::PostThread: VSYNC timestamp did not advance "
            "since last frame: timestamp=%" PRId64 " prediction_interval=%d",
            current_vsync_timestamp, vsync_prediction_interval_);
        vsync_prediction_interval_++;
      } else {
        // We have an updated vsync timestamp, reset the prediction interval.
        last_vsync_timestamp_ = current_vsync_timestamp;
        vsync_prediction_interval_ = 1;
      }
    }

    PostLayers(target_display_->id);
  }
}

bool HardwareComposer::UpdateTargetDisplay() {
  bool target_display_changed = false;
  auto displays = composer_callback_->GetDisplays();
  if (displays.external_display_was_hotplugged) {
    bool was_using_external_display = !target_display_->is_primary;
    if (was_using_external_display) {
      // The external display was hotplugged, so make sure to ignore any bad
      // display errors as we destroy the layers.
      for (auto& layer: layers_)
        layer.IgnoreBadDisplayErrorsOnDestroy(true);
    }

    if (displays.external_display) {
      // External display was connected
      external_display_ = GetDisplayParams(composer_.get(),
          *displays.external_display, /*is_primary*/ false);

      ALOGI("External display connected. Switching to external display.");
      target_display_ = &(*external_display_);
      target_display_changed = true;
    } else {
      // External display was disconnected
      external_display_ = std::nullopt;
      if (was_using_external_display) {
        ALOGI("External display disconnected. Switching to primary display.");
        target_display_ = &primary_display_;
        target_display_changed = true;
      }
    }
  }

  if (target_display_changed) {
    // If we're switching to the external display, turn the primary display off.
    if (!target_display_->is_primary) {
      EnableDisplay(primary_display_, false);
    }
    // If we're switching to the primary display, and the external display is
    // still connected, turn the external display off.
    else if (target_display_->is_primary && external_display_) {
      EnableDisplay(*external_display_, false);
    }

    // Update the cached edid data for the current display.
    UpdateEdidData(composer_.get(), target_display_->id);

    // Turn the new target display on.
    EnableDisplay(*target_display_, true);

    // When we switch displays we need to recreate all the layers, so clear the
    // current list, which will trigger layer recreation.
    layers_.clear();
  }

  return target_display_changed;
}

// Checks for changes in the surface stack and updates the layer config to
// accomodate the new stack.
void HardwareComposer::UpdateLayerConfig() {
  std::vector<std::shared_ptr<DirectDisplaySurface>> surfaces;
  {
    std::unique_lock<std::mutex> lock(post_thread_mutex_);

    if (!surfaces_changed_ && (!layers_.empty() || surfaces_.empty()))
      return;

    surfaces = surfaces_;
    surfaces_changed_ = false;
  }

  ATRACE_NAME("UpdateLayerConfig_HwLayers");

  // Sort the new direct surface list by z-order to determine the relative order
  // of the surfaces. This relative order is used for the HWC z-order value to
  // insulate VrFlinger and HWC z-order semantics from each other.
  std::sort(surfaces.begin(), surfaces.end(), [](const auto& a, const auto& b) {
    return a->z_order() < b->z_order();
  });

  // Prepare a new layer stack, pulling in layers from the previous
  // layer stack that are still active and updating their attributes.
  std::vector<Layer> layers;
  size_t layer_index = 0;
  for (const auto& surface : surfaces) {
    // The bottom layer is opaque, other layers blend.
    HWC::BlendMode blending =
        layer_index == 0 ? HWC::BlendMode::None : HWC::BlendMode::Coverage;

    // Try to find a layer for this surface in the set of active layers.
    auto search =
        std::lower_bound(layers_.begin(), layers_.end(), surface->surface_id());
    const bool found = search != layers_.end() &&
                       search->GetSurfaceId() == surface->surface_id();
    if (found) {
      // Update the attributes of the layer that may have changed.
      search->SetBlending(blending);
      search->SetZOrder(layer_index);  // Relative z-order.

      // Move the existing layer to the new layer set and remove the empty layer
      // object from the current set.
      layers.push_back(std::move(*search));
      layers_.erase(search);
    } else {
      // Insert a layer for the new surface.
      layers.emplace_back(composer_.get(), *target_display_, surface, blending,
          HWC::Composition::Device, layer_index);
    }

    ALOGI_IF(
        TRACE,
        "HardwareComposer::UpdateLayerConfig: layer_index=%zu surface_id=%d",
        layer_index, layers[layer_index].GetSurfaceId());

    layer_index++;
  }

  // Sort the new layer stack by ascending surface id.
  std::sort(layers.begin(), layers.end());

  // Replace the previous layer set with the new layer set. The destructor of
  // the previous set will clean up the remaining Layers that are not moved to
  // the new layer set.
  layers_ = std::move(layers);

  ALOGD_IF(TRACE, "HardwareComposer::UpdateLayerConfig: %zd active layers",
           layers_.size());
}

std::vector<sp<IVsyncCallback>>::const_iterator
HardwareComposer::VsyncService::FindCallback(
    const sp<IVsyncCallback>& callback) const {
  sp<IBinder> binder = IInterface::asBinder(callback);
  return std::find_if(callbacks_.cbegin(), callbacks_.cend(),
                      [&](const sp<IVsyncCallback>& callback) {
                        return IInterface::asBinder(callback) == binder;
                      });
}

status_t HardwareComposer::VsyncService::registerCallback(
    const sp<IVsyncCallback> callback) {
  std::lock_guard<std::mutex> autolock(mutex_);
  if (FindCallback(callback) == callbacks_.cend()) {
    callbacks_.push_back(callback);
  }
  return OK;
}

status_t HardwareComposer::VsyncService::unregisterCallback(
    const sp<IVsyncCallback> callback) {
  std::lock_guard<std::mutex> autolock(mutex_);
  auto iter = FindCallback(callback);
  if (iter != callbacks_.cend()) {
    callbacks_.erase(iter);
  }
  return OK;
}

void HardwareComposer::VsyncService::OnVsync(int64_t vsync_timestamp) {
  ATRACE_NAME("VsyncService::OnVsync");
  std::lock_guard<std::mutex> autolock(mutex_);
  for (auto iter = callbacks_.begin(); iter != callbacks_.end();) {
    if ((*iter)->onVsync(vsync_timestamp) == android::DEAD_OBJECT) {
      iter = callbacks_.erase(iter);
    } else {
      ++iter;
    }
  }
}

Return<void> HardwareComposer::ComposerCallback::onHotplug(
    Hwc2::Display display, IComposerCallback::Connection conn) {
  std::lock_guard<std::mutex> lock(mutex_);
  ALOGI("onHotplug display=%" PRIu64 " conn=%d", display, conn);

  bool is_primary = !got_first_hotplug_ || display == primary_display_.id;

  // Our first onHotplug callback is always for the primary display.
  if (!got_first_hotplug_) {
    LOG_ALWAYS_FATAL_IF(conn != IComposerCallback::Connection::CONNECTED,
        "Initial onHotplug callback should be primary display connected");
    got_first_hotplug_ = true;
  } else if (is_primary) {
    ALOGE("Ignoring unexpected onHotplug() call for primary display");
    return Void();
  }

  if (conn == IComposerCallback::Connection::CONNECTED) {
    if (!is_primary)
      external_display_ = DisplayInfo();
    DisplayInfo& display_info = is_primary ?
        primary_display_ : *external_display_;
    display_info.id = display;

    std::array<char, 1024> buffer;
    snprintf(buffer.data(), buffer.size(),
             "/sys/class/graphics/fb%" PRIu64 "/vsync_event", display);
    if (LocalHandle handle{buffer.data(), O_RDONLY}) {
      ALOGI(
          "HardwareComposer::ComposerCallback::onHotplug: Driver supports "
          "vsync_event node for display %" PRIu64,
          display);
      display_info.driver_vsync_event_fd = std::move(handle);
    } else {
      ALOGI(
          "HardwareComposer::ComposerCallback::onHotplug: Driver does not "
          "support vsync_event node for display %" PRIu64,
          display);
    }
  } else if (conn == IComposerCallback::Connection::DISCONNECTED) {
    external_display_ = std::nullopt;
  }

  if (!is_primary)
    external_display_was_hotplugged_ = true;

  return Void();
}

Return<void> HardwareComposer::ComposerCallback::onRefresh(
    Hwc2::Display /*display*/) {
  return hardware::Void();
}

Return<void> HardwareComposer::ComposerCallback::onVsync(Hwc2::Display display,
                                                         int64_t timestamp) {
  TRACE_FORMAT("vsync_callback|display=%" PRIu64 ";timestamp=%" PRId64 "|",
               display, timestamp);
  std::lock_guard<std::mutex> lock(mutex_);
  DisplayInfo* display_info = GetDisplayInfo(display);
  if (display_info) {
    display_info->callback_vsync_timestamp = timestamp;
  }
  if (primary_display_.id == display && vsync_service_ != nullptr) {
    vsync_service_->OnVsync(timestamp);
  }

  return Void();
}

Return<void> HardwareComposer::ComposerCallback::onVsync_2_4(
    Hwc2::Display /*display*/, int64_t /*timestamp*/,
    Hwc2::VsyncPeriodNanos /*vsyncPeriodNanos*/) {
  LOG_ALWAYS_FATAL("Unexpected onVsync_2_4 callback");
  return Void();
}

Return<void> HardwareComposer::ComposerCallback::onVsyncPeriodTimingChanged(
    Hwc2::Display /*display*/,
    const Hwc2::VsyncPeriodChangeTimeline& /*updatedTimeline*/) {
  LOG_ALWAYS_FATAL("Unexpected onVsyncPeriodTimingChanged callback");
  return Void();
}

Return<void> HardwareComposer::ComposerCallback::onSeamlessPossible(
    Hwc2::Display /*display*/) {
  LOG_ALWAYS_FATAL("Unexpected onSeamlessPossible callback");
  return Void();
}

void HardwareComposer::ComposerCallback::SetVsyncService(
    const sp<VsyncService>& vsync_service) {
  std::lock_guard<std::mutex> lock(mutex_);
  vsync_service_ = vsync_service;
}

HardwareComposer::ComposerCallback::Displays
HardwareComposer::ComposerCallback::GetDisplays() {
  std::lock_guard<std::mutex> lock(mutex_);
  Displays displays;
  displays.primary_display = primary_display_.id;
  if (external_display_)
    displays.external_display = external_display_->id;
  if (external_display_was_hotplugged_) {
    external_display_was_hotplugged_ = false;
    displays.external_display_was_hotplugged = true;
  }
  return displays;
}

Status<int64_t> HardwareComposer::ComposerCallback::GetVsyncTime(
    hwc2_display_t display) {
  std::lock_guard<std::mutex> autolock(mutex_);
  DisplayInfo* display_info = GetDisplayInfo(display);
  if (!display_info) {
    ALOGW("Attempt to get vsync time for unknown display %" PRIu64, display);
    return ErrorStatus(EINVAL);
  }

  // See if the driver supports direct vsync events.
  LocalHandle& event_fd = display_info->driver_vsync_event_fd;
  if (!event_fd) {
    // Fall back to returning the last timestamp returned by the vsync
    // callback.
    return display_info->callback_vsync_timestamp;
  }

  // When the driver supports the vsync_event sysfs node we can use it to
  // determine the latest vsync timestamp, even if the HWC callback has been
  // delayed.

  // The driver returns data in the form "VSYNC=<timestamp ns>".
  std::array<char, 32> data;
  data.fill('\0');

  // Seek back to the beginning of the event file.
  int ret = lseek(event_fd.Get(), 0, SEEK_SET);
  if (ret < 0) {
    const int error = errno;
    ALOGE(
        "HardwareComposer::ComposerCallback::GetVsyncTime: Failed to seek "
        "vsync event fd: %s",
        strerror(error));
    return ErrorStatus(error);
  }

  // Read the vsync event timestamp.
  ret = read(event_fd.Get(), data.data(), data.size());
  if (ret < 0) {
    const int error = errno;
    ALOGE_IF(error != EAGAIN,
             "HardwareComposer::ComposerCallback::GetVsyncTime: Error "
             "while reading timestamp: %s",
             strerror(error));
    return ErrorStatus(error);
  }

  int64_t timestamp;
  ret = sscanf(data.data(), "VSYNC=%" PRIu64,
               reinterpret_cast<uint64_t*>(&timestamp));
  if (ret < 0) {
    const int error = errno;
    ALOGE(
        "HardwareComposer::ComposerCallback::GetVsyncTime: Error while "
        "parsing timestamp: %s",
        strerror(error));
    return ErrorStatus(error);
  }

  return {timestamp};
}

HardwareComposer::ComposerCallback::DisplayInfo*
HardwareComposer::ComposerCallback::GetDisplayInfo(hwc2_display_t display) {
  if (display == primary_display_.id) {
    return &primary_display_;
  } else if (external_display_ && display == external_display_->id) {
    return &(*external_display_);
  }
  return nullptr;
}

void Layer::Reset() {
  if (hardware_composer_layer_) {
    HWC::Error error =
        composer_->destroyLayer(display_params_.id, hardware_composer_layer_);
    if (error != HWC::Error::None &&
        (!ignore_bad_display_errors_on_destroy_ ||
         error != HWC::Error::BadDisplay)) {
      ALOGE("destroyLayer() failed for display %" PRIu64 ", layer %" PRIu64
          ". error: %s", display_params_.id, hardware_composer_layer_,
          error.to_string().c_str());
    }
    hardware_composer_layer_ = 0;
  }

  z_order_ = 0;
  blending_ = HWC::BlendMode::None;
  composition_type_ = HWC::Composition::Invalid;
  target_composition_type_ = composition_type_;
  source_ = EmptyVariant{};
  acquire_fence_.Close();
  surface_rect_functions_applied_ = false;
  pending_visibility_settings_ = true;
  cached_buffer_map_.clear();
  ignore_bad_display_errors_on_destroy_ = false;
}

Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
             const std::shared_ptr<DirectDisplaySurface>& surface,
             HWC::BlendMode blending, HWC::Composition composition_type,
             size_t z_order)
    : composer_(composer),
      display_params_(display_params),
      z_order_{z_order},
      blending_{blending},
      target_composition_type_{composition_type},
      source_{SourceSurface{surface}} {
  CommonLayerSetup();
}

Layer::Layer(Hwc2::Composer* composer, const DisplayParams& display_params,
             const std::shared_ptr<IonBuffer>& buffer, HWC::BlendMode blending,
             HWC::Composition composition_type, size_t z_order)
    : composer_(composer),
      display_params_(display_params),
      z_order_{z_order},
      blending_{blending},
      target_composition_type_{composition_type},
      source_{SourceBuffer{buffer}} {
  CommonLayerSetup();
}

Layer::~Layer() { Reset(); }

Layer::Layer(Layer&& other) noexcept { *this = std::move(other); }

Layer& Layer::operator=(Layer&& other) noexcept {
  if (this != &other) {
    Reset();
    using std::swap;
    swap(composer_, other.composer_);
    swap(display_params_, other.display_params_);
    swap(hardware_composer_layer_, other.hardware_composer_layer_);
    swap(z_order_, other.z_order_);
    swap(blending_, other.blending_);
    swap(composition_type_, other.composition_type_);
    swap(target_composition_type_, other.target_composition_type_);
    swap(source_, other.source_);
    swap(acquire_fence_, other.acquire_fence_);
    swap(surface_rect_functions_applied_,
         other.surface_rect_functions_applied_);
    swap(pending_visibility_settings_, other.pending_visibility_settings_);
    swap(cached_buffer_map_, other.cached_buffer_map_);
    swap(ignore_bad_display_errors_on_destroy_,
         other.ignore_bad_display_errors_on_destroy_);
  }
  return *this;
}

void Layer::UpdateBuffer(const std::shared_ptr<IonBuffer>& buffer) {
  if (source_.is<SourceBuffer>())
    std::get<SourceBuffer>(source_) = {buffer};
}

void Layer::SetBlending(HWC::BlendMode blending) {
  if (blending_ != blending) {
    blending_ = blending;
    pending_visibility_settings_ = true;
  }
}

void Layer::SetZOrder(size_t z_order) {
  if (z_order_ != z_order) {
    z_order_ = z_order;
    pending_visibility_settings_ = true;
  }
}

IonBuffer* Layer::GetBuffer() {
  struct Visitor {
    IonBuffer* operator()(SourceSurface& source) { return source.GetBuffer(); }
    IonBuffer* operator()(SourceBuffer& source) { return source.GetBuffer(); }
    IonBuffer* operator()(EmptyVariant) { return nullptr; }
  };
  return source_.Visit(Visitor{});
}

void Layer::UpdateVisibilitySettings() {
  if (pending_visibility_settings_) {
    pending_visibility_settings_ = false;

    HWC::Error error;

    error = composer_->setLayerBlendMode(
        display_params_.id, hardware_composer_layer_,
        blending_.cast<Hwc2::IComposerClient::BlendMode>());
    ALOGE_IF(error != HWC::Error::None,
             "Layer::UpdateLayerSettings: Error setting layer blend mode: %s",
             error.to_string().c_str());

    error = composer_->setLayerZOrder(display_params_.id,
        hardware_composer_layer_, z_order_);
    ALOGE_IF(error != HWC::Error::None,
             "Layer::UpdateLayerSettings: Error setting z_ order: %s",
             error.to_string().c_str());
  }
}

void Layer::UpdateLayerSettings() {
  HWC::Error error;

  UpdateVisibilitySettings();

  // TODO(eieio): Use surface attributes or some other mechanism to control
  // the layer display frame.
  error = composer_->setLayerDisplayFrame(
      display_params_.id, hardware_composer_layer_,
      {0, 0, display_params_.width, display_params_.height});
  ALOGE_IF(error != HWC::Error::None,
           "Layer::UpdateLayerSettings: Error setting layer display frame: %s",
           error.to_string().c_str());

  error = composer_->setLayerVisibleRegion(
      display_params_.id, hardware_composer_layer_,
      {{0, 0, display_params_.width, display_params_.height}});
  ALOGE_IF(error != HWC::Error::None,
           "Layer::UpdateLayerSettings: Error setting layer visible region: %s",
           error.to_string().c_str());

  error = composer_->setLayerPlaneAlpha(display_params_.id,
      hardware_composer_layer_, 1.0f);
  ALOGE_IF(error != HWC::Error::None,
           "Layer::UpdateLayerSettings: Error setting layer plane alpha: %s",
           error.to_string().c_str());
}

void Layer::CommonLayerSetup() {
  HWC::Error error = composer_->createLayer(display_params_.id,
                                            &hardware_composer_layer_);
  ALOGE_IF(error != HWC::Error::None,
           "Layer::CommonLayerSetup: Failed to create layer on primary "
           "display: %s",
           error.to_string().c_str());
  UpdateLayerSettings();
}

bool Layer::CheckAndUpdateCachedBuffer(std::size_t slot, int buffer_id) {
  auto search = cached_buffer_map_.find(slot);
  if (search != cached_buffer_map_.end() && search->second == buffer_id)
    return true;

  // Assign or update the buffer slot.
  if (buffer_id >= 0)
    cached_buffer_map_[slot] = buffer_id;
  return false;
}

void Layer::Prepare() {
  int right, bottom, id;
  sp<GraphicBuffer> handle;
  std::size_t slot;

  // Acquire the next buffer according to the type of source.
  IfAnyOf<SourceSurface, SourceBuffer>::Call(&source_, [&](auto& source) {
    std::tie(right, bottom, id, handle, acquire_fence_, slot) =
        source.Acquire();
  });

  TRACE_FORMAT("Layer::Prepare|buffer_id=%d;slot=%zu|", id, slot);

  // Update any visibility (blending, z-order) changes that occurred since
  // last prepare.
  UpdateVisibilitySettings();

  // When a layer is first setup there may be some time before the first
  // buffer arrives. Setup the HWC layer as a solid color to stall for time
  // until the first buffer arrives. Once the first buffer arrives there will
  // always be a buffer for the frame even if it is old.
  if (!handle.get()) {
    if (composition_type_ == HWC::Composition::Invalid) {
      composition_type_ = HWC::Composition::SolidColor;
      composer_->setLayerCompositionType(
          display_params_.id, hardware_composer_layer_,
          composition_type_.cast<Hwc2::IComposerClient::Composition>());
      Hwc2::IComposerClient::Color layer_color = {0, 0, 0, 0};
      composer_->setLayerColor(display_params_.id, hardware_composer_layer_,
                               layer_color);
    } else {
      // The composition type is already set. Nothing else to do until a
      // buffer arrives.
    }
  } else {
    if (composition_type_ != target_composition_type_) {
      composition_type_ = target_composition_type_;
      composer_->setLayerCompositionType(
          display_params_.id, hardware_composer_layer_,
          composition_type_.cast<Hwc2::IComposerClient::Composition>());
    }

    // See if the HWC cache already has this buffer.
    const bool cached = CheckAndUpdateCachedBuffer(slot, id);
    if (cached)
      handle = nullptr;

    HWC::Error error{HWC::Error::None};
    error =
        composer_->setLayerBuffer(display_params_.id, hardware_composer_layer_,
                                  slot, handle, acquire_fence_.Get());

    ALOGE_IF(error != HWC::Error::None,
             "Layer::Prepare: Error setting layer buffer: %s",
             error.to_string().c_str());

    if (!surface_rect_functions_applied_) {
      const float float_right = right;
      const float float_bottom = bottom;
      error = composer_->setLayerSourceCrop(display_params_.id,
                                            hardware_composer_layer_,
                                            {0, 0, float_right, float_bottom});

      ALOGE_IF(error != HWC::Error::None,
               "Layer::Prepare: Error setting layer source crop: %s",
               error.to_string().c_str());

      surface_rect_functions_applied_ = true;
    }
  }
}

void Layer::Finish(int release_fence_fd) {
  IfAnyOf<SourceSurface, SourceBuffer>::Call(
      &source_, [release_fence_fd](auto& source) {
        source.Finish(LocalHandle(release_fence_fd));
      });
}

void Layer::Drop() { acquire_fence_.Close(); }

}  // namespace dvr
}  // namespace android