blob: 477f226f464592d63e58467daf0eb2256b292fb7 [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
Sean Pauled45a8e2017-02-28 13:17:34 -050020#include "drmdisplaycomposition.h"
Zach Reizner713a6782015-07-31 15:12:44 -070021#include "drmframebuffer.h"
Sean Paulf72cccd2018-08-27 13:59:08 -040022#include "drmhwcomposer.h"
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010023#include "resourcemanager.h"
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010024#include "vsyncworker.h"
Sean Paul98e73c82015-06-24 14:38:49 -070025
26#include <pthread.h>
Zach Reizner92f8e632015-10-12 17:47:13 -070027#include <memory>
Sean Paul98e73c82015-06-24 14:38:49 -070028#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070029#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070030
31#include <hardware/hardware.h>
32#include <hardware/hwcomposer.h>
33
Sean Paul647beb22015-11-19 13:55:48 -050034// One for the front, one for the back, and one for cases where we need to
35// squash a frame that the hw can't display with hw overlays.
36#define DRM_DISPLAY_BUFFERS 3
Zach Reizner713a6782015-07-31 15:12:44 -070037
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010038// If a scene is still for this number of vblanks flatten it to reduce power
39// consumption.
40#define FLATTEN_COUNTDOWN_INIT 60
41
Sean Paul98e73c82015-06-24 14:38:49 -070042namespace android {
43
44class DrmDisplayCompositor {
45 public:
46 DrmDisplayCompositor();
47 ~DrmDisplayCompositor();
48
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010049 int Init(ResourceManager *resource_manager, int display);
Sean Paul98e73c82015-06-24 14:38:49 -070050
Zach Reizner92f8e632015-10-12 17:47:13 -070051 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010052 std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
Sean Pauled45a8e2017-02-28 13:17:34 -050053 int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
Rob Herring4f6c62e2018-05-17 14:33:02 -050054 int TestComposition(DrmDisplayComposition *composition);
Sean Paul98e73c82015-06-24 14:38:49 -070055 int Composite();
56 void Dump(std::ostringstream *out) const;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010057 void Vsync(int display, int64_t timestamp);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030058 void ClearDisplay();
Matteo Franchinc56eede2019-12-03 17:10:38 +000059 int TakeOutFence() {
60 if (!active_composition_)
61 return -1;
62 return active_composition_->take_out_fence();
63 }
Sean Paul98e73c82015-06-24 14:38:49 -070064
Zach Reizner92f8e632015-10-12 17:47:13 -070065 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
66
Sean Paul98e73c82015-06-24 14:38:49 -070067 private:
Sean Paul35301f42015-11-17 14:46:56 -050068 struct ModeState {
69 bool needs_modeset = false;
70 DrmMode mode;
71 uint32_t blob_id = 0;
72 uint32_t old_blob_id = 0;
73 };
74
Sean Paul98e73c82015-06-24 14:38:49 -070075 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
76
Sean Paul971be152015-10-13 15:44:45 -040077 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
78 // kAcquireWaitTries times, logging a warning in between.
79 static const int kAcquireWaitTries = 5;
80 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -040081
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010082 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only,
83 DrmConnector *writeback_conn = NULL,
84 DrmHwcBuffer *writeback_buffer = NULL);
85 int SetupWritebackCommit(drmModeAtomicReqPtr pset, uint32_t crtc_id,
86 DrmConnector *writeback_conn,
87 DrmHwcBuffer *writeback_buffer);
Sean Pauldb7a17d2015-06-24 18:46:05 -070088 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -040089 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -070090
Haixia Shidda2fab2015-10-22 18:12:49 -070091 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010092 int status, bool writeback = false);
93 int FlattenActiveComposition();
94 int FlattenSerial(DrmConnector *writeback_conn);
95 int FlattenConcurrent(DrmConnector *writeback_conn);
96 int FlattenOnDisplay(std::unique_ptr<DrmDisplayComposition> &src,
97 DrmConnector *writeback_conn, DrmMode &src_mode,
98 DrmHwcLayer *writeback_layer);
99
100 bool CountdownExpired() const;
Haixia Shidda2fab2015-10-22 18:12:49 -0700101
Sean Paul35301f42015-11-17 14:46:56 -0500102 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
103
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100104 ResourceManager *resource_manager_;
Sean Paul98e73c82015-06-24 14:38:49 -0700105 int display_;
106
Sean Paul98e73c82015-06-24 14:38:49 -0700107 std::unique_ptr<DrmDisplayComposition> active_composition_;
108
Sean Paul98e73c82015-06-24 14:38:49 -0700109 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700110 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500111 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700112
Sean Paul35301f42015-11-17 14:46:56 -0500113 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400114
Zach Reizner713a6782015-07-31 15:12:44 -0700115 int framebuffer_index_;
116 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
Zach Reizner5757e822015-10-16 19:06:31 -0700117
Sean Pauled45a8e2017-02-28 13:17:34 -0500118 // mutable since we need to acquire in Dump()
Sean Paul98e73c82015-06-24 14:38:49 -0700119 mutable pthread_mutex_t lock_;
120
121 // State tracking progress since our last Dump(). These are mutable since
122 // we need to reset them on every Dump() call.
123 mutable uint64_t dump_frames_composited_;
124 mutable uint64_t dump_last_timestamp_ns_;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100125 VSyncWorker vsync_worker_;
126 int64_t flatten_countdown_;
127 std::unique_ptr<Planner> planner_;
128 int writeback_fence_;
Sean Paul98e73c82015-06-24 14:38:49 -0700129};
Sean Paulf72cccd2018-08-27 13:59:08 -0400130} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -0700131
132#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_