blob: b3746349943cb807761e79e3333e6f94c0ac9045 [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
Zach Reizner92f8e632015-10-12 17:47:13 -070036const size_t DrmCompositionPlane::kSourceNone;
37const size_t DrmCompositionPlane::kSourcePreComp;
38const size_t DrmCompositionPlane::kSourceSquash;
39const size_t DrmCompositionPlane::kSourceLayerMax;
Sean Paul98e73c82015-06-24 14:38:49 -070040
41DrmDisplayComposition::~DrmDisplayComposition() {
Zach Reiznerece04892015-07-17 14:13:28 -070042 if (timeline_fd_ >= 0) {
Zach Reizner92f8e632015-10-12 17:47:13 -070043 SignalCompositionDone();
Sean Paul98e73c82015-06-24 14:38:49 -070044 close(timeline_fd_);
Zach Reiznerece04892015-07-17 14:13:28 -070045 }
Sean Paul98e73c82015-06-24 14:38:49 -070046}
47
Zach Reizner09807052015-08-13 14:53:41 -070048int DrmDisplayComposition::Init(DrmResources *drm, DrmCrtc *crtc,
Sean Paulbdc67bf2015-09-21 10:04:02 -040049 Importer *importer, uint64_t frame_no) {
Sean Paul98e73c82015-06-24 14:38:49 -070050 drm_ = drm;
Zach Reizner4a253652015-09-10 18:30:54 -070051 crtc_ = crtc; // Can be NULL if we haven't modeset yet
Sean Paul98e73c82015-06-24 14:38:49 -070052 importer_ = importer;
Sean Paulbdc67bf2015-09-21 10:04:02 -040053 frame_no_ = frame_no;
Sean Paul98e73c82015-06-24 14:38:49 -070054
Zach Reizner4a253652015-09-10 18:30:54 -070055 int ret = sw_sync_timeline_create();
Sean Paul98e73c82015-06-24 14:38:49 -070056 if (ret < 0) {
57 ALOGE("Failed to create sw sync timeline %d", ret);
58 return ret;
59 }
60 timeline_fd_ = ret;
61 return 0;
62}
63
Sean Paulacb2a442015-06-24 18:43:01 -070064bool DrmDisplayComposition::validate_composition_type(DrmCompositionType des) {
65 return type_ == DRM_COMPOSITION_TYPE_EMPTY || type_ == des;
66}
67
Zach Reizner92f8e632015-10-12 17:47:13 -070068int DrmDisplayComposition::CreateNextTimelineFence() {
69 ++timeline_;
70 return sw_sync_fence_create(timeline_fd_, "hwc drm display composition fence",
71 timeline_);
72}
73
74int DrmDisplayComposition::IncreaseTimelineToPoint(int point) {
75 int timeline_increase = point - timeline_current_;
76 if (timeline_increase <= 0)
77 return 0;
78
79 int ret = sw_sync_timeline_inc(timeline_fd_, timeline_increase);
80 if (ret)
81 ALOGE("Failed to increment sync timeline %d", ret);
82 else
83 timeline_current_ = point;
84
85 return ret;
86}
87
88int DrmDisplayComposition::SetLayers(DrmHwcLayer *layers, size_t num_layers) {
89 int ret = 0;
90 if (!validate_composition_type(DRM_COMPOSITION_TYPE_FRAME))
91 return -EINVAL;
92
93 for (size_t layer_index = 0; layer_index < num_layers; layer_index++) {
94 layers_.emplace_back(std::move(layers[layer_index]));
95 }
96
97 type_ = DRM_COMPOSITION_TYPE_FRAME;
98 return 0;
99}
100
101int DrmDisplayComposition::SetDpmsMode(uint32_t dpms_mode) {
102 if (!validate_composition_type(DRM_COMPOSITION_TYPE_DPMS))
103 return -EINVAL;
104 dpms_mode_ = dpms_mode;
105 type_ = DRM_COMPOSITION_TYPE_DPMS;
106 return 0;
107}
108
109int DrmDisplayComposition::SetDisplayMode(const DrmMode &display_mode) {
110 if (!validate_composition_type(DRM_COMPOSITION_TYPE_MODESET))
111 return -EINVAL;
112 display_mode_ = display_mode;
113 dpms_mode_ = DRM_MODE_DPMS_ON;
114 type_ = DRM_COMPOSITION_TYPE_MODESET;
115 return 0;
116}
117
118int DrmDisplayComposition::AddPlaneDisable(DrmPlane *plane) {
119 composition_planes_.emplace_back(
120 DrmCompositionPlane{plane, crtc_, DrmCompositionPlane::kSourceNone});
121 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 Reizner92f8e632015-10-12 17:47:13 -0700155static 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)
160 if (in & mask)
161 out.push_back(index_map[i]);
162 return out;
Zach Reizner09807052015-08-13 14:53:41 -0700163}
Zach Reizner713a6782015-07-31 15:12:44 -0700164
Zach Reizner92f8e632015-10-12 17:47:13 -0700165static 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> &regions) {
170 if (num_used_layers > 64) {
171 ALOGE("Failed to separate layers because there are more than 64");
172 return;
173 }
Zach Reizner713a6782015-07-31 15:12:44 -0700174
Zach Reizner92f8e632015-10-12 17:47:13 -0700175 if (num_used_layers + num_exclude_rects > 64) {
176 ALOGW(
177 "Exclusion rectangles are being truncated to make the rectangle count "
178 "fit into 64");
179 num_exclude_rects = 64 - num_used_layers;
180 }
Zach Reizner713a6782015-07-31 15:12:44 -0700181
Zach Reizner92f8e632015-10-12 17:47:13 -0700182 // 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());
187 std::transform(
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; });
191
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;
195
196 for (seperate_rects::RectSet<uint64_t, int> &region : seperate_regions) {
197 if (region.id_set.getBits() & exclude_mask)
198 continue;
199 regions.emplace_back(DrmCompositionRegion{
200 region.rect,
201 SetBitsToVector(region.id_set.getBits() >> num_exclude_rects,
202 used_layers)});
203 }
Zach Reizner713a6782015-07-31 15:12:44 -0700204}
205
Zach Reizner92f8e632015-10-12 17:47:13 -0700206int 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());
213 return -ENODEV;
214 }
Zach Reizner09807052015-08-13 14:53:41 -0700215
Zach Reizner92f8e632015-10-12 17:47:13 -0700216 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);
Zach Reizner09807052015-08-13 14:53:41 -0700220
Zach Reizner92f8e632015-10-12 17:47:13 -0700221 std::vector<bool> stable_regions;
222 squash->StableRegionsWithMarginalHistory(changed_regions, stable_regions);
Zach Reizner09807052015-08-13 14:53:41 -0700223
Zach Reizner92f8e632015-10-12 17:47:13 -0700224 squash->RecordHistory(layers_.data(), changed_regions);
225
226 squash->RecordSquashed(stable_regions);
227
228 for (size_t region_index = 0; region_index < stable_regions.size();
229 region_index++) {
230 const SquashState::Region &region = 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;
236 layer_index++) {
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();
240 }
Zach Reizner09807052015-08-13 14:53:41 -0700241 }
Zach Reizner09807052015-08-13 14:53:41 -0700242 }
243 }
244 }
245
Zach Reizner92f8e632015-10-12 17:47:13 -0700246 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()) {
251 continue;
252 }
253
254 layers_remaining.push_back(layer_index);
255 }
256
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);
261 }
262
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);
275 }
276
277 for (size_t i : layers_remaining) {
278 composition_planes_.emplace_back(DrmCompositionPlane{
279 TakePlane(crtc_, primary_planes, overlay_planes), crtc_, i});
280 }
281
282 std::unordered_set<DrmHwcLayer *> squash_layers;
283 std::unordered_set<DrmHwcLayer *> pre_comp_layers;
284 std::unordered_set<DrmHwcLayer *> comp_layers;
285
286 for (const DrmCompositionRegion &region : 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);
290 }
291 }
292
293 for (const DrmCompositionRegion &region : 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);
298 }
299 }
300
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);
306 }
307 }
308
309 for (DrmHwcLayer *layer : squash_layers) {
310 int ret = layer->release_fence.Set(CreateNextTimelineFence());
311 if (ret < 0)
312 return ret;
313 }
314 timeline_squash_done_ = timeline_;
315
316 for (DrmHwcLayer *layer : pre_comp_layers) {
317 int ret = layer->release_fence.Set(CreateNextTimelineFence());
318 if (ret < 0)
319 return ret;
320 }
Zach Reizner09807052015-08-13 14:53:41 -0700321 timeline_pre_comp_done_ = timeline_;
322
Zach Reizner92f8e632015-10-12 17:47:13 -0700323 for (DrmHwcLayer *layer : comp_layers) {
324 int ret = layer->release_fence.Set(CreateNextTimelineFence());
325 if (ret < 0)
326 return ret;
Zach Reizner09807052015-08-13 14:53:41 -0700327 }
328
Zach Reizner09807052015-08-13 14:53:41 -0700329 return 0;
Zach Reizner46ddd452015-07-30 08:10:14 -0700330}
Sean Paul98e73c82015-06-24 14:38:49 -0700331}