blob: afdb79ecb46eb9ebb6b017169a721b2728d47347 [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
Roman Stratiienko863a3c22021-09-29 13:00:29 +030055 auto Init(ResourceManager *resource_manager, int display,
56 std::function<void()> client_refresh_callback) -> int;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020057
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010058 std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
Sean Pauled45a8e2017-02-28 13:17:34 -050059 int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
Rob Herring4f6c62e2018-05-17 14:33:02 -050060 int TestComposition(DrmDisplayComposition *composition);
Sean Paul98e73c82015-06-24 14:38:49 -070061 int Composite();
62 void Dump(std::ostringstream *out) const;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030063 void ClearDisplay();
Roman Stratiienko0fade372021-02-20 13:59:55 +020064 UniqueFd TakeOutFence() {
65 if (!active_composition_) {
66 return UniqueFd();
67 }
68 return std::move(active_composition_->out_fence_);
Matteo Franchinc56eede2019-12-03 17:10:38 +000069 }
Sean Paul98e73c82015-06-24 14:38:49 -070070
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020071 FlatteningState GetFlatteningState() const;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020072 uint32_t GetFlattenedFramesCount() const;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020073 bool ShouldFlattenOnClient() const;
74
Zach Reizner92f8e632015-10-12 17:47:13 -070075 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
76
Sean Paul98e73c82015-06-24 14:38:49 -070077 private:
Roman Stratiienko863a3c22021-09-29 13:00:29 +030078 std::function<void()> client_refresh_callback_;
Sean Paul35301f42015-11-17 14:46:56 -050079 struct ModeState {
80 bool needs_modeset = false;
81 DrmMode mode;
82 uint32_t blob_id = 0;
83 uint32_t old_blob_id = 0;
84 };
85
Sean Paul98e73c82015-06-24 14:38:49 -070086 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
87
Sean Paul971be152015-10-13 15:44:45 -040088 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
89 // kAcquireWaitTries times, logging a warning in between.
90 static const int kAcquireWaitTries = 5;
91 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -040092
Roman Stratiienko74774712021-02-05 16:32:47 +020093 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauldb7a17d2015-06-24 18:46:05 -070094 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -040095 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -070096
Haixia Shidda2fab2015-10-22 18:12:49 -070097 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
Roman Stratiienko74774712021-02-05 16:32:47 +020098 int status);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020099
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200100 void SetFlattening(FlatteningState new_state);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200101 bool IsFlatteningNeeded() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100102 int FlattenActiveComposition();
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200103 int FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100104
105 bool CountdownExpired() const;
Haixia Shidda2fab2015-10-22 18:12:49 -0700106
Sean Paul35301f42015-11-17 14:46:56 -0500107 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
108
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100109 ResourceManager *resource_manager_;
Sean Paul98e73c82015-06-24 14:38:49 -0700110 int display_;
111
Sean Paul98e73c82015-06-24 14:38:49 -0700112 std::unique_ptr<DrmDisplayComposition> active_composition_;
113
Sean Paul98e73c82015-06-24 14:38:49 -0700114 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700115 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500116 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700117
Sean Paul35301f42015-11-17 14:46:56 -0500118 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400119
Sean Pauled45a8e2017-02-28 13:17:34 -0500120 // mutable since we need to acquire in Dump()
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200121 mutable pthread_mutex_t lock_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700122
123 // State tracking progress since our last Dump(). These are mutable since
124 // we need to reset them on every Dump() call.
125 mutable uint64_t dump_frames_composited_;
126 mutable uint64_t dump_last_timestamp_ns_;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100127 VSyncWorker vsync_worker_;
128 int64_t flatten_countdown_;
129 std::unique_ptr<Planner> planner_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200130
131 FlatteningState flattening_state_;
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200132 uint32_t frames_flattened_;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200133
134 std::function<void(int)> refresh_display_cb_;
Sean Paul98e73c82015-06-24 14:38:49 -0700135};
Sean Paulf72cccd2018-08-27 13:59:08 -0400136} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -0700137
138#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_