blob: aed090aed891a50d325d2996bc9251e53af80ed7 [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 ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-display-compositor"
19
20#include "drmdisplaycompositor.h"
Zach Reiznerbff33ac2015-11-16 11:08:46 -080021
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030022#include <drm/drm_mode.h>
23#include <log/log.h>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080024#include <pthread.h>
25#include <sched.h>
26#include <stdlib.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030027#include <sync/sync.h>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080028#include <time.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030029#include <utils/Trace.h>
30
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020031#include <array>
Zach Reiznerbff33ac2015-11-16 11:08:46 -080032#include <sstream>
33#include <vector>
34
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030035#include "drm/drmcrtc.h"
36#include "drm/drmdevice.h"
37#include "drm/drmplane.h"
38#include "utils/autolock.h"
Sean Paul98e73c82015-06-24 14:38:49 -070039
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010040static const uint32_t kWaitWritebackFence = 100; // ms
41
Sean Paul98e73c82015-06-24 14:38:49 -070042namespace android {
43
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020044std::ostream &operator<<(std::ostream &str, FlatteningState state) {
45 std::array<const char *, 6> flattenting_state_str = {
46 "None", "Not needed", "SF Requested", "Squashed by GPU",
47 "Serial", "Concurrent",
48 };
49
50 return str << flattenting_state_str[static_cast<int>(state)];
51}
52
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010053class CompositorVsyncCallback : public VsyncCallback {
54 public:
55 CompositorVsyncCallback(DrmDisplayCompositor *compositor)
56 : compositor_(compositor) {
57 }
58
59 void Callback(int display, int64_t timestamp) {
60 compositor_->Vsync(display, timestamp);
61 }
62
63 private:
64 DrmDisplayCompositor *compositor_;
65};
66
Sean Paul98e73c82015-06-24 14:38:49 -070067DrmDisplayCompositor::DrmDisplayCompositor()
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +010068 : resource_manager_(NULL),
Sean Paul98e73c82015-06-24 14:38:49 -070069 display_(-1),
Sean Paul98e73c82015-06-24 14:38:49 -070070 initialized_(false),
Sean Pauldb7a17d2015-06-24 18:46:05 -070071 active_(false),
Sean Paul6c18b3b2015-11-25 11:04:25 -050072 use_hw_overlays_(true),
Sean Paul98e73c82015-06-24 14:38:49 -070073 dump_frames_composited_(0),
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010074 dump_last_timestamp_ns_(0),
75 flatten_countdown_(FLATTEN_COUNTDOWN_INIT),
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020076 writeback_fence_(-1),
Roman Kovalivskyi9170b312020-02-03 18:13:57 +020077 flattening_state_(FlatteningState::kNone),
78 frames_flattened_(0) {
Sean Paul98e73c82015-06-24 14:38:49 -070079 struct timespec ts;
80 if (clock_gettime(CLOCK_MONOTONIC, &ts))
81 return;
82 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
83}
84
85DrmDisplayCompositor::~DrmDisplayCompositor() {
86 if (!initialized_)
87 return;
88
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010089 vsync_worker_.Exit();
Sean Paul98e73c82015-06-24 14:38:49 -070090 int ret = pthread_mutex_lock(&lock_);
91 if (ret)
92 ALOGE("Failed to acquire compositor lock %d", ret);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010093 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
Sean Paul35301f42015-11-17 14:46:56 -050094 if (mode_.blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010095 drm->DestroyPropertyBlob(mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050096 if (mode_.old_blob_id)
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +010097 drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul35301f42015-11-17 14:46:56 -050098
Sean Paul98e73c82015-06-24 14:38:49 -070099 active_composition_.reset();
100
101 ret = pthread_mutex_unlock(&lock_);
102 if (ret)
103 ALOGE("Failed to acquire compositor lock %d", ret);
104
105 pthread_mutex_destroy(&lock_);
106}
107
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100108int DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display) {
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100109 resource_manager_ = resource_manager;
Sean Paul98e73c82015-06-24 14:38:49 -0700110 display_ = display;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100111 DrmDevice *drm = resource_manager_->GetDrmDevice(display);
112 if (!drm) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100113 ALOGE("Could not find drmdevice for display");
114 return -EINVAL;
115 }
Sean Paul98e73c82015-06-24 14:38:49 -0700116 int ret = pthread_mutex_init(&lock_, NULL);
117 if (ret) {
118 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
119 return ret;
120 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100121 planner_ = Planner::CreateInstance(drm);
122
123 vsync_worker_.Init(drm, display_);
124 auto callback = std::make_shared<CompositorVsyncCallback>(this);
125 vsync_worker_.RegisterCallback(callback);
Sean Paul98e73c82015-06-24 14:38:49 -0700126
127 initialized_ = true;
128 return 0;
129}
130
Zach Reizner92f8e632015-10-12 17:47:13 -0700131std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
132 const {
133 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
134}
135
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100136std::unique_ptr<DrmDisplayComposition>
137DrmDisplayCompositor::CreateInitializedComposition() const {
138 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
139 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
140 if (!crtc) {
141 ALOGE("Failed to find crtc for display = %d", display_);
142 return std::unique_ptr<DrmDisplayComposition>();
143 }
144 std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
145 std::shared_ptr<Importer> importer = resource_manager_->GetImporter(display_);
146 if (!importer) {
147 ALOGE("Failed to find resources for display = %d", display_);
148 return std::unique_ptr<DrmDisplayComposition>();
149 }
150 int ret = comp->Init(drm, crtc, importer.get(), planner_.get(), 0);
151 if (ret) {
152 ALOGE("Failed to init composition for display = %d", display_);
153 return std::unique_ptr<DrmDisplayComposition>();
154 }
155 return comp;
156}
157
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200158FlatteningState DrmDisplayCompositor::GetFlatteningState() const {
159 return flattening_state_;
160}
161
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200162uint32_t DrmDisplayCompositor::GetFlattenedFramesCount() const {
163 return frames_flattened_;
164}
165
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200166bool DrmDisplayCompositor::ShouldFlattenOnClient() const {
167 return flattening_state_ == FlatteningState::kClientRequested ||
168 flattening_state_ == FlatteningState::kClientDone;
169}
170
Zach Reizner92f8e632015-10-12 17:47:13 -0700171std::tuple<uint32_t, uint32_t, int>
172DrmDisplayCompositor::GetActiveModeResolution() {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100173 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
174 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Zach Reizner92f8e632015-10-12 17:47:13 -0700175 if (connector == NULL) {
176 ALOGE("Failed to determine display mode: no connector for display %d",
177 display_);
178 return std::make_tuple(0, 0, -ENODEV);
179 }
180
181 const DrmMode &mode = connector->active_mode();
182 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
Zach Reizner713a6782015-07-31 15:12:44 -0700183}
184
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400185int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600186 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400187 if (!pset) {
188 ALOGE("Failed to allocate property set");
189 return -ENOMEM;
190 }
191
192 int ret;
Sean Paulf72cccd2018-08-27 13:59:08 -0400193 std::vector<DrmCompositionPlane> &comp_planes = display_comp
194 ->composition_planes();
Zach Reizner92f8e632015-10-12 17:47:13 -0700195 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400196 DrmPlane *plane = comp_plane.plane();
Rob Herringbc9305e2016-02-04 14:01:24 -0600197 ret = drmModeAtomicAddProperty(pset, plane->id(),
198 plane->crtc_property().id(), 0) < 0 ||
199 drmModeAtomicAddProperty(pset, plane->id(), plane->fb_property().id(),
200 0) < 0;
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400201 if (ret) {
202 ALOGE("Failed to add plane %d disable to pset", plane->id());
Rob Herringbc9305e2016-02-04 14:01:24 -0600203 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400204 return ret;
205 }
206 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100207 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
208 ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400209 if (ret) {
210 ALOGE("Failed to commit pset ret=%d\n", ret);
Rob Herringbc9305e2016-02-04 14:01:24 -0600211 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400212 return ret;
213 }
214
Rob Herringbc9305e2016-02-04 14:01:24 -0600215 drmModeAtomicFree(pset);
Sean Paul7b1e4bc2015-10-13 15:47:22 -0400216 return 0;
217}
218
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100219int DrmDisplayCompositor::SetupWritebackCommit(drmModeAtomicReqPtr pset,
220 uint32_t crtc_id,
221 DrmConnector *writeback_conn,
222 DrmHwcBuffer *writeback_buffer) {
223 int ret = 0;
224 if (writeback_conn->writeback_fb_id().id() == 0 ||
225 writeback_conn->writeback_out_fence().id() == 0) {
226 ALOGE("Writeback properties don't exit");
227 return -EINVAL;
228 }
229 if ((*writeback_buffer)->fb_id == 0) {
230 ALOGE("Invalid writeback buffer");
231 return -EINVAL;
232 }
233 ret = drmModeAtomicAddProperty(pset, writeback_conn->id(),
234 writeback_conn->writeback_fb_id().id(),
235 (*writeback_buffer)->fb_id);
236 if (ret < 0) {
237 ALOGE("Failed to add writeback_fb_id");
238 return ret;
239 }
240 ret = drmModeAtomicAddProperty(pset, writeback_conn->id(),
241 writeback_conn->writeback_out_fence().id(),
242 (uint64_t)&writeback_fence_);
243 if (ret < 0) {
244 ALOGE("Failed to add writeback_out_fence");
245 return ret;
246 }
247
248 ret = drmModeAtomicAddProperty(pset, writeback_conn->id(),
249 writeback_conn->crtc_id_property().id(),
250 crtc_id);
251 if (ret < 0) {
252 ALOGE("Failed to attach writeback");
253 return ret;
254 }
255 return 0;
256}
257
Sean Paulc07b2112015-11-17 16:38:10 -0500258int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100259 bool test_only,
260 DrmConnector *writeback_conn,
261 DrmHwcBuffer *writeback_buffer) {
Haixia Shi3979f7d2015-10-29 14:33:37 -0700262 ATRACE_CALL();
263
Haixia Shidda2fab2015-10-22 18:12:49 -0700264 int ret = 0;
265
266 std::vector<DrmHwcLayer> &layers = display_comp->layers();
Sean Paulf72cccd2018-08-27 13:59:08 -0400267 std::vector<DrmCompositionPlane> &comp_planes = display_comp
268 ->composition_planes();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100269 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
270 uint64_t out_fences[drm->crtcs().size()];
Haixia Shidda2fab2015-10-22 18:12:49 -0700271
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100272 DrmConnector *connector = drm->GetConnectorForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400273 if (!connector) {
274 ALOGE("Could not locate connector for display %d", display_);
275 return -ENODEV;
276 }
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100277 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
Sean Paul57355412015-09-19 09:14:34 -0400278 if (!crtc) {
279 ALOGE("Could not locate crtc for display %d", display_);
280 return -ENODEV;
281 }
282
Rob Herringbc9305e2016-02-04 14:01:24 -0600283 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
Sean Paul98e73c82015-06-24 14:38:49 -0700284 if (!pset) {
285 ALOGE("Failed to allocate property set");
286 return -ENOMEM;
287 }
288
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100289 if (writeback_buffer != NULL) {
290 if (writeback_conn == NULL) {
291 ALOGE("Invalid arguments requested writeback without writeback conn");
292 return -EINVAL;
293 }
294 ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn,
295 writeback_buffer);
296 if (ret < 0) {
297 ALOGE("Failed to Setup Writeback Commit ret = %d", ret);
298 return ret;
299 }
300 }
Robert Fossa1ade4e2017-09-27 19:28:15 +0200301 if (crtc->out_fence_ptr_property().id() != 0) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400302 ret = drmModeAtomicAddProperty(pset, crtc->id(),
303 crtc->out_fence_ptr_property().id(),
304 (uint64_t)&out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200305 if (ret < 0) {
306 ALOGE("Failed to add OUT_FENCE_PTR property to pset: %d", ret);
307 drmModeAtomicFree(pset);
308 return ret;
309 }
310 }
311
Sean Paul35301f42015-11-17 14:46:56 -0500312 if (mode_.needs_modeset) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400313 ret = drmModeAtomicAddProperty(pset, crtc->id(),
314 crtc->active_property().id(), 1);
John Stultzb365b792018-01-23 15:16:36 -0800315 if (ret < 0) {
316 ALOGE("Failed to add crtc active to pset\n");
317 drmModeAtomicFree(pset);
318 return ret;
319 }
320
Rob Herringbc9305e2016-02-04 14:01:24 -0600321 ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(),
322 mode_.blob_id) < 0 ||
323 drmModeAtomicAddProperty(pset, connector->id(),
324 connector->crtc_id_property().id(),
325 crtc->id()) < 0;
Sean Paul57355412015-09-19 09:14:34 -0400326 if (ret) {
Sean Paul35301f42015-11-17 14:46:56 -0500327 ALOGE("Failed to add blob %d to pset", mode_.blob_id);
Rob Herringbc9305e2016-02-04 14:01:24 -0600328 drmModeAtomicFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400329 return ret;
330 }
331 }
332
Zach Reizner92f8e632015-10-12 17:47:13 -0700333 for (DrmCompositionPlane &comp_plane : comp_planes) {
Sean Paulca699be2016-05-11 16:29:45 -0400334 DrmPlane *plane = comp_plane.plane();
335 DrmCrtc *crtc = comp_plane.crtc();
336 std::vector<size_t> &source_layers = comp_plane.source_layers();
Zach Reizner92f8e632015-10-12 17:47:13 -0700337
338 int fb_id = -1;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400339 int fence_fd = -1;
Rob Herringcff7b1e2018-05-09 15:18:36 -0500340 hwc_rect_t display_frame;
341 hwc_frect_t source_crop;
Zach Reizner92f8e632015-10-12 17:47:13 -0700342 uint64_t rotation = 0;
Stefan Schake025d0a62018-05-04 18:03:00 +0200343 uint64_t alpha = 0xFFFF;
Lowry Li9b6cafd2018-08-28 17:58:21 +0800344 uint64_t blend;
Sean Paul04b47ea2015-11-19 21:46:11 -0500345
Sean Paulbbe39db2016-05-11 16:57:26 -0400346 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400347 if (source_layers.size() > 1) {
348 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
349 source_layers.size(), comp_plane.type());
350 continue;
351 }
352
353 if (source_layers.empty() || source_layers.front() >= layers.size()) {
354 ALOGE("Source layer index %zu out of bounds %zu type=%d",
355 source_layers.front(), layers.size(), comp_plane.type());
Sean Paul39b37842016-05-11 13:50:28 -0400356 break;
Sean Paul98e73c82015-06-24 14:38:49 -0700357 }
Sean Paulca699be2016-05-11 16:29:45 -0400358 DrmHwcLayer &layer = layers[source_layers.front()];
Sean Paul39b37842016-05-11 13:50:28 -0400359 if (!layer.buffer) {
360 ALOGE("Expected a valid framebuffer for pset");
361 break;
362 }
363 fb_id = layer.buffer->fb_id;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400364 fence_fd = layer.acquire_fence.get();
Sean Paul39b37842016-05-11 13:50:28 -0400365 display_frame = layer.display_frame;
366 source_crop = layer.source_crop;
Lowry Li9b6cafd2018-08-28 17:58:21 +0800367 alpha = layer.alpha;
368
369 if (plane->blend_property().id()) {
370 switch (layer.blending) {
371 case DrmHwcBlending::kPreMult:
372 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
373 "Pre-multiplied");
374 break;
375 case DrmHwcBlending::kCoverage:
376 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
377 "Coverage");
378 break;
379 case DrmHwcBlending::kNone:
380 default:
381 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
382 "None");
383 break;
384 }
385 }
Sean Paul98e73c82015-06-24 14:38:49 -0700386
Sean Paul2689aeb2019-03-13 14:36:52 -0400387 if (plane->zpos_property().id() &&
388 !plane->zpos_property().is_immutable()) {
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200389 uint64_t min_zpos = 0;
390
391 // Ignore ret and use min_zpos as 0 by default
392 std::tie(std::ignore, min_zpos) = plane->zpos_property().range_min();
393
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100394 ret = drmModeAtomicAddProperty(pset, plane->id(),
395 plane->zpos_property().id(),
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200396 source_layers.front() + min_zpos) < 0;
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100397 if (ret) {
398 ALOGE("Failed to add zpos property %d to plane %d",
399 plane->zpos_property().id(), plane->id());
400 break;
401 }
402 }
403
Sean Paul39b37842016-05-11 13:50:28 -0400404 rotation = 0;
405 if (layer.transform & DrmHwcTransform::kFlipH)
Rob Herring89095cc2017-10-06 16:46:48 -0500406 rotation |= DRM_MODE_REFLECT_X;
Sean Paul39b37842016-05-11 13:50:28 -0400407 if (layer.transform & DrmHwcTransform::kFlipV)
Rob Herring89095cc2017-10-06 16:46:48 -0500408 rotation |= DRM_MODE_REFLECT_Y;
Sean Paul39b37842016-05-11 13:50:28 -0400409 if (layer.transform & DrmHwcTransform::kRotate90)
Rob Herring89095cc2017-10-06 16:46:48 -0500410 rotation |= DRM_MODE_ROTATE_90;
Sean Paul39b37842016-05-11 13:50:28 -0400411 else if (layer.transform & DrmHwcTransform::kRotate180)
Rob Herring89095cc2017-10-06 16:46:48 -0500412 rotation |= DRM_MODE_ROTATE_180;
Sean Paul39b37842016-05-11 13:50:28 -0400413 else if (layer.transform & DrmHwcTransform::kRotate270)
Rob Herring89095cc2017-10-06 16:46:48 -0500414 rotation |= DRM_MODE_ROTATE_270;
Rob Herringbd03b992017-11-01 11:21:48 -0500415 else
416 rotation |= DRM_MODE_ROTATE_0;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400417
Rob Herring8428e6a2018-02-12 16:03:35 -0600418 if (fence_fd >= 0) {
Robert Fossb2bdfd82016-10-19 10:46:22 -0400419 int prop_id = plane->in_fence_fd_property().id();
420 if (prop_id == 0) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400421 ALOGE("Failed to get IN_FENCE_FD property id");
422 break;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400423 }
424 ret = drmModeAtomicAddProperty(pset, plane->id(), prop_id, fence_fd);
425 if (ret < 0) {
426 ALOGE("Failed to add IN_FENCE_FD property to pset: %d", ret);
427 break;
428 }
429 }
Sean Paul39b37842016-05-11 13:50:28 -0400430 }
Robert Fossb2bdfd82016-10-19 10:46:22 -0400431
Zach Reizner92f8e632015-10-12 17:47:13 -0700432 // Disable the plane if there's no framebuffer
433 if (fb_id < 0) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600434 ret = drmModeAtomicAddProperty(pset, plane->id(),
435 plane->crtc_property().id(), 0) < 0 ||
436 drmModeAtomicAddProperty(pset, plane->id(),
437 plane->fb_property().id(), 0) < 0;
Sean Paul2e46fbd2015-07-09 17:22:22 -0400438 if (ret) {
439 ALOGE("Failed to add plane %d disable to pset", plane->id());
440 break;
441 }
442 continue;
443 }
444
Rob Herringbc9305e2016-02-04 14:01:24 -0600445 ret = drmModeAtomicAddProperty(pset, plane->id(),
446 plane->crtc_property().id(), crtc->id()) < 0;
447 ret |= drmModeAtomicAddProperty(pset, plane->id(),
448 plane->fb_property().id(), fb_id) < 0;
449 ret |= drmModeAtomicAddProperty(pset, plane->id(),
450 plane->crtc_x_property().id(),
451 display_frame.left) < 0;
452 ret |= drmModeAtomicAddProperty(pset, plane->id(),
453 plane->crtc_y_property().id(),
454 display_frame.top) < 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400455 ret |= drmModeAtomicAddProperty(pset, plane->id(),
456 plane->crtc_w_property().id(),
457 display_frame.right - display_frame.left) <
458 0;
459 ret |= drmModeAtomicAddProperty(pset, plane->id(),
460 plane->crtc_h_property().id(),
461 display_frame.bottom - display_frame.top) <
462 0;
Rob Herringbc9305e2016-02-04 14:01:24 -0600463 ret |= drmModeAtomicAddProperty(pset, plane->id(),
464 plane->src_x_property().id(),
465 (int)(source_crop.left) << 16) < 0;
466 ret |= drmModeAtomicAddProperty(pset, plane->id(),
467 plane->src_y_property().id(),
468 (int)(source_crop.top) << 16) < 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400469 ret |= drmModeAtomicAddProperty(pset, plane->id(),
470 plane->src_w_property().id(),
471 (int)(source_crop.right - source_crop.left)
472 << 16) < 0;
473 ret |= drmModeAtomicAddProperty(pset, plane->id(),
474 plane->src_h_property().id(),
475 (int)(source_crop.bottom - source_crop.top)
476 << 16) < 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700477 if (ret) {
478 ALOGE("Failed to add plane %d to set", plane->id());
479 break;
480 }
Sean Paul1c4c3262015-07-14 15:51:52 -0400481
482 if (plane->rotation_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600483 ret = drmModeAtomicAddProperty(pset, plane->id(),
484 plane->rotation_property().id(),
485 rotation) < 0;
Sean Paul1c4c3262015-07-14 15:51:52 -0400486 if (ret) {
487 ALOGE("Failed to add rotation property %d to plane %d",
488 plane->rotation_property().id(), plane->id());
489 break;
490 }
491 }
Sean Pauld8aefb62015-10-15 15:17:31 -0400492
493 if (plane->alpha_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600494 ret = drmModeAtomicAddProperty(pset, plane->id(),
Sean Paulf72cccd2018-08-27 13:59:08 -0400495 plane->alpha_property().id(), alpha) < 0;
Sean Pauld8aefb62015-10-15 15:17:31 -0400496 if (ret) {
497 ALOGE("Failed to add alpha property %d to plane %d",
498 plane->alpha_property().id(), plane->id());
499 break;
500 }
501 }
Lowry Li9b6cafd2018-08-28 17:58:21 +0800502
503 if (plane->blend_property().id()) {
504 ret = drmModeAtomicAddProperty(pset, plane->id(),
505 plane->blend_property().id(), blend) < 0;
506 if (ret) {
507 ALOGE("Failed to add pixel blend mode property %d to plane %d",
508 plane->blend_property().id(), plane->id());
509 break;
510 }
511 }
Sean Paul98e73c82015-06-24 14:38:49 -0700512 }
513
514 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500515 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
516 if (test_only)
517 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
518
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100519 ret = drmModeAtomicCommit(drm->fd(), pset, flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400520 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700521 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500522 ALOGE("Failed to commit pset ret=%d\n", ret);
Rob Herringbc9305e2016-02-04 14:01:24 -0600523 drmModeAtomicFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400524 return ret;
525 }
Sean Paul98e73c82015-06-24 14:38:49 -0700526 }
527 if (pset)
Rob Herringbc9305e2016-02-04 14:01:24 -0600528 drmModeAtomicFree(pset);
Sean Paul98e73c82015-06-24 14:38:49 -0700529
Sean Paulc07b2112015-11-17 16:38:10 -0500530 if (!test_only && mode_.needs_modeset) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100531 ret = drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400532 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400533 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
Sean Paul35301f42015-11-17 14:46:56 -0500534 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400535 return ret;
536 }
537
538 /* TODO: Add dpms to the pset when the kernel supports it */
539 ret = ApplyDpms(display_comp);
540 if (ret) {
541 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
542 return ret;
543 }
544
Sean Paul35301f42015-11-17 14:46:56 -0500545 connector->set_active_mode(mode_.mode);
546 mode_.old_blob_id = mode_.blob_id;
547 mode_.blob_id = 0;
548 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400549 }
550
Robert Fossa1ade4e2017-09-27 19:28:15 +0200551 if (crtc->out_fence_ptr_property().id()) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400552 display_comp->set_out_fence((int)out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200553 }
554
Sean Paul98e73c82015-06-24 14:38:49 -0700555 return ret;
556}
557
Sean Pauldb7a17d2015-06-24 18:46:05 -0700558int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100559 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
560 DrmConnector *conn = drm->GetConnectorForDisplay(display_);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700561 if (!conn) {
562 ALOGE("Failed to get DrmConnector for display %d", display_);
563 return -ENODEV;
564 }
565
566 const DrmProperty &prop = conn->dpms_property();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100567 int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700568 display_comp->dpms_mode());
569 if (ret) {
570 ALOGE("Failed to set DPMS property for connector %d", conn->id());
571 return ret;
572 }
573 return 0;
574}
575
Sean Paul35301f42015-11-17 14:46:56 -0500576std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
577 const DrmMode &mode) {
578 struct drm_mode_modeinfo drm_mode;
579 memset(&drm_mode, 0, sizeof(drm_mode));
580 mode.ToDrmModeModeInfo(&drm_mode);
581
582 uint32_t id = 0;
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100583 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
584 int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
585 &id);
Sean Paul35301f42015-11-17 14:46:56 -0500586 if (ret) {
587 ALOGE("Failed to create mode property blob %d", ret);
588 return std::make_tuple(ret, 0);
589 }
Sean Paulf741c672016-05-11 13:49:38 -0400590 ALOGE("Create blob_id %" PRIu32 "\n", id);
Sean Paul35301f42015-11-17 14:46:56 -0500591 return std::make_tuple(ret, id);
592}
593
Sean Paul137a6a82016-06-22 22:48:22 -0400594void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400595 if (!active_composition_)
596 return;
597
598 if (DisablePlanes(active_composition_.get()))
599 return;
600
Sean Paul137a6a82016-06-22 22:48:22 -0400601 active_composition_.reset(NULL);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100602 vsync_worker_.VSyncControl(false);
Sean Paul137a6a82016-06-22 22:48:22 -0400603}
604
Haixia Shidda2fab2015-10-22 18:12:49 -0700605void DrmDisplayCompositor::ApplyFrame(
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100606 std::unique_ptr<DrmDisplayComposition> composition, int status,
607 bool writeback) {
Alexandru Gheorghedb440a92018-03-29 14:38:21 +0100608 AutoLock lock(&lock_, __func__);
609 if (lock.Lock())
610 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700611 int ret = status;
612
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100613 if (!ret) {
614 if (writeback && !CountdownExpired()) {
615 ALOGE("Abort playing back scene");
616 return;
617 }
Sean Paulc07b2112015-11-17 16:38:10 -0500618 ret = CommitFrame(composition.get(), false);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100619 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700620
621 if (ret) {
622 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700623 // Disable the hw used by the last active composition. This allows us to
624 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400625 ClearDisplay();
626 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700627 }
628 ++dump_frames_composited_;
629
Haixia Shidda2fab2015-10-22 18:12:49 -0700630 active_composition_.swap(composition);
631
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100632 flatten_countdown_ = FLATTEN_COUNTDOWN_INIT;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200633 if (flattening_state_ != FlatteningState::kClientRequested) {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200634 SetFlattening(FlatteningState::kNone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200635 } else {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200636 SetFlattening(FlatteningState::kClientDone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200637 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100638 vsync_worker_.VSyncControl(!writeback);
Haixia Shidda2fab2015-10-22 18:12:49 -0700639}
640
Sean Pauled45a8e2017-02-28 13:17:34 -0500641int DrmDisplayCompositor::ApplyComposition(
642 std::unique_ptr<DrmDisplayComposition> composition) {
643 int ret = 0;
Sean Paulacb2a442015-06-24 18:43:01 -0700644 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700645 case DRM_COMPOSITION_TYPE_FRAME:
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800646 if (composition->geometry_changed()) {
647 // Send the composition to the kernel to ensure we can commit it. This
Rob Herringaf0d9752018-05-04 16:34:19 -0500648 // is just a test, it won't actually commit the frame.
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800649 ret = CommitFrame(composition.get(), true);
Rob Herringaf0d9752018-05-04 16:34:19 -0500650 if (ret) {
651 ALOGE("Commit test failed for display %d, FIXME", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500652 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500653 }
654 }
Rob Herringaf0d9752018-05-04 16:34:19 -0500655
Sean Pauled45a8e2017-02-28 13:17:34 -0500656 ApplyFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700657 break;
658 case DRM_COMPOSITION_TYPE_DPMS:
Sean Pauled45a8e2017-02-28 13:17:34 -0500659 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700660 ret = ApplyDpms(composition.get());
661 if (ret)
662 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700663 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400664 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500665 mode_.mode = composition->display_mode();
666 if (mode_.blob_id)
Sean Paulf72cccd2018-08-27 13:59:08 -0400667 resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob(
668 mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -0500669 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
670 if (ret) {
671 ALOGE("Failed to create mode blob for display %d", display_);
672 return ret;
673 }
674 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400675 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700676 default:
677 ALOGE("Unknown composition type %d", composition->type());
678 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700679 }
Sean Paul98e73c82015-06-24 14:38:49 -0700680
Sean Paul98e73c82015-06-24 14:38:49 -0700681 return ret;
682}
683
Rob Herring4f6c62e2018-05-17 14:33:02 -0500684int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
685 return CommitFrame(composition, true);
686}
687
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100688// Flatten a scene on the display by using a writeback connector
689// and returns the composition result as a DrmHwcLayer.
690int DrmDisplayCompositor::FlattenOnDisplay(
691 std::unique_ptr<DrmDisplayComposition> &src, DrmConnector *writeback_conn,
692 DrmMode &src_mode, DrmHwcLayer *writeback_layer) {
693 int ret = 0;
694 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
695 ret = writeback_conn->UpdateModes();
696 if (ret) {
697 ALOGE("Failed to update modes %d", ret);
698 return ret;
699 }
700 for (const DrmMode &mode : writeback_conn->modes()) {
701 if (mode.h_display() == src_mode.h_display() &&
702 mode.v_display() == src_mode.v_display()) {
703 mode_.mode = mode;
704 if (mode_.blob_id)
705 drm->DestroyPropertyBlob(mode_.blob_id);
706 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
707 if (ret) {
708 ALOGE("Failed to create mode blob for display %d", display_);
709 return ret;
710 }
711 mode_.needs_modeset = true;
712 break;
713 }
714 }
715 if (mode_.blob_id <= 0) {
716 ALOGE("Failed to find similar mode");
717 return -EINVAL;
718 }
719
720 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
721 if (!crtc) {
722 ALOGE("Failed to find crtc for display %d", display_);
723 return -EINVAL;
724 }
725 // TODO what happens if planes could go to both CRTCs, I don't think it's
726 // handled anywhere
727 std::vector<DrmPlane *> primary_planes;
728 std::vector<DrmPlane *> overlay_planes;
729 for (auto &plane : drm->planes()) {
730 if (!plane->GetCrtcSupported(*crtc))
731 continue;
732 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
733 primary_planes.push_back(plane.get());
734 else if (plane->type() == DRM_PLANE_TYPE_OVERLAY)
735 overlay_planes.push_back(plane.get());
736 }
737
738 ret = src->Plan(&primary_planes, &overlay_planes);
739 if (ret) {
740 ALOGE("Failed to plan the composition ret = %d", ret);
741 return ret;
742 }
743
744 // Disable the planes we're not using
745 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
746 src->AddPlaneDisable(*i);
747 i = primary_planes.erase(i);
748 }
749 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
750 src->AddPlaneDisable(*i);
751 i = overlay_planes.erase(i);
752 }
753
754 AutoLock lock(&lock_, __func__);
755 ret = lock.Lock();
756 if (ret)
757 return ret;
758 DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_];
759 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
760 if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) {
761 ALOGE("Failed to allocate writeback buffer");
762 return -ENOMEM;
763 }
764 DrmHwcBuffer *writeback_buffer = &writeback_layer->buffer;
765 writeback_layer->sf_handle = writeback_fb->buffer()->handle;
766 ret = writeback_layer->ImportBuffer(
767 resource_manager_->GetImporter(display_).get());
768 if (ret) {
769 ALOGE("Failed to import writeback buffer");
770 return ret;
771 }
772
773 ret = CommitFrame(src.get(), true, writeback_conn, writeback_buffer);
774 if (ret) {
775 ALOGE("Atomic check failed");
776 return ret;
777 }
778 ret = CommitFrame(src.get(), false, writeback_conn, writeback_buffer);
779 if (ret) {
780 ALOGE("Atomic commit failed");
781 return ret;
782 }
783
784 ret = sync_wait(writeback_fence_, kWaitWritebackFence);
785 writeback_layer->acquire_fence.Set(writeback_fence_);
786 writeback_fence_ = -1;
787 if (ret) {
788 ALOGE("Failed to wait on writeback fence");
789 return ret;
790 }
791 return 0;
792}
793
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200794void DrmDisplayCompositor::SetFlattening(FlatteningState new_state) {
795 if (flattening_state_ != new_state) {
796 switch (flattening_state_) {
797 case FlatteningState::kClientDone:
798 case FlatteningState::kConcurrent:
799 case FlatteningState::kSerial:
800 ++frames_flattened_;
801 break;
802 case FlatteningState::kClientRequested:
803 case FlatteningState::kNone:
804 case FlatteningState::kNotNeeded:
805 break;
806 }
807 }
808 flattening_state_ = new_state;
809}
810
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200811bool DrmDisplayCompositor::IsFlatteningNeeded() const {
812 return CountdownExpired() && active_composition_->layers().size() >= 2;
813}
814
815int DrmDisplayCompositor::FlattenOnClient() {
816 if (refresh_display_cb_) {
817 {
818 AutoLock lock(&lock_, __func__);
819 if (!IsFlatteningNeeded()) {
820 if (flattening_state_ != FlatteningState::kClientDone) {
821 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200822 SetFlattening(FlatteningState::kNotNeeded);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200823 }
824 return -EALREADY;
825 }
826 }
827
828 ALOGV(
829 "No writeback connector available, "
830 "falling back to client composition");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200831 SetFlattening(FlatteningState::kClientRequested);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200832 refresh_display_cb_(display_);
833 return 0;
834 } else {
835 ALOGV("No writeback connector available");
836 return -EINVAL;
837 }
838}
839
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100840// Flatten a scene by enabling the writeback connector attached
841// to the same CRTC as the one driving the display.
842int DrmDisplayCompositor::FlattenSerial(DrmConnector *writeback_conn) {
843 ALOGV("FlattenSerial by enabling writeback connector to the same crtc");
844 // Flattened composition with only one layer that is obtained
845 // using the writeback connector
Sean Paulf72cccd2018-08-27 13:59:08 -0400846 std::unique_ptr<DrmDisplayComposition>
847 writeback_comp = CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100848 if (!writeback_comp)
849 return -EINVAL;
850
851 AutoLock lock(&lock_, __func__);
852 int ret = lock.Lock();
853 if (ret)
854 return ret;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200855 if (!IsFlatteningNeeded()) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100856 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200857 SetFlattening(FlatteningState::kNotNeeded);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100858 return -EALREADY;
859 }
860
861 DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_];
862 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
863 lock.Unlock();
864
865 if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) {
866 ALOGE("Failed to allocate writeback buffer");
867 return -ENOMEM;
868 }
869 writeback_comp->layers().emplace_back();
870
871 DrmHwcLayer &writeback_layer = writeback_comp->layers().back();
872 writeback_layer.sf_handle = writeback_fb->buffer()->handle;
873 writeback_layer.source_crop = {0, 0, (float)mode_.mode.h_display(),
874 (float)mode_.mode.v_display()};
875 writeback_layer.display_frame = {0, 0, (int)mode_.mode.h_display(),
876 (int)mode_.mode.v_display()};
877 ret = writeback_layer.ImportBuffer(
878 resource_manager_->GetImporter(display_).get());
879 if (ret || writeback_comp->layers().size() != 1) {
880 ALOGE("Failed to import writeback buffer");
881 return ret;
882 }
883
884 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
885 if (!pset) {
886 ALOGE("Failed to allocate property set");
887 return -ENOMEM;
888 }
889 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
890 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
891 if (!crtc) {
892 ALOGE("Failed to find crtc for display %d", display_);
893 return -EINVAL;
894 }
895 ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn,
896 &writeback_layer.buffer);
897 if (ret < 0) {
898 ALOGE("Failed to Setup Writeback Commit");
899 return ret;
900 }
901 ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm);
902 if (ret) {
903 ALOGE("Failed to enable writeback %d", ret);
904 return ret;
905 }
906 ret = sync_wait(writeback_fence_, kWaitWritebackFence);
907 writeback_layer.acquire_fence.Set(writeback_fence_);
908 writeback_fence_ = -1;
909 if (ret) {
910 ALOGE("Failed to wait on writeback fence");
911 return ret;
912 }
913
914 DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL,
915 crtc);
916 for (auto &drmplane : drm->planes()) {
917 if (!drmplane->GetCrtcSupported(*crtc))
918 continue;
919 if (!squashed_comp.plane() && drmplane->type() == DRM_PLANE_TYPE_PRIMARY)
920 squashed_comp.set_plane(drmplane.get());
921 else
922 writeback_comp->AddPlaneDisable(drmplane.get());
923 }
924 squashed_comp.source_layers().push_back(0);
925 ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp));
926 if (ret) {
927 ALOGE("Failed to add flatten scene");
928 return ret;
929 }
930
931 ApplyFrame(std::move(writeback_comp), 0, true);
932 return 0;
933}
934
935// Flatten a scene by using a crtc which works concurrent with
936// the one driving the display.
937int DrmDisplayCompositor::FlattenConcurrent(DrmConnector *writeback_conn) {
938 ALOGV("FlattenConcurrent by using an unused crtc/display");
939 int ret = 0;
940 DrmDisplayCompositor drmdisplaycompositor;
941 ret = drmdisplaycompositor.Init(resource_manager_, writeback_conn->display());
942 if (ret) {
943 ALOGE("Failed to init drmdisplaycompositor = %d", ret);
944 return ret;
945 }
946 // Copy of the active_composition, needed because of two things:
947 // 1) Not to hold the lock for the whole time we are accessing
948 // active_composition
949 // 2) It will be committed on a crtc that might not be on the same
950 // dri node, so buffers need to be imported on the right node.
Sean Paulf72cccd2018-08-27 13:59:08 -0400951 std::unique_ptr<DrmDisplayComposition>
952 copy_comp = drmdisplaycompositor.CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100953
954 // Writeback composition that will be committed to the display.
Sean Paulf72cccd2018-08-27 13:59:08 -0400955 std::unique_ptr<DrmDisplayComposition>
956 writeback_comp = CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100957
958 if (!copy_comp || !writeback_comp)
959 return -EINVAL;
960 AutoLock lock(&lock_, __func__);
961 ret = lock.Lock();
962 if (ret)
963 return ret;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200964 if (!IsFlatteningNeeded()) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100965 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200966 SetFlattening(FlatteningState::kNotNeeded);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100967 return -EALREADY;
968 }
969 DrmCrtc *crtc = active_composition_->crtc();
970
971 std::vector<DrmHwcLayer> copy_layers;
972 for (DrmHwcLayer &src_layer : active_composition_->layers()) {
973 DrmHwcLayer copy;
Sean Paulf72cccd2018-08-27 13:59:08 -0400974 ret = copy.InitFromDrmHwcLayer(&src_layer,
975 resource_manager_
976 ->GetImporter(writeback_conn->display())
977 .get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100978 if (ret) {
979 ALOGE("Failed to import buffer ret = %d", ret);
980 return -EINVAL;
981 }
982 copy_layers.emplace_back(std::move(copy));
983 }
984 ret = copy_comp->SetLayers(copy_layers.data(), copy_layers.size(), true);
985 if (ret) {
986 ALOGE("Failed to set copy_comp layers");
987 return ret;
988 }
989
990 lock.Unlock();
991 DrmHwcLayer writeback_layer;
992 ret = drmdisplaycompositor.FlattenOnDisplay(copy_comp, writeback_conn,
993 mode_.mode, &writeback_layer);
994 if (ret) {
995 ALOGE("Failed to flatten on display ret = %d", ret);
996 return ret;
997 }
998
999 DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL,
1000 crtc);
1001 for (auto &drmplane : resource_manager_->GetDrmDevice(display_)->planes()) {
1002 if (!drmplane->GetCrtcSupported(*crtc))
1003 continue;
1004 if (drmplane->type() == DRM_PLANE_TYPE_PRIMARY)
1005 squashed_comp.set_plane(drmplane.get());
1006 else
1007 writeback_comp->AddPlaneDisable(drmplane.get());
1008 }
1009 writeback_comp->layers().emplace_back();
1010 DrmHwcLayer &next_layer = writeback_comp->layers().back();
1011 next_layer.sf_handle = writeback_layer.get_usable_handle();
1012 next_layer.blending = DrmHwcBlending::kPreMult;
1013 next_layer.source_crop = {0, 0, (float)mode_.mode.h_display(),
1014 (float)mode_.mode.v_display()};
1015 next_layer.display_frame = {0, 0, (int)mode_.mode.h_display(),
1016 (int)mode_.mode.v_display()};
1017 ret = next_layer.ImportBuffer(resource_manager_->GetImporter(display_).get());
1018 if (ret) {
1019 ALOGE("Failed to import framebuffer for display %d", ret);
1020 return ret;
1021 }
1022 squashed_comp.source_layers().push_back(0);
1023 ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp));
1024 if (ret) {
1025 ALOGE("Failed to add plane composition %d", ret);
1026 return ret;
1027 }
1028 ApplyFrame(std::move(writeback_comp), 0, true);
1029 return ret;
1030}
1031
1032int DrmDisplayCompositor::FlattenActiveComposition() {
Sean Paulf72cccd2018-08-27 13:59:08 -04001033 DrmConnector *writeback_conn = resource_manager_->AvailableWritebackConnector(
1034 display_);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001035 if (!active_composition_ || !writeback_conn) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +02001036 // Try to fallback to GPU composition on client, since it is more
1037 // power-efficient than composition on device side
1038 return FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001039 }
1040
1041 if (writeback_conn->display() != display_) {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +02001042 SetFlattening(FlatteningState::kConcurrent);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001043 return FlattenConcurrent(writeback_conn);
1044 } else {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +02001045 SetFlattening(FlatteningState::kSerial);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001046 return FlattenSerial(writeback_conn);
1047 }
1048
1049 return 0;
1050}
1051
1052bool DrmDisplayCompositor::CountdownExpired() const {
1053 return flatten_countdown_ <= 0;
1054}
1055
1056void DrmDisplayCompositor::Vsync(int display, int64_t timestamp) {
1057 AutoLock lock(&lock_, __func__);
1058 if (lock.Lock())
1059 return;
1060 flatten_countdown_--;
1061 if (!CountdownExpired())
1062 return;
1063 lock.Unlock();
1064 int ret = FlattenActiveComposition();
1065 ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64
1066 " result = %d \n",
1067 display, timestamp, ret);
1068}
1069
Sean Paul98e73c82015-06-24 14:38:49 -07001070void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001071 int ret = pthread_mutex_lock(&lock_);
1072 if (ret)
1073 return;
1074
1075 uint64_t num_frames = dump_frames_composited_;
1076 dump_frames_composited_ = 0;
1077
1078 struct timespec ts;
1079 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1080 if (ret) {
1081 pthread_mutex_unlock(&lock_);
1082 return;
1083 }
1084
1085 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1086 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1087 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1088
1089 *out << "--DrmDisplayCompositor[" << display_
1090 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1091 << " fps=" << fps << "\n";
1092
1093 dump_last_timestamp_ns_ = cur_ts;
1094
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001095 pthread_mutex_unlock(&lock_);
Sean Paul98e73c82015-06-24 14:38:49 -07001096}
Sean Paulf72cccd2018-08-27 13:59:08 -04001097} // namespace android