hwc3: create the interface for expectedPresentTime

The expectedPresentTime is an optional value specified in
validateDisplay and presentOrValidateDisplay. The patch creates the
proper command dispatchers to read the information only in the two
functions. A warning message will show if the expectedPresentTime is
provided multiple times within one frame update.

Bug: 198186194
Test: VtsHalGraphicsComposer3_TargetTest
Change-Id: Ifff31202bef3297c1ddc28189ed301a7f6175695
diff --git a/hwc3/ComposerCommandEngine.cpp b/hwc3/ComposerCommandEngine.cpp
index 062ec1e..0c92824 100644
--- a/hwc3/ComposerCommandEngine.cpp
+++ b/hwc3/ComposerCommandEngine.cpp
@@ -46,6 +46,13 @@
         }                                                                    \
     } while (0)
 
+#define DISPATCH_DISPLAY_BOOL_COMMAND_AND_DATA(displayCmd, field, data, funcName) \
+    do {                                                                          \
+        if (displayCmd.field) {                                                   \
+            execute##funcName(displayCmd.display, displayCmd.data);               \
+        }                                                                         \
+    } while (0)
+
 bool ComposerCommandEngine::init() {
     mWriter = std::make_unique<ComposerServiceWriter>();
     return (mWriter != nullptr);
@@ -73,10 +80,12 @@
     DISPATCH_DISPLAY_COMMAND(command, virtualDisplayOutputBuffer, SetOutputBuffer);
     // TODO: (b/196171661) SDR & HDR blending
     // DISPATCH_DISPLAY_COMMAND(command, displayBrightness, SetDisplayBrightness);
-    DISPATCH_DISPLAY_BOOL_COMMAND(command, validateDisplay, ValidateDisplay);
+    DISPATCH_DISPLAY_BOOL_COMMAND_AND_DATA(command, validateDisplay, expectedPresentTime,
+                                           ValidateDisplay);
     DISPATCH_DISPLAY_BOOL_COMMAND(command, acceptDisplayChanges, AcceptDisplayChanges);
     DISPATCH_DISPLAY_BOOL_COMMAND(command, presentDisplay, PresentDisplay);
-    DISPATCH_DISPLAY_BOOL_COMMAND(command, presentOrValidateDisplay, PresentOrValidateDisplay);
+    DISPATCH_DISPLAY_BOOL_COMMAND_AND_DATA(command, presentOrValidateDisplay, expectedPresentTime,
+                                           PresentOrValidateDisplay);
 }
 
 void ComposerCommandEngine::dispatchLayerCommand(int64_t display, const LayerCommand& command) {
@@ -178,11 +187,21 @@
     }
 }
 
-void ComposerCommandEngine::executeValidateDisplay(int64_t display) {
+void ComposerCommandEngine::executeSetExpectedPresentTimeInternal(
+        int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime) {
+    mHal->setExpectedPresentTime(display, expectedPresentTime);
+}
+
+void ComposerCommandEngine::executeValidateDisplay(
+        int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime) {
+    executeSetExpectedPresentTimeInternal(display, expectedPresentTime);
     executeValidateDisplayInternal(display);
 }
 
-void ComposerCommandEngine::executePresentOrValidateDisplay(int64_t display) {
+void ComposerCommandEngine::executePresentOrValidateDisplay(
+        int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime) {
+    executeSetExpectedPresentTimeInternal(display, expectedPresentTime);
+
     int err;
     // First try to Present as is.
     if (mHal->hasCapability(Capability::SKIP_VALIDATE)) {
diff --git a/hwc3/ComposerCommandEngine.h b/hwc3/ComposerCommandEngine.h
index ff43c6e..437f1b4 100644
--- a/hwc3/ComposerCommandEngine.h
+++ b/hwc3/ComposerCommandEngine.h
@@ -44,8 +44,10 @@
       void executeSetColorTransform(int64_t display, const std::vector<float>& matrix);
       void executeSetClientTarget(int64_t display, const ClientTarget& command);
       void executeSetOutputBuffer(uint64_t display, const Buffer& buffer);
-      void executeValidateDisplay(int64_t display);
-      void executePresentOrValidateDisplay(int64_t display);
+      void executeValidateDisplay(int64_t display,
+                                  const std::optional<ClockMonotonicTimestamp> expectedPresentTime);
+      void executePresentOrValidateDisplay(
+              int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime);
       void executeAcceptDisplayChanges(int64_t display);
       int executePresentDisplay(int64_t display);
 
@@ -84,6 +86,8 @@
               const std::vector<std::optional<PerFrameMetadataBlob>>& perFrameMetadataBlob);
 
       int32_t executeValidateDisplayInternal(int64_t display);
+      void executeSetExpectedPresentTimeInternal(
+              int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime);
 
       IComposerHal* mHal;
       IResourceManager* mResources;
diff --git a/hwc3/impl/HalImpl.cpp b/hwc3/impl/HalImpl.cpp
index e3afeb5..74f850d 100644
--- a/hwc3/impl/HalImpl.cpp
+++ b/hwc3/impl/HalImpl.cpp
@@ -931,4 +931,20 @@
     return HWC2_ERROR_NONE;
 }
 
+int HalImpl::setExpectedPresentTime(
+        int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime) {
+    ExynosDisplay* halDisplay;
+    RET_IF_ERR(getHalDisplay(display, halDisplay));
+
+    if (!expectedPresentTime.has_value()) return HWC2_ERROR_NONE;
+
+    if (halDisplay->getPendingExpectedPresentTime() != 0) {
+        ALOGW("HalImpl: set expected present time multiple times in one frame");
+    }
+
+    halDisplay->setExpectedPresentTime(expectedPresentTime->timestampNanos);
+
+    return HWC2_ERROR_NONE;
+}
+
 } // namespace aidl::android::hardware::graphics::composer3::impl
diff --git a/hwc3/impl/HalImpl.h b/hwc3/impl/HalImpl.h
index 4ed4c15..eaf712d 100644
--- a/hwc3/impl/HalImpl.h
+++ b/hwc3/impl/HalImpl.h
@@ -136,6 +136,9 @@
                             std::vector<int64_t>* outRequestedLayers,
                             std::vector<int32_t>* outRequestMasks,
                             ClientTargetProperty* outClientTargetProperty) override;
+    int32_t setExpectedPresentTime(
+            int64_t display,
+            const std::optional<ClockMonotonicTimestamp> expectedPresentTime) override;
 
     EventCallback* getEventCallback() { return mEventCallback; }
 
diff --git a/hwc3/include/IComposerHal.h b/hwc3/include/IComposerHal.h
index 2586cbd..791a69d 100644
--- a/hwc3/include/IComposerHal.h
+++ b/hwc3/include/IComposerHal.h
@@ -201,7 +201,9 @@
                                     uint32_t* outDisplayRequestMask,
                                     std::vector<int64_t>* outRequestedLayers,
                                     std::vector<int32_t>* outRequestMasks,
-                                    ClientTargetProperty* outClientTargetProperty) = 0; //cmd
+                                    ClientTargetProperty* outClientTargetProperty) = 0;
+    virtual int32_t setExpectedPresentTime(
+            int64_t display, const std::optional<ClockMonotonicTimestamp> expectedPresentTime) = 0;
 };
 
 } // namespace aidl::android::hardware::graphics::composer3::detail