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" |
| 24 | |
| 25 | #include <pthread.h> |
| 26 | #include <sstream> |
| 27 | #include <stdlib.h> |
| 28 | #include <time.h> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | #include <sync/sync.h> |
| 33 | #include <utils/Trace.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | DrmDisplayCompositor::DrmDisplayCompositor() |
| 38 | : drm_(NULL), |
| 39 | display_(-1), |
| 40 | worker_(this), |
| 41 | frame_no_(0), |
| 42 | initialized_(false), |
| 43 | dump_frames_composited_(0), |
| 44 | dump_last_timestamp_ns_(0) { |
| 45 | struct timespec ts; |
| 46 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
| 47 | return; |
| 48 | dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 49 | } |
| 50 | |
| 51 | DrmDisplayCompositor::~DrmDisplayCompositor() { |
| 52 | if (!initialized_) |
| 53 | return; |
| 54 | |
| 55 | worker_.Exit(); |
| 56 | |
| 57 | int ret = pthread_mutex_lock(&lock_); |
| 58 | if (ret) |
| 59 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 60 | |
| 61 | while (!composite_queue_.empty()) { |
| 62 | composite_queue_.front().reset(); |
| 63 | composite_queue_.pop(); |
| 64 | } |
| 65 | active_composition_.reset(); |
| 66 | |
| 67 | ret = pthread_mutex_unlock(&lock_); |
| 68 | if (ret) |
| 69 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 70 | |
| 71 | pthread_mutex_destroy(&lock_); |
| 72 | } |
| 73 | |
| 74 | int DrmDisplayCompositor::Init(DrmResources *drm, int display) { |
| 75 | drm_ = drm; |
| 76 | display_ = display; |
| 77 | |
| 78 | int ret = pthread_mutex_init(&lock_, NULL); |
| 79 | if (ret) { |
| 80 | ALOGE("Failed to initialize drm compositor lock %d\n", ret); |
| 81 | return ret; |
| 82 | } |
| 83 | ret = worker_.Init(); |
| 84 | if (ret) { |
| 85 | pthread_mutex_destroy(&lock_); |
| 86 | ALOGE("Failed to initialize compositor worker %d\n", ret); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | initialized_ = true; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int DrmDisplayCompositor::QueueComposition( |
| 95 | std::unique_ptr<DrmDisplayComposition> composition) { |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame^] | 96 | switch (composition->type()) { |
| 97 | case DRM_COMPOSITION_TYPE_FRAME: |
| 98 | break; |
| 99 | case DRM_COMPOSITION_TYPE_EMPTY: |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 100 | return 0; |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame^] | 101 | default: |
| 102 | ALOGE("Unknown composition type %d/%d", composition->type(), display_); |
| 103 | return -ENOENT; |
| 104 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 105 | |
| 106 | int ret = pthread_mutex_lock(&lock_); |
| 107 | if (ret) { |
| 108 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | composite_queue_.push(std::move(composition)); |
| 113 | |
| 114 | ret = pthread_mutex_unlock(&lock_); |
| 115 | if (ret) { |
| 116 | ALOGE("Failed to release compositor lock %d", ret); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | worker_.Signal(); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) { |
| 125 | int ret = 0; |
| 126 | |
| 127 | drmModePropertySetPtr pset = drmModePropertySetAlloc(); |
| 128 | if (!pset) { |
| 129 | ALOGE("Failed to allocate property set"); |
| 130 | return -ENOMEM; |
| 131 | } |
| 132 | |
| 133 | DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers(); |
| 134 | for (DrmCompositionLayerVector_t::iterator iter = layers->begin(); |
| 135 | iter != layers->end(); ++iter) { |
| 136 | hwc_layer_1_t *layer = &iter->layer; |
| 137 | |
| 138 | if (layer->acquireFenceFd >= 0) { |
| 139 | ret = sync_wait(layer->acquireFenceFd, -1); |
| 140 | if (ret) { |
| 141 | ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret); |
| 142 | drmModePropertySetFree(pset); |
| 143 | return ret; |
| 144 | } |
| 145 | close(layer->acquireFenceFd); |
| 146 | layer->acquireFenceFd = -1; |
| 147 | } |
| 148 | |
| 149 | DrmPlane *plane = iter->plane; |
| 150 | ret = |
| 151 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(), |
| 152 | iter->crtc->id()) || |
| 153 | drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), |
| 154 | iter->bo.fb_id) || |
| 155 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(), |
| 156 | layer->displayFrame.left) || |
| 157 | drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(), |
| 158 | layer->displayFrame.top) || |
| 159 | drmModePropertySetAdd( |
| 160 | pset, plane->id(), plane->crtc_w_property().id(), |
| 161 | layer->displayFrame.right - layer->displayFrame.left) || |
| 162 | drmModePropertySetAdd( |
| 163 | pset, plane->id(), plane->crtc_h_property().id(), |
| 164 | layer->displayFrame.bottom - layer->displayFrame.top) || |
| 165 | drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(), |
| 166 | layer->sourceCropf.left) || |
| 167 | drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(), |
| 168 | layer->sourceCropf.top) || |
| 169 | drmModePropertySetAdd( |
| 170 | pset, plane->id(), plane->src_w_property().id(), |
| 171 | (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) || |
| 172 | drmModePropertySetAdd( |
| 173 | pset, plane->id(), plane->src_h_property().id(), |
| 174 | (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16); |
| 175 | if (ret) { |
| 176 | ALOGE("Failed to add plane %d to set", plane->id()); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (!ret) { |
| 182 | ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset); |
| 183 | if (ret) |
| 184 | ALOGE("Failed to commit pset ret=%d\n", ret); |
| 185 | } |
| 186 | if (pset) |
| 187 | drmModePropertySetFree(pset); |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | int DrmDisplayCompositor::Composite() { |
| 193 | ATRACE_CALL(); |
| 194 | int ret = pthread_mutex_lock(&lock_); |
| 195 | if (ret) { |
| 196 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 197 | return ret; |
| 198 | } |
| 199 | if (composite_queue_.empty()) { |
| 200 | ret = pthread_mutex_unlock(&lock_); |
| 201 | if (ret) |
| 202 | ALOGE("Failed to release compositor lock %d", ret); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | std::unique_ptr<DrmDisplayComposition> composition( |
| 207 | std::move(composite_queue_.front())); |
| 208 | composite_queue_.pop(); |
| 209 | |
| 210 | ret = pthread_mutex_unlock(&lock_); |
| 211 | if (ret) { |
| 212 | ALOGE("Failed to release compositor lock %d", ret); |
| 213 | return ret; |
| 214 | } |
| 215 | |
Sean Paul | acb2a44 | 2015-06-24 18:43:01 -0700 | [diff] [blame^] | 216 | switch (composition->type()) { |
| 217 | case DRM_COMPOSITION_TYPE_FRAME: |
| 218 | ret = ApplyFrame(composition.get()); |
| 219 | if (ret) { |
| 220 | ALOGE("Composite failed for display %d", display_); |
| 221 | return ret; |
| 222 | } |
| 223 | ++dump_frames_composited_; |
| 224 | break; |
| 225 | default: |
| 226 | ALOGE("Unknown composition type %d", composition->type()); |
| 227 | return -EINVAL; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 228 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 229 | |
| 230 | if (active_composition_) |
| 231 | active_composition_->FinishComposition(); |
| 232 | |
| 233 | active_composition_.swap(composition); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | bool DrmDisplayCompositor::HaveQueuedComposites() const { |
| 238 | int ret = pthread_mutex_lock(&lock_); |
| 239 | if (ret) { |
| 240 | ALOGE("Failed to acquire compositor lock %d", ret); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | bool empty_ret = !composite_queue_.empty(); |
| 245 | |
| 246 | ret = pthread_mutex_unlock(&lock_); |
| 247 | if (ret) { |
| 248 | ALOGE("Failed to release compositor lock %d", ret); |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | return empty_ret; |
| 253 | } |
| 254 | |
| 255 | void DrmDisplayCompositor::Dump(std::ostringstream *out) const { |
| 256 | uint64_t cur_ts; |
| 257 | |
| 258 | int ret = pthread_mutex_lock(&lock_); |
| 259 | if (ret) |
| 260 | return; |
| 261 | |
| 262 | uint64_t num_frames = dump_frames_composited_; |
| 263 | dump_frames_composited_ = 0; |
| 264 | |
| 265 | struct timespec ts; |
| 266 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 267 | |
| 268 | ret |= pthread_mutex_unlock(&lock_); |
| 269 | if (ret) |
| 270 | return; |
| 271 | |
| 272 | cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec; |
| 273 | uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000); |
| 274 | unsigned fps = num_ms ? (num_frames * 1000) / (num_ms) : 0; |
| 275 | |
| 276 | *out << "--DrmDisplayCompositor[" << display_ |
| 277 | << "]: num_frames=" << num_frames << " num_ms=" << num_ms |
| 278 | << " fps=" << fps << "\n"; |
| 279 | |
| 280 | dump_last_timestamp_ns_ = cur_ts; |
| 281 | } |
| 282 | } |