blob: 92dadca82412afaaf098120515d0bceef3afce7e [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
Zach Reizner92f8e632015-10-12 17:47:13 -070024#include <memory>
Sean Paul98e73c82015-06-24 14:38:49 -070025#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070026#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070027
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "DrmDisplayComposition.h"
29#include "DrmFramebuffer.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
Sean Paul647beb22015-11-19 13:55:48 -050035// One for the front, one for the back, and one for cases where we need to
36// squash a frame that the hw can't display with hw overlays.
37#define DRM_DISPLAY_BUFFERS 3
Zach Reizner713a6782015-07-31 15:12:44 -070038
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010039// If a scene is still for this number of vblanks flatten it to reduce power
40// consumption.
41#define FLATTEN_COUNTDOWN_INIT 60
42
Sean Paul98e73c82015-06-24 14:38:49 -070043namespace android {
44
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020045enum class FlatteningState {
46 kNone,
47 kNotNeeded,
48 kClientRequested,
49 kClientDone,
50 kSerial,
51 kConcurrent
52};
53
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020054std::ostream &operator<<(std::ostream &str, FlatteningState state);
55
Sean Paul98e73c82015-06-24 14:38:49 -070056class DrmDisplayCompositor {
57 public:
58 DrmDisplayCompositor();
59 ~DrmDisplayCompositor();
60
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010061 int Init(ResourceManager *resource_manager, int display);
Sean Paul98e73c82015-06-24 14:38:49 -070062
Roman Stratiienko23701092020-09-26 02:08:41 +030063 hwc2_callback_data_t refresh_callback_data_ = NULL;
64 HWC2_PFN_REFRESH refresh_callback_hook_ = NULL;
65 std::mutex refresh_callback_lock;
66
67 void SetRefreshCallback(hwc2_callback_data_t data,
68 hwc2_function_pointer_t hook) {
69 const std::lock_guard<std::mutex> lock(refresh_callback_lock);
70 refresh_callback_data_ = data;
71 refresh_callback_hook_ = reinterpret_cast<HWC2_PFN_REFRESH>(hook);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020072 }
73
Zach Reizner92f8e632015-10-12 17:47:13 -070074 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010075 std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
Sean Pauled45a8e2017-02-28 13:17:34 -050076 int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
Rob Herring4f6c62e2018-05-17 14:33:02 -050077 int TestComposition(DrmDisplayComposition *composition);
Sean Paul98e73c82015-06-24 14:38:49 -070078 int Composite();
79 void Dump(std::ostringstream *out) const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010080 void Vsync(int display, int64_t timestamp);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030081 void ClearDisplay();
Matteo Franchinc56eede2019-12-03 17:10:38 +000082 int TakeOutFence() {
83 if (!active_composition_)
84 return -1;
85 return active_composition_->take_out_fence();
86 }
Sean Paul98e73c82015-06-24 14:38:49 -070087
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020088 FlatteningState GetFlatteningState() const;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020089 uint32_t GetFlattenedFramesCount() const;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020090 bool ShouldFlattenOnClient() const;
91
Zach Reizner92f8e632015-10-12 17:47:13 -070092 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
93
Sean Paul98e73c82015-06-24 14:38:49 -070094 private:
Sean Paul35301f42015-11-17 14:46:56 -050095 struct ModeState {
96 bool needs_modeset = false;
97 DrmMode mode;
98 uint32_t blob_id = 0;
99 uint32_t old_blob_id = 0;
100 };
101
Sean Paul98e73c82015-06-24 14:38:49 -0700102 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
103
Sean Paul971be152015-10-13 15:44:45 -0400104 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
105 // kAcquireWaitTries times, logging a warning in between.
106 static const int kAcquireWaitTries = 5;
107 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -0400108
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100109 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only,
110 DrmConnector *writeback_conn = NULL,
111 DrmHwcBuffer *writeback_buffer = NULL);
112 int SetupWritebackCommit(drmModeAtomicReqPtr pset, uint32_t crtc_id,
113 DrmConnector *writeback_conn,
114 DrmHwcBuffer *writeback_buffer);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700115 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400116 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -0700117
Haixia Shidda2fab2015-10-22 18:12:49 -0700118 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100119 int status, bool writeback = false);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200120
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200121 void SetFlattening(FlatteningState new_state);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200122 bool IsFlatteningNeeded() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100123 int FlattenActiveComposition();
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200124 int FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100125 int FlattenSerial(DrmConnector *writeback_conn);
126 int FlattenConcurrent(DrmConnector *writeback_conn);
127 int FlattenOnDisplay(std::unique_ptr<DrmDisplayComposition> &src,
128 DrmConnector *writeback_conn, DrmMode &src_mode,
129 DrmHwcLayer *writeback_layer);
130
131 bool CountdownExpired() const;
Haixia Shidda2fab2015-10-22 18:12:49 -0700132
Sean Paul35301f42015-11-17 14:46:56 -0500133 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
134
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100135 ResourceManager *resource_manager_;
Sean Paul98e73c82015-06-24 14:38:49 -0700136 int display_;
137
Sean Paul98e73c82015-06-24 14:38:49 -0700138 std::unique_ptr<DrmDisplayComposition> active_composition_;
139
Sean Paul98e73c82015-06-24 14:38:49 -0700140 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700141 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500142 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700143
Sean Paul35301f42015-11-17 14:46:56 -0500144 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400145
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200146 int framebuffer_index_{};
Zach Reizner713a6782015-07-31 15:12:44 -0700147 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
Zach Reizner5757e822015-10-16 19:06:31 -0700148
Sean Pauled45a8e2017-02-28 13:17:34 -0500149 // mutable since we need to acquire in Dump()
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200150 mutable pthread_mutex_t lock_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700151
152 // State tracking progress since our last Dump(). These are mutable since
153 // we need to reset them on every Dump() call.
154 mutable uint64_t dump_frames_composited_;
155 mutable uint64_t dump_last_timestamp_ns_;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100156 VSyncWorker vsync_worker_;
157 int64_t flatten_countdown_;
158 std::unique_ptr<Planner> planner_;
159 int writeback_fence_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200160
161 FlatteningState flattening_state_;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200162 uint32_t frames_flattened_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200163
164 std::function<void(int)> refresh_display_cb_;
Sean Paul98e73c82015-06-24 14:38:49 -0700165};
Sean Paulf72cccd2018-08-27 13:59:08 -0400166} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -0700167
168#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_