Moved brightness from Lights to SF.

Test: manual.
      Check that brightness works.
Fixes: 111435292

Change-Id: I3d19bc2fc4088bc9a956a5cf38c3b163fe35256f
diff --git a/current.txt b/current.txt
index 7af62c6..f1b030e 100644
--- a/current.txt
+++ b/current.txt
@@ -479,7 +479,7 @@
 01c6398c90fc6be0640810e2c5d8a4863b457280132bb3f97dd5682e19632b62 android.hardware.graphics.bufferqueue@2.0::types
 7a2d64095252f85781b2d521f4f11d04ce774544feececcec2088c568656e93c android.hardware.graphics.common@1.2::types
 3dff04a36b86660b5807414587e530bb0c294ed56fdff06f8915ba0a9b73f974 android.hardware.graphics.composer@2.3::IComposer
-daa44e83d7709bf1c9e0bd9a6b552feff496fd14574a9461ee93c21980fc5b15 android.hardware.graphics.composer@2.3::IComposerClient
+54bc1dc874f8bc0781767786075dafd33a0796c1eea7d2317231b8929280e946 android.hardware.graphics.composer@2.3::IComposerClient
 5c8bf8e1af9efe225a4661db8c08ff1b7e13fdc8ed49f35291bd0b6c9436b8f2 android.hardware.graphics.mapper@3.0::IMapper
 7183d9d9acfa41a61a64bdfed548e98299265a7bb1821a3ed204173b5c2cfd4a android.hardware.graphics.mapper@3.0::types
 c3f831a66d5815baf74f5b82fe79cf099542ddae4dfab3f388e1d41828e794fc android.hardware.health.storage@1.0::IGarbageCollectCallback
diff --git a/graphics/composer/2.3/IComposerClient.hal b/graphics/composer/2.3/IComposerClient.hal
index 5fcc0e6..1eea306 100644
--- a/graphics/composer/2.3/IComposerClient.hal
+++ b/graphics/composer/2.3/IComposerClient.hal
@@ -61,6 +61,11 @@
          * PowerMode::DOZE_SUSPEND.
          */
         DOZE = 2,
+
+        /**
+         * Indicates that the display supports brightness operations.
+         */
+        BRIGHTNESS = 3,
     };
 
     /**
@@ -495,4 +500,38 @@
                        float maxLuminance,
                        float maxAverageLuminance,
                        float minLuminance);
+
+    /**
+     * Gets whether brightness operations are supported on a display.
+     *
+     * @param display
+     *      The display.
+     *
+     * @return error is NONE upon success. Otherwise,
+     *      BAD_DISPLAY   when the display is invalid, or
+     *      BAD_PARAMETER when the output parameter is invalid.
+     * @return support
+     *      Whether brightness operations are supported on the display.
+     */
+    getDisplayBrightnessSupport(Display display) generates (Error error, bool support);
+
+    /**
+     * Sets the brightness of a display.
+     *
+     * Ideally, the brightness change should take effect in the next frame post (so that it can be
+     * aligned with color transforms).
+     *
+     * @param display
+     *      The display whose brightness is set.
+     * @param brightness
+     *      A number between 0.0f (minimum brightness) and 1.0f (maximum brightness), or -1.0 to
+     *      turn the backlight off.
+     *
+     * @return error is NONE upon success. Otherwise,
+     *         BAD_DISPLAY   when the display is invalid, or
+     *         UNSUPPORTED   when brightness operations are not supported, or
+     *         BAD_PARAMETER when the brightness is invalid, or
+     *         NO_RESOURCES  when the brightness cannot be applied.
+     */
+    setDisplayBrightness(Display display, float brightness) generates (Error error);
 };
diff --git a/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerClient.h b/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerClient.h
index a272e72..1b40795 100644
--- a/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerClient.h
+++ b/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerClient.h
@@ -172,7 +172,19 @@
         return Void();
     }
 
