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.source_layer ==
176 DrmCompositionPlane::kSourceSquash;
180 DrmDisplayCompositor::FrameWorker::FrameWorker(DrmDisplayCompositor *compositor)
181 : Worker("frame-worker", HAL_PRIORITY_URGENT_DISPLAY),
182 compositor_(compositor) {
185 DrmDisplayCompositor::FrameWorker::~FrameWorker() {
188 int DrmDisplayCompositor::FrameWorker::Init() {
192 void DrmDisplayCompositor::FrameWorker::QueueFrame(
193 std::unique_ptr<DrmDisplayComposition> composition, int status) {
196 frame.composition = std::move(composition);
197 frame.status = status;
198 frame_queue_.push(std::move(frame));
203 void DrmDisplayCompositor::FrameWorker::Routine() {
206 ALOGE("Failed to lock worker, %d", ret);
211 if (frame_queue_.empty()) {
212 wait_ret = WaitForSignalOrExitLocked();
216 if (!frame_queue_.empty()) {
217 frame = std::move(frame_queue_.front());
223 ALOGE("Failed to unlock worker, %d", ret);
227 if (wait_ret == -EINTR) {
229 } else if (wait_ret) {
230 ALOGE("Failed to wait for signal, %d", wait_ret);
234 compositor_->ApplyFrame(std::move(frame.composition), frame.status);
237 DrmDisplayCompositor::DrmDisplayCompositor()
244 use_hw_overlays_(true),
245 framebuffer_index_(0),
246 squash_framebuffer_index_(0),
247 dump_frames_composited_(0),
248 dump_last_timestamp_ns_(0) {
250 if (clock_gettime(CLOCK_MONOTONIC, &ts))
252 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
255 DrmDisplayCompositor::~DrmDisplayCompositor() {
260 frame_worker_.Exit();
262 int ret = pthread_mutex_lock(&lock_);
264 ALOGE("Failed to acquire compositor lock %d", ret);
267 drm_->DestroyPropertyBlob(mode_.blob_id);
268 if (mode_.old_blob_id)
269 drm_->DestroyPropertyBlob(mode_.old_blob_id);
271 while (!composite_queue_.empty()) {
272 composite_queue_.front().reset();
273 composite_queue_.pop();
275 active_composition_.reset();
277 ret = pthread_mutex_unlock(&lock_);
279 ALOGE("Failed to acquire compositor lock %d", ret);
281 pthread_mutex_destroy(&lock_);
284 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
288 int ret = pthread_mutex_init(&lock_, NULL);
290 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
293 ret = worker_.Init();
295 pthread_mutex_destroy(&lock_);
296 ALOGE("Failed to initialize compositor worker %d\n", ret);
299 ret = frame_worker_.Init();
301 pthread_mutex_destroy(&lock_);
302 ALOGE("Failed to initialize frame worker %d\n", ret);
310 std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
312 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
315 int DrmDisplayCompositor::QueueComposition(
316 std::unique_ptr<DrmDisplayComposition> composition) {
317 switch (composition->type()) {
318 case DRM_COMPOSITION_TYPE_FRAME:
322 case DRM_COMPOSITION_TYPE_DPMS:
324 * Update the state as soon as we get it so we can start/stop queuing
327 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
329 case DRM_COMPOSITION_TYPE_MODESET:
331 case DRM_COMPOSITION_TYPE_EMPTY:
334 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
338 int ret = pthread_mutex_lock(&lock_);
340 ALOGE("Failed to acquire compositor lock %d", ret);
344 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
345 // to eat our buffer handles when we get about 1 second behind.
346 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
347 pthread_mutex_unlock(&lock_);
349 pthread_mutex_lock(&lock_);
352 composite_queue_.push(std::move(composition));
354 ret = pthread_mutex_unlock(&lock_);
356 ALOGE("Failed to release compositor lock %d", ret);
364 std::tuple<uint32_t, uint32_t, int>
365 DrmDisplayCompositor::GetActiveModeResolution() {
366 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
367 if (connector == NULL) {
368 ALOGE("Failed to determine display mode: no connector for display %d",
370 return std::make_tuple(0, 0, -ENODEV);
373 const DrmMode &mode = connector->active_mode();
374 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
377 int DrmDisplayCompositor::PrepareFramebuffer(
378 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
379 int ret = fb.WaitReleased(-1);
381 ALOGE("Failed to wait for framebuffer release %d", ret);
384 uint32_t width, height;
385 std::tie(width, height, ret) = GetActiveModeResolution();
388 "Failed to allocate framebuffer because the display resolution could "
389 "not be determined %d",
394 fb.set_release_fence_fd(-1);
395 if (!fb.Allocate(width, height)) {
396 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
400 display_comp->layers().emplace_back();
401 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
402 pre_comp_layer.sf_handle = fb.buffer()->handle;
403 pre_comp_layer.blending = DrmHwcBlending::kPreMult;
404 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
405 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
406 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
407 display_comp->importer());
409 ALOGE("Failed to import framebuffer for display %d", ret);
416 int DrmDisplayCompositor::ApplySquash(DrmDisplayComposition *display_comp) {
419 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
420 ret = PrepareFramebuffer(fb, display_comp);
422 ALOGE("Failed to prepare framebuffer for squash %d", ret);
426 std::vector<DrmCompositionRegion> ®ions = display_comp->squash_regions();
427 ret = pre_compositor_->Composite(display_comp->layers().data(),
428 regions.data(), regions.size(), fb.buffer());
429 pre_compositor_->Finish();
432 ALOGE("Failed to squash layers");
436 ret = display_comp->CreateNextTimelineFence();
438 ALOGE("Failed to create squash framebuffer release fence %d", ret);
442 fb.set_release_fence_fd(ret);
443 display_comp->SignalSquashDone();
448 int DrmDisplayCompositor::ApplyPreComposite(
449 DrmDisplayComposition *display_comp) {
452 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
453 ret = PrepareFramebuffer(fb, display_comp);
455 ALOGE("Failed to prepare framebuffer for pre-composite %d", ret);
459 std::vector<DrmCompositionRegion> ®ions = display_comp->pre_comp_regions();
460 ret = pre_compositor_->Composite(display_comp->layers().data(),
461 regions.data(), regions.size(), fb.buffer());
462 pre_compositor_->Finish();
465 ALOGE("Failed to pre-composite layers");
469 ret = display_comp->CreateNextTimelineFence();
471 ALOGE("Failed to create pre-composite framebuffer release fence %d", ret);
475 fb.set_release_fence_fd(ret);
476 display_comp->SignalPreCompDone();
481 int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
482 drmModePropertySetPtr pset = drmModePropertySetAlloc();
484 ALOGE("Failed to allocate property set");
489 std::vector<DrmCompositionPlane> &comp_planes =
490 display_comp->composition_planes();
491 for (DrmCompositionPlane &comp_plane : comp_planes) {
492 DrmPlane *plane = comp_plane.plane;
494 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
496 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), 0);
498 ALOGE("Failed to add plane %d disable to pset", plane->id());
499 drmModePropertySetFree(pset);
504 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
506 ALOGE("Failed to commit pset ret=%d\n", ret);
507 drmModePropertySetFree(pset);
511 drmModePropertySetFree(pset);
515 int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) {
518 std::vector<DrmHwcLayer> &layers = display_comp->layers();
519 std::vector<DrmCompositionPlane> &comp_planes =
520 display_comp->composition_planes();
521 std::vector<DrmCompositionRegion> &squash_regions =
522 display_comp->squash_regions();
523 std::vector<DrmCompositionRegion> &pre_comp_regions =
524 display_comp->pre_comp_regions();
526 int squash_layer_index = -1;
527 if (squash_regions.size() > 0) {
528 squash_framebuffer_index_ = (squash_framebuffer_index_ + 1) % 2;
529 ret = ApplySquash(display_comp);
533 squash_layer_index = layers.size() - 1;
535 if (UsesSquash(comp_planes)) {
536 DrmFramebuffer &fb = squash_framebuffers_[squash_framebuffer_index_];
537 layers.emplace_back();
538 squash_layer_index = layers.size() - 1;
539 DrmHwcLayer &squash_layer = layers.back();
540 ret = squash_layer.buffer.ImportBuffer(fb.buffer()->handle,
541 display_comp->importer());
543 ALOGE("Failed to import old squashed framebuffer %d", ret);
546 squash_layer.sf_handle = fb.buffer()->handle;
547 squash_layer.blending = DrmHwcBlending::kPreMult;
548 squash_layer.source_crop = DrmHwcRect<float>(
549 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
550 squash_layer.display_frame = DrmHwcRect<int>(
551 0, 0, squash_layer.buffer->width, squash_layer.buffer->height);
552 ret = display_comp->CreateNextTimelineFence();
555 ALOGE("Failed to create squash framebuffer release fence %d", ret);
559 fb.set_release_fence_fd(ret);
564 bool do_pre_comp = pre_comp_regions.size() > 0;
565 int pre_comp_layer_index = -1;
567 ret = ApplyPreComposite(display_comp);
571 pre_comp_layer_index = layers.size() - 1;
572 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
575 for (DrmCompositionPlane &comp_plane : comp_planes) {
576 switch (comp_plane.source_layer) {
577 case DrmCompositionPlane::kSourceSquash:
578 comp_plane.source_layer = squash_layer_index;
580 case DrmCompositionPlane::kSourcePreComp:
583 "Can not use pre composite framebuffer with no pre composite "
587 comp_plane.source_layer = pre_comp_layer_index;
597 int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp,
603 std::vector<DrmHwcLayer> &layers = display_comp->layers();
604 std::vector<DrmCompositionPlane> &comp_planes =
605 display_comp->composition_planes();
606 std::vector<DrmCompositionRegion> &pre_comp_regions =
607 display_comp->pre_comp_regions();
609 DrmFramebuffer *pre_comp_fb;
611 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
613 ALOGE("Could not locate connector for display %d", display_);
616 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
618 ALOGE("Could not locate crtc for display %d", display_);
622 drmModePropertySetPtr pset = drmModePropertySetAlloc();
624 ALOGE("Failed to allocate property set");
628 if (mode_.needs_modeset) {
629 ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
631 drmModePropertySetAdd(pset, connector->id(),
632 connector->crtc_id_property().id(), crtc->id());
634 ALOGE("Failed to add blob %d to pset", mode_.blob_id);
635 drmModePropertySetFree(pset);
640 for (DrmCompositionPlane &comp_plane : comp_planes) {
641 DrmPlane *plane = comp_plane.plane;
642 DrmCrtc *crtc = comp_plane.crtc;
645 DrmHwcRect<int> display_frame;
646 DrmHwcRect<float> source_crop;
647 uint64_t rotation = 0;
648 uint64_t alpha = 0xFF;
649 switch (comp_plane.source_layer) {
650 case DrmCompositionPlane::kSourceNone:
652 case DrmCompositionPlane::kSourceSquash:
653 ALOGE("Actual source layer index expected for squash layer");
655 case DrmCompositionPlane::kSourcePreComp:
656 ALOGE("Actual source layer index expected for pre-comp layer");
659 if (comp_plane.source_layer >= layers.size()) {
660 ALOGE("Source layer index %zu out of bounds %zu",
661 comp_plane.source_layer, layers.size());
664 DrmHwcLayer &layer = layers[comp_plane.source_layer];
665 if (!test_only && layer.acquire_fence.get() >= 0) {
666 int acquire_fence = layer.acquire_fence.get();
667 for (int i = 0; i < kAcquireWaitTries; ++i) {
668 ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs);
670 ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
671 acquire_fence, i, ret, (i + 1) * kAcquireWaitTimeoutMs);
674 ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
677 layer.acquire_fence.Close();
680 ALOGE("Expected a valid framebuffer for pset");
683 fb_id = layer.buffer->fb_id;
684 display_frame = layer.display_frame;
685 source_crop = layer.source_crop;
686 if (layer.blending == DrmHwcBlending::kPreMult)
690 if (layer.transform & DrmHwcTransform::kFlipH)
691 rotation |= 1 << DRM_REFLECT_X;
692 if (layer.transform & DrmHwcTransform::kFlipV)
693 rotation |= 1 << DRM_REFLECT_Y;
694 if (layer.transform & DrmHwcTransform::kRotate90)
695 rotation |= 1 << DRM_ROTATE_90;
696 else if (layer.transform & DrmHwcTransform::kRotate180)
697 rotation |= 1 << DRM_ROTATE_180;
698 else if (layer.transform & DrmHwcTransform::kRotate270)
699 rotation |= 1 << DRM_ROTATE_270;
703 // Disable the plane if there's no framebuffer
705 ret = drmModePropertySetAdd(pset, plane->id(),
706 plane->crtc_property().id(), 0) ||
707 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
710 ALOGE("Failed to add plane %d disable to pset", plane->id());
716 // TODO: Once we have atomic test, this should fall back to GL
717 if (rotation && plane->rotation_property().id() == 0) {
718 ALOGE("Rotation is not supported on plane %d", plane->id());
723 // TODO: Once we have atomic test, this should fall back to GL
724 if (alpha != 0xFF && plane->alpha_property().id() == 0) {
725 ALOGE("Alpha is not supported on plane %d", plane->id());
731 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
733 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
735 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
736 display_frame.left) ||
737 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
738 display_frame.top) ||
739 drmModePropertySetAdd(pset, plane->id(), plane->crtc_w_property().id(),
740 display_frame.right - display_frame.left) ||
741 drmModePropertySetAdd(pset, plane->id(), plane->crtc_h_property().id(),
742 display_frame.bottom - display_frame.top) ||
743 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
744 (int)(source_crop.left) << 16) ||
745 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
746 (int)(source_crop.top) << 16) ||
747 drmModePropertySetAdd(pset, plane->id(), plane->src_w_property().id(),
748 (int)(source_crop.right - source_crop.left)
750 drmModePropertySetAdd(pset, plane->id(), plane->src_h_property().id(),
751 (int)(source_crop.bottom - source_crop.top)
754 ALOGE("Failed to add plane %d to set", plane->id());
758 if (plane->rotation_property().id()) {
759 ret = drmModePropertySetAdd(pset, plane->id(),
760 plane->rotation_property().id(), rotation);
762 ALOGE("Failed to add rotation property %d to plane %d",
763 plane->rotation_property().id(), plane->id());
768 if (plane->alpha_property().id()) {
769 ret = drmModePropertySetAdd(pset, plane->id(),
770 plane->alpha_property().id(), alpha);
772 ALOGE("Failed to add alpha property %d to plane %d",
773 plane->alpha_property().id(), plane->id());
781 uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
783 flags |= DRM_MODE_ATOMIC_TEST_ONLY;
785 ret = drmModePropertySetCommit(drm_->fd(), flags, drm_, pset);
788 ALOGI("Commit test pset failed ret=%d\n", ret);
790 ALOGE("Failed to commit pset ret=%d\n", ret);
791 drmModePropertySetFree(pset);
796 drmModePropertySetFree(pset);
798 if (!test_only && mode_.needs_modeset) {
799 ret = drm_->DestroyPropertyBlob(mode_.old_blob_id);
801 ALOGE("Failed to destroy old mode property blob %lld/%d",
802 mode_.old_blob_id, ret);
806 /* TODO: Add dpms to the pset when the kernel supports it */
807 ret = ApplyDpms(display_comp);
809 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
813 connector->set_active_mode(mode_.mode);
814 mode_.old_blob_id = mode_.blob_id;
816 mode_.needs_modeset = false;
822 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
823 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
825 ALOGE("Failed to get DrmConnector for display %d", display_);
829 const DrmProperty &prop = conn->dpms_property();
830 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
831 display_comp->dpms_mode());
833 ALOGE("Failed to set DPMS property for connector %d", conn->id());
839 std::tuple<int, uint32_t> DrmDisplayCompositor::CreateModeBlob(
840 const DrmMode &mode) {
841 struct drm_mode_modeinfo drm_mode;
842 memset(&drm_mode, 0, sizeof(drm_mode));
843 mode.ToDrmModeModeInfo(&drm_mode);
846 int ret = drm_->CreatePropertyBlob(&drm_mode,
847 sizeof(struct drm_mode_modeinfo), &id);
849 ALOGE("Failed to create mode property blob %d", ret);
850 return std::make_tuple(ret, 0);
852 ALOGE("Create blob_id %ld\n", id);
853 return std::make_tuple(ret, id);
856 void DrmDisplayCompositor::ApplyFrame(
857 std::unique_ptr<DrmDisplayComposition> composition, int status) {
861 ret = CommitFrame(composition.get(), false);
864 ALOGE("Composite failed for display %d", display_);
866 // Disable the hw used by the last active composition. This allows us to
867 // signal the release fences from that composition to avoid hanging.
868 if (DisablePlanes(active_composition_.get()))
871 ++dump_frames_composited_;
873 if (active_composition_)
874 active_composition_->SignalCompositionDone();
876 ret = pthread_mutex_lock(&lock_);
878 ALOGE("Failed to acquire lock for active_composition swap");
880 active_composition_.swap(composition);
883 ret = pthread_mutex_unlock(&lock_);
885 ALOGE("Failed to release lock for active_composition swap");
888 int DrmDisplayCompositor::Composite() {
891 if (!pre_compositor_) {
892 pre_compositor_.reset(new GLWorkerCompositor());
893 int ret = pre_compositor_->Init();
895 ALOGE("Failed to initialize OpenGL compositor %d", ret);
900 int ret = pthread_mutex_lock(&lock_);
902 ALOGE("Failed to acquire compositor lock %d", ret);
905 if (composite_queue_.empty()) {
906 ret = pthread_mutex_unlock(&lock_);
908 ALOGE("Failed to release compositor lock %d", ret);
912 std::unique_ptr<DrmDisplayComposition> composition(
913 std::move(composite_queue_.front()));
915 composite_queue_.pop();
917 ret = pthread_mutex_unlock(&lock_);
919 ALOGE("Failed to release compositor lock %d", ret);
923 switch (composition->type()) {
924 case DRM_COMPOSITION_TYPE_FRAME:
925 ret = PrepareFrame(composition.get());
927 ALOGE("Failed to prepare frame for display %d", display_);
930 if (composition->geometry_changed()) {
931 // Send the composition to the kernel to ensure we can commit it. This
932 // is just a test, it won't actually commit the frame. If rejected,
933 // squash the frame into one layer and use the squashed composition
934 ret = CommitFrame(composition.get(), true);
936 ALOGI("Commit test failed, squashing frame for display %d", display_);
937 use_hw_overlays_ = !ret;
940 // If use_hw_overlays_ is false, we can't use hardware to composite the
941 // frame. So squash all layers into a single composition and queue that
943 if (!use_hw_overlays_) {
944 std::unique_ptr<DrmDisplayComposition> squashed = CreateComposition();
945 ret = SquashFrame(composition.get(), squashed.get());
947 composition = std::move(squashed);
949 ALOGE("Failed to squash frame for display %d", display_);
953 frame_worker_.QueueFrame(std::move(composition), ret);
955 case DRM_COMPOSITION_TYPE_DPMS:
956 ret = ApplyDpms(composition.get());
958 ALOGE("Failed to apply dpms for display %d", display_);
960 case DRM_COMPOSITION_TYPE_MODESET:
961 mode_.mode = composition->display_mode();
963 drm_->DestroyPropertyBlob(mode_.blob_id);
964 std::tie(ret, mode_.blob_id) = CreateModeBlob(mode_.mode);
966 ALOGE("Failed to create mode blob for display %d", display_);
969 mode_.needs_modeset = true;
972 ALOGE("Unknown composition type %d", composition->type());
979 bool DrmDisplayCompositor::HaveQueuedComposites() const {
980 int ret = pthread_mutex_lock(&lock_);
982 ALOGE("Failed to acquire compositor lock %d", ret);
986 bool empty_ret = !composite_queue_.empty();
988 ret = pthread_mutex_unlock(&lock_);
990 ALOGE("Failed to release compositor lock %d", ret);
997 int DrmDisplayCompositor::SquashAll() {
998 AutoLock lock(&lock_, "compositor");
999 int ret = lock.Lock();
1003 if (!active_composition_)
1006 std::unique_ptr<DrmDisplayComposition> comp = CreateComposition();
1007 ret = SquashFrame(active_composition_.get(), comp.get());
1009 // ApplyFrame needs the lock
1013 ApplyFrame(std::move(comp), 0);
1019 // - 0 if src is successfully squashed into dst
1020 // - -EALREADY if the src is already squashed
1021 // - Appropriate error if the squash fails
1022 int DrmDisplayCompositor::SquashFrame(DrmDisplayComposition *src,
1023 DrmDisplayComposition *dst) {
1024 if (src->type() != DRM_COMPOSITION_TYPE_FRAME)
1027 std::vector<DrmCompositionPlane> &src_planes = src->composition_planes();
1028 std::vector<DrmHwcLayer> &src_layers = src->layers();
1030 // Make sure there is more than one layer to squash.
1031 size_t src_planes_with_layer = std::count_if(
1032 src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) {
1033 return p.source_layer <= DrmCompositionPlane::kSourceLayerMax;
1035 if (src_planes_with_layer <= 1)
1038 int pre_comp_layer_index;
1040 int ret = dst->Init(drm_, src->crtc(), src->importer(), src->frame_no());
1042 ALOGE("Failed to init squash all composition %d", ret);
1046 std::vector<DrmPlane *> primary_planes;
1047 std::vector<DrmPlane *> fake_overlay_planes;
1048 std::vector<DrmHwcLayer> dst_layers;
1049 for (DrmCompositionPlane &comp_plane : src_planes) {
1050 // Composition planes without DRM planes should never happen
1051 if (comp_plane.plane == NULL) {
1052 ALOGE("Skipping squash all because of NULL plane");
1054 goto move_layers_back;
1057 if (comp_plane.source_layer == DrmCompositionPlane::kSourceNone)
1060 // Out of range layers should never happen. If they do, somebody probably
1061 // forgot to replace the symbolic names (kSourceSquash, kSourcePreComp) with
1063 if (comp_plane.source_layer >= src_layers.size()) {
1064 ALOGE("Skipping squash all because of out of range source layer %zu",
1065 comp_plane.source_layer);
1067 goto move_layers_back;
1070 DrmHwcLayer &layer = src_layers[comp_plane.source_layer];
1072 // Squashing protected layers is impossible.
1073 if (layer.protected_usage()) {
1075 goto move_layers_back;
1078 // The OutputFds point to freed memory after hwc_set returns. They are
1079 // returned to the default to prevent DrmDisplayComposition::Plan from
1080 // filling the OutputFds.
1081 layer.release_fence = OutputFd();
1082 dst_layers.emplace_back(std::move(layer));
1084 if (comp_plane.plane->type() == DRM_PLANE_TYPE_PRIMARY &&
1085 primary_planes.size() == 0)
1086 primary_planes.push_back(comp_plane.plane);
1088 dst->AddPlaneDisable(comp_plane.plane);
1091 ret = dst->SetLayers(dst_layers.data(), dst_layers.size(), false);
1093 ALOGE("Failed to set layers for squash all composition %d", ret);
1094 goto move_layers_back;
1098 dst->Plan(NULL /* SquashState */, &primary_planes, &fake_overlay_planes);
1100 ALOGE("Failed to plan for squash all composition %d", ret);
1101 goto move_layers_back;
1104 ret = ApplyPreComposite(dst);
1106 ALOGE("Failed to pre-composite for squash all composition %d", ret);
1107 goto move_layers_back;
1110 pre_comp_layer_index = dst->layers().size() - 1;
1111 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
1113 for (DrmCompositionPlane &plane : dst->composition_planes())
1114 if (plane.source_layer == DrmCompositionPlane::kSourcePreComp)
1115 plane.source_layer = pre_comp_layer_index;
1119 // TODO(zachr): think of a better way to transfer ownership back to the active
1122 for (size_t plane_index = 0;
1123 plane_index < src_planes.size() && plane_index < dst_layers.size();
1125 size_t source_layer_index = src_planes[plane_index].source_layer;
1126 src_layers[source_layer_index] = std::move(dst_layers[plane_index]);
1132 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {
1133 int ret = pthread_mutex_lock(&lock_);
1137 uint64_t num_frames = dump_frames_composited_;
1138 dump_frames_composited_ = 0;
1141 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
1143 pthread_mutex_unlock(&lock_);
1147 uint64_t cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
1148 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
1149 float fps = num_ms ? (num_frames * 1000.0f) / (num_ms) : 0.0f;
1151 *out << "--DrmDisplayCompositor[" << display_
1152 << "]: num_frames=" << num_frames << " num_ms=" << num_ms
1153 << " fps=" << fps << "\n";
1155 dump_last_timestamp_ns_ = cur_ts;
1157 if (active_composition_)
1158 active_composition_->Dump(out);
1160 squash_state_.Dump(out);
1162 pthread_mutex_unlock(&lock_);