2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "hwc-drm-display-composition"
19 #include "drmdisplaycomposition.h"
22 #include "drmresources.h"
27 #include <unordered_set>
29 #include <cutils/log.h>
31 #include <sync/sync.h>
32 #include <xf86drmMode.h>
36 DrmDisplayComposition::~DrmDisplayComposition() {
37 if (timeline_fd_ >= 0) {
38 SignalCompositionDone();
43 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
44 Importer *importer, uint64_t frame_no) {
46 crtc_ = crtc; // Can be NULL if we haven't modeset yet
50 int ret = sw_sync_timeline_create();
52 ALOGE("Failed to create sw sync timeline %d", ret);
59 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
60 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
63 int DrmDisplayComposition::CreateNextTimelineFence() {
65 return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
69 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
70 int timeline_increase = point - timeline_current_;
71 if (timeline_increase <= 0)
74 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
76 ALOGE("Failed to increment sync timeline %d", ret);
78 timeline_current_ = point;
83 int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
84 bool geometry_changed) {
85 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
88 geometry_changed_ = geometry_changed;
90 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
91 layers_.emplace_back(std::move(layers[layer_index]));
94 type_ = DRM_COMPOSITION_TYPE_FRAME;
98 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
99 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
101 dpms_mode_ = dpms_mode;
102 type_ = DRM_COMPOSITION_TYPE_DPMS;
106 int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
107 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
109 display_mode_ = display_mode;
110 dpms_mode_ = DRM_MODE_DPMS_ON;
111 type_ = DRM_COMPOSITION_TYPE_MODESET;
115 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
116 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
121 static size_t CountUsablePlanes(DrmCrtc *crtc,
122 std::vector<DrmPlane *> *primary_planes,
123 std::vector<DrmPlane *> *overlay_planes) {
124 return std::count_if(
125 primary_planes->begin(), primary_planes->end(),
126 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); }) +
128 overlay_planes->begin(), overlay_planes->end(),
129 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
132 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
133 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
134 if ((*iter)->GetCrtcSupported(*crtc)) {
135 DrmPlane *plane = *iter;
143 static DrmPlane *TakePlane(DrmCrtc *crtc,
144 std::vector<DrmPlane *> *primary_planes,
145 std::vector<DrmPlane *> *overlay_planes) {
146 DrmPlane *plane = TakePlane(crtc, primary_planes);
149 return TakePlane(crtc, overlay_planes);
152 void DrmDisplayComposition::EmplaceCompositionPlane(
153 DrmCompositionPlane::Type type, std::vector<DrmPlane *> *primary_planes,
154 std::vector<DrmPlane *> *overlay_planes) {
155 DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
158 "Failed to add composition plane because there are no planes "
162 composition_planes_.emplace_back(type, plane, crtc_);
165 void DrmDisplayComposition::EmplaceCompositionPlane(
166 size_t source_layer, std::vector<DrmPlane *> *primary_planes,
167 std::vector<DrmPlane *> *overlay_planes) {
168 DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
171 "Failed to add composition plane because there are no planes "
175 composition_planes_.emplace_back(DrmCompositionPlane::Type::kLayer, plane,
176 crtc_, source_layer);
179 static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
180 std::vector<size_t> out;
181 size_t msb = sizeof(in) * 8 - 1;
182 uint64_t mask = (uint64_t)1 << msb;
183 for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
185 out.push_back(index_map[i]);
189 void DrmDisplayComposition::SeparateLayers(size_t *used_layers,
190 size_t num_used_layers,
191 DrmHwcRect<int> *exclude_rects,
192 size_t num_exclude_rects) {
193 DrmCompositionPlane *comp = NULL;
194 std::vector<size_t> dedicated_layers;
196 // Go through the composition and find the precomp layer as well as any
197 // layers that have a dedicated plane located below the precomp layer.
198 for (auto &i : composition_planes_) {
199 if (i.type() == DrmCompositionPlane::Type::kLayer) {
200 dedicated_layers.insert(dedicated_layers.end(), i.source_layers().begin(),
201 i.source_layers().end());
202 } else if (i.type() == DrmCompositionPlane::Type::kPrecomp) {
210 if (num_used_layers > 64) {
211 ALOGE("Failed to separate layers because there are more than 64");
215 // Index at which the actual layers begin
216 size_t layer_offset = num_exclude_rects + dedicated_layers.size();
217 if (num_used_layers + layer_offset > 64) {
219 "Exclusion rectangles are being truncated to make the rectangle count "
221 num_exclude_rects = 64 - num_used_layers - dedicated_layers.size();
224 // We inject all the exclude rects into the rects list. Any resulting rect
225 // that includes ANY of the first num_exclude_rects is rejected. After the
226 // exclude rects, we add the lower layers. The rects that intersect with
227 // these layers will be inspected and only those which are to be composited
228 // above the layer will be included in the composition regions.
229 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + layer_offset);
230 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
231 layer_rects.begin());
233 dedicated_layers.begin(), dedicated_layers.end(),
234 layer_rects.begin() + num_exclude_rects,
235 [=](size_t layer_index) { return layers_[layer_index].display_frame; });
236 std::transform(used_layers, used_layers + num_used_layers,
237 layer_rects.begin() + layer_offset, [=](size_t layer_index) {
238 return layers_[layer_index].display_frame;
241 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
242 separate_rects::separate_rects_64(layer_rects, &separate_regions);
243 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
244 uint64_t dedicated_mask = (((uint64_t)1 << dedicated_layers.size()) - 1)
245 << num_exclude_rects;
247 for (separate_rects::RectSet<uint64_t, int> ®ion : separate_regions) {
248 if (region.id_set.getBits() & exclude_mask)
251 // If a rect intersects one of the dedicated layers, we need to remove the
252 // layers from the composition region which appear *below* the dedicated
253 // layer. This effectively punches a hole through the composition layer such
254 // that the dedicated layer can be placed below the composition and not
256 uint64_t dedicated_intersect = region.id_set.getBits() & dedicated_mask;
257 for (size_t i = 0; dedicated_intersect && i < dedicated_layers.size();
259 // Only exclude layers if they intersect this particular dedicated layer
260 if (!(dedicated_intersect & (1 << (i + num_exclude_rects))))
263 for (size_t j = 0; j < num_used_layers; ++j) {
264 if (used_layers[j] < dedicated_layers[i])
265 region.id_set.subtract(j + layer_offset);
268 if (!(region.id_set.getBits() >> layer_offset))
271 pre_comp_regions_.emplace_back(DrmCompositionRegion{
273 SetBitsToVector(region.id_set.getBits() >> layer_offset, used_layers)});
277 int DrmDisplayComposition::CreateAndAssignReleaseFences() {
278 std::unordered_set<DrmHwcLayer *> squash_layers;
279 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
280 std::unordered_set<DrmHwcLayer *> comp_layers;
282 for (const DrmCompositionRegion ®ion : squash_regions_) {
283 for (size_t source_layer_index : region.source_layers) {
284 DrmHwcLayer *source_layer = &layers_[source_layer_index];
285 squash_layers.emplace(source_layer);
289 for (const DrmCompositionRegion ®ion : pre_comp_regions_) {
290 for (size_t source_layer_index : region.source_layers) {
291 DrmHwcLayer *source_layer = &layers_[source_layer_index];
292 pre_comp_layers.emplace(source_layer);
293 squash_layers.erase(source_layer);
297 for (const DrmCompositionPlane &plane : composition_planes_) {
298 if (plane.type() == DrmCompositionPlane::Type::kLayer) {
299 for (auto i : plane.source_layers()) {
300 DrmHwcLayer *source_layer = &layers_[i];
301 comp_layers.emplace(source_layer);
302 pre_comp_layers.erase(source_layer);
307 for (DrmHwcLayer *layer : squash_layers) {
308 if (!layer->release_fence)
310 int ret = layer->release_fence.Set(CreateNextTimelineFence());
314 timeline_squash_done_ = timeline_;
316 for (DrmHwcLayer *layer : pre_comp_layers) {
317 if (!layer->release_fence)
319 int ret = layer->release_fence.Set(CreateNextTimelineFence());
323 timeline_pre_comp_done_ = timeline_;
325 for (DrmHwcLayer *layer : comp_layers) {
326 if (!layer->release_fence)
328 int ret = layer->release_fence.Set(CreateNextTimelineFence());
336 int DrmDisplayComposition::Plan(SquashState *squash,
337 std::vector<DrmPlane *> *primary_planes,
338 std::vector<DrmPlane *> *overlay_planes) {
339 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
342 size_t planes_can_use =
343 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
344 if (planes_can_use == 0) {
345 ALOGE("Display %d has no usable planes", crtc_->display());
349 bool use_squash_framebuffer = false;
350 // Used to determine which layers were entirely squashed
351 std::vector<int> layer_squash_area(layers_.size(), 0);
352 // Used to avoid rerendering regions that were squashed
353 std::vector<DrmHwcRect<int>> exclude_rects;
354 if (squash != NULL && planes_can_use >= 3) {
355 if (geometry_changed_) {
356 squash->Init(layers_.data(), layers_.size());
358 std::vector<bool> changed_regions;
359 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
361 std::vector<bool> stable_regions;
362 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
364 // Only if SOME region is stable
365 use_squash_framebuffer =
366 std::find(stable_regions.begin(), stable_regions.end(), true) !=
367 stable_regions.end();
369 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
371 // Changes in which regions are squashed triggers a rerender via
373 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
375 for (size_t region_index = 0; region_index < stable_regions.size();
377 const SquashState::Region ®ion = squash->regions()[region_index];
378 if (!stable_regions[region_index])
381 exclude_rects.emplace_back(region.rect);
384 squash_regions_.emplace_back();
385 squash_regions_.back().frame = region.rect;
388 int frame_area = region.rect.area();
389 // Source layers are sorted front to back i.e. top layer has lowest
391 for (size_t layer_index = layers_.size();
392 layer_index-- > 0; // Yes, I double checked this
393 /* See condition */) {
394 if (!region.layer_refs[layer_index])
396 layer_squash_area[layer_index] += frame_area;
398 squash_regions_.back().source_layers.push_back(layer_index);
404 // All protected layers get first usage of planes
405 std::vector<size_t> layers_remaining;
406 std::vector<size_t> protected_layers;
407 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
408 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
409 layers_remaining.push_back(layer_index);
412 protected_layers.push_back(layer_index);
416 if (planes_can_use == 0 && layers_remaining.size() > 0) {
417 for (auto i : protected_layers)
418 EmplaceCompositionPlane(i, primary_planes, overlay_planes);
420 ALOGE("Protected layers consumed all hardware planes");
421 return CreateAndAssignReleaseFences();
424 std::vector<size_t> layers_remaining_if_squash;
425 for (size_t layer_index : layers_remaining) {
426 if (layer_squash_area[layer_index] <
427 layers_[layer_index].display_frame.area())
428 layers_remaining_if_squash.push_back(layer_index);
431 if (use_squash_framebuffer) {
432 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
433 layers_remaining = std::move(layers_remaining_if_squash);
434 planes_can_use--; // Reserve plane for squashing
436 use_squash_framebuffer = false; // The squash buffer is still rendered
440 if (layers_remaining.size() > planes_can_use)
441 planes_can_use--; // Reserve one for pre-compositing
443 // Whatever planes that are not reserved get assigned a layer
444 size_t last_hw_comp_layer = 0;
445 size_t protected_idx = 0;
446 while(last_hw_comp_layer < layers_remaining.size() && planes_can_use > 0) {
447 size_t idx = layers_remaining[last_hw_comp_layer];
449 // Put the protected layers into the composition at the right place. We've
450 // already reserved them by decrementing planes_can_use, so no need to do
452 if (protected_idx < protected_layers.size() &&
453 idx > protected_layers[protected_idx]) {
454 EmplaceCompositionPlane(protected_layers[protected_idx], primary_planes,
460 EmplaceCompositionPlane(layers_remaining[last_hw_comp_layer],
461 primary_planes, overlay_planes);
462 last_hw_comp_layer++;
466 layers_remaining.erase(layers_remaining.begin(),
467 layers_remaining.begin() + last_hw_comp_layer);
469 // Enqueue the rest of the protected layers (if any) between the hw composited
470 // overlay layers and the squash/precomp layers.
471 for (size_t i = protected_idx; i < protected_layers.size(); ++i)
472 EmplaceCompositionPlane(protected_layers[i], primary_planes,
475 if (layers_remaining.size() > 0) {
476 EmplaceCompositionPlane(DrmCompositionPlane::Type::kPrecomp, primary_planes,
478 SeparateLayers(layers_remaining.data(), layers_remaining.size(),
479 exclude_rects.data(), exclude_rects.size());
482 if (use_squash_framebuffer) {
483 EmplaceCompositionPlane(DrmCompositionPlane::Type::kSquash, primary_planes,
487 return CreateAndAssignReleaseFences();
490 static const char *DrmCompositionTypeToString(DrmCompositionType type) {
492 case DRM_COMPOSITION_TYPE_EMPTY:
494 case DRM_COMPOSITION_TYPE_FRAME:
496 case DRM_COMPOSITION_TYPE_DPMS:
498 case DRM_COMPOSITION_TYPE_MODESET:
505 static const char *DPMSModeToString(int dpms_mode) {
507 case DRM_MODE_DPMS_ON:
509 case DRM_MODE_DPMS_OFF:
516 static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
518 *out << "buffer=<invalid>";
522 *out << "buffer[w/h/format]=";
523 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
526 static void DumpTransform(uint32_t transform, std::ostringstream *out) {
532 bool separator = false;
533 if (transform & DrmHwcTransform::kFlipH) {
537 if (transform & DrmHwcTransform::kFlipV) {
543 if (transform & DrmHwcTransform::kRotate90) {
549 if (transform & DrmHwcTransform::kRotate180) {
555 if (transform & DrmHwcTransform::kRotate270) {
562 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
563 DrmHwcTransform::kRotate90 |
564 DrmHwcTransform::kRotate180 |
565 DrmHwcTransform::kRotate270;
566 if (transform & ~valid_bits) {
574 static const char *BlendingToString(DrmHwcBlending blending) {
576 case DrmHwcBlending::kNone:
578 case DrmHwcBlending::kPreMult:
580 case DrmHwcBlending::kCoverage:
587 static void DumpRegion(const DrmCompositionRegion ®ion,
588 std::ostringstream *out) {
590 region.frame.Dump(out);
591 *out << " source_layers=(";
593 const std::vector<size_t> &source_layers = region.source_layers;
594 for (size_t i = 0; i < source_layers.size(); i++) {
595 *out << source_layers[i];
596 if (i < source_layers.size() - 1) {
604 void DrmDisplayComposition::Dump(std::ostringstream *out) const {
605 *out << "----DrmDisplayComposition"
606 << " crtc=" << (crtc_ ? crtc_->id() : -1)
607 << " type=" << DrmCompositionTypeToString(type_);
610 case DRM_COMPOSITION_TYPE_DPMS:
611 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
613 case DRM_COMPOSITION_TYPE_MODESET:
614 *out << " display_mode=" << display_mode_.h_display() << "x"
615 << display_mode_.v_display();
621 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
622 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
623 << timeline_ << "\n";
625 *out << " Layers: count=" << layers_.size() << "\n";
626 for (size_t i = 0; i < layers_.size(); i++) {
627 const DrmHwcLayer &layer = layers_[i];
628 *out << " [" << i << "] ";
630 DumpBuffer(layer.buffer, out);
632 if (layer.protected_usage())
633 *out << " protected";
635 *out << " transform=";
636 DumpTransform(layer.transform, out);
637 *out << " blending[a=" << (int)layer.alpha
638 << "]=" << BlendingToString(layer.blending) << " source_crop";
639 layer.source_crop.Dump(out);
640 *out << " display_frame";
641 layer.display_frame.Dump(out);
646 *out << " Planes: count=" << composition_planes_.size() << "\n";
647 for (size_t i = 0; i < composition_planes_.size(); i++) {
648 const DrmCompositionPlane &comp_plane = composition_planes_[i];
649 *out << " [" << i << "]"
650 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
652 switch (comp_plane.type()) {
653 case DrmCompositionPlane::Type::kDisable:
656 case DrmCompositionPlane::Type::kLayer:
659 case DrmCompositionPlane::Type::kPrecomp:
662 case DrmCompositionPlane::Type::kSquash:
670 *out << " source_layer=";
671 for (auto i : comp_plane.source_layers()) {
677 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
678 for (size_t i = 0; i < squash_regions_.size(); i++) {
679 *out << " [" << i << "] ";
680 DumpRegion(squash_regions_[i], out);
684 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
685 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
686 *out << " [" << i << "] ";
687 DumpRegion(pre_comp_regions_[i], out);