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 <drm/drm_mode.h>
32 #include <cutils/log.h>
33 #include <sync/sync.h>
34 #include <utils/Trace.h>
38 DrmDisplayCompositor::DrmDisplayCompositor()
45 dump_frames_composited_(0),
46 dump_last_timestamp_ns_(0) {
48 if (clock_gettime(CLOCK_MONOTONIC, &ts))
50 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
53 DrmDisplayCompositor::~DrmDisplayCompositor() {
59 int ret = pthread_mutex_lock(&lock_);
61 ALOGE("Failed to acquire compositor lock %d", ret);
63 while (!composite_queue_.empty()) {
64 composite_queue_.front().reset();
65 composite_queue_.pop();
67 active_composition_.reset();
69 ret = pthread_mutex_unlock(&lock_);
71 ALOGE("Failed to acquire compositor lock %d", ret);
73 pthread_mutex_destroy(&lock_);
76 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
80 int ret = pthread_mutex_init(&lock_, NULL);
82 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
87 pthread_mutex_destroy(&lock_);
88 ALOGE("Failed to initialize compositor worker %d\n", ret);
96 int DrmDisplayCompositor::QueueComposition(
97 std::unique_ptr<DrmDisplayComposition> composition) {
98 switch (composition->type()) {
99 case DRM_COMPOSITION_TYPE_FRAME:
103 case DRM_COMPOSITION_TYPE_DPMS:
105 * Update the state as soon as we get it so we can start/stop queuing
108 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
110 case DRM_COMPOSITION_TYPE_EMPTY:
113 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
117 int ret = pthread_mutex_lock(&lock_);
119 ALOGE("Failed to acquire compositor lock %d", ret);
123 composite_queue_.push(std::move(composition));
125 ret = pthread_mutex_unlock(&lock_);
127 ALOGE("Failed to release compositor lock %d", ret);
135 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
138 drmModePropertySetPtr pset = drmModePropertySetAlloc();
140 ALOGE("Failed to allocate property set");
144 DrmCompositionLayerVector_t *layers = display_comp->GetCompositionLayers();
145 for (DrmCompositionLayerVector_t::iterator iter = layers->begin();
146 iter != layers->end(); ++iter) {
147 hwc_layer_1_t *layer = &iter->layer;
149 if (layer->acquireFenceFd >= 0) {
150 ret = sync_wait(layer->acquireFenceFd, -1);
152 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
153 drmModePropertySetFree(pset);
156 close(layer->acquireFenceFd);
157 layer->acquireFenceFd = -1;
160 DrmPlane *plane = iter->plane;
161 DrmCrtc *crtc = iter->crtc;
163 // Disable the plane if there's no crtc
165 ret = drmModePropertySetAdd(pset, plane->id(),
166 plane->crtc_property().id(), 0) ||
167 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
170 ALOGE("Failed to add plane %d disable to pset", plane->id());
177 switch (layer->transform) {
178 case HWC_TRANSFORM_FLIP_H:
179 rotation = 1 << DRM_REFLECT_X;
181 case HWC_TRANSFORM_FLIP_V:
182 rotation = 1 << DRM_REFLECT_Y;
184 case HWC_TRANSFORM_ROT_90:
185 rotation = 1 << DRM_ROTATE_90;
187 case HWC_TRANSFORM_ROT_180:
188 rotation = 1 << DRM_ROTATE_180;
190 case HWC_TRANSFORM_ROT_270:
191 rotation = 1 << DRM_ROTATE_270;
197 ALOGE("Invalid transform value 0x%x given", layer->transform);
204 // TODO: Once we have atomic test, this should fall back to GL
205 if (rotation && plane->rotation_property().id() == 0) {
206 ALOGE("Rotation is not supported on plane %d", plane->id());
212 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
214 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
216 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
217 layer->displayFrame.left) ||
218 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
219 layer->displayFrame.top) ||
220 drmModePropertySetAdd(
221 pset, plane->id(), plane->crtc_w_property().id(),
222 layer->displayFrame.right - layer->displayFrame.left) ||
223 drmModePropertySetAdd(
224 pset, plane->id(), plane->crtc_h_property().id(),
225 layer->displayFrame.bottom - layer->displayFrame.top) ||
226 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
227 (int)(layer->sourceCropf.left) << 16) ||
228 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
229 (int)(layer->sourceCropf.top) << 16) ||
230 drmModePropertySetAdd(
231 pset, plane->id(), plane->src_w_property().id(),
232 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
233 drmModePropertySetAdd(
234 pset, plane->id(), plane->src_h_property().id(),
235 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
237 ALOGE("Failed to add plane %d to set", plane->id());
241 if (plane->rotation_property().id()) {
242 ret = drmModePropertySetAdd(pset, plane->id(),
243 plane->rotation_property().id(), rotation);
245 ALOGE("Failed to add rotation property %d to plane %d",
246 plane->rotation_property().id(), plane->id());
253 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
255 ALOGE("Failed to commit pset ret=%d\n", ret);
258 drmModePropertySetFree(pset);
263 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
264 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
266 ALOGE("Failed to get DrmConnector for display %d", display_);
270 const DrmProperty &prop = conn->dpms_property();
271 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
272 display_comp->dpms_mode());
274 ALOGE("Failed to set DPMS property for connector %d", conn->id());
280 int DrmDisplayCompositor::Composite() {
282 int ret = pthread_mutex_lock(&lock_);
284 ALOGE("Failed to acquire compositor lock %d", ret);
287 if (composite_queue_.empty()) {
288 ret = pthread_mutex_unlock(&lock_);
290 ALOGE("Failed to release compositor lock %d", ret);
294 std::unique_ptr<DrmDisplayComposition> composition(
295 std::move(composite_queue_.front()));
296 composite_queue_.pop();
298 ret = pthread_mutex_unlock(&lock_);
300 ALOGE("Failed to release compositor lock %d", ret);
304 switch (composition->type()) {
305 case DRM_COMPOSITION_TYPE_FRAME:
306 ret = ApplyFrame(composition.get());
308 ALOGE("Composite failed for display %d", display_);
311 ++dump_frames_composited_;
313 case DRM_COMPOSITION_TYPE_DPMS:
314 ret = ApplyDpms(composition.get());
316 ALOGE("Failed to apply dpms for display %d", display_);
319 ALOGE("Unknown composition type %d", composition->type());
323 if (active_composition_)
324 active_composition_->FinishComposition();
326 ret = pthread_mutex_lock(&lock_);
328 ALOGE("Failed to acquire lock for active_composition swap");
330 active_composition_.swap(composition);
333 ret = pthread_mutex_unlock(&lock_);
335 ALOGE("Failed to release lock for active_composition swap");
340 bool DrmDisplayCompositor::HaveQueuedComposites() const {
341 int ret = pthread_mutex_lock(&lock_);
343 ALOGE("Failed to acquire compositor lock %d", ret);
347 bool empty_ret = !composite_queue_.empty();
349 ret = pthread_mutex_unlock(&lock_);
351 ALOGE("Failed to release compositor lock %d", ret);
358 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
361 int ret = pthread_mutex_lock(&lock_);
365 uint64_t num_frames = dump_frames_composited_;
366 dump_frames_composited_ = 0;
369 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
371 DrmCompositionLayerVector_t layers;
372 if (active_composition_)
373 layers = *active_composition_->GetCompositionLayers();
377 ret |= pthread_mutex_unlock(&lock_);
381 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
382 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
383 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
385 *out << "--DrmDisplayCompositor[" << display_
386 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
387 << " fps=" << fps << "\n";
389 dump_last_timestamp_ns_ = cur_ts;
391 *out << "---- DrmDisplayCompositor Layers: num=" << layers.size() << "\n";
392 for (DrmCompositionLayerVector_t::iterator iter = layers.begin();
393 iter != layers.end(); ++iter) {
394 hwc_layer_1_t *layer = &iter->layer;
395 DrmPlane *plane = iter->plane;
397 *out << "------ DrmDisplayCompositor Layer: plane=" << plane->id() << " ";
399 DrmCrtc *crtc = iter->crtc;
401 *out << "disabled\n";
405 *out << "crtc=" << crtc->id()
406 << " crtc[x/y/w/h]=" << layer->displayFrame.left << "/"
407 << layer->displayFrame.top << "/"
408 << layer->displayFrame.right - layer->displayFrame.left << "/"
409 << layer->displayFrame.bottom - layer->displayFrame.top << " "
410 << " src[x/y/w/h]=" << layer->sourceCropf.left << "/"
411 << layer->sourceCropf.top << "/"
412 << layer->sourceCropf.right - layer->sourceCropf.left << "/"
413 << layer->sourceCropf.bottom - layer->sourceCropf.top
414 << " transform=" << layer->transform << "\n";