composer: vts: send a refresh command when required + test fix

When calling to setActiveConfigWithConstraints, the implementation may
need the client to send a refresh frame before the active config can be
changed. In addition, testing with a device with composer 2.4 revealed
few bugs which are fixed by this change.

Fix: 143775556
Test: adb shell data/nativetest64/VtsHalGraphicsComposerV2_4TargetTest/VtsHalGraphicsComposerV2_4TargetTest
Change-Id: Iafa1e85de60d99190d5d813f1d42924a62d94cc5
diff --git a/graphics/composer/2.4/utils/vts/Android.bp b/graphics/composer/2.4/utils/vts/Android.bp
index b87a116..673c15e 100644
--- a/graphics/composer/2.4/utils/vts/Android.bp
+++ b/graphics/composer/2.4/utils/vts/Android.bp
@@ -19,6 +19,7 @@
     defaults: ["hidl_defaults"],
     srcs: [
         "ComposerVts.cpp",
+        "GraphicsComposerCallback.cpp",
     ],
     static_libs: [
         "VtsHalHidlTargetTestBase",
diff --git a/graphics/composer/2.4/utils/vts/ComposerVts.cpp b/graphics/composer/2.4/utils/vts/ComposerVts.cpp
index 5b06d6d..8a9c006 100644
--- a/graphics/composer/2.4/utils/vts/ComposerVts.cpp
+++ b/graphics/composer/2.4/utils/vts/ComposerVts.cpp
@@ -84,6 +84,10 @@
     return value;
 }
 
+void ComposerClient::registerCallback_2_4(const sp<IComposerCallback>& callback) {
+    mClient->registerCallback_2_4(callback);
+}
+
 Error ComposerClient::getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriod) {
     Error error = Error::NONE;
     mClient->getDisplayVsyncPeriod(display, [&](const auto& tmpError, const auto& tmpVsyncPeriod) {
diff --git a/graphics/composer/2.4/utils/vts/GraphicsComposerCallback.cpp b/graphics/composer/2.4/utils/vts/GraphicsComposerCallback.cpp
new file mode 100644
index 0000000..c9366a8
--- /dev/null
+++ b/graphics/composer/2.4/utils/vts/GraphicsComposerCallback.cpp
@@ -0,0 +1,137 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <composer-vts/2.4/GraphicsComposerCallback.h>
+
+namespace android::hardware::graphics::composer::V2_4::vts {
+
+void GraphicsComposerCallback::setVsyncAllowed(bool allowed) {
+    std::lock_guard<std::mutex> lock(mMutex);
+    mVsyncAllowed = allowed;
+}
+
+std::vector<Display> GraphicsComposerCallback::getDisplays() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return std::vector<Display>(mDisplays.begin(), mDisplays.end());
+}
+
+int32_t GraphicsComposerCallback::getInvalidHotplugCount() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidHotplugCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidRefreshCount() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidRefreshCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidVsyncCount() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidVsyncCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidVsync_2_4Count() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidVsync_2_4Count;
+}
+
+int32_t GraphicsComposerCallback::getInvalidVsyncPeriodChangeCount() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidVsyncPeriodChangeCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidSeamlessPossibleCount() const {
+    std::lock_guard<std::mutex> lock(mMutex);
+    return mInvalidSeamlessPossibleCount;
+}
+
+std::optional<VsyncPeriodChangeTimeline>
+GraphicsComposerCallback::takeLastVsyncPeriodChangeTimeline() {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    std::optional<VsyncPeriodChangeTimeline> ret;
+    ret.swap(mTimeline);
+
+    return ret;
+}
+
+Return<void> GraphicsComposerCallback::onHotplug(Display display, Connection connection) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    if (connection == Connection::CONNECTED) {
+        if (!mDisplays.insert(display).second) {
+            mInvalidHotplugCount++;
+        }
+    } else if (connection == Connection::DISCONNECTED) {
+        if (!mDisplays.erase(display)) {
+            mInvalidHotplugCount++;
+        }
+    }
+
+    return Void();
+}
+
+Return<void> GraphicsComposerCallback::onRefresh(Display display) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    if (mDisplays.count(display) == 0) {
+        mInvalidRefreshCount++;
+    }
+
+    return Void();
+}
+
+Return<void> GraphicsComposerCallback::onVsync(Display, int64_t) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    // On composer 2.4, onVsync is not expected at all
+    mInvalidVsyncCount++;
+
+    return Void();
+}
+
+Return<void> GraphicsComposerCallback::onVsync_2_4(Display display, int64_t, VsyncPeriodNanos) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    if (!mVsyncAllowed || mDisplays.count(display) == 0) {
+        mInvalidVsync_2_4Count++;
+    }
+
+    return Void();
+}
+
+Return<void> GraphicsComposerCallback::onVsyncPeriodTimingChanged(
+        Display display, const VsyncPeriodChangeTimeline& updatedTimeline) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    if (mDisplays.count(display) == 0) {
+        mInvalidVsyncPeriodChangeCount++;
+    }
+
+    mTimeline = updatedTimeline;
+
+    return Void();
+}
+
+Return<void> GraphicsComposerCallback::onSeamlessPossible(Display) {
+    std::lock_guard<std::mutex> lock(mMutex);
+
+    mInvalidSeamlessPossibleCount++;
+
+    return Void();
+}
+
+}  // namespace android::hardware::graphics::composer::V2_4::vts
\ No newline at end of file
diff --git a/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/ComposerVts.h b/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/ComposerVts.h
index b094bc8..df75a48 100644
--- a/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/ComposerVts.h
+++ b/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/ComposerVts.h
@@ -77,6 +77,8 @@
     int32_t getDisplayAttribute_2_4(Display display, Config config,
                                     IComposerClient::Attribute attribute);
 
