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