blob: 3ae42ef4fc3b1f24fba64e87d8f72b24d8b164f3 [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
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#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 Stratiienko13cc3662020-08-29 21:35:39 +030035#include "drm/DrmCrtc.h"
36#include "drm/DrmDevice.h"
37#include "drm/DrmPlane.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038#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;
Matvii Zorin8338c342020-09-08 16:12:51 +0300345 uint64_t color_encoding = UINT64_MAX;
346 uint64_t color_range = UINT64_MAX;
Sean Paul04b47ea2015-11-19 21:46:11 -0500347
Sean Paulbbe39db2016-05-11 16:57:26 -0400348 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
Sean Paulca699be2016-05-11 16:29:45 -0400349 if (source_layers.size() > 1) {
350 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
351 source_layers.size(), comp_plane.type());
352 continue;
353 }
354
355 if (source_layers.empty() || source_layers.front() >= layers.size()) {
356 ALOGE("Source layer index %zu out of bounds %zu type=%d",
357 source_layers.front(), layers.size(), comp_plane.type());
Sean Paul39b37842016-05-11 13:50:28 -0400358 break;
Sean Paul98e73c82015-06-24 14:38:49 -0700359 }
Sean Paulca699be2016-05-11 16:29:45 -0400360 DrmHwcLayer &layer = layers[source_layers.front()];
Sean Paul39b37842016-05-11 13:50:28 -0400361 if (!layer.buffer) {
362 ALOGE("Expected a valid framebuffer for pset");
363 break;
364 }
365 fb_id = layer.buffer->fb_id;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400366 fence_fd = layer.acquire_fence.get();
Sean Paul39b37842016-05-11 13:50:28 -0400367 display_frame = layer.display_frame;
368 source_crop = layer.source_crop;
Lowry Li9b6cafd2018-08-28 17:58:21 +0800369 alpha = layer.alpha;
370
371 if (plane->blend_property().id()) {
372 switch (layer.blending) {
373 case DrmHwcBlending::kPreMult:
374 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
375 "Pre-multiplied");
376 break;
377 case DrmHwcBlending::kCoverage:
378 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
379 "Coverage");
380 break;
381 case DrmHwcBlending::kNone:
382 default:
383 std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
384 "None");
385 break;
386 }
387 }
Sean Paul98e73c82015-06-24 14:38:49 -0700388
Sean Paul2689aeb2019-03-13 14:36:52 -0400389 if (plane->zpos_property().id() &&
390 !plane->zpos_property().is_immutable()) {
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200391 uint64_t min_zpos = 0;
392
393 // Ignore ret and use min_zpos as 0 by default
394 std::tie(std::ignore, min_zpos) = plane->zpos_property().range_min();
395
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100396 ret = drmModeAtomicAddProperty(pset, plane->id(),
397 plane->zpos_property().id(),
Alexandru Gheorghe2ed463c2019-01-08 19:21:08 +0200398 source_layers.front() + min_zpos) < 0;
Alexandru Gheorgheea1c5e52018-09-17 10:48:54 +0100399 if (ret) {
400 ALOGE("Failed to add zpos property %d to plane %d",
401 plane->zpos_property().id(), plane->id());
402 break;
403 }
404 }
405
Sean Paul39b37842016-05-11 13:50:28 -0400406 rotation = 0;
407 if (layer.transform & DrmHwcTransform::kFlipH)
Rob Herring89095cc2017-10-06 16:46:48 -0500408 rotation |= DRM_MODE_REFLECT_X;
Sean Paul39b37842016-05-11 13:50:28 -0400409 if (layer.transform & DrmHwcTransform::kFlipV)
Rob Herring89095cc2017-10-06 16:46:48 -0500410 rotation |= DRM_MODE_REFLECT_Y;
Sean Paul39b37842016-05-11 13:50:28 -0400411 if (layer.transform & DrmHwcTransform::kRotate90)
Rob Herring89095cc2017-10-06 16:46:48 -0500412 rotation |= DRM_MODE_ROTATE_90;
Sean Paul39b37842016-05-11 13:50:28 -0400413 else if (layer.transform & DrmHwcTransform::kRotate180)
Rob Herring89095cc2017-10-06 16:46:48 -0500414 rotation |= DRM_MODE_ROTATE_180;
Sean Paul39b37842016-05-11 13:50:28 -0400415 else if (layer.transform & DrmHwcTransform::kRotate270)
Rob Herring89095cc2017-10-06 16:46:48 -0500416 rotation |= DRM_MODE_ROTATE_270;
Rob Herringbd03b992017-11-01 11:21:48 -0500417 else
418 rotation |= DRM_MODE_ROTATE_0;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400419
Rob Herring8428e6a2018-02-12 16:03:35 -0600420 if (fence_fd >= 0) {
Robert Fossb2bdfd82016-10-19 10:46:22 -0400421 int prop_id = plane->in_fence_fd_property().id();
422 if (prop_id == 0) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400423 ALOGE("Failed to get IN_FENCE_FD property id");
424 break;
Robert Fossb2bdfd82016-10-19 10:46:22 -0400425 }
426 ret = drmModeAtomicAddProperty(pset, plane->id(), prop_id, fence_fd);
427 if (ret < 0) {
428 ALOGE("Failed to add IN_FENCE_FD property to pset: %d", ret);
429 break;
430 }
431 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300432
433 if (plane->color_encoding_propery().id()) {
434 switch (layer.dataspace & HAL_DATASPACE_STANDARD_MASK) {
435 case HAL_DATASPACE_STANDARD_BT709:
436 std::tie(color_encoding,
437 ret) = plane->color_encoding_propery()
438 .GetEnumValueWithName("ITU-R BT.709 YCbCr");
439 break;
440 case HAL_DATASPACE_STANDARD_BT601_625:
441 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
442 case HAL_DATASPACE_STANDARD_BT601_525:
443 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
444 std::tie(color_encoding,
445 ret) = plane->color_encoding_propery()
446 .GetEnumValueWithName("ITU-R BT.601 YCbCr");
447 break;
448 case HAL_DATASPACE_STANDARD_BT2020:
449 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
450 std::tie(color_encoding,
451 ret) = plane->color_encoding_propery()
452 .GetEnumValueWithName("ITU-R BT.2020 YCbCr");
453 break;
454 }
455 }
456
457 if (plane->color_range_property().id()) {
458 switch (layer.dataspace & HAL_DATASPACE_RANGE_MASK) {
459 case HAL_DATASPACE_RANGE_FULL:
460 std::tie(color_range,
461 ret) = plane->color_range_property()
462 .GetEnumValueWithName("YCbCr full range");
463 break;
464 case HAL_DATASPACE_RANGE_LIMITED:
465 std::tie(color_range,
466 ret) = plane->color_range_property()
467 .GetEnumValueWithName("YCbCr limited range");
468 break;
469 }
470 }
Sean Paul39b37842016-05-11 13:50:28 -0400471 }
Robert Fossb2bdfd82016-10-19 10:46:22 -0400472
Zach Reizner92f8e632015-10-12 17:47:13 -0700473 // Disable the plane if there's no framebuffer
474 if (fb_id < 0) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600475 ret = drmModeAtomicAddProperty(pset, plane->id(),
476 plane->crtc_property().id(), 0) < 0 ||
477 drmModeAtomicAddProperty(pset, plane->id(),
478 plane->fb_property().id(), 0) < 0;
Sean Paul2e46fbd2015-07-09 17:22:22 -0400479 if (ret) {
480 ALOGE("Failed to add plane %d disable to pset", plane->id());
481 break;
482 }
483 continue;
484 }
485
Rob Herringbc9305e2016-02-04 14:01:24 -0600486 ret = drmModeAtomicAddProperty(pset, plane->id(),
487 plane->crtc_property().id(), crtc->id()) < 0;
488 ret |= drmModeAtomicAddProperty(pset, plane->id(),
489 plane->fb_property().id(), fb_id) < 0;
490 ret |= drmModeAtomicAddProperty(pset, plane->id(),
491 plane->crtc_x_property().id(),
492 display_frame.left) < 0;
493 ret |= drmModeAtomicAddProperty(pset, plane->id(),
494 plane->crtc_y_property().id(),
495 display_frame.top) < 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400496 ret |= drmModeAtomicAddProperty(pset, plane->id(),
497 plane->crtc_w_property().id(),
498 display_frame.right - display_frame.left) <
499 0;
500 ret |= drmModeAtomicAddProperty(pset, plane->id(),
501 plane->crtc_h_property().id(),
502 display_frame.bottom - display_frame.top) <
503 0;
Rob Herringbc9305e2016-02-04 14:01:24 -0600504 ret |= drmModeAtomicAddProperty(pset, plane->id(),
505 plane->src_x_property().id(),
506 (int)(source_crop.left) << 16) < 0;
507 ret |= drmModeAtomicAddProperty(pset, plane->id(),
508 plane->src_y_property().id(),
509 (int)(source_crop.top) << 16) < 0;
Sean Paulf72cccd2018-08-27 13:59:08 -0400510 ret |= drmModeAtomicAddProperty(pset, plane->id(),
511 plane->src_w_property().id(),
512 (int)(source_crop.right - source_crop.left)
513 << 16) < 0;
514 ret |= drmModeAtomicAddProperty(pset, plane->id(),
515 plane->src_h_property().id(),
516 (int)(source_crop.bottom - source_crop.top)
517 << 16) < 0;
Sean Paul98e73c82015-06-24 14:38:49 -0700518 if (ret) {
519 ALOGE("Failed to add plane %d to set", plane->id());
520 break;
521 }
Sean Paul1c4c3262015-07-14 15:51:52 -0400522
523 if (plane->rotation_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600524 ret = drmModeAtomicAddProperty(pset, plane->id(),
525 plane->rotation_property().id(),
526 rotation) < 0;
Sean Paul1c4c3262015-07-14 15:51:52 -0400527 if (ret) {
528 ALOGE("Failed to add rotation property %d to plane %d",
529 plane->rotation_property().id(), plane->id());
530 break;
531 }
532 }
Sean Pauld8aefb62015-10-15 15:17:31 -0400533
534 if (plane->alpha_property().id()) {
Rob Herringbc9305e2016-02-04 14:01:24 -0600535 ret = drmModeAtomicAddProperty(pset, plane->id(),
Sean Paulf72cccd2018-08-27 13:59:08 -0400536 plane->alpha_property().id(), alpha) < 0;
Sean Pauld8aefb62015-10-15 15:17:31 -0400537 if (ret) {
538 ALOGE("Failed to add alpha property %d to plane %d",
539 plane->alpha_property().id(), plane->id());
540 break;
541 }
542 }
Lowry Li9b6cafd2018-08-28 17:58:21 +0800543
544 if (plane->blend_property().id()) {
545 ret = drmModeAtomicAddProperty(pset, plane->id(),
546 plane->blend_property().id(), blend) < 0;
547 if (ret) {
548 ALOGE("Failed to add pixel blend mode property %d to plane %d",
549 plane->blend_property().id(), plane->id());
550 break;
551 }
552 }
Matvii Zorin8338c342020-09-08 16:12:51 +0300553
554 if (plane->color_encoding_propery().id() && color_encoding != UINT64_MAX) {
555 ret = drmModeAtomicAddProperty(pset, plane->id(),
556 plane->color_encoding_propery().id(),
557 color_encoding) < 0;
558 if (ret) {
559 ALOGE("Failed to add COLOR_ENCODING property %d to plane %d",
560 plane->color_encoding_propery().id(), plane->id());
561 break;
562 }
563 }
564
565 if (plane->color_range_property().id() && color_range != UINT64_MAX) {
566 ret = drmModeAtomicAddProperty(pset, plane->id(),
567 plane->color_range_property().id(),
568 color_range) < 0;
569 if (ret) {
570 ALOGE("Failed to add COLOR_RANGE property %d to plane %d",
571 plane->color_range_property().id(), plane->id());
572 break;
573 }
574 }
Sean Paul98e73c82015-06-24 14:38:49 -0700575 }
576
577 if (!ret) {
Sean Paulc07b2112015-11-17 16:38:10 -0500578 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
579 if (test_only)
580 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
581
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100582 ret = drmModeAtomicCommit(drm->fd(), pset, flags, drm);
Sean Paul57355412015-09-19 09:14:34 -0400583 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700584 if (!test_only)
Sean Paulc07b2112015-11-17 16:38:10 -0500585 ALOGE("Failed to commit pset ret=%d\n", ret);
Rob Herringbc9305e2016-02-04 14:01:24 -0600586 drmModeAtomicFree(pset);
Sean Paul57355412015-09-19 09:14:34 -0400587 return ret;
588 }
Sean Paul98e73c82015-06-24 14:38:49 -0700589 }
590 if (pset)
Rob Herringbc9305e2016-02-04 14:01:24 -0600591 drmModeAtomicFree(pset);
Sean Paul98e73c82015-06-24 14:38:49 -0700592
Sean Paulc07b2112015-11-17 16:38:10 -0500593 if (!test_only && mode_.needs_modeset) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100594 ret = drm->DestroyPropertyBlob(mode_.old_blob_id);
Sean Paul57355412015-09-19 09:14:34 -0400595 if (ret) {
Sean Paulf741c672016-05-11 13:49:38 -0400596 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
Sean Paul35301f42015-11-17 14:46:56 -0500597 mode_.old_blob_id, ret);
Sean Paul57355412015-09-19 09:14:34 -0400598 return ret;
599 }
600
601 /* TODO: Add dpms to the pset when the kernel supports it */
602 ret = ApplyDpms(display_comp);
603 if (ret) {
604 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
605 return ret;
606 }
607
Sean Paul35301f42015-11-17 14:46:56 -0500608 connector->set_active_mode(mode_.mode);
609 mode_.old_blob_id = mode_.blob_id;
610 mode_.blob_id = 0;
611 mode_.needs_modeset = false;
Sean Paul57355412015-09-19 09:14:34 -0400612 }
613
Robert Fossa1ade4e2017-09-27 19:28:15 +0200614 if (crtc->out_fence_ptr_property().id()) {
Sean Paulf72cccd2018-08-27 13:59:08 -0400615 display_comp->set_out_fence((int)out_fences[crtc->pipe()]);
Robert Fossa1ade4e2017-09-27 19:28:15 +0200616 }
617
Sean Paul98e73c82015-06-24 14:38:49 -0700618 return ret;
619}
620
Sean Pauldb7a17d2015-06-24 18:46:05 -0700621int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100622 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
623 DrmConnector *conn = drm->GetConnectorForDisplay(display_);
Sean Pauldb7a17d2015-06-24 18:46:05 -0700624 if (!conn) {
625 ALOGE("Failed to get DrmConnector for display %d", display_);
626 return -ENODEV;
627 }
628
629 const DrmProperty &prop = conn->dpms_property();
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100630 int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(),
Sean Pauldb7a17d2015-06-24 18:46:05 -0700631 display_comp->dpms_mode());
632 if (ret) {
633 ALOGE("Failed to set DPMS property for connector %d", conn->id());
634 return ret;
635 }
636 return 0;
637}
638
Sean Paul35301f42015-11-17 14:46:56 -0500639std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
640 const DrmMode &mode) {
641 struct drm_mode_modeinfo drm_mode;
642 memset(&drm_mode, 0, sizeof(drm_mode));
643 mode.ToDrmModeModeInfo(&drm_mode);
644
645 uint32_t id = 0;
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100646 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
647 int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
648 &id);
Sean Paul35301f42015-11-17 14:46:56 -0500649 if (ret) {
650 ALOGE("Failed to create mode property blob %d", ret);
651 return std::make_tuple(ret, 0);
652 }
Sean Paulf741c672016-05-11 13:49:38 -0400653 ALOGE("Create blob_id %" PRIu32 "\n", id);
Sean Paul35301f42015-11-17 14:46:56 -0500654 return std::make_tuple(ret, id);
655}
656
Sean Paul137a6a82016-06-22 22:48:22 -0400657void DrmDisplayCompositor::ClearDisplay() {
Sean Paul137a6a82016-06-22 22:48:22 -0400658 if (!active_composition_)
659 return;
660
661 if (DisablePlanes(active_composition_.get()))
662 return;
663
Sean Paul137a6a82016-06-22 22:48:22 -0400664 active_composition_.reset(NULL);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100665 vsync_worker_.VSyncControl(false);
Sean Paul137a6a82016-06-22 22:48:22 -0400666}
667
Haixia Shidda2fab2015-10-22 18:12:49 -0700668void DrmDisplayCompositor::ApplyFrame(
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100669 std::unique_ptr<DrmDisplayComposition> composition, int status,
670 bool writeback) {
Alexandru Gheorghedb440a92018-03-29 14:38:21 +0100671 AutoLock lock(&lock_, __func__);
672 if (lock.Lock())
673 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700674 int ret = status;
675
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100676 if (!ret) {
677 if (writeback && !CountdownExpired()) {
678 ALOGE("Abort playing back scene");
679 return;
680 }
Sean Paulc07b2112015-11-17 16:38:10 -0500681 ret = CommitFrame(composition.get(), false);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100682 }
Haixia Shidda2fab2015-10-22 18:12:49 -0700683
684 if (ret) {
685 ALOGE("Composite failed for display %d", display_);
Haixia Shidda2fab2015-10-22 18:12:49 -0700686 // Disable the hw used by the last active composition. This allows us to
687 // signal the release fences from that composition to avoid hanging.
Sean Paul137a6a82016-06-22 22:48:22 -0400688 ClearDisplay();
689 return;
Haixia Shidda2fab2015-10-22 18:12:49 -0700690 }
691 ++dump_frames_composited_;
692
Haixia Shidda2fab2015-10-22 18:12:49 -0700693 active_composition_.swap(composition);
694
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100695 flatten_countdown_ = FLATTEN_COUNTDOWN_INIT;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200696 if (flattening_state_ != FlatteningState::kClientRequested) {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200697 SetFlattening(FlatteningState::kNone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200698 } else {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200699 SetFlattening(FlatteningState::kClientDone);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200700 }
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100701 vsync_worker_.VSyncControl(!writeback);
Haixia Shidda2fab2015-10-22 18:12:49 -0700702}
703
Sean Pauled45a8e2017-02-28 13:17:34 -0500704int DrmDisplayCompositor::ApplyComposition(
705 std::unique_ptr<DrmDisplayComposition> composition) {
706 int ret = 0;
Sean Paulacb2a442015-06-24 18:43:01 -0700707 switch (composition->type()) {
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700708 case DRM_COMPOSITION_TYPE_FRAME:
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800709 if (composition->geometry_changed()) {
710 // Send the composition to the kernel to ensure we can commit it. This
Rob Herringaf0d9752018-05-04 16:34:19 -0500711 // is just a test, it won't actually commit the frame.
Haixia Shi6afbb6a2015-11-24 12:42:45 -0800712 ret = CommitFrame(composition.get(), true);
Rob Herringaf0d9752018-05-04 16:34:19 -0500713 if (ret) {
714 ALOGE("Commit test failed for display %d, FIXME", display_);
Sean Paul6c18b3b2015-11-25 11:04:25 -0500715 return ret;
Sean Paul647beb22015-11-19 13:55:48 -0500716 }
717 }
Rob Herringaf0d9752018-05-04 16:34:19 -0500718
Sean Pauled45a8e2017-02-28 13:17:34 -0500719 ApplyFrame(std::move(composition), ret);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700720 break;
721 case DRM_COMPOSITION_TYPE_DPMS:
Sean Pauled45a8e2017-02-28 13:17:34 -0500722 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700723 ret = ApplyDpms(composition.get());
724 if (ret)
725 ALOGE("Failed to apply dpms for display %d", display_);
Sean Paulacb2a442015-06-24 18:43:01 -0700726 return ret;
Sean Paul57355412015-09-19 09:14:34 -0400727 case DRM_COMPOSITION_TYPE_MODESET:
Sean Paul35301f42015-11-17 14:46:56 -0500728 mode_.mode = composition->display_mode();
729 if (mode_.blob_id)
Sean Paulf72cccd2018-08-27 13:59:08 -0400730 resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob(
731 mode_.blob_id);
Sean Paul35301f42015-11-17 14:46:56 -0500732 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
733 if (ret) {
734 ALOGE("Failed to create mode blob for display %d", display_);
735 return ret;
736 }
737 mode_.needs_modeset = true;
Sean Paul57355412015-09-19 09:14:34 -0400738 return 0;
Zach Reiznerb4a9aef2015-07-16 09:45:59 -0700739 default:
740 ALOGE("Unknown composition type %d", composition->type());
741 return -EINVAL;
Sean Paul98e73c82015-06-24 14:38:49 -0700742 }
Sean Paul98e73c82015-06-24 14:38:49 -0700743
Sean Paul98e73c82015-06-24 14:38:49 -0700744 return ret;
745}
746
Rob Herring4f6c62e2018-05-17 14:33:02 -0500747int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) {
748 return CommitFrame(composition, true);
749}
750
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100751// Flatten a scene on the display by using a writeback connector
752// and returns the composition result as a DrmHwcLayer.
753int DrmDisplayCompositor::FlattenOnDisplay(
754 std::unique_ptr<DrmDisplayComposition> &src, DrmConnector *writeback_conn,
755 DrmMode &src_mode, DrmHwcLayer *writeback_layer) {
756 int ret = 0;
757 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
758 ret = writeback_conn->UpdateModes();
759 if (ret) {
760 ALOGE("Failed to update modes %d", ret);
761 return ret;
762 }
763 for (const DrmMode &mode : writeback_conn->modes()) {
764 if (mode.h_display() == src_mode.h_display() &&
765 mode.v_display() == src_mode.v_display()) {
766 mode_.mode = mode;
767 if (mode_.blob_id)
768 drm->DestroyPropertyBlob(mode_.blob_id);
769 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
770 if (ret) {
771 ALOGE("Failed to create mode blob for display %d", display_);
772 return ret;
773 }
774 mode_.needs_modeset = true;
775 break;
776 }
777 }
778 if (mode_.blob_id <= 0) {
779 ALOGE("Failed to find similar mode");
780 return -EINVAL;
781 }
782
783 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
784 if (!crtc) {
785 ALOGE("Failed to find crtc for display %d", display_);
786 return -EINVAL;
787 }
788 // TODO what happens if planes could go to both CRTCs, I don't think it's
789 // handled anywhere
790 std::vector<DrmPlane *> primary_planes;
791 std::vector<DrmPlane *> overlay_planes;
792 for (auto &plane : drm->planes()) {
793 if (!plane->GetCrtcSupported(*crtc))
794 continue;
795 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
796 primary_planes.push_back(plane.get());
797 else if (plane->type() == DRM_PLANE_TYPE_OVERLAY)
798 overlay_planes.push_back(plane.get());
799 }
800
801 ret = src->Plan(&primary_planes, &overlay_planes);
802 if (ret) {
803 ALOGE("Failed to plan the composition ret = %d", ret);
804 return ret;
805 }
806
807 // Disable the planes we're not using
808 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
809 src->AddPlaneDisable(*i);
810 i = primary_planes.erase(i);
811 }
812 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
813 src->AddPlaneDisable(*i);
814 i = overlay_planes.erase(i);
815 }
816
817 AutoLock lock(&lock_, __func__);
818 ret = lock.Lock();
819 if (ret)
820 return ret;
821 DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_];
822 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
823 if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) {
824 ALOGE("Failed to allocate writeback buffer");
825 return -ENOMEM;
826 }
827 DrmHwcBuffer *writeback_buffer = &writeback_layer->buffer;
828 writeback_layer->sf_handle = writeback_fb->buffer()->handle;
829 ret = writeback_layer->ImportBuffer(
830 resource_manager_->GetImporter(display_).get());
831 if (ret) {
832 ALOGE("Failed to import writeback buffer");
833 return ret;
834 }
835
836 ret = CommitFrame(src.get(), true, writeback_conn, writeback_buffer);
837 if (ret) {
838 ALOGE("Atomic check failed");
839 return ret;
840 }
841 ret = CommitFrame(src.get(), false, writeback_conn, writeback_buffer);
842 if (ret) {
843 ALOGE("Atomic commit failed");
844 return ret;
845 }
846
847 ret = sync_wait(writeback_fence_, kWaitWritebackFence);
848 writeback_layer->acquire_fence.Set(writeback_fence_);
849 writeback_fence_ = -1;
850 if (ret) {
851 ALOGE("Failed to wait on writeback fence");
852 return ret;
853 }
854 return 0;
855}
856
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200857void DrmDisplayCompositor::SetFlattening(FlatteningState new_state) {
858 if (flattening_state_ != new_state) {
859 switch (flattening_state_) {
860 case FlatteningState::kClientDone:
861 case FlatteningState::kConcurrent:
862 case FlatteningState::kSerial:
863 ++frames_flattened_;
864 break;
865 case FlatteningState::kClientRequested:
866 case FlatteningState::kNone:
867 case FlatteningState::kNotNeeded:
868 break;
869 }
870 }
871 flattening_state_ = new_state;
872}
873
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200874bool DrmDisplayCompositor::IsFlatteningNeeded() const {
875 return CountdownExpired() && active_composition_->layers().size() >= 2;
876}
877
878int DrmDisplayCompositor::FlattenOnClient() {
Roman Stratiienko23701092020-09-26 02:08:41 +0300879 const std::lock_guard<std::mutex> lock(refresh_callback_lock);
880
881 if (refresh_callback_hook_ && refresh_callback_data_) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200882 {
883 AutoLock lock(&lock_, __func__);
884 if (!IsFlatteningNeeded()) {
885 if (flattening_state_ != FlatteningState::kClientDone) {
886 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200887 SetFlattening(FlatteningState::kNotNeeded);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200888 }
889 return -EALREADY;
890 }
891 }
892
893 ALOGV(
894 "No writeback connector available, "
895 "falling back to client composition");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200896 SetFlattening(FlatteningState::kClientRequested);
Roman Stratiienko23701092020-09-26 02:08:41 +0300897 refresh_callback_hook_(refresh_callback_data_, display_);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200898 return 0;
899 } else {
900 ALOGV("No writeback connector available");
901 return -EINVAL;
902 }
903}
904
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100905// Flatten a scene by enabling the writeback connector attached
906// to the same CRTC as the one driving the display.
907int DrmDisplayCompositor::FlattenSerial(DrmConnector *writeback_conn) {
908 ALOGV("FlattenSerial by enabling writeback connector to the same crtc");
909 // Flattened composition with only one layer that is obtained
910 // using the writeback connector
Sean Paulf72cccd2018-08-27 13:59:08 -0400911 std::unique_ptr<DrmDisplayComposition>
912 writeback_comp = CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100913 if (!writeback_comp)
914 return -EINVAL;
915
916 AutoLock lock(&lock_, __func__);
917 int ret = lock.Lock();
918 if (ret)
919 return ret;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200920 if (!IsFlatteningNeeded()) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100921 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200922 SetFlattening(FlatteningState::kNotNeeded);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +0100923 return -EALREADY;
924 }
925
926 DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_];
927 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
928 lock.Unlock();
929
930 if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) {
931 ALOGE("Failed to allocate writeback buffer");
932 return -ENOMEM;
933 }
934 writeback_comp->layers().emplace_back();
935
936 DrmHwcLayer &writeback_layer = writeback_comp->layers().back();
937 writeback_layer.sf_handle = writeback_fb->buffer()->handle;
938 writeback_layer.source_crop = {0, 0, (float)mode_.mode.h_display(),
939 (float)mode_.mode.v_display()};
940 writeback_layer.display_frame = {0, 0, (int)mode_.mode.h_display(),
941 (int)mode_.mode.v_display()};
942 ret = writeback_layer.ImportBuffer(
943 resource_manager_->GetImporter(display_).get());
944 if (ret || writeback_comp->layers().size() != 1) {
945 ALOGE("Failed to import writeback buffer");
946 return ret;
947 }
948
949 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
950 if (!pset) {
951 ALOGE("Failed to allocate property set");
952 return -ENOMEM;
953 }
954 DrmDevice *drm = resource_manager_->GetDrmDevice(display_);
955 DrmCrtc *crtc = drm->GetCrtcForDisplay(display_);
956 if (!crtc) {
957 ALOGE("Failed to find crtc for display %d", display_);
958 return -EINVAL;
959 }
960 ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn,
961 &writeback_layer.buffer);
962 if (ret < 0) {
963 ALOGE("Failed to Setup Writeback Commit");
964 return ret;
965 }
966 ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm);
967 if (ret) {
968 ALOGE("Failed to enable writeback %d", ret);
969 return ret;
970 }
971 ret = sync_wait(writeback_fence_, kWaitWritebackFence);
972 writeback_layer.acquire_fence.Set(writeback_fence_);
973 writeback_fence_ = -1;
974 if (ret) {
975 ALOGE("Failed to wait on writeback fence");
976 return ret;
977 }
978
979 DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL,
980 crtc);
981 for (auto &drmplane : drm->planes()) {
982 if (!drmplane->GetCrtcSupported(*crtc))
983 continue;
984 if (!squashed_comp.plane() && drmplane->type() == DRM_PLANE_TYPE_PRIMARY)
985 squashed_comp.set_plane(drmplane.get());
986 else
987 writeback_comp->AddPlaneDisable(drmplane.get());
988 }
989 squashed_comp.source_layers().push_back(0);
990 ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp));
991 if (ret) {
992 ALOGE("Failed to add flatten scene");
993 return ret;
994 }
995
996 ApplyFrame(std::move(writeback_comp), 0, true);
997 return 0;
998}
999
1000// Flatten a scene by using a crtc which works concurrent with
1001// the one driving the display.
1002int DrmDisplayCompositor::FlattenConcurrent(DrmConnector *writeback_conn) {
1003 ALOGV("FlattenConcurrent by using an unused crtc/display");
1004 int ret = 0;
1005 DrmDisplayCompositor drmdisplaycompositor;
1006 ret = drmdisplaycompositor.Init(resource_manager_, writeback_conn->display());
1007 if (ret) {
1008 ALOGE("Failed to init drmdisplaycompositor = %d", ret);
1009 return ret;
1010 }
1011 // Copy of the active_composition, needed because of two things:
1012 // 1) Not to hold the lock for the whole time we are accessing
1013 // active_composition
1014 // 2) It will be committed on a crtc that might not be on the same
1015 // dri node, so buffers need to be imported on the right node.
Sean Paulf72cccd2018-08-27 13:59:08 -04001016 std::unique_ptr<DrmDisplayComposition>
1017 copy_comp = drmdisplaycompositor.CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001018
1019 // Writeback composition that will be committed to the display.
Sean Paulf72cccd2018-08-27 13:59:08 -04001020 std::unique_ptr<DrmDisplayComposition>
1021 writeback_comp = CreateInitializedComposition();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001022
1023 if (!copy_comp || !writeback_comp)
1024 return -EINVAL;
1025 AutoLock lock(&lock_, __func__);
1026 ret = lock.Lock();
1027 if (ret)
1028 return ret;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +02001029 if (!IsFlatteningNeeded()) {
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001030 ALOGV("Flattening is not needed");
Roman Kovalivskyi9170b312020-02-03 18:13:57 +02001031 SetFlattening(FlatteningState::kNotNeeded);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001032 return -EALREADY;
1033 }
1034 DrmCrtc *crtc = active_composition_->crtc();
1035
1036 std::vector<DrmHwcLayer> copy_layers;
1037 for (DrmHwcLayer &src_layer : active_composition_->layers()) {
1038 DrmHwcLayer copy;
Sean Paulf72cccd2018-08-27 13:59:08 -04001039 ret = copy.InitFromDrmHwcLayer(&src_layer,
1040 resource_manager_
1041 ->GetImporter(writeback_conn->display())
1042 .get());
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001043 if (ret) {
1044 ALOGE("Failed to import buffer ret = %d", ret);
1045 return -EINVAL;
1046 }
1047 copy_layers.emplace_back(std::move(copy));
1048 }
1049 ret = copy_comp->SetLayers(copy_layers.data(), copy_layers.size(), true);
1050 if (ret) {
1051 ALOGE("Failed to set copy_comp layers");
1052 return ret;
1053 }
1054
1055 lock.Unlock();
1056 DrmHwcLayer writeback_layer;
1057 ret = drmdisplaycompositor.FlattenOnDisplay(copy_comp, writeback_conn,
1058 mode_.mode, &writeback_layer);
1059 if (ret) {
1060 ALOGE("Failed to flatten on display ret = %d", ret);
1061 return ret;
1062 }
1063
1064 DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL,
1065 crtc);
1066 for (auto &drmplane : resource_manager_->GetDrmDevice(display_)->planes()) {
1067 if (!drmplane->GetCrtcSupported(*crtc))
1068 continue;
1069 if (drmplane->type() == DRM_PLANE_TYPE_PRIMARY)
1070 squashed_comp.set_plane(drmplane.get());
1071 else
1072 writeback_comp->AddPlaneDisable(drmplane.get());
1073 }
1074 writeback_comp->layers().emplace_back();
1075 DrmHwcLayer &next_layer = writeback_comp->layers().back();
1076 next_layer.sf_handle = writeback_layer.get_usable_handle();
1077 next_layer.blending = DrmHwcBlending::kPreMult;
1078 next_layer.source_crop = {0, 0, (float)mode_.mode.h_display(),
1079 (float)mode_.mode.v_display()};
1080 next_layer.display_frame = {0, 0, (int)mode_.mode.h_display(),
1081 (int)mode_.mode.v_display()};
1082 ret = next_layer.ImportBuffer(resource_manager_->GetImporter(display_).get());
1083 if (ret) {
1084 ALOGE("Failed to import framebuffer for display %d", ret);
1085 return ret;
1086 }
1087 squashed_comp.source_layers().push_back(0);
1088 ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp));
1089 if (ret) {
1090 ALOGE("Failed to add plane composition %d", ret);
1091 return ret;
1092 }
1093 ApplyFrame(std::move(writeback_comp), 0, true);
1094 return ret;
1095}
1096
1097int DrmDisplayCompositor::FlattenActiveComposition() {
Sean Paulf72cccd2018-08-27 13:59:08 -04001098 DrmConnector *writeback_conn = resource_manager_->AvailableWritebackConnector(
1099 display_);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001100 if (!active_composition_ || !writeback_conn) {
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +02001101 // Try to fallback to GPU composition on client, since it is more
1102 // power-efficient than composition on device side
1103 return FlattenOnClient();
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001104 }
1105
1106 if (writeback_conn->display() != display_) {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +02001107 SetFlattening(FlatteningState::kConcurrent);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001108 return FlattenConcurrent(writeback_conn);
1109 } else {
Roman Kovalivskyi9170b312020-02-03 18:13:57 +02001110 SetFlattening(FlatteningState::kSerial);
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +01001111 return FlattenSerial(writeback_conn);
1112 }
1113
1114 return 0;
1115}
1116
1117bool DrmDisplayCompositor::CountdownExpired() const {
1118 return flatten_countdown_ <= 0;
1119}
1120
1121void DrmDisplayCompositor::Vsync(int display, int64_t timestamp) {
1122 AutoLock lock(&lock_, __func__);
1123 if (lock.Lock())
1124 return;
1125 flatten_countdown_--;
1126 if (!CountdownExpired())
1127 return;
1128 lock.Unlock();
1129 int ret = FlattenActiveComposition();
1130 ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64
1131 " result = %d \n",
1132 display, timestamp, ret);
1133}
1134
Sean Paul98e73c82015-06-24 14:38:49 -07001135void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001136 int ret = pthread_mutex_lock(&lock_);
1137 if (ret)
1138 return;
1139
1140 uint64_t num_frames = dump_frames_composited_;
1141 dump_frames_composited_ = 0;
1142
1143 struct timespec ts;
1144 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1145 if (ret) {
1146 pthread_mutex_unlock(&lock_);
1147 return;
1148 }
1149
1150 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1151 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1152 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1153
1154 *out << "--DrmDisplayCompositor[" << display_
1155 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1156 << " fps=" << fps << "\n";
1157
1158 dump_last_timestamp_ns_ = cur_ts;
1159
Zach Reiznerfd6dc332015-10-13 21:12:48 -07001160 pthread_mutex_unlock(&lock_);
Sean Paul98e73c82015-06-24 14:38:49 -07001161}
Sean Paulf72cccd2018-08-27 13:59:08 -04001162} // namespace android