+    void registerCallback_2_4(const sp<IComposerCallback>& callback);
+
     Error getDisplayVsyncPeriod(Display display, VsyncPeriodNanos* outVsyncPeriods);
 
     Error setActiveConfigWithConstraints(
diff --git a/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/GraphicsComposerCallback.h b/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/GraphicsComposerCallback.h
new file mode 100644
index 0000000..f4e23ae
--- /dev/null
+++ b/graphics/composer/2.4/utils/vts/include/composer-vts/2.4/GraphicsComposerCallback.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include <android/hardware/graphics/composer/2.4/IComposerCallback.h>
+
+#include <mutex>
+#include <unordered_set>
+
+namespace android::hardware::graphics::composer::V2_4::vts {
+
+using Display = V2_1::Display;
+
+// IComposerCallback to be installed with IComposerClient::registerCallback.
+class GraphicsComposerCallback : public IComposerCallback {
+  public:
+    void setVsyncAllowed(bool allowed);
+
+    std::vector<Display> getDisplays() const;
+
+    int32_t getInvalidHotplugCount() const;
+
+    int32_t getInvalidRefreshCount() const;
+
+    int32_t getInvalidVsyncCount() const;
+
+    int32_t getInvalidVsync_2_4Count() const;
+
+    int32_t getInvalidVsyncPeriodChangeCount() const;
+
+    int32_t getInvalidSeamlessPossibleCount() const;
+
+    std::optional<VsyncPeriodChangeTimeline> takeLastVsyncPeriodChangeTimeline();
+
+  private:
+    Return<void> onHotplug(Display display, Connection connection) override;
+    Return<void> onRefresh(Display display) override;
+    Return<void> onVsync(Display display, int64_t) override;
+    Return<void> onVsync_2_4(Display display, int64_t, VsyncPeriodNanos vsyncPeriodNanos) override;
+    Return<void> onVsyncPeriodTimingChanged(
+            Display display, const VsyncPeriodChangeTimeline& updatedTimeline) override;
+    Return<void> onSeamlessPossible(Display display) override;
+
+    mutable std::mutex mMutex;
+    // the set of all currently connected displays
+    std::unordered_set<Display> mDisplays;
+    // true only when vsync is enabled
+    bool mVsyncAllowed = true;
+
+    std::optional<VsyncPeriodChangeTimeline> mTimeline;
+
+    // track invalid callbacks
+    int32_t mInvalidHotplugCount = 0;
+    int32_t mInvalidRefreshCount = 0;
+    int32_t mInvalidVsyncCount = 0;
+    int32_t mInvalidVsync_2_4Count = 0;
+    int32_t mInvalidVsyncPeriodChangeCount = 0;
+    int32_t mInvalidSeamlessPossibleCount = 0;
+};
+
+}  // namespace android::hardware::graphics::composer::V2_4::vts