blob: 13760ef25ecaf28da8abebd228ae01a1b18255d3 [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#define LOG_TAG "hwc-drm-display-composition"
18
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmDisplayComposition.h"
Sean Paul98e73c82015-06-24 14:38:49 -070020
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030021#include <sync/sync.h>
22#include <xf86drmMode.h>
Sean Paul98e73c82015-06-24 14:38:49 -070023
Zach Reizner92f8e632015-10-12 17:47:13 -070024#include <algorithm>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020025#include <cstdlib>
Zach Reizner92f8e632015-10-12 17:47:13 -070026#include <unordered_set>
27
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "DrmDisplayCompositor.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030029#include "Planner.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "drm/DrmDevice.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020031#include "utils/log.h"
Sean Paul98e73c82015-06-24 14:38:49 -070032
33namespace android {
34
Matvii Zorin704ea0e2021-01-08 12:55:45 +020035DrmDisplayComposition::DrmDisplayComposition(DrmCrtc *crtc, Planner *planner)
36 : crtc_(crtc), // Can be NULL if we haven't modeset yet
37 planner_(planner) {
Sean Paul98e73c82015-06-24 14:38:49 -070038}
39
Sean Paulacb2a442015-06-24 18:43:01 -070040bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
41 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
42}
43
Zach Reizner5757e822015-10-16 19:06:31 -070044int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
45 bool geometry_changed) {
Zach Reizner92f8e632015-10-12 17:47:13 -070046 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
47 return -EINVAL;
48
Zach Reizner5757e822015-10-16 19:06:31 -070049 geometry_changed_ = geometry_changed;
50
Zach Reizner92f8e632015-10-12 17:47:13 -070051 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
52 layers_.emplace_back(std::move(layers[layer_index]));
53 }
54
55 type_ = DRM_COMPOSITION_TYPE_FRAME;
56 return 0;
57}
58
59int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
60 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
61 return -EINVAL;
62 dpms_mode_ = dpms_mode;
63 type_ = DRM_COMPOSITION_TYPE_DPMS;
64 return 0;
65}
66
67int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020068 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET)) {
69 ALOGE("SetDisplayMode() Failed to validate composition type");
Zach Reizner92f8e632015-10-12 17:47:13 -070070 return -EINVAL;
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020071 }
Zach Reizner92f8e632015-10-12 17:47:13 -070072 display_mode_ = display_mode;
73 dpms_mode_ = DRM_MODE_DPMS_ON;
74 type_ = DRM_COMPOSITION_TYPE_MODESET;
75 return 0;
76}
77
78int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Matvii Zorinf0757c22021-01-18 15:54:22 +020079 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane);
Zach Reizner92f8e632015-10-12 17:47:13 -070080 return 0;
81}
82
Sean Paul4f4ef692016-05-03 16:40:59 -070083int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
84 composition_planes_.emplace_back(std::move(plane));
85 return 0;
86}
87
Rob Herringaf0d9752018-05-04 16:34:19 -050088int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -070089 std::vector<DrmPlane *> *overlay_planes) {
90 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
91 return 0;
92
Sean Paulaa18d912016-05-12 14:28:05 -040093 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -070094
Rob Herringaf0d9752018-05-04 16:34:19 -050095 for (size_t i = 0; i < layers_.size(); ++i)
96 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -070097
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020098 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -040099 std::tie(ret,
100 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
101 primary_planes,
102 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400103 if (ret) {
104 ALOGE("Planner failed provisioning planes ret=%d", ret);
105 return ret;
106 }
107
108 // Remove the planes we used from the pool before returning. This ensures they
109 // won't be reused by another display in the composition.
110 for (auto &i : composition_planes_) {
111 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700112 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700113
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700114 // make sure that source layers are ordered based on zorder
115 std::sort(i.source_layers().begin(), i.source_layers().end());
116
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200117 std::vector<DrmPlane *> *container = nullptr;
Sean Paulaa18d912016-05-12 14:28:05 -0400118 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
119 container = primary_planes;
120 else
121 container = overlay_planes;
122 for (auto j = container->begin(); j != container->end(); ++j) {
123 if (*j == i.plane()) {
124 container->erase(j);
125 break;
126 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700127 }
128 }
Zach Reizner5757e822015-10-16 19:06:31 -0700129
Rob Herringaf0d9752018-05-04 16:34:19 -0500130 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700131}
132
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700133static const char *DrmCompositionTypeToString(DrmCompositionType type) {
134 switch (type) {
135 case DRM_COMPOSITION_TYPE_EMPTY:
136 return "EMPTY";
137 case DRM_COMPOSITION_TYPE_FRAME:
138 return "FRAME";
139 case DRM_COMPOSITION_TYPE_DPMS:
140 return "DPMS";
141 case DRM_COMPOSITION_TYPE_MODESET:
142 return "MODESET";
143 default:
144 return "<invalid>";
145 }
146}
147
148static const char *DPMSModeToString(int dpms_mode) {
149 switch (dpms_mode) {
150 case DRM_MODE_DPMS_ON:
151 return "ON";
152 case DRM_MODE_DPMS_OFF:
153 return "OFF";
154 default:
155 return "<invalid>";
156 }
157}
158
Roman Stratiienko51287152021-02-10 14:59:52 +0200159static void DumpBuffer(const DrmHwcLayer &layer, std::ostringstream *out) {
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700160 *out << "buffer[w/h/format]=";
Roman Stratiienko51287152021-02-10 14:59:52 +0200161 *out << layer.buffer_info.width << "/" << layer.buffer_info.height << "/"
162 << layer.buffer_info.format;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700163}
164
Sean Paul04b47ea2015-11-19 21:46:11 -0500165static void DumpTransform(uint32_t transform, std::ostringstream *out) {
166 *out << "[";
167
168 if (transform == 0)
169 *out << "IDENTITY";
170
171 bool separator = false;
172 if (transform & DrmHwcTransform::kFlipH) {
173 *out << "FLIPH";
174 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700175 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500176 if (transform & DrmHwcTransform::kFlipV) {
177 if (separator)
178 *out << "|";
179 *out << "FLIPV";
180 separator = true;
181 }
182 if (transform & DrmHwcTransform::kRotate90) {
183 if (separator)
184 *out << "|";
185 *out << "ROTATE90";
186 separator = true;
187 }
188 if (transform & DrmHwcTransform::kRotate180) {
189 if (separator)
190 *out << "|";
191 *out << "ROTATE180";
192 separator = true;
193 }
194 if (transform & DrmHwcTransform::kRotate270) {
195 if (separator)
196 *out << "|";
197 *out << "ROTATE270";
198 separator = true;
199 }
200
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200201 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipV |
Sean Paul04b47ea2015-11-19 21:46:11 -0500202 DrmHwcTransform::kRotate90 |
203 DrmHwcTransform::kRotate180 |
204 DrmHwcTransform::kRotate270;
205 if (transform & ~valid_bits) {
206 if (separator)
207 *out << "|";
208 *out << "INVALID";
209 }
210 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700211}
212
213static const char *BlendingToString(DrmHwcBlending blending) {
214 switch (blending) {
215 case DrmHwcBlending::kNone:
216 return "NONE";
217 case DrmHwcBlending::kPreMult:
218 return "PREMULT";
219 case DrmHwcBlending::kCoverage:
220 return "COVERAGE";
221 default:
222 return "<invalid>";
223 }
224}
225
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700226void DrmDisplayComposition::Dump(std::ostringstream *out) const {
227 *out << "----DrmDisplayComposition"
228 << " crtc=" << (crtc_ ? crtc_->id() : -1)
229 << " type=" << DrmCompositionTypeToString(type_);
230
231 switch (type_) {
232 case DRM_COMPOSITION_TYPE_DPMS:
233 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
234 break;
235 case DRM_COMPOSITION_TYPE_MODESET:
236 *out << " display_mode=" << display_mode_.h_display() << "x"
237 << display_mode_.v_display();
238 break;
239 default:
240 break;
241 }
242
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700243 *out << " Layers: count=" << layers_.size() << "\n";
244 for (size_t i = 0; i < layers_.size(); i++) {
245 const DrmHwcLayer &layer = layers_[i];
246 *out << " [" << i << "] ";
247
Roman Stratiienko51287152021-02-10 14:59:52 +0200248 DumpBuffer(layer, out);
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700249
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700250 if (layer.protected_usage())
251 *out << " protected";
252
Sean Paul04b47ea2015-11-19 21:46:11 -0500253 *out << " transform=";
254 DumpTransform(layer.transform, out);
255 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500256 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700257 }
258
259 *out << " Planes: count=" << composition_planes_.size() << "\n";
260 for (size_t i = 0; i < composition_planes_.size(); i++) {
261 const DrmCompositionPlane &comp_plane = composition_planes_[i];
262 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400263 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400264 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400265 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400266 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400267 *out << "DISABLE";
268 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400269 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400270 *out << "LAYER";
271 break;
Sean Paul39b37842016-05-11 13:50:28 -0400272 default:
273 *out << "<invalid>";
274 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700275 }
276
Sean Paulca699be2016-05-11 16:29:45 -0400277 *out << " source_layer=";
278 for (auto i : comp_plane.source_layers()) {
279 *out << i << " ";
280 }
281 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700282 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700283}
Sean Paulf72cccd2018-08-27 13:59:08 -0400284} // namespace android