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()
44 dump_frames_composited_(0),
45 dump_last_timestamp_ns_(0) {
47 if (clock_gettime(CLOCK_MONOTONIC, &ts))
49 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
52 DrmDisplayCompositor::~DrmDisplayCompositor() {
58 int ret = pthread_mutex_lock(&lock_);
60 ALOGE("Failed to acquire compositor lock %d", ret);
62 while (!composite_queue_.empty()) {
63 composite_queue_.front().reset();
64 composite_queue_.pop();
66 active_composition_.reset();
68 ret = pthread_mutex_unlock(&lock_);
70 ALOGE("Failed to acquire compositor lock %d", ret);
72 pthread_mutex_destroy(&lock_);
75 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
79 int ret = pthread_mutex_init(&lock_, NULL);
81 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
86 pthread_mutex_destroy(&lock_);
87 ALOGE("Failed to initialize compositor worker %d\n", ret);
95 int DrmDisplayCompositor::QueueComposition(
96 std::unique_ptr<DrmDisplayComposition> composition) {
97 switch (composition->type()) {
98 case DRM_COMPOSITION_TYPE_FRAME:
102 case DRM_COMPOSITION_TYPE_DPMS:
104 * Update the state as soon as we get it so we can start/stop queuing
107 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
109 case DRM_COMPOSITION_TYPE_EMPTY:
112 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
116 int ret = pthread_mutex_lock(&lock_);
118 ALOGE("Failed to acquire compositor lock %d", ret);
122 composite_queue_.push(std::move(composition));
124 ret = pthread_mutex_unlock(&lock_);
126 ALOGE("Failed to release compositor lock %d", ret);
134 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
137 drmModePropertySetPtr pset = drmModePropertySetAlloc();
139 ALOGE("Failed to allocate property set");
143 DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers();
144 for (DrmCompositionLayerVector_t::iterator iter = layers->begin();
145 iter != layers->end(); ++iter) {
146 hwc_layer_1_t *layer = &iter->layer;
148 if (layer->acquireFenceFd >= 0) {
149 ret = sync_wait(layer->acquireFenceFd, -1);
151 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
152 drmModePropertySetFree(pset);
155 close(layer->acquireFenceFd);
156 layer->acquireFenceFd = -1;
159 DrmPlane *plane = iter->plane;
161 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
163 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
165 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
166 layer->displayFrame.left) ||
167 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
168 layer->displayFrame.top) ||
169 drmModePropertySetAdd(
170 pset, plane->id(), plane->crtc_w_property().id(),
171 layer->displayFrame.right - layer->displayFrame.left) ||
172 drmModePropertySetAdd(
173 pset, plane->id(), plane->crtc_h_property().id(),
174 layer->displayFrame.bottom - layer->displayFrame.top) ||
175 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
176 layer->sourceCropf.left) ||
177 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
178 layer->sourceCropf.top) ||
179 drmModePropertySetAdd(
180 pset, plane->id(), plane->src_w_property().id(),
181 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
182 drmModePropertySetAdd(
183 pset, plane->id(), plane->src_h_property().id(),
184 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
186 ALOGE("Failed to add plane %d to set", plane->id());
192 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
194 ALOGE("Failed to commit pset ret=%d\n", ret);
197 drmModePropertySetFree(pset);
202 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
203 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
205 ALOGE("Failed to get DrmConnector for display %d", display_);
209 const DrmProperty &prop = conn->dpms_property();
210 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
211 display_comp->dpms_mode());
213 ALOGE("Failed to set DPMS property for connector %d", conn->id());
219 int DrmDisplayCompositor::Composite() {
221 int ret = pthread_mutex_lock(&lock_);
223 ALOGE("Failed to acquire compositor lock %d", ret);
226 if (composite_queue_.empty()) {
227 ret = pthread_mutex_unlock(&lock_);
229 ALOGE("Failed to release compositor lock %d", ret);
233 std::unique_ptr<DrmDisplayComposition> composition(
234 std::move(composite_queue_.front()));
235 composite_queue_.pop();
237 ret = pthread_mutex_unlock(&lock_);
239 ALOGE("Failed to release compositor lock %d", ret);
243 switch (composition->type()) {
244 case DRM_COMPOSITION_TYPE_FRAME:
245 ret = ApplyFrame(composition.get());
247 ALOGE("Composite failed for display %d", display_);
250 ++dump_frames_composited_;
252 case DRM_COMPOSITION_TYPE_DPMS:
253 ret = ApplyDpms(composition.get());
255 ALOGE("Failed to apply dpms for display %d", display_);
258 ALOGE("Unknown composition type %d", composition->type());
262 if (active_composition_)
263 active_composition_->FinishComposition();
265 active_composition_.swap(composition);
269 bool DrmDisplayCompositor::HaveQueuedComposites() const {
270 int ret = pthread_mutex_lock(&lock_);
272 ALOGE("Failed to acquire compositor lock %d", ret);
276 bool empty_ret = !composite_queue_.empty();
278 ret = pthread_mutex_unlock(&lock_);
280 ALOGE("Failed to release compositor lock %d", ret);
287 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
290 int ret = pthread_mutex_lock(&lock_);
294 uint64_t num_frames = dump_frames_composited_;
295 dump_frames_composited_ = 0;
298 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
300 ret |= pthread_mutex_unlock(&lock_);
304 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
305 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
306 unsigned fps = num_ms ? (num_frames * 1000) / (num_ms) : 0;
308 *out << "--DrmDisplayCompositor[" << display_
309 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
310 << " fps=" << fps << "\n";
312 dump_last_timestamp_ns_ = cur_ts;