Add default template initialization.

Adds a flow for Metadata to initialize templates.
PartialMetadataInterfaces expose another populate method,
which use a default value getter from ControlOptionsInterfaces.

BUG: 30140438
TEST: unit tests pass

Change-Id: I1c01469dcf4d06f7c4c62ebe2acd3d9b2294a161
diff --git a/modules/camera/3_4/metadata/metadata.cpp b/modules/camera/3_4/metadata/metadata.cpp
index 976a77d..ddd73c1 100644
--- a/modules/camera/3_4/metadata/metadata.cpp
+++ b/modules/camera/3_4/metadata/metadata.cpp
@@ -17,6 +17,7 @@
 #include "metadata.h"
 
 #include <camera/CameraMetadata.h>
+#include <hardware/camera3.h>
 
 #include "../common.h"
 #include "metadata_common.h"
@@ -96,6 +97,7 @@
     return -ENODEV;
   }
 
+  // TODO(b/31018853): cache result.
   return 0;
 }
 
@@ -118,6 +120,39 @@
   return true;
 }
 
+int Metadata::GetRequestTemplate(int template_type,
+                                 android::CameraMetadata* template_metadata) {
+  HAL_LOG_ENTER();
+
+  // Templates are numbered 1 through COUNT-1 for some reason.
+  if (template_type < 1 || template_type >= CAMERA3_TEMPLATE_COUNT) {
+    HAL_LOGE("Unrecognized template type %d.", template_type);
+    return -EINVAL;
+  }
+
+  for (auto& component : components_) {
+    // Prevent components from potentially overriding others.
+    android::CameraMetadata additional_metadata;
+    int res =
+        component->PopulateTemplateRequest(template_type, &additional_metadata);
+    if (res) {
+      HAL_LOGE("Failed to get all default request fields.");
+      return res;
+    }
+    // Add it to the overall result.
+    if (!additional_metadata.isEmpty()) {
+      res = template_metadata->append(additional_metadata);
+      if (res != android::OK) {
+        HAL_LOGE("Failed to append all default request fields.");
+        return res;
+      }
+    }
+  }
+
+  // TODO(b/31018853): cache result.
+  return 0;
+}
+
 int Metadata::SetRequestSettings(const android::CameraMetadata& metadata) {
   HAL_LOG_ENTER();
 
@@ -128,7 +163,6 @@
   for (auto& component : components_) {
     int res = component->SetRequestValues(metadata);
     if (res) {
-      // Exit early if possible.
       HAL_LOGE("Failed to set all requested settings.");
       return res;
     }
@@ -143,7 +177,6 @@
     android::CameraMetadata additional_metadata;
     int res = component->PopulateDynamicFields(&additional_metadata);
     if (res) {
-      // Exit early if possible.
       HAL_LOGE("Failed to get all dynamic result fields.");
       return res;
     }