blob: e4c3b53e111f1d18991f7312ce426e0a27e9c23f [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
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
Sean Paul98e73c82015-06-24 14:38:49 -070039namespace android {
40
Zach Reizner713a6782015-07-31 15:12:44 -070041class GLWorkerCompositor;
42
Zach Reizner92f8e632015-10-12 17:47:13 -070043class SquashState {
44 public:
Haixia Shidda2fab2015-10-22 18:12:49 -070045 static const unsigned kHistoryLength = 6; // TODO: make this number not magic
Zach Reizner92f8e632015-10-12 17:47:13 -070046 static const unsigned kMaxLayers = 64;
47
48 struct Region {
49 DrmHwcRect<int> rect;
50 std::bitset<kMaxLayers> layer_refs;
51 std::bitset<kHistoryLength> change_history;
52 bool squashed = false;
53 };
54
55 bool is_stable(int region_index) const {
56 return valid_history_ >= kHistoryLength &&
57 regions_[region_index].change_history.none();
58 }
59
60 const std::vector<Region> &regions() const {
61 return regions_;
62 }
63
64 void Init(DrmHwcLayer *layers, size_t num_layers);
Zach Reizner5757e822015-10-16 19:06:31 -070065 void GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -070066 std::vector<bool> &changed_regions) const;
67 void StableRegionsWithMarginalHistory(
68 const std::vector<bool> &changed_regions,
69 std::vector<bool> &stable_regions) const;
Zach Reizner5757e822015-10-16 19:06:31 -070070 void RecordHistory(DrmHwcLayer *layers, size_t num_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -070071 const std::vector<bool> &changed_regions);
Zach Reizner5757e822015-10-16 19:06:31 -070072 bool RecordAndCompareSquashed(const std::vector<bool> &squashed_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -070073
Zach Reiznerfd6dc332015-10-13 21:12:48 -070074 void Dump(std::ostringstream *out) const;
75
Zach Reizner92f8e632015-10-12 17:47:13 -070076 private:
77 size_t generation_number_ = 0;
78 unsigned valid_history_ = 0;
79 std::vector<buffer_handle_t> last_handles_;
80
81 std::vector<Region> regions_;
82};
83
Sean Paul98e73c82015-06-24 14:38:49 -070084class DrmDisplayCompositor {
85 public:
86 DrmDisplayCompositor();
87 ~DrmDisplayCompositor();
88
89 int Init(DrmResources *drm, int display);
90
Zach Reizner92f8e632015-10-12 17:47:13 -070091 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
Sean Paul98e73c82015-06-24 14:38:49 -070092 int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition);
93 int Composite();
Zach Reiznerbff33ac2015-11-16 11:08:46 -080094 int SquashAll();
Sean Paul98e73c82015-06-24 14:38:49 -070095 void Dump(std::ostringstream *out) const;
96
Zach Reizner92f8e632015-10-12 17:47:13 -070097 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
98
Sean Paul98e73c82015-06-24 14:38:49 -070099 bool HaveQueuedComposites() const;
100
Zach Reizner92f8e632015-10-12 17:47:13 -0700101 SquashState *squash_state() {
Zach Reizner5757e822015-10-16 19:06:31 -0700102 return &squash_state_;
Zach Reizner92f8e632015-10-12 17:47:13 -0700103 }
104
Sean Paul98e73c82015-06-24 14:38:49 -0700105 private:
Haixia Shidda2fab2015-10-22 18:12:49 -0700106 struct FrameState {
107 std::unique_ptr<DrmDisplayComposition> composition;
108 int status = 0;
109 };
110
111 class FrameWorker : public Worker {
112 public:
113 FrameWorker(DrmDisplayCompositor *compositor);
Haixia Shi479412c2015-10-27 10:40:48 -0700114 ~FrameWorker() override;
Haixia Shidda2fab2015-10-22 18:12:49 -0700115
116 int Init();
117 void QueueFrame(std::unique_ptr<DrmDisplayComposition> composition,
118 int status);
119
120 protected:
Haixia Shi479412c2015-10-27 10:40:48 -0700121 void Routine() override;
Haixia Shidda2fab2015-10-22 18:12:49 -0700122
123 private:
124 DrmDisplayCompositor *compositor_;
125 std::queue<FrameState> frame_queue_;
126 };
127
Sean Paul35301f42015-11-17 14:46:56 -0500128 struct ModeState {
129 bool needs_modeset = false;
130 DrmMode mode;
131 uint32_t blob_id = 0;
132 uint32_t old_blob_id = 0;
133 };
134
Sean Paul98e73c82015-06-24 14:38:49 -0700135 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
136
Sean Paul971be152015-10-13 15:44:45 -0400137 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
138 // kAcquireWaitTries times, logging a warning in between.
139 static const int kAcquireWaitTries = 5;
140 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -0400141
Zach Reizner92f8e632015-10-12 17:47:13 -0700142 int PrepareFramebuffer(DrmFramebuffer &fb,
143 DrmDisplayComposition *display_comp);
Zach Reizner5757e822015-10-16 19:06:31 -0700144 int ApplySquash(DrmDisplayComposition *display_comp);
Zach Reizner713a6782015-07-31 15:12:44 -0700145 int ApplyPreComposite(DrmDisplayComposition *display_comp);
Haixia Shidda2fab2015-10-22 18:12:49 -0700146 int PrepareFrame(DrmDisplayComposition *display_comp);
Sean Paulc07b2112015-11-17 16:38:10 -0500147 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauld51c7612015-11-18 14:12:51 -0500148 int SquashFrame(DrmDisplayComposition *src, DrmDisplayComposition *dst);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700149 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400150 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -0700151
Haixia Shidda2fab2015-10-22 18:12:49 -0700152 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
153 int status);
154
Sean Paul35301f42015-11-17 14:46:56 -0500155 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
156
Sean Paul98e73c82015-06-24 14:38:49 -0700157 DrmResources *drm_;
158 int display_;
159
160 DrmCompositorWorker worker_;
Haixia Shidda2fab2015-10-22 18:12:49 -0700161 FrameWorker frame_worker_;
Sean Paul98e73c82015-06-24 14:38:49 -0700162
163 std::queue<std::unique_ptr<DrmDisplayComposition>> composite_queue_;
164 std::unique_ptr<DrmDisplayComposition> active_composition_;
165
Sean Paul98e73c82015-06-24 14:38:49 -0700166 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -0700167 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -0500168 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -0700169
Sean Paul35301f42015-11-17 14:46:56 -0500170 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -0400171
Zach Reizner713a6782015-07-31 15:12:44 -0700172 int framebuffer_index_;
173 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
174 std::unique_ptr<GLWorkerCompositor> pre_compositor_;
175
Zach Reizner5757e822015-10-16 19:06:31 -0700176 SquashState squash_state_;
177 int squash_framebuffer_index_;
178 DrmFramebuffer squash_framebuffers_[2];
179
Sean Paul98e73c82015-06-24 14:38:49 -0700180 // mutable since we need to acquire in HaveQueuedComposites
181 mutable pthread_mutex_t lock_;
182
183 // State tracking progress since our last Dump(). These are mutable since
184 // we need to reset them on every Dump() call.
185 mutable uint64_t dump_frames_composited_;
186 mutable uint64_t dump_last_timestamp_ns_;
187};
188}
189
190#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_