Merge "hwc2: Address remaining review comments"
diff --git a/sdm/libs/hwc2/hwc_display.cpp b/sdm/libs/hwc2/hwc_display.cpp
index d9b3c73..14cbbdb 100644
--- a/sdm/libs/hwc2/hwc_display.cpp
+++ b/sdm/libs/hwc2/hwc_display.cpp
@@ -128,10 +128,10 @@
 
 // LayerStack operations
 HWC2::Error HWCDisplay::CreateLayer(hwc2_layer_t *out_layer_id) {
-  auto layer = *layer_set_.emplace(new HWCLayer(id_));
+  HWCLayer *layer = *layer_set_.emplace(new HWCLayer(id_));
   layer_map_.emplace(std::make_pair(layer->GetId(), layer));
   *out_layer_id = layer->GetId();
-  geometry_changes_ = GeometryChanges::kAdded;
+  geometry_changes_ |= GeometryChanges::kAdded;
   return HWC2::Error::None;
 }
 
@@ -161,7 +161,7 @@
     }
   }
 
-  geometry_changes_ = GeometryChanges::kRemoved;
+  geometry_changes_ |= GeometryChanges::kRemoved;
   return HWC2::Error::None;
 }
 
@@ -535,8 +535,11 @@
     if (!needs_fb_refresh && composition == kCompositionGPU) {
       composition = kCompositionSDE;
     }
+    HWC2::Composition current_hwc_composition  = hwc_layer->GetCompositionType();
+    // Convert the SDM layer composition to HWC2 type
     hwc_layer->SetComposition(composition);
-    if (hwc_layer->CompositionChanged()) {
+    // Update the changes list only if the HWC2 comp type changed from the previous cycle
+    if (current_hwc_composition != hwc_layer->GetCompositionType()) {
       layer_changes_[hwc_layer->GetId()] = hwc_layer->GetCompositionType();
     }
   }
@@ -1357,6 +1360,7 @@
     auto layer = hwc_layer->GetSDMLayer();
     if (layer->input_buffer->acquire_fence_fd >= 0) {
       close(layer->input_buffer->acquire_fence_fd);
+      layer->input_buffer->acquire_fence_fd = -1;
     }
   }
   int32_t &client_target_acquire_fence =
diff --git a/sdm/libs/hwc2/hwc_display.h b/sdm/libs/hwc2/hwc_display.h
index 6430538..dd2b55d 100644
--- a/sdm/libs/hwc2/hwc_display.h
+++ b/sdm/libs/hwc2/hwc_display.h
@@ -159,14 +159,14 @@
     OUTPUT_LAYER_DUMP,
   };
 
-  CoreInterface *core_intf_;
-  HWCCallbacks *callbacks_;
+  CoreInterface *core_intf_ = nullptr;
+  HWCCallbacks *callbacks_  = nullptr;
   DisplayType type_;
   hwc2_display_t id_;
   bool needs_blit_ = false;
   DisplayInterface *display_intf_ = NULL;
   LayerStack layer_stack_;
-  HWCLayer *client_target_;                             // Also known as framebuffer target
+  HWCLayer *client_target_ = nullptr;                   // Also known as framebuffer target
   std::map<hwc2_layer_t, HWCLayer *> layer_map_;        // Look up by Id - TODO
   std::multiset<HWCLayer *, SortLayersByZ> layer_set_;  // Maintain a set sorted by Z
   std::map<hwc2_layer_t, HWC2::Composition> layer_changes_;
@@ -202,7 +202,7 @@
   BlitEngine *blit_engine_ = NULL;
   qService::QService *qservice_ = NULL;
   DisplayClass display_class_;
-  int32_t stored_retire_fence_;
+  int32_t stored_retire_fence_ = -1;
   uint32_t geometry_changes_ = GeometryChanges::kNone;
 };
 
diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp
index 9be2317..ada1cac 100644
--- a/sdm/libs/hwc2/hwc_layers.cpp
+++ b/sdm/libs/hwc2/hwc_layers.cpp
@@ -26,10 +26,6 @@
 
 namespace sdm {
 
-bool SortLayersByZ::operator()(const HWCLayer *lhs, const HWCLayer *rhs) {
-  return lhs->GetZ() < rhs->GetZ();
-}
-
 std::atomic<hwc2_layer_t> HWCLayer::next_id_(1);
 
 // Layer operations
@@ -170,7 +166,7 @@
 
 HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) {
   // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter
-  layer_->plane_alpha = static_cast<uint8_t>(255.0f * alpha + 0.5f);
+  layer_->plane_alpha = static_cast<uint8_t>(std::round(255.0f * alpha));
   geometry_changes_ |= kPlaneAlpha;
   return HWC2::Error::None;
 }
@@ -337,6 +333,36 @@
     case HAL_PIXEL_FORMAT_YCbCr_422_I:
       format = kFormatYCbCr422H2V1Packed;
       break;
