blob: 1e9e1273efb6b291435cd5fc4e090f05bb554698 [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) {
75 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
76 return -EINVAL;
77 display_mode_ = display_mode;
78 dpms_mode_ = DRM_MODE_DPMS_ON;
79 type_ = DRM_COMPOSITION_TYPE_MODESET;
80 return 0;
81}
82
83int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Sean Paulbbe39db2016-05-11 16:57:26 -040084 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
Sean Paulca699be2016-05-11 16:29:45 -040085 crtc_);
Zach Reizner92f8e632015-10-12 17:47:13 -070086 return 0;
87}
88
Sean Paul4f4ef692016-05-03 16:40:59 -070089int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
90 composition_planes_.emplace_back(std::move(plane));
91 return 0;
92}
93
Rob Herringaf0d9752018-05-04 16:34:19 -050094int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -070095 std::vector<DrmPlane *> *overlay_planes) {
96 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
97 return 0;
98
Sean Paulaa18d912016-05-12 14:28:05 -040099 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -0700100
Rob Herringaf0d9752018-05-04 16:34:19 -0500101 for (size_t i = 0; i < layers_.size(); ++i)
102 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -0700103
Sean Paulaa18d912016-05-12 14:28:05 -0400104 int ret;
Sean Paulf72cccd2018-08-27 13:59:08 -0400105 std::tie(ret,
106 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
107 primary_planes,
108 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400109 if (ret) {
110 ALOGE("Planner failed provisioning planes ret=%d", ret);
111 return ret;
112 }
113
114 // Remove the planes we used from the pool before returning. This ensures they
115 // won't be reused by another display in the composition.
116 for (auto &i : composition_planes_) {
117 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700118 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700119
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700120 // make sure that source layers are ordered based on zorder
121 std::sort(i.source_layers().begin(), i.source_layers().end());
122
Sean Paulaa18d912016-05-12 14:28:05 -0400123 std::vector<DrmPlane *> *container;
124 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
125 container = primary_planes;
126 else
127 container = overlay_planes;
128 for (auto j = container->begin(); j != container->end(); ++j) {
129 if (*j == i.plane()) {
130 container->erase(j);
131 break;
132 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700133 }
134 }
Zach Reizner5757e822015-10-16 19:06:31 -0700135
Rob Herringaf0d9752018-05-04 16:34:19 -0500136 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700137}
138
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700139static const char *DrmCompositionTypeToString(DrmCompositionType type) {
140 switch (type) {
141 case DRM_COMPOSITION_TYPE_EMPTY:
142 return "EMPTY";
143 case DRM_COMPOSITION_TYPE_FRAME:
144 return "FRAME";
145 case DRM_COMPOSITION_TYPE_DPMS:
146 return "DPMS";
147 case DRM_COMPOSITION_TYPE_MODESET:
148 return "MODESET";
149 default:
150 return "<invalid>";
151 }
152}
153
154static const char *DPMSModeToString(int dpms_mode) {
155 switch (dpms_mode) {
156 case DRM_MODE_DPMS_ON:
157 return "ON";
158 case DRM_MODE_DPMS_OFF:
159 return "OFF";
160 default:
161 return "<invalid>";
162 }
163}
164
165static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
166 if (!buffer) {
167 *out << "buffer=<invalid>";
168 return;
169 }
170
171 *out << "buffer[w/h/format]=";
172 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
173}
174
Sean Paul04b47ea2015-11-19 21:46:11 -0500175static void DumpTransform(uint32_t transform, std::ostringstream *out) {
176 *out << "[";
177
178 if (transform == 0)
179 *out << "IDENTITY";
180
181 bool separator = false;
182 if (transform & DrmHwcTransform::kFlipH) {
183 *out << "FLIPH";
184 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700185 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500186 if (transform & DrmHwcTransform::kFlipV) {
187 if (separator)
188 *out << "|";
189 *out << "FLIPV";
190 separator = true;
191 }
192 if (transform & DrmHwcTransform::kRotate90) {
193 if (separator)
194 *out << "|";
195 *out << "ROTATE90";
196 separator = true;
197 }
198 if (transform & DrmHwcTransform::kRotate180) {
199 if (separator)
200 *out << "|";
201 *out << "ROTATE180";
202 separator = true;
203 }
204 if (transform & DrmHwcTransform::kRotate270) {
205 if (separator)
206 *out << "|";
207 *out << "ROTATE270";
208 separator = true;
209 }
210
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200211 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipV |
Sean Paul04b47ea2015-11-19 21:46:11 -0500212 DrmHwcTransform::kRotate90 |
213 DrmHwcTransform::kRotate180 |
214 DrmHwcTransform::kRotate270;
215 if (transform & ~valid_bits) {
216 if (separator)
217 *out << "|";
218 *out << "INVALID";
219 }
220 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700221}
222
223static const char *BlendingToString(DrmHwcBlending blending) {
224 switch (blending) {
225 case DrmHwcBlending::kNone:
226 return "NONE";
227 case DrmHwcBlending::kPreMult:
228 return "PREMULT";
229 case DrmHwcBlending::kCoverage:
230 return "COVERAGE";
231 default:
232 return "<invalid>";
233 }
234}
235
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700236void DrmDisplayComposition::Dump(std::ostringstream *out) const {
237 *out << "----DrmDisplayComposition"
238 << " crtc=" << (crtc_ ? crtc_->id() : -1)
239 << " type=" << DrmCompositionTypeToString(type_);
240
241 switch (type_) {
242 case DRM_COMPOSITION_TYPE_DPMS:
243 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
244 break;
245 case DRM_COMPOSITION_TYPE_MODESET:
246 *out << " display_mode=" << display_mode_.h_display() << "x"
247 << display_mode_.v_display();
248 break;
249 default:
250 break;
251 }
252
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700253 *out << " Layers: count=" << layers_.size() << "\n";
254 for (size_t i = 0; i < layers_.size(); i++) {
255 const DrmHwcLayer &layer = layers_[i];
256 *out << " [" << i << "] ";
257
258 DumpBuffer(layer.buffer, out);
259
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700260 if (layer.protected_usage())
261 *out << " protected";
262
Sean Paul04b47ea2015-11-19 21:46:11 -0500263 *out << " transform=";
264 DumpTransform(layer.transform, out);
265 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500266 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700267 }
268
269 *out << " Planes: count=" << composition_planes_.size() << "\n";
270 for (size_t i = 0; i < composition_planes_.size(); i++) {
271 const DrmCompositionPlane &comp_plane = composition_planes_[i];
272 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400273 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400274 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400275 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400276 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400277 *out << "DISABLE";
278 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400279 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400280 *out << "LAYER";
281 break;
Sean Paul39b37842016-05-11 13:50:28 -0400282 default:
283 *out << "<invalid>";
284 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700285 }
286
Sean Paulca699be2016-05-11 16:29:45 -0400287 *out << " source_layer=";
288 for (auto i : comp_plane.source_layers()) {
289 *out << i << " ";
290 }
291 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700292 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700293}
Sean Paulf72cccd2018-08-27 13:59:08 -0400294} // namespace android