blob: 9183925787539f6173af8db6e7f8c3118a5cd487 [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_COMPOSITION_H_
18#define ANDROID_DRM_DISPLAY_COMPOSITION_H_
19
Zach Reizner09807052015-08-13 14:53:41 -070020#include "drmcrtc.h"
Haixia Shi6afbb6a2015-11-24 12:42:45 -080021#include "drmhwcomposer.h"
Sean Paul98e73c82015-06-24 14:38:49 -070022#include "drmplane.h"
Zach Reizner09807052015-08-13 14:53:41 -070023#include "glworker.h"
Sean Paul98e73c82015-06-24 14:38:49 -070024
Zach Reiznerfd6dc332015-10-13 21:12:48 -070025#include <sstream>
Sean Paul98e73c82015-06-24 14:38:49 -070026#include <vector>
27
Zach Reiznerb44fd102015-08-07 16:00:01 -070028#include <hardware/gralloc.h>
Sean Paul98e73c82015-06-24 14:38:49 -070029#include <hardware/hardware.h>
30#include <hardware/hwcomposer.h>
31
32namespace android {
33
Sean Paul4c4646e2016-05-10 04:19:24 -040034class Importer;
Sean Paulaa18d912016-05-12 14:28:05 -040035class Planner;
Sean Paul5325e102016-03-29 13:55:35 -040036class SquashState;
Zach Reizner92f8e632015-10-12 17:47:13 -070037
Sean Paulacb2a442015-06-24 18:43:01 -070038enum DrmCompositionType {
39 DRM_COMPOSITION_TYPE_EMPTY,
40 DRM_COMPOSITION_TYPE_FRAME,
Sean Pauldb7a17d2015-06-24 18:46:05 -070041 DRM_COMPOSITION_TYPE_DPMS,
Sean Paul57355412015-09-19 09:14:34 -040042 DRM_COMPOSITION_TYPE_MODESET,
Sean Paulacb2a442015-06-24 18:43:01 -070043};
44
Sean Pauled45a8e2017-02-28 13:17:34 -050045struct DrmCompositionDisplayLayersMap {
46 int display;
47 bool geometry_changed = true;
48 std::vector<DrmHwcLayer> layers;
49
50 DrmCompositionDisplayLayersMap() = default;
51 DrmCompositionDisplayLayersMap(DrmCompositionDisplayLayersMap &&rhs) =
52 default;
53};
54
Zach Reizner92f8e632015-10-12 17:47:13 -070055struct DrmCompositionRegion {
56 DrmHwcRect<int> frame;
57 std::vector<size_t> source_layers;
58};
Sean Paul98e73c82015-06-24 14:38:49 -070059
Sean Paulca699be2016-05-11 16:29:45 -040060class DrmCompositionPlane {
61 public:
Sean Paulbbe39db2016-05-11 16:57:26 -040062 enum class Type : int32_t {
63 kDisable,
64 kLayer,
65 kPrecomp,
66 kSquash,
67 };
68
Sean Paulca699be2016-05-11 16:29:45 -040069 DrmCompositionPlane() = default;
70 DrmCompositionPlane(DrmCompositionPlane &&rhs) = default;
71 DrmCompositionPlane &operator=(DrmCompositionPlane &&other) = default;
Sean Paulbbe39db2016-05-11 16:57:26 -040072 DrmCompositionPlane(Type type, DrmPlane *plane, DrmCrtc *crtc)
Sean Paulca699be2016-05-11 16:29:45 -040073 : type_(type), plane_(plane), crtc_(crtc) {
74 }
Sean Paulbbe39db2016-05-11 16:57:26 -040075 DrmCompositionPlane(Type type, DrmPlane *plane, DrmCrtc *crtc,
76 size_t source_layer)
Sean Paulca699be2016-05-11 16:29:45 -040077 : type_(type),
78 plane_(plane),
79 crtc_(crtc),
80 source_layers_(1, source_layer) {
81 }
82
Sean Paulbbe39db2016-05-11 16:57:26 -040083 Type type() const {
Sean Paulca699be2016-05-11 16:29:45 -040084 return type_;
85 }
86
87 DrmPlane *plane() const {
88 return plane_;
89 }
90 void set_plane(DrmPlane *plane) {
91 plane_ = plane;
92 }
93
94 DrmCrtc *crtc() const {
95 return crtc_;
96 }
97
98 std::vector<size_t> &source_layers() {
99 return source_layers_;
100 }
101
102 const std::vector<size_t> &source_layers() const {
103 return source_layers_;
104 }
105
106 private:
Sean Paulbbe39db2016-05-11 16:57:26 -0400107 Type type_ = Type::kDisable;
Sean Paulca699be2016-05-11 16:29:45 -0400108 DrmPlane *plane_ = NULL;
109 DrmCrtc *crtc_ = NULL;
110 std::vector<size_t> source_layers_;
Zach Reizner4a253652015-09-10 18:30:54 -0700111};
Sean Paul98e73c82015-06-24 14:38:49 -0700112
113class DrmDisplayComposition {
114 public:
Zach Reizner92f8e632015-10-12 17:47:13 -0700115 DrmDisplayComposition() = default;
116 DrmDisplayComposition(const DrmDisplayComposition &) = delete;
Sean Paul98e73c82015-06-24 14:38:49 -0700117 ~DrmDisplayComposition();
118
Sean Paulbdc67bf2015-09-21 10:04:02 -0400119 int Init(DrmResources *drm, DrmCrtc *crtc, Importer *importer,
Sean Paulaa18d912016-05-12 14:28:05 -0400120 Planner *planner, uint64_t frame_no);
Sean Paul98e73c82015-06-24 14:38:49 -0700121
Zach Reizner5757e822015-10-16 19:06:31 -0700122 int SetLayers(DrmHwcLayer *layers, size_t num_layers, bool geometry_changed);
Sean Paul4f4ef692016-05-03 16:40:59 -0700123 int AddPlaneComposition(DrmCompositionPlane plane);
Sean Paul2e46fbd2015-07-09 17:22:22 -0400124 int AddPlaneDisable(DrmPlane *plane);
Zach Reizner09807052015-08-13 14:53:41 -0700125 int SetDpmsMode(uint32_t dpms_mode);
Sean Paul57355412015-09-19 09:14:34 -0400126 int SetDisplayMode(const DrmMode &display_mode);
Sean Paul98e73c82015-06-24 14:38:49 -0700127
Zach Reizner92f8e632015-10-12 17:47:13 -0700128 int Plan(SquashState *squash, std::vector<DrmPlane *> *primary_planes,
129 std::vector<DrmPlane *> *overlay_planes);
Sean Paulacb2a442015-06-24 18:43:01 -0700130
Sean Paul4f4ef692016-05-03 16:40:59 -0700131 int FinalizeComposition();
132
Zach Reizner09807052015-08-13 14:53:41 -0700133 int CreateNextTimelineFence();
Zach Reizner92f8e632015-10-12 17:47:13 -0700134 int SignalSquashDone() {
135 return IncreaseTimelineToPoint(timeline_squash_done_);
136 }
137 int SignalPreCompDone() {
138 return IncreaseTimelineToPoint(timeline_pre_comp_done_);
139 }
140 int SignalCompositionDone() {
141 return IncreaseTimelineToPoint(timeline_);
142 }
143
144 std::vector<DrmHwcLayer> &layers() {
145 return layers_;
146 }
147
148 std::vector<DrmCompositionRegion> &squash_regions() {
149 return squash_regions_;
150 }
151
152 std::vector<DrmCompositionRegion> &pre_comp_regions() {
153 return pre_comp_regions_;
154 }
155
156 std::vector<DrmCompositionPlane> &composition_planes() {
157 return composition_planes_;
158 }
159
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800160 bool geometry_changed() const {
161 return geometry_changed_;
162 }
163
Zach Reizner92f8e632015-10-12 17:47:13 -0700164 uint64_t frame_no() const {
165 return frame_no_;
166 }
167
168 DrmCompositionType type() const {
169 return type_;
170 }
171
172 uint32_t dpms_mode() const {
173 return dpms_mode_;
174 }
175
176 const DrmMode &display_mode() const {
177 return display_mode_;
178 }
179
180 DrmCrtc *crtc() const {
181 return crtc_;
182 }
183
184 Importer *importer() const {
185 return importer_;
186 }
187
Sean Paulaa18d912016-05-12 14:28:05 -0400188 Planner *planner() const {
189 return planner_;
190 }
191
Robert Fossa1ade4e2017-09-27 19:28:15 +0200192 int take_out_fence() {
193 return out_fence_.Release();
194 }
195
196 void set_out_fence(int out_fence) {
197 out_fence_.Set(out_fence);
198 }
199
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700200 void Dump(std::ostringstream *out) const;
201
Zach Reizner92f8e632015-10-12 17:47:13 -0700202 private:
203 bool validate_composition_type(DrmCompositionType desired);
204
Zach Reizner09807052015-08-13 14:53:41 -0700205 int IncreaseTimelineToPoint(int point);
206
Sean Paulaa18d912016-05-12 14:28:05 -0400207 int FinalizeComposition(DrmHwcRect<int> *exclude_rects,
208 size_t num_exclude_rects);
209 void SeparateLayers(DrmHwcRect<int> *exclude_rects, size_t num_exclude_rects);
Zach Reizner5757e822015-10-16 19:06:31 -0700210 int CreateAndAssignReleaseFences();
211
Zach Reizner92f8e632015-10-12 17:47:13 -0700212 DrmResources *drm_ = NULL;
213 DrmCrtc *crtc_ = NULL;
214 Importer *importer_ = NULL;
Sean Paulaa18d912016-05-12 14:28:05 -0400215 Planner *planner_ = NULL;
Sean Paul98e73c82015-06-24 14:38:49 -0700216
Zach Reizner92f8e632015-10-12 17:47:13 -0700217 DrmCompositionType type_ = DRM_COMPOSITION_TYPE_EMPTY;
218 uint32_t dpms_mode_ = DRM_MODE_DPMS_ON;
Sean Paul57355412015-09-19 09:14:34 -0400219 DrmMode display_mode_;
Sean Paulbdc67bf2015-09-21 10:04:02 -0400220
Zach Reizner92f8e632015-10-12 17:47:13 -0700221 int timeline_fd_ = -1;
222 int timeline_ = 0;
223 int timeline_current_ = 0;
224 int timeline_squash_done_ = 0;
225 int timeline_pre_comp_done_ = 0;
Robert Fossa1ade4e2017-09-27 19:28:15 +0200226 UniqueFd out_fence_ = -1;
Zach Reizner92f8e632015-10-12 17:47:13 -0700227
Zach Reizner5757e822015-10-16 19:06:31 -0700228 bool geometry_changed_;
Zach Reizner92f8e632015-10-12 17:47:13 -0700229 std::vector<DrmHwcLayer> layers_;
230 std::vector<DrmCompositionRegion> squash_regions_;
231 std::vector<DrmCompositionRegion> pre_comp_regions_;
232 std::vector<DrmCompositionPlane> composition_planes_;
233
234 uint64_t frame_no_ = 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700235};
236}
237
238#endif // ANDROID_DRM_DISPLAY_COMPOSITION_H_