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;
160 DrmCrtc *crtc = iter->crtc;
162 // Disable the plane if there's no crtc
165 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
167 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
170 ALOGE("Failed to add plane %d disable to pset", plane->id());
177 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
179 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
181 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
182 layer->displayFrame.left) ||
183 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
184 layer->displayFrame.top) ||
185 drmModePropertySetAdd(
186 pset, plane->id(), plane->crtc_w_property().id(),
187 layer->displayFrame.right - layer->displayFrame.left) ||
188 drmModePropertySetAdd(
189 pset, plane->id(), plane->crtc_h_property().id(),
190 layer->displayFrame.bottom - layer->displayFrame.top) ||
191 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
192 (int)(layer->sourceCropf.left) << 16) ||
193 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
194 (int)(layer->sourceCropf.top) << 16) ||
195 drmModePropertySetAdd(
196 pset, plane->id(), plane->src_w_property().id(),
197 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
198 drmModePropertySetAdd(
199 pset, plane->id(), plane->src_h_property().id(),
200 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
202 ALOGE("Failed to add plane %d to set", plane->id());
208 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
210 ALOGE("Failed to commit pset ret=%d\n", ret);
213 drmModePropertySetFree(pset);
218 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
219 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
221 ALOGE("Failed to get DrmConnector for display %d", display_);
225 const DrmProperty &prop = conn->dpms_property();
226 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
227 display_comp->dpms_mode());
229 ALOGE("Failed to set DPMS property for connector %d", conn->id());
235 int DrmDisplayCompositor::Composite() {
237 int ret = pthread_mutex_lock(&lock_);
239 ALOGE("Failed to acquire compositor lock %d", ret);
242 if (composite_queue_.empty()) {
243 ret = pthread_mutex_unlock(&lock_);
245 ALOGE("Failed to release compositor lock %d", ret);
249 std::unique_ptr<DrmDisplayComposition> composition(
250 std::move(composite_queue_.front()));
251 composite_queue_.pop();
253 ret = pthread_mutex_unlock(&lock_);
255 ALOGE("Failed to release compositor lock %d", ret);
259 switch (composition->type()) {
260 case DRM_COMPOSITION_TYPE_FRAME:
261 ret = ApplyFrame(composition.get());
263 ALOGE("Composite failed for display %d", display_);
266 ++dump_frames_composited_;
268 case DRM_COMPOSITION_TYPE_DPMS:
269 ret = ApplyDpms(composition.get());
271 ALOGE("Failed to apply dpms for display %d", display_);
274 ALOGE("Unknown composition type %d", composition->type());
278 if (active_composition_)
279 active_composition_->FinishComposition();
281 ret = pthread_mutex_lock(&lock_);
283 ALOGE("Failed to acquire lock for active_composition swap");
285 active_composition_.swap(composition);
288 ret = pthread_mutex_unlock(&lock_);
290 ALOGE("Failed to release lock for active_composition swap");
295 bool DrmDisplayCompositor::HaveQueuedComposites() const {
296 int ret = pthread_mutex_lock(&lock_);
298 ALOGE("Failed to acquire compositor lock %d", ret);
302 bool empty_ret = !composite_queue_.empty();
304 ret = pthread_mutex_unlock(&lock_);
306 ALOGE("Failed to release compositor lock %d", ret);
313 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
316 int ret = pthread_mutex_lock(&lock_);
320 uint64_t num_frames = dump_frames_composited_;
321 dump_frames_composited_ = 0;
324 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
326 DrmCompositionLayerVector_t layers;
327 if (active_composition_)
328 layers = *active_composition_->GetCompositionLayers();
332 ret |= pthread_mutex_unlock(&lock_);
336 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
337 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
338 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
340 *out << "--DrmDisplayCompositor[" << display_
341 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
342 << " fps=" << fps << "\n";
344 dump_last_timestamp_ns_ = cur_ts;
346 *out << "---- DrmDisplayCompositor Layers: num=" << layers.size() << "\n";
347 for (DrmCompositionLayerVector_t::iterator iter = layers.begin();
348 iter != layers.end(); ++iter) {
349 hwc_layer_1_t *layer = &iter->layer;
350 DrmPlane *plane = iter->plane;
352 *out << "------ DrmDisplayCompositor Layer: plane=" << plane->id() << " ";
354 DrmCrtc *crtc = iter->crtc;
356 *out << "disabled\n";
360 *out << "crtc=" << crtc->id() << " crtc[x/y/w/h]=" <<
361 layer->displayFrame.left << "/" << layer->displayFrame.top << "/" <<
362 layer->displayFrame.right - layer->displayFrame.left << "/" <<
363 layer->displayFrame.bottom - layer->displayFrame.top << " " <<
364 " src[x/y/w/h]=" << layer->sourceCropf.left << "/" <<
365 layer->sourceCropf.top << "/" <<
366 layer->sourceCropf.right - layer->sourceCropf.left << "/" <<
367 layer->sourceCropf.bottom - layer->sourceCropf.top << "\n";