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" |
| 21 | #include "drmcrtc.h" |
| 22 | #include "drmplane.h" |
| 23 | #include "drmresources.h" |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 24 | #include "glworker.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 25 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 27 | #include <pthread.h> |
Zach Reizner | d410f04 | 2015-07-28 15:33:07 -0700 | [diff] [blame] | 28 | #include <sched.h> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 29 | #include <sstream> |
| 30 | #include <stdlib.h> |
| 31 | #include <time.h> |
| 32 | #include <vector> |
| 33 | |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 34 | #include <drm/drm_mode.h> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 35 | #include <cutils/log.h> |
| 36 | #include <sync/sync.h> |
| 37 | #include <utils/Trace.h> |
| 38 | |
Zach Reizner | d410f04 | 2015-07-28 15:33:07 -0700 | [diff] [blame] | 39 | #define DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH 3 |
| 40 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
| 43 | DrmDisplayCompositor::DrmDisplayCompositor() |
| 44 | : drm_(NULL), |
| 45 | display_(-1), |
| 46 | worker_(this), |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 47 | initialized_(false), |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 48 | active_(false), |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 49 | needs_modeset_(false), |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 50 | framebuffer_index_(0), |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 51 | dump_frames_composited_(0), |
| 52 | dump_last_timestamp_ns_(0) { |
| 53 | struct timespec ts; |
| 54 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 55 | return; |
| 56 | dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 57 | } |
| 58 | |
| 59 | DrmDisplayCompositor::~DrmDisplayCompositor() { |
| 60 | if (!initialized_) |
| 61 | return; |
| 62 | |
| 63 | worker_.Exit(); |
| 64 | |
| 65 | int ret = pthread_mutex_lock(&lock_); |
| 66 | if (ret) |
| 67 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 68 | |
| 69 | while (!composite_queue_.empty()) { |
| 70 | composite_queue_.front().reset(); |
| 71 | composite_queue_.pop(); |
| 72 | } |
| 73 | active_composition_.reset(); |
| 74 | |
| 75 | ret = pthread_mutex_unlock(&lock_); |
| 76 | if (ret) |
| 77 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 78 | |
| 79 | pthread_mutex_destroy(&lock_); |
| 80 | } |
| 81 | |
| 82 | int DrmDisplayCompositor::Init(DrmResources *drm, int display) { |
| 83 | drm_ = drm; |
| 84 | display_ = display; |
| 85 | |
| 86 | int ret = pthread_mutex_init(&lock_, NULL); |
| 87 | if (ret) { |
| 88 | ALOGE("Failed to initialize drm compositor lock %d\n", ret); |
| 89 | return ret; |
| 90 | } |
| 91 | ret = worker_.Init(); |
| 92 | if (ret) { |
| 93 | pthread_mutex_destroy(&lock_); |
| 94 | ALOGE("Failed to initialize compositor worker %d\n", ret); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | initialized_ = true; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int DrmDisplayCompositor::QueueComposition( |
| 103 | std::unique_ptr<DrmDisplayComposition> composition) { |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 104 | switch (composition->type()) { |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 105 | case DRM_COMPOSITION_TYPE_FRAME: |
| 106 | if (!active_) |
| 107 | return -ENODEV; |
| 108 | break; |
| 109 | case DRM_COMPOSITION_TYPE_DPMS: |
| 110 | /* |
| 111 | * Update the state as soon as we get it so we can start/stop queuing |
| 112 | * frames asap. |
| 113 | */ |
| 114 | active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON); |
| 115 | break; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 116 | case DRM_COMPOSITION_TYPE_MODESET: |
| 117 | break; |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 118 | case DRM_COMPOSITION_TYPE_EMPTY: |
| 119 | return 0; |
| 120 | default: |
| 121 | ALOGE("Unknown composition type %d/%d", composition->type(), display_); |
| 122 | return -ENOENT; |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 123 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 124 | |
| 125 | int ret = pthread_mutex_lock(&lock_); |
| 126 | if (ret) { |
| 127 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 128 | return ret; |
| 129 | } |
| 130 | |
Zach Reizner | d410f04 | 2015-07-28 15:33:07 -0700 | [diff] [blame] | 131 | // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start |
| 132 | // to eat our buffer handles when we get about 1 second behind. |
| 133 | while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) { |
| 134 | pthread_mutex_unlock(&lock_); |
| 135 | sched_yield(); |
| 136 | pthread_mutex_lock(&lock_); |
| 137 | } |
| 138 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 139 | composite_queue_.push(std::move(composition)); |
| 140 | |
| 141 | ret = pthread_mutex_unlock(&lock_); |
| 142 | if (ret) { |
| 143 | ALOGE("Failed to release compositor lock %d", ret); |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | worker_.Signal(); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 151 | static bool drm_composition_layer_has_plane( |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 152 | const DrmCompositionLayer &comp_layer) { |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 153 | if (comp_layer.plane != NULL) |
| 154 | if (comp_layer.plane->type() == DRM_PLANE_TYPE_OVERLAY || |
| 155 | comp_layer.plane->type() == DRM_PLANE_TYPE_PRIMARY) |
| 156 | return true; |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | static bool drm_composition_layer_has_no_plane( |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 161 | const DrmCompositionLayer &comp_layer) { |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 162 | return comp_layer.plane == NULL; |
| 163 | } |
| 164 | |
| 165 | int DrmDisplayCompositor::ApplyPreComposite( |
| 166 | DrmDisplayComposition *display_comp) { |
| 167 | int ret = 0; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 168 | std::vector<DrmCompositionLayer> *layers = |
| 169 | display_comp->GetCompositionLayers(); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 170 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 171 | DrmConnector *connector = drm_->GetConnectorForDisplay(display_); |
| 172 | if (connector == NULL) { |
| 173 | ALOGE("Failed to determine display mode: no connector for display %d", |
| 174 | display_); |
| 175 | return -ENODEV; |
| 176 | } |
| 177 | |
| 178 | const DrmMode &mode = connector->active_mode(); |
| 179 | DrmFramebuffer &fb = framebuffers_[framebuffer_index_]; |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 180 | ret = fb.WaitReleased(fb.kReleaseWaitTimeoutMs); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 181 | if (ret) { |
| 182 | ALOGE("Failed to wait for framebuffer release %d", ret); |
| 183 | return ret; |
| 184 | } |
| 185 | fb.set_release_fence_fd(-1); |
| 186 | if (!fb.Allocate(mode.h_display(), mode.v_display())) { |
| 187 | ALOGE("Failed to allocate framebuffer with size %dx%d", mode.h_display(), |
| 188 | mode.v_display()); |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 192 | std::vector<DrmCompositionLayer> pre_comp_layers; |
Zach Reizner | b44fd10 | 2015-08-07 16:00:01 -0700 | [diff] [blame] | 193 | for (auto &comp_layer : *layers) { |
| 194 | if (comp_layer.plane == NULL) { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 195 | pre_comp_layers.emplace_back(std::move(comp_layer)); |
Zach Reizner | b44fd10 | 2015-08-07 16:00:01 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Zach Reizner | 8d63e7f | 2015-08-20 14:52:12 -0700 | [diff] [blame] | 199 | ret = pre_compositor_->Composite(pre_comp_layers.data(), |
| 200 | pre_comp_layers.size(), fb.buffer()); |
| 201 | pre_compositor_->Finish(); |
Zach Reizner | b44fd10 | 2015-08-07 16:00:01 -0700 | [diff] [blame] | 202 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 203 | if (ret) { |
| 204 | ALOGE("Failed to composite layers"); |
| 205 | return ret; |
| 206 | } |
| 207 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 208 | DrmCompositionLayer &pre_comp_layer = |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 209 | layers->at(display_comp->pre_composition_layer_index()); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 210 | ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle, |
| 211 | display_comp->importer()); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 212 | if (ret) { |
| 213 | ALOGE("Failed to import handle of layer %d", ret); |
| 214 | return ret; |
| 215 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 216 | pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, fb.buffer()->getWidth(), |
| 217 | fb.buffer()->getHeight()); |
| 218 | pre_comp_layer.display_frame = |
| 219 | DrmHwcRect<int>(0, 0, fb.buffer()->getWidth(), fb.buffer()->getHeight()); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 220 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 221 | // TODO(zachr) get a release fence |
| 222 | // fb.set_release_fence_fd(pre_comp_layer.release_fence.Release()); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 223 | framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; |
| 224 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 225 | display_comp->RemoveNoPlaneLayers(); |
| 226 | display_comp->SignalPreCompositionDone(); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 227 | return ret; |
| 228 | } |
| 229 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 230 | int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) { |
| 231 | int ret = 0; |
| 232 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 233 | if (display_comp->pre_composition_layer_index() >= 0) { |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 234 | ret = ApplyPreComposite(display_comp); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | } |
| 238 | |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 239 | DrmConnector *connector = drm_->GetConnectorForDisplay(display_); |
| 240 | if (!connector) { |
| 241 | ALOGE("Could not locate connector for display %d", display_); |
| 242 | return -ENODEV; |
| 243 | } |
| 244 | DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_); |
| 245 | if (!crtc) { |
| 246 | ALOGE("Could not locate crtc for display %d", display_); |
| 247 | return -ENODEV; |
| 248 | } |
| 249 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 250 | drmModePropertySetPtr pset = drmModePropertySetAlloc(); |
| 251 | if (!pset) { |
| 252 | ALOGE("Failed to allocate property set"); |
| 253 | return -ENOMEM; |
| 254 | } |
| 255 | |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 256 | uint32_t blob_id = 0; |
| 257 | uint64_t old_blob_id; |
| 258 | if (needs_modeset_) { |
| 259 | DrmProperty old_mode; |
| 260 | ret = drm_->GetCrtcProperty(*crtc, crtc->mode_property().name().c_str(), |
| 261 | &old_mode); |
| 262 | if (ret) { |
| 263 | ALOGE("Failed to get old mode property from crtc %d", crtc->id()); |
| 264 | drmModePropertySetFree(pset); |
| 265 | return ret; |
| 266 | } |
| 267 | ret = old_mode.value(&old_blob_id); |
| 268 | if (ret) { |
| 269 | ALOGE("Could not get old blob id value %d", ret); |
| 270 | drmModePropertySetFree(pset); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | struct drm_mode_modeinfo drm_mode; |
| 275 | memset(&drm_mode, 0, sizeof(drm_mode)); |
| 276 | next_mode_.ToDrmModeModeInfo(&drm_mode); |
| 277 | |
| 278 | ret = drm_->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo), |
| 279 | &blob_id); |
| 280 | if (ret) { |
| 281 | ALOGE("Failed to create mode property blob %d", ret); |
| 282 | drmModePropertySetFree(pset); |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(), |
| 287 | blob_id) || |
| 288 | drmModePropertySetAdd(pset, connector->id(), |
| 289 | connector->crtc_id_property().id(), crtc->id()); |
| 290 | if (ret) { |
| 291 | ALOGE("Failed to add blob %d to pset", blob_id); |
| 292 | drmModePropertySetFree(pset); |
| 293 | drm_->DestroyPropertyBlob(blob_id); |
| 294 | return ret; |
| 295 | } |
| 296 | } |
| 297 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 298 | std::vector<DrmCompositionLayer> *layers = |
| 299 | display_comp->GetCompositionLayers(); |
| 300 | for (DrmCompositionLayer &layer : *layers) { |
| 301 | int acquire_fence = layer.acquire_fence.get(); |
| 302 | if (acquire_fence >= 0) { |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 303 | ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 304 | if (ret) { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 305 | ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 306 | drmModePropertySetFree(pset); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 307 | drm_->DestroyPropertyBlob(blob_id); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 308 | return ret; |
| 309 | } |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 310 | layer.acquire_fence.Close(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 313 | DrmPlane *plane = layer.plane; |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 314 | |
| 315 | // Disable the plane if there's no crtc |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 316 | if (!layer.crtc) { |
Zach Reizner | 952f70a | 2015-07-17 14:10:03 -0700 | [diff] [blame] | 317 | ret = drmModePropertySetAdd(pset, plane->id(), |
| 318 | plane->crtc_property().id(), 0) || |
| 319 | drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), |
| 320 | 0); |
Sean Paul | 2e46fbd | 2015-07-09 17:22:22 -0400 | [diff] [blame] | 321 | if (ret) { |
| 322 | ALOGE("Failed to add plane %d disable to pset", plane->id()); |
| 323 | break; |
| 324 | } |
| 325 | continue; |
| 326 | } |
| 327 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 328 | if (!layer.buffer) { |
| 329 | ALOGE("Expected a valid framebuffer for pset"); |
| 330 | ret = -EINVAL; |
| 331 | break; |
| 332 | } |
| 333 | |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 334 | uint64_t rotation; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 335 | switch (layer.transform) { |
| 336 | case DrmHwcTransform::kFlipH: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 337 | rotation = 1 << DRM_REFLECT_X; |
| 338 | break; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 339 | case DrmHwcTransform::kFlipV: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 340 | rotation = 1 << DRM_REFLECT_Y; |
| 341 | break; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 342 | case DrmHwcTransform::kRotate90: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 343 | rotation = 1 << DRM_ROTATE_90; |
| 344 | break; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 345 | case DrmHwcTransform::kRotate180: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 346 | rotation = 1 << DRM_ROTATE_180; |
| 347 | break; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 348 | case DrmHwcTransform::kRotate270: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 349 | rotation = 1 << DRM_ROTATE_270; |
| 350 | break; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 351 | case DrmHwcTransform::kIdentity: |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 352 | rotation = 0; |
| 353 | break; |
| 354 | default: |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 355 | ALOGE("Invalid transform value 0x%x given", layer.transform); |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 356 | ret = -EINVAL; |
| 357 | break; |
| 358 | } |
Zach Reizner | 952f70a | 2015-07-17 14:10:03 -0700 | [diff] [blame] | 359 | if (ret) |
| 360 | break; |
| 361 | |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 362 | // TODO: Once we have atomic test, this should fall back to GL |
| 363 | if (rotation && plane->rotation_property().id() == 0) { |
| 364 | ALOGE("Rotation is not supported on plane %d", plane->id()); |
| 365 | ret = -EINVAL; |
| 366 | break; |
| 367 | } |
| 368 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 369 | ret = |
| 370 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(), |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 371 | layer.crtc->id()) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 372 | drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 373 | layer.buffer->fb_id) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 374 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 375 | layer.display_frame.left) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 376 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 377 | layer.display_frame.top) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 378 | drmModePropertySetAdd( |
| 379 | pset, plane->id(), plane->crtc_w_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 380 | layer.display_frame.right - layer.display_frame.left) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 381 | drmModePropertySetAdd( |
| 382 | pset, plane->id(), plane->crtc_h_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 383 | layer.display_frame.bottom - layer.display_frame.top) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 384 | drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 385 | (int)(layer.source_crop.left) << 16) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 386 | drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 387 | (int)(layer.source_crop.top) << 16) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 388 | drmModePropertySetAdd( |
| 389 | pset, plane->id(), plane->src_w_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 390 | (int)(layer.source_crop.right - layer.source_crop.left) << 16) || |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 391 | drmModePropertySetAdd( |
| 392 | pset, plane->id(), plane->src_h_property().id(), |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 393 | (int)(layer.source_crop.bottom - layer.source_crop.top) << 16); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 394 | if (ret) { |
| 395 | ALOGE("Failed to add plane %d to set", plane->id()); |
| 396 | break; |
| 397 | } |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 398 | |
| 399 | if (plane->rotation_property().id()) { |
Zach Reizner | 952f70a | 2015-07-17 14:10:03 -0700 | [diff] [blame] | 400 | ret = drmModePropertySetAdd(pset, plane->id(), |
| 401 | plane->rotation_property().id(), rotation); |
Sean Paul | 1c4c326 | 2015-07-14 15:51:52 -0400 | [diff] [blame] | 402 | if (ret) { |
| 403 | ALOGE("Failed to add rotation property %d to plane %d", |
| 404 | plane->rotation_property().id(), plane->id()); |
| 405 | break; |
| 406 | } |
| 407 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if (!ret) { |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 411 | ret = drmModePropertySetCommit(drm_->fd(), DRM_MODE_ATOMIC_ALLOW_MODESET, |
| 412 | drm_, pset); |
| 413 | if (ret) { |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 414 | ALOGE("Failed to commit pset ret=%d\n", ret); |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 415 | drmModePropertySetFree(pset); |
| 416 | if (needs_modeset_) |
| 417 | drm_->DestroyPropertyBlob(blob_id); |
| 418 | return ret; |
| 419 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 420 | } |
| 421 | if (pset) |
| 422 | drmModePropertySetFree(pset); |
| 423 | |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 424 | if (needs_modeset_) { |
| 425 | ret = drm_->DestroyPropertyBlob(old_blob_id); |
| 426 | if (ret) { |
| 427 | ALOGE("Failed to destroy old mode property blob %lld/%d", old_blob_id, |
| 428 | ret); |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | /* TODO: Add dpms to the pset when the kernel supports it */ |
| 433 | ret = ApplyDpms(display_comp); |
| 434 | if (ret) { |
| 435 | ALOGE("Failed to apply DPMS after modeset %d\n", ret); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | connector->set_active_mode(next_mode_); |
| 440 | needs_modeset_ = false; |
| 441 | } |
| 442 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 446 | int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) { |
| 447 | DrmConnector *conn = drm_->GetConnectorForDisplay(display_); |
| 448 | if (!conn) { |
| 449 | ALOGE("Failed to get DrmConnector for display %d", display_); |
| 450 | return -ENODEV; |
| 451 | } |
| 452 | |
| 453 | const DrmProperty &prop = conn->dpms_property(); |
| 454 | int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(), |
| 455 | display_comp->dpms_mode()); |
| 456 | if (ret) { |
| 457 | ALOGE("Failed to set DPMS property for connector %d", conn->id()); |
| 458 | return ret; |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 463 | int DrmDisplayCompositor::Composite() { |
| 464 | ATRACE_CALL(); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame] | 465 | |
| 466 | if (!pre_compositor_) { |
| 467 | pre_compositor_.reset(new GLWorkerCompositor()); |
| 468 | int ret = pre_compositor_->Init(); |
| 469 | if (ret) { |
| 470 | ALOGE("Failed to initialize OpenGL compositor %d", ret); |
| 471 | return ret; |
| 472 | } |
| 473 | } |
| 474 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 475 | int ret = pthread_mutex_lock(&lock_); |
| 476 | if (ret) { |
| 477 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 478 | return ret; |
| 479 | } |
| 480 | if (composite_queue_.empty()) { |
| 481 | ret = pthread_mutex_unlock(&lock_); |
| 482 | if (ret) |
| 483 | ALOGE("Failed to release compositor lock %d", ret); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | std::unique_ptr<DrmDisplayComposition> composition( |
| 488 | std::move(composite_queue_.front())); |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 489 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 490 | composite_queue_.pop(); |
| 491 | |
| 492 | ret = pthread_mutex_unlock(&lock_); |
| 493 | if (ret) { |
| 494 | ALOGE("Failed to release compositor lock %d", ret); |
| 495 | return ret; |
| 496 | } |
| 497 | |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 498 | switch (composition->type()) { |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 499 | case DRM_COMPOSITION_TYPE_FRAME: |
| 500 | ret = ApplyFrame(composition.get()); |
| 501 | if (ret) { |
| 502 | ALOGE("Composite failed for display %d", display_); |
| 503 | return ret; |
| 504 | } |
| 505 | ++dump_frames_composited_; |
| 506 | break; |
| 507 | case DRM_COMPOSITION_TYPE_DPMS: |
| 508 | ret = ApplyDpms(composition.get()); |
| 509 | if (ret) |
| 510 | ALOGE("Failed to apply dpms for display %d", display_); |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame] | 511 | return ret; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame^] | 512 | case DRM_COMPOSITION_TYPE_MODESET: |
| 513 | next_mode_ = composition->display_mode(); |
| 514 | needs_modeset_ = true; |
| 515 | return 0; |
Zach Reizner | b4a9aef | 2015-07-16 09:45:59 -0700 | [diff] [blame] | 516 | default: |
| 517 | ALOGE("Unknown composition type %d", composition->type()); |
| 518 | return -EINVAL; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 519 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 520 | |
| 521 | if (active_composition_) |
| 522 | active_composition_->FinishComposition(); |
| 523 | |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 524 | ret = pthread_mutex_lock(&lock_); |
| 525 | if (ret) |
| 526 | ALOGE("Failed to acquire lock for active_composition swap"); |
| 527 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 528 | active_composition_.swap(composition); |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 529 | |
| 530 | if (!ret) |
| 531 | ret = pthread_mutex_unlock(&lock_); |
Zach Reizner | 952f70a | 2015-07-17 14:10:03 -0700 | [diff] [blame] | 532 | if (ret) |
| 533 | ALOGE("Failed to release lock for active_composition swap"); |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 534 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | bool DrmDisplayCompositor::HaveQueuedComposites() const { |
| 539 | int ret = pthread_mutex_lock(&lock_); |
| 540 | if (ret) { |
| 541 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | bool empty_ret = !composite_queue_.empty(); |
| 546 | |
| 547 | ret = pthread_mutex_unlock(&lock_); |
| 548 | if (ret) { |
| 549 | ALOGE("Failed to release compositor lock %d", ret); |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | return empty_ret; |
| 554 | } |
| 555 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 556 | struct DrmDumpLayer { |
| 557 | int plane_id; |
| 558 | int crtc_id; |
| 559 | DrmHwcTransform transform; |
| 560 | DrmHwcRect<float> source_crop; |
| 561 | DrmHwcRect<int> display_frame; |
| 562 | |
| 563 | DrmDumpLayer(DrmCompositionLayer &rhs) |
| 564 | : plane_id(rhs.plane->id()), |
| 565 | crtc_id(rhs.crtc ? rhs.crtc->id() : -1), |
| 566 | transform(rhs.transform), |
| 567 | source_crop(rhs.source_crop), |
| 568 | display_frame(rhs.display_frame) { |
| 569 | } |
| 570 | }; |
| 571 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 572 | void DrmDisplayCompositor::Dump(std::ostringstream *out) const { |
| 573 | uint64_t cur_ts; |
| 574 | |
| 575 | int ret = pthread_mutex_lock(&lock_); |
| 576 | if (ret) |
| 577 | return; |
| 578 | |
| 579 | uint64_t num_frames = dump_frames_composited_; |
| 580 | dump_frames_composited_ = 0; |
| 581 | |
| 582 | struct timespec ts; |
| 583 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 584 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 585 | std::vector<DrmCompositionLayer> *input_layers = |
| 586 | active_composition_->GetCompositionLayers(); |
| 587 | std::vector<DrmDumpLayer> layers; |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 588 | if (active_composition_) |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 589 | layers.insert(layers.begin(), input_layers->begin(), input_layers->end()); |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 590 | else |
| 591 | ret = -EAGAIN; |
| 592 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 593 | ret |= pthread_mutex_unlock(&lock_); |
| 594 | if (ret) |
| 595 | return; |
| 596 | |
| 597 | cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 598 | uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000); |
Stéphane Marchesin | 8395992 | 2015-07-13 12:35:41 -0700 | [diff] [blame] | 599 | float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 600 | |
| 601 | *out << "--DrmDisplayCompositor[" << display_ |
| 602 | << "]: num_frames=" << num_frames << " num_ms=" << num_ms |
| 603 | << " fps=" << fps << "\n"; |
| 604 | |
| 605 | dump_last_timestamp_ns_ = cur_ts; |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 606 | |
| 607 | *out << "---- DrmDisplayCompositor Layers: num=" << layers.size() << "\n"; |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 608 | for (std::vector<DrmDumpLayer>::iterator iter = layers.begin(); |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 609 | iter != layers.end(); ++iter) { |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 610 | *out << "------ DrmDisplayCompositor Layer: plane=" << iter->plane_id |
| 611 | << " "; |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 612 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 613 | if (iter->crtc_id < 0) { |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 614 | *out << "disabled\n"; |
| 615 | continue; |
| 616 | } |
| 617 | |
Zach Reizner | 4a25365 | 2015-09-10 18:30:54 -0700 | [diff] [blame] | 618 | *out << "crtc=" << iter->crtc_id |
| 619 | << " crtc[x/y/w/h]=" << iter->display_frame.left << "/" |
| 620 | << iter->display_frame.top << "/" |
| 621 | << iter->display_frame.right - iter->display_frame.left << "/" |
| 622 | << iter->display_frame.bottom - iter->display_frame.top << " " |
| 623 | << " src[x/y/w/h]=" << iter->source_crop.left << "/" |
| 624 | << iter->source_crop.top << "/" |
| 625 | << iter->source_crop.right - iter->source_crop.left << "/" |
| 626 | << iter->source_crop.bottom - iter->source_crop.top |
| 627 | << " transform=" << (uint32_t)iter->transform << "\n"; |
Sean Paul | fd37dfe | 2015-07-13 12:41:37 -0400 | [diff] [blame] | 628 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 629 | } |
| 630 | } |