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"
29 #include <cutils/log.h>
30 #include <drm/drm_mode.h>
31 #include <sync/sync.h>
32 #include <utils/Trace.h>
37 #include "drmresources.h"
40 #define DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH 2
44 void SquashState::Init(DrmHwcLayer *layers, size_t num_layers) {
48 last_handles_.clear();
50 std::vector<DrmHwcRect<int>> in_rects;
51 for (size_t i = 0; i < num_layers; i++) {
52 DrmHwcLayer *layer = &layers[i];
53 in_rects.emplace_back(layer->display_frame);
54 last_handles_.push_back(layer->sf_handle);
57 std::vector<separate_rects::RectSet<uint64_t, int>> out_regions;
58 separate_rects::separate_rects_64(in_rects, &out_regions);
60 for (const separate_rects::RectSet<uint64_t, int> &out_region : out_regions) {
61 regions_.emplace_back();
62 Region ®ion = regions_.back();
63 region.rect = out_region.rect;
64 region.layer_refs = out_region.id_set.getBits();
68 void SquashState::GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
69 std::vector<bool> &changed_regions) const {
70 changed_regions.resize(regions_.size());
71 if (num_layers != last_handles_.size()) {
72 ALOGE("SquashState::GenerateHistory expected %zu layers but got %zu layers",
73 last_handles_.size(), num_layers);
76 std::bitset<kMaxLayers> changed_layers;
77 for (size_t i = 0; i < last_handles_.size(); i++) {
78 DrmHwcLayer *layer = &layers[i];
79 // Protected layers can't be squashed so we treat them as constantly
81 if (layer->protected_usage() || last_handles_[i] != layer->sf_handle)
82 changed_layers.set(i);
85 for (size_t i = 0; i < regions_.size(); i++) {
86 changed_regions[i] = (regions_[i].layer_refs & changed_layers).any();
90 void SquashState::StableRegionsWithMarginalHistory(
91 const std::vector<bool> &changed_regions,
92 std::vector<bool> &stable_regions) const {
93 stable_regions.resize(regions_.size());
94 for (size_t i = 0; i < regions_.size(); i++) {
95 stable_regions[i] = !changed_regions[i] && is_stable(i);
99 void SquashState::RecordHistory(DrmHwcLayer *layers, size_t num_layers,
100 const std::vector<bool> &changed_regions) {
101 if (num_layers != last_handles_.size()) {
102 ALOGE("SquashState::RecordHistory expected %zu layers but got %zu layers",
103 last_handles_.size(), num_layers);
106 if (changed_regions.size() != regions_.size()) {
107 ALOGE("SquashState::RecordHistory expected %zu regions but got %zu regions",
108 regions_.size(), changed_regions.size());
112 for (size_t i = 0; i < last_handles_.size(); i++) {
113 DrmHwcLayer *layer = &layers[i];
114 last_handles_[i] = layer->sf_handle;
117 for (size_t i = 0; i < regions_.size(); i++) {
118 regions_[i].change_history <<= 1;
119 regions_[i].change_history.set(/* LSB */ 0, changed_regions[i]);
125 bool SquashState::RecordAndCompareSquashed(
126 const std::vector<bool> &squashed_regions) {
127 if (squashed_regions.size() != regions_.size()) {
129 "SquashState::RecordAndCompareSquashed expected %zu regions but got "
131 regions_.size(), squashed_regions.size());
134 bool changed = false;
135 for (size_t i = 0; i < regions_.size(); i++) {
136 if (regions_[i].squashed != squashed_regions[i]) {
137 regions_[i].squashed = squashed_regions[i];
144 void SquashState::Dump(std::ostringstream *out) const {
145 *out << "----SquashState generation=" << generation_number_
146 << " history=" << valid_history_ << "\n"
147 << " Regions: count=" << regions_.size() << "\n";
148 for (size_t i = 0; i < regions_.size(); i++) {
149 const Region ®ion = regions_[i];
150 *out << " [" << i << "]"
151 << " history=" << region.change_history << " rect";
152 region.rect.Dump(out);
155 for (size_t layer_index = 0; layer_index < kMaxLayers; layer_index++) {
156 if ((region.layer_refs &
157 std::bitset<kMaxLayers>((size_t)1 << layer_index))
172 static bool UsesSquash(const std::vector<DrmCompositionPlane> &comp_planes) {
173 return std::any_of(comp_planes.begin(), comp_planes.end(),
174 [](const DrmCompositionPlane &plane) {
175 return plane.type() == DrmCompositionPlane::Type::kSquash;
179 DrmDisplayCompositor::FrameWorker::FrameWorker(DrmDisplayCompositor *compositor)
180 : Worker("frame-worker", HAL_PRIORITY_URGENT_DISPLAY),
181 compositor_(compositor) {
184 DrmDisplayCompositor::FrameWorker::~FrameWorker() {
187 int DrmDisplayCompositor::FrameWorker::Init() {
191 void DrmDisplayCompositor::FrameWorker::QueueFrame(
192 std::unique_ptr<DrmDisplayComposition> composition, int status) {
195 frame.composition = std::move(composition);
196 frame.status = status;
197 frame_queue_.push(std::move(frame));
202 void DrmDisplayCompositor::FrameWorker::Routine() {
206 if (frame_queue_.empty()) {
207 wait_ret = WaitForSignalOrExitLocked();
211 if (!frame_queue_.empty()) {
212 frame = std::move(frame_queue_.front());
217 if (wait_ret == -EINTR) {
219 } else if (wait_ret) {
220 ALOGE("Failed to wait for signal, %d", wait_ret);
223 compositor_->ApplyFrame(std::move(frame.composition), frame.status);
226 DrmDisplayCompositor::DrmDisplayCompositor()
233 use_hw_overlays_(true),
234 framebuffer_index_(0),
235 squash_framebuffer_index_(0),
236 dump_frames_composited_(0),
237 dump_last_timestamp_ns_(0) {
239 if (clock_gettime(CLOCK_MONOTONIC, &ts))
241 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
244 DrmDisplayCompositor::~DrmDisplayCompositor() {
249 frame_worker_.Exit();
251 int ret = pthread_mutex_lock(&lock_);
253 ALOGE("Failed to acquire compositor lock %d", ret);
256 drm_->DestroyPropertyBlob(mode_.blob_id);
257 if (mode_.old_blob_id)
258 drm_->DestroyPropertyBlob(mode_.old_blob_id);
260 while (!composite_queue_.empty()) {
261 composite_queue_.front().reset();
262 composite_queue_.pop();
264 active_composition_.reset();
266 ret = pthread_mutex_unlock(&lock_);
268 ALOGE("Failed to acquire compositor lock %d", ret);
270 pthread_mutex_destroy(&lock_);
273 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
277 int ret = pthread_mutex_init(&lock_, NULL);
279 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
282 ret = worker_.Init();
284 pthread_mutex_destroy(&lock_);
285 ALOGE("Failed to initialize compositor worker %d\n", ret);
288 ret = frame_worker_.Init();
290 pthread_mutex_destroy(&lock_);
291 ALOGE("Failed to initialize frame worker %d\n", ret);
299 std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
301 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
304 int DrmDisplayCompositor::QueueComposition(
305 std::unique_ptr<DrmDisplayComposition> composition) {
306 switch (composition->type()) {
307 case DRM_COMPOSITION_TYPE_FRAME:
311 case DRM_COMPOSITION_TYPE_DPMS:
313 * Update the state as soon as we get it so we can start/stop queuing
316 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
318 case DRM_COMPOSITION_TYPE_MODESET:
320 case DRM_COMPOSITION_TYPE_EMPTY:
323 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
327 int ret = pthread_mutex_lock(&lock_);
329 ALOGE("Failed to acquire compositor lock %d", ret);
333 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
334 // to eat our buffer handles when we get about 1 second behind.
335 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
336 pthread_mutex_unlock(&lock_);
338 pthread_mutex_lock(&lock_);
341 composite_queue_.push(std::move(composition));
343 ret = pthread_mutex_unlock(&lock_);
345 ALOGE("Failed to release compositor lock %d", ret);
353 std::tuple<uint32_t, uint32_t, int>
354 DrmDisplayCompositor::GetActiveModeResolution() {
355 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
356 if (connector == NULL) {
357 ALOGE("Failed to determine display mode: no connector for display %d",
359 return std::make_tuple(0, 0, -ENODEV);
362 const DrmMode &mode = connector->active_mode();
363 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
366 int DrmDisplayCompositor::PrepareFramebuffer(
367 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
368 int ret = fb.WaitReleased(-1);
370 ALOGE("Failed to wait for framebuffer release %d", ret);
373 uint32_t width, height;
374 std::tie(width, height, ret) = GetActiveModeResolution();
377 "Failed to allocate framebuffer because the display resolution could "
378 "not be determined %d",
383 fb.set_release_fence_fd(-1);
384 if (!fb.Allocate(width, height)) {
385 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
389 display_comp->layers().emplace_back();
390 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
391 pre_comp_layer.sf_handle = fb.buffer()->handle;
392 pre_comp_layer.blending = DrmHwcBlending::kPreMult;
393 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
394 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
395 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
396 display_comp->importer());
398 ALOGE("Failed to import framebuffer for display %d", ret);
405 int DrmDisplayCompositor::ApplySquash(DrmDisplayComposition *display_comp) {
408 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
409 ret = PrepareFramebuffer(fb, display_comp);
411 ALOGE("Failed to prepare framebuffer for squash %d", ret);
415 std::vector<DrmCompositionRegion> ®ions = display_comp->squash_regions();
416 ret = pre_compositor_->Composite(display_comp->layers().data(),
417 regions.data(), regions.size(), fb.buffer(),
418 display_comp->importer());
419 pre_compositor_->Finish();
422 ALOGE("Failed to squash layers");
426 ret = display_comp->CreateNextTimelineFence();
428 ALOGE("Failed to create squash framebuffer release fence %d", ret);
432 fb.set_release_fence_fd(ret);
433 display_comp->SignalSquashDone();
438 int DrmDisplayCompositor::ApplyPreComposite(
439 DrmDisplayComposition *display_comp) {
442 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
443 ret = PrepareFramebuffer(fb, display_comp);
445 ALOGE("Failed to prepare framebuffer for pre-composite %d", ret);
449 std::vector<DrmCompositionRegion> ®ions = display_comp->pre_comp_regions();
450 ret = pre_compositor_->Composite(display_comp->layers().data(),
451 regions.data(), regions.size(), fb.buffer(),
452 display_comp->importer());
453 pre_compositor_->Finish();
456 ALOGE("Failed to pre-composite layers");
460 ret = display_comp->CreateNextTimelineFence();
462 ALOGE("Failed to create pre-composite framebuffer release fence %d", ret);
466 fb.set_release_fence_fd(ret);
467 display_comp->SignalPreCompDone();
472 int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
473 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
475 ALOGE("Failed to allocate property set");
480 std::vector<DrmCompositionPlane> &comp_planes =
481 display_comp->composition_planes();
482 for (DrmCompositionPlane &comp_plane : comp_planes) {
483 DrmPlane *plane = comp_plane.plane();
484 ret = drmModeAtomicAddProperty(pset, plane->id(),
485 plane->crtc_property().id(), 0) < 0 ||
486 drmModeAtomicAddProperty(pset, plane->id(), plane->fb_property().id(),
489 ALOGE("Failed to add plane %d disable to pset", plane->id());
490 drmModeAtomicFree(pset);
495 ret = drmModeAtomicCommit(drm_->fd(), pset, 0, drm_);
497 ALOGE("Failed to commit pset ret=%d\n", ret);
498 drmModeAtomicFree(pset);
502 drmModeAtomicFree(pset);
506 int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) {
509 std::vector<DrmHwcLayer> &layers = display_comp->layers();
510 std::vector<DrmCompositionPlane> &comp_planes =
511 display_comp->composition_planes();
512 std::vector<DrmCompositionRegion> &squash_regions =
513 display_comp->squash_regions();
514 std::vector<DrmCompositionRegion> &pre_comp_regions =
515 display_comp->pre_comp_regions();
517 int squash_layer_index = -1;
518 if (squash_regions.size() > 0) {
519 squash_framebuffer_index_ = (squash_framebuffer_index_ + 1) % 2;
520 ret = ApplySquash(display_comp);
524 squash_layer_index = layers.size() - 1;
526 if (UsesSquash(comp_planes)) {
527 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
528 layers.emplace_back();
529 squash_layer_index = layers.size() - 1;
530 DrmHwcLayer &squash_layer = layers.back();
531 ret = squash_layer.buffer.ImportBuffer(fb.buffer()->handle,
532 display_comp->importer());
534 ALOGE("Failed to import old squashed framebuffer %d", ret);
537 squash_layer.sf_handle = fb.buffer()->handle;
538 squash_layer.blending = DrmHwcBlending::kPreMult;
539 squash_layer.source_crop = DrmHwcRect<float>(
540 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
541 squash_layer.display_frame = DrmHwcRect<int>(
542 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
543 ret = display_comp->CreateNextTimelineFence();
546 ALOGE("Failed to create squash framebuffer release fence %d", ret);
550 fb.set_release_fence_fd(ret);
555 bool do_pre_comp = pre_comp_regions.size() > 0;
556 int pre_comp_layer_index = -1;
558 ret = ApplyPreComposite(display_comp);
562 pre_comp_layer_index = layers.size() - 1;
563 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
566 for (DrmCompositionPlane &comp_plane : comp_planes) {
567 std::vector<size_t> &source_layers = comp_plane.source_layers();
568 switch (comp_plane.type()) {
569 case DrmCompositionPlane::Type::kSquash:
570 if (source_layers.size())
571 ALOGE("Squash source_layers is expected to be empty (%zu/%d)",
572 source_layers[0], squash_layer_index);
573 source_layers.push_back(squash_layer_index);
575 case DrmCompositionPlane::Type::kPrecomp:
578 "Can not use pre composite framebuffer with no pre composite "
582 // Replace source_layers with the output of the precomposite
583 source_layers.clear();
584 source_layers.push_back(pre_comp_layer_index);
594 int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
600 std::vector<DrmHwcLayer> &layers = display_comp->layers();
601 std::vector<DrmCompositionPlane> &comp_planes =
602 display_comp->composition_planes();
603 std::vector<DrmCompositionRegion> &pre_comp_regions =
604 display_comp->pre_comp_regions();
605 uint64_t out_fences[drm_->crtcs().size()];
607 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
609 ALOGE("Could not locate connector for display %d", display_);
612 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
614 ALOGE("Could not locate crtc for display %d", display_);
618 drmModeAtomicReqPtr pset = drmModeAtomicAlloc();
620 ALOGE("Failed to allocate property set");
624 if (crtc->out_fence_ptr_property().id() != 0) {
625 ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->out_fence_ptr_property().id(),
626 (uint64_t) &out_fences[crtc->pipe()]);
628 ALOGE("Failed to add OUT_FENCE_PTR property to pset: %d", ret);
629 drmModeAtomicFree(pset);
634 if (mode_.needs_modeset) {
635 ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->mode_property().id(),
636 mode_.blob_id) < 0 ||
637 drmModeAtomicAddProperty(pset, connector->id(),
638 connector->crtc_id_property().id(),
641 ALOGE("Failed to add blob %d to pset", mode_.blob_id);
642 drmModeAtomicFree(pset);
647 for (DrmCompositionPlane &comp_plane : comp_planes) {
648 DrmPlane *plane = comp_plane.plane();
649 DrmCrtc *crtc = comp_plane.crtc();
650 std::vector<size_t> &source_layers = comp_plane.source_layers();
654 DrmHwcRect<int> display_frame;
655 DrmHwcRect<float> source_crop;
656 uint64_t rotation = 0;
657 uint64_t alpha = 0xFF;
659 if (comp_plane.type() != DrmCompositionPlane::Type::kDisable) {
660 if (source_layers.size() > 1) {
661 ALOGE("Can't handle more than one source layer sz=%zu type=%d",
662 source_layers.size(), comp_plane.type());
666 if (source_layers.empty() || source_layers.front() >= layers.size()) {
667 ALOGE("Source layer index %zu out of bounds %zu type=%d",
668 source_layers.front(), layers.size(), comp_plane.type());
671 DrmHwcLayer &layer = layers[source_layers.front()];
673 ALOGE("Expected a valid framebuffer for pset");
676 fb_id = layer.buffer->fb_id;
677 fence_fd = layer.acquire_fence.get();
678 display_frame = layer.display_frame;
679 source_crop = layer.source_crop;
680 if (layer.blending == DrmHwcBlending::kPreMult)
684 if (layer.transform & DrmHwcTransform::kFlipH)
685 rotation |= 1 << DRM_REFLECT_X;
686 if (layer.transform & DrmHwcTransform::kFlipV)
687 rotation |= 1 << DRM_REFLECT_Y;
688 if (layer.transform & DrmHwcTransform::kRotate90)
689 rotation |= 1 << DRM_ROTATE_90;
690 else if (layer.transform & DrmHwcTransform::kRotate180)
691 rotation |= 1 << DRM_ROTATE_180;
692 else if (layer.transform & DrmHwcTransform::kRotate270)
693 rotation |= 1 << DRM_ROTATE_270;
696 int prop_id = plane->in_fence_fd_property().id();
698 ALOGE("Failed to get IN_FENCE_FD property id");
701 ret = drmModeAtomicAddProperty(pset, plane->id(), prop_id, fence_fd);
703 ALOGE("Failed to add IN_FENCE_FD property to pset: %d", ret);
709 // Disable the plane if there's no framebuffer
711 ret = drmModeAtomicAddProperty(pset, plane->id(),
712 plane->crtc_property().id(), 0) < 0 ||
713 drmModeAtomicAddProperty(pset, plane->id(),
714 plane->fb_property().id(), 0) < 0;
716 ALOGE("Failed to add plane %d disable to pset", plane->id());
722 // TODO: Once we have atomic test, this should fall back to GL
723 if (rotation && plane->rotation_property().id() == 0) {
724 ALOGE("Rotation is not supported on plane %d", plane->id());
729 // TODO: Once we have atomic test, this should fall back to GL
730 if (alpha != 0xFF && plane->alpha_property().id() == 0) {
731 ALOGE("Alpha is not supported on plane %d", plane->id());
736 ret = drmModeAtomicAddProperty(pset, plane->id(),
737 plane->crtc_property().id(), crtc->id()) < 0;
738 ret |= drmModeAtomicAddProperty(pset, plane->id(),
739 plane->fb_property().id(), fb_id) < 0;
740 ret |= drmModeAtomicAddProperty(pset, plane->id(),
741 plane->crtc_x_property().id(),
742 display_frame.left) < 0;
743 ret |= drmModeAtomicAddProperty(pset, plane->id(),
744 plane->crtc_y_property().id(),
745 display_frame.top) < 0;
746 ret |= drmModeAtomicAddProperty(
747 pset, plane->id(), plane->crtc_w_property().id(),
748 display_frame.right - display_frame.left) < 0;
749 ret |= drmModeAtomicAddProperty(
750 pset, plane->id(), plane->crtc_h_property().id(),
751 display_frame.bottom - display_frame.top) < 0;
752 ret |= drmModeAtomicAddProperty(pset, plane->id(),
753 plane->src_x_property().id(),
754 (int)(source_crop.left) << 16) < 0;
755 ret |= drmModeAtomicAddProperty(pset, plane->id(),
756 plane->src_y_property().id(),
757 (int)(source_crop.top) << 16) < 0;
758 ret |= drmModeAtomicAddProperty(
759 pset, plane->id(), plane->src_w_property().id(),
760 (int)(source_crop.right - source_crop.left) << 16) < 0;
761 ret |= drmModeAtomicAddProperty(
762 pset, plane->id(), plane->src_h_property().id(),
763 (int)(source_crop.bottom - source_crop.top) << 16) < 0;
765 ALOGE("Failed to add plane %d to set", plane->id());
769 if (plane->rotation_property().id()) {
770 ret = drmModeAtomicAddProperty(pset, plane->id(),
771 plane->rotation_property().id(),
774 ALOGE("Failed to add rotation property %d to plane %d",
775 plane->rotation_property().id(), plane->id());
780 if (plane->alpha_property().id()) {
781 ret = drmModeAtomicAddProperty(pset, plane->id(),
782 plane->alpha_property().id(),
785 ALOGE("Failed to add alpha property %d to plane %d",
786 plane->alpha_property().id(), plane->id());
794 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
796 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
798 ret = drmModeAtomicCommit(drm_->fd(), pset, flags, drm_);
801 ALOGI("Commit test pset failed ret=%d\n", ret);
803 ALOGE("Failed to commit pset ret=%d\n", ret);
804 drmModeAtomicFree(pset);
809 drmModeAtomicFree(pset);
811 if (!test_only && mode_.needs_modeset) {
812 ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
814 ALOGE("Failed to destroy old mode property blob %" PRIu32 "/%d",
815 mode_.old_blob_id, ret);
819 /* TODO: Add dpms to the pset when the kernel supports it */
820 ret = ApplyDpms(display_comp);
822 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
826 connector->set_active_mode(mode_.mode);
827 mode_.old_blob_id = mode_.blob_id;
829 mode_.needs_modeset = false;
832 if (crtc->out_fence_ptr_property().id()) {
833 display_comp->set_out_fence((int) out_fences[crtc->pipe()]);
839 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
840 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
842 ALOGE("Failed to get DrmConnector for display %d", display_);
846 const DrmProperty &prop = conn->dpms_property();
847 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
848 display_comp->dpms_mode());
850 ALOGE("Failed to set DPMS property for connector %d", conn->id());
856 std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
857 const DrmMode &mode) {
858 struct drm_mode_modeinfo drm_mode;
859 memset(&drm_mode, 0, sizeof(drm_mode));
860 mode.ToDrmModeModeInfo(&drm_mode);
863 int ret = drm_->CreatePropertyBlob(&drm_mode,
864 sizeof(struct drm_mode_modeinfo), &id);
866 ALOGE("Failed to create mode property blob %d", ret);
867 return std::make_tuple(ret, 0);
869 ALOGE("Create blob_id %" PRIu32 "\n", id);
870 return std::make_tuple(ret, id);
873 void DrmDisplayCompositor::ClearDisplay() {
874 AutoLock lock(&lock_, "compositor");
875 int ret = lock.Lock();
879 if (!active_composition_)
882 if (DisablePlanes(active_composition_.get()))
885 active_composition_->SignalCompositionDone();
887 active_composition_.reset(NULL);
890 void DrmDisplayCompositor::ApplyFrame(
891 std::unique_ptr<DrmDisplayComposition> composition, int status) {
895 ret = CommitFrame(composition.get(), false);
898 ALOGE("Composite failed for display %d", display_);
899 // Disable the hw used by the last active composition. This allows us to
900 // signal the release fences from that composition to avoid hanging.
904 ++dump_frames_composited_;
906 if (active_composition_)
907 active_composition_->SignalCompositionDone();
909 ret = pthread_mutex_lock(&lock_);
911 ALOGE("Failed to acquire lock for active_composition swap");
913 active_composition_.swap(composition);
916 ret = pthread_mutex_unlock(&lock_);
918 ALOGE("Failed to release lock for active_composition swap");
921 int DrmDisplayCompositor::Composite() {
924 if (!pre_compositor_) {
925 pre_compositor_.reset(new GLWorkerCompositor());
926 int ret = pre_compositor_->Init();
928 ALOGE("Failed to initialize OpenGL compositor %d", ret);
933 int ret = pthread_mutex_lock(&lock_);
935 ALOGE("Failed to acquire compositor lock %d", ret);
938 if (composite_queue_.empty()) {
939 ret = pthread_mutex_unlock(&lock_);
941 ALOGE("Failed to release compositor lock %d", ret);
945 std::unique_ptr<DrmDisplayComposition> composition(
946 std::move(composite_queue_.front()));
948 composite_queue_.pop();
950 ret = pthread_mutex_unlock(&lock_);
952 ALOGE("Failed to release compositor lock %d", ret);
956 switch (composition->type()) {
957 case DRM_COMPOSITION_TYPE_FRAME:
958 ret = PrepareFrame(composition.get());
960 ALOGE("Failed to prepare frame for display %d", display_);
963 if (composition->geometry_changed()) {
964 // Send the composition to the kernel to ensure we can commit it. This
965 // is just a test, it won't actually commit the frame. If rejected,
966 // squash the frame into one layer and use the squashed composition
967 ret = CommitFrame(composition.get(), true);
969 ALOGI("Commit test failed, squashing frame for display %d", display_);
970 use_hw_overlays_ = !ret;
973 // If use_hw_overlays_ is false, we can't use hardware to composite the
974 // frame. So squash all layers into a single composition and queue that
976 if (!use_hw_overlays_) {
977 std::unique_ptr<DrmDisplayComposition> squashed = CreateComposition();
978 ret = SquashFrame(composition.get(), squashed.get());
980 composition = std::move(squashed);
982 ALOGE("Failed to squash frame for display %d", display_);
983 // Disable the hw used by the last active composition. This allows us
984 // to signal the release fences from that composition to avoid
990 frame_worker_.QueueFrame(std::move(composition), ret);
992 case DRM_COMPOSITION_TYPE_DPMS:
993 ret = ApplyDpms(composition.get());
995 ALOGE("Failed to apply dpms for display %d", display_);
997 case DRM_COMPOSITION_TYPE_MODESET:
998 mode_.mode = composition->display_mode();
1000 drm_->DestroyPropertyBlob(mode_.blob_id);
1001 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
1003 ALOGE("Failed to create mode blob for display %d", display_);
1006 mode_.needs_modeset = true;
1009 ALOGE("Unknown composition type %d", composition->type());
1016 bool DrmDisplayCompositor::HaveQueuedComposites() const {
1017 int ret = pthread_mutex_lock(&lock_);
1019 ALOGE("Failed to acquire compositor lock %d", ret);
1023 bool empty_ret = !composite_queue_.empty();
1025 ret = pthread_mutex_unlock(&lock_);
1027 ALOGE("Failed to release compositor lock %d", ret);
1034 int DrmDisplayCompositor::SquashAll() {
1035 AutoLock lock(&lock_, "compositor");
1036 int ret = lock.Lock();
1040 if (!active_composition_)
1043 std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
1044 ret = SquashFrame(active_composition_.get(), comp.get());
1046 // ApplyFrame needs the lock
1050 ApplyFrame(std::move(comp), 0);
1056 // - 0 if src is successfully squashed into dst
1057 // - -EALREADY if the src is already squashed
1058 // - Appropriate error if the squash fails
1059 int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
1060 DrmDisplayComposition *dst) {
1061 if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
1064 std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
1065 std::vector<DrmHwcLayer> &src_layers = src->layers();
1067 // Make sure there is more than one layer to squash.
1068 size_t src_planes_with_layer = std::count_if(
1069 src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
1070 return p.type() != DrmCompositionPlane::Type::kDisable;
1072 if (src_planes_with_layer <= 1)
1075 int pre_comp_layer_index;
1077 int ret = dst->Init(drm_, src->crtc(), src->importer(), src->planner(),
1080 ALOGE("Failed to init squash all composition %d", ret);
1084 DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kPrecomp, NULL,
1086 std::vector<DrmHwcLayer> dst_layers;
1087 for (DrmCompositionPlane &comp_plane : src_planes) {
1088 // Composition planes without DRM planes should never happen
1089 if (comp_plane.plane() == NULL) {
1090 ALOGE("Skipping squash all because of NULL plane");
1092 goto move_layers_back;
1095 if (comp_plane.type() == DrmCompositionPlane::Type::kDisable) {
1096 dst->AddPlaneDisable(comp_plane.plane());
1100 for (auto i : comp_plane.source_layers()) {
1101 DrmHwcLayer &layer = src_layers[i];
1103 // Squashing protected layers is impossible.
1104 if (layer.protected_usage()) {
1106 goto move_layers_back;
1109 // The OutputFds point to freed memory after hwc_set returns. They are
1110 // returned to the default to prevent DrmDisplayComposition::Plan from
1111 // filling the OutputFds.
1112 layer.release_fence = OutputFd();
1113 dst_layers.emplace_back(std::move(layer));
1114 squashed_comp.source_layers().push_back(
1115 squashed_comp.source_layers().size());
1118 if (comp_plane.plane()->type() == DRM_PLANE_TYPE_PRIMARY)
1119 squashed_comp.set_plane(comp_plane.plane());
1121 dst->AddPlaneDisable(comp_plane.plane());
1124 ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
1126 ALOGE("Failed to set layers for squash all composition %d", ret);
1127 goto move_layers_back;
1130 ret = dst->AddPlaneComposition(std::move(squashed_comp));
1132 ALOGE("Failed to add squashed plane composition %d", ret);
1133 goto move_layers_back;
1136 ret = dst->FinalizeComposition();
1138 ALOGE("Failed to plan for squash all composition %d", ret);
1139 goto move_layers_back;
1142 ret = ApplyPreComposite(dst);
1144 ALOGE("Failed to pre-composite for squash all composition %d", ret);
1145 goto move_layers_back;
1148 pre_comp_layer_index = dst->layers().size() - 1;
1149 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
1151 for (DrmCompositionPlane &plane : dst->composition_planes()) {
1152 if (plane.type() == DrmCompositionPlane::Type::kPrecomp) {
1153 // Replace source_layers with the output of the precomposite
1154 plane.source_layers().clear();
1155 plane.source_layers().push_back(pre_comp_layer_index);
1162 // TODO(zachr): think of a better way to transfer ownership back to the active
1165 for (size_t plane_index = 0;
1166 plane_index < src_planes.size() && plane_index < dst_layers.size();) {
1167 if (src_planes[plane_index].source_layers().empty()) {
1171 for (auto i : src_planes[plane_index].source_layers())
1172 src_layers[i] = std::move(dst_layers[plane_index++]);
1178 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
1179 int ret = pthread_mutex_lock(&lock_);
1183 uint64_t num_frames = dump_frames_composited_;
1184 dump_frames_composited_ = 0;
1187 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1189 pthread_mutex_unlock(&lock_);
1193 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1194 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1195 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1197 *out << "--DrmDisplayCompositor[" << display_
1198 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1199 << " fps=" << fps << "\n";
1201 dump_last_timestamp_ns_ = cur_ts;
1203 if (active_composition_)
1204 active_composition_->Dump(out);
1206 squash_state_.Dump(out);
1208 pthread_mutex_unlock(&lock_);