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 switch (composition->type()) {
97 case DRM_COMPOSITION_TYPE_FRAME:
99 case DRM_COMPOSITION_TYPE_EMPTY:
102 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
106 int ret = pthread_mutex_lock(&lock_);
108 ALOGE("Failed to acquire compositor lock %d", ret);
112 composite_queue_.push(std::move(composition));
114 ret = pthread_mutex_unlock(&lock_);
116 ALOGE("Failed to release compositor lock %d", ret);
124 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
127 drmModePropertySetPtr pset = drmModePropertySetAlloc();
129 ALOGE("Failed to allocate property set");
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;
138 if (layer->acquireFenceFd >= 0) {
139 ret = sync_wait(layer->acquireFenceFd, -1);
141 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
142 drmModePropertySetFree(pset);
145 close(layer->acquireFenceFd);
146 layer->acquireFenceFd = -1;
149 DrmPlane *plane = iter->plane;
151 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
153 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().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);
176 ALOGE("Failed to add plane %d to set", plane->id());
182 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
184 ALOGE("Failed to commit pset ret=%d\n", ret);
187 drmModePropertySetFree(pset);
192 int DrmDisplayCompositor::Composite() {
194 int ret = pthread_mutex_lock(&lock_);
196 ALOGE("Failed to acquire compositor lock %d", ret);
199 if (composite_queue_.empty()) {
200 ret = pthread_mutex_unlock(&lock_);
202 ALOGE("Failed to release compositor lock %d", ret);
206 std::unique_ptr<DrmDisplayComposition> composition(
207 std::move(composite_queue_.front()));
208 composite_queue_.pop();
210 ret = pthread_mutex_unlock(&lock_);
212 ALOGE("Failed to release compositor lock %d", ret);
216 switch (composition->type()) {
217 case DRM_COMPOSITION_TYPE_FRAME:
218 ret = ApplyFrame(composition.get());
220 ALOGE("Composite failed for display %d", display_);
223 ++dump_frames_composited_;
226 ALOGE("Unknown composition type %d", composition->type());
230 if (active_composition_)
231 active_composition_->FinishComposition();
233 active_composition_.swap(composition);
237 bool DrmDisplayCompositor::HaveQueuedComposites() const {
238 int ret = pthread_mutex_lock(&lock_);
240 ALOGE("Failed to acquire compositor lock %d", ret);
244 bool empty_ret = !composite_queue_.empty();
246 ret = pthread_mutex_unlock(&lock_);
248 ALOGE("Failed to release compositor lock %d", ret);
255 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
258 int ret = pthread_mutex_lock(&lock_);
262 uint64_t num_frames = dump_frames_composited_;
263 dump_frames_composited_ = 0;
266 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
268 ret |= pthread_mutex_unlock(&lock_);
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;
276 *out << "--DrmDisplayCompositor[" << display_
277 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
278 << " fps=" << fps << "\n";
280 dump_last_timestamp_ns_ = cur_ts;