Merge "Populates the displays required for the tests."
diff --git a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/Android.bp b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/Android.bp
index 8a36b1d..420b32c 100644
--- a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/Android.bp
+++ b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/Android.bp
@@ -29,7 +29,10 @@
         "VtsHalTargetTestDefaults",
         "use_libaidlvintf_gtest_helper_static",
     ],
-    srcs: ["VtsHalGraphicsComposer3_TargetTest.cpp"],
+    srcs: [
+        "VtsHalGraphicsComposer3_TargetTest.cpp",
+        "composer-vts/GraphicsComposerCallback.cpp",
+    ],
 
     // TODO(b/64437680): Assume these libs are always available on the device.
     shared_libs: [
@@ -42,6 +45,7 @@
         "android.hardware.graphics.common@1.2",
         "android.hardware.common-V2-ndk",
         "android.hardware.common.fmq-V1-ndk",
+        "android.hardware.graphics.composer@3-vts",
     ],
 
     test_suites: [
diff --git a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/VtsHalGraphicsComposer3_TargetTest.cpp b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/VtsHalGraphicsComposer3_TargetTest.cpp
index 40dfe06..2dc80fc 100644
--- a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/VtsHalGraphicsComposer3_TargetTest.cpp
+++ b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/VtsHalGraphicsComposer3_TargetTest.cpp
@@ -6,22 +6,28 @@
 #include <android/binder_manager.h>
 #include <android/binder_process.h>
 #include <binder/ProcessState.h>
+#include <composer-vts/include/GraphicsComposerCallback.h>
 #include <gtest/gtest.h>
 #include <string>
+#include <thread>
 
 #pragma push_macro("LOG_TAG")
 #undef LOG_TAG
 #define LOG_TAG "VtsHalGraphicsComposer3_TargetTest"
 
+typedef int32_t Config;
+
 namespace aidl::android::hardware::graphics::composer3::vts {
 namespace {
 
+using namespace std::chrono_literals;
+
 class VtsDisplay {
   public:
-    VtsDisplay(uint64_t displayId, int32_t displayWidth, int32_t displayHeight)
+    VtsDisplay(int64_t displayId, int32_t displayWidth, int32_t displayHeight)
         : mDisplayId(displayId), mDisplayWidth(displayWidth), mDisplayHeight(displayHeight) {}
 
-    uint64_t get() const { return mDisplayId; }
+    int64_t get() const { return mDisplayId; }
 
     void setDimensions(int32_t displayWidth, int32_t displayHeight) {
         mDisplayWidth = displayWidth;
@@ -29,7 +35,7 @@
     }
 
   private:
-    const uint64_t mDisplayId;
+    const int64_t mDisplayId;
     int32_t mDisplayWidth;
     int32_t mDisplayHeight;
 };
@@ -44,6 +50,12 @@
         ASSERT_NE(mComposer, nullptr);
         ASSERT_NO_FATAL_FAILURE(mComposer->createClient(&mComposerClient));
         mInvalidDisplayId = GetInvalidDisplayId();
+
+        mComposerCallback = ::ndk::SharedRefBase::make<GraphicsComposerCallback>();
+        EXPECT_TRUE(mComposerClient->registerCallback(mComposerCallback).isOk());
+
+        // assume the first displays are built-in and are never removed
+        mDisplays = waitForDisplays();
     }
 
     // returns an invalid display id (one that has not been registered to a
@@ -62,11 +74,43 @@
         return 0;
     }
 
+    std::vector<VtsDisplay> waitForDisplays() {
+        while (true) {
+            // Sleep for a small period of time to allow all built-in displays
+            // to post hotplug events
+            std::this_thread::sleep_for(5ms);
+            std::vector<int64_t> displays = mComposerCallback->getDisplays();
+            if (displays.empty()) {
+                continue;
+            }
+
+            std::vector<VtsDisplay> vtsDisplays;
+            vtsDisplays.reserve(displays.size());
+            for (int64_t display : displays) {
+                Config activeConfig;
+                EXPECT_TRUE(mComposerClient->getActiveConfig(display, &activeConfig).isOk());
+                int32_t displayWidth;
+                EXPECT_TRUE(mComposerClient
+                                    ->getDisplayAttribute(display, activeConfig,
+                                                          DisplayAttribute::WIDTH, &displayWidth)
+                                    .isOk());
+                int32_t displayHeight;
+                EXPECT_TRUE(mComposerClient
+                                    ->getDisplayAttribute(display, activeConfig,
+                                                          DisplayAttribute::HEIGHT, &displayHeight)
+                                    .isOk());
+                vtsDisplays.emplace_back(VtsDisplay{display, displayWidth, displayHeight});
+            }
+
+            return vtsDisplays;
+        }
+    }
+
     std::shared_ptr<IComposer> mComposer;
     std::shared_ptr<IComposerClient> mComposerClient;
-    uint64_t mInvalidDisplayId;
-    std::vector<VtsDisplay>
-            mDisplays;  // TODO(b/202401906) populate all the displays available for test.
+    int64_t mInvalidDisplayId;
+    std::vector<VtsDisplay> mDisplays;
+    std::shared_ptr<GraphicsComposerCallback> mComposerCallback;
 };
 
 TEST_P(GraphicsComposerAidlTest, getDisplayCapabilitiesBadDisplay) {
diff --git a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/Android.bp b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/Android.bp
new file mode 100644
index 0000000..bb5f3f1
--- /dev/null
+++ b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/Android.bp
@@ -0,0 +1,44 @@
+/**
+ * Copyright (c) 2021, 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.
+ */
+
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "hardware_interfaces_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["hardware_interfaces_license"],
+}
+
+cc_library_static {
+    name: "android.hardware.graphics.composer@3-vts",
+    defaults: ["hidl_defaults"],
+    srcs: [
+        "GraphicsComposerCallback.cpp",
+    ],
+    static_libs: [
+        "android.hardware.graphics.composer3-V1-ndk",
+        "android.hardware.graphics.common-V3-ndk",
+        "libgtest",
+        "libbase",
+    ],
+    cflags: [
+        "-O0",
+        "-g",
+        "-DLOG_TAG=\"ComposerVts\"",
+    ],
+    export_include_dirs: ["include"],
+}
diff --git a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/GraphicsComposerCallback.cpp b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/GraphicsComposerCallback.cpp
new file mode 100644
index 0000000..daf9924
--- /dev/null
+++ b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/GraphicsComposerCallback.cpp
@@ -0,0 +1,136 @@
+/**
+ * Copyright (c) 2021, 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 "include/GraphicsComposerCallback.h"
+#include <log/log_main.h>
+
+#pragma push_macro("LOG_TAG")
+#undef LOG_TAG
+#define LOG_TAG "GraphicsComposerCallback"
+
+namespace aidl::android::hardware::graphics::composer3::vts {
+
+void GraphicsComposerCallback::setVsyncAllowed(bool allowed) {
+    std::scoped_lock lock(mMutex);
+    mVsyncAllowed = allowed;
+}
+
+std::vector<int64_t> GraphicsComposerCallback::getDisplays() const {
+    std::scoped_lock lock(mMutex);
+    return std::vector<int64_t>(mDisplays.begin(), mDisplays.end());
+}
+
+int32_t GraphicsComposerCallback::getInvalidHotplugCount() const {
+    std::scoped_lock lock(mMutex);
+    return mInvalidHotplugCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidRefreshCount() const {
+    std::scoped_lock lock(mMutex);
+    return mInvalidRefreshCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidVsyncCount() const {
+    std::scoped_lock lock(mMutex);
+    return mInvalidVsyncCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidVsyncPeriodChangeCount() const {
+    std::scoped_lock lock(mMutex);
+    return mInvalidVsyncPeriodChangeCount;
+}
+
+int32_t GraphicsComposerCallback::getInvalidSeamlessPossibleCount() const {
+    std::scoped_lock lock(mMutex);
+    return mInvalidSeamlessPossibleCount;
+}
+
+std::optional<VsyncPeriodChangeTimeline>
+GraphicsComposerCallback::takeLastVsyncPeriodChangeTimeline() {
+    std::scoped_lock lock(mMutex);
+
+    std::optional<VsyncPeriodChangeTimeline> ret;
+    ret.swap(mTimeline);
+
+    return ret;
+}
+
+::ndk::ScopedAStatus GraphicsComposerCallback::onHotplug(int64_t in_display, bool in_connected) {
+    std::scoped_lock lock(mMutex);
+    if (in_connected) {
+        if (!mDisplays.insert(in_display).second) {
+            mInvalidHotplugCount++;
+        }
+    } else {
+        if (!mDisplays.erase(in_display)) {
+            mInvalidHotplugCount++;
+        }
+    }
+    return ::ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus GraphicsComposerCallback::onRefresh(int64_t display) {
+    std::scoped_lock lock(mMutex);
+
+    if (mDisplays.count(display) == 0) {
+        mInvalidRefreshCount++;
+    }
+
+    return ::ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus GraphicsComposerCallback::onVsync(int64_t in_display, int64_t in_timestamp,
+                                                       int32_t in_vsyncPeriodNanos) {
+    std::scoped_lock lock(mMutex);
+    if (!mVsyncAllowed || mDisplays.count(in_display) == 0) {
+        mInvalidVsyncCount++;
+    }
+
+    ALOGV("%ld, %d", static_cast<long>(in_timestamp), in_vsyncPeriodNanos);
+
+    return ::ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus GraphicsComposerCallback::onVsyncPeriodTimingChanged(
+        int64_t in_display,
+        const ::aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline&
+                in_updatedTimeline) {
+    std::scoped_lock lock(mMutex);
+    if (mDisplays.count(in_display) == 0) {
+        mInvalidVsyncPeriodChangeCount++;
+    }
+    mTimeline = in_updatedTimeline;
+
+    return ::ndk::ScopedAStatus::ok();
+}
+
+::ndk::ScopedAStatus GraphicsComposerCallback::onSeamlessPossible(int64_t in_display) {
+    std::scoped_lock lock(mMutex);
+    if (mDisplays.count(in_display)) {
+        mInvalidSeamlessPossibleCount++;
+    }
+    return ::ndk::ScopedAStatus::ok();
+}
+
+::ndk::SpAIBinder GraphicsComposerCallback::asBinder() {
+    return nullptr;
+}
+
+bool GraphicsComposerCallback::isRemote() {
+    return true;
+}
+
+}  // namespace aidl::android::hardware::graphics::composer3::vts
diff --git a/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/include/GraphicsComposerCallback.h b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/include/GraphicsComposerCallback.h
new file mode 100644
index 0000000..9afc72f
--- /dev/null
+++ b/graphics/composer/aidl/android/hardware/graphics/composer3/vts/functional/composer-vts/include/GraphicsComposerCallback.h
@@ -0,0 +1,75 @@
+/**
+ * Copyright (c) 2021, 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 <aidl/android/hardware/graphics/composer3/IComposerCallback.h>
+
+#include <android-base/thread_annotations.h>
+#include <mutex>
+#include <unordered_set>
+
+namespace aidl::android::hardware::graphics::composer3::vts {
+
+// IComposerCallback to be installed with IComposerClient::registerCallback.
+class GraphicsComposerCallback : public IComposerCallback {
+  public:
+    void setVsyncAllowed(bool allowed);
+
+    std::vector<int64_t> getDisplays() const;
+
+    int32_t getInvalidHotplugCount() const;
+
+    int32_t getInvalidRefreshCount() const;
+
+    int32_t getInvalidVsyncCount() const;
+
+    int32_t getInvalidVsyncPeriodChangeCount() const;
+
+    int32_t getInvalidSeamlessPossibleCount() const;
+
+    std::optional<VsyncPeriodChangeTimeline> takeLastVsyncPeriodChangeTimeline();
+
+  private:
+    virtual ::ndk::ScopedAStatus onHotplug(int64_t in_display, bool in_connected) override;
+    virtual ::ndk::ScopedAStatus onRefresh(int64_t in_display) override;
+    virtual ::ndk::ScopedAStatus onSeamlessPossible(int64_t in_display) override;
+    virtual ::ndk::ScopedAStatus onVsync(int64_t in_display, int64_t in_timestamp,
+                                         int32_t in_vsyncPeriodNanos) override;
+    virtual ::ndk::ScopedAStatus onVsyncPeriodTimingChanged(
+            int64_t in_display,
+            const ::aidl::android::hardware::graphics::composer3::VsyncPeriodChangeTimeline&
+                    in_updatedTimeline) override;
+
+    ::ndk::SpAIBinder asBinder() override;
+    bool isRemote() override;
+
+    mutable std::mutex mMutex;
+    // the set of all currently connected displays
+    std::unordered_set<int64_t> mDisplays GUARDED_BY(mMutex);
+    // true only when vsync is enabled
+    bool mVsyncAllowed GUARDED_BY(mMutex) = true;
+
+    std::optional<VsyncPeriodChangeTimeline> mTimeline GUARDED_BY(mMutex);
+
+    // track invalid callbacks
+    int32_t mInvalidHotplugCount GUARDED_BY(mMutex) = 0;
+    int32_t mInvalidRefreshCount GUARDED_BY(mMutex) = 0;
+    int32_t mInvalidVsyncCount GUARDED_BY(mMutex) = 0;
+    int32_t mInvalidVsyncPeriodChangeCount GUARDED_BY(mMutex) = 0;
+    int32_t mInvalidSeamlessPossibleCount GUARDED_BY(mMutex) = 0;
+};
+
+}  // namespace aidl::android::hardware::graphics::composer3::vts
\ No newline at end of file