blob: aebb6ff53f83c4d531160eb8703707912ad0bdad [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_DRM_DISPLAY_COMPOSITOR_H_
18#define ANDROID_DRM_DISPLAY_COMPOSITOR_H_
19
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030020#include <hardware/hardware.h>
21#include <hardware/hwcomposer.h>
Sean Paul98e73c82015-06-24 14:38:49 -070022#include <pthread.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030023
Roman Stratiienkod21071f2021-03-09 21:56:50 +020024#include <functional>
Zach Reizner92f8e632015-10-12 17:47:13 -070025#include <memory>
Sean Paul98e73c82015-06-24 14:38:49 -070026#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070027#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070028
Roman Stratiienko13cc3662020-08-29 21:35:39 +030029#include "DrmDisplayComposition.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030#include "Planner.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030031#include "drm/ResourceManager.h"
32#include "drm/VSyncWorker.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030033#include "drmhwcomposer.h"
Sean Paul98e73c82015-06-24 14:38:49 -070034
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010035// If a scene is still for this number of vblanks flatten it to reduce power
36// consumption.
37#define FLATTEN_COUNTDOWN_INIT 60
38
Sean Paul98e73c82015-06-24 14:38:49 -070039namespace android {
40
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020041enum class FlatteningState {
42 kNone,
43 kNotNeeded,
44 kClientRequested,
45 kClientDone,
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020046};
47
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020048std::ostream &operator<<(std::ostream &str, FlatteningState state);
49
Sean Paul98e73c82015-06-24 14:38:49 -070050class DrmDisplayCompositor {
51 public:
52 DrmDisplayCompositor();
53 ~DrmDisplayCompositor();
54
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010055 int Init(ResourceManager *resource_manager, int display);
Sean Paul98e73c82015-06-24 14:38:49 -070056
Roman Stratiienko23701092020-09-26 02:08:41 +030057 hwc2_callback_data_t refresh_callback_data_ = NULL;
58 HWC2_PFN_REFRESH refresh_callback_hook_ = NULL;
59 std::mutex refresh_callback_lock;
60
61 void SetRefreshCallback(hwc2_callback_data_t data,
62 hwc2_function_pointer_t hook) {
63 const std::lock_guard<std::mutex> lock(refresh_callback_lock);
64 refresh_callback_data_ = data;
65 refresh_callback_hook_ = reinterpret_cast<HWC2_PFN_REFRESH>(hook);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020066 }
67
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010068 std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
Sean Pauled45a8e2017-02-28 13:17:34 -050069 int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
Rob Herring4f6c62e2018-05-17 14:33:02 -050070 int TestComposition(DrmDisplayComposition *composition);
Sean Paul98e73c82015-06-24 14:38:49 -070071 int Composite();
72 void Dump(std::ostringstream *out) const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010073 void Vsync(int display, int64_t timestamp);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074 void ClearDisplay();
Matteo Franchinc56eede2019-12-03 17:10:38 +000075 int TakeOutFence() {
76 if (!active_composition_)
77 return -1;
78 return active_composition_->take_out_fence();
79 }
Sean Paul98e73c82015-06-24 14:38:49 -070080
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020081 FlatteningState GetFlatteningState() const;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020082 uint32_t GetFlattenedFramesCount() const;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020083 bool ShouldFlattenOnClient() const;
84
Zach Reizner92f8e632015-10-12 17:47:13 -070085 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
86
Sean Paul98e73c82015-06-24 14:38:49 -070087 private:
Sean Paul35301f42015-11-17 14:46:56 -050088 struct ModeState {
89 bool needs_modeset = false;
90 DrmMode mode;
91 uint32_t blob_id = 0;
92 uint32_t old_blob_id = 0;
93 };
94
Sean Paul98e73c82015-06-24 14:38:49 -070095 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
96
Sean Paul971be152015-10-13 15:44:45 -040097 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
98 // kAcquireWaitTries times, logging a warning in between.
99 static const int kAcquireWaitTries = 5;
100 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -0400101
Roman Stratiienko74774712021-02-05 16:32:47 +0200102 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700103 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400104 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -0700105
Haixia Shidda2fab2015-10-22 18:12:49 -0700106 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
Roman Stratiienko74774712021-02-05 16:32:47 +0200107 int status);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200108
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200109 void SetFlattening(FlatteningState new_state);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200110 bool IsFlatteningNeeded() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100111 int FlattenActiveComposition();
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200112 int FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100113
114 bool CountdownExpired() const;
Haixia Shidda2fab2015-10-22 18:12:49 -0700115
Sean Paul35301f42015-11-17 14:46:56 -0500116 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
117
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100118 ResourceManager *resource_manager_;
Sean Paul98e73c82015-06-24 14:38:49 -0700119 int display_;
120
Sean Paul98e73c82015-06-24 14:38:49 -0700121 std::unique_ptr<DrmDisplayComposition> active_composition_;
122
Sean Paul98e73c82015-06-24 14:38:49 -0700123 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700124 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500125 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700126
Sean Paul35301f42015-11-17 14:46:56 -0500127 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400128
Sean Pauled45a8e2017-02-28 13:17:34 -0500129 // mutable since we need to acquire in Dump()
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200130 mutable pthread_mutex_t lock_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700131
132 // State tracking progress since our last Dump(). These are mutable since
133 // we need to reset them on every Dump() call.
134 mutable uint64_t dump_frames_composited_;
135 mutable uint64_t dump_last_timestamp_ns_;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100136 VSyncWorker vsync_worker_;
137 int64_t flatten_countdown_;
138 std::unique_ptr<Planner> planner_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200139
140 FlatteningState flattening_state_;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200141 uint32_t frames_flattened_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200142
143 std::function<void(int)> refresh_display_cb_;
Sean Paul98e73c82015-06-24 14:38:49 -0700144};
Sean Paulf72cccd2018-08-27 13:59:08 -0400145} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -0700146
147#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_