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 DrmHwcRect<int> *exclude_rects,
184 size_t num_exclude_rects,
185 std::vector<DrmCompositionRegion> ®ions) {
186 if (num_used_layers > 64) {
187 ALOGE("Failed to separate layers because there are more than 64");
191 if (num_used_layers + num_exclude_rects > 64) {
193 "Exclusion rectangles are being truncated to make the rectangle count "
195 num_exclude_rects = 64 - num_used_layers;
198 // We inject all the exclude rects into the rects list. Any resulting rect
199 // that includes ANY of the first num_exclude_rects is rejected.
200 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + num_exclude_rects);
201 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
202 layer_rects.begin());
204 used_layers, used_layers + num_used_layers,
205 layer_rects.begin() + num_exclude_rects,
206 [=](size_t layer_index) { return layers[layer_index].display_frame; });
208 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
209 separate_rects::separate_rects_64(layer_rects, &separate_regions);
210 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
212 for (separate_rects::RectSet<uint64_t, int> ®ion : separate_regions) {
213 if (region.id_set.getBits() & exclude_mask)
215 regions.emplace_back(DrmCompositionRegion{
217 SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
222 int DrmDisplayComposition::CreateAndAssignReleaseFences() {
223 std::unordered_set<DrmHwcLayer *> squash_layers;
224 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
225 std::unordered_set<DrmHwcLayer *> comp_layers;
227 for (const DrmCompositionRegion ®ion : squash_regions_) {
228 for (size_t source_layer_index : region.source_layers) {
229 DrmHwcLayer *source_layer = &layers_[source_layer_index];
230 squash_layers.emplace(source_layer);
234 for (const DrmCompositionRegion ®ion : pre_comp_regions_) {
235 for (size_t source_layer_index : region.source_layers) {
236 DrmHwcLayer *source_layer = &layers_[source_layer_index];
237 pre_comp_layers.emplace(source_layer);
238 squash_layers.erase(source_layer);
242 for (const DrmCompositionPlane &plane : composition_planes_) {
243 if (plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
244 DrmHwcLayer *source_layer = &layers_[plane.source_layer];
245 comp_layers.emplace(source_layer);
246 pre_comp_layers.erase(source_layer);
250 for (DrmHwcLayer *layer : squash_layers) {
251 if (!layer->release_fence)
253 int ret = layer->release_fence.Set(CreateNextTimelineFence());
257 timeline_squash_done_ = timeline_;
259 for (DrmHwcLayer *layer : pre_comp_layers) {
260 if (!layer->release_fence)
262 int ret = layer->release_fence.Set(CreateNextTimelineFence());
266 timeline_pre_comp_done_ = timeline_;
268 for (DrmHwcLayer *layer : comp_layers) {
269 if (!layer->release_fence)
271 int ret = layer->release_fence.Set(CreateNextTimelineFence());
279 int DrmDisplayComposition::Plan(SquashState *squash,
280 std::vector<DrmPlane *> *primary_planes,
281 std::vector<DrmPlane *> *overlay_planes) {
282 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
285 size_t planes_can_use =
286 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
287 if (planes_can_use == 0) {
288 ALOGE("Display %d has no usable planes", crtc_->display());
292 bool use_squash_framebuffer = false;
293 // Used to determine which layers were entirely squashed
294 std::vector<int> layer_squash_area(layers_.size(), 0);
295 // Used to avoid rerendering regions that were squashed
296 std::vector<DrmHwcRect<int>> exclude_rects;
297 if (squash != NULL && planes_can_use >= 3) {
298 if (geometry_changed_) {
299 squash->Init(layers_.data(), layers_.size());
301 std::vector<bool> changed_regions;
302 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
304 std::vector<bool> stable_regions;
305 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
307 // Only if SOME region is stable
308 use_squash_framebuffer =
309 std::find(stable_regions.begin(), stable_regions.end(), true) !=
310 stable_regions.end();
312 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
314 // Changes in which regions are squashed triggers a rerender via
316 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
318 for (size_t region_index = 0; region_index < stable_regions.size();
320 const SquashState::Region ®ion = squash->regions()[region_index];
321 if (!stable_regions[region_index])
324 exclude_rects.emplace_back(region.rect);
327 squash_regions_.emplace_back();
328 squash_regions_.back().frame = region.rect;
331 int frame_area = region.rect.area();
332 // Source layers are sorted front to back i.e. top layer has lowest
334 for (size_t layer_index = layers_.size();
335 layer_index-- > 0; // Yes, I double checked this
336 /* See condition */) {
337 if (!region.layer_refs[layer_index])
339 layer_squash_area[layer_index] += frame_area;
341 squash_regions_.back().source_layers.push_back(layer_index);
347 // All protected layers get first usage of planes
348 std::vector<size_t> layers_remaining;
349 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
350 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
351 layers_remaining.push_back(layer_index);
354 EmplaceCompositionPlane(layer_index, primary_planes, overlay_planes);
358 if (planes_can_use == 0 && layers_remaining.size() > 0) {
359 ALOGE("Protected layers consumed all hardware planes");
360 return CreateAndAssignReleaseFences();
363 std::vector<size_t> layers_remaining_if_squash;
364 for (size_t layer_index : layers_remaining) {
365 if (layer_squash_area[layer_index] <
366 layers_[layer_index].display_frame.area())
367 layers_remaining_if_squash.push_back(layer_index);
370 if (use_squash_framebuffer) {
371 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
372 layers_remaining = std::move(layers_remaining_if_squash);
373 planes_can_use--; // Reserve plane for squashing
375 use_squash_framebuffer = false; // The squash buffer is still rendered
379 if (layers_remaining.size() > planes_can_use)
380 planes_can_use--; // Reserve one for pre-compositing
382 // Whatever planes that are not reserved get assigned a layer
383 size_t last_composition_layer = 0;
384 for (last_composition_layer = 0;
385 last_composition_layer < layers_remaining.size() && planes_can_use > 0;
386 last_composition_layer++, planes_can_use--) {
387 EmplaceCompositionPlane(layers_remaining[last_composition_layer],
388 primary_planes, overlay_planes);
391 layers_remaining.erase(layers_remaining.begin(),
392 layers_remaining.begin() + last_composition_layer);
394 if (layers_remaining.size() > 0) {
395 EmplaceCompositionPlane(DrmCompositionPlane::kSourcePreComp, primary_planes,
397 SeparateLayers(layers_.data(), layers_remaining.data(),
398 layers_remaining.size(), exclude_rects.data(),
399 exclude_rects.size(), pre_comp_regions_);
402 if (use_squash_framebuffer) {
403 EmplaceCompositionPlane(DrmCompositionPlane::kSourceSquash, primary_planes,
407 return CreateAndAssignReleaseFences();
410 static const char *DrmCompositionTypeToString(DrmCompositionType type) {
412 case DRM_COMPOSITION_TYPE_EMPTY:
414 case DRM_COMPOSITION_TYPE_FRAME:
416 case DRM_COMPOSITION_TYPE_DPMS:
418 case DRM_COMPOSITION_TYPE_MODESET:
425 static const char *DPMSModeToString(int dpms_mode) {
427 case DRM_MODE_DPMS_ON:
429 case DRM_MODE_DPMS_OFF:
436 static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
438 *out << "buffer=<invalid>";
442 *out << "buffer[w/h/format]=";
443 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
446 static void DumpTransform(uint32_t transform, std::ostringstream *out) {
452 bool separator = false;
453 if (transform & DrmHwcTransform::kFlipH) {
457 if (transform & DrmHwcTransform::kFlipV) {
463 if (transform & DrmHwcTransform::kRotate90) {
469 if (transform & DrmHwcTransform::kRotate180) {
475 if (transform & DrmHwcTransform::kRotate270) {
482 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
483 DrmHwcTransform::kRotate90 |
484 DrmHwcTransform::kRotate180 |
485 DrmHwcTransform::kRotate270;
486 if (transform & ~valid_bits) {
494 static const char *BlendingToString(DrmHwcBlending blending) {
496 case DrmHwcBlending::kNone:
498 case DrmHwcBlending::kPreMult:
500 case DrmHwcBlending::kCoverage:
507 static void DumpRegion(const DrmCompositionRegion ®ion,
508 std::ostringstream *out) {
510 region.frame.Dump(out);
511 *out << " source_layers=(";
513 const std::vector<size_t> &source_layers = region.source_layers;
514 for (size_t i = 0; i < source_layers.size(); i++) {
515 *out << source_layers[i];
516 if (i < source_layers.size() - 1) {
524 void DrmDisplayComposition::Dump(std::ostringstream *out) const {
525 *out << "----DrmDisplayComposition"
526 << " crtc=" << (crtc_ ? crtc_->id() : -1)
527 << " type=" << DrmCompositionTypeToString(type_);
530 case DRM_COMPOSITION_TYPE_DPMS:
531 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
533 case DRM_COMPOSITION_TYPE_MODESET:
534 *out << " display_mode=" << display_mode_.h_display() << "x"
535 << display_mode_.v_display();
541 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
542 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
543 << timeline_ << "\n";
545 *out << " Layers: count=" << layers_.size() << "\n";
546 for (size_t i = 0; i < layers_.size(); i++) {
547 const DrmHwcLayer &layer = layers_[i];
548 *out << " [" << i << "] ";
550 DumpBuffer(layer.buffer, out);
552 if (layer.protected_usage())
553 *out << " protected";
555 *out << " transform=";
556 DumpTransform(layer.transform, out);
557 *out << " blending[a=" << (int)layer.alpha
558 << "]=" << BlendingToString(layer.blending) << " source_crop";
559 layer.source_crop.Dump(out);
560 *out << " display_frame";
561 layer.display_frame.Dump(out);
566 *out << " Planes: count=" << composition_planes_.size() << "\n";
567 for (size_t i = 0; i < composition_planes_.size(); i++) {
568 const DrmCompositionPlane &comp_plane = composition_planes_[i];
569 *out << " [" << i << "]"
570 << " plane=" << (comp_plane.plane ? comp_plane.plane->id() : -1)
572 if (comp_plane.source_layer <= DrmCompositionPlane::kSourceLayerMax) {
573 *out << comp_plane.source_layer;
575 switch (comp_plane.source_layer) {
576 case DrmCompositionPlane::kSourceNone:
579 case DrmCompositionPlane::kSourcePreComp:
582 case DrmCompositionPlane::kSourceSquash:
594 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
595 for (size_t i = 0; i < squash_regions_.size(); i++) {
596 *out << " [" << i << "] ";
597 DumpRegion(squash_regions_[i], out);
601 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
602 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
603 *out << " [" << i << "] ";
604 DumpRegion(pre_comp_regions_[i], out);