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; |
Sean Paul | 04b47ea | 2015-11-19 21:46:11 -0500 | [diff] [blame] | 319 | |
Sean Paul | bbe39db | 2016-05-11 16:57:26 -0400 | [diff] [blame] | 320 | if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) { |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 321 | if (source_layers.size() > 1) { |
| 322 | ALOGE("Can't handle more than one source layer sz=%zu type=%d", |
| 323 | source_layers.size(), comp_plane.type()); |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | if (source_layers.empty() || source_layers.front() >= layers.size()) { |
| 328 | ALOGE("Source layer index %zu out of bounds %zu type=%d", |
| 329 | source_layers.front(), layers.size(), comp_plane.type()); |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 330 | break; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 331 | } |
Sean Paul | ca699be | 2016-05-11 16:29:45 -0400 | [diff] [blame] | 332 | DrmHwcLayer &layer = layers[source_layers.front()]; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 333 | if (!layer.buffer) { |
| 334 | ALOGE("Expected a valid framebuffer for pset"); |
| 335 | break; |
| 336 | } |
| 337 | fb_id = layer.buffer->fb_id; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 338 | fence_fd = layer.acquire_fence.get(); |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 339 | display_frame = layer.display_frame; |
| 340 | source_crop = layer.source_crop; |
| 341 | if (layer.blending == DrmHwcBlending::kPreMult) |
| 342 | alpha = layer.alpha; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 343 | |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 344 | rotation = 0; |
| 345 | if (layer.transform & DrmHwcTransform::kFlipH) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 346 | rotation |= DRM_MODE_REFLECT_X; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 347 | if (layer.transform & DrmHwcTransform::kFlipV) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 348 | rotation |= DRM_MODE_REFLECT_Y; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 349 | if (layer.transform & DrmHwcTransform::kRotate90) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 350 | rotation |= DRM_MODE_ROTATE_90; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 351 | else if (layer.transform & DrmHwcTransform::kRotate180) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 352 | rotation |= DRM_MODE_ROTATE_180; |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 353 | else if (layer.transform & DrmHwcTransform::kRotate270) |
Rob Herring | 89095cc | 2017-10-06 16:46:48 -0500 | [diff] [blame] | 354 | rotation |= DRM_MODE_ROTATE_270; |
Rob Herring | bd03b99 | 2017-11-01 11:21:48 -0500 | [diff] [blame] | 355 | else |
| 356 | rotation |= DRM_MODE_ROTATE_0; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 357 | |
Rob Herring | 8428e6a | 2018-02-12 16:03:35 -0600 | [diff] [blame] | 358 | if (fence_fd >= 0) { |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 359 | int prop_id = plane->in_fence_fd_property().id(); |
| 360 | if (prop_id == 0) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 361 | ALOGE("Failed to get IN_FENCE_FD property id"); |
| 362 | break; |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 363 | } |
| 364 | ret = drmModeAtomicAddProperty(pset, plane->id(), prop_id, fence_fd); |
| 365 | if (ret < 0) { |
| 366 | ALOGE("Failed to add IN_FENCE_FD property to pset: %d", ret); |
| 367 | break; |
| 368 | } |
| 369 | } |
Sean Paul | 39b3784 | 2016-05-11 13:50:28 -0400 | [diff] [blame] | 370 | } |
Robert Foss | b2bdfd8 | 2016-10-19 10:46:22 -0400 | [diff] [blame] | 371 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 372 | // Disable the plane if there's no framebuffer |
| 373 | if (fb_id < 0) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 374 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 375 | plane->crtc_property().id(), 0) < 0 || |
| 376 | drmModeAtomicAddProperty(pset, plane->id(), |
| 377 | plane->fb_property().id(), 0) < 0; |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 378 | if (ret) { |
| 379 | ALOGE("Failed to add plane %d disable to pset", plane->id()); |
| 380 | break; |
| 381 | } |
| 382 | continue; |
| 383 | } |
| 384 | |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 385 | // TODO: Once we have atomic test, this should fall back to GL |
Rob Herring | bd03b99 | 2017-11-01 11:21:48 -0500 | [diff] [blame] | 386 | if (rotation != DRM_MODE_ROTATE_0 && plane->rotation_property().id() == 0) { |
John Stultz | 78c9f6c | 2018-05-24 16:43:35 -0700 | [diff] [blame] | 387 | ALOGV("Rotation is not supported on plane %d", plane->id()); |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 388 | ret = -EINVAL; |
| 389 | break; |
| 390 | } |
| 391 | |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 392 | // TODO: Once we have atomic test, this should fall back to GL |
John Stultz | d3e5213 | 2018-05-04 13:22:45 -0700 | [diff] [blame] | 393 | if (alpha != 0xFFFF && plane->alpha_property().id() == 0) { |
John Stultz | 78c9f6c | 2018-05-24 16:43:35 -0700 | [diff] [blame] | 394 | ALOGV("Alpha is not supported on plane %d", plane->id()); |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 395 | ret = -EINVAL; |
| 396 | break; |
| 397 | } |
| 398 | |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 399 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 400 | plane->crtc_property().id(), crtc->id()) < 0; |
| 401 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 402 | plane->fb_property().id(), fb_id) < 0; |
| 403 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 404 | plane->crtc_x_property().id(), |
| 405 | display_frame.left) < 0; |
| 406 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 407 | plane->crtc_y_property().id(), |
| 408 | display_frame.top) < 0; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 409 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 410 | plane->crtc_w_property().id(), |
| 411 | display_frame.right - display_frame.left) < |
| 412 | 0; |
| 413 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 414 | plane->crtc_h_property().id(), |
| 415 | display_frame.bottom - display_frame.top) < |
| 416 | 0; |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 417 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 418 | plane->src_x_property().id(), |
| 419 | (int)(source_crop.left) << 16) < 0; |
| 420 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 421 | plane->src_y_property().id(), |
| 422 | (int)(source_crop.top) << 16) < 0; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 423 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 424 | plane->src_w_property().id(), |
| 425 | (int)(source_crop.right - source_crop.left) |
| 426 | << 16) < 0; |
| 427 | ret |= drmModeAtomicAddProperty(pset, plane->id(), |
| 428 | plane->src_h_property().id(), |
| 429 | (int)(source_crop.bottom - source_crop.top) |
| 430 | << 16) < 0; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 431 | if (ret) { |
| 432 | ALOGE("Failed to add plane %d to set", plane->id()); |
| 433 | break; |
| 434 | } |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 435 | |
| 436 | if (plane->rotation_property().id()) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 437 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
| 438 | plane->rotation_property().id(), |
| 439 | rotation) < 0; |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 440 | if (ret) { |
| 441 | ALOGE("Failed to add rotation property %d to plane %d", |
| 442 | plane->rotation_property().id(), plane->id()); |
| 443 | break; |
| 444 | } |
| 445 | } |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 446 | |
| 447 | if (plane->alpha_property().id()) { |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 448 | ret = drmModeAtomicAddProperty(pset, plane->id(), |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 449 | plane->alpha_property().id(), alpha) < 0; |
Sean Paul | d8aefb6 | 2015-10-15 15:17:31 -0400 | [diff] [blame] | 450 | if (ret) { |
| 451 | ALOGE("Failed to add alpha property %d to plane %d", |
| 452 | plane->alpha_property().id(), plane->id()); |
| 453 | break; |
| 454 | } |
| 455 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | if (!ret) { |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 459 | uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET; |
| 460 | if (test_only) |
| 461 | flags |= DRM_MODE_ATOMIC_TEST_ONLY; |
| 462 | |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 463 | ret = drmModeAtomicCommit(drm->fd(), pset, flags, drm); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 464 | if (ret) { |
John Stultz | 78c9f6c | 2018-05-24 16:43:35 -0700 | [diff] [blame] | 465 | if (!test_only) |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 466 | ALOGE("Failed to commit pset ret=%d\n", ret); |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 467 | drmModeAtomicFree(pset); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 468 | return ret; |
| 469 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 470 | } |
| 471 | if (pset) |
Rob Herring | bc9305e | 2016-02-04 14:01:24 -0600 | [diff] [blame] | 472 | drmModeAtomicFree(pset); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 473 | |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 474 | if (!test_only && mode_.needs_modeset) { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 475 | ret = drm->DestroyPropertyBlob(mode_.old_blob_id); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 476 | if (ret) { |
Sean Paul | f741c67 | 2016-05-11 13:49:38 -0400 | [diff] [blame] | 477 | ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d", |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 478 | mode_.old_blob_id, ret); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | /* TODO: Add dpms to the pset when the kernel supports it */ |
| 483 | ret = ApplyDpms(display_comp); |
| 484 | if (ret) { |
| 485 | ALOGE("Failed to apply DPMS after modeset %d\n", ret); |
| 486 | return ret; |
| 487 | } |
| 488 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 489 | connector->set_active_mode(mode_.mode); |
| 490 | mode_.old_blob_id = mode_.blob_id; |
| 491 | mode_.blob_id = 0; |
| 492 | mode_.needs_modeset = false; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 493 | } |
| 494 | |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 495 | if (crtc->out_fence_ptr_property().id()) { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 496 | display_comp->set_out_fence((int)out_fences[crtc->pipe()]); |
Robert Foss | a1ade4e | 2017-09-27 19:28:15 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 499 | return ret; |
| 500 | } |
| 501 | |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 502 | int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) { |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 503 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 504 | DrmConnector *conn = drm->GetConnectorForDisplay(display_); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 505 | if (!conn) { |
| 506 | ALOGE("Failed to get DrmConnector for display %d", display_); |
| 507 | return -ENODEV; |
| 508 | } |
| 509 | |
| 510 | const DrmProperty &prop = conn->dpms_property(); |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 511 | int ret = drmModeConnectorSetProperty(drm->fd(), conn->id(), prop.id(), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 512 | display_comp->dpms_mode()); |
| 513 | if (ret) { |
| 514 | ALOGE("Failed to set DPMS property for connector %d", conn->id()); |
| 515 | return ret; |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 520 | std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob( |
| 521 | const DrmMode &mode) { |
| 522 | struct drm_mode_modeinfo drm_mode; |
| 523 | memset(&drm_mode, 0, sizeof(drm_mode)); |
| 524 | mode.ToDrmModeModeInfo(&drm_mode); |
| 525 | |
| 526 | uint32_t id = 0; |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 527 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 528 | int ret = drm->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo), |
| 529 | &id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 530 | if (ret) { |
| 531 | ALOGE("Failed to create mode property blob %d", ret); |
| 532 | return std::make_tuple(ret, 0); |
| 533 | } |
Sean Paul | f741c67 | 2016-05-11 13:49:38 -0400 | [diff] [blame] | 534 | ALOGE("Create blob_id %" PRIu32 "\n", id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 535 | return std::make_tuple(ret, id); |
| 536 | } |
| 537 | |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 538 | void DrmDisplayCompositor::ClearDisplay() { |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 539 | if (!active_composition_) |
| 540 | return; |
| 541 | |
| 542 | if (DisablePlanes(active_composition_.get())) |
| 543 | return; |
| 544 | |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 545 | active_composition_.reset(NULL); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 546 | vsync_worker_.VSyncControl(false); |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 549 | void DrmDisplayCompositor::ApplyFrame( |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 550 | std::unique_ptr<DrmDisplayComposition> composition, int status, |
| 551 | bool writeback) { |
Alexandru Gheorghe | db440a9 | 2018-03-29 14:38:21 +0100 | [diff] [blame] | 552 | AutoLock lock(&lock_, __func__); |
| 553 | if (lock.Lock()) |
| 554 | return; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 555 | int ret = status; |
| 556 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 557 | if (!ret) { |
| 558 | if (writeback && !CountdownExpired()) { |
| 559 | ALOGE("Abort playing back scene"); |
| 560 | return; |
| 561 | } |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 562 | ret = CommitFrame(composition.get(), false); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 563 | } |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 564 | |
| 565 | if (ret) { |
| 566 | ALOGE("Composite failed for display %d", display_); |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 567 | // Disable the hw used by the last active composition. This allows us to |
| 568 | // signal the release fences from that composition to avoid hanging. |
Sean Paul | 137a6a8 | 2016-06-22 22:48:22 -0400 | [diff] [blame] | 569 | ClearDisplay(); |
| 570 | return; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 571 | } |
| 572 | ++dump_frames_composited_; |
| 573 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 574 | active_composition_.swap(composition); |
| 575 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 576 | flatten_countdown_ = FLATTEN_COUNTDOWN_INIT; |
| 577 | vsync_worker_.VSyncControl(!writeback); |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 580 | int DrmDisplayCompositor::ApplyComposition( |
| 581 | std::unique_ptr<DrmDisplayComposition> composition) { |
| 582 | int ret = 0; |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 583 | switch (composition->type()) { |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 584 | case DRM_COMPOSITION_TYPE_FRAME: |
Haixia Shi | 6afbb6a | 2015-11-24 12:42:45 -0800 | [diff] [blame] | 585 | if (composition->geometry_changed()) { |
| 586 | // 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] | 587 | // is just a test, it won't actually commit the frame. |
Haixia Shi | 6afbb6a | 2015-11-24 12:42:45 -0800 | [diff] [blame] | 588 | ret = CommitFrame(composition.get(), true); |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 589 | if (ret) { |
| 590 | ALOGE("Commit test failed for display %d, FIXME", display_); |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame] | 591 | return ret; |
Sean Paul | 647beb2 | 2015-11-19 13:55:48 -0500 | [diff] [blame] | 592 | } |
| 593 | } |
Rob Herring | af0d975 | 2018-05-04 16:34:19 -0500 | [diff] [blame] | 594 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 595 | ApplyFrame(std::move(composition), ret); |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 596 | break; |
| 597 | case DRM_COMPOSITION_TYPE_DPMS: |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 598 | active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON); |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 599 | ret = ApplyDpms(composition.get()); |
| 600 | if (ret) |
| 601 | ALOGE("Failed to apply dpms for display %d", display_); |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 602 | return ret; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 603 | case DRM_COMPOSITION_TYPE_MODESET: |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 604 | mode_.mode = composition->display_mode(); |
| 605 | if (mode_.blob_id) |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 606 | resource_manager_->GetDrmDevice(display_)->DestroyPropertyBlob( |
| 607 | mode_.blob_id); |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 608 | std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode); |
| 609 | if (ret) { |
| 610 | ALOGE("Failed to create mode blob for display %d", display_); |
| 611 | return ret; |
| 612 | } |
| 613 | mode_.needs_modeset = true; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 614 | return 0; |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 615 | default: |
| 616 | ALOGE("Unknown composition type %d", composition->type()); |
| 617 | return -EINVAL; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 618 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 619 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 620 | return ret; |
| 621 | } |
| 622 | |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 623 | int DrmDisplayCompositor::TestComposition(DrmDisplayComposition *composition) { |
| 624 | return CommitFrame(composition, true); |
| 625 | } |
| 626 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 627 | // Flatten a scene on the display by using a writeback connector |
| 628 | // and returns the composition result as a DrmHwcLayer. |
| 629 | int DrmDisplayCompositor::FlattenOnDisplay( |
| 630 | std::unique_ptr<DrmDisplayComposition> &src, DrmConnector *writeback_conn, |
| 631 | DrmMode &src_mode, DrmHwcLayer *writeback_layer) { |
| 632 | int ret = 0; |
| 633 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 634 | ret = writeback_conn->UpdateModes(); |
| 635 | if (ret) { |
| 636 | ALOGE("Failed to update modes %d", ret); |
| 637 | return ret; |
| 638 | } |
| 639 | for (const DrmMode &mode : writeback_conn->modes()) { |
| 640 | if (mode.h_display() == src_mode.h_display() && |
| 641 | mode.v_display() == src_mode.v_display()) { |
| 642 | mode_.mode = mode; |
| 643 | if (mode_.blob_id) |
| 644 | drm->DestroyPropertyBlob(mode_.blob_id); |
| 645 | std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode); |
| 646 | if (ret) { |
| 647 | ALOGE("Failed to create mode blob for display %d", display_); |
| 648 | return ret; |
| 649 | } |
| 650 | mode_.needs_modeset = true; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | if (mode_.blob_id <= 0) { |
| 655 | ALOGE("Failed to find similar mode"); |
| 656 | return -EINVAL; |
| 657 | } |
| 658 | |
| 659 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
| 660 | if (!crtc) { |
| 661 | ALOGE("Failed to find crtc for display %d", display_); |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | // TODO what happens if planes could go to both CRTCs, I don't think it's |
| 665 | // handled anywhere |
| 666 | std::vector<DrmPlane *> primary_planes; |
| 667 | std::vector<DrmPlane *> overlay_planes; |
| 668 | for (auto &plane : drm->planes()) { |
| 669 | if (!plane->GetCrtcSupported(*crtc)) |
| 670 | continue; |
| 671 | if (plane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 672 | primary_planes.push_back(plane.get()); |
| 673 | else if (plane->type() == DRM_PLANE_TYPE_OVERLAY) |
| 674 | overlay_planes.push_back(plane.get()); |
| 675 | } |
| 676 | |
| 677 | ret = src->Plan(&primary_planes, &overlay_planes); |
| 678 | if (ret) { |
| 679 | ALOGE("Failed to plan the composition ret = %d", ret); |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | // Disable the planes we're not using |
| 684 | for (auto i = primary_planes.begin(); i != primary_planes.end();) { |
| 685 | src->AddPlaneDisable(*i); |
| 686 | i = primary_planes.erase(i); |
| 687 | } |
| 688 | for (auto i = overlay_planes.begin(); i != overlay_planes.end();) { |
| 689 | src->AddPlaneDisable(*i); |
| 690 | i = overlay_planes.erase(i); |
| 691 | } |
| 692 | |
| 693 | AutoLock lock(&lock_, __func__); |
| 694 | ret = lock.Lock(); |
| 695 | if (ret) |
| 696 | return ret; |
| 697 | DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_]; |
| 698 | framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; |
| 699 | if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) { |
| 700 | ALOGE("Failed to allocate writeback buffer"); |
| 701 | return -ENOMEM; |
| 702 | } |
| 703 | DrmHwcBuffer *writeback_buffer = &writeback_layer->buffer; |
| 704 | writeback_layer->sf_handle = writeback_fb->buffer()->handle; |
| 705 | ret = writeback_layer->ImportBuffer( |
| 706 | resource_manager_->GetImporter(display_).get()); |
| 707 | if (ret) { |
| 708 | ALOGE("Failed to import writeback buffer"); |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | ret = CommitFrame(src.get(), true, writeback_conn, writeback_buffer); |
| 713 | if (ret) { |
| 714 | ALOGE("Atomic check failed"); |
| 715 | return ret; |
| 716 | } |
| 717 | ret = CommitFrame(src.get(), false, writeback_conn, writeback_buffer); |
| 718 | if (ret) { |
| 719 | ALOGE("Atomic commit failed"); |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | ret = sync_wait(writeback_fence_, kWaitWritebackFence); |
| 724 | writeback_layer->acquire_fence.Set(writeback_fence_); |
| 725 | writeback_fence_ = -1; |
| 726 | if (ret) { |
| 727 | ALOGE("Failed to wait on writeback fence"); |
| 728 | return ret; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | // Flatten a scene by enabling the writeback connector attached |
| 734 | // to the same CRTC as the one driving the display. |
| 735 | int DrmDisplayCompositor::FlattenSerial(DrmConnector *writeback_conn) { |
| 736 | ALOGV("FlattenSerial by enabling writeback connector to the same crtc"); |
| 737 | // Flattened composition with only one layer that is obtained |
| 738 | // using the writeback connector |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 739 | std::unique_ptr<DrmDisplayComposition> |
| 740 | writeback_comp = CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 741 | if (!writeback_comp) |
| 742 | return -EINVAL; |
| 743 | |
| 744 | AutoLock lock(&lock_, __func__); |
| 745 | int ret = lock.Lock(); |
| 746 | if (ret) |
| 747 | return ret; |
| 748 | if (!CountdownExpired() || active_composition_->layers().size() < 2) { |
| 749 | ALOGV("Flattening is not needed"); |
| 750 | return -EALREADY; |
| 751 | } |
| 752 | |
| 753 | DrmFramebuffer *writeback_fb = &framebuffers_[framebuffer_index_]; |
| 754 | framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; |
| 755 | lock.Unlock(); |
| 756 | |
| 757 | if (!writeback_fb->Allocate(mode_.mode.h_display(), mode_.mode.v_display())) { |
| 758 | ALOGE("Failed to allocate writeback buffer"); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | writeback_comp->layers().emplace_back(); |
| 762 | |
| 763 | DrmHwcLayer &writeback_layer = writeback_comp->layers().back(); |
| 764 | writeback_layer.sf_handle = writeback_fb->buffer()->handle; |
| 765 | writeback_layer.source_crop = {0, 0, (float)mode_.mode.h_display(), |
| 766 | (float)mode_.mode.v_display()}; |
| 767 | writeback_layer.display_frame = {0, 0, (int)mode_.mode.h_display(), |
| 768 | (int)mode_.mode.v_display()}; |
| 769 | ret = writeback_layer.ImportBuffer( |
| 770 | resource_manager_->GetImporter(display_).get()); |
| 771 | if (ret || writeback_comp->layers().size() != 1) { |
| 772 | ALOGE("Failed to import writeback buffer"); |
| 773 | return ret; |
| 774 | } |
| 775 | |
| 776 | drmModeAtomicReqPtr pset = drmModeAtomicAlloc(); |
| 777 | if (!pset) { |
| 778 | ALOGE("Failed to allocate property set"); |
| 779 | return -ENOMEM; |
| 780 | } |
| 781 | DrmDevice *drm = resource_manager_->GetDrmDevice(display_); |
| 782 | DrmCrtc *crtc = drm->GetCrtcForDisplay(display_); |
| 783 | if (!crtc) { |
| 784 | ALOGE("Failed to find crtc for display %d", display_); |
| 785 | return -EINVAL; |
| 786 | } |
| 787 | ret = SetupWritebackCommit(pset, crtc->id(), writeback_conn, |
| 788 | &writeback_layer.buffer); |
| 789 | if (ret < 0) { |
| 790 | ALOGE("Failed to Setup Writeback Commit"); |
| 791 | return ret; |
| 792 | } |
| 793 | ret = drmModeAtomicCommit(drm->fd(), pset, 0, drm); |
| 794 | if (ret) { |
| 795 | ALOGE("Failed to enable writeback %d", ret); |
| 796 | return ret; |
| 797 | } |
| 798 | ret = sync_wait(writeback_fence_, kWaitWritebackFence); |
| 799 | writeback_layer.acquire_fence.Set(writeback_fence_); |
| 800 | writeback_fence_ = -1; |
| 801 | if (ret) { |
| 802 | ALOGE("Failed to wait on writeback fence"); |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL, |
| 807 | crtc); |
| 808 | for (auto &drmplane : drm->planes()) { |
| 809 | if (!drmplane->GetCrtcSupported(*crtc)) |
| 810 | continue; |
| 811 | if (!squashed_comp.plane() && drmplane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 812 | squashed_comp.set_plane(drmplane.get()); |
| 813 | else |
| 814 | writeback_comp->AddPlaneDisable(drmplane.get()); |
| 815 | } |
| 816 | squashed_comp.source_layers().push_back(0); |
| 817 | ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp)); |
| 818 | if (ret) { |
| 819 | ALOGE("Failed to add flatten scene"); |
| 820 | return ret; |
| 821 | } |
| 822 | |
| 823 | ApplyFrame(std::move(writeback_comp), 0, true); |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | // Flatten a scene by using a crtc which works concurrent with |
| 828 | // the one driving the display. |
| 829 | int DrmDisplayCompositor::FlattenConcurrent(DrmConnector *writeback_conn) { |
| 830 | ALOGV("FlattenConcurrent by using an unused crtc/display"); |
| 831 | int ret = 0; |
| 832 | DrmDisplayCompositor drmdisplaycompositor; |
| 833 | ret = drmdisplaycompositor.Init(resource_manager_, writeback_conn->display()); |
| 834 | if (ret) { |
| 835 | ALOGE("Failed to init drmdisplaycompositor = %d", ret); |
| 836 | return ret; |
| 837 | } |
| 838 | // Copy of the active_composition, needed because of two things: |
| 839 | // 1) Not to hold the lock for the whole time we are accessing |
| 840 | // active_composition |
| 841 | // 2) It will be committed on a crtc that might not be on the same |
| 842 | // dri node, so buffers need to be imported on the right node. |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 843 | std::unique_ptr<DrmDisplayComposition> |
| 844 | copy_comp = drmdisplaycompositor.CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 845 | |
| 846 | // Writeback composition that will be committed to the display. |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 847 | std::unique_ptr<DrmDisplayComposition> |
| 848 | writeback_comp = CreateInitializedComposition(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 849 | |
| 850 | if (!copy_comp || !writeback_comp) |
| 851 | return -EINVAL; |
| 852 | AutoLock lock(&lock_, __func__); |
| 853 | ret = lock.Lock(); |
| 854 | if (ret) |
| 855 | return ret; |
| 856 | if (!CountdownExpired() || active_composition_->layers().size() < 2) { |
| 857 | ALOGV("Flattening is not needed"); |
| 858 | return -EALREADY; |
| 859 | } |
| 860 | DrmCrtc *crtc = active_composition_->crtc(); |
| 861 | |
| 862 | std::vector<DrmHwcLayer> copy_layers; |
| 863 | for (DrmHwcLayer &src_layer : active_composition_->layers()) { |
| 864 | DrmHwcLayer copy; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 865 | ret = copy.InitFromDrmHwcLayer(&src_layer, |
| 866 | resource_manager_ |
| 867 | ->GetImporter(writeback_conn->display()) |
| 868 | .get()); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 869 | if (ret) { |
| 870 | ALOGE("Failed to import buffer ret = %d", ret); |
| 871 | return -EINVAL; |
| 872 | } |
| 873 | copy_layers.emplace_back(std::move(copy)); |
| 874 | } |
| 875 | ret = copy_comp->SetLayers(copy_layers.data(), copy_layers.size(), true); |
| 876 | if (ret) { |
| 877 | ALOGE("Failed to set copy_comp layers"); |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | lock.Unlock(); |
| 882 | DrmHwcLayer writeback_layer; |
| 883 | ret = drmdisplaycompositor.FlattenOnDisplay(copy_comp, writeback_conn, |
| 884 | mode_.mode, &writeback_layer); |
| 885 | if (ret) { |
| 886 | ALOGE("Failed to flatten on display ret = %d", ret); |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kLayer, NULL, |
| 891 | crtc); |
| 892 | for (auto &drmplane : resource_manager_->GetDrmDevice(display_)->planes()) { |
| 893 | if (!drmplane->GetCrtcSupported(*crtc)) |
| 894 | continue; |
| 895 | if (drmplane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 896 | squashed_comp.set_plane(drmplane.get()); |
| 897 | else |
| 898 | writeback_comp->AddPlaneDisable(drmplane.get()); |
| 899 | } |
| 900 | writeback_comp->layers().emplace_back(); |
| 901 | DrmHwcLayer &next_layer = writeback_comp->layers().back(); |
| 902 | next_layer.sf_handle = writeback_layer.get_usable_handle(); |
| 903 | next_layer.blending = DrmHwcBlending::kPreMult; |
| 904 | next_layer.source_crop = {0, 0, (float)mode_.mode.h_display(), |
| 905 | (float)mode_.mode.v_display()}; |
| 906 | next_layer.display_frame = {0, 0, (int)mode_.mode.h_display(), |
| 907 | (int)mode_.mode.v_display()}; |
| 908 | ret = next_layer.ImportBuffer(resource_manager_->GetImporter(display_).get()); |
| 909 | if (ret) { |
| 910 | ALOGE("Failed to import framebuffer for display %d", ret); |
| 911 | return ret; |
| 912 | } |
| 913 | squashed_comp.source_layers().push_back(0); |
| 914 | ret = writeback_comp->AddPlaneComposition(std::move(squashed_comp)); |
| 915 | if (ret) { |
| 916 | ALOGE("Failed to add plane composition %d", ret); |
| 917 | return ret; |
| 918 | } |
| 919 | ApplyFrame(std::move(writeback_comp), 0, true); |
| 920 | return ret; |
| 921 | } |
| 922 | |
| 923 | int DrmDisplayCompositor::FlattenActiveComposition() { |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 924 | DrmConnector *writeback_conn = resource_manager_->AvailableWritebackConnector( |
| 925 | display_); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 926 | if (!active_composition_ || !writeback_conn) { |
| 927 | ALOGV("No writeback connector available"); |
| 928 | return -EINVAL; |
| 929 | } |
| 930 | |
| 931 | if (writeback_conn->display() != display_) { |
| 932 | return FlattenConcurrent(writeback_conn); |
| 933 | } else { |
| 934 | return FlattenSerial(writeback_conn); |
| 935 | } |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | bool DrmDisplayCompositor::CountdownExpired() const { |
| 941 | return flatten_countdown_ <= 0; |
| 942 | } |
| 943 | |
| 944 | void DrmDisplayCompositor::Vsync(int display, int64_t timestamp) { |
| 945 | AutoLock lock(&lock_, __func__); |
| 946 | if (lock.Lock()) |
| 947 | return; |
| 948 | flatten_countdown_--; |
| 949 | if (!CountdownExpired()) |
| 950 | return; |
| 951 | lock.Unlock(); |
| 952 | int ret = FlattenActiveComposition(); |
| 953 | ALOGV("scene flattening triggered for display %d at timestamp %" PRIu64 |
| 954 | " result = %d \n", |
| 955 | display, timestamp, ret); |
| 956 | } |
| 957 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 958 | void DrmDisplayCompositor::Dump(std::ostringstream *out) const { |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 959 | int ret = pthread_mutex_lock(&lock_); |
| 960 | if (ret) |
| 961 | return; |
| 962 | |
| 963 | uint64_t num_frames = dump_frames_composited_; |
| 964 | dump_frames_composited_ = 0; |
| 965 | |
| 966 | struct timespec ts; |
| 967 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 968 | if (ret) { |
| 969 | pthread_mutex_unlock(&lock_); |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 974 | uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000); |
| 975 | float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f; |
| 976 | |
| 977 | *out << "--DrmDisplayCompositor[" << display_ |
| 978 | << "]: num_frames=" << num_frames << " num_ms=" << num_ms |
| 979 | << " fps=" << fps << "\n"; |
| 980 | |
| 981 | dump_last_timestamp_ns_ = cur_ts; |
| 982 | |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 983 | pthread_mutex_unlock(&lock_); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 984 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame^] | 985 | } // namespace android |