blob: 0d1278e72b96b9124840508edc7cb40493129303 [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) {
Sean Paulbbe39db2016-05-11 16:57:26 -040079 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
Sean Paulca699be2016-05-11 16:29:45 -040080 crtc_);
Zach Reizner92f8e632015-10-12 17:47:13 -070081 return 0;
82}
83
Sean Paul4f4ef692016-05-03 16:40:59 -070084int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
85 composition_planes_.emplace_back(std::move(plane));
86 return 0;
87}
88
Rob Herringaf0d9752018-05-04 16:34:19 -050089int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -070090 std::vector<DrmPlane *> *overlay_planes) {
91 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
92 return 0;
93
Sean Paulaa18d912016-05-12 14:28:05 -040094 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -070095
Rob Herringaf0d9752018-05-04 16:34:19 -050096 for (size_t i = 0; i < layers_.size(); ++i)
97 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -070098
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020099 int ret = 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400100 std::tie(ret,
101 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
102 primary_planes,
103 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400104 if (ret) {
105 ALOGE("Planner failed provisioning planes ret=%d", ret);
106 return ret;
107 }
108
109 // Remove the planes we used from the pool before returning. This ensures they
110 // won't be reused by another display in the composition.
111 for (auto &i : composition_planes_) {
112 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700113 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700114
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700115 // make sure that source layers are ordered based on zorder
116 std::sort(i.source_layers().begin(), i.source_layers().end());
117
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200118 std::vector<DrmPlane *> *container = nullptr;
Sean Paulaa18d912016-05-12 14:28:05 -0400119 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
120 container = primary_planes;
121 else
122 container = overlay_planes;
123 for (auto j = container->begin(); j != container->end(); ++j) {
124 if (*j == i.plane()) {
125 container->erase(j);
126 break;
127 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700128 }
129 }
Zach Reizner5757e822015-10-16 19:06:31 -0700130
Rob Herringaf0d9752018-05-04 16:34:19 -0500131 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700132}
133
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700134static const char *DrmCompositionTypeToString(DrmCompositionType type) {
135 switch (type) {
136 case DRM_COMPOSITION_TYPE_EMPTY:
137 return "EMPTY";
138 case DRM_COMPOSITION_TYPE_FRAME:
139 return "FRAME";
140 case DRM_COMPOSITION_TYPE_DPMS:
141 return "DPMS";
142 case DRM_COMPOSITION_TYPE_MODESET:
143 return "MODESET";
144 default:
145 return "<invalid>";
146 }
147}
148
149static const char *DPMSModeToString(int dpms_mode) {
150 switch (dpms_mode) {
151 case DRM_MODE_DPMS_ON:
152 return "ON";
153 case DRM_MODE_DPMS_OFF:
154 return "OFF";
155 default:
156 return "<invalid>";
157 }
158}
159
160static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
161 if (!buffer) {
162 *out << "buffer=<invalid>";
163 return;
164 }
165
166 *out << "buffer[w/h/format]=";
167 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
168}
169
Sean Paul04b47ea2015-11-19 21:46:11 -0500170static void DumpTransform(uint32_t transform, std::ostringstream *out) {
171 *out << "[";
172
173 if (transform == 0)
174 *out << "IDENTITY";
175
176 bool separator = false;
177 if (transform & DrmHwcTransform::kFlipH) {
178 *out << "FLIPH";
179 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700180 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500181 if (transform & DrmHwcTransform::kFlipV) {
182 if (separator)
183 *out << "|";
184 *out << "FLIPV";
185 separator = true;
186 }
187 if (transform & DrmHwcTransform::kRotate90) {
188 if (separator)
189 *out << "|";
190 *out << "ROTATE90";
191 separator = true;
192 }
193 if (transform & DrmHwcTransform::kRotate180) {
194 if (separator)
195 *out << "|";
196 *out << "ROTATE180";
197 separator = true;
198 }
199 if (transform & DrmHwcTransform::kRotate270) {
200 if (separator)
201 *out << "|";
202 *out << "ROTATE270";
203 separator = true;
204 }
205
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200206 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipV |
Sean Paul04b47ea2015-11-19 21:46:11 -0500207 DrmHwcTransform::kRotate90 |
208 DrmHwcTransform::kRotate180 |
209 DrmHwcTransform::kRotate270;
210 if (transform & ~valid_bits) {
211 if (separator)
212 *out << "|";
213 *out << "INVALID";
214 }
215 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700216}
217
218static const char *BlendingToString(DrmHwcBlending blending) {
219 switch (blending) {
220 case DrmHwcBlending::kNone:
221 return "NONE";
222 case DrmHwcBlending::kPreMult:
223 return "PREMULT";
224 case DrmHwcBlending::kCoverage:
225 return "COVERAGE";
226 default:
227 return "<invalid>";
228 }
229}
230
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700231void DrmDisplayComposition::Dump(std::ostringstream *out) const {
232 *out << "----DrmDisplayComposition"
233 << " crtc=" << (crtc_ ? crtc_->id() : -1)
234 << " type=" << DrmCompositionTypeToString(type_);
235
236 switch (type_) {
237 case DRM_COMPOSITION_TYPE_DPMS:
238 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
239 break;
240 case DRM_COMPOSITION_TYPE_MODESET:
241 *out << " display_mode=" << display_mode_.h_display() << "x"
242 << display_mode_.v_display();
243 break;
244 default:
245 break;
246 }
247
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700248 *out << " Layers: count=" << layers_.size() << "\n";
249 for (size_t i = 0; i < layers_.size(); i++) {
250 const DrmHwcLayer &layer = layers_[i];
251 *out << " [" << i << "] ";
252
253 DumpBuffer(layer.buffer, out);
254
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700255 if (layer.protected_usage())
256 *out << " protected";
257
Sean Paul04b47ea2015-11-19 21:46:11 -0500258 *out << " transform=";
259 DumpTransform(layer.transform, out);
260 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500261 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700262 }
263
264 *out << " Planes: count=" << composition_planes_.size() << "\n";
265 for (size_t i = 0; i < composition_planes_.size(); i++) {
266 const DrmCompositionPlane &comp_plane = composition_planes_[i];
267 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400268 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400269 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400270 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400271 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400272 *out << "DISABLE";
273 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400274 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400275 *out << "LAYER";
276 break;
Sean Paul39b37842016-05-11 13:50:28 -0400277 default:
278 *out << "<invalid>";
279 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700280 }
281
Sean Paulca699be2016-05-11 16:29:45 -0400282 *out << " source_layer=";
283 for (auto i : comp_plane.source_layers()) {
284 *out << i << " ";
285 }
286 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700287 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700288}
Sean Paulf72cccd2018-08-27 13:59:08 -0400289} // namespace android