Merge remote-tracking branch 'aosp/upstream-master' into HEAD am: 37b57553a1

Original change: https://android-review.googlesource.com/c/platform/external/drm_hwcomposer/+/1355362

Change-Id: Ie6697ec47a734ea5599d17a4c41588ee58d9acd5
diff --git a/Android.bp b/Android.bp
index 85be0b5..8bcd1aa 100644
--- a/Android.bp
+++ b/Android.bp
@@ -61,6 +61,12 @@
         "-DHWC2_INCLUDE_STRINGIFICATION",
     ],
 
+    product_variables: {
+        platform_sdk_version: {
+            cflags: ["-DPLATFORM_SDK_VERSION=%d"],
+        },
+    },
+
     relative_install_path: "hw",
     vendor: true,
 }
diff --git a/drmhwctwo.cpp b/drmhwctwo.cpp
index 605406b..5afc96d 100644
--- a/drmhwctwo.cpp
+++ b/drmhwctwo.cpp
@@ -910,9 +910,14 @@
   if (avail_planes < layers_.size())
     avail_planes--;
 
-  std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
+  std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map, z_map_tmp;
+  uint32_t z_index = 0;
+  // First create a map of layers and z_order values
   for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
-    z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
+    z_map_tmp.emplace(std::make_pair(l.second.z_order(), &l.second));
+  // normalise the map so that the lowest z_order layer has key 0
+  for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map_tmp)
+    z_map.emplace(std::make_pair(z_index++, l.second));
 
   uint32_t total_pixops = CalcPixOps(z_map, 0, z_map.size()), gpu_pixops = 0;
 
@@ -963,7 +968,9 @@
 
     MarkValidated(z_map, client_start, client_size);
 
-    if (CreateComposition(true) != HWC2::Error::None) {
+    bool testing_needed = !(client_start == 0 && client_size == z_map.size());
+
+    if (testing_needed && CreateComposition(true) != HWC2::Error::None) {
       ++total_stats_.failed_kms_validate_;
       gpu_pixops = total_pixops;
       client_size = z_map.size();
@@ -997,10 +1004,13 @@
 
   blob = drmModeGetPropertyBlob(drm_->fd(), blob_id);
 
-  outData = static_cast<uint8_t *>(blob->data);
-
+  if (outData) {
+    *outDataSize = std::min(*outDataSize, blob->length);
+    memcpy(outData, blob->data, *outDataSize);
+  } else {
+    *outDataSize = blob->length;
+  }
   *outPort = connector_->id();
-  *outDataSize = blob->length;
 
   return HWC2::Error::None;
 }
@@ -1017,8 +1027,50 @@
 
   return HWC2::Error::None;
 }
+
+HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
+    bool *supported) {
+  *supported = false;
+  return HWC2::Error::None;
+}
+
+HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
+    float /* brightness */) {
+  return HWC2::Error::Unsupported;
+}
+
 #endif /* PLATFORM_SDK_VERSION > 28 */
 
+#if PLATFORM_SDK_VERSION > 27
+
+HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
+    int32_t mode, uint32_t *outNumIntents,
+    int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
+  if (mode != HAL_COLOR_MODE_NATIVE) {
+    return HWC2::Error::BadParameter;
+  }
+
+  if (outIntents == nullptr) {
+    *outNumIntents = 1;
+    return HWC2::Error::None;
+  }
+  *outNumIntents = 1;
+  outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
+  return HWC2::Error::None;
+}
+
+HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
+                                                          int32_t intent) {
+  if (mode != HAL_COLOR_MODE_NATIVE)
+    return HWC2::Error::BadParameter;
+  if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
+    return HWC2::Error::BadParameter;
+  color_mode_ = mode;
+  return HWC2::Error::None;
+}
+
+#endif /* PLATFORM_SDK_VERSION > 27 */
+
 HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
   supported(__func__);
   cursor_x_ = x;
@@ -1037,12 +1089,6 @@
   supported(__func__);
   UniqueFd uf(acquire_fence);
 
-  // The buffer and acquire_fence are handled elsewhere
-  if (sf_type_ == HWC2::Composition::Client ||
-      sf_type_ == HWC2::Composition::Sideband ||
-      sf_type_ == HWC2::Composition::SolidColor)
-    return HWC2::Error::None;
-
   set_buffer(buffer);
   set_acquire_fence(uf.get());
   return HWC2::Error::None;
@@ -1341,6 +1387,17 @@
       return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
           DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
                       &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
+#if PLATFORM_SDK_VERSION > 27
+    case HWC2::FunctionDescriptor::GetRenderIntents:
+      return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
+          DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
+                      &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
+                      int32_t *>);
+    case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
+      return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
+          DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
+                      &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
+#endif
 #if PLATFORM_SDK_VERSION > 28
     case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
       return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
@@ -1352,6 +1409,14 @@
           DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
                       &HwcDisplay::GetDisplayCapabilities, uint32_t *,
                       uint32_t *>);
+    case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
+      return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
+          DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
+                      &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
+    case HWC2::FunctionDescriptor::SetDisplayBrightness:
+      return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
+          DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
+                      &HwcDisplay::SetDisplayBrightness, float>);
 #endif /* PLATFORM_SDK_VERSION > 28 */
     // Layer functions
     case HWC2::FunctionDescriptor::SetCursorPosition:
diff --git a/include/drmhwctwo.h b/include/drmhwctwo.h
index 2bbe334..1f226bc 100644
--- a/include/drmhwctwo.h
+++ b/include/drmhwctwo.h
@@ -190,12 +190,19 @@
                                    uint32_t *num_elements, hwc2_layer_t *layers,
                                    int32_t *layer_requests);
     HWC2::Error GetDisplayType(int32_t *type);
+#if PLATFORM_SDK_VERSION > 27
+    HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents,
+                                 int32_t *outIntents);
+    HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent);
+#endif
 #if PLATFORM_SDK_VERSION > 28
     HWC2::Error GetDisplayIdentificationData(uint8_t *outPort,
                                              uint32_t *outDataSize,
                                              uint8_t *outData);
     HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities,
                                        uint32_t *outCapabilities);
+    HWC2::Error GetDisplayBrightnessSupport(bool *supported);
+    HWC2::Error SetDisplayBrightness(float);
 #endif
     HWC2::Error GetDozeSupport(int32_t *support);
     HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types,