From 5b4a48a90b2bbeb06778e130a29420e2cfa908e9 Mon Sep 17 00:00:00 2001
From: Adithya R <gh0strider.2k18.reborn@gmail.com>
Date: Fri, 12 Aug 2022 23:25:57 +0530
Subject: [PATCH] SurfaceFlinger: Allow forcing HWC brightness support
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Some vendors purposely disable brightness capability
even though the HWC completely supports it, in order
to leverage their custom brightness implementations.
One such example of this is in stock HWC of some
xiaomi devices.

Since light HAL offers lesser granual control than
HWC (since its in gamma space and not float), and
since it would be rather difficult to patch the blob
to return the right capabilities, allow forcing the
usage of HWC brightness functions via a prop:

ro.sf.force_hwc_brightness=1 (default 0).

Change-Id: I7d2fef03afbbb936572c717f769f30c9e49213e0
Signed-off-by: Adithya R <gh0strider.2k18.reborn@gmail.com>
Signed-off-by: Simão Gomes Viana <devel@superboring.dev>
---
 services/surfaceflinger/SurfaceFlinger.cpp | 6 +++++-
 services/surfaceflinger/SurfaceFlinger.h   | 1 +
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 776b71e2ce..39645e53fc 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -509,6 +509,9 @@ SurfaceFlinger::SurfaceFlinger(Factory& factory) : SurfaceFlinger(factory, SkipI
     property_get("debug.sf.dim_in_gamma_in_enhanced_screenshots", value, 0);
     mDimInGammaSpaceForEnhancedScreenshots = atoi(value);
 
+    property_get("ro.sf.force_hwc_brightness", value, "0");
+    mForceHwcBrightness = atoi(value);
+
     mIgnoreHwcPhysicalDisplayOrientation =
             base::GetBoolProperty("debug.sf.ignore_hwc_physical_display_orientation"s, false);
 
@@ -2019,7 +2022,8 @@ status_t SurfaceFlinger::getDisplayBrightnessSupport(const sp<IBinder>& displayT
     if (!displayId) {
         return NAME_NOT_FOUND;
     }
-    *outSupport = getHwComposer().hasDisplayCapability(*displayId, DisplayCapability::BRIGHTNESS);
+    *outSupport = mForceHwcBrightness ? true :
+	    getHwComposer().hasDisplayCapability(*displayId, DisplayCapability::BRIGHTNESS);
     return NO_ERROR;
 }
 
diff --git a/services/surfaceflinger/SurfaceFlinger.h b/services/surfaceflinger/SurfaceFlinger.h
index 7c2d164ac9..8b647c392a 100644
--- a/services/surfaceflinger/SurfaceFlinger.h
+++ b/services/surfaceflinger/SurfaceFlinger.h
@@ -1421,6 +1421,7 @@ private:
     scheduler::PresentLatencyTracker mPresentLatencyTracker GUARDED_BY(kMainThreadContext);
 
     bool mLumaSampling = true;
+    bool mForceHwcBrightness = false;
     sp<RegionSamplingThread> mRegionSamplingThread;
     sp<FpsReporter> mFpsReporter;
     sp<TunnelModeEnabledReporter> mTunnelModeEnabledReporter;
-- 
GitLab