blob: 129bec2aac826b0b768c7cd4ca1f6b3de8d71f27 [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 -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;
110 std::vector<DrmCompositionPlane> plan;
Rob Herringaf0d9752018-05-04 16:34:19 -0500111 std::tie(ret, composition_planes_) = planner_->ProvisionPlanes(
112 to_composite, crtc_, primary_planes, overlay_planes);
Sean Paulaa18d912016-05-12 14:28:05 -0400113 if (ret) {
114 ALOGE("Planner failed provisioning planes ret=%d", ret);
115 return ret;
116 }
117
118 // Remove the planes we used from the pool before returning. This ensures they
119 // won't be reused by another display in the composition.
120 for (auto &i : composition_planes_) {
121 if (!i.plane())
Zach Reizner5757e822015-10-16 19:06:31 -0700122 continue;
Zach Reizner5757e822015-10-16 19:06:31 -0700123
Adrian Salido1a1cf9b2017-09-21 16:53:48 -0700124 // make sure that source layers are ordered based on zorder
125 std::sort(i.source_layers().begin(), i.source_layers().end());
126
Sean Paulaa18d912016-05-12 14:28:05 -0400127 std::vector<DrmPlane *> *container;
128 if (i.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
129 container = primary_planes;
130 else
131 container = overlay_planes;
132 for (auto j = container->begin(); j != container->end(); ++j) {
133 if (*j == i.plane()) {
134 container->erase(j);
135 break;
136 }
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700137 }
138 }
Zach Reizner5757e822015-10-16 19:06:31 -0700139
Rob Herringaf0d9752018-05-04 16:34:19 -0500140 return 0;
Zach Reizner5757e822015-10-16 19:06:31 -0700141}
142
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700143static const char *DrmCompositionTypeToString(DrmCompositionType type) {
144 switch (type) {
145 case DRM_COMPOSITION_TYPE_EMPTY:
146 return "EMPTY";
147 case DRM_COMPOSITION_TYPE_FRAME:
148 return "FRAME";
149 case DRM_COMPOSITION_TYPE_DPMS:
150 return "DPMS";
151 case DRM_COMPOSITION_TYPE_MODESET:
152 return "MODESET";
153 default:
154 return "<invalid>";
155 }
156}
157
158static const char *DPMSModeToString(int dpms_mode) {
159 switch (dpms_mode) {
160 case DRM_MODE_DPMS_ON:
161 return "ON";
162 case DRM_MODE_DPMS_OFF:
163 return "OFF";
164 default:
165 return "<invalid>";
166 }
167}
168
169static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
170 if (!buffer) {
171 *out << "buffer=<invalid>";
172 return;
173 }
174
175 *out << "buffer[w/h/format]=";
176 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
177}
178
Sean Paul04b47ea2015-11-19 21:46:11 -0500179static void DumpTransform(uint32_t transform, std::ostringstream *out) {
180 *out << "[";
181
182 if (transform == 0)
183 *out << "IDENTITY";
184
185 bool separator = false;
186 if (transform & DrmHwcTransform::kFlipH) {
187 *out << "FLIPH";
188 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700189 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500190 if (transform & DrmHwcTransform::kFlipV) {
191 if (separator)
192 *out << "|";
193 *out << "FLIPV";
194 separator = true;
195 }
196 if (transform & DrmHwcTransform::kRotate90) {
197 if (separator)
198 *out << "|";
199 *out << "ROTATE90";
200 separator = true;
201 }
202 if (transform & DrmHwcTransform::kRotate180) {
203 if (separator)
204 *out << "|";
205 *out << "ROTATE180";
206 separator = true;
207 }
208 if (transform & DrmHwcTransform::kRotate270) {
209 if (separator)
210 *out << "|";
211 *out << "ROTATE270";
212 separator = true;
213 }
214
215 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
216 DrmHwcTransform::kRotate90 |
217 DrmHwcTransform::kRotate180 |
218 DrmHwcTransform::kRotate270;
219 if (transform & ~valid_bits) {
220 if (separator)
221 *out << "|";
222 *out << "INVALID";
223 }
224 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700225}
226
227static const char *BlendingToString(DrmHwcBlending blending) {
228 switch (blending) {
229 case DrmHwcBlending::kNone:
230 return "NONE";
231 case DrmHwcBlending::kPreMult:
232 return "PREMULT";
233 case DrmHwcBlending::kCoverage:
234 return "COVERAGE";
235 default:
236 return "<invalid>";
237 }
238}
239
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700240void DrmDisplayComposition::Dump(std::ostringstream *out) const {
241 *out << "----DrmDisplayComposition"
242 << " crtc=" << (crtc_ ? crtc_->id() : -1)
243 << " type=" << DrmCompositionTypeToString(type_);
244
245 switch (type_) {
246 case DRM_COMPOSITION_TYPE_DPMS:
247 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
248 break;
249 case DRM_COMPOSITION_TYPE_MODESET:
250 *out << " display_mode=" << display_mode_.h_display() << "x"
251 << display_mode_.v_display();
252 break;
253 default:
254 break;
255 }
256
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700257 *out << " Layers: count=" << layers_.size() << "\n";
258 for (size_t i = 0; i < layers_.size(); i++) {
259 const DrmHwcLayer &layer = layers_[i];
260 *out << " [" << i << "] ";
261
262 DumpBuffer(layer.buffer, out);
263
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700264 if (layer.protected_usage())
265 *out << " protected";
266
Sean Paul04b47ea2015-11-19 21:46:11 -0500267 *out << " transform=";
268 DumpTransform(layer.transform, out);
269 *out << " blending[a=" << (int)layer.alpha
Rob Herringcff7b1e2018-05-09 15:18:36 -0500270 << "]=" << BlendingToString(layer.blending) << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700271 }
272
273 *out << " Planes: count=" << composition_planes_.size() << "\n";
274 for (size_t i = 0; i < composition_planes_.size(); i++) {
275 const DrmCompositionPlane &comp_plane = composition_planes_[i];
276 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400277 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400278 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400279 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400280 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400281 *out << "DISABLE";
282 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400283 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400284 *out << "LAYER";
285 break;
Sean Paul39b37842016-05-11 13:50:28 -0400286 default:
287 *out << "<invalid>";
288 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700289 }
290
Sean Paulca699be2016-05-11 16:29:45 -0400291 *out << " source_layer=";
292 for (auto i : comp_plane.source_layers()) {
293 *out << i << " ";
294 }
295 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700296 }
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700297}
Sean Paul98e73c82015-06-24 14:38:49 -0700298}