blob: ef151a17f06e0af9df3758bc812a8f5e6843b95d [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 <log/log.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030022#include <sync/sync.h>
23#include <xf86drmMode.h>
Sean Paul98e73c82015-06-24 14:38:49 -070024
Zach Reizner92f8e632015-10-12 17:47:13 -070025#include <algorithm>
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <cstdlib>
Zach Reizner92f8e632015-10-12 17:47:13 -070027#include <unordered_set>
28
Roman Stratiienko13cc3662020-08-29 21:35:39 +030029#include "DrmDisplayCompositor.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030030#include "Planner.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030031#include "drm/DrmDevice.h"
Sean Paul98e73c82015-06-24 14:38:49 -070032
33namespace android {
34
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010035int DrmDisplayComposition::Init(DrmDevice *drm, DrmCrtc *crtc,
Sean Paulaa18d912016-05-12 14:28:05 -040036 Importer *importer, Planner *planner,
37 uint64_t frame_no) {
Sean Paul98e73c82015-06-24 14:38:49 -070038 drm_ = drm;
Zach Reizner4a253652015-09-10 18:30:54 -070039 crtc_ = crtc; // Can be NULL if we haven't modeset yet
Sean Paul98e73c82015-06-24 14:38:49 -070040 importer_ = importer;
Sean Paulaa18d912016-05-12 14:28:05 -040041 planner_ = planner;
Sean Paulbdc67bf2015-09-21 10:04:02 -040042 frame_no_ = frame_no;
Sean Paul98e73c82015-06-24 14:38:49 -070043
Sean Paul98e73c82015-06-24 14:38:49 -070044 return 0;
45}
46
Sean Paulacb2a442015-06-24 18:43:01 -070047bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
48 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
49}
50
Zach Reizner5757e822015-10-16 19:06:31 -070051int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
52 bool geometry_changed) {
Zach Reizner92f8e632015-10-12 17:47:13 -070053 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
54 return -EINVAL;
55
Zach Reizner5757e822015-10-16 19:06:31 -070056 geometry_changed_ = geometry_changed;
57
Zach Reizner92f8e632015-10-12 17:47:13 -070058 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
59 layers_.emplace_back(std::move(layers[layer_index]));
60 }
61
62 type_ = DRM_COMPOSITION_TYPE_FRAME;
63 return 0;
64}
65
66int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
67 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
68 return -EINVAL;
69 dpms_mode_ = dpms_mode;
70 type_ = DRM_COMPOSITION_TYPE_DPMS;
71 return 0;
72}
73
74int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020075 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET)) {
76 ALOGE("SetDisplayMode() Failed to validate composition type");
Zach Reizner92f8e632015-10-12 17:47:13 -070077 return -EINVAL;
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +020078 }
Zach Reizner92f8e632015-10-12 17:47:13 -070079 display_mode_ = display_mode;
80 dpms_mode_ = DRM_MODE_DPMS_ON;
81 type_ = DRM_COMPOSITION_TYPE_MODESET;
82 return 0;
83}
84
85int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Sean Paulbbe39db2016-05-11 16:57:26 -040086 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
Sean Paulca699be2016-05-11 16:29:45 -040087 crtc_);
Zach Reizner92f8e632015-10-12 17:47:13 -070088 return 0;
89}
90
Sean Paul4f4ef692016-05-03 16:40:59 -070091int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
92 composition_planes_.emplace_back(std::move(plane));
93 return 0;
94}
95
Rob Herringaf0d9752018-05-04 16:34:19 -050096int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -070097 std::vector<DrmPlane *> *overlay_planes) {
98 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
99 return 0;
100
Sean Paulaa18d912016-05-12 14:28:05 -0400101 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -0700102
Rob Herringaf0d9752018-05-04 16:34:19 -0500103 for (size_t i = 0; i < layers_.size(); ++i)
104 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -0700105
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200106 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400107 std::tie(ret,
108 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
109 primary_planes,
110 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400111 if (ret) {
112 ALOGE("Planner failed provisioning planes ret=%d", ret);
113 return ret;
114 }
115
116 // Remove the planes we used from the pool before returning. This ensures they
117 // won't be reused by another display in the composition.
118 for (auto &i : composition_planes_) {
119 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700120 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700121
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700122 // make sure that source layers are ordered based on zorder
123 std::sort(i.source_layers().begin(), i.source_layers().end());
124
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200125 std::vector<DrmPlane *> *container = nullptr;
Sean Paulaa18d912016-05-12 14:28:05 -0400126 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
127 container = primary_planes;
128 else
129 container = overlay_planes;
130 for (auto j = container->begin(); j != container->end(); ++j) {
131 if (*j == i.plane()) {
132 container->erase(j);
133 break;
134 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700135 }
136 }
Zach Reizner5757e822015-10-16 19:06:31 -0700137
Rob Herringaf0d9752018-05-04 16:34:19 -0500138 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700139}
140
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700141static const char *DrmCompositionTypeToString(DrmCompositionType type) {
142 switch (type) {
143 case DRM_COMPOSITION_TYPE_EMPTY:
144 return "EMPTY";
145 case DRM_COMPOSITION_TYPE_FRAME:
146 return "FRAME";
147 case DRM_COMPOSITION_TYPE_DPMS:
148 return "DPMS";
149 case DRM_COMPOSITION_TYPE_MODESET:
150 return "MODESET";
151 default:
152 return "<invalid>";
153 }
154}
155
156static const char *DPMSModeToString(int dpms_mode) {
157 switch (dpms_mode) {
158 case DRM_MODE_DPMS_ON:
159 return "ON";
160 case DRM_MODE_DPMS_OFF:
161 return "OFF";
162 default:
163 return "<invalid>";
164 }
165}
166
167static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
168 if (!buffer) {
169 *out << "buffer=<invalid>";
170 return;
171 }
172
173 *out << "buffer[w/h/format]=";
174 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
175}
176
Sean Paul04b47ea2015-11-19 21:46:11 -0500177static void DumpTransform(uint32_t transform, std::ostringstream *out) {
178 *out << "[";
179
180 if (transform == 0)
181 *out << "IDENTITY";
182
183 bool separator = false;
184 if (transform & DrmHwcTransform::kFlipH) {
185 *out << "FLIPH";
186 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700187 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500188 if (transform & DrmHwcTransform::kFlipV) {
189 if (separator)
190 *out << "|";
191 *out << "FLIPV";
192 separator = true;
193 }
194 if (transform & DrmHwcTransform::kRotate90) {
195 if (separator)
196 *out << "|";
197 *out << "ROTATE90";
198 separator = true;
199 }
200 if (transform & DrmHwcTransform::kRotate180) {
201 if (separator)
202 *out << "|";
203 *out << "ROTATE180";
204 separator = true;
205 }
206 if (transform & DrmHwcTransform::kRotate270) {
207 if (separator)
208 *out << "|";
209 *out << "ROTATE270";
210 separator = true;
211 }
212
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200213 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipV |
Sean Paul04b47ea2015-11-19 21:46:11 -0500214 DrmHwcTransform::kRotate90 |
215 DrmHwcTransform::kRotate180 |
216 DrmHwcTransform::kRotate270;
217 if (transform & ~valid_bits) {
218 if (separator)
219 *out << "|";
220 *out << "INVALID";
221 }
222 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700223}
224
225static const char *BlendingToString(DrmHwcBlending blending) {
226 switch (blending) {
227 case DrmHwcBlending::kNone:
228 return "NONE";
229 case DrmHwcBlending::kPreMult:
230 return "PREMULT";
231 case DrmHwcBlending::kCoverage:
232 return "COVERAGE";
233 default:
234 return "<invalid>";
235 }
236}
237
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700238void DrmDisplayComposition::Dump(std::ostringstream *out) const {
239 *out << "----DrmDisplayComposition"
240 << " crtc=" << (crtc_ ? crtc_->id() : -1)
241 << " type=" << DrmCompositionTypeToString(type_);
242
243 switch (type_) {
244 case DRM_COMPOSITION_TYPE_DPMS:
245 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
246 break;
247 case DRM_COMPOSITION_TYPE_MODESET:
248 *out << " display_mode=" << display_mode_.h_display() << "x"
249 << display_mode_.v_display();
250 break;
251 default:
252 break;
253 }
254
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700255 *out << " Layers: count=" << layers_.size() << "\n";
256 for (size_t i = 0; i < layers_.size(); i++) {
257 const DrmHwcLayer &layer = layers_[i];
258 *out << " [" << i << "] ";
259
260 DumpBuffer(layer.buffer, out);
261
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700262 if (layer.protected_usage())
263 *out << " protected";
264
Sean Paul04b47ea2015-11-19 21:46:11 -0500265 *out << " transform=";
266 DumpTransform(layer.transform, out);
267 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500268 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700269 }
270
271 *out << " Planes: count=" << composition_planes_.size() << "\n";
272 for (size_t i = 0; i < composition_planes_.size(); i++) {
273 const DrmCompositionPlane &comp_plane = composition_planes_[i];
274 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400275 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400276 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400277 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400278 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400279 *out << "DISABLE";
280 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400281 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400282 *out << "LAYER";
283 break;
Sean Paul39b37842016-05-11 13:50:28 -0400284 default:
285 *out << "<invalid>";
286 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700287 }
288
Sean Paulca699be2016-05-11 16:29:45 -0400289 *out << " source_layer=";
290 for (auto i : comp_plane.source_layers()) {
291 *out << i << " ";
292 }
293 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700294 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700295}
Sean Paulf72cccd2018-08-27 13:59:08 -0400296} // namespace android