drm_hwcomposer: Move HwcLayer out of DrmHwcTwo class

... to reduce complexity of DrmHwcTwo.* files.

Bump-up tidy level of new files to NORMAL (fix function naming,
add NOLINT, etc.)

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
diff --git a/Android.bp b/Android.bp
index aae6436..0a05376 100644
--- a/Android.bp
+++ b/Android.bp
@@ -110,6 +110,7 @@
         "backend/BackendManager.cpp",
         "backend/BackendRCarDu.cpp",
 
+        "hwc2_device/HwcLayer.cpp",
         "hwc2_device/hwc2_device.cpp",
     ],
 }
diff --git a/DrmHwcTwo.cpp b/DrmHwcTwo.cpp
index 06c1909..76fecf9 100644
--- a/DrmHwcTwo.cpp
+++ b/DrmHwcTwo.cpp
@@ -328,8 +328,8 @@
 }
 
 HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
-  for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
-    l.second.accept_type_change();
+  for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_)
+    l.second.AcceptTypeChange();
   return HWC2::Error::None;
 }
 
@@ -360,12 +360,12 @@
 HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
     uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
   uint32_t num_changes = 0;
-  for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
-    if (l.second.type_changed()) {
+  for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
+    if (l.second.IsTypeChanged()) {
       if (layers && num_changes < *num_elements)
         layers[num_changes] = l.first;
       if (types && num_changes < *num_elements)
-        types[num_changes] = static_cast<int32_t>(l.second.validated_type());
+        types[num_changes] = static_cast<int32_t>(l.second.GetValidatedType());
       ++num_changes;
     }
   }
@@ -669,7 +669,7 @@
                                                     int32_t *fences) {
   uint32_t num_layers = 0;
 
-  for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
+  for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
     ++num_layers;
     if (layers == nullptr || fences == nullptr)
       continue;
@@ -680,7 +680,7 @@
     }
 
     layers[num_layers - 1] = l.first;
-    fences[num_layers - 1] = l.second.release_fence_.Release();
+    fences[num_layers - 1] = l.second.GetReleaseFence().Release();
   }
   *num_elements = num_layers;
   return HWC2::Error::None;
@@ -690,16 +690,16 @@
   // order the layers by z-order
   bool use_client_layer = false;
   uint32_t client_z_order = UINT32_MAX;