-   protected:
+    Return<void> getDisplayBrightnessSupport(
+            Display display, IComposerClient::getDisplayBrightnessSupport_cb hidl_cb) override {
+        bool support = false;
+        Error error = mHal->getDisplayBrightnessSupport(display, &support);
+        hidl_cb(error, support);
+        return Void();
+    }
+
+    Return<Error> setDisplayBrightness(Display display, float brightness) override {
+        return mHal->setDisplayBrightness(display, brightness);
+    }
+
+  protected:
     std::unique_ptr<V2_1::hal::ComposerCommandEngine> createCommandEngine() override {
         return std::make_unique<ComposerCommandEngine>(
             mHal, static_cast<V2_2::hal::ComposerResources*>(mResources.get()));
diff --git a/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerHal.h b/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerHal.h
index a0812ad..186b004 100644
--- a/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerHal.h
+++ b/graphics/composer/2.3/utils/hal/include/composer-hal/2.3/ComposerHal.h
@@ -119,6 +119,8 @@
     virtual Error setLayerPerFrameMetadataBlobs(
         Display display, Layer layer,
         std::vector<IComposerClient::PerFrameMetadataBlob>& blobs) = 0;
+    virtual Error getDisplayBrightnessSupport(Display display, bool* outSupport) = 0;
+    virtual Error setDisplayBrightness(Display display, float brightness) = 0;
 };
 
 }  // namespace hal
diff --git a/graphics/composer/2.3/utils/passthrough/include/composer-passthrough/2.3/HwcHal.h b/graphics/composer/2.3/utils/passthrough/include/composer-passthrough/2.3/HwcHal.h
index 41e333a..070cf80 100644
--- a/graphics/composer/2.3/utils/passthrough/include/composer-passthrough/2.3/HwcHal.h
+++ b/graphics/composer/2.3/utils/passthrough/include/composer-passthrough/2.3/HwcHal.h
@@ -245,7 +245,29 @@
         return static_cast<Error>(err);
     }
 
-   protected:
+    Error getDisplayBrightnessSupport(Display display, bool* outSupport) {
+        if (!mDispatch.getDisplayBrightnessSupport) {
+            return Error::UNSUPPORTED;
+        }
+        bool support = false;
+        int32_t error = mDispatch.getDisplayBrightnessSupport(mDevice, display, &support);
+        *outSupport = support;
+        return static_cast<Error>(error);
+    }
+
+    Error setDisplayBrightness(Display display, float brightness) {
+        if (std::isnan(brightness) || brightness > 1.0f ||
+            (brightness < 0.0f && brightness != -1.0f)) {
+            return Error::BAD_PARAMETER;
+        }
+        if (!mDispatch.setDisplayBrightness) {
+            return Error::UNSUPPORTED;
+        }
+        int32_t error = mDispatch.setDisplayBrightness(mDevice, display, brightness);
+        return static_cast<Error>(error);
+    }
+
+  protected:
     bool initDispatch() override {
         if (!BaseType2_2::initDispatch()) {
             return false;
@@ -265,6 +287,10 @@
                                    &mDispatch.getDisplayCapabilities);
         this->initOptionalDispatch(HWC2_FUNCTION_SET_LAYER_PER_FRAME_METADATA_BLOBS,
                                    &mDispatch.setLayerPerFrameMetadataBlobs);
+        this->initOptionalDispatch(HWC2_FUNCTION_GET_DISPLAY_BRIGHTNESS_SUPPORT,
+                                   &mDispatch.getDisplayBrightnessSupport);
+        this->initOptionalDispatch(HWC2_FUNCTION_SET_DISPLAY_BRIGHTNESS,
+                                   &mDispatch.setDisplayBrightness);
         return true;
     }
 
@@ -277,6 +303,8 @@
         HWC2_PFN_GET_DISPLAYED_CONTENT_SAMPLE getDisplayedContentSample;
         HWC2_PFN_GET_DISPLAY_CAPABILITIES getDisplayCapabilities;
         HWC2_PFN_SET_LAYER_PER_FRAME_METADATA_BLOBS setLayerPerFrameMetadataBlobs;
+        HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT getDisplayBrightnessSupport;
+        HWC2_PFN_SET_DISPLAY_BRIGHTNESS setDisplayBrightness;
     } mDispatch = {};
 
     using BaseType2_2 = V2_2::passthrough::detail::HwcHalImpl<Hal>;
