sdm: Check FBT layer handle during prepare

Rely on display properties for invalid FBT layer handle.
This is required since SF doesn't guarantee valid FBT handle
during prepare.

Change-Id: Ie3dbb4d30cb84cf46a50750583f8da1808b82349
diff --git a/sdm/libs/hwc/hwc_display.cpp b/sdm/libs/hwc/hwc_display.cpp
index ef24854..d764a5c 100644
--- a/sdm/libs/hwc/hwc_display.cpp
+++ b/sdm/libs/hwc/hwc_display.cpp
@@ -30,6 +30,7 @@
 #include <math.h>
 #include <errno.h>
 #include <gralloc_priv.h>
+#include <gr.h>
 #include <utils/constants.h>
 #include <qdMetaData.h>
 #include <sync/sync.h>
@@ -330,6 +331,87 @@
   return 0;
 }
 
+int HWCDisplay::PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer, uint32_t fps) {
+  const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer->handle);
+
+  LayerBuffer *layer_buffer = layer->input_buffer;
+
+  if (pvt_handle) {
+    layer_buffer->format = GetSDMFormat(pvt_handle->format, pvt_handle->flags);
+    if (layer_buffer->format == kFormatInvalid) {
+      return -EINVAL;
+    }
+
+    layer_buffer->width = pvt_handle->width;
+    layer_buffer->height = pvt_handle->height;
+    if (pvt_handle->bufferType == BUFFER_TYPE_VIDEO) {
+      layer_stack_.flags.video_present = true;
+      layer_buffer->flags.video = true;
+    }
+    if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
+      layer_stack_.flags.secure_present = true;
+      layer_buffer->flags.secure = true;
+    }
+
+    layer->frame_rate = fps;
+    MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
+    if (meta_data && meta_data->operation & UPDATE_REFRESH_RATE) {
+      layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate);
+    }
+
+    if (meta_data && meta_data->operation == PP_PARAM_INTERLACED && meta_data->interlaced) {
+      layer_buffer->flags.interlace = true;
+    }
+
+    if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
+      layer_buffer->flags.secure_display = true;
+    }
+  } else {
+    // for FBT layer
+    if (hwc_layer->compositionType == HWC_FRAMEBUFFER_TARGET) {
+      uint32_t x_pixels;
+      uint32_t y_pixels;
+      int aligned_width;
+      int aligned_height;
+      int usage = GRALLOC_USAGE_HW_FB;
+      int format = HAL_PIXEL_FORMAT_RGBA_8888;
+      int ubwc_enabled = 0;
+      HWCDebugHandler::Get()->GetProperty("debug.gralloc.enable_fb_ubwc", &ubwc_enabled);
+      if (ubwc_enabled == 1) {
+        usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
+      }
+
+      GetFrameBufferResolution(&x_pixels, &y_pixels);
+
+      AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format,
+                                                            usage, aligned_width, aligned_height);
+      layer_buffer->width = aligned_width;
+      layer_buffer->height = aligned_height;
+      layer->frame_rate = fps;
+    }
+  }
+
+  return 0;
+}
+
+void HWCDisplay::CommitLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer) {
+  const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer->handle);
+  LayerBuffer *layer_buffer = layer->input_buffer;
+
+  if (pvt_handle) {
+    layer_buffer->planes[0].fd = pvt_handle->fd;
+    layer_buffer->planes[0].offset = pvt_handle->offset;
+    layer_buffer->planes[0].stride = pvt_handle->width;
+  }
+
+  // if swapinterval property is set to 0 then close and reset the acquireFd
+  if (swap_interval_zero_ && hwc_layer->acquireFenceFd >= 0) {
+    close(hwc_layer->acquireFenceFd);
+    hwc_layer->acquireFenceFd = -1;
+  }
+  layer_buffer->acquire_fence_fd = hwc_layer->acquireFenceFd;
+}
+
 int HWCDisplay::PrepareLayerStack(hwc_display_contents_1_t *content_list) {
   if (!content_list || !content_list->numHwLayers) {
     DLOGW("Invalid content list");
@@ -345,6 +427,7 @@
   DisplayConfigVariableInfo active_config;
   uint32_t active_config_index = 0;
   display_intf_->GetActiveConfig(&active_config_index);
+  int ret;
 
   display_intf_->GetConfig(active_config_index, &active_config);
 
@@ -356,36 +439,11 @@
     Layer &layer = layer_stack_.layers[i];
     LayerBuffer *layer_buffer = layer.input_buffer;
 
-    if (pvt_handle) {
-      layer_buffer->format = GetSDMFormat(pvt_handle->format, pvt_handle->flags);
-      if (layer_buffer->format == kFormatInvalid) {
-        return -EINVAL;
-      }
+    ret = PrepareLayerParams(&content_list->hwLayers[i], &layer_stack_.layers[i],
+                             active_config.fps);
 
-      layer_buffer->width = pvt_handle->width;
-      layer_buffer->height = pvt_handle->height;
-      if (pvt_handle->bufferType == BUFFER_TYPE_VIDEO) {
-        layer_stack_.flags.video_present = true;
-        layer_buffer->flags.video = true;
-      }
-      if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
-        layer_stack_.flags.secure_present = true;
-        layer_buffer->flags.secure = true;
-      }
-
-      layer.frame_rate = UINT32(active_config.fps);
-      MetaData_t *meta_data = reinterpret_cast<MetaData_t *>(pvt_handle->base_metadata);
-      if (meta_data && meta_data->operation & UPDATE_REFRESH_RATE) {
-        layer.frame_rate = RoundToStandardFPS(meta_data->refreshrate);
-      }
-
-      if (meta_data && meta_data->operation == PP_PARAM_INTERLACED && meta_data->interlaced) {
-        layer_buffer->flags.interlace = true;
-      }
-
-      if (pvt_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) {
-        layer_buffer->flags.secure_display = true;
-      }
+    if (ret != kErrorNone) {
+      return ret;
     }
 
     hwc_rect_t scaled_display_frame = hwc_layer.displayFrame;
@@ -395,7 +453,7 @@
     SetRect(scaled_display_frame, &layer.dst_rect);
     SetRect(hwc_layer.sourceCropf, &layer.src_rect);
     for (size_t j = 0; j < hwc_layer.visibleRegionScreen.numRects; j++) {
-        SetRect(hwc_layer.visibleRegionScreen.rects[j], &layer.visible_regions.rect[j]);
+      SetRect(hwc_layer.visibleRegionScreen.rects[j], &layer.visible_regions.rect[j]);
     }
     SetRect(hwc_layer.dirtyRect, &layer.dirty_regions.rect[0]);
     SetComposition(hwc_layer.compositionType, &layer.composition);
@@ -484,22 +542,7 @@
 
   if (!flush_) {
     for (size_t i = 0; i < num_hw_layers; i++) {
-      hwc_layer_1_t &hwc_layer = content_list->hwLayers[i];
-      const private_handle_t *pvt_handle = static_cast<const private_handle_t *>(hwc_layer.handle);
-      LayerBuffer *layer_buffer = layer_stack_.layers[i].input_buffer;
-
-      if (pvt_handle) {
-        layer_buffer->planes[0].fd = pvt_handle->fd;
-        layer_buffer->planes[0].offset = pvt_handle->offset;
-        layer_buffer->planes[0].stride = pvt_handle->width;
-      }
-
-      // if swapinterval property is set to 0 then close and reset the acquireFd
-      if (swap_interval_zero_ && hwc_layer.acquireFenceFd >= 0) {
-        close(hwc_layer.acquireFenceFd);
-        hwc_layer.acquireFenceFd = -1;
-      }
-      layer_buffer->acquire_fence_fd = hwc_layer.acquireFenceFd;
+      CommitLayerParams(&content_list->hwLayers[i], &layer_stack_.layers[i]);
     }
 
     DisplayError error = display_intf_->Commit(&layer_stack_);
diff --git a/sdm/libs/hwc/hwc_display.h b/sdm/libs/hwc/hwc_display.h
index 2df0928..ce38e5b 100644
--- a/sdm/libs/hwc/hwc_display.h
+++ b/sdm/libs/hwc/hwc_display.h
@@ -98,7 +98,6 @@
   virtual int PrepareLayerStack(hwc_display_contents_1_t *content_list);
   virtual int CommitLayerStack(hwc_display_contents_1_t *content_list);
   virtual int PostCommitLayerStack(hwc_display_contents_1_t *content_list);
-  bool NeedsFrameBufferRefresh(hwc_display_contents_1_t *content_list);
   void CacheLayerStackInfo(hwc_display_contents_1_t *content_list);
   inline void SetRect(const hwc_rect_t &source, LayerRect *target);
   inline void SetRect(const hwc_frect_t &source, LayerRect *target);
@@ -107,11 +106,9 @@
   inline void SetBlending(const int32_t &source, LayerBlending *target);
   int SetFormat(const int32_t &source, const int flags, LayerBufferFormat *target);
   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
-  void DumpInputBuffers(hwc_display_contents_1_t *content_list);
   const char *GetHALPixelFormatString(int format);
   const char *GetDisplayString();
   void ScaleDisplayFrame(hwc_rect_t *display_frame);
-  bool IsFrameBufferScaled();
   void MarkLayersForGPUBypass(hwc_display_contents_1_t *content_list);
   void CloseAcquireFences(hwc_display_contents_1_t *content_list);
   uint32_t RoundToStandardFPS(uint32_t fps);
@@ -141,6 +138,13 @@
   bool display_paused_;
   bool use_metadata_refresh_rate_;
   uint32_t metadata_refresh_rate_;
+
+ private:
+  bool IsFrameBufferScaled();
+  void DumpInputBuffers(hwc_display_contents_1_t *content_list);
+  bool NeedsFrameBufferRefresh(hwc_display_contents_1_t *content_list);
+  int PrepareLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer, uint32_t fps);
+  void CommitLayerParams(hwc_layer_1_t *hwc_layer, Layer *layer);
 };
 
 }  // namespace sdm