blob: ccf2676c493a7e26fcbec71396ba241b1c17e4d0 [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
19#include "drmdisplaycomposition.h"
Sean Pauled45a8e2017-02-28 13:17:34 -050020#include "drmdisplaycompositor.h"
Sean Paul98e73c82015-06-24 14:38:49 -070021#include "drmcrtc.h"
22#include "drmplane.h"
23#include "drmresources.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
Zach Reizner09807052015-08-13 14:53:41 -070040int DrmDisplayComposition::Init(DrmResources *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 -050099void DrmDisplayComposition::SeparateLayers(DrmHwcRect<int> *, size_t) {
Sean Paul3960cdd2016-05-10 04:17:31 -0400100 std::vector<size_t> dedicated_layers;
101
Rob Herringaf0d9752018-05-04 16:34:19 -0500102 // Go through the composition and find the layers that have a dedicated plane.
Sean Paul3960cdd2016-05-10 04:17:31 -0400103 for (auto &i : composition_planes_) {
104 if (i.type() == DrmCompositionPlane::Type::kLayer) {
105 dedicated_layers.insert(dedicated_layers.end(), i.source_layers().begin(),
106 i.source_layers().end());
Sean Paul3960cdd2016-05-10 04:17:31 -0400107 }
108 }
Zach Reizner713a6782015-07-31 15:12:44 -0700109}
110
Rob Herringaf0d9752018-05-04 16:34:19 -0500111int DrmDisplayComposition::Plan(std::vector<DrmPlane *> *primary_planes,
Zach Reizner5757e822015-10-16 19:06:31 -0700112 std::vector<DrmPlane *> *overlay_planes) {
113 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
114 return 0;
115
Sean Paulaa18d912016-05-12 14:28:05 -0400116 std::map<size_t, DrmHwcLayer *> to_composite;
Zach Reizner5757e822015-10-16 19:06:31 -0700117
Rob Herringaf0d9752018-05-04 16:34:19 -0500118 for (size_t i = 0; i < layers_.size(); ++i)
119 to_composite.emplace(std::make_pair(i, &layers_[i]));
Zach Reizner5757e822015-10-16 19:06:31 -0700120
Sean Paulaa18d912016-05-12 14:28:05 -0400121 int ret;
122 std::vector<DrmCompositionPlane> plan;
Rob Herringaf0d9752018-05-04 16:34:19 -0500123 std::tie(ret, composition_planes_) = planner_->ProvisionPlanes(
124 to_composite, crtc_, primary_planes, overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400125 if (ret) {
126 ALOGE("Planner failed provisioning planes ret=%d", ret);
127 return ret;
128 }
129
130 // Remove the planes we used from the pool before returning. This ensures they
131 // won't be reused by another display in the composition.
132 for (auto &i : composition_planes_) {
133 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700134 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700135
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700136 // make sure that source layers are ordered based on zorder
137 std::sort(i.source_layers().begin(), i.source_layers().end());
138
Sean Paulaa18d912016-05-12 14:28:05 -0400139 std::vector<DrmPlane *> *container;
140 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
141 container = primary_planes;
142 else
143 container = overlay_planes;
144 for (auto j = container->begin(); j != container->end(); ++j) {
145 if (*j == i.plane()) {
146 container->erase(j);
147 break;
148 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700149 }
150 }
Zach Reizner5757e822015-10-16 19:06:31 -0700151
Rob Herringaf0d9752018-05-04 16:34:19 -0500152 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700153}
154
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700155static const char *DrmCompositionTypeToString(DrmCompositionType type) {
156 switch (type) {
157 case DRM_COMPOSITION_TYPE_EMPTY:
158 return "EMPTY";
159 case DRM_COMPOSITION_TYPE_FRAME:
160 return "FRAME";
161 case DRM_COMPOSITION_TYPE_DPMS:
162 return "DPMS";
163 case DRM_COMPOSITION_TYPE_MODESET:
164 return "MODESET";
165 default:
166 return "<invalid>";
167 }
168}
169
170static const char *DPMSModeToString(int dpms_mode) {
171 switch (dpms_mode) {
172 case DRM_MODE_DPMS_ON:
173 return "ON";
174 case DRM_MODE_DPMS_OFF:
175 return "OFF";
176 default:
177 return "<invalid>";
178 }
179}
180
181static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
182 if (!buffer) {
183 *out << "buffer=<invalid>";
184 return;
185 }
186
187 *out << "buffer[w/h/format]=";
188 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
189}
190
Sean Paul04b47ea2015-11-19 21:46:11 -0500191static void DumpTransform(uint32_t transform, std::ostringstream *out) {
192 *out << "[";
193
194 if (transform == 0)
195 *out << "IDENTITY";
196
197 bool separator = false;
198 if (transform & DrmHwcTransform::kFlipH) {
199 *out << "FLIPH";
200 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700201 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500202 if (transform & DrmHwcTransform::kFlipV) {
203 if (separator)
204 *out << "|";
205 *out << "FLIPV";
206 separator = true;
207 }
208 if (transform & DrmHwcTransform::kRotate90) {
209 if (separator)
210 *out << "|";
211 *out << "ROTATE90";
212 separator = true;
213 }
214 if (transform & DrmHwcTransform::kRotate180) {
215 if (separator)
216 *out << "|";
217 *out << "ROTATE180";
218 separator = true;
219 }
220 if (transform & DrmHwcTransform::kRotate270) {
221 if (separator)
222 *out << "|";
223 *out << "ROTATE270";
224 separator = true;
225 }
226
227 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
228 DrmHwcTransform::kRotate90 |
229 DrmHwcTransform::kRotate180 |
230 DrmHwcTransform::kRotate270;
231 if (transform & ~valid_bits) {
232 if (separator)
233 *out << "|";
234 *out << "INVALID";
235 }
236 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700237}
238
239static const char *BlendingToString(DrmHwcBlending blending) {
240 switch (blending) {
241 case DrmHwcBlending::kNone:
242 return "NONE";
243 case DrmHwcBlending::kPreMult:
244 return "PREMULT";
245 case DrmHwcBlending::kCoverage:
246 return "COVERAGE";
247 default:
248 return "<invalid>";
249 }
250}
251
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700252void DrmDisplayComposition::Dump(std::ostringstream *out) const {
253 *out << "----DrmDisplayComposition"
254 << " crtc=" << (crtc_ ? crtc_->id() : -1)
255 << " type=" << DrmCompositionTypeToString(type_);
256
257 switch (type_) {
258 case DRM_COMPOSITION_TYPE_DPMS:
259 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
260 break;
261 case DRM_COMPOSITION_TYPE_MODESET:
262 *out << " display_mode=" << display_mode_.h_display() << "x"
263 << display_mode_.v_display();
264 break;
265 default:
266 break;
267 }
268
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700269 *out << " Layers: count=" << layers_.size() << "\n";
270 for (size_t i = 0; i < layers_.size(); i++) {
271 const DrmHwcLayer &layer = layers_[i];
272 *out << " [" << i << "] ";
273
274 DumpBuffer(layer.buffer, out);
275
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700276 if (layer.protected_usage())
277 *out << " protected";
278
Sean Paul04b47ea2015-11-19 21:46:11 -0500279 *out << " transform=";
280 DumpTransform(layer.transform, out);
281 *out << " blending[a=" << (int)layer.alpha
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700282 << "]=" << BlendingToString(layer.blending) << " source_crop";
283 layer.source_crop.Dump(out);
284 *out << " display_frame";
285 layer.display_frame.Dump(out);
286
287 *out << "\n";
288 }
289
290 *out << " Planes: count=" << composition_planes_.size() << "\n";
291 for (size_t i = 0; i < composition_planes_.size(); i++) {
292 const DrmCompositionPlane &comp_plane = composition_planes_[i];
293 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400294 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400295 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400296 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400297 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400298 *out << "DISABLE";
299 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400300 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400301 *out << "LAYER";
302 break;
Sean Paul39b37842016-05-11 13:50:28 -0400303 default:
304 *out << "<invalid>";
305 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700306 }
307
Sean Paulca699be2016-05-11 16:29:45 -0400308 *out << " source_layer=";
309 for (auto i : comp_plane.source_layers()) {
310 *out << i << " ";
311 }
312 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700313 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700314}
Sean Paul98e73c82015-06-24 14:38:49 -0700315}