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"
33 #include <drm/drm_mode.h>
34 #include <cutils/log.h>
35 #include <sync/sync.h>
36 #include <utils/Trace.h>
38 #define DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH 3
42 void SquashState::Init(DrmHwcLayer *layers, size_t num_layers) {
46 last_handles_.clear();
48 std::vector<DrmHwcRect<int>> in_rects;
49 for (size_t i = 0; i < num_layers; i++) {
50 DrmHwcLayer *layer = &layers[i];
51 in_rects.emplace_back(layer->display_frame);
52 last_handles_.push_back(layer->sf_handle);
55 std::vector<seperate_rects::RectSet<uint64_t, int>> out_regions;
56 seperate_rects::seperate_rects_64(in_rects, &out_regions);
58 for (const seperate_rects::RectSet<uint64_t, int> &out_region : out_regions) {
59 regions_.emplace_back();
60 Region ®ion = regions_.back();
61 region.rect = out_region.rect;
62 region.layer_refs = out_region.id_set.getBits();
66 void SquashState::GenerateHistory(DrmHwcLayer *layers,
67 std::vector<bool> &changed_regions) const {
68 std::bitset<kMaxLayers> changed_layers;
69 for (size_t i = 0; i < last_handles_.size(); i++) {
70 DrmHwcLayer *layer = &layers[i];
71 if (last_handles_[i] != layer->sf_handle) {
72 changed_layers.set(i);
76 changed_regions.resize(regions_.size());
77 for (size_t i = 0; i < regions_.size(); i++) {
78 changed_regions[i] = (regions_[i].layer_refs & changed_layers).any();
82 void SquashState::StableRegionsWithMarginalHistory(
83 const std::vector<bool> &changed_regions,
84 std::vector<bool> &stable_regions) const {
85 stable_regions.resize(regions_.size());
86 for (size_t i = 0; i < regions_.size(); i++) {
87 stable_regions[i] = !changed_regions[i] && is_stable(i);
91 void SquashState::RecordHistory(DrmHwcLayer *layers,
92 const std::vector<bool> &changed_regions) {
93 for (size_t i = 0; i < last_handles_.size(); i++) {
94 DrmHwcLayer *layer = &layers[i];
95 last_handles_[i] = layer->sf_handle;
98 for (size_t i = 0; i < regions_.size(); i++) {
99 regions_[i].change_history <<= 1;
100 regions_[i].change_history.set(/* LSB */ 0, changed_regions[i]);
106 void SquashState::RecordSquashed(const std::vector<bool> &squashed_regions) {
107 for (size_t i = 0; i < regions_.size(); i++) {
108 regions_[i].squashed = squashed_regions[i];
112 void SquashState::Dump(std::ostringstream *out) const {
113 *out << "----SquashState generation=" << generation_number_
114 << " history=" << valid_history_ << "\n"
115 << " Regions: count=" << regions_.size() << "\n";
116 for (size_t i = 0; i < regions_.size(); i++) {
117 const Region ®ion = regions_[i];
118 *out << " [" << i << "]"
119 << " history=" << region.change_history << " rect";
120 region.rect.Dump(out);
123 for (size_t layer_index = 0; layer_index < kMaxLayers; layer_index++) {
124 if ((region.layer_refs &
125 std::bitset<kMaxLayers>((size_t)1 << layer_index))
140 DrmDisplayCompositor::DrmDisplayCompositor()
146 needs_modeset_(false),
147 framebuffer_index_(0),
148 dump_frames_composited_(0),
149 dump_last_timestamp_ns_(0) {
151 if (clock_gettime(CLOCK_MONOTONIC, &ts))
153 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
156 DrmDisplayCompositor::~DrmDisplayCompositor() {
162 int ret = pthread_mutex_lock(&lock_);
164 ALOGE("Failed to acquire compositor lock %d", ret);
166 while (!composite_queue_.empty()) {
167 composite_queue_.front().reset();
168 composite_queue_.pop();
170 active_composition_.reset();
172 ret = pthread_mutex_unlock(&lock_);
174 ALOGE("Failed to acquire compositor lock %d", ret);
176 pthread_mutex_destroy(&lock_);
179 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
183 int ret = pthread_mutex_init(&lock_, NULL);
185 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
188 ret = worker_.Init();
190 pthread_mutex_destroy(&lock_);
191 ALOGE("Failed to initialize compositor worker %d\n", ret);
199 std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
201 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
204 int DrmDisplayCompositor::QueueComposition(
205 std::unique_ptr<DrmDisplayComposition> composition) {
206 switch (composition->type()) {
207 case DRM_COMPOSITION_TYPE_FRAME:
211 case DRM_COMPOSITION_TYPE_DPMS:
213 * Update the state as soon as we get it so we can start/stop queuing
216 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
218 case DRM_COMPOSITION_TYPE_MODESET:
220 case DRM_COMPOSITION_TYPE_EMPTY:
223 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
227 int ret = pthread_mutex_lock(&lock_);
229 ALOGE("Failed to acquire compositor lock %d", ret);
233 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
234 // to eat our buffer handles when we get about 1 second behind.
235 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
236 pthread_mutex_unlock(&lock_);
238 pthread_mutex_lock(&lock_);
241 composite_queue_.push(std::move(composition));
243 ret = pthread_mutex_unlock(&lock_);
245 ALOGE("Failed to release compositor lock %d", ret);
253 std::tuple<uint32_t, uint32_t, int>
254 DrmDisplayCompositor::GetActiveModeResolution() {
255 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
256 if (connector == NULL) {
257 ALOGE("Failed to determine display mode: no connector for display %d",
259 return std::make_tuple(0, 0, -ENODEV);
262 const DrmMode &mode = connector->active_mode();
263 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
266 int DrmDisplayCompositor::PrepareFramebuffer(
267 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
268 int ret = fb.WaitReleased(-1);
270 ALOGE("Failed to wait for framebuffer release %d", ret);
273 uint32_t width, height;
274 std::tie(width, height, ret) = GetActiveModeResolution();
277 "Failed to allocate framebuffer because the display resolution could "
278 "not be determined %d",
283 fb.set_release_fence_fd(-1);
284 if (!fb.Allocate(width, height)) {
285 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
289 display_comp->layers().emplace_back();
290 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
291 pre_comp_layer.sf_handle = fb.buffer()->handle;
292 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
293 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
294 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
295 display_comp->importer());
297 ALOGE("Failed to import framebuffer for display %d", ret);
304 int DrmDisplayCompositor::ApplyPreComposite(
305 DrmDisplayComposition *display_comp) {
308 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
309 ret = PrepareFramebuffer(fb, display_comp);
311 ALOGE("Failed to prepare framebuffer for precomposite %d", ret);
315 std::vector<DrmCompositionRegion> ®ions = display_comp->pre_comp_regions();
316 ret = pre_compositor_->Composite(display_comp->layers().data(),
317 regions.data(), regions.size(), fb.buffer());
318 pre_compositor_->Finish();
321 ALOGE("Failed to composite layers");
325 ret = display_comp->CreateNextTimelineFence();
327 ALOGE("Failed to create pre comp framebuffer release fence %d", ret);
331 fb.set_release_fence_fd(ret);
332 display_comp->SignalPreCompDone();
337 int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
338 drmModePropertySetPtr pset = drmModePropertySetAlloc();
340 ALOGE("Failed to allocate property set");
345 std::vector<DrmCompositionPlane> &comp_planes =
346 display_comp->composition_planes();
347 for (DrmCompositionPlane &comp_plane : comp_planes) {
348 DrmPlane *plane = comp_plane.plane;
350 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
352 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), 0);
354 ALOGE("Failed to add plane %d disable to pset", plane->id());
355 drmModePropertySetFree(pset);
360 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
362 ALOGE("Failed to commit pset ret=%d\n", ret);
363 drmModePropertySetFree(pset);
367 drmModePropertySetFree(pset);
371 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
374 std::vector<DrmHwcLayer> &layers = display_comp->layers();
375 std::vector<DrmCompositionPlane> &comp_planes =
376 display_comp->composition_planes();
377 std::vector<DrmCompositionRegion> &pre_comp_regions =
378 display_comp->pre_comp_regions();
380 bool do_pre_comp = pre_comp_regions.size() > 0;
381 DrmFramebuffer *pre_comp_fb;
382 int pre_comp_layer_index = -1;
385 ret = ApplyPreComposite(display_comp);
389 pre_comp_layer_index = layers.size() - 1;
390 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
393 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
395 ALOGE("Could not locate connector for display %d", display_);
398 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
400 ALOGE("Could not locate crtc for display %d", display_);
404 drmModePropertySetPtr pset = drmModePropertySetAlloc();
406 ALOGE("Failed to allocate property set");
410 uint32_t blob_id = 0;
411 uint64_t old_blob_id;
412 if (needs_modeset_) {
413 DrmProperty old_mode;
414 ret = drm_->GetCrtcProperty(*crtc, crtc->mode_property().name().c_str(),
417 ALOGE("Failed to get old mode property from crtc %d", crtc->id());
418 drmModePropertySetFree(pset);
421 ret = old_mode.value(&old_blob_id);
423 ALOGE("Could not get old blob id value %d", ret);
424 drmModePropertySetFree(pset);
428 struct drm_mode_modeinfo drm_mode;
429 memset(&drm_mode, 0, sizeof(drm_mode));
430 next_mode_.ToDrmModeModeInfo(&drm_mode);
432 ret = drm_->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
435 ALOGE("Failed to create mode property blob %d", ret);
436 drmModePropertySetFree(pset);
440 ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
442 drmModePropertySetAdd(pset, connector->id(),
443 connector->crtc_id_property().id(), crtc->id());
445 ALOGE("Failed to add blob %d to pset", blob_id);
446 drmModePropertySetFree(pset);
447 drm_->DestroyPropertyBlob(blob_id);
452 for (DrmCompositionPlane &comp_plane : comp_planes) {
453 DrmPlane *plane = comp_plane.plane;
454 DrmCrtc *crtc = comp_plane.crtc;
457 DrmHwcRect<int> display_frame;
458 DrmHwcRect<float> source_crop;
459 uint64_t rotation = 0;
460 switch (comp_plane.source_layer) {
461 case DrmCompositionPlane::kSourceNone:
463 case DrmCompositionPlane::kSourcePreComp: {
466 "Can not use pre composite framebuffer with no pre composite "
471 DrmHwcLayer &layer = layers[pre_comp_layer_index];
472 fb_id = layer.buffer->fb_id;
473 display_frame = layer.display_frame;
474 source_crop = layer.source_crop;
476 case DrmCompositionPlane::kSourceSquash:
479 if (comp_plane.source_layer >= layers.size()) {
480 ALOGE("Source layer index %zu out of bounds %zu",
481 comp_plane.source_layer, layers.size());
484 DrmHwcLayer &layer = layers[comp_plane.source_layer];
485 if (layer.acquire_fence.get() >= 0) {
486 int acquire_fence = layer.acquire_fence.get();
487 for (int i = 0; i < kAcquireWaitTries; ++i) {
488 ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs);
490 ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
491 acquire_fence, i, ret, (i + 1) * kAcquireWaitTimeoutMs);
494 ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
497 layer.acquire_fence.Close();
500 ALOGE("Expected a valid framebuffer for pset");
503 fb_id = layer.buffer->fb_id;
504 display_frame = layer.display_frame;
505 source_crop = layer.source_crop;
506 switch (layer.transform) {
507 case DrmHwcTransform::kFlipH:
508 rotation = 1 << DRM_REFLECT_X;
510 case DrmHwcTransform::kFlipV:
511 rotation = 1 << DRM_REFLECT_Y;
513 case DrmHwcTransform::kRotate90:
514 rotation = 1 << DRM_ROTATE_90;
516 case DrmHwcTransform::kRotate180:
517 rotation = 1 << DRM_ROTATE_180;
519 case DrmHwcTransform::kRotate270:
520 rotation = 1 << DRM_ROTATE_270;
522 case DrmHwcTransform::kIdentity:
526 ALOGE("Invalid transform value 0x%x given", layer.transform);
532 // Disable the plane if there's no framebuffer
534 ret = drmModePropertySetAdd(pset, plane->id(),
535 plane->crtc_property().id(), 0) ||
536 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
539 ALOGE("Failed to add plane %d disable to pset", plane->id());
545 // TODO: Once we have atomic test, this should fall back to GL
546 if (rotation && plane->rotation_property().id() == 0) {
547 ALOGE("Rotation is not supported on plane %d", plane->id());
553 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
555 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
557 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
558 display_frame.left) ||
559 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
560 display_frame.top) ||
561 drmModePropertySetAdd(pset, plane->id(), plane->crtc_w_property().id(),
562 display_frame.right - display_frame.left) ||
563 drmModePropertySetAdd(pset, plane->id(), plane->crtc_h_property().id(),
564 display_frame.bottom - display_frame.top) ||
565 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
566 (int)(source_crop.left) << 16) ||
567 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
568 (int)(source_crop.top) << 16) ||
569 drmModePropertySetAdd(pset, plane->id(), plane->src_w_property().id(),
570 (int)(source_crop.right - source_crop.left)
572 drmModePropertySetAdd(pset, plane->id(), plane->src_h_property().id(),
573 (int)(source_crop.bottom - source_crop.top)
576 ALOGE("Failed to add plane %d to set", plane->id());
580 if (plane->rotation_property().id()) {
581 ret = drmModePropertySetAdd(pset, plane->id(),
582 plane->rotation_property().id(), rotation);
584 ALOGE("Failed to add rotation property %d to plane %d",
585 plane->rotation_property().id(), plane->id());
593 ret = drmModePropertySetCommit(drm_->fd(), DRM_MODE_ATOMIC_ALLOW_MODESET,
596 ALOGE("Failed to commit pset ret=%d\n", ret);
597 drmModePropertySetFree(pset);
599 drm_->DestroyPropertyBlob(blob_id);
604 drmModePropertySetFree(pset);
606 if (needs_modeset_) {
607 ret = drm_->DestroyPropertyBlob(old_blob_id);
609 ALOGE("Failed to destroy old mode property blob %lld/%d", old_blob_id,
614 /* TODO: Add dpms to the pset when the kernel supports it */
615 ret = ApplyDpms(display_comp);
617 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
621 connector->set_active_mode(next_mode_);
622 needs_modeset_ = false;
628 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
629 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
631 ALOGE("Failed to get DrmConnector for display %d", display_);
635 const DrmProperty &prop = conn->dpms_property();
636 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
637 display_comp->dpms_mode());
639 ALOGE("Failed to set DPMS property for connector %d", conn->id());
645 int DrmDisplayCompositor::Composite() {
648 if (!pre_compositor_) {
649 pre_compositor_.reset(new GLWorkerCompositor());
650 int ret = pre_compositor_->Init();
652 ALOGE("Failed to initialize OpenGL compositor %d", ret);
657 int ret = pthread_mutex_lock(&lock_);
659 ALOGE("Failed to acquire compositor lock %d", ret);
662 if (composite_queue_.empty()) {
663 ret = pthread_mutex_unlock(&lock_);
665 ALOGE("Failed to release compositor lock %d", ret);
669 std::unique_ptr<DrmDisplayComposition> composition(
670 std::move(composite_queue_.front()));
672 composite_queue_.pop();
674 ret = pthread_mutex_unlock(&lock_);
676 ALOGE("Failed to release compositor lock %d", ret);
680 switch (composition->type()) {
681 case DRM_COMPOSITION_TYPE_FRAME:
682 ret = ApplyFrame(composition.get());
684 ALOGE("Composite failed for display %d", display_);
686 // Disable the hw used by the last active composition. This allows us to
687 // signal the release fences from that composition to avoid hanging.
688 if (DisablePlanes(active_composition_.get()))
691 ++dump_frames_composited_;
693 case DRM_COMPOSITION_TYPE_DPMS:
694 ret = ApplyDpms(composition.get());
696 ALOGE("Failed to apply dpms for display %d", display_);
698 case DRM_COMPOSITION_TYPE_MODESET:
699 next_mode_ = composition->display_mode();
700 needs_modeset_ = true;
703 ALOGE("Unknown composition type %d", composition->type());
707 if (active_composition_)
708 active_composition_->SignalCompositionDone();
710 ret = pthread_mutex_lock(&lock_);
712 ALOGE("Failed to acquire lock for active_composition swap");
714 active_composition_.swap(composition);
717 ret = pthread_mutex_unlock(&lock_);
719 ALOGE("Failed to release lock for active_composition swap");
724 bool DrmDisplayCompositor::HaveQueuedComposites() const {
725 int ret = pthread_mutex_lock(&lock_);
727 ALOGE("Failed to acquire compositor lock %d", ret);
731 bool empty_ret = !composite_queue_.empty();
733 ret = pthread_mutex_unlock(&lock_);
735 ALOGE("Failed to release compositor lock %d", ret);
742 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
743 int ret = pthread_mutex_lock(&lock_);
747 uint64_t num_frames = dump_frames_composited_;
748 dump_frames_composited_ = 0;
751 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
753 pthread_mutex_unlock(&lock_);
757 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
758 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
759 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
761 *out << "--DrmDisplayCompositor[" << display_
762 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
763 << " fps=" << fps << "\n";
765 dump_last_timestamp_ns_ = cur_ts;
767 if (active_composition_)
768 active_composition_->Dump(out);
770 pthread_mutex_unlock(&lock_);