blob: 49bacad9410f877832933cd6797b03a208cf0bda [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"
21#include "drmplane.h"
22#include "drmresources.h"
23
24#include <stdlib.h>
25
Zach Reizner92f8e632015-10-12 17:47:13 -070026#include <algorithm>
27#include <unordered_set>
28
Sean Paul98e73c82015-06-24 14:38:49 -070029#include <cutils/log.h>
30#include <sw_sync.h>
31#include <sync/sync.h>
Sean Pauldb7a17d2015-06-24 18:46:05 -070032#include <xf86drmMode.h>
Sean Paul98e73c82015-06-24 14:38:49 -070033
34namespace android {
35
Zach Reizner92f8e632015-10-12 17:47:13 -070036const size_t DrmCompositionPlane::kSourceNone;
37const size_t DrmCompositionPlane::kSourcePreComp;
38const size_t DrmCompositionPlane::kSourceSquash;
39const size_t DrmCompositionPlane::kSourceLayerMax;
Sean Paul98e73c82015-06-24 14:38:49 -070040
41DrmDisplayComposition::~DrmDisplayComposition() {
Zach Reiznerece04892015-07-17 14:13:28 -070042 if (timeline_fd_ >= 0) {
Zach Reizner92f8e632015-10-12 17:47:13 -070043 SignalCompositionDone();
Sean Paul98e73c82015-06-24 14:38:49 -070044 close(timeline_fd_);
Zach Reiznerece04892015-07-17 14:13:28 -070045 }
Sean Paul98e73c82015-06-24 14:38:49 -070046}
47
Zach Reizner09807052015-08-13 14:53:41 -070048int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
Sean Paulbdc67bf2015-09-21 10:04:02 -040049 Importer *importer, uint64_t frame_no) {
Sean Paul98e73c82015-06-24 14:38:49 -070050 drm_ = drm;
Zach Reizner4a253652015-09-10 18:30:54 -070051 crtc_ = crtc; // Can be NULL if we haven't modeset yet
Sean Paul98e73c82015-06-24 14:38:49 -070052 importer_ = importer;
Sean Paulbdc67bf2015-09-21 10:04:02 -040053 frame_no_ = frame_no;
Sean Paul98e73c82015-06-24 14:38:49 -070054
Zach Reizner4a253652015-09-10 18:30:54 -070055 int ret = sw_sync_timeline_create();
Sean Paul98e73c82015-06-24 14:38:49 -070056 if (ret < 0) {
57 ALOGE("Failed to create sw sync timeline %d", ret);
58 return ret;
59 }
60 timeline_fd_ = ret;
61 return 0;
62}
63
Sean Paulacb2a442015-06-24 18:43:01 -070064bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
65 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
66}
67
Zach Reizner92f8e632015-10-12 17:47:13 -070068int DrmDisplayComposition::CreateNextTimelineFence() {
69 ++timeline_;
70 return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
71 timeline_);
72}
73
74int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
75 int timeline_increase = point - timeline_current_;
76 if (timeline_increase <= 0)
77 return 0;
78
79 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
80 if (ret)
81 ALOGE("Failed to increment sync timeline %d", ret);
82 else
83 timeline_current_ = point;
84
85 return ret;
86}
87
88int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers) {
89 int ret = 0;
90 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
91 return -EINVAL;
92
93 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
94 layers_.emplace_back(std::move(layers[layer_index]));
95 }
96
97 type_ = DRM_COMPOSITION_TYPE_FRAME;
98 return 0;
99}
100
101int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
102 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
103 return -EINVAL;
104 dpms_mode_ = dpms_mode;
105 type_ = DRM_COMPOSITION_TYPE_DPMS;
106 return 0;
107}
108
109int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
110 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
111 return -EINVAL;
112 display_mode_ = display_mode;
113 dpms_mode_ = DRM_MODE_DPMS_ON;
114 type_ = DRM_COMPOSITION_TYPE_MODESET;
115 return 0;
116}
117
118int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
119 composition_planes_.emplace_back(
120 DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
121 return 0;
122}
123
124static size_t CountUsablePlanes(DrmCrtc *crtc,
125 std::vector<DrmPlane *> *primary_planes,
126 std::vector<DrmPlane *> *overlay_planes) {
127 return std::count_if(
128 primary_planes->begin(), primary_planes->end(),
129 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); }) +
130 std::count_if(
131 overlay_planes->begin(), overlay_planes->end(),
132 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
133}
134
Zach Reizner09807052015-08-13 14:53:41 -0700135static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
136 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
137 if ((*iter)->GetCrtcSupported(*crtc)) {
138 DrmPlane *plane = *iter;
139 planes->erase(iter);
140 return plane;
141 }
Zach Reiznerb44fd102015-08-07 16:00:01 -0700142 }
Zach Reizner09807052015-08-13 14:53:41 -0700143 return NULL;
Sean Paul98e73c82015-06-24 14:38:49 -0700144}
145
Zach Reizner09807052015-08-13 14:53:41 -0700146static DrmPlane *TakePlane(DrmCrtc *crtc,
147 std::vector<DrmPlane *> *primary_planes,
148 std::vector<DrmPlane *> *overlay_planes) {
149 DrmPlane *plane = TakePlane(crtc, primary_planes);
150 if (plane)
151 return plane;
152 return TakePlane(crtc, overlay_planes);
153}
Zach Reizner713a6782015-07-31 15:12:44 -0700154
Zach Reizner92f8e632015-10-12 17:47:13 -0700155static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
156 std::vector<size_t> out;
157 size_t msb = sizeof(in) * 8 - 1;
158 uint64_t mask = (uint64_t)1 << msb;
159 for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
160 if (in & mask)
161 out.push_back(index_map[i]);
162 return out;
Zach Reizner09807052015-08-13 14:53:41 -0700163}
Zach Reizner713a6782015-07-31 15:12:44 -0700164
Zach Reizner92f8e632015-10-12 17:47:13 -0700165static void SeperateLayers(DrmHwcLayer *layers, size_t *used_layers,
166 size_t num_used_layers,
167 DrmHwcRect<int> *exclude_rects,
168 size_t num_exclude_rects,
169 std::vector<DrmCompositionRegion> &regions) {
170 if (num_used_layers > 64) {
171 ALOGE("Failed to separate layers because there are more than 64");
172 return;
173 }
Zach Reizner713a6782015-07-31 15:12:44 -0700174
Zach Reizner92f8e632015-10-12 17:47:13 -0700175 if (num_used_layers + num_exclude_rects > 64) {
176 ALOGW(
177 "Exclusion rectangles are being truncated to make the rectangle count "
178 "fit into 64");
179 num_exclude_rects = 64 - num_used_layers;
180 }
Zach Reizner713a6782015-07-31 15:12:44 -0700181
Zach Reizner92f8e632015-10-12 17:47:13 -0700182 // We inject all the exclude rects into the rects list. Any resulting rect
183 // that includes ANY of the first num_exclude_rects is rejected.
184 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + num_exclude_rects);
185 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
186 layer_rects.begin());
187 std::transform(
188 used_layers, used_layers + num_used_layers,
189 layer_rects.begin() + num_exclude_rects,
190 [=](size_t layer_index) { return layers[layer_index].display_frame; });
191
192 std::vector<seperate_rects::RectSet<uint64_t, int>> seperate_regions;
193 seperate_rects::seperate_rects_64(layer_rects, &seperate_regions);
194 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
195
196 for (seperate_rects::RectSet<uint64_t, int> &region : seperate_regions) {
197 if (region.id_set.getBits() & exclude_mask)
198 continue;
199 regions.emplace_back(DrmCompositionRegion{
200 region.rect,
201 SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
202 used_layers)});
203 }
Zach Reizner713a6782015-07-31 15:12:44 -0700204}
205
Zach Reizner92f8e632015-10-12 17:47:13 -0700206int DrmDisplayComposition::Plan(SquashState *squash,
207 std::vector<DrmPlane *> *primary_planes,
208 std::vector<DrmPlane *> *overlay_planes) {
209 size_t planes_can_use =
210 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
211 if (planes_can_use == 0) {
212 ALOGE("Display %d has no usable planes", crtc_->display());
213 return -ENODEV;
214 }
Zach Reizner09807052015-08-13 14:53:41 -0700215
Zach Reizner92f8e632015-10-12 17:47:13 -0700216 std::vector<int> layer_squash_area(layers_.size());
217 if (squash != NULL && planes_can_use >= 3) {
218 std::vector<bool> changed_regions;
219 squash->GenerateHistory(layers_.data(), changed_regions);
Zach Reizner09807052015-08-13 14:53:41 -0700220
Zach Reizner92f8e632015-10-12 17:47:13 -0700221 std::vector<bool> stable_regions;
222 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
Zach Reizner09807052015-08-13 14:53:41 -0700223
Zach Reizner92f8e632015-10-12 17:47:13 -0700224 squash->RecordHistory(layers_.data(), changed_regions);
225
226 squash->RecordSquashed(stable_regions);
227
228 for (size_t region_index = 0; region_index < stable_regions.size();
229 region_index++) {
230 const SquashState::Region &region = squash->regions()[region_index];
231 if (stable_regions[region_index]) {
232 squash_regions_.emplace_back();
233 DrmCompositionRegion &squash_region = squash_regions_.back();
234 squash_region.frame = region.rect;
235 for (size_t layer_index = 0; layer_index < SquashState::kMaxLayers;
236 layer_index++) {
237 if (region.layer_refs[layer_index]) {
238 squash_region.source_layers.push_back(layer_index);
239 layer_squash_area[layer_index] += squash_region.frame.area();
240 }
Zach Reizner09807052015-08-13 14:53:41 -0700241 }
Zach Reizner09807052015-08-13 14:53:41 -0700242 }
243 }
244 }
245
Zach Reizner92f8e632015-10-12 17:47:13 -0700246 std::vector<size_t> layers_remaining;
247 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
248 // Skip layers that were completely squashed
249 if (layer_squash_area[layer_index] >=
250 layers_[layer_index].display_frame.area()) {
251 continue;
252 }
253
254 layers_remaining.push_back(layer_index);
255 }
256
257 size_t layer_to_composite = layers_remaining.size();
258 size_t num_layers_to_pre_composite = 0;
259 if (squash_regions_.size() > 0) {
260 layers_remaining.push_back(DrmCompositionPlane::kSourceSquash);
261 }
262
263 if (layers_remaining.size() > planes_can_use) {
264 layers_remaining.insert(layers_remaining.begin() + layer_to_composite,
265 DrmCompositionPlane::kSourcePreComp);
266 size_t num_layers_to_pre_composite =
267 layer_to_composite - planes_can_use + 1;
268 size_t first_layer_to_pre_composite = planes_can_use - 1;
269 SeperateLayers(layers_.data(),
270 &layers_remaining[first_layer_to_pre_composite],
271 num_layers_to_pre_composite, NULL, 0, pre_comp_regions_);
272 layers_remaining.erase(
273 layers_remaining.begin() + first_layer_to_pre_composite,
274 layers_remaining.begin() + layer_to_composite);
275 }
276
277 for (size_t i : layers_remaining) {
278 composition_planes_.emplace_back(DrmCompositionPlane{
279 TakePlane(crtc_, primary_planes, overlay_planes), crtc_, i});
280 }
281
282 std::unordered_set<DrmHwcLayer *> squash_layers;
283 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
284 std::unordered_set<DrmHwcLayer *> comp_layers;
285
286 for (const DrmCompositionRegion &region : squash_regions_) {
287 for (size_t source_layer_index : region.source_layers) {
288 DrmHwcLayer *source_layer = &layers_[source_layer_index];
289 squash_layers.emplace(source_layer);
290 }
291 }
292
293 for (const DrmCompositionRegion &region : pre_comp_regions_) {
294 for (size_t source_layer_index : region.source_layers) {
295 DrmHwcLayer *source_layer = &layers_[source_layer_index];
296 pre_comp_layers.emplace(source_layer);
297 squash_layers.erase(source_layer);
298 }
299 }
300
301 for (const DrmCompositionPlane &plane : composition_planes_) {
302 if (plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
303 DrmHwcLayer *source_layer = &layers_[plane.source_layer];
304 comp_layers.emplace(source_layer);
305 pre_comp_layers.erase(source_layer);
306 }
307 }
308
309 for (DrmHwcLayer *layer : squash_layers) {
310 int ret = layer->release_fence.Set(CreateNextTimelineFence());
311 if (ret < 0)
312 return ret;
313 }
314 timeline_squash_done_ = timeline_;
315
316 for (DrmHwcLayer *layer : pre_comp_layers) {
317 int ret = layer->release_fence.Set(CreateNextTimelineFence());
318 if (ret < 0)
319 return ret;
320 }
Zach Reizner09807052015-08-13 14:53:41 -0700321 timeline_pre_comp_done_ = timeline_;
322
Zach Reizner92f8e632015-10-12 17:47:13 -0700323 for (DrmHwcLayer *layer : comp_layers) {
324 int ret = layer->release_fence.Set(CreateNextTimelineFence());
325 if (ret < 0)
326 return ret;
Zach Reizner09807052015-08-13 14:53:41 -0700327 }
328
Zach Reizner09807052015-08-13 14:53:41 -0700329 return 0;
Zach Reizner46ddd452015-07-30 08:10:14 -0700330}
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700331
332static const char *DrmCompositionTypeToString(DrmCompositionType type) {
333 switch (type) {
334 case DRM_COMPOSITION_TYPE_EMPTY:
335 return "EMPTY";
336 case DRM_COMPOSITION_TYPE_FRAME:
337 return "FRAME";
338 case DRM_COMPOSITION_TYPE_DPMS:
339 return "DPMS";
340 case DRM_COMPOSITION_TYPE_MODESET:
341 return "MODESET";
342 default:
343 return "<invalid>";
344 }
345}
346
347static const char *DPMSModeToString(int dpms_mode) {
348 switch (dpms_mode) {
349 case DRM_MODE_DPMS_ON:
350 return "ON";
351 case DRM_MODE_DPMS_OFF:
352 return "OFF";
353 default:
354 return "<invalid>";
355 }
356}
357
358static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
359 if (!buffer) {
360 *out << "buffer=<invalid>";
361 return;
362 }
363
364 *out << "buffer[w/h/format]=";
365 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
366}
367
368static const char *TransformToString(DrmHwcTransform transform) {
369 switch (transform) {
370 case DrmHwcTransform::kIdentity:
371 return "IDENTITY";
372 case DrmHwcTransform::kFlipH:
373 return "FLIPH";
374 case DrmHwcTransform::kFlipV:
375 return "FLIPV";
376 case DrmHwcTransform::kRotate90:
377 return "ROTATE90";
378 case DrmHwcTransform::kRotate180:
379 return "ROTATE180";
380 case DrmHwcTransform::kRotate270:
381 return "ROTATE270";
382 default:
383 return "<invalid>";
384 }
385}
386
387static const char *BlendingToString(DrmHwcBlending blending) {
388 switch (blending) {
389 case DrmHwcBlending::kNone:
390 return "NONE";
391 case DrmHwcBlending::kPreMult:
392 return "PREMULT";
393 case DrmHwcBlending::kCoverage:
394 return "COVERAGE";
395 default:
396 return "<invalid>";
397 }
398}
399
400static void DumpRegion(const DrmCompositionRegion &region,
401 std::ostringstream *out) {
402 *out << "frame";
403 region.frame.Dump(out);
404 *out << " source_layers=(";
405
406 const std::vector<size_t> &source_layers = region.source_layers;
407 for (size_t i = 0; i < source_layers.size(); i++) {
408 *out << source_layers[i];
409 if (i < source_layers.size() - 1) {
410 *out << " ";
411 }
412 }
413
414 *out << ")";
415}
416
417void DrmDisplayComposition::Dump(std::ostringstream *out) const {
418 *out << "----DrmDisplayComposition"
419 << " crtc=" << (crtc_ ? crtc_->id() : -1)
420 << " type=" << DrmCompositionTypeToString(type_);
421
422 switch (type_) {
423 case DRM_COMPOSITION_TYPE_DPMS:
424 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
425 break;
426 case DRM_COMPOSITION_TYPE_MODESET:
427 *out << " display_mode=" << display_mode_.h_display() << "x"
428 << display_mode_.v_display();
429 break;
430 default:
431 break;
432 }
433
434 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
435 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
436 << timeline_ << "\n";
437
438 *out << " Layers: count=" << layers_.size() << "\n";
439 for (size_t i = 0; i < layers_.size(); i++) {
440 const DrmHwcLayer &layer = layers_[i];
441 *out << " [" << i << "] ";
442
443 DumpBuffer(layer.buffer, out);
444
445 *out << " transform=" << TransformToString(layer.transform)
446 << " blending[a=" << (int)layer.alpha
447 << "]=" << BlendingToString(layer.blending) << " source_crop";
448 layer.source_crop.Dump(out);
449 *out << " display_frame";
450 layer.display_frame.Dump(out);
451
452 *out << "\n";
453 }
454
455 *out << " Planes: count=" << composition_planes_.size() << "\n";
456 for (size_t i = 0; i < composition_planes_.size(); i++) {
457 const DrmCompositionPlane &comp_plane = composition_planes_[i];
458 *out << " [" << i << "]"
459 << " plane=" << (comp_plane.plane ? comp_plane.plane->id() : -1)
460 << " source_layer=";
461 if (comp_plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
462 *out << comp_plane.source_layer;
463 } else {
464 switch (comp_plane.source_layer) {
465 case DrmCompositionPlane::kSourceNone:
466 *out << "NONE";
467 break;
468 case DrmCompositionPlane::kSourcePreComp:
469 *out << "PRECOMP";
470 break;
471 case DrmCompositionPlane::kSourceSquash:
472 *out << "SQUASH";
473 break;
474 default:
475 *out << "<invalid>";
476 break;
477 }
478 }
479
480 *out << "\n";
481 }
482
483 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
484 for (size_t i = 0; i < squash_regions_.size(); i++) {
485 *out << " [" << i << "] ";
486 DumpRegion(squash_regions_[i], out);
487 *out << "\n";
488 }
489
490 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
491 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
492 *out << " [" << i << "] ";
493 DumpRegion(pre_comp_regions_[i], out);
494 *out << "\n";
495 }
496}
Sean Paul98e73c82015-06-24 14:38:49 -0700497}