blob: bc19f35d51d17f7a1f55d069164893541ace1574 [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
Zach Reizner7642c922015-10-29 10:11:16 -070020#include "drmhwcomposer.h"
Sean Paul98e73c82015-06-24 14:38:49 -070021#include "drmcomposition.h"
22#include "drmcompositorworker.h"
Zach Reizner713a6782015-07-31 15:12:44 -070023#include "drmframebuffer.h"
Haixia Shiaa2f4a52015-11-02 10:54:29 -080024#include "separate_rects.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 <queue>
29#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070030#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070031
32#include <hardware/hardware.h>
33#include <hardware/hwcomposer.h>
34
Zach Reizner713a6782015-07-31 15:12:44 -070035#define DRM_DISPLAY_BUFFERS 2
36
Sean Paul98e73c82015-06-24 14:38:49 -070037namespace android {
38
Zach Reizner713a6782015-07-31 15:12:44 -070039class GLWorkerCompositor;
40
Zach Reizner92f8e632015-10-12 17:47:13 -070041class SquashState {
42 public:
Haixia Shidda2fab2015-10-22 18:12:49 -070043 static const unsigned kHistoryLength = 6; // TODO: make this number not magic
Zach Reizner92f8e632015-10-12 17:47:13 -070044 static const unsigned kMaxLayers = 64;
45
46 struct Region {
47 DrmHwcRect<int> rect;
48 std::bitset<kMaxLayers> layer_refs;
49 std::bitset<kHistoryLength> change_history;
50 bool squashed = false;
51 };
52
53 bool is_stable(int region_index) const {
54 return valid_history_ >= kHistoryLength &&
55 regions_[region_index].change_history.none();
56 }
57
58 const std::vector<Region> &regions() const {
59 return regions_;
60 }
61
62 void Init(DrmHwcLayer *layers, size_t num_layers);
Zach Reizner5757e822015-10-16 19:06:31 -070063 void GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -070064 std::vector<bool> &changed_regions) const;
65 void StableRegionsWithMarginalHistory(
66 const std::vector<bool> &changed_regions,
67 std::vector<bool> &stable_regions) const;
Zach Reizner5757e822015-10-16 19:06:31 -070068 void RecordHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -070069 const std::vector<bool> &changed_regions);
Zach Reizner5757e822015-10-16 19:06:31 -070070 bool RecordAndCompareSquashed(const std::vector<bool> &squashed_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -070071
Zach Reiznerfd6dc332015-10-13 21:12:48 -070072 void Dump(std::ostringstream *out) const;
73
Zach Reizner92f8e632015-10-12 17:47:13 -070074 private:
75 size_t generation_number_ = 0;
76 unsigned valid_history_ = 0;
77 std::vector<buffer_handle_t> last_handles_;
78
79 std::vector<Region> regions_;
80};
81
Sean Paul98e73c82015-06-24 14:38:49 -070082class DrmDisplayCompositor {
83 public:
84 DrmDisplayCompositor();
85 ~DrmDisplayCompositor();
86
87 int Init(DrmResources *drm, int display);
88
Zach Reizner92f8e632015-10-12 17:47:13 -070089 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
Sean Paul98e73c82015-06-24 14:38:49 -070090 int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition);
91 int Composite();
Zach Reiznerbff33ac2015-11-16 11:08:46 -080092 int SquashAll();
Sean Paul98e73c82015-06-24 14:38:49 -070093 void Dump(std::ostringstream *out) const;
94
Zach Reizner92f8e632015-10-12 17:47:13 -070095 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
96
Sean Paul98e73c82015-06-24 14:38:49 -070097 bool HaveQueuedComposites() const;
98
Zach Reizner92f8e632015-10-12 17:47:13 -070099 SquashState *squash_state() {
Zach Reizner5757e822015-10-16 19:06:31 -0700100 return &squash_state_;
Zach Reizner92f8e632015-10-12 17:47:13 -0700101 }
102
Sean Paul98e73c82015-06-24 14:38:49 -0700103 private:
Haixia Shidda2fab2015-10-22 18:12:49 -0700104 struct FrameState {
105 std::unique_ptr<DrmDisplayComposition> composition;
106 int status = 0;
107 };
108
109 class FrameWorker : public Worker {
110 public:
111 FrameWorker(DrmDisplayCompositor *compositor);
Haixia Shi479412c2015-10-27 10:40:48 -0700112 ~FrameWorker() override;
Haixia Shidda2fab2015-10-22 18:12:49 -0700113
114 int Init();
115 void QueueFrame(std::unique_ptr<DrmDisplayComposition> composition,
116 int status);
117
118 protected:
Haixia Shi479412c2015-10-27 10:40:48 -0700119 void Routine() override;
Haixia Shidda2fab2015-10-22 18:12:49 -0700120
121 private:
122 DrmDisplayCompositor *compositor_;
123 std::queue<FrameState> frame_queue_;
124 };
125
Sean Paul35301f42015-11-17 14:46:56 -0500126 struct ModeState {
127 bool needs_modeset = false;
128 DrmMode mode;
129 uint32_t blob_id = 0;
130 uint32_t old_blob_id = 0;
131 };
132
Sean Paul98e73c82015-06-24 14:38:49 -0700133 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
134
Sean Paul971be152015-10-13 15:44:45 -0400135 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
136 // kAcquireWaitTries times, logging a warning in between.
137 static const int kAcquireWaitTries = 5;
138 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -0400139
Zach Reizner92f8e632015-10-12 17:47:13 -0700140 int PrepareFramebuffer(DrmFramebuffer &fb,
141 DrmDisplayComposition *display_comp);
Zach Reizner5757e822015-10-16 19:06:31 -0700142 int ApplySquash(DrmDisplayComposition *display_comp);
Zach Reizner713a6782015-07-31 15:12:44 -0700143 int ApplyPreComposite(DrmDisplayComposition *display_comp);
Haixia Shidda2fab2015-10-22 18:12:49 -0700144 int PrepareFrame(DrmDisplayComposition *display_comp);
Sean Paulc07b2112015-11-17 16:38:10 -0500145 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700146 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400147 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -0700148
Haixia Shidda2fab2015-10-22 18:12:49 -0700149 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
150 int status);
151
Sean Paul35301f42015-11-17 14:46:56 -0500152 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
153
Sean Paul98e73c82015-06-24 14:38:49 -0700154 DrmResources *drm_;
155 int display_;
156
157 DrmCompositorWorker worker_;
Haixia Shidda2fab2015-10-22 18:12:49 -0700158 FrameWorker frame_worker_;
Sean Paul98e73c82015-06-24 14:38:49 -0700159
160 std::queue<std::unique_ptr<DrmDisplayComposition>> composite_queue_;
161 std::unique_ptr<DrmDisplayComposition> active_composition_;
162
Sean Paul98e73c82015-06-24 14:38:49 -0700163 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700164 bool active_;
Sean Paul98e73c82015-06-24 14:38:49 -0700165
Sean Paul35301f42015-11-17 14:46:56 -0500166 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400167
Zach Reizner713a6782015-07-31 15:12:44 -0700168 int framebuffer_index_;
169 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
170 std::unique_ptr<GLWorkerCompositor> pre_compositor_;
171
Zach Reizner5757e822015-10-16 19:06:31 -0700172 SquashState squash_state_;
173 int squash_framebuffer_index_;
174 DrmFramebuffer squash_framebuffers_[2];
175
Sean Paul98e73c82015-06-24 14:38:49 -0700176 // mutable since we need to acquire in HaveQueuedComposites
177 mutable pthread_mutex_t lock_;
178
179 // State tracking progress since our last Dump(). These are mutable since
180 // we need to reset them on every Dump() call.
181 mutable uint64_t dump_frames_composited_;
182 mutable uint64_t dump_last_timestamp_ns_;
183};
184}
185
186#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_