+    case HAL_PIXEL_FORMAT_RGBA_1010102:
+      format = kFormatRGBA1010102;
+      break;
+    case HAL_PIXEL_FORMAT_ARGB_2101010:
+      format = kFormatARGB2101010;
+      break;
+    case HAL_PIXEL_FORMAT_RGBX_1010102:
+      format = kFormatRGBX1010102;
+      break;
+    case HAL_PIXEL_FORMAT_XRGB_2101010:
+      format = kFormatXRGB2101010;
+      break;
+    case HAL_PIXEL_FORMAT_BGRA_1010102:
+      format = kFormatBGRA1010102;
+      break;
+    case HAL_PIXEL_FORMAT_ABGR_2101010:
+      format = kFormatABGR2101010;
+      break;
+    case HAL_PIXEL_FORMAT_BGRX_1010102:
+      format = kFormatBGRX1010102;
+      break;
+    case HAL_PIXEL_FORMAT_XBGR_2101010:
+      format = kFormatXBGR2101010;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_P010:
+      format = kFormatYCbCr420P010;
+      break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
+      format = kFormatYCbCr420TP10Ubwc;
+      break;
     default:
       DLOGW("Unsupported format type = %d", source);
       return kFormatInvalid;
@@ -480,11 +506,7 @@
   if (layer_->composition == kCompositionSDE && layer_->flags.solid_fill != 0) {
     composition = HWC2::Composition::SolidColor;
   }
-  if (composition != composition_) {
-    // Composition changed for this layer
-    composition_changed_ = true;
-    composition_ = composition;
-  }
+  composition_ = composition;
 }
 void HWCLayer::PushReleaseFence(int32_t fence) {
   release_fences_.push(fence);
diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h
index de3b7bb..b756c38 100644
--- a/sdm/libs/hwc2/hwc_layers.h
+++ b/sdm/libs/hwc2/hwc_layers.h
@@ -38,16 +38,16 @@
 namespace sdm {
 
 enum GeometryChanges {
-  kNone = 0x00,
-  kBlendMode = 0x01,
-  kDataspace = 0x02,
-  kDisplayFrame = 0x04,
-  kPlaneAlpha = 0x08,
-  kSourceCrop = 0x0A,
-  kTransform = 0x10,
-  kZOrder = 0x12,
-  kAdded = 0x14,
-  kRemoved = 0x18,
+  kNone         = 0x000,
+  kBlendMode    = 0x001,
+  kDataspace    = 0x002,
+  kDisplayFrame = 0x004,
+  kPlaneAlpha   = 0x008,
+  kSourceCrop   = 0x010,
+  kTransform    = 0x020,
+  kZOrder       = 0x040,
+  kAdded        = 0x080,
+  kRemoved      = 0x100,
 };
 
 class HWCLayer {
@@ -71,7 +71,6 @@
   HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
   HWC2::Error SetLayerZOrder(uint32_t z);
   void SetComposition(const LayerComposition &source);
-  bool CompositionChanged(void) { return composition_changed_; }
   HWC2::Composition GetCompositionType() { return composition_; }
   uint32_t GetGeometryChanges() { return geometry_changes_; }
   void ResetGeometryChanges() { geometry_changes_ = GeometryChanges::kNone; }
@@ -87,7 +86,6 @@
   std::queue<int32_t> release_fences_;
 
   HWC2::Composition composition_ = HWC2::Composition::Device;
-  bool composition_changed_ = false;
   uint32_t geometry_changes_ = GeometryChanges::kNone;
 
   void SetRect(const hwc_rect_t &source, LayerRect *target);
@@ -102,7 +100,9 @@
 };
 
 struct SortLayersByZ {
-  bool operator()(const HWCLayer *lhs, const HWCLayer *rhs);
+  bool operator()(const HWCLayer *lhs, const HWCLayer *rhs) {
+    return lhs->GetZ() < rhs->GetZ();
+  }
 };
 
 }  // namespace sdm
diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp
index a7d954c..e774e55 100644
--- a/sdm/libs/hwc2/hwc_session.cpp
+++ b/sdm/libs/hwc2/hwc_session.cpp
@@ -391,9 +391,8 @@
   auto desc = static_cast<HWC2::Callback>(descriptor);
   auto error = hwc_session->callbacks_.Register(desc, callback_data, pointer);
   DLOGD("Registering callback: %s", to_string(desc).c_str());
-  // TODO(user): The hotplug should only be called when the HOTPLUG callback is registered
-  // However, this causes SurfaceFlinger to behave weirdly - investigate further.
-  hwc_session->callbacks_.Hotplug(HWC_DISPLAY_PRIMARY, HWC2::Connection::Connected);
+  if (descriptor == HWC2_CALLBACK_HOTPLUG)
+    hwc_session->callbacks_.Hotplug(HWC_DISPLAY_PRIMARY, HWC2::Connection::Connected);
   return INT32(error);
 }