blob: 2af9d5aa5cd58b90a6fd2d2e250d2dd021fa79e2 [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 Paulca699be2016-05-11 16:29:45 -0400116 composition_planes_.emplace_back(DrmCompositionPlaneType::kDisable, plane,
117 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 Paulca699be2016-05-11 16:29:45 -0400153 DrmCompositionPlaneType 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 }
175 composition_planes_.emplace_back(DrmCompositionPlaneType::kLayer, plane,
176 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
Zach Reizner5757e822015-10-16 19:06:31 -0700189static void SeparateLayers(DrmHwcLayer *layers, size_t *used_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700190 size_t num_used_layers,
Sean Paul8a628b92016-04-18 15:53:36 -0400191 size_t *protected_layers,
192 size_t num_protected_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700193 DrmHwcRect<int> *exclude_rects,
194 size_t num_exclude_rects,
195 std::vector<DrmCompositionRegion> &regions) {
196 if (num_used_layers > 64) {
197 ALOGE("Failed to separate layers because there are more than 64");
198 return;
199 }
Zach Reizner713a6782015-07-31 15:12:44 -0700200
Sean Paul8a628b92016-04-18 15:53:36 -0400201 // Index at which the actual layers begin
202 size_t layer_offset = num_exclude_rects + num_protected_layers;
203
204 if (num_used_layers + layer_offset > 64) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700205 ALOGW(
206 "Exclusion rectangles are being truncated to make the rectangle count "
207 "fit into 64");
Sean Paul8a628b92016-04-18 15:53:36 -0400208 num_exclude_rects = 64 - num_used_layers - num_protected_layers;
Zach Reizner92f8e632015-10-12 17:47:13 -0700209 }
Zach Reizner713a6782015-07-31 15:12:44 -0700210
Zach Reizner92f8e632015-10-12 17:47:13 -0700211 // We inject all the exclude rects into the rects list. Any resulting rect
Sean Paul8a628b92016-04-18 15:53:36 -0400212 // that includes ANY of the first num_exclude_rects is rejected. After the
213 // exclude rects, we add the protected layers. The rects that intersect with
214 // the protected layer will be inspected and only those which are above the
215 // protected layer will be included in the composition regions.
216 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + layer_offset);
Zach Reizner92f8e632015-10-12 17:47:13 -0700217 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
218 layer_rects.begin());
219 std::transform(
Sean Paul8a628b92016-04-18 15:53:36 -0400220 protected_layers, protected_layers + num_protected_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700221 layer_rects.begin() + num_exclude_rects,
222 [=](size_t layer_index) { return layers[layer_index].display_frame; });
Sean Paul8a628b92016-04-18 15:53:36 -0400223 std::transform(
224 used_layers, used_layers + num_used_layers,
225 layer_rects.begin() + layer_offset,
226 [=](size_t layer_index) { return layers[layer_index].display_frame; });
Zach Reizner92f8e632015-10-12 17:47:13 -0700227
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800228 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
229 separate_rects::separate_rects_64(layer_rects, &separate_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -0700230 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
Sean Paul8a628b92016-04-18 15:53:36 -0400231 uint64_t protected_mask = (((uint64_t)1 << num_protected_layers) - 1) <<
232 num_exclude_rects;
Zach Reizner92f8e632015-10-12 17:47:13 -0700233
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800234 for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700235 if (region.id_set.getBits() & exclude_mask)
236 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400237
238 // If a rect intersects a protected layer, we need to remove the layers
239 // from the composition region which appear *below* the protected layer.
240 // This effectively punches a hole through the composition layer such
241 // that the protected layer can be placed below the composition and not
242 // be occluded by things like the background.
243 uint64_t protected_intersect = region.id_set.getBits() & protected_mask;
244 for (size_t i = 0; protected_intersect && i < num_protected_layers; ++i) {
245 // Only exclude layers if they intersect this particular protected layer
246 if (!(protected_intersect & (1 << (i + num_exclude_rects))))
247 continue;
248
Sean Paulf1d25792016-05-10 03:42:55 -0400249 for (size_t j = 0; j < num_used_layers; ++j) {
250 if (used_layers[j] < protected_layers[i])
251 region.id_set.subtract(j + layer_offset);
252 }
Sean Paul8a628b92016-04-18 15:53:36 -0400253 }
Sean Paulf1d25792016-05-10 03:42:55 -0400254 if (!(region.id_set.getBits() >> layer_offset))
Sean Paul8a628b92016-04-18 15:53:36 -0400255 continue;
256
Zach Reizner92f8e632015-10-12 17:47:13 -0700257 regions.emplace_back(DrmCompositionRegion{
258 region.rect,
Sean Paulf1d25792016-05-10 03:42:55 -0400259 SetBitsToVector(region.id_set.getBits() >> layer_offset, used_layers)});
Zach Reizner92f8e632015-10-12 17:47:13 -0700260 }
Zach Reizner713a6782015-07-31 15:12:44 -0700261}
262
Zach Reizner5757e822015-10-16 19:06:31 -0700263int DrmDisplayComposition::CreateAndAssignReleaseFences() {
Zach Reizner92f8e632015-10-12 17:47:13 -0700264 std::unordered_set<DrmHwcLayer *> squash_layers;
265 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
266 std::unordered_set<DrmHwcLayer *> comp_layers;
267
268 for (const DrmCompositionRegion &region : squash_regions_) {
269 for (size_t source_layer_index : region.source_layers) {
270 DrmHwcLayer *source_layer = &layers_[source_layer_index];
271 squash_layers.emplace(source_layer);
272 }
273 }
274
275 for (const DrmCompositionRegion &region : pre_comp_regions_) {
276 for (size_t source_layer_index : region.source_layers) {
277 DrmHwcLayer *source_layer = &layers_[source_layer_index];
278 pre_comp_layers.emplace(source_layer);
279 squash_layers.erase(source_layer);
280 }
281 }
282
283 for (const DrmCompositionPlane &plane : composition_planes_) {
Sean Paulca699be2016-05-11 16:29:45 -0400284 if (plane.type() == DrmCompositionPlaneType::kLayer) {
285 for (auto i : plane.source_layers()) {
286 DrmHwcLayer *source_layer = &layers_[i];
287 comp_layers.emplace(source_layer);
288 pre_comp_layers.erase(source_layer);
289 }
Zach Reizner92f8e632015-10-12 17:47:13 -0700290 }
291 }
292
293 for (DrmHwcLayer *layer : squash_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800294 if (!layer->release_fence)
295 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700296 int ret = layer->release_fence.Set(CreateNextTimelineFence());
297 if (ret < 0)
298 return ret;
299 }
300 timeline_squash_done_ = timeline_;
301
302 for (DrmHwcLayer *layer : pre_comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800303 if (!layer->release_fence)
304 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700305 int ret = layer->release_fence.Set(CreateNextTimelineFence());
306 if (ret < 0)
307 return ret;
308 }
Zach Reizner09807052015-08-13 14:53:41 -0700309 timeline_pre_comp_done_ = timeline_;
310
Zach Reizner92f8e632015-10-12 17:47:13 -0700311 for (DrmHwcLayer *layer : comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800312 if (!layer->release_fence)
313 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700314 int ret = layer->release_fence.Set(CreateNextTimelineFence());
315 if (ret < 0)
316 return ret;
Zach Reizner09807052015-08-13 14:53:41 -0700317 }
318
Zach Reizner09807052015-08-13 14:53:41 -0700319 return 0;
Zach Reizner46ddd452015-07-30 08:10:14 -0700320}
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700321
Zach Reizner5757e822015-10-16 19:06:31 -0700322int DrmDisplayComposition::Plan(SquashState *squash,
323 std::vector<DrmPlane *> *primary_planes,
324 std::vector<DrmPlane *> *overlay_planes) {
325 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
326 return 0;
327
328 size_t planes_can_use =
329 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
330 if (planes_can_use == 0) {
331 ALOGE("Display %d has no usable planes", crtc_->display());
332 return -ENODEV;
333 }
334
335 bool use_squash_framebuffer = false;
336 // Used to determine which layers were entirely squashed
337 std::vector<int> layer_squash_area(layers_.size(), 0);
338 // Used to avoid rerendering regions that were squashed
339 std::vector<DrmHwcRect<int>> exclude_rects;
340 if (squash != NULL && planes_can_use >= 3) {
341 if (geometry_changed_) {
342 squash->Init(layers_.data(), layers_.size());
343 } else {
344 std::vector<bool> changed_regions;
345 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
346
347 std::vector<bool> stable_regions;
348 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
349
350 // Only if SOME region is stable
351 use_squash_framebuffer =
352 std::find(stable_regions.begin(), stable_regions.end(), true) !=
353 stable_regions.end();
354
355 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
356
357 // Changes in which regions are squashed triggers a rerender via
358 // squash_regions.
359 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
360
361 for (size_t region_index = 0; region_index < stable_regions.size();
362 region_index++) {
363 const SquashState::Region &region = squash->regions()[region_index];
364 if (!stable_regions[region_index])
365 continue;
366
367 exclude_rects.emplace_back(region.rect);
368
369 if (render_squash) {
370 squash_regions_.emplace_back();
371 squash_regions_.back().frame = region.rect;
372 }
373
374 int frame_area = region.rect.area();
375 // Source layers are sorted front to back i.e. top layer has lowest
376 // index.
377 for (size_t layer_index = layers_.size();
378 layer_index-- > 0; // Yes, I double checked this
379 /* See condition */) {
380 if (!region.layer_refs[layer_index])
381 continue;
382 layer_squash_area[layer_index] += frame_area;
383 if (render_squash)
384 squash_regions_.back().source_layers.push_back(layer_index);
385 }
386 }
387 }
388 }
389
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700390 // All protected layers get first usage of planes
Zach Reizner5757e822015-10-16 19:06:31 -0700391 std::vector<size_t> layers_remaining;
Sean Paul8a628b92016-04-18 15:53:36 -0400392 std::vector<size_t> protected_layers;
Zach Reizner5757e822015-10-16 19:06:31 -0700393 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700394 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
395 layers_remaining.push_back(layer_index);
Zach Reizner5757e822015-10-16 19:06:31 -0700396 continue;
397 }
Sean Paul8a628b92016-04-18 15:53:36 -0400398 protected_layers.push_back(layer_index);
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700399 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700400 }
401
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700402 if (planes_can_use == 0 && layers_remaining.size() > 0) {
Sean Paul39b37842016-05-11 13:50:28 -0400403 for (auto i : protected_layers)
Sean Paulca699be2016-05-11 16:29:45 -0400404 EmplaceCompositionPlane(i, primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400405
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700406 ALOGE("Protected layers consumed all hardware planes");
407 return CreateAndAssignReleaseFences();
408 }
409
410 std::vector<size_t> layers_remaining_if_squash;
411 for (size_t layer_index : layers_remaining) {
412 if (layer_squash_area[layer_index] <
413 layers_[layer_index].display_frame.area())
414 layers_remaining_if_squash.push_back(layer_index);
415 }
416
417 if (use_squash_framebuffer) {
418 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
419 layers_remaining = std::move(layers_remaining_if_squash);
420 planes_can_use--; // Reserve plane for squashing
421 } else {
422 use_squash_framebuffer = false; // The squash buffer is still rendered
423 }
424 }
Zach Reizner5757e822015-10-16 19:06:31 -0700425
426 if (layers_remaining.size() > planes_can_use)
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700427 planes_can_use--; // Reserve one for pre-compositing
Zach Reizner5757e822015-10-16 19:06:31 -0700428
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700429 // Whatever planes that are not reserved get assigned a layer
Sean Paul8a628b92016-04-18 15:53:36 -0400430 size_t last_hw_comp_layer = 0;
431 size_t protected_idx = 0;
432 while(last_hw_comp_layer < layers_remaining.size() && planes_can_use > 0) {
433 size_t idx = layers_remaining[last_hw_comp_layer];
434
435 // Put the protected layers into the composition at the right place. We've
436 // already reserved them by decrementing planes_can_use, so no need to do
437 // that again.
438 if (protected_idx < protected_layers.size() &&
439 idx > protected_layers[protected_idx]) {
Sean Paulca699be2016-05-11 16:29:45 -0400440 EmplaceCompositionPlane(protected_layers[protected_idx], primary_planes,
Sean Paul39b37842016-05-11 13:50:28 -0400441 overlay_planes);
442 protected_idx++;
443 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400444 }
445
Sean Paulca699be2016-05-11 16:29:45 -0400446 EmplaceCompositionPlane(layers_remaining[last_hw_comp_layer],
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700447 primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400448 last_hw_comp_layer++;
449 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700450 }
451
452 layers_remaining.erase(layers_remaining.begin(),
Sean Paul8a628b92016-04-18 15:53:36 -0400453 layers_remaining.begin() + last_hw_comp_layer);
454
455 // Enqueue the rest of the protected layers (if any) between the hw composited
456 // overlay layers and the squash/precomp layers.
Sean Paulca699be2016-05-11 16:29:45 -0400457 for (size_t i = protected_idx; i < protected_layers.size(); ++i)
458 EmplaceCompositionPlane(protected_layers[i], primary_planes,
Sean Paul8a628b92016-04-18 15:53:36 -0400459 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700460
461 if (layers_remaining.size() > 0) {
Sean Paulca699be2016-05-11 16:29:45 -0400462 EmplaceCompositionPlane(DrmCompositionPlaneType::kPrecomp, primary_planes,
463 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700464 SeparateLayers(layers_.data(), layers_remaining.data(),
Sean Paul8a628b92016-04-18 15:53:36 -0400465 layers_remaining.size(), protected_layers.data(),
466 protected_layers.size(), exclude_rects.data(),
Zach Reizner5757e822015-10-16 19:06:31 -0700467 exclude_rects.size(), pre_comp_regions_);
468 }
469
470 if (use_squash_framebuffer) {
Sean Paulca699be2016-05-11 16:29:45 -0400471 EmplaceCompositionPlane(DrmCompositionPlaneType::kSquash, primary_planes,
472 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700473 }
474
475 return CreateAndAssignReleaseFences();
476}
477
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700478static const char *DrmCompositionTypeToString(DrmCompositionType type) {
479 switch (type) {
480 case DRM_COMPOSITION_TYPE_EMPTY:
481 return "EMPTY";
482 case DRM_COMPOSITION_TYPE_FRAME:
483 return "FRAME";
484 case DRM_COMPOSITION_TYPE_DPMS:
485 return "DPMS";
486 case DRM_COMPOSITION_TYPE_MODESET:
487 return "MODESET";
488 default:
489 return "<invalid>";
490 }
491}
492
493static const char *DPMSModeToString(int dpms_mode) {
494 switch (dpms_mode) {
495 case DRM_MODE_DPMS_ON:
496 return "ON";
497 case DRM_MODE_DPMS_OFF:
498 return "OFF";
499 default:
500 return "<invalid>";
501 }
502}
503
504static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
505 if (!buffer) {
506 *out << "buffer=<invalid>";
507 return;
508 }
509
510 *out << "buffer[w/h/format]=";
511 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
512}
513
Sean Paul04b47ea2015-11-19 21:46:11 -0500514static void DumpTransform(uint32_t transform, std::ostringstream *out) {
515 *out << "[";
516
517 if (transform == 0)
518 *out << "IDENTITY";
519
520 bool separator = false;
521 if (transform & DrmHwcTransform::kFlipH) {
522 *out << "FLIPH";
523 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700524 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500525 if (transform & DrmHwcTransform::kFlipV) {
526 if (separator)
527 *out << "|";
528 *out << "FLIPV";
529 separator = true;
530 }
531 if (transform & DrmHwcTransform::kRotate90) {
532 if (separator)
533 *out << "|";
534 *out << "ROTATE90";
535 separator = true;
536 }
537 if (transform & DrmHwcTransform::kRotate180) {
538 if (separator)
539 *out << "|";
540 *out << "ROTATE180";
541 separator = true;
542 }
543 if (transform & DrmHwcTransform::kRotate270) {
544 if (separator)
545 *out << "|";
546 *out << "ROTATE270";
547 separator = true;
548 }
549
550 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
551 DrmHwcTransform::kRotate90 |
552 DrmHwcTransform::kRotate180 |
553 DrmHwcTransform::kRotate270;
554 if (transform & ~valid_bits) {
555 if (separator)
556 *out << "|";
557 *out << "INVALID";
558 }
559 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700560}
561
562static const char *BlendingToString(DrmHwcBlending blending) {
563 switch (blending) {
564 case DrmHwcBlending::kNone:
565 return "NONE";
566 case DrmHwcBlending::kPreMult:
567 return "PREMULT";
568 case DrmHwcBlending::kCoverage:
569 return "COVERAGE";
570 default:
571 return "<invalid>";
572 }
573}
574
575static void DumpRegion(const DrmCompositionRegion &region,
576 std::ostringstream *out) {
577 *out << "frame";
578 region.frame.Dump(out);
579 *out << " source_layers=(";
580
581 const std::vector<size_t> &source_layers = region.source_layers;
582 for (size_t i = 0; i < source_layers.size(); i++) {
583 *out << source_layers[i];
584 if (i < source_layers.size() - 1) {
585 *out << " ";
586 }
587 }
588
589 *out << ")";
590}
591
592void DrmDisplayComposition::Dump(std::ostringstream *out) const {
593 *out << "----DrmDisplayComposition"
594 << " crtc=" << (crtc_ ? crtc_->id() : -1)
595 << " type=" << DrmCompositionTypeToString(type_);
596
597 switch (type_) {
598 case DRM_COMPOSITION_TYPE_DPMS:
599 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
600 break;
601 case DRM_COMPOSITION_TYPE_MODESET:
602 *out << " display_mode=" << display_mode_.h_display() << "x"
603 << display_mode_.v_display();
604 break;
605 default:
606 break;
607 }
608
609 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
610 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
611 << timeline_ << "\n";
612
613 *out << " Layers: count=" << layers_.size() << "\n";
614 for (size_t i = 0; i < layers_.size(); i++) {
615 const DrmHwcLayer &layer = layers_[i];
616 *out << " [" << i << "] ";
617
618 DumpBuffer(layer.buffer, out);
619
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700620 if (layer.protected_usage())
621 *out << " protected";
622
Sean Paul04b47ea2015-11-19 21:46:11 -0500623 *out << " transform=";
624 DumpTransform(layer.transform, out);
625 *out << " blending[a=" << (int)layer.alpha
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700626 << "]=" << BlendingToString(layer.blending) << " source_crop";
627 layer.source_crop.Dump(out);
628 *out << " display_frame";
629 layer.display_frame.Dump(out);
630
631 *out << "\n";
632 }
633
634 *out << " Planes: count=" << composition_planes_.size() << "\n";
635 for (size_t i = 0; i < composition_planes_.size(); i++) {
636 const DrmCompositionPlane &comp_plane = composition_planes_[i];
637 *out << " [" << i << "]"
Sean Paulca699be2016-05-11 16:29:45 -0400638 << " plane=" << (comp_plane.plane() ? comp_plane.plane()->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400639 << " type=";
Sean Paulca699be2016-05-11 16:29:45 -0400640 switch (comp_plane.type()) {
Sean Paul39b37842016-05-11 13:50:28 -0400641 case DrmCompositionPlaneType::kDisable:
642 *out << "DISABLE";
643 break;
644 case DrmCompositionPlaneType::kLayer:
645 *out << "LAYER";
646 break;
647 case DrmCompositionPlaneType::kPrecomp:
648 *out << "PRECOMP";
649 break;
650 case DrmCompositionPlaneType::kSquash:
651 *out << "SQUASH";
652 break;
653 default:
654 *out << "<invalid>";
655 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700656 }
657
Sean Paulca699be2016-05-11 16:29:45 -0400658 *out << " source_layer=";
659 for (auto i : comp_plane.source_layers()) {
660 *out << i << " ";
661 }
662 *out << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700663 }
664
665 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
666 for (size_t i = 0; i < squash_regions_.size(); i++) {
667 *out << " [" << i << "] ";
668 DumpRegion(squash_regions_[i], out);
669 *out << "\n";
670 }
671
672 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
673 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
674 *out << " [" << i << "] ";
675 DumpRegion(pre_comp_regions_[i], out);
676 *out << "\n";
677 }
678}
Sean Paul98e73c82015-06-24 14:38:49 -0700679}