-  std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
-  for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
-    switch (l.second.validated_type()) {
+  std::map<uint32_t, HwcLayer *> z_map;
+  for (std::pair<const hwc2_layer_t, HwcLayer> &l : layers_) {
+    switch (l.second.GetValidatedType()) {
       case HWC2::Composition::Device:
-        z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
+        z_map.emplace(std::make_pair(l.second.GetZOrder(), &l.second));
         break;
       case HWC2::Composition::Client:
         // Place it at the z_order of the lowest client layer
         use_client_layer = true;
-        client_z_order = std::min(client_z_order, l.second.z_order());
+        client_z_order = std::min(client_z_order, l.second.GetZOrder());
         break;
       default:
         continue;
@@ -714,7 +714,7 @@
   std::vector<DrmHwcLayer> composition_layers;
 
   // now that they're ordered by z, add them to the composition
-  for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
+  for (std::pair<const uint32_t, HwcLayer *> &l : z_map) {
     DrmHwcLayer layer;
     l.second->PopulateDrmLayer(&layer);
     int ret = layer.ImportBuffer(drm_);
@@ -821,10 +821,18 @@
                                                    int32_t acquire_fence,
                                                    int32_t dataspace,
                                                    hwc_region_t /*damage*/) {
-  client_layer_.set_buffer(target);
-  client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
+  client_layer_.SetLayerBuffer(target, acquire_fence);
   client_layer_.SetLayerDataspace(dataspace);
 
+  /*
+   * target can be nullptr, this does mean the Composer Service is calling
+   * cleanDisplayResources() on after receiving HOTPLUG event. See more at:
+   * https://cs.android.com/android/platform/superproject/+/master:hardware/interfaces/graphics/composer/2.1/utils/hal/include/composer-hal/2.1/ComposerClient.h;l=350;drc=944b68180b008456ed2eb4d4d329e33b19bd5166
+   */
+  if (target == nullptr) {
+    return HWC2::Error::None;
+  }
+
   /* TODO: Do not update source_crop every call.
    * It makes sense to do it once after every hotplug event. */
   HwcDrmBo bo{};
@@ -915,9 +923,8 @@
   return backend_->ValidateDisplay(this, num_types, num_requests);
 }
 
-std::vector<DrmHwcTwo::HwcLayer *>
-DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
-  std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
+std::vector<HwcLayer *> DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
+  std::vector<HwcLayer *> ordered_layers;
   ordered_layers.reserve(layers_.size());
 
   for (auto &[handle, layer] : layers_) {
@@ -925,8 +932,8 @@
   }
 
   std::sort(std::begin(ordered_layers), std::end(ordered_layers),
-            [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
-              return lhs->z_order() < rhs->z_order();
+            [](const HwcLayer *lhs, const HwcLayer *rhs) {
+              return lhs->GetZOrder() < rhs->GetZOrder();
             });
 
   return ordered_layers;
@@ -1078,158 +1085,6 @@
   backend_ = std::move(backend);
 }
 
-HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
-                                                   int32_t /*y*/) {
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
-  switch (static_cast<HWC2::BlendMode>(mode)) {
-    case HWC2::BlendMode::None:
-      blending_ = DrmHwcBlending::kNone;
-      break;
-    case HWC2::BlendMode::Premultiplied:
-      blending_ = DrmHwcBlending::kPreMult;
-      break;
-    case HWC2::BlendMode::Coverage:
-      blending_ = DrmHwcBlending::kCoverage;
-      break;
-    default:
-      ALOGE("Unknown blending mode b=%d", blending_);
-      blending_ = DrmHwcBlending::kNone;
-      break;
-  }
-  return HWC2::Error::None;
-}
-
-/* Find API details at:
- * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
- */
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
-                                                int32_t acquire_fence) {
-  set_buffer(buffer);
-  acquire_fence_ = UniqueFd(acquire_fence);
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
-  // TODO(nobody): Put to client composition here?
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
-  sf_type_ = static_cast<HWC2::Composition>(type);
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
-  switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
-    case HAL_DATASPACE_STANDARD_BT709:
-      color_space_ = DrmHwcColorSpace::kItuRec709;
-      break;
-    case HAL_DATASPACE_STANDARD_BT601_625:
-    case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
-    case HAL_DATASPACE_STANDARD_BT601_525:
-    case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
-      color_space_ = DrmHwcColorSpace::kItuRec601;
-      break;
-    case HAL_DATASPACE_STANDARD_BT2020:
-    case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
-      color_space_ = DrmHwcColorSpace::kItuRec2020;
-      break;
-    default:
-      color_space_ = DrmHwcColorSpace::kUndefined;
-  }
-
-  switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
-    case HAL_DATASPACE_RANGE_FULL:
-      sample_range_ = DrmHwcSampleRange::kFullRange;
-      break;
-    case HAL_DATASPACE_RANGE_LIMITED:
-      sample_range_ = DrmHwcSampleRange::kLimitedRange;
-      break;
-    default:
-      sample_range_ = DrmHwcSampleRange::kUndefined;
-  }
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
-  display_frame_ = frame;
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
-  alpha_ = alpha;
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
-    const native_handle_t * /*stream*/) {
-  // TODO(nobody): We don't support sideband
-  return HWC2::Error::Unsupported;
-  ;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
-  source_crop_ = crop;
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(
-    hwc_region_t /*damage*/) {
-  // TODO(nobody): We don't use surface damage, marking as unsupported
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
-  uint32_t l_transform = 0;
-
-  // 270* and 180* cannot be combined with flips. More specifically, they
-  // already contain both horizontal and vertical flips, so those fields are
-  // redundant in this case. 90* rotation can be combined with either horizontal
-  // flip or vertical flip, so treat it differently
-  if (transform == HWC_TRANSFORM_ROT_270) {
-    l_transform = DrmHwcTransform::kRotate270;
-  } else if (transform == HWC_TRANSFORM_ROT_180) {
-    l_transform = DrmHwcTransform::kRotate180;
-  } else {
-    if (transform & HWC_TRANSFORM_FLIP_H)
-      l_transform |= DrmHwcTransform::kFlipH;
-    if (transform & HWC_TRANSFORM_FLIP_V)
-      l_transform |= DrmHwcTransform::kFlipV;
-    if (transform & HWC_TRANSFORM_ROT_90)
-      l_transform |= DrmHwcTransform::kRotate90;
-  }
-
-  transform_ = static_cast<DrmHwcTransform>(l_transform);
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(
-    hwc_region_t /*visible*/) {
-  // TODO(nobody): We don't use this information, marking as unsupported
-  return HWC2::Error::None;
-}
-
-HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
-  z_order_ = order;
-  return HWC2::Error::None;
-}
-
-void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
-  layer->sf_handle = buffer_;
-  // TODO(rsglobal): Avoid extra fd duplication
-  layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
-  layer->display_frame = display_frame_;
-  layer->alpha = std::lround(65535.0F * alpha_);
-  layer->blending = blending_;
-  layer->source_crop = source_crop_;
-  layer->transform = transform_;
-  layer->color_space = color_space_;
-  layer->sample_range = sample_range_;
-}
-
 void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
   const std::lock_guard<std::mutex> lock(callback_lock_);
 
