Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 1 | /* |
| 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 Reizner | bff33ac | 2015-11-16 11:08:46 -0800 | [diff] [blame] | 21 | |
| 22 | #include <pthread.h> |
| 23 | #include <sched.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <time.h> |
| 26 | #include <sstream> |
| 27 | #include <vector> |
| 28 | |
Zach Reizner | bff33ac | 2015-11-16 11:08:46 -0800 | [diff] [blame] | 29 | #include <drm/drm_mode.h> |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 30 | #include <log/log.h> |
Zach Reizner | bff33ac | 2015-11-16 11:08:46 -0800 | [diff] [blame] | 31 | #include <sync/sync.h> |
| 32 | #include <utils/Trace.h> |
| 33 | |
| 34 | #include "autolock.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 35 | #include "drmcrtc.h" |
Alexandru Gheorghe | 0f5abd7 | 2018-05-01 14:37:10 +0100 | [diff] [blame] | 36 | #include "drmdevice.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 37 | #include "drmplane.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 38 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 39 | static const uint32_t kWaitWritebackFence = 100; // ms |
| 40 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 43 | class CompositorVsyncCallback : public VsyncCallback { |
| 44 | public: |
| 45 | CompositorVsyncCallback(DrmDisplayCompositor *compositor) |
| 46 | : compositor_(compositor) { |
| 47 | } |
| 48 | |
| 49 | void Callback(int display, int64_t timestamp) { |
| 50 | compositor_->Vsync(display, timestamp); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | DrmDisplayCompositor *compositor_; |
| 55 | }; |
| 56 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 57 | DrmDisplayCompositor::DrmDisplayCompositor() |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 58 | : resource_manager_(NULL), |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 59 | display_(-1), |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 60 | initialized_(false), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 61 | active_(false), |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame] | 62 | use_hw_overlays_(true), |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 63 | dump_frames_composited_(0), |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 64 | dump_last_timestamp_ns_(0), |
| 65 | flatten_countdown_(FLATTEN_COUNTDOWN_INIT), |
| 66 | writeback_fence_(-1) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 67 | struct timespec ts; |
| 68 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 69 | return; |
| 70 | dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 71 | } |
| 72 | |
| 73 | DrmDisplayCompositor::~DrmDisplayCompositor() { |
| 74 | if (!initialized_) |
| 75 | return; |
| 76 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 77 | vsync_worker_.Exit(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 78 | int ret = pthread_mutex_lock(&lock_); |
| 79 | if (ret) |
| 80 | ALOGE("Failed to acquire compositor lock %d", ret); |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 81 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 82 | if (mode_.blob_id) |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 83 | drm->DestroyPropertyBlob(mode_.blob_id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 84 | if (mode_.old_blob_id) |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 85 | drm->DestroyPropertyBlob(mode_.old_blob_id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 86 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 87 | active_composition_.reset(); |
| 88 | |
| 89 | ret = pthread_mutex_unlock(&lock_); |
| 90 | if (ret) |
| 91 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 92 | |
| 93 | pthread_mutex_destroy(&lock_); |
| 94 | } |
| 95 | |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 96 | int DrmDisplayCompositor::Init(ResourceManager *resource_manager, int display) { |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 97 | resource_manager_ = resource_manager; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 98 | display_ = display; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 99 | DrmDevice *drm = resource_manager_->GetDrmDevice(display); |
| 100 | if (!drm) { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 101 | ALOGE("Could not find drmdevice for display"); |
| 102 | return -EINVAL; |
| 103 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 104 | int ret = pthread_mutex_init(&lock_, NULL); |
| 105 | if (ret) { |
| 106 | ALOGE("Failed to initialize drm compositor lock %d\n", ret); |
| 107 | return ret; |
| 108 | } |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 109 | planner_ = Planner::CreateInstance(drm); |
| 110 | |
| 111 | vsync_worker_.Init(drm, display_); |
| 112 | auto callback = std::make_shared<CompositorVsyncCallback>(this); |
| 113 | vsync_worker_.RegisterCallback(callback); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 114 | |
| 115 | initialized_ = true; |
| 116 | return 0; |
| 117 | } |
| 118 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 119 | std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition() |
| 120 | const { |
| 121 | return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition()); |
| 122 | } |
| 123 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 124 | std::unique_ptr<DrmDisplayComposition> |
| 125 | DrmDisplayCompositor::CreateInitializedComposition() const { |
| 126 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 127 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
| 128 | if (!crtc) { |
| 129 | ALOGE("Failed to find crtc for display = %d", display_); |
| 130 | return std::unique_ptr<DrmDisplayComposition>(); |
| 131 | } |
| 132 | std::unique_ptr<DrmDisplayComposition> comp = CreateComposition(); |
| 133 | std::shared_ptr<Importer> importer = resource_manager_->GetImporter(display_); |
| 134 | if (!importer) { |
| 135 | ALOGE("Failed to find resources for display = %d", display_); |
| 136 | return std::unique_ptr<DrmDisplayComposition>(); |
| 137 | } |
| 138 | int ret = comp->Init(drm, crtc, importer.get(), planner_.get(), 0); |
| 139 | if (ret) { |
| 140 | ALOGE("Failed to init composition for display = %d", display_); |
| 141 | return std::unique_ptr<DrmDisplayComposition>(); |
| 142 | } |
| 143 | return comp; |
| 144 | } |
| 145 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 146 | std::tuple<uint32_t, uint32_t, int> |
| 147 | DrmDisplayCompositor::GetActiveModeResolution() { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 148 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 149 | DrmConnector *connector = drm->GetConnectorForDisplay(display_); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 150 | if (connector == NULL) { |
| 151 | ALOGE("Failed to determine display mode: no connector for display %d", |
| 152 | display_); |
| 153 | return std::make_tuple(0, 0, -ENODEV); |
| 154 | } |
| 155 | |
| 156 | const DrmMode &mode = connector->active_mode(); |
| 157 | return std::make_tuple(mode.h_display(), mode.v_display(), 0); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 160 | int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 161 | drmModeAtomicReqPtr pset = drmModeAtomicAlloc(); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 162 | if (!pset) { |
| 163 | ALOGE("Failed to allocate property set"); |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | |
| 167 | int ret; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 168 | std::vector<DrmCompositionPlane> &comp_planes = display_comp |
| 169 | ->composition_planes(); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 170 | for (DrmCompositionPlane &comp_plane : comp_planes) { |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 171 | DrmPlane *plane = comp_plane.plane(); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 172 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 173 | plane->crtc_property().id(), 0) < 0 || |
| 174 | drmModeAtomicAddProperty(pset, plane->id(), plane->fb_property().id(), |
| 175 | 0) < 0; |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 176 | if (ret) { |
| 177 | ALOGE("Failed to add plane %d disable to pset", plane->id()); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 178 | drmModeAtomicFree(pset); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 179 | return ret; |
| 180 | } |
| 181 | } |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 182 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 183 | ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 184 | if (ret) { |
| 185 | ALOGE("Failed to commit pset ret=%d\n", ret); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 186 | drmModeAtomicFree(pset); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 187 | return ret; |
| 188 | } |
| 189 | |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 190 | drmModeAtomicFree(pset); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 194 | int DrmDisplayCompositor::SetupWritebackCommit(drmModeAtomicReqPtr pset, |
| 195 | uint32_t crtc_id, |
| 196 | DrmConnector *writeback_conn, |
| 197 | DrmHwcBuffer *writeback_buffer) { |
| 198 | int ret = 0; |
| 199 | if (writeback_conn->writeback_fb_id().id() == 0 || |
| 200 | writeback_conn->writeback_out_fence().id() == 0) { |
| 201 | ALOGE("Writeback properties don't exit"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | if ((*writeback_buffer)->fb_id == 0) { |
| 205 | ALOGE("Invalid writeback buffer"); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), |
| 209 | writeback_conn->writeback_fb_id().id(), |
| 210 | (*writeback_buffer)->fb_id); |
| 211 | if (ret < 0) { |
| 212 | ALOGE("Failed to add writeback_fb_id"); |
| 213 | return ret; |
| 214 | } |
| 215 | ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), |
| 216 | writeback_conn->writeback_out_fence().id(), |
| 217 | (uint64_t)&writeback_fence_); |
| 218 | if (ret < 0) { |
| 219 | ALOGE("Failed to add writeback_out_fence"); |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), |
| 224 | writeback_conn->crtc_id_property().id(), |
| 225 | crtc_id); |
| 226 | if (ret < 0) { |
| 227 | ALOGE("Failed to attach writeback"); |
| 228 | return ret; |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 233 | int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 234 | bool test_only, |
| 235 | DrmConnector *writeback_conn, |
| 236 | DrmHwcBuffer *writeback_buffer) { |
Haixia Shi | 3979f7d | 2015-10-29 14:33:37 -0700 | [diff] [blame] | 237 | ATRACE_CALL(); |
| 238 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 239 | int ret = 0; |
| 240 | |
| 241 | std::vector<DrmHwcLayer> &layers = display_comp->layers(); |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 242 | std::vector<DrmCompositionPlane> &comp_planes = display_comp |
| 243 | ->composition_planes(); |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 244 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 245 | uint64_t out_fences[drm->crtcs().size()]; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 246 | |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 247 | DrmConnector *connector = drm->GetConnectorForDisplay(display_); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 248 | if (!connector) { |
| 249 | ALOGE("Could not locate connector for display %d", display_); |
| 250 | return -ENODEV; |
| 251 | } |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 252 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 253 | if (!crtc) { |
| 254 | ALOGE("Could not locate crtc for display %d", display_); |
| 255 | return -ENODEV; |
| 256 | } |
| 257 | |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 258 | drmModeAtomicReqPtr pset = drmModeAtomicAlloc(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 259 | if (!pset) { |
| 260 | ALOGE("Failed to allocate property set"); |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 264 | if (writeback_buffer != NULL) { |
| 265 | if (writeback_conn == NULL) { |
| 266 | ALOGE("Invalid arguments requested writeback without writeback conn"); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn, |
| 270 | writeback_buffer); |
| 271 | if (ret < 0) { |
| 272 | ALOGE("Failed to Setup Writeback Commit ret = %d", ret); |
| 273 | return ret; |
| 274 | } |
| 275 | } |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 276 | if (crtc->out_fence_ptr_property().id() != 0) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 277 | ret = drmModeAtomicAddProperty(pset, crtc->id(), |
| 278 | crtc->out_fence_ptr_property().id(), |
| 279 | (uint64_t)&out_fences[crtc->pipe()]); |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 280 | if (ret < 0) { |
| 281 | ALOGE("Failed to add OUT_FENCE_PTR property to pset: %d", ret); |
| 282 | drmModeAtomicFree(pset); |
| 283 | return ret; |
| 284 | } |
| 285 | } |
| 286 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 287 | if (mode_.needs_modeset) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 288 | ret = drmModeAtomicAddProperty(pset, crtc->id(), |
| 289 | crtc->active_property().id(), 1); |
John Stultz | b365b79 | 2018-01-23 15:16:36 -0800 | [diff] [blame] | 290 | if (ret < 0) { |
| 291 | ALOGE("Failed to add crtc active to pset\n"); |
| 292 | drmModeAtomicFree(pset); |
| 293 | return ret; |
| 294 | } |
| 295 | |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 296 | ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(), |
| 297 | mode_.blob_id) < 0 || |
| 298 | drmModeAtomicAddProperty(pset, connector->id(), |
| 299 | connector->crtc_id_property().id(), |
| 300 | crtc->id()) < 0; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 301 | if (ret) { |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 302 | ALOGE("Failed to add blob %d to pset", mode_.blob_id); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 303 | drmModeAtomicFree(pset); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 304 | return ret; |
| 305 | } |
| 306 | } |
| 307 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 308 | for (DrmCompositionPlane &comp_plane : comp_planes) { |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 309 | DrmPlane *plane = comp_plane.plane(); |
| 310 | DrmCrtc *crtc = comp_plane.crtc(); |
| 311 | std::vector<size_t> &source_layers = comp_plane.source_layers(); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 312 | |
| 313 | int fb_id = -1; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 314 | int fence_fd = -1; |
Rob Herring | cff7b1e | 2018-05-09 15:18:36 -0500 | [diff] [blame] | 315 | hwc_rect_t display_frame; |
| 316 | hwc_frect_t source_crop; |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 317 | uint64_t rotation = 0; |
Stefan Schake | 025d0a6 | 2018-05-04 18:03:00 +0200 | [diff] [blame] | 318 | uint64_t alpha = 0xFFFF; |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 319 | uint64_t blend; |
Sean Paul | 04b47ea | 2015-11-19 21:46:11 -0500 | [diff] [blame] | 320 | |
Sean Paul | bbe39db | 2016-05-11 16:57:26 -0400 | [diff] [blame] | 321 | if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) { |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 322 | if (source_layers.size() > 1) { |
| 323 | ALOGE("Can't handle more than one source layer sz=%zu type=%d", |
| 324 | source_layers.size(), comp_plane.type()); |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | if (source_layers.empty() || source_layers.front() >= layers.size()) { |
| 329 | ALOGE("Source layer index %zu out of bounds %zu type=%d", |
| 330 | source_layers.front(), layers.size(), comp_plane.type()); |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 331 | break; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 332 | } |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 333 | DrmHwcLayer &layer = layers[source_layers.front()]; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 334 | if (!layer.buffer) { |
| 335 | ALOGE("Expected a valid framebuffer for pset"); |
| 336 | break; |
| 337 | } |
| 338 | fb_id = layer.buffer->fb_id; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 339 | fence_fd = layer.acquire_fence.get(); |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 340 | display_frame = layer.display_frame; |
| 341 | source_crop = layer.source_crop; |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 342 | alpha = layer.alpha; |
| 343 | |
| 344 | if (plane->blend_property().id()) { |
| 345 | switch (layer.blending) { |
| 346 | case DrmHwcBlending::kPreMult: |
| 347 | std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName( |
| 348 | "Pre-multiplied"); |
| 349 | break; |
| 350 | case DrmHwcBlending::kCoverage: |
| 351 | std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName( |
| 352 | "Coverage"); |
| 353 | break; |
| 354 | case DrmHwcBlending::kNone: |
| 355 | default: |
| 356 | std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName( |
| 357 | "None"); |
| 358 | break; |
| 359 | } |
| 360 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 361 | |
Sean Paul | 2689aeb | 2019-03-13 14:36:52 -0400 | [diff] [blame^] | 362 | if (plane->zpos_property().id() && |
| 363 | !plane->zpos_property().is_immutable()) { |
Alexandru Gheorghe | ea1c5e5 | 2018-09-17 10:48:54 +0100 | [diff] [blame] | 364 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 365 | plane->zpos_property().id(), |
| 366 | source_layers.front()) < 0; |
| 367 | if (ret) { |
| 368 | ALOGE("Failed to add zpos property %d to plane %d", |
| 369 | plane->zpos_property().id(), plane->id()); |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 374 | rotation = 0; |
| 375 | if (layer.transform & DrmHwcTransform::kFlipH) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 376 | rotation |= DRM_MODE_REFLECT_X; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 377 | if (layer.transform & DrmHwcTransform::kFlipV) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 378 | rotation |= DRM_MODE_REFLECT_Y; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 379 | if (layer.transform & DrmHwcTransform::kRotate90) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 380 | rotation |= DRM_MODE_ROTATE_90; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 381 | else if (layer.transform & DrmHwcTransform::kRotate180) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 382 | rotation |= DRM_MODE_ROTATE_180; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 383 | else if (layer.transform & DrmHwcTransform::kRotate270) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 384 | rotation |= DRM_MODE_ROTATE_270; |
Rob Herring | bd03b99 | 2017-11-01 11:21:48 -0500 | [diff] [blame] | 385 | else |
| 386 | rotation |= DRM_MODE_ROTATE_0; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 387 | |
Rob Herring | 8428e6a | 2018-02-12 16:03:35 -0600 | [diff] [blame] | 388 | if (fence_fd >= 0) { |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 389 | int prop_id = plane->in_fence_fd_property().id(); |
| 390 | if (prop_id == 0) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 391 | ALOGE("Failed to get IN_FENCE_FD property id"); |
| 392 | break; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 393 | } |
| 394 | ret = drmModeAtomicAddProperty(pset, plane->id(), prop_id, fence_fd); |
| 395 | if (ret < 0) { |
| 396 | ALOGE("Failed to add IN_FENCE_FD property to pset: %d", ret); |
| 397 | break; |
| 398 | } |
| 399 | } |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 400 | } |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 401 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 402 | // Disable the plane if there's no framebuffer |
| 403 | if (fb_id < 0) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 404 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 405 | plane->crtc_property().id(), 0) < 0 || |
| 406 | drmModeAtomicAddProperty(pset, plane->id(), |
| 407 | plane->fb_property().id(), 0) < 0; |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 408 | if (ret) { |
| 409 | ALOGE("Failed to add plane %d disable to pset", plane->id()); |
| 410 | break; |
| 411 | } |
| 412 | continue; |
| 413 | } |
| 414 | |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 415 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 416 | plane->crtc_property().id(), crtc->id()) < 0; |
| 417 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 418 | plane->fb_property().id(), fb_id) < 0; |
| 419 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 420 | plane->crtc_x_property().id(), |
| 421 | display_frame.left) < 0; |
| 422 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 423 | plane->crtc_y_property().id(), |
| 424 | display_frame.top) < 0; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 425 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 426 | plane->crtc_w_property().id(), |
| 427 | display_frame.right - display_frame.left) < |
| 428 | 0; |
| 429 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 430 | plane->crtc_h_property().id(), |
| 431 | display_frame.bottom - display_frame.top) < |
| 432 | 0; |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 433 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 434 | plane->src_x_property().id(), |
| 435 | (int)(source_crop.left) << 16) < 0; |
| 436 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 437 | plane->src_y_property().id(), |
| 438 | (int)(source_crop.top) << 16) < 0; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 439 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 440 | plane->src_w_property().id(), |
| 441 | (int)(source_crop.right - source_crop.left) |
| 442 | << 16) < 0; |
| 443 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 444 | plane->src_h_property().id(), |
| 445 | (int)(source_crop.bottom - source_crop.top) |
| 446 | << 16) < 0; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 447 | if (ret) { |
| 448 | ALOGE("Failed to add plane %d to set", plane->id()); |
| 449 | break; |
| 450 | } |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 451 | |
| 452 | if (plane->rotation_property().id()) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 453 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 454 | plane->rotation_property().id(), |
| 455 | rotation) < 0; |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 456 | if (ret) { |
| 457 | ALOGE("Failed to add rotation property %d to plane %d", |
| 458 | plane->rotation_property().id(), plane->id()); |
| 459 | break; |
| 460 | } |
| 461 | } |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 462 | |
| 463 | if (plane->alpha_property().id()) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 464 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 465 | plane->alpha_property().id(), alpha) < 0; |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 466 | if (ret) { |
| 467 | ALOGE("Failed to add alpha property %d to plane %d", |
| 468 | plane->alpha_property().id(), plane->id()); |
| 469 | break; |
| 470 | } |
| 471 | } |
Lowry Li | 9b6cafd | 2018-08-28 17:58:21 +0800 | [diff] [blame] | 472 | |
| 473 | if (plane->blend_property().id()) { |
| 474 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 475 | plane->blend_property().id(), blend) < 0; |
| 476 | if (ret) { |
| 477 | ALOGE("Failed to add pixel blend mode property %d to plane %d", |
| 478 | plane->blend_property().id(), plane->id()); |
| 479 | break; |
| 480 | } |
| 481 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (!ret) { |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 485 | uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 486 | if (test_only) |
| 487 | flags |= DRM_MODE_ATOMIC_TEST_ONLY; |
| 488 | |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 489 | ret = drmModeAtomicCommit(drm->fd(), pset, flags, drm); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 490 | if (ret) { |
John Stultz | 78c9f6c | 2018-05-24 16:43:35 -0700 | [diff] [blame] | 491 | if (!test_only) |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 492 | ALOGE("Failed to commit pset ret=%d\n", ret); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 493 | drmModeAtomicFree(pset); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 494 | return ret; |
| 495 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 496 | } |
| 497 | if (pset) |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 498 | drmModeAtomicFree(pset); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 499 | |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 500 | if (!test_only && mode_.needs_modeset) { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 501 | ret = drm->DestroyPropertyBlob(mode_.old_blob_id); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 502 | if (ret) { |
Sean Paul | f741c67 | 2016-05-11 13:49:38 -0400 | [diff] [blame] | 503 | ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d", |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 504 | mode_.old_blob_id, ret); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | /* TODO: Add dpms to the pset when the kernel supports it */ |
| 509 | ret = ApplyDpms(display_comp); |
| 510 | if (ret) { |
| 511 | ALOGE("Failed to apply DPMS after modeset %d\n", ret); |
| 512 | return ret; |
| 513 | } |
| 514 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 515 | connector->set_active_mode(mode_.mode); |
| 516 | mode_.old_blob_id = mode_.blob_id; |
| 517 | mode_.blob_id = 0; |
| 518 | mode_.needs_modeset = false; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 521 | if (crtc->out_fence_ptr_property().id()) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 522 | display_comp->set_out_fence((int)out_fences[crtc->pipe()]); |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 525 | return ret; |
| 526 | } |
| 527 | |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 528 | int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 529 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 530 | DrmConnector *conn = drm->GetConnectorForDisplay(display_); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 531 | if (!conn) { |
| 532 | ALOGE("Failed to get DrmConnector for display %d", display_); |
| 533 | return -ENODEV; |
| 534 | } |
| 535 | |
| 536 | const DrmProperty &prop = conn->dpms_property(); |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 537 | int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 538 | display_comp->dpms_mode()); |
| 539 | if (ret) { |
| 540 | ALOGE("Failed to set DPMS property for connector %d", conn->id()); |
| 541 | return ret; |
| 542 | } |
| 543 | return 0; |
| 544 | } |
| 545 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 546 | std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob( |
| 547 | const DrmMode &mode) { |
| 548 | struct drm_mode_modeinfo drm_mode; |
| 549 | memset(&drm_mode, 0, sizeof(drm_mode)); |
| 550 | mode.ToDrmModeModeInfo(&drm_mode); |
| 551 | |
| 552 | uint32_t id = 0; |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 553 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 554 | int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo), |
| 555 | &id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 556 | if (ret) { |
| 557 | ALOGE("Failed to create mode property blob %d", ret); |
| 558 | return std::make_tuple(ret, 0); |
| 559 | } |
Sean Paul | f741c67 | 2016-05-11 13:49:38 -0400 | [diff] [blame] | 560 | ALOGE("Create blob_id %" PRIu32 "\n", id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 561 | return std::make_tuple(ret, id); |
| 562 | } |
| 563 | |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 564 | void DrmDisplayCompositor::ClearDisplay() { |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 565 | if (!active_composition_) |
| 566 | return; |
| 567 | |
| 568 | if (DisablePlanes(active_composition_.get())) |
| 569 | return; |
| 570 | |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 571 | active_composition_.reset(NULL); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 572 | vsync_worker_.VSyncControl(false); |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 573 | } |
| 574 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 575 | void DrmDisplayCompositor::ApplyFrame( |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 576 | std::unique_ptr<DrmDisplayComposition> composition, int status, |
| 577 | bool writeback) { |
Alexandru Gheorghe | db440a9 | 2018-03-29 14:38:21 +0100 | [diff] [blame] | 578 | AutoLock lock(&lock_, __func__); |
| 579 | if (lock.Lock()) |
| 580 | return; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 581 | int ret = status; |
| 582 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 583 | if (!ret) { |
| 584 | if (writeback && !CountdownExpired()) { |
| 585 | ALOGE("Abort playing back scene"); |
| 586 | return; |
| 587 | } |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 588 | ret = CommitFrame(composition.get(), false); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 589 | } |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 590 | |
| 591 | if (ret) { |
| 592 | ALOGE("Composite failed for display %d", display_); |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 593 | // Disable the hw used by the last active composition. This allows us to |
| 594 | // signal the release fences from that composition to avoid hanging. |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 595 | ClearDisplay(); |
| 596 | return; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 597 | } |
| 598 | ++dump_frames_composited_; |
| 599 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 600 | active_composition_.swap(composition); |
| 601 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 602 | flatten_countdown_ = FLATTEN_COUNTDOWN_INIT; |
| 603 | vsync_worker_.VSyncControl(!writeback); |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 606 | int DrmDisplayCompositor::ApplyComposition( |
| 607 | std::unique_ptr<DrmDisplayComposition> composition) { |
| 608 | int ret = 0; |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 609 | switch (composition->type()) { |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 610 | case DRM_COMPOSITION_TYPE_FRAME: |
Haixia Shi | 6afbb6a | 2015-11-24 12:42:45 -0800 | [diff] [blame] | 611 | if (composition->geometry_changed()) { |
| 612 | // Send the composition to the kernel to ensure we can commit it. This |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 613 | // is just a test, it won't actually commit the frame. |
Haixia Shi | 6afbb6a | 2015-11-24 12:42:45 -0800 | [diff] [blame] | 614 | ret = CommitFrame(composition.get(), true); |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 615 | if (ret) { |
| 616 | ALOGE("Commit test failed for display %d, FIXME", display_); |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame] | 617 | return ret; |
Sean Paul | 647beb2 | 2015-11-19 13:55:48 -0500 | [diff] [blame] | 618 | } |
| 619 | } |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 620 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 621 | ApplyFrame(std::move(composition), ret); |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 622 | break; |
| 623 | case DRM_COMPOSITION_TYPE_DPMS: |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 624 | active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON); |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 625 | ret = ApplyDpms(composition.get()); |
| 626 | if (ret) |
| 627 | ALOGE("Failed to apply dpms for display %d", display_); |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 628 | return ret; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 629 | case DRM_COMPOSITION_TYPE_MODESET: |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 630 | mode_.mode = composition->display_mode(); |
| 631 | if (mode_.blob_id) |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 632 | resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob( |
| 633 | mode_.blob_id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 634 | std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode); |
| 635 | if (ret) { |
| 636 | ALOGE("Failed to create mode blob for display %d", display_); |
| 637 | return ret; |
| 638 | } |
| 639 | mode_.needs_modeset = true; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 640 | return 0; |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 641 | default: |
| 642 | ALOGE("Unknown composition type %d", composition->type()); |
| 643 | return -EINVAL; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 644 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 645 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 646 | return ret; |
| 647 | } |
| 648 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 649 | int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) { |
| 650 | return CommitFrame(composition, true); |
| 651 | } |
| 652 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 653 | // Flatten a scene on the display by using a writeback connector |
| 654 | // and returns the composition result as a DrmHwcLayer. |
| 655 | int DrmDisplayCompositor::FlattenOnDisplay( |
| 656 | std::unique_ptr<DrmDisplayComposition> &src, DrmConnector *writeback_conn, |
| 657 | DrmMode &src_mode, DrmHwcLayer *writeback_layer) { |
| 658 | int ret = 0; |
| 659 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 660 | ret = writeback_conn->UpdateModes(); |
| 661 | if (ret) { |
| 662 | ALOGE("Failed to update modes %d", ret); |
| 663 | return ret; |
| 664 | } |
| 665 | for (const DrmMode &mode : writeback_conn->modes()) { |
| 666 | if (mode.h_display() == src_mode.h_display() && |
| 667 | mode.v_display() == src_mode.v_display()) { |
| 668 | mode_.mode = mode; |
| 669 | if (mode_.blob_id) |
| 670 | drm->DestroyPropertyBlob(mode_.blob_id); |
| 671 | std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode); |
| 672 | if (ret) { |
| 673 | ALOGE("Failed to create mode blob for display %d", display_); |
| 674 | return ret; |
| 675 | } |
| 676 | mode_.needs_modeset = true; |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | if (mode_.blob_id <= 0) { |
| 681 | ALOGE("Failed to find similar mode"); |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
| 686 | if (!crtc) { |
| 687 | ALOGE("Failed to find crtc for display %d", display_); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | // TODO what happens if planes could go to both CRTCs, I don't think it's |
| 691 | // handled anywhere |
| 692 | std::vector<DrmPlane *> primary_planes; |
| 693 | std::vector<DrmPlane *> overlay_planes; |
| 694 | for (auto &plane : drm->planes()) { |
| 695 | if (!plane->GetCrtcSupported(*crtc)) |
| 696 | continue; |
| 697 | if (plane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 698 | primary_planes.push_back(plane.get()); |
| 699 | else if (plane->type() == DRM_PLANE_TYPE_OVERLAY) |
| 700 | overlay_planes.push_back(plane.get()); |
| 701 | } |
| 702 | |
| 703 | ret = src->Plan(&primary_planes, &overlay_planes); |
| 704 | if (ret) { |
| 705 | ALOGE("Failed to plan the composition ret = %d", ret); |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | // Disable the planes we're not using |
| 710 | for (auto i = primary_planes.begin(); i != primary_planes.end();) { |
| 711 | src->AddPlaneDisable(*i); |
| 712 | i = primary_planes.erase(i); |
| 713 | } |
| 714 | for (auto i = overlay_planes.begin(); i != overlay_planes.end();) { |
| 715 | src->AddPlaneDisable(*i); |
| 716 | i = overlay_planes.erase(i); |
| 717 | } |
| 718 | |
| 719 | AutoLock lock(&lock_, __func__); |
| 720 | ret = lock.Lock(); |
| 721 | if (ret) |
| 722 | return ret; |
| 723 | DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_]; |
| 724 | framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; |
| 725 | if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) { |
| 726 | ALOGE("Failed to allocate writeback buffer"); |
| 727 | return -ENOMEM; |
| 728 | } |
| 729 | DrmHwcBuffer *writeback_buffer = &writeback_layer->buffer; |
| 730 | writeback_layer->sf_handle = writeback_fb->buffer()->handle; |
| 731 | ret = writeback_layer->ImportBuffer( |
| 732 | resource_manager_->GetImporter(display_).get()); |
| 733 | if (ret) { |
| 734 | ALOGE("Failed to import writeback buffer"); |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | ret = CommitFrame(src.get(), true, writeback_conn, writeback_buffer); |
| 739 | if (ret) { |
| 740 | ALOGE("Atomic check failed"); |
| 741 | return ret; |
| 742 | } |
| 743 | ret = CommitFrame(src.get(), false, writeback_conn, writeback_buffer); |
| 744 | if (ret) { |
| 745 | ALOGE("Atomic commit failed"); |
| 746 | return ret; |
| 747 | } |
| 748 | |
| 749 | ret = sync_wait(writeback_fence_, kWaitWritebackFence); |
| 750 | writeback_layer->acquire_fence.Set(writeback_fence_); |
| 751 | writeback_fence_ = -1; |
| 752 | if (ret) { |
| 753 | ALOGE("Failed to wait on writeback fence"); |
| 754 | return ret; |
| 755 | } |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | // Flatten a scene by enabling the writeback connector attached |
| 760 | // to the same CRTC as the one driving the display. |
| 761 | int DrmDisplayCompositor::FlattenSerial(DrmConnector *writeback_conn) { |
| 762 | ALOGV("FlattenSerial by enabling writeback connector to the same crtc"); |
| 763 | // Flattened composition with only one layer that is obtained |
| 764 | // using the writeback connector |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 765 | std::unique_ptr<DrmDisplayComposition> |
| 766 | writeback_comp = CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 767 | if (!writeback_comp) |
| 768 | return -EINVAL; |
| 769 | |
| 770 | AutoLock lock(&lock_, __func__); |
| 771 | int ret = lock.Lock(); |
| 772 | if (ret) |
| 773 | return ret; |
| 774 | if (!CountdownExpired() || active_composition_->layers().size() < 2) { |
| 775 | ALOGV("Flattening is not needed"); |
| 776 | return -EALREADY; |
| 777 | } |
| 778 | |
| 779 | DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_]; |
| 780 | framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; |
| 781 | lock.Unlock(); |
| 782 | |
| 783 | if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) { |
| 784 | ALOGE("Failed to allocate writeback buffer"); |
| 785 | return -ENOMEM; |
| 786 | } |
| 787 | writeback_comp->layers().emplace_back(); |
| 788 | |
| 789 | DrmHwcLayer &writeback_layer = writeback_comp->layers().back(); |
| 790 | writeback_layer.sf_handle = writeback_fb->buffer()->handle; |
| 791 | writeback_layer.source_crop = {0, 0, (float)mode_.mode.h_display(), |
| 792 | (float)mode_.mode.v_display()}; |
| 793 | writeback_layer.display_frame = {0, 0, (int)mode_.mode.h_display(), |
| 794 | (int)mode_.mode.v_display()}; |
| 795 | ret = writeback_layer.ImportBuffer( |
| 796 | resource_manager_->GetImporter(display_).get()); |
| 797 | if (ret || writeback_comp->layers().size() != 1) { |
| 798 | ALOGE("Failed to import writeback buffer"); |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | drmModeAtomicReqPtr pset = drmModeAtomicAlloc(); |
| 803 | if (!pset) { |
| 804 | ALOGE("Failed to allocate property set"); |
| 805 | return -ENOMEM; |
| 806 | } |
| 807 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 808 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
| 809 | if (!crtc) { |
| 810 | ALOGE("Failed to find crtc for display %d", display_); |
| 811 | return -EINVAL; |
| 812 | } |
| 813 | ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn, |
| 814 | &writeback_layer.buffer); |
| 815 | if (ret < 0) { |
| 816 | ALOGE("Failed to Setup Writeback Commit"); |
| 817 | return ret; |
| 818 | } |
| 819 | ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm); |
| 820 | if (ret) { |
| 821 | ALOGE("Failed to enable writeback %d", ret); |
| 822 | return ret; |
| 823 | } |
| 824 | ret = sync_wait(writeback_fence_, kWaitWritebackFence); |
| 825 | writeback_layer.acquire_fence.Set(writeback_fence_); |
| 826 | writeback_fence_ = -1; |
| 827 | if (ret) { |
| 828 | ALOGE("Failed to wait on writeback fence"); |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL, |
| 833 | crtc); |
| 834 | for (auto &drmplane : drm->planes()) { |
| 835 | if (!drmplane->GetCrtcSupported(*crtc)) |
| 836 | continue; |
| 837 | if (!squashed_comp.plane() && drmplane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 838 | squashed_comp.set_plane(drmplane.get()); |
| 839 | else |
| 840 | writeback_comp->AddPlaneDisable(drmplane.get()); |
| 841 | } |
| 842 | squashed_comp.source_layers().push_back(0); |
| 843 | ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp)); |
| 844 | if (ret) { |
| 845 | ALOGE("Failed to add flatten scene"); |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | ApplyFrame(std::move(writeback_comp), 0, true); |
| 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | // Flatten a scene by using a crtc which works concurrent with |
| 854 | // the one driving the display. |
| 855 | int DrmDisplayCompositor::FlattenConcurrent(DrmConnector *writeback_conn) { |
| 856 | ALOGV("FlattenConcurrent by using an unused crtc/display"); |
| 857 | int ret = 0; |
| 858 | DrmDisplayCompositor drmdisplaycompositor; |
| 859 | ret = drmdisplaycompositor.Init(resource_manager_, writeback_conn->display()); |
| 860 | if (ret) { |
| 861 | ALOGE("Failed to init drmdisplaycompositor = %d", ret); |
| 862 | return ret; |
| 863 | } |
| 864 | // Copy of the active_composition, needed because of two things: |
| 865 | // 1) Not to hold the lock for the whole time we are accessing |
| 866 | // active_composition |
| 867 | // 2) It will be committed on a crtc that might not be on the same |
| 868 | // dri node, so buffers need to be imported on the right node. |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 869 | std::unique_ptr<DrmDisplayComposition> |
| 870 | copy_comp = drmdisplaycompositor.CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 871 | |
| 872 | // Writeback composition that will be committed to the display. |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 873 | std::unique_ptr<DrmDisplayComposition> |
| 874 | writeback_comp = CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 875 | |
| 876 | if (!copy_comp || !writeback_comp) |
| 877 | return -EINVAL; |
| 878 | AutoLock lock(&lock_, __func__); |
| 879 | ret = lock.Lock(); |
| 880 | if (ret) |
| 881 | return ret; |
| 882 | if (!CountdownExpired() || active_composition_->layers().size() < 2) { |
| 883 | ALOGV("Flattening is not needed"); |
| 884 | return -EALREADY; |
| 885 | } |
| 886 | DrmCrtc *crtc = active_composition_->crtc(); |
| 887 | |
| 888 | std::vector<DrmHwcLayer> copy_layers; |
| 889 | for (DrmHwcLayer &src_layer : active_composition_->layers()) { |
| 890 | DrmHwcLayer copy; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 891 | ret = copy.InitFromDrmHwcLayer(&src_layer, |
| 892 | resource_manager_ |
| 893 | ->GetImporter(writeback_conn->display()) |
| 894 | .get()); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 895 | if (ret) { |
| 896 | ALOGE("Failed to import buffer ret = %d", ret); |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | copy_layers.emplace_back(std::move(copy)); |
| 900 | } |
| 901 | ret = copy_comp->SetLayers(copy_layers.data(), copy_layers.size(), true); |
| 902 | if (ret) { |
| 903 | ALOGE("Failed to set copy_comp layers"); |
| 904 | return ret; |
| 905 | } |
| 906 | |
| 907 | lock.Unlock(); |
| 908 | DrmHwcLayer writeback_layer; |
| 909 | ret = drmdisplaycompositor.FlattenOnDisplay(copy_comp, writeback_conn, |
| 910 | mode_.mode, &writeback_layer); |
| 911 | if (ret) { |
| 912 | ALOGE("Failed to flatten on display ret = %d", ret); |
| 913 | return ret; |
| 914 | } |
| 915 | |
| 916 | DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL, |
| 917 | crtc); |
| 918 | for (auto &drmplane : resource_manager_->GetDrmDevice(display_)->planes()) { |
| 919 | if (!drmplane->GetCrtcSupported(*crtc)) |
| 920 | continue; |
| 921 | if (drmplane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 922 | squashed_comp.set_plane(drmplane.get()); |
| 923 | else |
| 924 | writeback_comp->AddPlaneDisable(drmplane.get()); |
| 925 | } |
| 926 | writeback_comp->layers().emplace_back(); |
| 927 | DrmHwcLayer &next_layer = writeback_comp->layers().back(); |
| 928 | next_layer.sf_handle = writeback_layer.get_usable_handle(); |
| 929 | next_layer.blending = DrmHwcBlending::kPreMult; |
| 930 | next_layer.source_crop = {0, 0, (float)mode_.mode.h_display(), |
| 931 | (float)mode_.mode.v_display()}; |
| 932 | next_layer.display_frame = {0, 0, (int)mode_.mode.h_display(), |
| 933 | (int)mode_.mode.v_display()}; |
| 934 | ret = next_layer.ImportBuffer(resource_manager_->GetImporter(display_).get()); |
| 935 | if (ret) { |
| 936 | ALOGE("Failed to import framebuffer for display %d", ret); |
| 937 | return ret; |
| 938 | } |
| 939 | squashed_comp.source_layers().push_back(0); |
| 940 | ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp)); |
| 941 | if (ret) { |
| 942 | ALOGE("Failed to add plane composition %d", ret); |
| 943 | return ret; |
| 944 | } |
| 945 | ApplyFrame(std::move(writeback_comp), 0, true); |
| 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | int DrmDisplayCompositor::FlattenActiveComposition() { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 950 | DrmConnector *writeback_conn = resource_manager_->AvailableWritebackConnector( |
| 951 | display_); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 952 | if (!active_composition_ || !writeback_conn) { |
| 953 | ALOGV("No writeback connector available"); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
| 957 | if (writeback_conn->display() != display_) { |
| 958 | return FlattenConcurrent(writeback_conn); |
| 959 | } else { |
| 960 | return FlattenSerial(writeback_conn); |
| 961 | } |
| 962 | |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | bool DrmDisplayCompositor::CountdownExpired() const { |
| 967 | return flatten_countdown_ <= 0; |
| 968 | } |
| 969 | |
| 970 | void DrmDisplayCompositor::Vsync(int display, int64_t timestamp) { |
| 971 | AutoLock lock(&lock_, __func__); |
| 972 | if (lock.Lock()) |
| 973 | return; |
| 974 | flatten_countdown_--; |
| 975 | if (!CountdownExpired()) |
| 976 | return; |
| 977 | lock.Unlock(); |
| 978 | int ret = FlattenActiveComposition(); |
| 979 | ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64 |
| 980 | " result = %d \n", |
| 981 | display, timestamp, ret); |
| 982 | } |
| 983 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 984 | void DrmDisplayCompositor::Dump(std::ostringstream *out) const { |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 985 | int ret = pthread_mutex_lock(&lock_); |
| 986 | if (ret) |
| 987 | return; |
| 988 | |
| 989 | uint64_t num_frames = dump_frames_composited_; |
| 990 | dump_frames_composited_ = 0; |
| 991 | |
| 992 | struct timespec ts; |
| 993 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 994 | if (ret) { |
| 995 | pthread_mutex_unlock(&lock_); |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 1000 | uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000); |
| 1001 | float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f; |
| 1002 | |
| 1003 | *out << "--DrmDisplayCompositor[" << display_ |
| 1004 | << "]: num_frames=" << num_frames << " num_ms=" << num_ms |
| 1005 | << " fps=" << fps << "\n"; |
| 1006 | |
| 1007 | dump_last_timestamp_ns_ = cur_ts; |
| 1008 | |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 1009 | pthread_mutex_unlock(&lock_); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 1010 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 1011 | } // namespace android |