blob: b710fe1a66b435db91ec5c4dd3df6582d0171f33 [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"
20#include "drmcrtc.h"
Sean Paulf72cccd2018-08-27 13:59:08 -040021#include "drmdevice.h"
22#include "drmdisplaycompositor.h"
Sean Paul98e73c82015-06-24 14:38:49 -070023#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;
Sean Paulf72cccd2018-08-27 13:59:08 -0400110 std::tie(ret,
111 composition_planes_) = planner_->ProvisionPlanes(to_composite, crtc_,
112 primary_planes,
113 overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400114 if (ret) {
115 ALOGE("Planner failed provisioning planes ret=%d", ret);
116 return ret;
117 }
118
119 // Remove the planes we used from the pool before returning. This ensures they
120 // won't be reused by another display in the composition.
121 for (auto &i : composition_planes_) {
122 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700123 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700124
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700125 // make sure that source layers are ordered based on zorder
126 std::sort(i.source_layers().begin(), i.source_layers().end());
127
Sean Paulaa18d912016-05-12 14:28:05 -0400128 std::vector<DrmPlane *> *container;
129 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
130 container = primary_planes;
131 else
132 container = overlay_planes;
133 for (auto j = container->begin(); j != container->end(); ++j) {
134 if (*j == i.plane()) {
135 container->erase(j);
136 break;
137 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700138 }
139 }
Zach Reizner5757e822015-10-16 19:06:31 -0700140
Rob Herringaf0d9752018-05-04 16:34:19 -0500141 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700142}
143
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700144static const char *DrmCompositionTypeToString(DrmCompositionType type) {
145 switch (type) {
146 case DRM_COMPOSITION_TYPE_EMPTY:
147 return "EMPTY";
148 case DRM_COMPOSITION_TYPE_FRAME:
149 return "FRAME";
150 case DRM_COMPOSITION_TYPE_DPMS:
151 return "DPMS";
152 case DRM_COMPOSITION_TYPE_MODESET:
153 return "MODESET";
154 default:
155 return "<invalid>";
156 }
157}
158
159static const char *DPMSModeToString(int dpms_mode) {
160 switch (dpms_mode) {
161 case DRM_MODE_DPMS_ON:
162 return "ON";
163 case DRM_MODE_DPMS_OFF:
164 return "OFF";
165 default:
166 return "<invalid>";
167 }
168}
169
170static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
171 if (!buffer) {
172 *out << "buffer=<invalid>";
173 return;
174 }
175
176 *out << "buffer[w/h/format]=";
177 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
178}
179
Sean Paul04b47ea2015-11-19 21:46:11 -0500180static void DumpTransform(uint32_t transform, std::ostringstream *out) {
181 *out << "[";
182
183 if (transform == 0)
184 *out << "IDENTITY";
185
186 bool separator = false;
187 if (transform & DrmHwcTransform::kFlipH) {
188 *out << "FLIPH";
189 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700190 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500191 if (transform & DrmHwcTransform::kFlipV) {
192 if (separator)
193 *out << "|";
194 *out << "FLIPV";
195 separator = true;
196 }
197 if (transform & DrmHwcTransform::kRotate90) {
198 if (separator)
199 *out << "|";
200 *out << "ROTATE90";
201 separator = true;
202 }
203 if (transform & DrmHwcTransform::kRotate180) {
204 if (separator)
205 *out << "|";
206 *out << "ROTATE180";
207 separator = true;
208 }
209 if (transform & DrmHwcTransform::kRotate270) {
210 if (separator)
211 *out << "|";
212 *out << "ROTATE270";
213 separator = true;
214 }
215
216 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
217 DrmHwcTransform::kRotate90 |
218 DrmHwcTransform::kRotate180 |
219 DrmHwcTransform::kRotate270;
220 if (transform & ~valid_bits) {
221 if (separator)
222 *out << "|";
223 *out << "INVALID";
224 }
225 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700226}
227
228static const char *BlendingToString(DrmHwcBlending blending) {
229 switch (blending) {
230 case DrmHwcBlending::kNone:
231 return "NONE";
232 case DrmHwcBlending::kPreMult:
233 return "PREMULT";
234 case DrmHwcBlending::kCoverage:
235 return "COVERAGE";
236 default:
237 return "<invalid>";
238 }
239}
240
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700241void DrmDisplayComposition::Dump(std::ostringstream *out) const {
242 *out << "----DrmDisplayComposition"
243 << " crtc=" << (crtc_ ? crtc_->id() : -1)
244 << " type=" << DrmCompositionTypeToString(type_);
245
246 switch (type_) {
247 case DRM_COMPOSITION_TYPE_DPMS:
248 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
249 break;
250 case DRM_COMPOSITION_TYPE_MODESET:
251 *out << " display_mode=" << display_mode_.h_display() << "x"
252 << display_mode_.v_display();
253 break;
254 default:
255 break;
256 }
257
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700258 *out << " Layers: count=" << layers_.size() << "\n";
259 for (size_t i = 0; i < layers_.size(); i++) {
260 const DrmHwcLayer &layer = layers_[i];
261 *out << " [" << i << "] ";
262
263 DumpBuffer(layer.buffer, out);
264
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700265 if (layer.protected_usage())
266 *out << " protected";
267
Sean Paul04b47ea2015-11-19 21:46:11 -0500268 *out << " transform=";
269 DumpTransform(layer.transform, out);
270 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500271 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700272 }
273
274 *out << " Planes: count=" << composition_planes_.size() << "\n";
275 for (size_t i = 0; i < composition_planes_.size(); i++) {
276 const DrmCompositionPlane &comp_plane = composition_planes_[i];
277 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400278 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400279 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400280 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400281 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400282 *out << "DISABLE";
283 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400284 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400285 *out << "LAYER";
286 break;
Sean Paul39b37842016-05-11 13:50:28 -0400287 default:
288 *out << "<invalid>";
289 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700290 }
291
Sean Paulca699be2016-05-11 16:29:45 -0400292 *out << " source_layer=";
293 for (auto i : comp_plane.source_layers()) {
294 *out << i << " ";
295 }
296 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700297 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700298}
Sean Paulf72cccd2018-08-27 13:59:08 -0400299} // namespace android