diff --git a/DrmHwcTwo.h b/DrmHwcTwo.h
index 5ba9ee1..de70199 100644
--- a/DrmHwcTwo.h
+++ b/DrmHwcTwo.h
@@ -28,6 +28,7 @@
 #include "drm/ResourceManager.h"
 #include "drm/VSyncWorker.h"
 #include "drmhwcomposer.h"
+#include "hwc2_device/HwcLayer.h"
 
 namespace android {
 
@@ -48,97 +49,6 @@
 
   std::mutex callback_lock_;
 
-  class HwcLayer {
-   public:
-    HWC2::Composition sf_type() const {
-      return sf_type_;
-    }
-    HWC2::Composition validated_type() const {
-      return validated_type_;
-    }
-    void accept_type_change() {
-      sf_type_ = validated_type_;
-    }
-    void set_validated_type(HWC2::Composition type) {
-      validated_type_ = type;
-    }
-    bool type_changed() const {
-      return sf_type_ != validated_type_;
-    }
-
-    uint32_t z_order() const {
-      return z_order_;
-    }
-
-    buffer_handle_t buffer() {
-      return buffer_;
-    }
-    void set_buffer(buffer_handle_t buffer) {
-      buffer_ = buffer;
-    }
-
-    hwc_rect_t display_frame() {
-      return display_frame_;
-    }
-
-    void PopulateDrmLayer(DrmHwcLayer *layer);
-
-    bool RequireScalingOrPhasing() const {
-      float src_width = source_crop_.right - source_crop_.left;
-      float src_height = source_crop_.bottom - source_crop_.top;
-
-      auto dest_width = float(display_frame_.right - display_frame_.left);
-      auto dest_height = float(display_frame_.bottom - display_frame_.top);
-
-      bool scaling = src_width != dest_width || src_height != dest_height;
-      bool phasing = (source_crop_.left - std::floor(source_crop_.left) != 0) ||
-                     (source_crop_.top - std::floor(source_crop_.top) != 0);
-      return scaling || phasing;
-    }
-
-    // Layer hooks
-    HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
-    HWC2::Error SetLayerBlendMode(int32_t mode);
-    HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
-    HWC2::Error SetLayerColor(hwc_color_t /*color*/);
-    HWC2::Error SetLayerCompositionType(int32_t type);
-    HWC2::Error SetLayerDataspace(int32_t dataspace);
-    HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
-    HWC2::Error SetLayerPlaneAlpha(float alpha);
-    HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
-    HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
-    HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
-    HWC2::Error SetLayerTransform(int32_t transform);
-    HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
-    HWC2::Error SetLayerZOrder(uint32_t order);
-
-    UniqueFd acquire_fence_;
-
-    /*
-     * Release fence is not used.
-     * There is no release fence support available in the DRM/KMS. In case no
-     * release fence provided application will use this buffer for writing when
-     * the next frame present fence is signaled.
-     */
-    UniqueFd release_fence_;
-
-   private:
-    // sf_type_ stores the initial type given to us by surfaceflinger,
-    // validated_type_ stores the type after running ValidateDisplay
-    HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
-    HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
-
-    buffer_handle_t buffer_ = nullptr;
-    hwc_rect_t display_frame_;
-    float alpha_ = 1.0F;
-    hwc_frect_t source_crop_;
-    DrmHwcTransform transform_ = DrmHwcTransform::kIdentity;
-    uint32_t z_order_ = 0;
-    DrmHwcBlending blending_ = DrmHwcBlending::kNone;
-    DrmHwcColorSpace color_space_ = DrmHwcColorSpace::kUndefined;
-    DrmHwcSampleRange sample_range_ = DrmHwcSampleRange::kUndefined;
-  };
-
   class HwcDisplay {
    public:
     HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm,
@@ -147,7 +57,7 @@
     HWC2::Error Init(std::vector<DrmPlane *> *planes);
 
     HWC2::Error CreateComposition(AtomicCommitArgs &a_args);
-    std::vector<DrmHwcTwo::HwcLayer *> GetOrderLayersByZPos();
+    std::vector<HwcLayer *> GetOrderLayersByZPos();
 
     void ClearDisplay();
 
diff --git a/backend/Backend.cpp b/backend/Backend.cpp
index 6f334c7..7f87b05 100644
--- a/backend/Backend.cpp
+++ b/backend/Backend.cpp
@@ -67,8 +67,7 @@
 }
 
 std::tuple<int, size_t> Backend::GetClientLayers(
-    DrmHwcTwo::HwcDisplay *display,
-    const std::vector<DrmHwcTwo::HwcLayer *> &layers) {
+    DrmHwcTwo::HwcDisplay *display, const std::vector<HwcLayer *> &layers) {
   int client_start = -1;
   size_t client_size = 0;
 
@@ -83,10 +82,9 @@
   return GetExtraClientRange(display, layers, client_start, client_size);
 }
 
-bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
-                            DrmHwcTwo::HwcLayer *layer) {
-  return !HardwareSupportsLayerType(layer->sf_type()) ||
-         !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
+bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display, HwcLayer *layer) {
+  return !HardwareSupportsLayerType(layer->GetSfType()) ||
+         !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->GetBuffer()) ||
          display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
          (layer->RequireScalingOrPhasing() &&
           display->resource_manager()->ForcedScalingWithGpu());
