2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwc-drm-display-compositor"
20 #include "drmdisplaycompositor.h"
23 #include "drmresources.h"
31 #include <cutils/log.h>
32 #include <sync/sync.h>
33 #include <utils/Trace.h>
37 DrmDisplayCompositor::DrmDisplayCompositor()
43 dump_frames_composited_(0),
44 dump_last_timestamp_ns_(0) {
46 if (clock_gettime(CLOCK_MONOTONIC, &ts))
48 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
51 DrmDisplayCompositor::~DrmDisplayCompositor() {
57 int ret = pthread_mutex_lock(&lock_);
59 ALOGE("Failed to acquire compositor lock %d", ret);
61 while (!composite_queue_.empty()) {
62 composite_queue_.front().reset();
63 composite_queue_.pop();
65 active_composition_.reset();
67 ret = pthread_mutex_unlock(&lock_);
69 ALOGE("Failed to acquire compositor lock %d", ret);
71 pthread_mutex_destroy(&lock_);
74 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
78 int ret = pthread_mutex_init(&lock_, NULL);
80 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
85 pthread_mutex_destroy(&lock_);
86 ALOGE("Failed to initialize compositor worker %d\n", ret);
94 int DrmDisplayCompositor::QueueComposition(
95 std::unique_ptr<DrmDisplayComposition> composition) {
96 if (composition->GetCompositionLayers()->empty())
99 int ret = pthread_mutex_lock(&lock_);
101 ALOGE("Failed to acquire compositor lock %d", ret);
105 composite_queue_.push(std::move(composition));
107 ret = pthread_mutex_unlock(&lock_);
109 ALOGE("Failed to release compositor lock %d", ret);
117 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
120 drmModePropertySetPtr pset = drmModePropertySetAlloc();
122 ALOGE("Failed to allocate property set");
126 DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers();
127 for (DrmCompositionLayerVector_t::iterator iter = layers->begin();
128 iter != layers->end(); ++iter) {
129 hwc_layer_1_t *layer = &iter->layer;
131 if (layer->acquireFenceFd >= 0) {
132 ret = sync_wait(layer->acquireFenceFd, -1);
134 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
135 drmModePropertySetFree(pset);
138 close(layer->acquireFenceFd);
139 layer->acquireFenceFd = -1;
142 DrmPlane *plane = iter->plane;
144 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
146 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
148 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
149 layer->displayFrame.left) ||
150 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
151 layer->displayFrame.top) ||
152 drmModePropertySetAdd(
153 pset, plane->id(), plane->crtc_w_property().id(),
154 layer->displayFrame.right - layer->displayFrame.left) ||
155 drmModePropertySetAdd(
156 pset, plane->id(), plane->crtc_h_property().id(),
157 layer->displayFrame.bottom - layer->displayFrame.top) ||
158 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
159 layer->sourceCropf.left) ||
160 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
161 layer->sourceCropf.top) ||
162 drmModePropertySetAdd(
163 pset, plane->id(), plane->src_w_property().id(),
164 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
165 drmModePropertySetAdd(
166 pset, plane->id(), plane->src_h_property().id(),
167 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
169 ALOGE("Failed to add plane %d to set", plane->id());
175 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
177 ALOGE("Failed to commit pset ret=%d\n", ret);
180 drmModePropertySetFree(pset);
185 int DrmDisplayCompositor::Composite() {
187 int ret = pthread_mutex_lock(&lock_);
189 ALOGE("Failed to acquire compositor lock %d", ret);
192 if (composite_queue_.empty()) {
193 ret = pthread_mutex_unlock(&lock_);
195 ALOGE("Failed to release compositor lock %d", ret);
199 std::unique_ptr<DrmDisplayComposition> composition(
200 std::move(composite_queue_.front()));
201 composite_queue_.pop();
203 ret = pthread_mutex_unlock(&lock_);
205 ALOGE("Failed to release compositor lock %d", ret);
209 ret = ApplyFrame(composition.get());
211 ALOGE("Composite failed for display %d", display_);
214 ++dump_frames_composited_;
216 if (active_composition_)
217 active_composition_->FinishComposition();
219 active_composition_.swap(composition);
223 bool DrmDisplayCompositor::HaveQueuedComposites() const {
224 int ret = pthread_mutex_lock(&lock_);
226 ALOGE("Failed to acquire compositor lock %d", ret);
230 bool empty_ret = !composite_queue_.empty();
232 ret = pthread_mutex_unlock(&lock_);
234 ALOGE("Failed to release compositor lock %d", ret);
241 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
244 int ret = pthread_mutex_lock(&lock_);
248 uint64_t num_frames = dump_frames_composited_;
249 dump_frames_composited_ = 0;
252 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
254 ret |= pthread_mutex_unlock(&lock_);
258 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
259 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
260 unsigned fps = num_ms ? (num_frames * 1000) / (num_ms) : 0;
262 *out << "--DrmDisplayCompositor[" << display_
263 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
264 << " fps=" << fps << "\n";
266 dump_last_timestamp_ns_ = cur_ts;