blob: 208915764b6f2a2cacfbbd9bb672a2a654eaa150 [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) {
116 composition_planes_.emplace_back(
Sean Paul39b37842016-05-11 13:50:28 -0400117 DrmCompositionPlane{.plane = plane,
118 .crtc = crtc_,
119 .type = DrmCompositionPlaneType::kDisable,
120 .source_layer = -1});
Zach Reizner92f8e632015-10-12 17:47:13 -0700121 return 0;
122}
123
124static 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); }) +
130 std::count_if(
131 overlay_planes->begin(), overlay_planes->end(),
132 [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
133}
134
Zach Reizner09807052015-08-13 14:53:41 -0700135static 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;
139 planes->erase(iter);
140 return plane;
141 }
Zach Reiznerb44fd102015-08-07 16:00:01 -0700142 }
Zach Reizner09807052015-08-13 14:53:41 -0700143 return NULL;
Sean Paul98e73c82015-06-24 14:38:49 -0700144}
145
Zach Reizner09807052015-08-13 14:53:41 -0700146static DrmPlane *TakePlane(DrmCrtc *crtc,
147 std::vector<DrmPlane *> *primary_planes,
148 std::vector<DrmPlane *> *overlay_planes) {
149 DrmPlane *plane = TakePlane(crtc, primary_planes);
150 if (plane)
151 return plane;
152 return TakePlane(crtc, overlay_planes);
153}
Zach Reizner713a6782015-07-31 15:12:44 -0700154
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700155void DrmDisplayComposition::EmplaceCompositionPlane(
Sean Paul39b37842016-05-11 13:50:28 -0400156 DrmCompositionPlaneType type, int source_layer,
157 std::vector<DrmPlane *> *primary_planes,
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700158 std::vector<DrmPlane *> *overlay_planes) {
159 DrmPlane *plane = TakePlane(crtc_, primary_planes, overlay_planes);
160 if (plane == NULL) {
161 ALOGE(
162 "Failed to add composition plane because there are no planes "
163 "remaining");
164 return;
165 }
166 composition_planes_.emplace_back(
Sean Paul39b37842016-05-11 13:50:28 -0400167 DrmCompositionPlane{.plane = plane,
168 .crtc = crtc_,
169 .type = type,
170 .source_layer = source_layer});
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700171}
172
Zach Reizner92f8e632015-10-12 17:47:13 -0700173static std::vector<size_t> SetBitsToVector(uint64_t in, size_t *index_map) {
174 std::vector<size_t> out;
175 size_t msb = sizeof(in) * 8 - 1;
176 uint64_t mask = (uint64_t)1 << msb;
177 for (size_t i = msb; mask != (uint64_t)0; i--, mask >>= 1)
178 if (in & mask)
179 out.push_back(index_map[i]);
180 return out;
Zach Reizner09807052015-08-13 14:53:41 -0700181}
Zach Reizner713a6782015-07-31 15:12:44 -0700182
Zach Reizner5757e822015-10-16 19:06:31 -0700183static void SeparateLayers(DrmHwcLayer *layers, size_t *used_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700184 size_t num_used_layers,
Sean Paul8a628b92016-04-18 15:53:36 -0400185 size_t *protected_layers,
186 size_t num_protected_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700187 DrmHwcRect<int> *exclude_rects,
188 size_t num_exclude_rects,
189 std::vector<DrmCompositionRegion> &regions) {
190 if (num_used_layers > 64) {
191 ALOGE("Failed to separate layers because there are more than 64");
192 return;
193 }
Zach Reizner713a6782015-07-31 15:12:44 -0700194
Sean Paul8a628b92016-04-18 15:53:36 -0400195 // Index at which the actual layers begin
196 size_t layer_offset = num_exclude_rects + num_protected_layers;
197
198 if (num_used_layers + layer_offset > 64) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700199 ALOGW(
200 "Exclusion rectangles are being truncated to make the rectangle count "
201 "fit into 64");
Sean Paul8a628b92016-04-18 15:53:36 -0400202 num_exclude_rects = 64 - num_used_layers - num_protected_layers;
Zach Reizner92f8e632015-10-12 17:47:13 -0700203 }
Zach Reizner713a6782015-07-31 15:12:44 -0700204
Zach Reizner92f8e632015-10-12 17:47:13 -0700205 // We inject all the exclude rects into the rects list. Any resulting rect
Sean Paul8a628b92016-04-18 15:53:36 -0400206 // that includes ANY of the first num_exclude_rects is rejected. After the
207 // exclude rects, we add the protected layers. The rects that intersect with
208 // the protected layer will be inspected and only those which are above the
209 // protected layer will be included in the composition regions.
210 std::vector<DrmHwcRect<int>> layer_rects(num_used_layers + layer_offset);
Zach Reizner92f8e632015-10-12 17:47:13 -0700211 std::copy(exclude_rects, exclude_rects + num_exclude_rects,
212 layer_rects.begin());
213 std::transform(
Sean Paul8a628b92016-04-18 15:53:36 -0400214 protected_layers, protected_layers + num_protected_layers,
Zach Reizner92f8e632015-10-12 17:47:13 -0700215 layer_rects.begin() + num_exclude_rects,
216 [=](size_t layer_index) { return layers[layer_index].display_frame; });
Sean Paul8a628b92016-04-18 15:53:36 -0400217 std::transform(
218 used_layers, used_layers + num_used_layers,
219 layer_rects.begin() + layer_offset,
220 [=](size_t layer_index) { return layers[layer_index].display_frame; });
Zach Reizner92f8e632015-10-12 17:47:13 -0700221
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800222 std::vector<separate_rects::RectSet<uint64_t, int>> separate_regions;
223 separate_rects::separate_rects_64(layer_rects, &separate_regions);
Zach Reizner92f8e632015-10-12 17:47:13 -0700224 uint64_t exclude_mask = ((uint64_t)1 << num_exclude_rects) - 1;
Sean Paul8a628b92016-04-18 15:53:36 -0400225 uint64_t protected_mask = (((uint64_t)1 << num_protected_layers) - 1) <<
226 num_exclude_rects;
Zach Reizner92f8e632015-10-12 17:47:13 -0700227
Haixia Shiaa2f4a52015-11-02 10:54:29 -0800228 for (separate_rects::RectSet<uint64_t, int> &region : separate_regions) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700229 if (region.id_set.getBits() & exclude_mask)
230 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400231
232 // If a rect intersects a protected layer, we need to remove the layers
233 // from the composition region which appear *below* the protected layer.
234 // This effectively punches a hole through the composition layer such
235 // that the protected layer can be placed below the composition and not
236 // be occluded by things like the background.
237 uint64_t protected_intersect = region.id_set.getBits() & protected_mask;
238 for (size_t i = 0; protected_intersect && i < num_protected_layers; ++i) {
239 // Only exclude layers if they intersect this particular protected layer
240 if (!(protected_intersect & (1 << (i + num_exclude_rects))))
241 continue;
242
Sean Paulf1d25792016-05-10 03:42:55 -0400243 for (size_t j = 0; j < num_used_layers; ++j) {
244 if (used_layers[j] < protected_layers[i])
245 region.id_set.subtract(j + layer_offset);
246 }
Sean Paul8a628b92016-04-18 15:53:36 -0400247 }
Sean Paulf1d25792016-05-10 03:42:55 -0400248 if (!(region.id_set.getBits() >> layer_offset))
Sean Paul8a628b92016-04-18 15:53:36 -0400249 continue;
250
Zach Reizner92f8e632015-10-12 17:47:13 -0700251 regions.emplace_back(DrmCompositionRegion{
252 region.rect,
Sean Paulf1d25792016-05-10 03:42:55 -0400253 SetBitsToVector(region.id_set.getBits() >> layer_offset, used_layers)});
Zach Reizner92f8e632015-10-12 17:47:13 -0700254 }
Zach Reizner713a6782015-07-31 15:12:44 -0700255}
256
Zach Reizner5757e822015-10-16 19:06:31 -0700257int DrmDisplayComposition::CreateAndAssignReleaseFences() {
Zach Reizner92f8e632015-10-12 17:47:13 -0700258 std::unordered_set<DrmHwcLayer *> squash_layers;
259 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
260 std::unordered_set<DrmHwcLayer *> comp_layers;
261
262 for (const DrmCompositionRegion &region : squash_regions_) {
263 for (size_t source_layer_index : region.source_layers) {
264 DrmHwcLayer *source_layer = &layers_[source_layer_index];
265 squash_layers.emplace(source_layer);
266 }
267 }
268
269 for (const DrmCompositionRegion &region : pre_comp_regions_) {
270 for (size_t source_layer_index : region.source_layers) {
271 DrmHwcLayer *source_layer = &layers_[source_layer_index];
272 pre_comp_layers.emplace(source_layer);
273 squash_layers.erase(source_layer);
274 }
275 }
276
277 for (const DrmCompositionPlane &plane : composition_planes_) {
Sean Paul39b37842016-05-11 13:50:28 -0400278 if (plane.type == DrmCompositionPlaneType::kLayer) {
Zach Reizner92f8e632015-10-12 17:47:13 -0700279 DrmHwcLayer *source_layer = &layers_[plane.source_layer];
280 comp_layers.emplace(source_layer);
281 pre_comp_layers.erase(source_layer);
282 }
283 }
284
285 for (DrmHwcLayer *layer : squash_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800286 if (!layer->release_fence)
287 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700288 int ret = layer->release_fence.Set(CreateNextTimelineFence());
289 if (ret < 0)
290 return ret;
291 }
292 timeline_squash_done_ = timeline_;
293
294 for (DrmHwcLayer *layer : pre_comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800295 if (!layer->release_fence)
296 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700297 int ret = layer->release_fence.Set(CreateNextTimelineFence());
298 if (ret < 0)
299 return ret;
300 }
Zach Reizner09807052015-08-13 14:53:41 -0700301 timeline_pre_comp_done_ = timeline_;
302
Zach Reizner92f8e632015-10-12 17:47:13 -0700303 for (DrmHwcLayer *layer : comp_layers) {
Zach Reiznercb1cfc82015-11-13 16:11:37 -0800304 if (!layer->release_fence)
305 continue;
Zach Reizner92f8e632015-10-12 17:47:13 -0700306 int ret = layer->release_fence.Set(CreateNextTimelineFence());
307 if (ret < 0)
308 return ret;
Zach Reizner09807052015-08-13 14:53:41 -0700309 }
310
Zach Reizner09807052015-08-13 14:53:41 -0700311 return 0;
Zach Reizner46ddd452015-07-30 08:10:14 -0700312}
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700313
Zach Reizner5757e822015-10-16 19:06:31 -0700314int DrmDisplayComposition::Plan(SquashState *squash,
315 std::vector<DrmPlane *> *primary_planes,
316 std::vector<DrmPlane *> *overlay_planes) {
317 if (type_ != DRM_COMPOSITION_TYPE_FRAME)
318 return 0;
319
320 size_t planes_can_use =
321 CountUsablePlanes(crtc_, primary_planes, overlay_planes);
322 if (planes_can_use == 0) {
323 ALOGE("Display %d has no usable planes", crtc_->display());
324 return -ENODEV;
325 }
326
327 bool use_squash_framebuffer = false;
328 // Used to determine which layers were entirely squashed
329 std::vector<int> layer_squash_area(layers_.size(), 0);
330 // Used to avoid rerendering regions that were squashed
331 std::vector<DrmHwcRect<int>> exclude_rects;
332 if (squash != NULL && planes_can_use >= 3) {
333 if (geometry_changed_) {
334 squash->Init(layers_.data(), layers_.size());
335 } else {
336 std::vector<bool> changed_regions;
337 squash->GenerateHistory(layers_.data(), layers_.size(), changed_regions);
338
339 std::vector<bool> stable_regions;
340 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
341
342 // Only if SOME region is stable
343 use_squash_framebuffer =
344 std::find(stable_regions.begin(), stable_regions.end(), true) !=
345 stable_regions.end();
346
347 squash->RecordHistory(layers_.data(), layers_.size(), changed_regions);
348
349 // Changes in which regions are squashed triggers a rerender via
350 // squash_regions.
351 bool render_squash = squash->RecordAndCompareSquashed(stable_regions);
352
353 for (size_t region_index = 0; region_index < stable_regions.size();
354 region_index++) {
355 const SquashState::Region &region = squash->regions()[region_index];
356 if (!stable_regions[region_index])
357 continue;
358
359 exclude_rects.emplace_back(region.rect);
360
361 if (render_squash) {
362 squash_regions_.emplace_back();
363 squash_regions_.back().frame = region.rect;
364 }
365
366 int frame_area = region.rect.area();
367 // Source layers are sorted front to back i.e. top layer has lowest
368 // index.
369 for (size_t layer_index = layers_.size();
370 layer_index-- > 0; // Yes, I double checked this
371 /* See condition */) {
372 if (!region.layer_refs[layer_index])
373 continue;
374 layer_squash_area[layer_index] += frame_area;
375 if (render_squash)
376 squash_regions_.back().source_layers.push_back(layer_index);
377 }
378 }
379 }
380 }
381
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700382 // All protected layers get first usage of planes
Zach Reizner5757e822015-10-16 19:06:31 -0700383 std::vector<size_t> layers_remaining;
Sean Paul8a628b92016-04-18 15:53:36 -0400384 std::vector<size_t> protected_layers;
Zach Reizner5757e822015-10-16 19:06:31 -0700385 for (size_t layer_index = 0; layer_index < layers_.size(); layer_index++) {
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700386 if (!layers_[layer_index].protected_usage() || planes_can_use == 0) {
387 layers_remaining.push_back(layer_index);
Zach Reizner5757e822015-10-16 19:06:31 -0700388 continue;
389 }
Sean Paul8a628b92016-04-18 15:53:36 -0400390 protected_layers.push_back(layer_index);
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700391 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700392 }
393
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700394 if (planes_can_use == 0 && layers_remaining.size() > 0) {
Sean Paul39b37842016-05-11 13:50:28 -0400395 for (auto i : protected_layers)
396 EmplaceCompositionPlane(DrmCompositionPlaneType::kLayer, i,
397 primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400398
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700399 ALOGE("Protected layers consumed all hardware planes");
400 return CreateAndAssignReleaseFences();
401 }
402
403 std::vector<size_t> layers_remaining_if_squash;
404 for (size_t layer_index : layers_remaining) {
405 if (layer_squash_area[layer_index] <
406 layers_[layer_index].display_frame.area())
407 layers_remaining_if_squash.push_back(layer_index);
408 }
409
410 if (use_squash_framebuffer) {
411 if (planes_can_use > 1 || layers_remaining_if_squash.size() == 0) {
412 layers_remaining = std::move(layers_remaining_if_squash);
413 planes_can_use--; // Reserve plane for squashing
414 } else {
415 use_squash_framebuffer = false; // The squash buffer is still rendered
416 }
417 }
Zach Reizner5757e822015-10-16 19:06:31 -0700418
419 if (layers_remaining.size() > planes_can_use)
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700420 planes_can_use--; // Reserve one for pre-compositing
Zach Reizner5757e822015-10-16 19:06:31 -0700421
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700422 // Whatever planes that are not reserved get assigned a layer
Sean Paul8a628b92016-04-18 15:53:36 -0400423 size_t last_hw_comp_layer = 0;
424 size_t protected_idx = 0;
425 while(last_hw_comp_layer < layers_remaining.size() && planes_can_use > 0) {
426 size_t idx = layers_remaining[last_hw_comp_layer];
427
428 // Put the protected layers into the composition at the right place. We've
429 // already reserved them by decrementing planes_can_use, so no need to do
430 // that again.
431 if (protected_idx < protected_layers.size() &&
432 idx > protected_layers[protected_idx]) {
Sean Paul39b37842016-05-11 13:50:28 -0400433 EmplaceCompositionPlane(DrmCompositionPlaneType::kLayer,
434 protected_layers[protected_idx], primary_planes,
435 overlay_planes);
436 protected_idx++;
437 continue;
Sean Paul8a628b92016-04-18 15:53:36 -0400438 }
439
Sean Paul39b37842016-05-11 13:50:28 -0400440 EmplaceCompositionPlane(DrmCompositionPlaneType::kLayer,
441 layers_remaining[last_hw_comp_layer],
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700442 primary_planes, overlay_planes);
Sean Paul8a628b92016-04-18 15:53:36 -0400443 last_hw_comp_layer++;
444 planes_can_use--;
Zach Reizner5757e822015-10-16 19:06:31 -0700445 }
446
447 layers_remaining.erase(layers_remaining.begin(),
Sean Paul8a628b92016-04-18 15:53:36 -0400448 layers_remaining.begin() + last_hw_comp_layer);
449
450 // Enqueue the rest of the protected layers (if any) between the hw composited
451 // overlay layers and the squash/precomp layers.
Sean Paul39b37842016-05-11 13:50:28 -0400452 for (int i = protected_idx; i < protected_layers.size(); ++i)
453 EmplaceCompositionPlane(DrmCompositionPlaneType::kLayer,
454 protected_layers[i], primary_planes,
Sean Paul8a628b92016-04-18 15:53:36 -0400455 overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700456
457 if (layers_remaining.size() > 0) {
Sean Paul39b37842016-05-11 13:50:28 -0400458 EmplaceCompositionPlane(DrmCompositionPlaneType::kPrecomp, -1,
459 primary_planes, overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700460 SeparateLayers(layers_.data(), layers_remaining.data(),
Sean Paul8a628b92016-04-18 15:53:36 -0400461 layers_remaining.size(), protected_layers.data(),
462 protected_layers.size(), exclude_rects.data(),
Zach Reizner5757e822015-10-16 19:06:31 -0700463 exclude_rects.size(), pre_comp_regions_);
464 }
465
466 if (use_squash_framebuffer) {
Sean Paul39b37842016-05-11 13:50:28 -0400467 EmplaceCompositionPlane(DrmCompositionPlaneType::kSquash, -1,
468 primary_planes, overlay_planes);
Zach Reizner5757e822015-10-16 19:06:31 -0700469 }
470
471 return CreateAndAssignReleaseFences();
472}
473
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700474static const char *DrmCompositionTypeToString(DrmCompositionType type) {
475 switch (type) {
476 case DRM_COMPOSITION_TYPE_EMPTY:
477 return "EMPTY";
478 case DRM_COMPOSITION_TYPE_FRAME:
479 return "FRAME";
480 case DRM_COMPOSITION_TYPE_DPMS:
481 return "DPMS";
482 case DRM_COMPOSITION_TYPE_MODESET:
483 return "MODESET";
484 default:
485 return "<invalid>";
486 }
487}
488
489static const char *DPMSModeToString(int dpms_mode) {
490 switch (dpms_mode) {
491 case DRM_MODE_DPMS_ON:
492 return "ON";
493 case DRM_MODE_DPMS_OFF:
494 return "OFF";
495 default:
496 return "<invalid>";
497 }
498}
499
500static void DumpBuffer(const DrmHwcBuffer &buffer, std::ostringstream *out) {
501 if (!buffer) {
502 *out << "buffer=<invalid>";
503 return;
504 }
505
506 *out << "buffer[w/h/format]=";
507 *out << buffer->width << "/" << buffer->height << "/" << buffer->format;
508}
509
Sean Paul04b47ea2015-11-19 21:46:11 -0500510static void DumpTransform(uint32_t transform, std::ostringstream *out) {
511 *out << "[";
512
513 if (transform == 0)
514 *out << "IDENTITY";
515
516 bool separator = false;
517 if (transform & DrmHwcTransform::kFlipH) {
518 *out << "FLIPH";
519 separator = true;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700520 }
Sean Paul04b47ea2015-11-19 21:46:11 -0500521 if (transform & DrmHwcTransform::kFlipV) {
522 if (separator)
523 *out << "|";
524 *out << "FLIPV";
525 separator = true;
526 }
527 if (transform & DrmHwcTransform::kRotate90) {
528 if (separator)
529 *out << "|";
530 *out << "ROTATE90";
531 separator = true;
532 }
533 if (transform & DrmHwcTransform::kRotate180) {
534 if (separator)
535 *out << "|";
536 *out << "ROTATE180";
537 separator = true;
538 }
539 if (transform & DrmHwcTransform::kRotate270) {
540 if (separator)
541 *out << "|";
542 *out << "ROTATE270";
543 separator = true;
544 }
545
546 uint32_t valid_bits = DrmHwcTransform::kFlipH | DrmHwcTransform::kFlipH |
547 DrmHwcTransform::kRotate90 |
548 DrmHwcTransform::kRotate180 |
549 DrmHwcTransform::kRotate270;
550 if (transform & ~valid_bits) {
551 if (separator)
552 *out << "|";
553 *out << "INVALID";
554 }
555 *out << "]";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700556}
557
558static const char *BlendingToString(DrmHwcBlending blending) {
559 switch (blending) {
560 case DrmHwcBlending::kNone:
561 return "NONE";
562 case DrmHwcBlending::kPreMult:
563 return "PREMULT";
564 case DrmHwcBlending::kCoverage:
565 return "COVERAGE";
566 default:
567 return "<invalid>";
568 }
569}
570
571static void DumpRegion(const DrmCompositionRegion &region,
572 std::ostringstream *out) {
573 *out << "frame";
574 region.frame.Dump(out);
575 *out << " source_layers=(";
576
577 const std::vector<size_t> &source_layers = region.source_layers;
578 for (size_t i = 0; i < source_layers.size(); i++) {
579 *out << source_layers[i];
580 if (i < source_layers.size() - 1) {
581 *out << " ";
582 }
583 }
584
585 *out << ")";
586}
587
588void DrmDisplayComposition::Dump(std::ostringstream *out) const {
589 *out << "----DrmDisplayComposition"
590 << " crtc=" << (crtc_ ? crtc_->id() : -1)
591 << " type=" << DrmCompositionTypeToString(type_);
592
593 switch (type_) {
594 case DRM_COMPOSITION_TYPE_DPMS:
595 *out << " dpms_mode=" << DPMSModeToString(dpms_mode_);
596 break;
597 case DRM_COMPOSITION_TYPE_MODESET:
598 *out << " display_mode=" << display_mode_.h_display() << "x"
599 << display_mode_.v_display();
600 break;
601 default:
602 break;
603 }
604
605 *out << " timeline[current/squash/pre-comp/done]=" << timeline_current_ << "/"
606 << timeline_squash_done_ << "/" << timeline_pre_comp_done_ << "/"
607 << timeline_ << "\n";
608
609 *out << " Layers: count=" << layers_.size() << "\n";
610 for (size_t i = 0; i < layers_.size(); i++) {
611 const DrmHwcLayer &layer = layers_[i];
612 *out << " [" << i << "] ";
613
614 DumpBuffer(layer.buffer, out);
615
Zach Reiznerdb81fce2015-10-27 16:18:06 -0700616 if (layer.protected_usage())
617 *out << " protected";
618
Sean Paul04b47ea2015-11-19 21:46:11 -0500619 *out << " transform=";
620 DumpTransform(layer.transform, out);
621 *out << " blending[a=" << (int)layer.alpha
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700622 << "]=" << BlendingToString(layer.blending) << " source_crop";
623 layer.source_crop.Dump(out);
624 *out << " display_frame";
625 layer.display_frame.Dump(out);
626
627 *out << "\n";
628 }
629
630 *out << " Planes: count=" << composition_planes_.size() << "\n";
631 for (size_t i = 0; i < composition_planes_.size(); i++) {
632 const DrmCompositionPlane &comp_plane = composition_planes_[i];
633 *out << " [" << i << "]"
634 << " plane=" << (comp_plane.plane ? comp_plane.plane->id() : -1)
Sean Paul39b37842016-05-11 13:50:28 -0400635 << " type=";
636 switch (comp_plane.type) {
637 case DrmCompositionPlaneType::kDisable:
638 *out << "DISABLE";
639 break;
640 case DrmCompositionPlaneType::kLayer:
641 *out << "LAYER";
642 break;
643 case DrmCompositionPlaneType::kPrecomp:
644 *out << "PRECOMP";
645 break;
646 case DrmCompositionPlaneType::kSquash:
647 *out << "SQUASH";
648 break;
649 default:
650 *out << "<invalid>";
651 break;
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700652 }
653
Sean Paul39b37842016-05-11 13:50:28 -0400654 *out << " source_layer=" << comp_plane.source_layer << "\n";
Zach Reiznerfd6dc332015-10-13 21:12:48 -0700655 }
656
657 *out << " Squash Regions: count=" << squash_regions_.size() << "\n";
658 for (size_t i = 0; i < squash_regions_.size(); i++) {
659 *out << " [" << i << "] ";
660 DumpRegion(squash_regions_[i], out);
661 *out << "\n";
662 }
663
664 *out << " Pre-Comp Regions: count=" << pre_comp_regions_.size() << "\n";
665 for (size_t i = 0; i < pre_comp_regions_.size(); i++) {
666 *out << " [" << i << "] ";
667 DumpRegion(pre_comp_regions_[i], out);
668 *out << "\n";
669 }
670}
Sean Paul98e73c82015-06-24 14:38:49 -0700671}