@@ -97,32 +95,31 @@
          comp_type == HWC2::Composition::Cursor;
 }
 
-uint32_t Backend::CalcPixOps(const std::vector<DrmHwcTwo::HwcLayer *> &layers,
+uint32_t Backend::CalcPixOps(const std::vector<HwcLayer *> &layers,
                              size_t first_z, size_t size) {
   uint32_t pixops = 0;
   for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
     if (z_order >= first_z && z_order < first_z + size) {
-      hwc_rect_t df = layers[z_order]->display_frame();
+      hwc_rect_t df = layers[z_order]->GetDisplayFrame();
       pixops += (df.right - df.left) * (df.bottom - df.top);
     }
   }
   return pixops;
 }
 
-void Backend::MarkValidated(std::vector<DrmHwcTwo::HwcLayer *> &layers,
+void Backend::MarkValidated(std::vector<HwcLayer *> &layers,
                             size_t client_first_z, size_t client_size) {
   for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
     if (z_order >= client_first_z && z_order < client_first_z + client_size)
-      layers[z_order]->set_validated_type(HWC2::Composition::Client);
+      layers[z_order]->SetValidatedType(HWC2::Composition::Client);
     else
-      layers[z_order]->set_validated_type(HWC2::Composition::Device);
+      layers[z_order]->SetValidatedType(HWC2::Composition::Device);
   }
 }
 
 std::tuple<int, int> Backend::GetExtraClientRange(
-    DrmHwcTwo::HwcDisplay *display,
-    const std::vector<DrmHwcTwo::HwcLayer *> &layers, int client_start,
-    size_t client_size) {
+    DrmHwcTwo::HwcDisplay *display, const std::vector<HwcLayer *> &layers,
+    int client_start, size_t client_size) {
   size_t avail_planes = display->primary_planes().size() +
                         display->overlay_planes().size();
 
diff --git a/backend/Backend.h b/backend/Backend.h
index 38376aa..0273570 100644
--- a/backend/Backend.h
+++ b/backend/Backend.h
@@ -28,21 +28,18 @@
                                       uint32_t *num_types,
                                       uint32_t *num_requests);
   virtual std::tuple<int, size_t> GetClientLayers(
-      DrmHwcTwo::HwcDisplay *display,
-      const std::vector<DrmHwcTwo::HwcLayer *> &layers);
-  virtual bool IsClientLayer(DrmHwcTwo::HwcDisplay *display,
-                             DrmHwcTwo::HwcLayer *layer);
+      DrmHwcTwo::HwcDisplay *display, const std::vector<HwcLayer *> &layers);
+  virtual bool IsClientLayer(DrmHwcTwo::HwcDisplay *display, HwcLayer *layer);
 
  protected:
   static bool HardwareSupportsLayerType(HWC2::Composition comp_type);
