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 const size_t DrmCompositionPlane::kSourceNone;
37 const size_t DrmCompositionPlane::kSourcePreComp;
38 const size_t DrmCompositionPlane::kSourceSquash;
39 const size_t DrmCompositionPlane::kSourceLayerMax;
41 DrmDisplayComposition::~DrmDisplayComposition() {
42 if (timeline_fd_ >= 0) {
43 SignalCompositionDone();
48 int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
49 Importer *importer, uint64_t frame_no) {
51 crtc_ = crtc; // Can be NULL if we haven't modeset yet
55 int ret = sw_sync_timeline_create();
57 ALOGE("Failed to create sw sync timeline %d", ret);
64 bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
65 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
68 int DrmDisplayComposition::CreateNextTimelineFence() {
70 return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
74 int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
75 int timeline_increase = point - timeline_current_;
76 if (timeline_increase <= 0)
79 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
81 ALOGE("Failed to increment sync timeline %d", ret);
83 timeline_current_ = point;
88 int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
89 bool geometry_changed) {
90 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
93 geometry_changed_ = geometry_changed;
95 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
96 layers_.emplace_back(std::move(layers[layer_index]));
99 type_ = DRM_COMPOSITION_TYPE_FRAME;
103 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
104 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
106 dpms_mode_ = dpms_mode;
107 type_ = DRM_COMPOSITION_TYPE_DPMS;
111 int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
112 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
114 display_mode_ = display_mode;
115 dpms_mode_ = DRM_MODE_DPMS_ON;
116 type_ = DRM_COMPOSITION_TYPE_MODESET;
120 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
121 composition_planes_.emplace_back(
122 DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
126 static size_t CountUsablePlanes(DrmCrtc *crtc,
127 std::vector<DrmPlane *> *primary_planes,
128 std::vector<DrmPlane *> *overlay_planes) {
129 return std::count_if(
130 primary_planes->begin(), primary_planes->end(),
131 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); }) +
133 overlay_planes->begin(), overlay_planes->end(),
134 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
137 static DrmPlane *TakePlane(DrmCrtc *crtc, std::vector<DrmPlane *> *planes) {
138 for (auto iter = planes->begin(); iter != planes->end(); ++iter) {
139 if ((*iter)->GetCrtcSupported(*crtc)) {
140 DrmPlane *plane = *iter;
148 static DrmPlane *TakePlane(DrmCrtc *crtc,
149 std::vector<DrmPlane *> *primary_planes,
150 std::vector<DrmPlane *> *overlay_planes) {
151 DrmPlane *plane = TakePlane(crtc, primary_planes);
154 return TakePlane(crtc, overlay_planes);
157 void DrmDisplayComposition::EmplaceCompositionPlane(
158 size_t source_layer, std::vector<DrmPlane *> *primary_planes,
159 std::vector<DrmPlane *> *overlay_planes) {
160 DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
163 "Failed to add composition plane because there are no planes "
167 composition_planes_.emplace_back(
168 DrmCompositionPlane{plane, crtc_, source_layer});
171 static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
172 std::vector<size_t> out;
173 size_t msb = sizeof(in) * 8 - 1;
174 uint64_t mask = (uint64_t)1 << msb;
175 for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
177 out.push_back(index_map[i]);
181 static void SeparateLayers(DrmHwcLayer *layers, size_t *used_layers,
182 size_t num_used_layers,
183 size_t *protected_layers,
184 size_t num_protected_layers,
185 DrmHwcRect<int> *exclude_rects,
186 size_t num_exclude_rects,
187 std::vector<DrmCompositionRegion> ®ions) {
188 if (num_used_layers > 64) {
189 ALOGE("Failed to separate layers because there are more than 64");
193 // Index at which the actual layers begin
194 size_t layer_offset = num_exclude_rects + num_protected_layers;
196 if (num_used_layers + layer_offset > 64) {
198 "Exclusion rectangles are being truncated to make the rectangle count "
200 num_exclude_rects = 64 - num_used_layers - num_protected_layers;
203 // We inject all the exclude rects into the rects list. Any resulting rect
204 // that includes ANY of the first num_exclude_rects is rejected. After the
205 // exclude rects, we add the protected layers. The rects that intersect with
206 // the protected layer will be inspected and only those which are above the
207 // protected layer will be included in the composition regions.
208 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + layer_offset);
209 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
210 layer_rects.begin());
212 protected_layers, protected_layers + num_protected_layers,
213 layer_rects.begin() + num_exclude_rects,
214 [=](size_t layer_index) { return layers[layer_index].display_frame; });
216 used_layers, used_layers + num_used_layers,
217 layer_rects.begin() + layer_offset,
218 [=](size_t layer_index) { return layers[layer_index].display_frame; });
220 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
221 separate_rects::separate_rects_64(layer_rects, &separate_regions);
222 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
223 uint64_t protected_mask = (((uint64_t)1 << num_protected_layers) - 1) <<
226 for (separate_rects::RectSet<uint64_t, int> ®ion : separate_regions) {
227 if (region.id_set.getBits() & exclude_mask)
230 // If a rect intersects a protected layer, we need to remove the layers
231 // from the composition region which appear *below* the protected layer.
232 // This effectively punches a hole through the composition layer such
233 // that the protected layer can be placed below the composition and not
234 // be occluded by things like the background.
235 uint64_t protected_intersect = region.id_set.getBits() & protected_mask;
236 for (size_t i = 0; protected_intersect && i < num_protected_layers; ++i) {
237 // Only exclude layers if they intersect this particular protected layer
238 if (!(protected_intersect & (1 << (i + num_exclude_rects))))
241 for (size_t j = 0; j < num_used_layers; ++j) {
242 if (used_layers[j] < protected_layers[i])
243 region.id_set.subtract(j + layer_offset);
246 if (!(region.id_set.getBits() >> layer_offset))
249 regions.emplace_back(DrmCompositionRegion{
251 SetBitsToVector(region.id_set.getBits() >> layer_offset, used_layers)});
255 int DrmDisplayComposition::CreateAndAssignReleaseFences() {
256 std::unordered_set<DrmHwcLayer *> squash_layers;
257 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
258 std::unordered_set<DrmHwcLayer *> comp_layers;
260 for (const DrmCompositionRegion ®ion : squash_regions_) {
261 for (size_t source_layer_index : region.source_layers) {
262 DrmHwcLayer *source_layer = &layers_[source_layer_index];
263 squash_layers.emplace(source_layer);
267 for (const DrmCompositionRegion ®ion : pre_comp_regions_) {
268 for (size_t source_layer_index : region.source_layers) {
269 DrmHwcLayer *source_layer = &layers_[source_layer_index];
270 pre_comp_layers.emplace(source_layer);
271 squash_layers.erase(source_layer);
275 for (const DrmCompositionPlane &plane : composition_planes_) {
276 if (plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
277 DrmHwcLayer *source_layer = &layers_[plane.source_layer];
278 comp_layers.emplace(source_layer);
279 pre_comp_layers.erase(source_layer);
283 for (DrmHwcLayer *layer : squash_layers) {
284 if (!layer->release_fence)
286 int ret = layer->release_fence.Set(CreateNextTimelineFence());
290 timeline_squash_done_ = timeline_;
292 for (DrmHwcLayer *layer : pre_comp_layers) {
293 if (!layer->release_fence)
295 int ret = layer->release_fence.Set(CreateNextTimelineFence());
299 timeline_pre_comp_done_ = timeline_;
301 for (DrmHwcLayer *layer : comp_layers) {
302 if (!layer->release_fence)
304 int ret = layer->release_fence.Set(CreateNextTimelineFence());
312 int DrmDisplayComposition::Plan(SquashState *squash,
313 std::vector<DrmPlane *> *primary_planes,
314 std::vector<DrmPlane *> *overlay_planes) {
315 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
318 size_t planes_can_use =
319 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
320 if (planes_can_use == 0) {
321 ALOGE("Display %d has no usable planes", crtc_->display());
325 bool use_squash_framebuffer = false;
326 // Used to determine which layers were entirely squashed
327 std::vector<int> layer_squash_area(layers_.size(), 0);
328 // Used to avoid rerendering regions that were squashed
329 std::vector<DrmHwcRect<int>> exclude_rects;
330 if (squash != NULL && planes_can_use >= 3) {
331 if (geometry_changed_) {
332 squash->Init(layers_.data(), layers_.size());
334 std::vector<bool> changed_regions;
335 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
337 std::vector<bool> stable_regions;
338 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
340 // Only if SOME region is stable
341 use_squash_framebuffer =
342 std::find(stable_regions.begin(), stable_regions.end(), true) !=
343 stable_regions.end();
345 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
347 // Changes in which regions are squashed triggers a rerender via
349 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
351 for (size_t region_index = 0; region_index < stable_regions.size();
353 const SquashState::Region ®ion = squash->regions()[region_index];
354 if (!stable_regions[region_index])
357 exclude_rects.emplace_back(region.rect);
360 squash_regions_.emplace_back();
361 squash_regions_.back().frame = region.rect;
364 int frame_area = region.rect.area();
365 // Source layers are sorted front to back i.e. top layer has lowest
367 for (size_t layer_index = layers_.size();
368 layer_index-- > 0; // Yes, I double checked this
369 /* See condition */) {
370 if (!region.layer_refs[layer_index])
372 layer_squash_area[layer_index] += frame_area;
374 squash_regions_.back().source_layers.push_back(layer_index);
380 // All protected layers get first usage of planes
381 std::vector<size_t> layers_remaining;
382 std::vector<size_t> protected_layers;
383 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
384 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
385 layers_remaining.push_back(layer_index);
388 protected_layers.push_back(layer_index);
392 if (planes_can_use == 0 && layers_remaining.size() > 0) {
393 for(auto i : protected_layers)
394 EmplaceCompositionPlane(i, primary_planes, overlay_planes);
396 ALOGE("Protected layers consumed all hardware planes");
397 return CreateAndAssignReleaseFences();
400 std::vector<size_t> layers_remaining_if_squash;
401 for (size_t layer_index : layers_remaining) {
402 if (layer_squash_area[layer_index] <
403 layers_[layer_index].display_frame.area())
404 layers_remaining_if_squash.push_back(layer_index);
407 if (use_squash_framebuffer) {
408 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
409 layers_remaining = std::move(layers_remaining_if_squash);
410 planes_can_use--; // Reserve plane for squashing
412 use_squash_framebuffer = false; // The squash buffer is still rendered
416 if (layers_remaining.size() > planes_can_use)
417 planes_can_use--; // Reserve one for pre-compositing
419 // Whatever planes that are not reserved get assigned a layer
420 size_t last_hw_comp_layer = 0;
421 size_t protected_idx = 0;
422 while(last_hw_comp_layer < layers_remaining.size() && planes_can_use > 0) {
423 size_t idx = layers_remaining[last_hw_comp_layer];
425 // Put the protected layers into the composition at the right place. We've
426 // already reserved them by decrementing planes_can_use, so no need to do
428 if (protected_idx < protected_layers.size() &&
429 idx > protected_layers[protected_idx]) {
430 EmplaceCompositionPlane(protected_layers[protected_idx], primary_planes,
436 EmplaceCompositionPlane(layers_remaining[last_hw_comp_layer],
437 primary_planes, overlay_planes);
438 last_hw_comp_layer++;
442 layers_remaining.erase(layers_remaining.begin(),
443 layers_remaining.begin() + last_hw_comp_layer);
445 // Enqueue the rest of the protected layers (if any) between the hw composited
446 // overlay layers and the squash/precomp layers.
447 for(int i = protected_idx; i < protected_layers.size(); ++i)
448 EmplaceCompositionPlane(protected_layers[i], primary_planes,
451 if (layers_remaining.size() > 0) {
452 EmplaceCompositionPlane(DrmCompositionPlane::kSourcePreComp, primary_planes,
454 SeparateLayers(layers_.data(), layers_remaining.data(),
455 layers_remaining.size(), protected_layers.data(),
456 protected_layers.size(), exclude_rects.data(),
457 exclude_rects.size(), pre_comp_regions_);
460 if (use_squash_framebuffer) {
461 EmplaceCompositionPlane(DrmCompositionPlane::kSourceSquash, primary_planes,
465 return CreateAndAssignReleaseFences();
468 static const char *DrmCompositionTypeToString(DrmCompositionType type) {
470 case DRM_COMPOSITION_TYPE_EMPTY:
472 case DRM_COMPOSITION_TYPE_FRAME:
474 case DRM_COMPOSITION_TYPE_DPMS:
476 case DRM_COMPOSITION_TYPE_MODESET:
483 static const char *DPMSModeToString(int dpms_mode) {
485 case DRM_MODE_DPMS_ON:
487 case DRM_MODE_DPMS_OFF:
494 static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
496 *out << "buffer=<invalid>";
500 *out << "buffer[w/h/format]=";
501 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
504 static void DumpTransform(uint32_t transform, std::ostringstream *out) {
510 bool separator = false;
511 if (transform & DrmHwcTransform::kFlipH) {
515 if (transform & DrmHwcTransform::kFlipV) {
521 if (transform & DrmHwcTransform::kRotate90) {
527 if (transform & DrmHwcTransform::kRotate180) {
533 if (transform & DrmHwcTransform::kRotate270) {
540 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
541 DrmHwcTransform::kRotate90 |
542 DrmHwcTransform::kRotate180 |
543 DrmHwcTransform::kRotate270;
544 if (transform & ~valid_bits) {
552 static const char *BlendingToString(DrmHwcBlending blending) {
554 case DrmHwcBlending::kNone:
556 case DrmHwcBlending::kPreMult:
558 case DrmHwcBlending::kCoverage:
565 static void DumpRegion(const DrmCompositionRegion ®ion,
566 std::ostringstream *out) {
568 region.frame.Dump(out);
569 *out << " source_layers=(";
571 const std::vector<size_t> &source_layers = region.source_layers;
572 for (size_t i = 0; i < source_layers.size(); i++) {
573 *out << source_layers[i];
574 if (i < source_layers.size() - 1) {
582 void DrmDisplayComposition::Dump(std::ostringstream *out) const {
583 *out << "----DrmDisplayComposition"
584 << " crtc=" << (crtc_ ? crtc_->id() : -1)
585 << " type=" << DrmCompositionTypeToString(type_);
588 case DRM_COMPOSITION_TYPE_DPMS:
589 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
591 case DRM_COMPOSITION_TYPE_MODESET:
592 *out << " display_mode=" << display_mode_.h_display() << "x"
593 << display_mode_.v_display();
599 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
600 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
601 << timeline_ << "\n";
603 *out << " Layers: count=" << layers_.size() << "\n";
604 for (size_t i = 0; i < layers_.size(); i++) {
605 const DrmHwcLayer &layer = layers_[i];
606 *out << " [" << i << "] ";
608 DumpBuffer(layer.buffer, out);
610 if (layer.protected_usage())
611 *out << " protected";
613 *out << " transform=";
614 DumpTransform(layer.transform, out);
615 *out << " blending[a=" << (int)layer.alpha
616 << "]=" << BlendingToString(layer.blending) << " source_crop";
617 layer.source_crop.Dump(out);
618 *out << " display_frame";
619 layer.display_frame.Dump(out);
624 *out << " Planes: count=" << composition_planes_.size() << "\n";
625 for (size_t i = 0; i < composition_planes_.size(); i++) {
626 const DrmCompositionPlane &comp_plane = composition_planes_[i];
627 *out << " [" << i << "]"
628 << " plane=" << (comp_plane.plane ? comp_plane.plane->id() : -1)
630 if (comp_plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
631 *out << comp_plane.source_layer;
633 switch (comp_plane.source_layer) {
634 case DrmCompositionPlane::kSourceNone:
637 case DrmCompositionPlane::kSourcePreComp:
640 case DrmCompositionPlane::kSourceSquash:
652 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
653 for (size_t i = 0; i < squash_regions_.size(); i++) {
654 *out << " [" << i << "] ";
655 DumpRegion(squash_regions_[i], out);
659 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
660 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
661 *out << " [" << i << "] ";
662 DumpRegion(pre_comp_regions_[i], out);