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) {
90 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
93 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
94 layers_.emplace_back(std::move(layers[layer_index]));
97 type_ = DRM_COMPOSITION_TYPE_FRAME;
101 int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
102 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
104 dpms_mode_ = dpms_mode;
105 type_ = DRM_COMPOSITION_TYPE_DPMS;
109 int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
110 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
112 display_mode_ = display_mode;
113 dpms_mode_ = DRM_MODE_DPMS_ON;
114 type_ = DRM_COMPOSITION_TYPE_MODESET;
118 int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
119 composition_planes_.emplace_back(
120 DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
124 static 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); }) +
131 overlay_planes->begin(), overlay_planes->end(),
132 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
135 static 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;
146 static DrmPlane *TakePlane(DrmCrtc *crtc,
147 std::vector<DrmPlane *> *primary_planes,
148 std::vector<DrmPlane *> *overlay_planes) {
149 DrmPlane *plane = TakePlane(crtc, primary_planes);
152 return TakePlane(crtc, overlay_planes);
155 static 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)
161 out.push_back(index_map[i]);
165 static 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> ®ions) {
170 if (num_used_layers > 64) {
171 ALOGE("Failed to separate layers because there are more than 64");
175 if (num_used_layers + num_exclude_rects > 64) {
177 "Exclusion rectangles are being truncated to make the rectangle count "
179 num_exclude_rects = 64 - num_used_layers;
182 // 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());
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; });
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;
196 for (seperate_rects::RectSet<uint64_t, int> ®ion : seperate_regions) {
197 if (region.id_set.getBits() & exclude_mask)
199 regions.emplace_back(DrmCompositionRegion{
201 SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
206 int 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());
216 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);
221 std::vector<bool> stable_regions;
222 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
224 squash->RecordHistory(layers_.data(), changed_regions);
226 squash->RecordSquashed(stable_regions);
228 for (size_t region_index = 0; region_index < stable_regions.size();
230 const SquashState::Region ®ion = 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;
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();
246 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()) {
254 layers_remaining.push_back(layer_index);
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);
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);
277 for (size_t i : layers_remaining) {
278 composition_planes_.emplace_back(DrmCompositionPlane{
279 TakePlane(crtc_, primary_planes, overlay_planes), crtc_, i});
282 std::unordered_set<DrmHwcLayer *> squash_layers;
283 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
284 std::unordered_set<DrmHwcLayer *> comp_layers;
286 for (const DrmCompositionRegion ®ion : 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);
293 for (const DrmCompositionRegion ®ion : 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);
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);
309 for (DrmHwcLayer *layer : squash_layers) {
310 int ret = layer->release_fence.Set(CreateNextTimelineFence());
314 timeline_squash_done_ = timeline_;
316 for (DrmHwcLayer *layer : pre_comp_layers) {
317 int ret = layer->release_fence.Set(CreateNextTimelineFence());
321 timeline_pre_comp_done_ = timeline_;
323 for (DrmHwcLayer *layer : comp_layers) {
324 int ret = layer->release_fence.Set(CreateNextTimelineFence());