blob: c0eed0cbeeefdfc45a6d518459d2e0493d6ed366 [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();
Roman Stratiienko0fade372021-02-20 13:59:55 +020075 UniqueFd TakeOutFence() {
76 if (!active_composition_) {
77 return UniqueFd();
78 }
79 return std::move(active_composition_->out_fence_);
Matteo Franchinc56eede2019-12-03 17:10:38 +000080 }
Sean Paul98e73c82015-06-24 14:38:49 -070081
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020082 FlatteningState GetFlatteningState() const;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020083 uint32_t GetFlattenedFramesCount() const;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020084 bool ShouldFlattenOnClient() const;
85
Zach Reizner92f8e632015-10-12 17:47:13 -070086 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
87
Sean Paul98e73c82015-06-24 14:38:49 -070088 private:
Sean Paul35301f42015-11-17 14:46:56 -050089 struct ModeState {
90 bool needs_modeset = false;
91 DrmMode mode;
92 uint32_t blob_id = 0;
93 uint32_t old_blob_id = 0;
94 };
95
Sean Paul98e73c82015-06-24 14:38:49 -070096 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
97
Sean Paul971be152015-10-13 15:44:45 -040098 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
99 // kAcquireWaitTries times, logging a warning in between.
100 static const int kAcquireWaitTries = 5;
101 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -0400102
Roman Stratiienko74774712021-02-05 16:32:47 +0200103 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700104 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400105 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -0700106
Haixia Shidda2fab2015-10-22 18:12:49 -0700107 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
Roman Stratiienko74774712021-02-05 16:32:47 +0200108 int status);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200109
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200110 void SetFlattening(FlatteningState new_state);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200111 bool IsFlatteningNeeded() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100112 int FlattenActiveComposition();
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200113 int FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100114
115 bool CountdownExpired() const;
Haixia Shidda2fab2015-10-22 18:12:49 -0700116
Sean Paul35301f42015-11-17 14:46:56 -0500117 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
118
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100119 ResourceManager *resource_manager_;
Sean Paul98e73c82015-06-24 14:38:49 -0700120 int display_;
121
Sean Paul98e73c82015-06-24 14:38:49 -0700122 std::unique_ptr<DrmDisplayComposition> active_composition_;
123
Sean Paul98e73c82015-06-24 14:38:49 -0700124 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700125 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500126 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700127
Sean Paul35301f42015-11-17 14:46:56 -0500128 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400129
Sean Pauled45a8e2017-02-28 13:17:34 -0500130 // mutable since we need to acquire in Dump()
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200131 mutable pthread_mutex_t lock_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700132
133 // State tracking progress since our last Dump(). These are mutable since
134 // we need to reset them on every Dump() call.
135 mutable uint64_t dump_frames_composited_;
136 mutable uint64_t dump_last_timestamp_ns_;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100137 VSyncWorker vsync_worker_;
138 int64_t flatten_countdown_;
139 std::unique_ptr<Planner> planner_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200140
141 FlatteningState flattening_state_;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200142 uint32_t frames_flattened_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200143
144 std::function<void(int)> refresh_display_cb_;
Sean Paul98e73c82015-06-24 14:38:49 -0700145};
Sean Paulf72cccd2018-08-27 13:59:08 -0400146} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -0700147
148#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_