-  static uint32_t CalcPixOps(const std::vector<DrmHwcTwo::HwcLayer *> &layers,
+  static uint32_t CalcPixOps(const std::vector<HwcLayer *> &layers,
                              size_t first_z, size_t size);
-  static void MarkValidated(std::vector<DrmHwcTwo::HwcLayer *> &layers,
+  static void MarkValidated(std::vector<HwcLayer *> &layers,
                             size_t client_first_z, size_t client_size);
   static std::tuple<int, int> GetExtraClientRange(
-      DrmHwcTwo::HwcDisplay *display,
-      const std::vector<DrmHwcTwo::HwcLayer *> &layers, int client_start,
-      size_t client_size);
+      DrmHwcTwo::HwcDisplay *display, const std::vector<HwcLayer *> &layers,
+      int client_start, size_t client_size);
 };
 }  // namespace android
 
diff --git a/backend/BackendClient.cpp b/backend/BackendClient.cpp
index 49963b8..73a3b3d 100644
--- a/backend/BackendClient.cpp
+++ b/backend/BackendClient.cpp
@@ -24,7 +24,7 @@
                                            uint32_t *num_types,
                                            uint32_t * /*num_requests*/) {
   for (auto & [ layer_handle, layer ] : display->layers()) {
-    layer.set_validated_type(HWC2::Composition::Client);
+    layer.SetValidatedType(HWC2::Composition::Client);
     ++*num_types;
   }
   return HWC2::Error::HasChanges;
diff --git a/backend/BackendRCarDu.cpp b/backend/BackendRCarDu.cpp
index 9adfa62..b319d89 100644
--- a/backend/BackendRCarDu.cpp
+++ b/backend/BackendRCarDu.cpp
@@ -23,10 +23,10 @@
 namespace android {
 
 bool BackendRCarDu::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
-                                  DrmHwcTwo::HwcLayer *layer) {
+                                  HwcLayer *layer) {
   hwc_drm_bo_t bo;
 
-  int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(layer->buffer(),
+  int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(layer->GetBuffer(),
                                                            &bo);
   if (ret != 0)
     return true;
diff --git a/backend/BackendRCarDu.h b/backend/BackendRCarDu.h
index 8a1011a..862bef2 100644
--- a/backend/BackendRCarDu.h
+++ b/backend/BackendRCarDu.h
@@ -23,8 +23,7 @@
 
 class BackendRCarDu : public Backend {
  public:
-  bool IsClientLayer(DrmHwcTwo::HwcDisplay *display,
-                     DrmHwcTwo::HwcLayer *layer) override;
+  bool IsClientLayer(DrmHwcTwo::HwcDisplay *display, HwcLayer *layer) override;
 };
 }  // namespace android
 
diff --git a/hwc2_device/HwcLayer.cpp b/hwc2_device/HwcLayer.cpp
new file mode 100644
index 0000000..66babda
--- /dev/null
+++ b/hwc2_device/HwcLayer.cpp
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#define LOG_TAG "hwc-layer"
+
+#include "HwcLayer.h"
+
+#include <fcntl.h>
+
+#include "utils/log.h"
+
+namespace android {
+
+// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
+HWC2::Error HwcLayer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerBlendMode(int32_t mode) {
+  switch (static_cast<HWC2::BlendMode>(mode)) {
+    case HWC2::BlendMode::None:
+      blending_ = DrmHwcBlending::kNone;
+      break;
+    case HWC2::BlendMode::Premultiplied:
+      blending_ = DrmHwcBlending::kPreMult;
+      break;
+    case HWC2::BlendMode::Coverage:
+      blending_ = DrmHwcBlending::kCoverage;
+      break;
+    default:
+      ALOGE("Unknown blending mode b=%d", blending_);
+      blending_ = DrmHwcBlending::kNone;
+      break;
+  }
+  return HWC2::Error::None;
+}
+
+/* Find API details at:
+ * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
+ */
+HWC2::Error HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
+                                     int32_t acquire_fence) {
+  buffer_ = buffer;
+  acquire_fence_ = UniqueFd(acquire_fence);
+  return HWC2::Error::None;
+}
+
+// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
+HWC2::Error HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
+  // TODO(nobody): Put to client composition here?
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerCompositionType(int32_t type) {
+  sf_type_ = static_cast<HWC2::Composition>(type);
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerDataspace(int32_t dataspace) {
+  switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
+    case HAL_DATASPACE_STANDARD_BT709:
+      color_space_ = DrmHwcColorSpace::kItuRec709;
+      break;
+    case HAL_DATASPACE_STANDARD_BT601_625:
+    case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
+    case HAL_DATASPACE_STANDARD_BT601_525:
+    case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
+      color_space_ = DrmHwcColorSpace::kItuRec601;
+      break;
+    case HAL_DATASPACE_STANDARD_BT2020:
+    case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
+      color_space_ = DrmHwcColorSpace::kItuRec2020;
+      break;
+    default:
+      color_space_ = DrmHwcColorSpace::kUndefined;
+  }
+
+  switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
+    case HAL_DATASPACE_RANGE_FULL:
+      sample_range_ = DrmHwcSampleRange::kFullRange;
+      break;
+    case HAL_DATASPACE_RANGE_LIMITED:
+      sample_range_ = DrmHwcSampleRange::kLimitedRange;
+      break;
+    default:
+      sample_range_ = DrmHwcSampleRange::kUndefined;
+  }
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
+  display_frame_ = frame;
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerPlaneAlpha(float alpha) {
+  alpha_ = alpha;
+  return HWC2::Error::None;
+}
+
+// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
+HWC2::Error HwcLayer::SetLayerSidebandStream(
+    const native_handle_t * /*stream*/) {
+  // TODO(nobody): We don't support sideband
+  return HWC2::Error::Unsupported;
+}
+
+HWC2::Error HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
+  source_crop_ = crop;
+  return HWC2::Error::None;
+}
+
+// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
+HWC2::Error HwcLayer::SetLayerSurfaceDamage(hwc_region_t /*damage*/) {
+  // TODO(nobody): We don't use surface damage, marking as unsupported
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerTransform(int32_t transform) {
+  uint32_t l_transform = 0;
+
+  // 270* and 180* cannot be combined with flips. More specifically, they
+  // already contain both horizontal and vertical flips, so those fields are
+  // redundant in this case. 90* rotation can be combined with either horizontal
+  // flip or vertical flip, so treat it differently
+  if (transform == HWC_TRANSFORM_ROT_270) {
+    l_transform = DrmHwcTransform::kRotate270;
+  } else if (transform == HWC_TRANSFORM_ROT_180) {
+    l_transform = DrmHwcTransform::kRotate180;
+  } else {
+    if ((transform & HWC_TRANSFORM_FLIP_H) != 0)
+      l_transform |= DrmHwcTransform::kFlipH;
+    if ((transform & HWC_TRANSFORM_FLIP_V) != 0)
+      l_transform |= DrmHwcTransform::kFlipV;
+    if ((transform & HWC_TRANSFORM_ROT_90) != 0)
+      l_transform |= DrmHwcTransform::kRotate90;
+  }
+
+  transform_ = static_cast<DrmHwcTransform>(l_transform);
+  return HWC2::Error::None;
+}
+
+// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
+HWC2::Error HwcLayer::SetLayerVisibleRegion(hwc_region_t /*visible*/) {
+  // TODO(nobody): We don't use this information, marking as unsupported
+  return HWC2::Error::None;
+}
+
+HWC2::Error HwcLayer::SetLayerZOrder(uint32_t order) {
+  z_order_ = order;
+  return HWC2::Error::None;
+}
+
+void HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
+  layer->sf_handle = buffer_;
+  // TODO(rsglobal): Avoid extra fd duplication
+  layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
+  layer->display_frame = display_frame_;
+  layer->alpha = std::lround(alpha_ * UINT16_MAX);
+  layer->blending = blending_;
+  layer->source_crop = source_crop_;
+  layer->transform = transform_;
+  layer->color_space = color_space_;
+  layer->sample_range = sample_range_;
+}
+
+}  // namespace android
\ No newline at end of file
diff --git a/hwc2_device/HwcLayer.h b/hwc2_device/HwcLayer.h
new file mode 100644
index 0000000..df4ce6d
--- /dev/null
+++ b/hwc2_device/HwcLayer.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2022 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.
+ */
+
+#ifndef ANDROID_HWC2_DEVICE_HWC_LAYER_H
+#define ANDROID_HWC2_DEVICE_HWC_LAYER_H
+
+#include <hardware/hwcomposer2.h>
+
+#include <cmath>
+
+#include "drmhwcomposer.h"
+
+namespace android {
+
+class HwcLayer {
+ public:
+  HWC2::Composition GetSfType() const {
+    return sf_type_;
+  }
+  HWC2::Composition GetValidatedType() const {
+    return validated_type_;
+  }
+  void AcceptTypeChange() {
+    sf_type_ = validated_type_;
+  }
+  void SetValidatedType(HWC2::Composition type) {
+    validated_type_ = type;
+  }
+  bool IsTypeChanged() const {
+    return sf_type_ != validated_type_;
+  }
+
+  uint32_t GetZOrder() const {
+    return z_order_;
+  }
+
+  buffer_handle_t GetBuffer() {
+    return buffer_;
+  }
+
+  hwc_rect_t GetDisplayFrame() {
+    return display_frame_;
+  }
+
+  UniqueFd GetReleaseFence() {
+    return std::move(release_fence_);
+  }
+
+  void PopulateDrmLayer(DrmHwcLayer *layer);
+
+  bool RequireScalingOrPhasing() const {
+    float src_width = source_crop_.right - source_crop_.left;
+    float src_height = source_crop_.bottom - source_crop_.top;
+
+    auto dest_width = float(display_frame_.right - display_frame_.left);
+    auto dest_height = float(display_frame_.bottom - display_frame_.top);
+
+    bool scaling = src_width != dest_width || src_height != dest_height;
+    bool phasing = (source_crop_.left - std::floor(source_crop_.left) != 0) ||
+                   (source_crop_.top - std::floor(source_crop_.top) != 0);
+    return scaling || phasing;
+  }
+
+  // Layer hooks
+  HWC2::Error SetCursorPosition(int32_t /*x*/, int32_t /*y*/);
+  HWC2::Error SetLayerBlendMode(int32_t mode);
+  HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence);
+  HWC2::Error SetLayerColor(hwc_color_t /*color*/);
+  HWC2::Error SetLayerCompositionType(int32_t type);
+  HWC2::Error SetLayerDataspace(int32_t dataspace);
+  HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame);
+  HWC2::Error SetLayerPlaneAlpha(float alpha);
+  HWC2::Error SetLayerSidebandStream(const native_handle_t *stream);
+  HWC2::Error SetLayerSourceCrop(hwc_frect_t crop);
+  HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage);
+  HWC2::Error SetLayerTransform(int32_t transform);
+  HWC2::Error SetLayerVisibleRegion(hwc_region_t visible);
+  HWC2::Error SetLayerZOrder(uint32_t order);
+
+ private:
+  // sf_type_ stores the initial type given to us by surfaceflinger,
+  // validated_type_ stores the type after running ValidateDisplay
+  HWC2::Composition sf_type_ = HWC2::Composition::Invalid;
+  HWC2::Composition validated_type_ = HWC2::Composition::Invalid;
+
+  buffer_handle_t buffer_ = nullptr;
+  hwc_rect_t display_frame_;
+  static constexpr float kOpaqueFloat = 1.0F;
+  float alpha_ = kOpaqueFloat;
+  hwc_frect_t source_crop_;
+  DrmHwcTransform transform_ = DrmHwcTransform::kIdentity;
+  uint32_t z_order_ = 0;
+  DrmHwcBlending blending_ = DrmHwcBlending::kNone;
+  DrmHwcColorSpace color_space_ = DrmHwcColorSpace::kUndefined;
+  DrmHwcSampleRange sample_range_ = DrmHwcSampleRange::kUndefined;
+
+  UniqueFd acquire_fence_;
+
+  /*
+   * Release fence is not used.
+   * There is no release fence support available in the DRM/KMS. In case no
+   * release fence provided application will use this buffer for writing when
+   * the next frame present fence is signaled.
+   */
+  UniqueFd release_fence_;
+};
+
+}  // namespace android
+
+#endif
diff --git a/hwc2_device/hwc2_device.cpp b/hwc2_device/hwc2_device.cpp
index 24edda7..72074ad 100644
--- a/hwc2_device/hwc2_device.cpp
+++ b/hwc2_device/hwc2_device.cpp
@@ -63,7 +63,7 @@
   if (!display)
     return static_cast<int32_t>(HWC2::Error::BadDisplay);
 
