blob: 65d7cf17bc8492d63ae15deafc2fe41379b019fa [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
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010019#include "drmdevice.h"
Sean Paul98e73c82015-06-24 14:38:49 -070020#include "drmdisplaycomposition.h"
Sean Pauled45a8e2017-02-28 13:17:34 -050021#include "drmdisplaycompositor.h"
Sean Paul98e73c82015-06-24 14:38:49 -070022#include "drmcrtc.h"
23#include "drmplane.h"
Sean Paulaa18d912016-05-12 14:28:05 -040024#include "platform.h"
Sean Paul98e73c82015-06-24 14:38:49 -070025
26#include <stdlib.h>
27
Zach Reizner92f8e632015-10-12 17:47:13 -070028#include <algorithm>
29#include <unordered_set>
30
John Stultz9057a6f2018-04-26 12:05:55 -070031#include <log/log.h>
Sean Paul98e73c82015-06-24 14:38:49 -070032#include <sync/sync.h>
Sean Pauldb7a17d2015-06-24 18:46:05 -070033#include <xf86drmMode.h>
Sean Paul98e73c82015-06-24 14:38:49 -070034
35namespace android {
36
Sean Paul98e73c82015-06-24 14:38:49 -070037DrmDisplayComposition::~DrmDisplayComposition() {
Sean Paul98e73c82015-06-24 14:38:49 -070038}
39
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010040int DrmDisplayComposition::Init(DrmDevice *drm, DrmCrtc *crtc,
Sean Paulaa18d912016-05-12 14:28:05 -040041 Importer *importer, Planner *planner,
42 uint64_t frame_no) {
Sean Paul98e73c82015-06-24 14:38:49 -070043 drm_ = drm;
Zach Reizner4a253652015-09-10 18:30:54 -070044 crtc_ = crtc; // Can be NULL if we haven't modeset yet
Sean Paul98e73c82015-06-24 14:38:49 -070045 importer_ = importer;
Sean Paulaa18d912016-05-12 14:28:05 -040046 planner_ = planner;
Sean Paulbdc67bf2015-09-21 10:04:02 -040047 frame_no_ = frame_no;
Sean Paul98e73c82015-06-24 14:38:49 -070048
Sean Paul98e73c82015-06-24 14:38:49 -070049 return 0;
50}
51
Sean Paulacb2a442015-06-24 18:43:01 -070052bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
53 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
54}
55
Zach Reizner5757e822015-10-16 19:06:31 -070056int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
57 bool geometry_changed) {
Zach Reizner92f8e632015-10-12 17:47:13 -070058 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
59 return -EINVAL;
60
Zach Reizner5757e822015-10-16 19:06:31 -070061 geometry_changed_ = geometry_changed;
62
Zach Reizner92f8e632015-10-12 17:47:13 -070063 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
64 layers_.emplace_back(std::move(layers[layer_index]));
65 }
66
67 type_ = DRM_COMPOSITION_TYPE_FRAME;
68 return 0;
69}
70
71int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
72 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
73 return -EINVAL;
74 dpms_mode_ = dpms_mode;
75 type_ = DRM_COMPOSITION_TYPE_DPMS;
76 return 0;
77}
78
79int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
80 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
81 return -EINVAL;
82 display_mode_ = display_mode;
83 dpms_mode_ = DRM_MODE_DPMS_ON;
84 type_ = DRM_COMPOSITION_TYPE_MODESET;
85 return 0;
86}
87
88int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Sean Paulbbe39db2016-05-11 16:57:26 -040089 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
Sean Paulca699be2016-05-11 16:29:45 -040090 crtc_);
Zach Reizner92f8e632015-10-12 17:47:13 -070091 return 0;
92}
93
Sean Paul4f4ef692016-05-03 16:40:59 -070094int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
95 composition_planes_.emplace_back(std::move(plane));
96 return 0;
97}
98
Rob Herringaf0d9752018-05-04 16:34:19 -050099int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -0700100 std::vector<DrmPlane *> *overlay_planes) {
101 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
102 return 0;
103
Sean Paulaa18d912016-05-12 14:28:05 -0400104 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -0700105
Rob Herringaf0d9752018-05-04 16:34:19 -0500106 for (size_t i = 0; i < layers_.size(); ++i)
107 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -0700108
Sean Paulaa18d912016-05-12 14:28:05 -0400109 int ret;
Rob Herringaf0d9752018-05-04 16:34:19 -0500110 std::tie(ret, composition_planes_) = planner_->ProvisionPlanes(
111 to_composite, crtc_, primary_planes, overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400112 if (ret) {
113 ALOGE("Planner failed provisioning planes ret=%d", ret);
114 return ret;
115 }
116
117 // Remove the planes we used from the pool before returning. This ensures they
118 // won't be reused by another display in the composition.
119 for (auto &i : composition_planes_) {
120 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700121 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700122
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700123 // make sure that source layers are ordered based on zorder
124 std::sort(i.source_layers().begin(), i.source_layers().end());
125
Sean Paulaa18d912016-05-12 14:28:05 -0400126 std::vector<DrmPlane *> *container;
127 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
128 container = primary_planes;
129 else
130 container = overlay_planes;
131 for (auto j = container->begin(); j != container->end(); ++j) {
132 if (*j == i.plane()) {
133 container->erase(j);
134 break;
135 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700136 }
137 }
Zach Reizner5757e822015-10-16 19:06:31 -0700138
Rob Herringaf0d9752018-05-04 16:34:19 -0500139 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700140}
141
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700142static const char *DrmCompositionTypeToString(DrmCompositionType type) {
143 switch (type) {
144 case DRM_COMPOSITION_TYPE_EMPTY:
145 return "EMPTY";
146 case DRM_COMPOSITION_TYPE_FRAME:
147 return "FRAME";
148 case DRM_COMPOSITION_TYPE_DPMS:
149 return "DPMS";
150 case DRM_COMPOSITION_TYPE_MODESET:
151 return "MODESET";
152 default:
153 return "<invalid>";
154 }
155}
156
157static const char *DPMSModeToString(int dpms_mode) {
158 switch (dpms_mode) {
159 case DRM_MODE_DPMS_ON:
160 return "ON";
161 case DRM_MODE_DPMS_OFF:
162 return "OFF";
163 default:
164 return "<invalid>";
165 }
166}
167
168static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
169 if (!buffer) {
170 *out << "buffer=<invalid>";
171 return;
172 }
173
174 *out << "buffer[w/h/format]=";
175 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
176}
177
Sean Paul04b47ea2015-11-19 21:46:11 -0500178static void DumpTransform(uint32_t transform, std::ostringstream *out) {
179 *out << "[";
180
181 if (transform == 0)
182 *out << "IDENTITY";
183
184 bool separator = false;
185 if (transform & DrmHwcTransform::kFlipH) {
186 *out << "FLIPH";
187 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700188 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500189 if (transform & DrmHwcTransform::kFlipV) {
190 if (separator)
191 *out << "|";
192 *out << "FLIPV";
193 separator = true;
194 }
195 if (transform & DrmHwcTransform::kRotate90) {
196 if (separator)
197 *out << "|";
198 *out << "ROTATE90";
199 separator = true;
200 }
201 if (transform & DrmHwcTransform::kRotate180) {
202 if (separator)
203 *out << "|";
204 *out << "ROTATE180";
205 separator = true;
206 }
207 if (transform & DrmHwcTransform::kRotate270) {
208 if (separator)
209 *out << "|";
210 *out << "ROTATE270";
211 separator = true;
212 }
213
214 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
215 DrmHwcTransform::kRotate90 |
216 DrmHwcTransform::kRotate180 |
217 DrmHwcTransform::kRotate270;
218 if (transform & ~valid_bits) {
219 if (separator)
220 *out << "|";
221 *out << "INVALID";
222 }
223 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700224}
225
226static const char *BlendingToString(DrmHwcBlending blending) {
227 switch (blending) {
228 case DrmHwcBlending::kNone:
229 return "NONE";
230 case DrmHwcBlending::kPreMult:
231 return "PREMULT";
232 case DrmHwcBlending::kCoverage:
233 return "COVERAGE";
234 default:
235 return "<invalid>";
236 }
237}
238
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700239void DrmDisplayComposition::Dump(std::ostringstream *out) const {
240 *out << "----DrmDisplayComposition"
241 << " crtc=" << (crtc_ ? crtc_->id() : -1)
242 << " type=" << DrmCompositionTypeToString(type_);
243
244 switch (type_) {
245 case DRM_COMPOSITION_TYPE_DPMS:
246 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
247 break;
248 case DRM_COMPOSITION_TYPE_MODESET:
249 *out << " display_mode=" << display_mode_.h_display() << "x"
250 << display_mode_.v_display();
251 break;
252 default:
253 break;
254 }
255
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700256 *out << " Layers: count=" << layers_.size() << "\n";
257 for (size_t i = 0; i < layers_.size(); i++) {
258 const DrmHwcLayer &layer = layers_[i];
259 *out << " [" << i << "] ";
260
261 DumpBuffer(layer.buffer, out);
262
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700263 if (layer.protected_usage())
264 *out << " protected";
265
Sean Paul04b47ea2015-11-19 21:46:11 -0500266 *out << " transform=";
267 DumpTransform(layer.transform, out);
268 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500269 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700270 }
271
272 *out << " Planes: count=" << composition_planes_.size() << "\n";
273 for (size_t i = 0; i < composition_planes_.size(); i++) {
274 const DrmCompositionPlane &comp_plane = composition_planes_[i];
275 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400276 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400277 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400278 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400279 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400280 *out << "DISABLE";
281 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400282 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400283 *out << "LAYER";
284 break;
Sean Paul39b37842016-05-11 13:50:28 -0400285 default:
286 *out << "<invalid>";
287 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700288 }
289
Sean Paulca699be2016-05-11 16:29:45 -0400290 *out << " source_layer=";
291 for (auto i : comp_plane.source_layers()) {
292 *out << i << " ";
293 }
294 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700295 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700296}
Sean Paul98e73c82015-06-24 14:38:49 -0700297}