diff --git a/graphics/composer/2.3/utils/vts/ComposerVts.cpp b/graphics/composer/2.3/utils/vts/ComposerVts.cpp
index 0e541ed..4de85d6 100644
--- a/graphics/composer/2.3/utils/vts/ComposerVts.cpp
+++ b/graphics/composer/2.3/utils/vts/ComposerVts.cpp
@@ -186,6 +186,19 @@
     return capabilities;
 }
 
+bool ComposerClient::getDisplayBrightnessSupport(Display display) {
+    bool support = false;
+    mClient->getDisplayBrightnessSupport(display, [&](const auto& error, const auto& tmpSupport) {
+        ASSERT_EQ(Error::NONE, error) << "failed to get brightness support";
+        support = tmpSupport;
+    });
+    return support;
+}
+
+Error ComposerClient::setDisplayBrightness(Display display, float brightness) {
+    return mClient->setDisplayBrightness(display, brightness);
+}
+
 }  // namespace vts
 }  // namespace V2_3
 }  // namespace composer
diff --git a/graphics/composer/2.3/utils/vts/include/composer-vts/2.3/ComposerVts.h b/graphics/composer/2.3/utils/vts/include/composer-vts/2.3/ComposerVts.h
index ad4ef0b..a0e764d 100644
--- a/graphics/composer/2.3/utils/vts/include/composer-vts/2.3/ComposerVts.h
+++ b/graphics/composer/2.3/utils/vts/include/composer-vts/2.3/ComposerVts.h
@@ -52,7 +52,7 @@
 
     std::unique_ptr<ComposerClient> createClient();
 
-   protected:
+  protected:
     explicit Composer(const sp<IComposer>& composer);
 
    private:
@@ -99,7 +99,11 @@
 
     std::vector<IComposerClient::PerFrameMetadataKey> getPerFrameMetadataKeys_2_3(Display display);
 
-   private:
+    bool getDisplayBrightnessSupport(Display display);
+
+    Error setDisplayBrightness(Display display, float brightness);
+
+  private:
     const sp<IComposerClient> mClient;
 };
 
diff --git a/graphics/composer/2.3/vts/functional/VtsHalGraphicsComposerV2_3TargetTest.cpp b/graphics/composer/2.3/vts/functional/VtsHalGraphicsComposerV2_3TargetTest.cpp
index de74e28..b983e42 100644
--- a/graphics/composer/2.3/vts/functional/VtsHalGraphicsComposerV2_3TargetTest.cpp
+++ b/graphics/composer/2.3/vts/functional/VtsHalGraphicsComposerV2_3TargetTest.cpp
@@ -600,6 +600,36 @@
     }
 }
 
+/*
+ * Test that getDisplayBrightnessSupport works as expected.
+ */
+TEST_F(GraphicsComposerHidlTest, getDisplayBrightnessSupport) {
+    auto capabilities = mComposerClient->getDisplayCapabilities(mPrimaryDisplay);
+    bool brightnessSupport =
+            std::find(capabilities.begin(), capabilities.end(),
+                      IComposerClient::DisplayCapability::BRIGHTNESS) != capabilities.end();
+    EXPECT_EQ(mComposerClient->getDisplayBrightnessSupport(mPrimaryDisplay), brightnessSupport);
+}
+
+/*
+ * Test that if brightness operations are supported, setDisplayBrightness works as expected.
+ */
+TEST_F(GraphicsComposerHidlTest, setDisplayBrightness) {
+    if (!mComposerClient->getDisplayBrightnessSupport(mPrimaryDisplay)) {
+        EXPECT_EQ(mComposerClient->getRaw()->setDisplayBrightness(mPrimaryDisplay, 0.5f),
+                  Error::UNSUPPORTED);
+        GTEST_SUCCEED() << "Brightness operations are not supported";
+    }
+
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 0.0f), Error::NONE);
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 0.5f), Error::NONE);
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, 1.0f), Error::NONE);
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, -1.0f), Error::NONE);
+
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, +2.0f), Error::BAD_PARAMETER);
+    EXPECT_EQ(mComposerClient->setDisplayBrightness(mPrimaryDisplay, -2.0f), Error::BAD_PARAMETER);
+}
+
 }  // namespace
 }  // namespace vts
 }  // namespace V2_3