blob: d5450ff22922a0f365063fd44e7d22a75f8154a9 [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
Sean Paul98e73c82015-06-24 14:38:49 -070036DrmDisplayComposition::~DrmDisplayComposition() {
Zach Reiznerece04892015-07-17 14:13:28 -070037 if (timeline_fd_ >= 0) {
Zach Reizner92f8e632015-10-12 17:47:13 -070038 SignalCompositionDone();
Sean Paul98e73c82015-06-24 14:38:49 -070039 close(timeline_fd_);
Zach Reiznerece04892015-07-17 14:13:28 -070040 }
Sean Paul98e73c82015-06-24 14:38:49 -070041}
42
Zach Reizner09807052015-08-13 14:53:41 -070043int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
Sean Paulbdc67bf2015-09-21 10:04:02 -040044 Importer *importer, uint64_t frame_no) {
Sean Paul98e73c82015-06-24 14:38:49 -070045 drm_ = drm;
Zach Reizner4a253652015-09-10 18:30:54 -070046 crtc_ = crtc; // Can be NULL if we haven't modeset yet
Sean Paul98e73c82015-06-24 14:38:49 -070047 importer_ = importer;
Sean Paulbdc67bf2015-09-21 10:04:02 -040048 frame_no_ = frame_no;
Sean Paul98e73c82015-06-24 14:38:49 -070049
Zach Reizner4a253652015-09-10 18:30:54 -070050 int ret = sw_sync_timeline_create();
Sean Paul98e73c82015-06-24 14:38:49 -070051 if (ret < 0) {
52 ALOGE("Failed to create sw sync timeline %d", ret);
53 return ret;
54 }
55 timeline_fd_ = ret;
56 return 0;
57}
58
Sean Paulacb2a442015-06-24 18:43:01 -070059bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
60 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
61}
62
Zach Reizner92f8e632015-10-12 17:47:13 -070063int DrmDisplayComposition::CreateNextTimelineFence() {
64 ++timeline_;
65 return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
66 timeline_);
67}
68
69int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
70 int timeline_increase = point - timeline_current_;
71 if (timeline_increase <= 0)
72 return 0;
73
74 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
75 if (ret)
76 ALOGE("Failed to increment sync timeline %d", ret);
77 else
78 timeline_current_ = point;
79
80 return ret;
81}
82
Zach Reizner5757e822015-10-16 19:06:31 -070083int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers,
84 bool geometry_changed) {
Zach Reizner92f8e632015-10-12 17:47:13 -070085 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
86 return -EINVAL;
87
Zach Reizner5757e822015-10-16 19:06:31 -070088 geometry_changed_ = geometry_changed;
89
Zach Reizner92f8e632015-10-12 17:47:13 -070090 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
91 layers_.emplace_back(std::move(layers[layer_index]));
92 }
93
94 type_ = DRM_COMPOSITION_TYPE_FRAME;
95 return 0;
96}
97
98int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
99 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
100 return -EINVAL;
101 dpms_mode_ = dpms_mode;
102 type_ = DRM_COMPOSITION_TYPE_DPMS;
103 return 0;
104}
105
106int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
107 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
108 return -EINVAL;
109 display_mode_ = display_mode;
110 dpms_mode_ = DRM_MODE_DPMS_ON;
111 type_ = DRM_COMPOSITION_TYPE_MODESET;
112 return 0;
113}
114
115int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400116 composition_planes_.emplace_back(DrmCompositionPlane::Type::kDisable, plane,
Sean Paulca699be2016-05-11 16:29:45 -0400117 crtc_);
Zach Reizner92f8e632015-10-12 17:47:13 -0700118 return 0;
119}
120
121static 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); }) +
127 std::count_if(
128 overlay_planes->begin(), overlay_planes->end(),
129 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
130}
131
Zach Reizner09807052015-08-13 14:53:41 -0700132static 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;
136 planes->erase(iter);
137 return plane;
138 }
Zach Reiznerb44fd102015-08-07 16:00:01 -0700139 }
Zach Reizner09807052015-08-13 14:53:41 -0700140 return NULL;
Sean Paul98e73c82015-06-24 14:38:49 -0700141}
142
Zach Reizner09807052015-08-13 14:53:41 -0700143static DrmPlane *TakePlane(DrmCrtc *crtc,
144 std::vector<DrmPlane *> *primary_planes,
145 std::vector<DrmPlane *> *overlay_planes) {
146 DrmPlane *plane = TakePlane(crtc, primary_planes);
147 if (plane)
148 return plane;
149 return TakePlane(crtc, overlay_planes);
150}
Zach Reizner713a6782015-07-31 15:12:44 -0700151
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700152void DrmDisplayComposition::EmplaceCompositionPlane(
Sean Paulbbe39db2016-05-11 16:57:26 -0400153 DrmCompositionPlane::Type type, std::vector<DrmPlane *> *primary_planes,
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700154 std::vector<DrmPlane *> *overlay_planes) {
155 DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
156 if (plane == NULL) {
157 ALOGE(
158 "Failed to add composition plane because there are no planes "
159 "remaining");
160 return;
161 }
Sean Paulca699be2016-05-11 16:29:45 -0400162 composition_planes_.emplace_back(type, plane, crtc_);
163}
164
165void 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);
169 if (plane == NULL) {
170 ALOGE(
171 "Failed to add composition plane because there are no planes "
172 "remaining");
173 return;
174 }
Sean Paulbbe39db2016-05-11 16:57:26 -0400175 composition_planes_.emplace_back(DrmCompositionPlane::Type::kLayer, plane,
Sean Paulca699be2016-05-11 16:29:45 -0400176 crtc_, source_layer);
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700177}
178
Zach Reizner92f8e632015-10-12 17:47:13 -0700179static 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)
184 if (in & mask)
185 out.push_back(index_map[i]);
186 return out;
Zach Reizner09807052015-08-13 14:53:41 -0700187}
Zach Reizner713a6782015-07-31 15:12:44 -0700188
Sean Paul4f4ef692016-05-03 16:40:59 -0700189int DrmDisplayComposition::AddPlaneComposition(DrmCompositionPlane plane) {
190 composition_planes_.emplace_back(std::move(plane));
191 return 0;
192}
193
Sean Paul3960cdd2016-05-10 04:17:31 -0400194void DrmDisplayComposition::SeparateLayers(size_t *used_layers,
195 size_t num_used_layers,
196 DrmHwcRect<int> *exclude_rects,
197 size_t num_exclude_rects) {
198 DrmCompositionPlane *comp = NULL;
199 std::vector<size_t> dedicated_layers;
200
201 // Go through the composition and find the precomp layer as well as any
202 // layers that have a dedicated plane located below the precomp layer.
203 for (auto &i : composition_planes_) {
204 if (i.type() == DrmCompositionPlane::Type::kLayer) {
205 dedicated_layers.insert(dedicated_layers.end(), i.source_layers().begin(),
206 i.source_layers().end());
207 } else if (i.type() == DrmCompositionPlane::Type::kPrecomp) {
208 comp = &i;
209 break;
210 }
211 }
212 if (!comp)
213 return;
214
Zach Reizner92f8e632015-10-12 17:47:13 -0700215 if (num_used_layers > 64) {
216 ALOGE("Failed to separate layers because there are more than 64");
217 return;
218 }
Zach Reizner713a6782015-07-31 15:12:44 -0700219
Sean Paul8a628b92016-04-18 15:53:36 -0400220 // Index at which the actual layers begin
Sean Paul3960cdd2016-05-10 04:17:31 -0400221 size_t layer_offset = num_exclude_rects + dedicated_layers.size();
Sean Paul8a628b92016-04-18 15:53:36 -0400222 if (num_used_layers + layer_offset > 64) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700223 ALOGW(
224 "Exclusion rectangles are being truncated to make the rectangle count "
225 "fit into 64");
Sean Paul3960cdd2016-05-10 04:17:31 -0400226 num_exclude_rects = 64 - num_used_layers - dedicated_layers.size();
Zach Reizner92f8e632015-10-12 17:47:13 -0700227 }
Zach Reizner713a6782015-07-31 15:12:44 -0700228
Zach Reizner92f8e632015-10-12 17:47:13 -0700229 // We inject all the exclude rects into the rects list. Any resulting rect
Sean Paul8a628b92016-04-18 15:53:36 -0400230 // that includes ANY of the first num_exclude_rects is rejected. After the
Sean Paul3960cdd2016-05-10 04:17:31 -0400231 // exclude rects, we add the lower layers. The rects that intersect with
232 // these layers will be inspected and only those which are to be composited
233 // above the layer will be included in the composition regions.
Sean Paul8a628b92016-04-18 15:53:36 -0400234 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + layer_offset);
Zach Reizner92f8e632015-10-12 17:47:13 -0700235 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
236 layer_rects.begin());
237 std::transform(
Sean Paul3960cdd2016-05-10 04:17:31 -0400238 dedicated_layers.begin(), dedicated_layers.end(),
Zach Reizner92f8e632015-10-12 17:47:13 -0700239 layer_rects.begin() + num_exclude_rects,
Sean Paul3960cdd2016-05-10 04:17:31 -0400240 [=](size_t layer_index) { return layers_[layer_index].display_frame; });
241 std::transform(used_layers, used_layers + num_used_layers,
242 layer_rects.begin() + layer_offset, [=](size_t layer_index) {
243 return layers_[layer_index].display_frame;
244 });
Zach Reizner92f8e632015-10-12 17:47:13 -0700245
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800246 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
247 separate_rects::separate_rects_64(layer_rects, &separate_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -0700248 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
Sean Paul3960cdd2016-05-10 04:17:31 -0400249 uint64_t dedicated_mask = (((uint64_t)1 << dedicated_layers.size()) - 1)
250 << num_exclude_rects;
Zach Reizner92f8e632015-10-12 17:47:13 -0700251
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800252 for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700253 if (region.id_set.getBits() & exclude_mask)
254 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400255
Sean Paul3960cdd2016-05-10 04:17:31 -0400256 // If a rect intersects one of the dedicated layers, we need to remove the
257 // layers from the composition region which appear *below* the dedicated
258 // layer. This effectively punches a hole through the composition layer such
259 // that the dedicated layer can be placed below the composition and not
260 // be occluded.
261 uint64_t dedicated_intersect = region.id_set.getBits() & dedicated_mask;
262 for (size_t i = 0; dedicated_intersect && i < dedicated_layers.size();
263 ++i) {
264 // Only exclude layers if they intersect this particular dedicated layer
265 if (!(dedicated_intersect & (1 << (i + num_exclude_rects))))
Sean Paul8a628b92016-04-18 15:53:36 -0400266 continue;
267
Sean Paulf1d25792016-05-10 03:42:55 -0400268 for (size_t j = 0; j < num_used_layers; ++j) {
Sean Paul3960cdd2016-05-10 04:17:31 -0400269 if (used_layers[j] < dedicated_layers[i])
Sean Paulf1d25792016-05-10 03:42:55 -0400270 region.id_set.subtract(j + layer_offset);
271 }
Sean Paul8a628b92016-04-18 15:53:36 -0400272 }
Sean Paulf1d25792016-05-10 03:42:55 -0400273 if (!(region.id_set.getBits() >> layer_offset))
Sean Paul8a628b92016-04-18 15:53:36 -0400274 continue;
275
Sean Paul3960cdd2016-05-10 04:17:31 -0400276 pre_comp_regions_.emplace_back(DrmCompositionRegion{
Zach Reizner92f8e632015-10-12 17:47:13 -0700277 region.rect,
Sean Paulf1d25792016-05-10 03:42:55 -0400278 SetBitsToVector(region.id_set.getBits() >> layer_offset, used_layers)});
Zach Reizner92f8e632015-10-12 17:47:13 -0700279 }
Zach Reizner713a6782015-07-31 15:12:44 -0700280}
281
Zach Reizner5757e822015-10-16 19:06:31 -0700282int DrmDisplayComposition::CreateAndAssignReleaseFences() {
Zach Reizner92f8e632015-10-12 17:47:13 -0700283 std::unordered_set<DrmHwcLayer *> squash_layers;
284 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
285 std::unordered_set<DrmHwcLayer *> comp_layers;
286
287 for (const DrmCompositionRegion &region : squash_regions_) {
288 for (size_t source_layer_index : region.source_layers) {
289 DrmHwcLayer *source_layer = &layers_[source_layer_index];
290 squash_layers.emplace(source_layer);
291 }
292 }
293
294 for (const DrmCompositionRegion &region : pre_comp_regions_) {
295 for (size_t source_layer_index : region.source_layers) {
296 DrmHwcLayer *source_layer = &layers_[source_layer_index];
297 pre_comp_layers.emplace(source_layer);
298 squash_layers.erase(source_layer);
299 }
300 }
301
302 for (const DrmCompositionPlane &plane : composition_planes_) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400303 if (plane.type() == DrmCompositionPlane::Type::kLayer) {
Sean Paulca699be2016-05-11 16:29:45 -0400304 for (auto i : plane.source_layers()) {
305 DrmHwcLayer *source_layer = &layers_[i];
306 comp_layers.emplace(source_layer);
307 pre_comp_layers.erase(source_layer);
308 }
Zach Reizner92f8e632015-10-12 17:47:13 -0700309 }
310 }
311
312 for (DrmHwcLayer *layer : squash_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800313 if (!layer->release_fence)
314 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700315 int ret = layer->release_fence.Set(CreateNextTimelineFence());
316 if (ret < 0)
317 return ret;
318 }
319 timeline_squash_done_ = timeline_;
320
321 for (DrmHwcLayer *layer : pre_comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800322 if (!layer->release_fence)
323 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700324 int ret = layer->release_fence.Set(CreateNextTimelineFence());
325 if (ret < 0)
326 return ret;
327 }
Zach Reizner09807052015-08-13 14:53:41 -0700328 timeline_pre_comp_done_ = timeline_;
329
Zach Reizner92f8e632015-10-12 17:47:13 -0700330 for (DrmHwcLayer *layer : comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800331 if (!layer->release_fence)
332 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700333 int ret = layer->release_fence.Set(CreateNextTimelineFence());
334 if (ret < 0)
335 return ret;
Zach Reizner09807052015-08-13 14:53:41 -0700336 }
337
Zach Reizner09807052015-08-13 14:53:41 -0700338 return 0;
Zach Reizner46ddd452015-07-30 08:10:14 -0700339}
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700340
Zach Reizner5757e822015-10-16 19:06:31 -0700341int DrmDisplayComposition::Plan(SquashState *squash,
342 std::vector<DrmPlane *> *primary_planes,
343 std::vector<DrmPlane *> *overlay_planes) {
344 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
345 return 0;
346
347 size_t planes_can_use =
348 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
349 if (planes_can_use == 0) {
350 ALOGE("Display %d has no usable planes", crtc_->display());
351 return -ENODEV;
352 }
353
354 bool use_squash_framebuffer = false;
355 // Used to determine which layers were entirely squashed
356 std::vector<int> layer_squash_area(layers_.size(), 0);
357 // Used to avoid rerendering regions that were squashed
358 std::vector<DrmHwcRect<int>> exclude_rects;
359 if (squash != NULL && planes_can_use >= 3) {
360 if (geometry_changed_) {
361 squash->Init(layers_.data(), layers_.size());
362 } else {
363 std::vector<bool> changed_regions;
364 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
365
366 std::vector<bool> stable_regions;
367 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
368
369 // Only if SOME region is stable
370 use_squash_framebuffer =
371 std::find(stable_regions.begin(), stable_regions.end(), true) !=
372 stable_regions.end();
373
374 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
375
376 // Changes in which regions are squashed triggers a rerender via
377 // squash_regions.
378 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
379
380 for (size_t region_index = 0; region_index < stable_regions.size();
381 region_index++) {
382 const SquashState::Region &region = squash->regions()[region_index];
383 if (!stable_regions[region_index])
384 continue;
385
386 exclude_rects.emplace_back(region.rect);
387
388 if (render_squash) {
389 squash_regions_.emplace_back();
390 squash_regions_.back().frame = region.rect;
391 }
392
393 int frame_area = region.rect.area();
394 // Source layers are sorted front to back i.e. top layer has lowest
395 // index.
396 for (size_t layer_index = layers_.size();
397 layer_index-- > 0; // Yes, I double checked this
398 /* See condition */) {
399 if (!region.layer_refs[layer_index])
400 continue;
401 layer_squash_area[layer_index] += frame_area;
402 if (render_squash)
403 squash_regions_.back().source_layers.push_back(layer_index);
404 }
405 }
406 }
407 }
408
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700409 // All protected layers get first usage of planes
Zach Reizner5757e822015-10-16 19:06:31 -0700410 std::vector<size_t> layers_remaining;
Sean Paul8a628b92016-04-18 15:53:36 -0400411 std::vector<size_t> protected_layers;
Zach Reizner5757e822015-10-16 19:06:31 -0700412 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700413 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
414 layers_remaining.push_back(layer_index);
Zach Reizner5757e822015-10-16 19:06:31 -0700415 continue;
416 }
Sean Paul8a628b92016-04-18 15:53:36 -0400417 protected_layers.push_back(layer_index);
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700418 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700419 }
420
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700421 if (planes_can_use == 0 && layers_remaining.size() > 0) {
Sean Paul39b37842016-05-11 13:50:28 -0400422 for (auto i : protected_layers)
Sean Paulca699be2016-05-11 16:29:45 -0400423 EmplaceCompositionPlane(i, primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400424
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700425 ALOGE("Protected layers consumed all hardware planes");
426 return CreateAndAssignReleaseFences();
427 }
428
429 std::vector<size_t> layers_remaining_if_squash;
430 for (size_t layer_index : layers_remaining) {
431 if (layer_squash_area[layer_index] <
432 layers_[layer_index].display_frame.area())
433 layers_remaining_if_squash.push_back(layer_index);
434 }
435
436 if (use_squash_framebuffer) {
437 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
438 layers_remaining = std::move(layers_remaining_if_squash);
439 planes_can_use--; // Reserve plane for squashing
440 } else {
441 use_squash_framebuffer = false; // The squash buffer is still rendered
442 }
443 }
Zach Reizner5757e822015-10-16 19:06:31 -0700444
445 if (layers_remaining.size() > planes_can_use)
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700446 planes_can_use--; // Reserve one for pre-compositing
Zach Reizner5757e822015-10-16 19:06:31 -0700447
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700448 // Whatever planes that are not reserved get assigned a layer
Sean Paul8a628b92016-04-18 15:53:36 -0400449 size_t last_hw_comp_layer = 0;
450 size_t protected_idx = 0;
451 while(last_hw_comp_layer < layers_remaining.size() && planes_can_use > 0) {
452 size_t idx = layers_remaining[last_hw_comp_layer];
453
454 // Put the protected layers into the composition at the right place. We've
455 // already reserved them by decrementing planes_can_use, so no need to do
456 // that again.
457 if (protected_idx < protected_layers.size() &&
458 idx > protected_layers[protected_idx]) {
Sean Paulca699be2016-05-11 16:29:45 -0400459 EmplaceCompositionPlane(protected_layers[protected_idx], primary_planes,
Sean Paul39b37842016-05-11 13:50:28 -0400460 overlay_planes);
461 protected_idx++;
462 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400463 }
464
Sean Paulca699be2016-05-11 16:29:45 -0400465 EmplaceCompositionPlane(layers_remaining[last_hw_comp_layer],
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700466 primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400467 last_hw_comp_layer++;
468 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700469 }
470
471 layers_remaining.erase(layers_remaining.begin(),
Sean Paul8a628b92016-04-18 15:53:36 -0400472 layers_remaining.begin() + last_hw_comp_layer);
473
474 // Enqueue the rest of the protected layers (if any) between the hw composited
475 // overlay layers and the squash/precomp layers.
Sean Paulca699be2016-05-11 16:29:45 -0400476 for (size_t i = protected_idx; i < protected_layers.size(); ++i)
477 EmplaceCompositionPlane(protected_layers[i], primary_planes,
Sean Paul8a628b92016-04-18 15:53:36 -0400478 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700479
480 if (layers_remaining.size() > 0) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400481 EmplaceCompositionPlane(DrmCompositionPlane::Type::kPrecomp, primary_planes,
Sean Paulca699be2016-05-11 16:29:45 -0400482 overlay_planes);
Sean Paul3960cdd2016-05-10 04:17:31 -0400483 SeparateLayers(layers_remaining.data(), layers_remaining.size(),
484 exclude_rects.data(), exclude_rects.size());
Zach Reizner5757e822015-10-16 19:06:31 -0700485 }
486
487 if (use_squash_framebuffer) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400488 EmplaceCompositionPlane(DrmCompositionPlane::Type::kSquash, primary_planes,
Sean Paulca699be2016-05-11 16:29:45 -0400489 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700490 }
491
Sean Paul4f4ef692016-05-03 16:40:59 -0700492 return FinalizeComposition();
493}
494
495int DrmDisplayComposition::FinalizeComposition() {
Zach Reizner5757e822015-10-16 19:06:31 -0700496 return CreateAndAssignReleaseFences();
497}
498
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700499static const char *DrmCompositionTypeToString(DrmCompositionType type) {
500 switch (type) {
501 case DRM_COMPOSITION_TYPE_EMPTY:
502 return "EMPTY";
503 case DRM_COMPOSITION_TYPE_FRAME:
504 return "FRAME";
505 case DRM_COMPOSITION_TYPE_DPMS:
506 return "DPMS";
507 case DRM_COMPOSITION_TYPE_MODESET:
508 return "MODESET";
509 default:
510 return "<invalid>";
511 }
512}
513
514static const char *DPMSModeToString(int dpms_mode) {
515 switch (dpms_mode) {
516 case DRM_MODE_DPMS_ON:
517 return "ON";
518 case DRM_MODE_DPMS_OFF:
519 return "OFF";
520 default:
521 return "<invalid>";
522 }
523}
524
525static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
526 if (!buffer) {
527 *out << "buffer=<invalid>";
528 return;
529 }
530
531 *out << "buffer[w/h/format]=";
532 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
533}
534
Sean Paul04b47ea2015-11-19 21:46:11 -0500535static void DumpTransform(uint32_t transform, std::ostringstream *out) {
536 *out << "[";
537
538 if (transform == 0)
539 *out << "IDENTITY";
540
541 bool separator = false;
542 if (transform & DrmHwcTransform::kFlipH) {
543 *out << "FLIPH";
544 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700545 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500546 if (transform & DrmHwcTransform::kFlipV) {
547 if (separator)
548 *out << "|";
549 *out << "FLIPV";
550 separator = true;
551 }
552 if (transform & DrmHwcTransform::kRotate90) {
553 if (separator)
554 *out << "|";
555 *out << "ROTATE90";
556 separator = true;
557 }
558 if (transform & DrmHwcTransform::kRotate180) {
559 if (separator)
560 *out << "|";
561 *out << "ROTATE180";
562 separator = true;
563 }
564 if (transform & DrmHwcTransform::kRotate270) {
565 if (separator)
566 *out << "|";
567 *out << "ROTATE270";
568 separator = true;
569 }
570
571 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
572 DrmHwcTransform::kRotate90 |
573 DrmHwcTransform::kRotate180 |
574 DrmHwcTransform::kRotate270;
575 if (transform & ~valid_bits) {
576 if (separator)
577 *out << "|";
578 *out << "INVALID";
579 }
580 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700581}
582
583static const char *BlendingToString(DrmHwcBlending blending) {
584 switch (blending) {
585 case DrmHwcBlending::kNone:
586 return "NONE";
587 case DrmHwcBlending::kPreMult:
588 return "PREMULT";
589 case DrmHwcBlending::kCoverage:
590 return "COVERAGE";
591 default:
592 return "<invalid>";
593 }
594}
595
596static void DumpRegion(const DrmCompositionRegion &region,
597 std::ostringstream *out) {
598 *out << "frame";
599 region.frame.Dump(out);
600 *out << " source_layers=(";
601
602 const std::vector<size_t> &source_layers = region.source_layers;
603 for (size_t i = 0; i < source_layers.size(); i++) {
604 *out << source_layers[i];
605 if (i < source_layers.size() - 1) {
606 *out << " ";
607 }
608 }
609
610 *out << ")";
611}
612
613void DrmDisplayComposition::Dump(std::ostringstream *out) const {
614 *out << "----DrmDisplayComposition"
615 << " crtc=" << (crtc_ ? crtc_->id() : -1)
616 << " type=" << DrmCompositionTypeToString(type_);
617
618 switch (type_) {
619 case DRM_COMPOSITION_TYPE_DPMS:
620 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
621 break;
622 case DRM_COMPOSITION_TYPE_MODESET:
623 *out << " display_mode=" << display_mode_.h_display() << "x"
624 << display_mode_.v_display();
625 break;
626 default:
627 break;
628 }
629
630 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
631 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
632 << timeline_ << "\n";
633
634 *out << " Layers: count=" << layers_.size() << "\n";
635 for (size_t i = 0; i < layers_.size(); i++) {
636 const DrmHwcLayer &layer = layers_[i];
637 *out << " [" << i << "] ";
638
639 DumpBuffer(layer.buffer, out);
640
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700641 if (layer.protected_usage())
642 *out << " protected";
643
Sean Paul04b47ea2015-11-19 21:46:11 -0500644 *out << " transform=";
645 DumpTransform(layer.transform, out);
646 *out << " blending[a=" << (int)layer.alpha
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700647 << "]=" << BlendingToString(layer.blending) << " source_crop";
648 layer.source_crop.Dump(out);
649 *out << " display_frame";
650 layer.display_frame.Dump(out);
651
652 *out << "\n";
653 }
654
655 *out << " Planes: count=" << composition_planes_.size() << "\n";
656 for (size_t i = 0; i < composition_planes_.size(); i++) {
657 const DrmCompositionPlane &comp_plane = composition_planes_[i];
658 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400659 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400660 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400661 switch (comp_plane.type()) {
Sean Paulbbe39db2016-05-11 16:57:26 -0400662 case DrmCompositionPlane::Type::kDisable:
Sean Paul39b37842016-05-11 13:50:28 -0400663 *out << "DISABLE";
664 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400665 case DrmCompositionPlane::Type::kLayer:
Sean Paul39b37842016-05-11 13:50:28 -0400666 *out << "LAYER";
667 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400668 case DrmCompositionPlane::Type::kPrecomp:
Sean Paul39b37842016-05-11 13:50:28 -0400669 *out << "PRECOMP";
670 break;
Sean Paulbbe39db2016-05-11 16:57:26 -0400671 case DrmCompositionPlane::Type::kSquash:
Sean Paul39b37842016-05-11 13:50:28 -0400672 *out << "SQUASH";
673 break;
674 default:
675 *out << "<invalid>";
676 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700677 }
678
Sean Paulca699be2016-05-11 16:29:45 -0400679 *out << " source_layer=";
680 for (auto i : comp_plane.source_layers()) {
681 *out << i << " ";
682 }
683 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700684 }
685
686 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
687 for (size_t i = 0; i < squash_regions_.size(); i++) {
688 *out << " [" << i << "] ";
689 DumpRegion(squash_regions_[i], out);
690 *out << "\n";
691 }
692
693 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
694 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
695 *out << " [" << i << "] ";
696 DumpRegion(pre_comp_regions_[i], out);
697 *out << "\n";
698 }
699}
Sean Paul98e73c82015-06-24 14:38:49 -0700700}