-  DrmHwcTwo::HwcLayer *layer = display->get_layer(layer_handle);
+  HwcLayer *layer = display->get_layer(layer_handle);
   if (!layer)
     return static_cast<int32_t>(HWC2::Error::BadLayer);
 
@@ -292,62 +292,61 @@
     // Layer functions
     case HWC2::FunctionDescriptor::SetCursorPosition:
       return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetCursorPosition),
-                    &DrmHwcTwo::HwcLayer::SetCursorPosition, int32_t, int32_t>);
+          LayerHook<decltype(&HwcLayer::SetCursorPosition),
+                    &HwcLayer::SetCursorPosition, int32_t, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerBlendMode:
       return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerBlendMode),
-                    &DrmHwcTwo::HwcLayer::SetLayerBlendMode, int32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
+                    &HwcLayer::SetLayerBlendMode, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerBuffer:
       return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerBuffer),
-                    &DrmHwcTwo::HwcLayer::SetLayerBuffer, buffer_handle_t,
-                    int32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerBuffer),
+                    &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerColor:
       return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerColor),
-                    &DrmHwcTwo::HwcLayer::SetLayerColor, hwc_color_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerColor),
+                    &HwcLayer::SetLayerColor, hwc_color_t>);
     case HWC2::FunctionDescriptor::SetLayerCompositionType:
       return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerCompositionType),
