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 DrmDisplayCompositor::DrmDisplayCompositor()
118 needs_modeset_(false),
119 framebuffer_index_(0),
120 dump_frames_composited_(0),
121 dump_last_timestamp_ns_(0) {
123 if (clock_gettime(CLOCK_MONOTONIC, &ts))
125 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
128 DrmDisplayCompositor::~DrmDisplayCompositor() {
134 int ret = pthread_mutex_lock(&lock_);
136 ALOGE("Failed to acquire compositor lock %d", ret);
138 while (!composite_queue_.empty()) {
139 composite_queue_.front().reset();
140 composite_queue_.pop();
142 active_composition_.reset();
144 ret = pthread_mutex_unlock(&lock_);
146 ALOGE("Failed to acquire compositor lock %d", ret);
148 pthread_mutex_destroy(&lock_);
151 int DrmDisplayCompositor::Init(DrmResources *drm, int display) {
155 int ret = pthread_mutex_init(&lock_, NULL);
157 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
160 ret = worker_.Init();
162 pthread_mutex_destroy(&lock_);
163 ALOGE("Failed to initialize compositor worker %d\n", ret);
171 std::unique_ptr<DrmDisplayComposition> DrmDisplayCompositor::CreateComposition()
173 return std::unique_ptr<DrmDisplayComposition>(new DrmDisplayComposition());
176 int DrmDisplayCompositor::QueueComposition(
177 std::unique_ptr<DrmDisplayComposition> composition) {
178 switch (composition->type()) {
179 case DRM_COMPOSITION_TYPE_FRAME:
183 case DRM_COMPOSITION_TYPE_DPMS:
185 * Update the state as soon as we get it so we can start/stop queuing
188 active_ = (composition->dpms_mode() == DRM_MODE_DPMS_ON);
190 case DRM_COMPOSITION_TYPE_MODESET:
192 case DRM_COMPOSITION_TYPE_EMPTY:
195 ALOGE("Unknown composition type %d/%d", composition->type(), display_);
199 int ret = pthread_mutex_lock(&lock_);
201 ALOGE("Failed to acquire compositor lock %d", ret);
205 // Block the queue if it gets too large. Otherwise, SurfaceFlinger will start
206 // to eat our buffer handles when we get about 1 second behind.
207 while (composite_queue_.size() >= DRM_DISPLAY_COMPOSITOR_MAX_QUEUE_DEPTH) {
208 pthread_mutex_unlock(&lock_);
210 pthread_mutex_lock(&lock_);
213 composite_queue_.push(std::move(composition));
215 ret = pthread_mutex_unlock(&lock_);
217 ALOGE("Failed to release compositor lock %d", ret);
225 std::tuple<uint32_t, uint32_t, int>
226 DrmDisplayCompositor::GetActiveModeResolution() {
227 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
228 if (connector == NULL) {
229 ALOGE("Failed to determine display mode: no connector for display %d",
231 return std::make_tuple(0, 0, -ENODEV);
234 const DrmMode &mode = connector->active_mode();
235 return std::make_tuple(mode.h_display(), mode.v_display(), 0);
238 int DrmDisplayCompositor::PrepareFramebuffer(
239 DrmFramebuffer &fb, DrmDisplayComposition *display_comp) {
240 int ret = fb.WaitReleased(-1);
242 ALOGE("Failed to wait for framebuffer release %d", ret);
245 uint32_t width, height;
246 std::tie(width, height, ret) = GetActiveModeResolution();
249 "Failed to allocate framebuffer because the display resolution could "
250 "not be determined %d",
255 fb.set_release_fence_fd(-1);
256 if (!fb.Allocate(width, height)) {
257 ALOGE("Failed to allocate framebuffer with size %dx%d", width, height);
261 display_comp->layers().emplace_back();
262 DrmHwcLayer &pre_comp_layer = display_comp->layers().back();
263 pre_comp_layer.sf_handle = fb.buffer()->handle;
264 pre_comp_layer.source_crop = DrmHwcRect<float>(0, 0, width, height);
265 pre_comp_layer.display_frame = DrmHwcRect<int>(0, 0, width, height);
266 ret = pre_comp_layer.buffer.ImportBuffer(fb.buffer()->handle,
267 display_comp->importer());
269 ALOGE("Failed to import framebuffer for display %d", ret);
276 int DrmDisplayCompositor::ApplyPreComposite(
277 DrmDisplayComposition *display_comp) {
280 DrmFramebuffer &fb = framebuffers_[framebuffer_index_];
281 ret = PrepareFramebuffer(fb, display_comp);
283 ALOGE("Failed to prepare framebuffer for precomposite %d", ret);
287 std::vector<DrmCompositionRegion> ®ions = display_comp->pre_comp_regions();
288 ret = pre_compositor_->Composite(display_comp->layers().data(),
289 regions.data(), regions.size(), fb.buffer());
290 pre_compositor_->Finish();
293 ALOGE("Failed to composite layers");
297 ret = display_comp->CreateNextTimelineFence();
299 ALOGE("Failed to create pre comp framebuffer release fence %d", ret);
303 fb.set_release_fence_fd(ret);
304 display_comp->SignalPreCompDone();
309 int DrmDisplayCompositor::DisablePlanes(DrmDisplayComposition *display_comp) {
310 drmModePropertySetPtr pset = drmModePropertySetAlloc();
312 ALOGE("Failed to allocate property set");
317 std::vector<DrmCompositionPlane> &comp_planes =
318 display_comp->composition_planes();
319 for (DrmCompositionPlane &comp_plane : comp_planes) {
320 DrmPlane *plane = comp_plane.plane;
322 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
324 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(), 0);
326 ALOGE("Failed to add plane %d disable to pset", plane->id());
327 drmModePropertySetFree(pset);
332 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
334 ALOGE("Failed to commit pset ret=%d\n", ret);
335 drmModePropertySetFree(pset);
339 drmModePropertySetFree(pset);
343 int DrmDisplayCompositor::ApplyFrame(DrmDisplayComposition *display_comp) {
346 std::vector<DrmHwcLayer> &layers = display_comp->layers();
347 std::vector<DrmCompositionPlane> &comp_planes =
348 display_comp->composition_planes();
349 std::vector<DrmCompositionRegion> &pre_comp_regions =
350 display_comp->pre_comp_regions();
352 bool do_pre_comp = pre_comp_regions.size() > 0;
353 DrmFramebuffer *pre_comp_fb;
354 int pre_comp_layer_index = -1;
357 ret = ApplyPreComposite(display_comp);
361 pre_comp_layer_index = layers.size() - 1;
362 framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS;
365 DrmConnector *connector = drm_->GetConnectorForDisplay(display_);
367 ALOGE("Could not locate connector for display %d", display_);
370 DrmCrtc *crtc = drm_->GetCrtcForDisplay(display_);
372 ALOGE("Could not locate crtc for display %d", display_);
376 drmModePropertySetPtr pset = drmModePropertySetAlloc();
378 ALOGE("Failed to allocate property set");
382 uint32_t blob_id = 0;
383 uint64_t old_blob_id;
384 if (needs_modeset_) {
385 DrmProperty old_mode;
386 ret = drm_->GetCrtcProperty(*crtc, crtc->mode_property().name().c_str(),
389 ALOGE("Failed to get old mode property from crtc %d", crtc->id());
390 drmModePropertySetFree(pset);
393 ret = old_mode.value(&old_blob_id);
395 ALOGE("Could not get old blob id value %d", ret);
396 drmModePropertySetFree(pset);
400 struct drm_mode_modeinfo drm_mode;
401 memset(&drm_mode, 0, sizeof(drm_mode));
402 next_mode_.ToDrmModeModeInfo(&drm_mode);
404 ret = drm_->CreatePropertyBlob(&drm_mode, sizeof(struct drm_mode_modeinfo),
407 ALOGE("Failed to create mode property blob %d", ret);
408 drmModePropertySetFree(pset);
412 ret = drmModePropertySetAdd(pset, crtc->id(), crtc->mode_property().id(),
414 drmModePropertySetAdd(pset, connector->id(),
415 connector->crtc_id_property().id(), crtc->id());
417 ALOGE("Failed to add blob %d to pset", blob_id);
418 drmModePropertySetFree(pset);
419 drm_->DestroyPropertyBlob(blob_id);
424 for (DrmCompositionPlane &comp_plane : comp_planes) {
425 DrmPlane *plane = comp_plane.plane;
426 DrmCrtc *crtc = comp_plane.crtc;
429 DrmHwcRect<int> display_frame;
430 DrmHwcRect<float> source_crop;
431 uint64_t rotation = 0;
432 switch (comp_plane.source_layer) {
433 case DrmCompositionPlane::kSourceNone:
435 case DrmCompositionPlane::kSourcePreComp: {
438 "Can not use pre composite framebuffer with no pre composite "
443 DrmHwcLayer &layer = layers[pre_comp_layer_index];
444 fb_id = layer.buffer->fb_id;
445 display_frame = layer.display_frame;
446 source_crop = layer.source_crop;
448 case DrmCompositionPlane::kSourceSquash:
451 if (comp_plane.source_layer >= layers.size()) {
452 ALOGE("Source layer index %zu out of bounds %zu",
453 comp_plane.source_layer, layers.size());
456 DrmHwcLayer &layer = layers[comp_plane.source_layer];
457 if (layer.acquire_fence.get() >= 0) {
458 int acquire_fence = layer.acquire_fence.get();
459 for (int i = 0; i < kAcquireWaitTries; ++i) {
460 ret = sync_wait(acquire_fence, kAcquireWaitTimeoutMs);
462 ALOGW("Acquire fence %d wait %d failed (%d). Total time %d",
463 acquire_fence, i, ret, (i + 1) * kAcquireWaitTimeoutMs);
466 ALOGE("Failed to wait for acquire %d/%d", acquire_fence, ret);
469 layer.acquire_fence.Close();
472 ALOGE("Expected a valid framebuffer for pset");
475 fb_id = layer.buffer->fb_id;
476 display_frame = layer.display_frame;
477 source_crop = layer.source_crop;
478 switch (layer.transform) {
479 case DrmHwcTransform::kFlipH:
480 rotation = 1 << DRM_REFLECT_X;
482 case DrmHwcTransform::kFlipV:
483 rotation = 1 << DRM_REFLECT_Y;
485 case DrmHwcTransform::kRotate90:
486 rotation = 1 << DRM_ROTATE_90;
488 case DrmHwcTransform::kRotate180:
489 rotation = 1 << DRM_ROTATE_180;
491 case DrmHwcTransform::kRotate270:
492 rotation = 1 << DRM_ROTATE_270;
494 case DrmHwcTransform::kIdentity:
498 ALOGE("Invalid transform value 0x%x given", layer.transform);
504 // Disable the plane if there's no framebuffer
506 ret = drmModePropertySetAdd(pset, plane->id(),
507 plane->crtc_property().id(), 0) ||
508 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
511 ALOGE("Failed to add plane %d disable to pset", plane->id());
517 // TODO: Once we have atomic test, this should fall back to GL
518 if (rotation && plane->rotation_property().id() == 0) {
519 ALOGE("Rotation is not supported on plane %d", plane->id());
525 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
527 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
529 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
530 display_frame.left) ||
531 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
532 display_frame.top) ||
533 drmModePropertySetAdd(pset, plane->id(), plane->crtc_w_property().id(),
534 display_frame.right - display_frame.left) ||
535 drmModePropertySetAdd(pset, plane->id(), plane->crtc_h_property().id(),
536 display_frame.bottom - display_frame.top) ||
537 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
538 (int)(source_crop.left) << 16) ||
539 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
540 (int)(source_crop.top) << 16) ||
541 drmModePropertySetAdd(pset, plane->id(), plane->src_w_property().id(),
542 (int)(source_crop.right - source_crop.left)
544 drmModePropertySetAdd(pset, plane->id(), plane->src_h_property().id(),
545 (int)(source_crop.bottom - source_crop.top)
548 ALOGE("Failed to add plane %d to set", plane->id());
552 if (plane->rotation_property().id()) {
553 ret = drmModePropertySetAdd(pset, plane->id(),
554 plane->rotation_property().id(), rotation);
556 ALOGE("Failed to add rotation property %d to plane %d",
557 plane->rotation_property().id(), plane->id());
565 ret = drmModePropertySetCommit(drm_->fd(), DRM_MODE_ATOMIC_ALLOW_MODESET,
568 ALOGE("Failed to commit pset ret=%d\n", ret);
569 drmModePropertySetFree(pset);
571 drm_->DestroyPropertyBlob(blob_id);
576 drmModePropertySetFree(pset);
578 if (needs_modeset_) {
579 ret = drm_->DestroyPropertyBlob(old_blob_id);
581 ALOGE("Failed to destroy old mode property blob %lld/%d", old_blob_id,
586 /* TODO: Add dpms to the pset when the kernel supports it */
587 ret = ApplyDpms(display_comp);
589 ALOGE("Failed to apply DPMS after modeset %d\n", ret);
593 connector->set_active_mode(next_mode_);
594 needs_modeset_ = false;
600 int DrmDisplayCompositor::ApplyDpms(DrmDisplayComposition *display_comp) {
601 DrmConnector *conn = drm_->GetConnectorForDisplay(display_);
603 ALOGE("Failed to get DrmConnector for display %d", display_);
607 const DrmProperty &prop = conn->dpms_property();
608 int ret = drmModeConnectorSetProperty(drm_->fd(), conn->id(), prop.id(),
609 display_comp->dpms_mode());
611 ALOGE("Failed to set DPMS property for connector %d", conn->id());
617 int DrmDisplayCompositor::Composite() {
620 if (!pre_compositor_) {
621 pre_compositor_.reset(new GLWorkerCompositor());
622 int ret = pre_compositor_->Init();
624 ALOGE("Failed to initialize OpenGL compositor %d", ret);
629 int ret = pthread_mutex_lock(&lock_);
631 ALOGE("Failed to acquire compositor lock %d", ret);
634 if (composite_queue_.empty()) {
635 ret = pthread_mutex_unlock(&lock_);
637 ALOGE("Failed to release compositor lock %d", ret);
641 std::unique_ptr<DrmDisplayComposition> composition(
642 std::move(composite_queue_.front()));
644 composite_queue_.pop();
646 ret = pthread_mutex_unlock(&lock_);
648 ALOGE("Failed to release compositor lock %d", ret);
652 switch (composition->type()) {
653 case DRM_COMPOSITION_TYPE_FRAME:
654 ret = ApplyFrame(composition.get());
656 ALOGE("Composite failed for display %d", display_);
658 // Disable the hw used by the last active composition. This allows us to
659 // signal the release fences from that composition to avoid hanging.
660 if (DisablePlanes(active_composition_.get()))
663 ++dump_frames_composited_;
665 case DRM_COMPOSITION_TYPE_DPMS:
666 ret = ApplyDpms(composition.get());
668 ALOGE("Failed to apply dpms for display %d", display_);
670 case DRM_COMPOSITION_TYPE_MODESET:
671 next_mode_ = composition->display_mode();
672 needs_modeset_ = true;
675 ALOGE("Unknown composition type %d", composition->type());
679 if (active_composition_)
680 active_composition_->SignalCompositionDone();
682 ret = pthread_mutex_lock(&lock_);
684 ALOGE("Failed to acquire lock for active_composition swap");
686 active_composition_.swap(composition);
689 ret = pthread_mutex_unlock(&lock_);
691 ALOGE("Failed to release lock for active_composition swap");
696 bool DrmDisplayCompositor::HaveQueuedComposites() const {
697 int ret = pthread_mutex_lock(&lock_);
699 ALOGE("Failed to acquire compositor lock %d", ret);
703 bool empty_ret = !composite_queue_.empty();
705 ret = pthread_mutex_unlock(&lock_);
707 ALOGE("Failed to release compositor lock %d", ret);
714 void DrmDisplayCompositor::Dump(std::ostringstream *out) const {