From patchwork Tue Mar 13 16:21:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru-Cosmin Gheorghe X-Patchwork-Id: 10279853 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 581996038F for ; Tue, 13 Mar 2018 16:21:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46EBB29081 for ; Tue, 13 Mar 2018 16:21:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B8DF290CD; Tue, 13 Mar 2018 16:21:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAD_ENC_HEADER,BAYES_00, DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DDDF42910E for ; Tue, 13 Mar 2018 16:21:41 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 182D66E598; Tue, 13 Mar 2018 16:21:41 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-he1eur01on0613.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe1e::613]) by gabe.freedesktop.org (Postfix) with ESMTPS id 994CA6E598 for ; Tue, 13 Mar 2018 16:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector1-arm-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=oHqHZ/73qbl29/B5vyy5Ui9m0xiZDBWqW3UExSAThVQ=; b=B3BtHfBduhB1OoHFZYISN0RcKSnfIBxV8tnuL0r3fbyfCPgbsSmXw+Cma48P+0uzBNN8eOMxZpdU9+m9x8LPLWo3qyb+zkv6Q6cIM+FyfOKg22koj+Dc2/AIJfjjJYiztYoX8OwN91GxCZkH2JbIu7pIpWehhn9UPlPg2VeFaK8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Alexandru-Cosmin.Gheorghe@arm.com; Received: from e114479-lin.cambridge.arm.com (217.140.96.140) by HE1PR0801MB1257.eurprd08.prod.outlook.com (2603:10a6:3:39::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.548.13; Tue, 13 Mar 2018 16:21:34 +0000 From: Alexandru Gheorghe To: dri-devel@lists.freedesktop.org Subject: [PATCH hwc v1] [RFC] drm_hwcomposer: Flatten composition using writeback connector Date: Tue, 13 Mar 2018 16:21:20 +0000 Message-Id: <1520958080-9280-1-git-send-email-alexandru-cosmin.gheorghe@arm.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-Originating-IP: [217.140.96.140] X-ClientProxiedBy: MRXP264CA0011.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500:15::23) To HE1PR0801MB1257.eurprd08.prod.outlook.com (2603:10a6:3:39::7) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 6181950c-5d28-48e1-f2a7-08d588fe7d6d X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7153060)(7193020); SRVR:HE1PR0801MB1257; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1257; 3:NMveKhc53HN9VoYbrPyq8nCdXkwFOpiu7OD8sCUWuV2YJQIEnysKwP8Ys9QyF3XPrP4NAmB5YixdxIZj4cVWh2uuBq7rL5UtSVpi1bOFKSNmfJK+2YW5eWq+FUuhA6oPpLsUECrexhd8C3j8m3PfUBfl3GncFS/gII4rn/7DS+gjuHDbueQz/uN88ygZcqitCbPn2vf6RnK0taYCN3qExx+TVAxhscA4dL3VzhIa5LFEmTrG/chwFzjyty5DTRc6; 25:7SqXKLleLYriy9AIwesMY1FOmlmWXn0egOv7UITfeILjCbAkkyhs9KyLqrpS/CcZ1VWQNll3p0CvtrY7KIu7X9FKl2zhJqdNwBUj+ZNGr7lRp1EsDrh+GEw4wanPTXguoAT/eQMztt0nLnAp37S8+Yn0hESyIke791rIVdXDa1FLYNSrXRcp8JzATcZnteLZb2Wa1Jpwb47xkGtS8l76eqg+3zyP+DLzW9pPN2Nd22q3zo1oG+Y2O2I4FIFrzDpx6nx32krweXyhQ4/KClucMRaknwpDkMY8SL23xwhYl0Fbbnh4/D6OrFRoNzhYueQn+Vjv4KVgTleRjsz4QVTRvg==; 31:X5BZBqChzV/AnjESB5yuEhNzJX1JqAuEwapkSFHkB99dOXo4c5/2bDty/zhHejwYXKa9bm17LaHNAEnCzXTv9XN4kuk4AQXQJh4dWHgydHwOSKfX4nB7YQdfFtSb9ubC5oPFA7GGPgIdpeieGu7//Sa6/goGNAhqmsqxBwuzmEMGfxmyCBxy//C9jXY0xUtB0kGNglURsgHGbGrWm191y8zUq3UkftCp3Yxfbw2sePI= X-MS-TrafficTypeDiagnostic: HE1PR0801MB1257: X-Microsoft-Exchange-Diagnostics: 1; HE1PR0801MB1257; 20:xm+AiDmGBFZnJVrsg9nb1FgEsg3rX5kNpFbZDmpQmtw7qMDeeQW2ReA1UdDFuyyr/1JZQRjWWdBgnsaXa3iPGim25CdhXARtEs5l6vTs9ETxlExzjOahqfVnpHlT/sT83pMD7QlHdmX7B5wzXlyTVavrftUo+HNyHqL3SQqMgk60pGXYWM18h9ah66/o7Fc1MwXZiJzEeoTsdx+qVGcTnQLEiH5tKmRNjtCQD6axmULvFp91Uu+by/wT/sCVMql6eUBnee6QEvTteMXZRnZzxQqEZYsMuc+347pJSBQSxKS/EWoaiaCgKu8YnT2iemGGJHQY64noRaEoUZvVrU/sKfEQaBROegrdsrsS6Sv4ZWY38QRJPYd91KbLTW0PXg2A7rDZwm84jhRSa+I4CrfsL9URmZ54Xye1X/R4ZzROIjfzc6szO/cUD/2VKUXg1gOUhKDg5USX+QBl4C6S/AfDhzboIdfT9U1WGkKgmJCFc9W/TEOPbGbD/LMtrAhVWdrQ; 4:lJ+j2afUzo0kRczVjF0WyROf1PmADY49l7RBUcQjLHYW9vPWhPjDgKt9GuzuNvzxekLbrLSLckif7duoZYrro0RwGbaklhrIUtk8DIbYb4UJ6xWURouELvbtNHTTRWCLxHMvghLVbDhPR2jx4ZCjlfWBN/YlPbbH+RRIUU2Ltj+EBjWV40OKEp/przLn5AcygcjCc9eDEJgCUalTt492/ftPaMjprKMT7yZHIN/ehnX/yRxHIsxD7Gr/kmOpXmgZYf3kvY9XIdcRrM3AMjYl5TUfr6McIBnTcmKps9YQvMRcYjnwuBT/Cao7vidSRquAm+D/VxOcc6J2eRRQnPC2XoO97C+2fd3Kz0D1Bz57v4Evq97xaFHMdFlK4dZE7wxLxoJRB9wFJfzuuLTEhNf17gPUQbWLu/UMODKAEBdCwzM= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(180628864354917)(217544274631240)(21532816269658)(17755550239193); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(3002001)(93006095)(93001095)(10201501046)(3231221)(944501244)(52105095)(6055026)(6041310)(20161123560045)(20161123564045)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(6072148)(201708071742011); SRVR:HE1PR0801MB1257; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0801MB1257; X-Forefront-PRVS: 0610D16BBE X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(39860400002)(39380400002)(346002)(366004)(376002)(396003)(377424004)(40434004)(199004)(189003)(26005)(4326008)(186003)(59450400001)(386003)(106356001)(53936002)(2361001)(2351001)(2906002)(25786009)(52116002)(36756003)(16526019)(105586002)(5890100001)(51416003)(66066001)(47776003)(81156014)(8936002)(8676002)(50466002)(97736004)(81166006)(8746002)(6666003)(6486002)(50226002)(478600001)(316002)(5660300001)(7696005)(6116002)(48376002)(966005)(86362001)(6916009)(305945005)(6306002)(68736007)(72206003)(7736002)(3846002); DIR:OUT; SFP:1101; SCL:1; SRVR:HE1PR0801MB1257; H:e114479-lin.cambridge.arm.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: arm.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; HE1PR0801MB1257; 23:XGvFHf5SMazKVN9WoVZug2ISOd4yUF/GJnHypcR?= =?us-ascii?Q?hGfGqXIAxlpuEkS2vVO8xVwrcmw0Ih11hT5tfXPkaLCxytuxtBxagcsL42pw?= =?us-ascii?Q?+lnApJ+KnZ34/HeaKkWOhF3ngEOIbHhqOZniNBekxzzygABZricdzJtyDLyV?= =?us-ascii?Q?rZzpHs1TzrKb00XejeAWhh+siWjk/0oIBNIc1sN3dUTpeovNEtfluRQHlkLB?= =?us-ascii?Q?Cck621GsQLdLdHWD/doYFw6cQoFtyU3rrnuS5Pstr7yAw8CD3XstGlnC3gIf?= =?us-ascii?Q?9a0nNNiatVn2wo52uJk2ED21y9xHCJe/nB1uCys0gK9edxJS/gVSeERFX4ot?= =?us-ascii?Q?9OVGbeXbrp4gADtaISqk6ScKvQ+zZowOJxAcUO+3fmatixrYjk2HNW7IGeZ9?= =?us-ascii?Q?+fYQCyXLZzta8VRT7LvGoJJe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GJh3Oa3ectOSZermEHyeXCY5V9CMHmea2f3eaMOeaHFg SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Mar 2018 16:21:34.5690 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6181950c-5d28-48e1-f2a7-08d588fe7d6d X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0801MB1257 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alexandru Gheorghe , liviu.dudau@arm.com Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patchset tries to add support for using writeback connector to flatten a scene when it doesn't change for a while. This idea had been floated around on IRC here [1] and here [2]. Developed on top of the latest writeback series, sent by Liviu here [3]. Probably the patch should/could be broken in more patches, but since I want to put this out there to get feedback, I kept them as a single patch for now. This change could be summarize as follow: - Attach a writeback connector to the CRTC that's controlling a display. - Detect the scene did not change for a while(60 vblanks). - Re-commit scene and get the composited scene through the writeback connector. - Commit the whole scene as a single plane. Some areas that I consider important and I could use some feedback/ideas: 1. Building the pipeline. Currently, drm_hwcomposer allows to connect just a single connector to a crtc. For now, I decided to treat the writeback connector as a separate field inside DrmCrtc. I'm not sure if it's a good idea to try to handle this in a unified way, since the writeback connector is such a special connector. Regarding the allocation of writeback connectors, my idea was to allocate writeback connector to the primary display first and then continue allocating while respecting the display number. 0 gets a writeback before 1 and so on. 2. Heuristic for triggering the flattening. I just created a VSyncWorker which will trigger the flattening of the scene if it doesn't change for 60 consecutive vsyncs. The countdown gets reset every time ValidateDisplay is called. This is a relatively basic heuristic, so I'm open to suggestions. 3. Locking scheme corner cases. The Vsynworker is a separate thread which will contend with SurfaceFlinger for showing things on the screen. I tried to limit the race window by resetting the countdown on ValidateDisplay and explicitely checking that we still need to use the flatten scene before commiting to get the writeback result or before applying the flattened scene. 4. Building the DrmDisplayComposition for the flattened scene. I kind of lost myself in all types of layers/planes and compositions, so I'm not sure if I'm correctly building the DrmDisplayComposition object for the FlattenScene, it works and shows what I expect on the screen. So, any feedback here is appreciated. 5. I see there is a drmcompositorworker.cpp which implemented the same idea using the GPU, however that seems to not be used anymore, does anyone know the rationale behind it? Some unfinished/untested things: - Make sure the DrmFrameBuffer allocates one of the formats reported in WRITEBACK_PIXEL_FORMATS. - I'm using a hacked setup where, when needed it, the GL compositing is done by Surfaceflinger, so I'm not sure how well this changes are getting along with the GLCompositorWorker. [1] https://people.freedesktop.org/~cbrill/dri-log/?channel=dri-devel&highlight_names=&date=2018-02-23 [2] https://people.freedesktop.org/~cbrill/dri-log/?channel=dri-devel&highlight_names=&date=2018-02-09 [3] https://lists.freedesktop.org/archives/dri-devel/2018-February/167703.html Signed-off-by: Alexandru Gheorghe --- drmconnector.cpp | 36 ++++++++++- drmconnector.h | 8 +++ drmcrtc.cpp | 11 +++- drmcrtc.h | 8 ++- drmdisplaycompositor.cpp | 164 +++++++++++++++++++++++++++++++++++++++++++++-- drmdisplaycompositor.h | 16 +++-- drmencoder.cpp | 15 +++++ drmencoder.h | 7 +- drmhwctwo.cpp | 1 + drmresources.cpp | 56 +++++++++++++++- drmresources.h | 1 + 11 files changed, 306 insertions(+), 17 deletions(-) -- 2.7.4 IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. diff --git a/drmconnector.cpp b/drmconnector.cpp index 145518f..2ed4f23 100644 --- a/drmconnector.cpp +++ b/drmconnector.cpp @@ -52,6 +52,23 @@ int DrmConnector::Init() { ALOGE("Could not get CRTC_ID property\n"); return ret; } + if (writeback()) { + ret = drm_->GetConnectorProperty(*this, "WRITEBACK_PIXEL_FORMATS", &writeback_pixel_formats_); + if (ret) { + ALOGE("Could not get WRITEBACK_PIXEL_FORMATS connector_id = %d\n", id_); + return ret; + } + ret = drm_->GetConnectorProperty(*this, "WRITEBACK_FB_ID", &writeback_fb_id_); + if (ret) { + ALOGE("Could not get WRITEBACK_FB_ID connector_id = %d\n", id_); + return ret; + } + ret = drm_->GetConnectorProperty(*this, "WRITEBACK_OUT_FENCE_PTR", &writeback_out_fence_); + if (ret) { + ALOGE("Could not get WRITEBACK_OUT_FENCE_PTR connector_id = %d\n", id_); + return ret; + } + } return 0; } @@ -78,8 +95,13 @@ bool DrmConnector::external() const { type_ == DRM_MODE_CONNECTOR_VGA; } +#define DRM_MODE_CONNECTOR_WRITEBACK 18 +bool DrmConnector::writeback() const { + return type_ == DRM_MODE_CONNECTOR_WRITEBACK; +} + bool DrmConnector::valid_type() const { - return internal() || external(); + return internal() || external() || writeback(); } int DrmConnector::UpdateModes() { @@ -130,6 +152,18 @@ const DrmProperty &DrmConnector::crtc_id_property() const { return crtc_id_property_; } +const DrmProperty &DrmConnector::writeback_pixel_formats() const { + return writeback_pixel_formats_; +} + +const DrmProperty &DrmConnector::writeback_fb_id() const { + return writeback_fb_id_; +} + +const DrmProperty &DrmConnector::writeback_out_fence() const { + return writeback_out_fence_; +} + DrmEncoder *DrmConnector::encoder() const { return encoder_; } diff --git a/drmconnector.h b/drmconnector.h index 5601e06..ad18762 100644 --- a/drmconnector.h +++ b/drmconnector.h @@ -28,6 +28,7 @@ namespace android { class DrmResources; +class DrmCrtc; class DrmConnector { public: @@ -46,6 +47,7 @@ class DrmConnector { bool internal() const; bool external() const; + bool writeback() const; bool valid_type() const; int UpdateModes(); @@ -58,6 +60,9 @@ class DrmConnector { const DrmProperty &dpms_property() const; const DrmProperty &crtc_id_property() const; + const DrmProperty &writeback_pixel_formats() const; + const DrmProperty &writeback_fb_id() const; + const DrmProperty &writeback_out_fence() const; const std::vector &possible_encoders() const { return possible_encoders_; @@ -88,6 +93,9 @@ class DrmConnector { DrmProperty dpms_property_; DrmProperty crtc_id_property_; + DrmProperty writeback_pixel_formats_; + DrmProperty writeback_fb_id_; + DrmProperty writeback_out_fence_; std::vector possible_encoders_; }; diff --git a/drmcrtc.cpp b/drmcrtc.cpp index 1b354fe..f8c9f25 100644 --- a/drmcrtc.cpp +++ b/drmcrtc.cpp @@ -31,7 +31,8 @@ DrmCrtc::DrmCrtc(DrmResources *drm, drmModeCrtcPtr c, unsigned pipe) id_(c->crtc_id), pipe_(pipe), display_(-1), - mode_(&c->mode) { + mode_(&c->mode), + writeback_conn_(nullptr) { } int DrmCrtc::Init() { @@ -75,6 +76,14 @@ bool DrmCrtc::can_bind(int display) const { return display_ == -1 || display_ == display; } +DrmConnector* DrmCrtc::writeback_conn() const { + return writeback_conn_; +} + +void DrmCrtc::set_writeback_conn(DrmConnector* writeback_conn) { + writeback_conn_ = writeback_conn; +} + const DrmProperty &DrmCrtc::active_property() const { return active_property_; } diff --git a/drmcrtc.h b/drmcrtc.h index c5a5599..bbd8d37 100644 --- a/drmcrtc.h +++ b/drmcrtc.h @@ -22,11 +22,11 @@ #include #include - +#include "drmconnector.h" namespace android { class DrmResources; - +class DrmConnector; class DrmCrtc { public: DrmCrtc(DrmResources *drm, drmModeCrtcPtr c, unsigned pipe); @@ -39,7 +39,9 @@ class DrmCrtc { unsigned pipe() const; int display() const; + DrmConnector* writeback_conn() const; void set_display(int display); + void set_writeback_conn(DrmConnector*); bool can_bind(int display) const; @@ -55,7 +57,7 @@ class DrmCrtc { int display_; DrmMode mode_; - + DrmConnector *writeback_conn_; DrmProperty active_property_; DrmProperty mode_property_; DrmProperty out_fence_ptr_property_; diff --git a/drmdisplaycompositor.cpp b/drmdisplaycompositor.cpp index e556e86..9783852 100644 --- a/drmdisplaycompositor.cpp +++ b/drmdisplaycompositor.cpp @@ -16,7 +16,6 @@ #define ATRACE_TAG ATRACE_TAG_GRAPHICS #define LOG_TAG "hwc-drm-display-compositor" - #include "drmdisplaycompositor.h" #include @@ -36,9 +35,24 @@ #include "drmplane.h" #include "drmresources.h" #include "glworker.h" +static const uint32_t kWaitWritebackFence = 100; //ms namespace android { +class CompositorVsyncCallback : public VsyncCallback { + public: + CompositorVsyncCallback(DrmDisplayCompositor *compositor) + :compositor_(compositor) { + } + + void Callback(int display, int64_t timestamp) { + compositor_->Vsync(display, timestamp); + } + + private: + DrmDisplayCompositor *compositor_; +}; + void SquashState::Init(DrmHwcLayer *layers, size_t num_layers) { generation_number_++; valid_history_ = 0; @@ -183,7 +197,8 @@ DrmDisplayCompositor::DrmDisplayCompositor() framebuffer_index_(0), squash_framebuffer_index_(0), dump_frames_composited_(0), - dump_last_timestamp_ns_(0) { + dump_last_timestamp_ns_(0), + flatten_countdown_(FLATTEN_COUNTDOWN_INIT) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) return; @@ -193,7 +208,7 @@ DrmDisplayCompositor::DrmDisplayCompositor() DrmDisplayCompositor::~DrmDisplayCompositor() { if (!initialized_) return; - + vsync_worker_.Exit(); int ret = pthread_mutex_lock(&lock_); if (ret) ALOGE("Failed to acquire compositor lock %d", ret); @@ -223,6 +238,9 @@ int DrmDisplayCompositor::Init(DrmResources *drm, int display) { } initialized_ = true; + vsync_worker_.Init(drm_, display_); + auto callback = std::make_shared(this); + vsync_worker_.RegisterCallback(callback); return 0; } @@ -482,7 +500,7 @@ int DrmDisplayCompositor::PrepareFrame(DrmDisplayComposition *display_comp) { } int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, - bool test_only) { + bool test_only, DrmDisplayComposition *writeback_comp) { ATRACE_CALL(); int ret = 0; @@ -491,7 +509,8 @@ int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, std::vector &comp_planes = display_comp->composition_planes(); uint64_t out_fences[drm_->crtcs().size()]; - + int writeback_fence = -1; + DrmFramebuffer *writeback_fb = nullptr; DrmConnector *connector = drm_->GetConnectorForDisplay(display_); if (!connector) { ALOGE("Could not locate connector for display %d", display_); @@ -508,6 +527,32 @@ int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, ALOGE("Failed to allocate property set"); return -ENOMEM; } + DrmConnector *writeback_conn = crtc->writeback_conn(); + if (writeback_comp != nullptr) { + if (writeback_conn == nullptr) + return -EINVAL; + writeback_fb = &framebuffers_[framebuffer_index_]; + framebuffer_index_ = (framebuffer_index_ + 1) % DRM_DISPLAY_BUFFERS; + ret = PrepareFramebuffer(*writeback_fb, writeback_comp); + if (ret) { + ALOGE("Failed to prepare framebuffer for pre-composite %d", ret); + return ret; + } + if (writeback_conn->writeback_fb_id().id() == 0 || writeback_conn->writeback_out_fence().id() == 0) + return -EINVAL; + ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), writeback_conn->writeback_fb_id().id(), + writeback_comp->layers().back().buffer->fb_id); + if (ret < 0) { + ALOGE("Failed to add writeback_fb_id"); + return ret; + } + ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), writeback_conn->writeback_out_fence().id(), + (uint64_t) &writeback_fence); + if (ret < 0) { + ALOGE("Failed to add writeback_out_fence"); + return ret; + } + } if (crtc->out_fence_ptr_property().id() != 0) { ret = drmModeAtomicAddProperty(pset, crtc->id(), crtc->out_fence_ptr_property().id(), @@ -537,6 +582,12 @@ int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, drmModeAtomicFree(pset); return ret; } + if (writeback_conn != nullptr) { + ret = drmModeAtomicAddProperty(pset, writeback_conn->id(), writeback_conn->crtc_id_property().id(), crtc->id()); + if (ret < 0) { + ALOGE("Failed to attach writeback"); + } + } } for (DrmCompositionPlane &comp_plane : comp_planes) { @@ -691,6 +742,17 @@ int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, if (test_only) flags |= DRM_MODE_ATOMIC_TEST_ONLY; + // There is a race when we recommit a scene to get the writeback result + // to shorten that race, check if we still need that result + bool abort_commit = false; + if (writeback_comp != nullptr) { + AutoLock lock(&lock_, "CommitFrame"); + lock.Lock(); + abort_commit = !CountdownExpired(); + } + if (abort_commit) + return -EINVAL; + ret = drmModeAtomicCommit(drm_->fd(), pset, flags, drm_); if (ret) { if (test_only) @@ -729,6 +791,14 @@ int DrmDisplayCompositor::CommitFrame(DrmDisplayComposition *display_comp, display_comp->set_out_fence((int) out_fences[crtc->pipe()]); } + if (writeback_fence >= 0 && writeback_fb != nullptr) { + ret = sync_wait(writeback_fence, kWaitWritebackFence); + if (ret) { + ALOGE("Failed to wait on writeback fence"); + } + close(writeback_fence); + } + return ret; } @@ -784,7 +854,7 @@ void DrmDisplayCompositor::ClearDisplay() { } void DrmDisplayCompositor::ApplyFrame( - std::unique_ptr composition, int status) { + std::unique_ptr composition, int status, bool start_countdown) { int ret = status; if (!ret) @@ -807,6 +877,8 @@ void DrmDisplayCompositor::ApplyFrame( ALOGE("Failed to acquire lock for active_composition swap"); active_composition_.swap(composition); + flatten_countdown_ = FLATTEN_COUNTDOWN_INIT; + vsync_worker_.VSyncControl(start_countdown); if (!ret) ret = pthread_mutex_unlock(&lock_); @@ -878,6 +950,65 @@ int DrmDisplayCompositor::ApplyComposition( return ret; } +int DrmDisplayCompositor::FlattenScene() { + std::unique_ptr comp = CreateComposition(); + DrmDisplayComposition *src = active_composition_.get(); + DrmDisplayComposition *dst = comp.get(); + std::vector &src_planes = src->composition_planes(); + if (src == nullptr || dst == nullptr) + return -EINVAL; + size_t src_planes_with_layer = std::count_if( + src_planes.begin(), src_planes.end(), [](DrmCompositionPlane &p) { + return p.type() != DrmCompositionPlane::Type::kDisable; + }); + + if (src_planes_with_layer <= 1) + return -EALREADY; + int ret = dst->Init(drm_, src->crtc(), src->importer(), src->planner(), + src->frame_no()); + if (ret) + return ret; + DrmCompositionPlane squashed_comp(DrmCompositionPlane::Type::kPrecomp, NULL, + src->crtc()); + for (DrmCompositionPlane &comp_plane : src_planes) { + if (comp_plane.plane() == NULL) { + ALOGE("Skipping squash all because of NULL plane"); + ret = -EINVAL; + } + if (comp_plane.plane()->type() == DRM_PLANE_TYPE_PRIMARY) + squashed_comp.set_plane(comp_plane.plane()); + else + dst->AddPlaneDisable(comp_plane.plane()); + } + ret = CommitFrame(active_composition_.get(), 0, dst); + if (ret || dst->layers().size() != 1) { + ALOGE("Failed to flatten scene using writeback"); + return -EINVAL; + } + squashed_comp.source_layers().push_back(0); + + ret = dst->AddPlaneComposition(std::move(squashed_comp)); + if (ret) { + ALOGE("Failed to add flatten scene"); + return ret; + } + ret = dst->FinalizeComposition(); + if (ret) { + ALOGE("Failed to finalize composition"); + return ret; + } + // If countdown it's still expired ApplyFlattenScene + AutoLock lock(&lock_, "FlattenScene"); + lock.Lock(); + if (CountdownExpired()) { + lock.Unlock(); + ApplyFrame(std::move(comp), 0, false); + } else { + return -EAGAIN; + } + return 0; +} + int DrmDisplayCompositor::SquashAll() { AutoLock lock(&lock_, "compositor"); int ret = lock.Lock(); @@ -1026,6 +1157,27 @@ move_layers_back: return ret; } +bool DrmDisplayCompositor::CountdownExpired() const { + return flatten_countdown_ <= 0; +} + +void DrmDisplayCompositor::ResetCountdown() { + AutoLock lock(&lock_, "ResetCountdown"); + lock.Lock(); + flatten_countdown_ = FLATTEN_COUNTDOWN_INIT; +} + +void DrmDisplayCompositor::Vsync(int display, int64_t timestamp) { + AutoLock lock(&lock_, "VSync"); + lock.Lock(); + flatten_countdown_--; + if (CountdownExpired()) { + lock.Unlock(); + int ret = FlattenScene(); + ALOGI("Vsync: Flatten scene for display %d at timestamp %" PRIu64 " ret = %d \n", display, timestamp, ret); + } +} + void DrmDisplayCompositor::Dump(std::ostringstream *out) const { int ret = pthread_mutex_lock(&lock_); if (ret) diff --git a/drmdisplaycompositor.h b/drmdisplaycompositor.h index f1965fb..4a5696a 100644 --- a/drmdisplaycompositor.h +++ b/drmdisplaycompositor.h @@ -29,11 +29,15 @@ #include #include +#include // One for the front, one for the back, and one for cases where we need to // squash a frame that the hw can't display with hw overlays. #define DRM_DISPLAY_BUFFERS 3 +// If a scene is still for this number of vblanks flatten it to reduce power consumption. +#define FLATTEN_COUNTDOWN_INIT 60 + namespace android { class GLWorkerCompositor; @@ -91,7 +95,8 @@ class DrmDisplayCompositor { int Composite(); int SquashAll(); void Dump(std::ostringstream *out) const; - + void Vsync(int display, int64_t timestamp); + void ResetCountdown(); std::tuple GetActiveModeResolution(); SquashState *squash_state() { @@ -118,15 +123,16 @@ class DrmDisplayCompositor { int ApplySquash(DrmDisplayComposition *display_comp); int ApplyPreComposite(DrmDisplayComposition *display_comp); int PrepareFrame(DrmDisplayComposition *display_comp); - int CommitFrame(DrmDisplayComposition *display_comp, bool test_only); + int CommitFrame(DrmDisplayComposition *display_comp, bool test_only, DrmDisplayComposition *writeback_comp = nullptr); int SquashFrame(DrmDisplayComposition *src, DrmDisplayComposition *dst); int ApplyDpms(DrmDisplayComposition *display_comp); int DisablePlanes(DrmDisplayComposition *display_comp); void ClearDisplay(); void ApplyFrame(std::unique_ptr composition, - int status); - + int status, bool start_countdown = true); + int FlattenScene(); + bool CountdownExpired() const; std::tuple CreateModeBlob(const DrmMode &mode); DrmResources *drm_; @@ -155,6 +161,8 @@ class DrmDisplayCompositor { // we need to reset them on every Dump() call. mutable uint64_t dump_frames_composited_; mutable uint64_t dump_last_timestamp_ns_; + VSyncWorker vsync_worker_; + int64_t flatten_countdown_; }; } diff --git a/drmencoder.cpp b/drmencoder.cpp index 3d762f3..3df06a2 100644 --- a/drmencoder.cpp +++ b/drmencoder.cpp @@ -27,6 +27,7 @@ DrmEncoder::DrmEncoder(drmModeEncoderPtr e, DrmCrtc *current_crtc, const std::vector &possible_crtcs) : id_(e->encoder_id), crtc_(current_crtc), + display_(-1), possible_crtcs_(possible_crtcs) { } @@ -40,5 +41,19 @@ DrmCrtc *DrmEncoder::crtc() const { void DrmEncoder::set_crtc(DrmCrtc *crtc) { crtc_ = crtc; + set_display(crtc->display()); } + +int DrmEncoder::display() const { + return display_; +} + +void DrmEncoder::set_display(int display) { + display_ = display; +} + +bool DrmEncoder::can_bind(int display) const { + return display_ == -1 || display_ == display; +} + } diff --git a/drmencoder.h b/drmencoder.h index 58ccbfb..6b39505 100644 --- a/drmencoder.h +++ b/drmencoder.h @@ -25,6 +25,8 @@ namespace android { +class DrmCrtc; + class DrmEncoder { public: DrmEncoder(drmModeEncoderPtr e, DrmCrtc *current_crtc, @@ -36,7 +38,9 @@ class DrmEncoder { DrmCrtc *crtc() const; void set_crtc(DrmCrtc *crtc); - + bool can_bind(int display) const; + void set_display(int display); + int display() const; const std::vector &possible_crtcs() const { return possible_crtcs_; } @@ -44,6 +48,7 @@ class DrmEncoder { private: uint32_t id_; DrmCrtc *crtc_; + int display_; std::vector possible_crtcs_; }; diff --git a/drmhwctwo.cpp b/drmhwctwo.cpp index dfca1a6..65337cc 100644 --- a/drmhwctwo.cpp +++ b/drmhwctwo.cpp @@ -700,6 +700,7 @@ HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types, break; } } + compositor_.ResetCountdown(); return *num_types ? HWC2::Error::HasChanges : HWC2::Error::None; } diff --git a/drmresources.cpp b/drmresources.cpp index 32dd376..880cef2 100644 --- a/drmresources.cpp +++ b/drmresources.cpp @@ -33,6 +33,8 @@ #include #include +#define DRM_CLIENT_CAP_WRITEBACK_CONNECTORS 4 + namespace android { DrmResources::DrmResources() : event_listener_(this) { @@ -65,6 +67,11 @@ int DrmResources::Init() { return ret; } + ret = drmSetClientCap(fd(), DRM_CLIENT_CAP_WRITEBACK_CONNECTORS, 1); + if (ret) { + ALOGI("Failed to set writeback cap %d", ret); + ret = 0; + } drmModeResPtr res = drmModeGetResources(fd()); if (!res) { ALOGE("Failed to get DrmResources resources"); @@ -77,6 +84,7 @@ int DrmResources::Init() { std::pair(res->max_width, res->max_height); bool found_primary = false; + int primary_index = 0; int display_num = 1; for (int i = 0; !ret && i < res->count_crtcs; ++i) { @@ -162,18 +170,22 @@ int DrmResources::Init() { if (conn->internal() && !found_primary) { conn->set_display(0); found_primary = true; - } else { + } else if (conn->external()) { + if (!found_primary) primary_index++; conn->set_display(display_num); ++display_num; } } // Then look for primary amongst external connectors + if (!found_primary) primary_index = 0; for (auto &conn : connectors_) { if (conn->external() && !found_primary) { conn->set_display(0); found_primary = true; + break; } + if (!found_primary) primary_index++; } if (res) @@ -220,12 +232,29 @@ int DrmResources::Init() { } for (auto &conn : connectors_) { + if (conn->writeback()) + continue; ret = CreateDisplayPipe(conn.get()); if (ret) { ALOGE("Failed CreateDisplayPipe %d with %d", conn->id(), ret); return ret; } } + /* Allocate writebacks according to the display number */ + /* 0 should get a writeback before 1 */ + for (auto &writeback_conn : connectors_) { + if (!writeback_conn->writeback()) + continue; + int index = primary_index; + do { + if (connectors_[index]->display() < 0) + continue; + ret = AttachWriteback(writeback_conn.get(), connectors_[index].get()); + if (!ret) + break; + index = (index + 1) % connectors_.size(); + } while (index != primary_index); + } return 0; } @@ -314,6 +343,31 @@ int DrmResources::CreateDisplayPipe(DrmConnector *connector) { return -ENODEV; } +/* + * Attach writeback connector to the CRTC linked to the display_conn + * + */ +int DrmResources::AttachWriteback(DrmConnector *writeback_conn, DrmConnector *display_conn) { + int ret = -EINVAL; + if (display_conn->writeback()) + return -EINVAL; + if (!display_conn->encoder() || ! display_conn->encoder()->crtc()) + return -EINVAL; + DrmCrtc *display_crtc = display_conn->encoder()->crtc(); + if (display_crtc->writeback_conn() != nullptr) + return -EINVAL; + for (DrmEncoder *writeback_enc : writeback_conn->possible_encoders()) { + // Use just encoders which had not been bound already. + if (writeback_enc->can_bind(display_crtc->display())) { + writeback_enc->set_crtc(display_crtc); + writeback_conn->set_encoder(writeback_enc); + display_crtc->set_writeback_conn(writeback_conn); + ret = 0; + } + } + return ret; +} + int DrmResources::CreatePropertyBlob(void *data, size_t length, uint32_t *blob_id) { struct drm_mode_create_blob create_blob; diff --git a/drmresources.h b/drmresources.h index 4cca48c..ad285ec 100644 --- a/drmresources.h +++ b/drmresources.h @@ -78,6 +78,7 @@ class DrmResources { DrmProperty *property); int CreateDisplayPipe(DrmConnector *connector); + int AttachWriteback(DrmConnector *writeback_conn, DrmConnector *display_conn); UniqueFd fd_; uint32_t mode_id_ = 0;