-                    &DrmHwcTwo::HwcLayer::SetLayerCompositionType, int32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
+                    &HwcLayer::SetLayerCompositionType, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerDataspace:
       return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerDataspace),
-                    &DrmHwcTwo::HwcLayer::SetLayerDataspace, int32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerDataspace),
+                    &HwcLayer::SetLayerDataspace, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
       return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerDisplayFrame),
-                    &DrmHwcTwo::HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
+                    &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
     case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
       return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha),
-                    &DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha, float>);
+          LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
+                    &HwcLayer::SetLayerPlaneAlpha, float>);
     case HWC2::FunctionDescriptor::SetLayerSidebandStream:
       return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerSidebandStream),
-                    &DrmHwcTwo::HwcLayer::SetLayerSidebandStream,
+          LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
+                    &HwcLayer::SetLayerSidebandStream,
                     const native_handle_t *>);
     case HWC2::FunctionDescriptor::SetLayerSourceCrop:
       return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerSourceCrop),
-                    &DrmHwcTwo::HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
+                    &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
     case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
       return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage),
-                    &DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
+                    &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
     case HWC2::FunctionDescriptor::SetLayerTransform:
       return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerTransform),
-                    &DrmHwcTwo::HwcLayer::SetLayerTransform, int32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerTransform),
+                    &HwcLayer::SetLayerTransform, int32_t>);
     case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
       return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerVisibleRegion),
-                    &DrmHwcTwo::HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
+                    &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
     case HWC2::FunctionDescriptor::SetLayerZOrder:
       return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
-          LayerHook<decltype(&DrmHwcTwo::HwcLayer::SetLayerZOrder),
-                    &DrmHwcTwo::HwcLayer::SetLayerZOrder, uint32_t>);
+          LayerHook<decltype(&HwcLayer::SetLayerZOrder),
+                    &HwcLayer::SetLayerZOrder, uint32_t>);
     case HWC2::FunctionDescriptor::Invalid:
     default:
       return nullptr;