Merge "gralloc/qdutils: Remove unused gralloc code, add driver type check"
diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp
index 583767a..7190fc0 100644
--- a/libgralloc/alloc_controller.cpp
+++ b/libgralloc/alloc_controller.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 - 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011 - 2017, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -107,7 +107,6 @@
 
 //------------- MDPCapabilityInfo-----------------------//
 MDPCapabilityInfo :: MDPCapabilityInfo() {
-  qdutils::querySDEInfo(HAS_MACRO_TILE, &isMacroTileSupported);
   qdutils::querySDEInfo(HAS_UBWC, &isUBwcSupported);
   qdutils::querySDEInfo(HAS_WB_UBWC, &isWBUBWCSupported);
 }
@@ -117,7 +116,6 @@
 {
     LINK_adreno_compute_aligned_width_and_height = NULL;
     LINK_adreno_compute_padding = NULL;
-    LINK_adreno_isMacroTilingSupportedByGpu = NULL;
     LINK_adreno_compute_compressedfmt_aligned_width_and_height = NULL;
     LINK_adreno_isUBWCSupportedByGpu = NULL;
     LINK_adreno_get_gpu_pixel_alignment = NULL;
@@ -128,8 +126,6 @@
                 ::dlsym(libadreno_utils, "compute_aligned_width_and_height");
         *(void **)&LINK_adreno_compute_padding =
                 ::dlsym(libadreno_utils, "compute_surface_padding");
-        *(void **)&LINK_adreno_isMacroTilingSupportedByGpu =
-                ::dlsym(libadreno_utils, "isMacroTilingSupportedByGpu");
         *(void **)&LINK_adreno_compute_compressedfmt_aligned_width_and_height =
                 ::dlsym(libadreno_utils,
                         "compute_compressedfmt_aligned_width_and_height");
@@ -157,16 +153,6 @@
     }
 }
 
-int AdrenoMemInfo::isMacroTilingSupportedByGPU()
-{
-    if ((libadreno_utils)) {
-        if(LINK_adreno_isMacroTilingSupportedByGpu) {
-            return LINK_adreno_isMacroTilingSupportedByGpu();
-        }
-    }
-    return 0;
-}
-
 void AdrenoMemInfo::getAlignedWidthAndHeight(const private_handle_t *hnd, int& aligned_w,
                           int& aligned_h) {
     MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
@@ -241,7 +227,7 @@
 
     // Currently surface padding is only computed for RGB* surfaces.
     if (isUncompressedRgbFormat(format) == true) {
-        int tileEnabled = ubwc_enabled || isMacroTileEnabled(format, usage);
+        int tileEnabled = ubwc_enabled;
         getGpuAlignedWidthHeight(width, height, format, tileEnabled, aligned_w, aligned_h);
     } else if (ubwc_enabled) {
         getYuvUBwcWidthHeight(width, height, format, aligned_w, aligned_h);
@@ -538,38 +524,6 @@
     return memalloc;
 }
 
-bool isMacroTileEnabled(int format, int usage)
-{
-    bool tileEnabled = false;
-    // Check whether GPU & MDSS supports MacroTiling feature
-    if(AdrenoMemInfo::getInstance().isMacroTilingSupportedByGPU() &&
-       MDPCapabilityInfo::getInstance().isMacroTilingSupportedByMDP())
-    {
-        // check the format
-        switch(format)
-        {
-            case  HAL_PIXEL_FORMAT_RGBA_8888:
-            case  HAL_PIXEL_FORMAT_RGBX_8888:
-            case  HAL_PIXEL_FORMAT_BGRA_8888:
-            case  HAL_PIXEL_FORMAT_RGB_565:
-            case  HAL_PIXEL_FORMAT_BGR_565:
-                {
-                    tileEnabled = true;
-                    // check the usage flags
-                    if (usage & (GRALLOC_USAGE_SW_READ_MASK |
-                                GRALLOC_USAGE_SW_WRITE_MASK)) {
-                        // Application intends to use CPU for rendering
-                        tileEnabled = false;
-                    }
-                    break;
-                }
-            default:
-                break;
-        }
-    }
-    return tileEnabled;
-}
-
 // helper function
 unsigned int getSize(int format, int width, int height, int usage,
         const int alignedw, const int alignedh) {
@@ -736,20 +690,6 @@
     return size;
 }
 
-void getBufferAttributes(int width, int height, int format, int usage,
-        int& alignedw, int &alignedh, int& tiled, unsigned int& size)
-{
-    tiled = isUBwcEnabled(format, usage) || isMacroTileEnabled(format, usage);
-
-    AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
-            height,
-            format,
-            usage,
-            alignedw,
-            alignedh);
-    size = getSize(format, width, height, usage, alignedw, alignedh);
-}
-
 void getYuvUbwcSPPlaneInfo(uint64_t base, int width, int height,
                            int color_format, struct android_ycbcr* ycbcr)
 {
diff --git a/libgralloc/gpu.cpp b/libgralloc/gpu.cpp
index 5911314..a0339b4 100644
--- a/libgralloc/gpu.cpp
+++ b/libgralloc/gpu.cpp
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2010 The Android Open Source Project
- * Copyright (c) 2011-2014 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2014,2017 The Linux Foundation. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -134,10 +134,6 @@
             flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
         }
 
-        if(isMacroTileEnabled(format, usage)) {
-            flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
-        }
-
         if (isUBwcEnabled(format, usage)) {
             flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
         }
diff --git a/libgralloc/gr.h b/libgralloc/gr.h
index 7d055fc..ae26df9 100644
--- a/libgralloc/gr.h
+++ b/libgralloc/gr.h
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2008 The Android Open Source Project
- * Copyright (c) 2011 - 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011 - 2017, The Linux Foundation. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -54,15 +54,6 @@
 unsigned int getBufferSizeAndDimensions(int width, int height, int format,
         int& alignedw, int &alignedh);
 
-
-// Attributes include aligned width, aligned height, tileEnabled and size of the buffer
-void getBufferAttributes(int width, int height, int format, int usage,
-                           int& alignedw, int &alignedh,
-                           int& tileEnabled, unsigned int &size);
-
-
-bool isMacroTileEnabled(int format, int usage);
-
 int decideBufferHandlingMechanism(int format, const char *compositionUsed,
                                   int hasBlitEngine, int *needConversion,
                                   int *useBufferDirectly);
@@ -148,15 +139,6 @@
      */
     void getUnalignedWidthAndHeight(const private_handle_t *hnd, int& unaligned_w,
                             int& unaligned_h);
-
-    /*
-     * Function to return whether GPU support MacroTile feature
-     *
-     * @return >0 : supported
-     *          0 : not supported
-     */
-    int isMacroTilingSupportedByGPU();
-
     /*
      * Function to query whether GPU supports UBWC for given HAL format
      * @return > 0 : supported
@@ -190,8 +172,6 @@
                                                 int *aligned_w,
                                                 int *aligned_h);
 
-        int (*LINK_adreno_isMacroTilingSupportedByGpu) (void);
-
         void(*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
                                                 int width,
                                                 int height,
@@ -211,20 +191,12 @@
 
 class MDPCapabilityInfo : public android::Singleton <MDPCapabilityInfo>
 {
-    int isMacroTileSupported = 0;
     int isUBwcSupported = 0;
     int isWBUBWCSupported = 0;
 
     public:
         MDPCapabilityInfo();
         /*
-        * Function to return whether MDP support MacroTile feature
-        *
-        * @return  1 : supported
-        *          0 : not supported
-        */
-        int isMacroTilingSupportedByMDP() { return isMacroTileSupported; }
-        /*
         * Function to return whether MDP supports UBWC feature
         *
         * @return  1 : supported
diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp
index 132c768..acf5e2c 100644
--- a/libgralloc/mapper.cpp
+++ b/libgralloc/mapper.cpp
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2008 The Android Open Source Project
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -397,8 +397,7 @@
                 int *alignedWidth = va_arg(args, int *);
                 int *alignedHeight = va_arg(args, int *);
                 int *tileEnabled = va_arg(args,int *);
-                *tileEnabled = isUBwcEnabled(format, usage) ||
-                               isMacroTileEnabled(format, usage);
+                *tileEnabled = isUBwcEnabled(format, usage);
                 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
                         height, format, usage, *alignedWidth, *alignedHeight);
                 res = 0;
diff --git a/libgralloc1/gr_adreno_info.cpp b/libgralloc1/gr_adreno_info.cpp
index adb59f0..0692ca6 100644
--- a/libgralloc1/gr_adreno_info.cpp
+++ b/libgralloc1/gr_adreno_info.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -47,8 +47,6 @@
         ::dlsym(libadreno_utils_, "compute_aligned_width_and_height");
     *reinterpret_cast<void **>(&LINK_adreno_compute_padding) =
         ::dlsym(libadreno_utils_, "compute_surface_padding");
-    *reinterpret_cast<void **>(&LINK_adreno_isMacroTilingSupportedByGpu) =
-        ::dlsym(libadreno_utils_, "isMacroTilingSupportedByGpu");
     *reinterpret_cast<void **>(&LINK_adreno_compute_compressedfmt_aligned_width_and_height) =
         ::dlsym(libadreno_utils_, "compute_compressedfmt_aligned_width_and_height");
     *reinterpret_cast<void **>(&LINK_adreno_isUBWCSupportedByGpu) =
@@ -84,14 +82,6 @@
   }
 }
 
-bool AdrenoMemInfo::IsMacroTilingSupportedByGPU() {
-  if (LINK_adreno_isMacroTilingSupportedByGpu) {
-    return LINK_adreno_isMacroTilingSupportedByGpu();
-  }
-
-  return false;
-}
-
 void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled,
                                          unsigned int *aligned_w, unsigned int *aligned_h) {
   *aligned_w = (unsigned int)ALIGN(width, 32);
diff --git a/libgralloc1/gr_adreno_info.h b/libgralloc1/gr_adreno_info.h
index 7b053ad..9b239c7 100644
--- a/libgralloc1/gr_adreno_info.h
+++ b/libgralloc1/gr_adreno_info.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -112,14 +112,6 @@
   uint32_t GetGpuPixelAlignment();
 
   /*
-   * Function to return whether GPU support MacroTile feature
-   *
-   * @return >0 : supported
-   *          0 : not supported
-   */
-  bool IsMacroTilingSupportedByGPU();
-
-  /*
    * Function to query whether GPU supports UBWC for given HAL format
    * @return > 0 : supported
    *           0 : not supported
@@ -139,7 +131,6 @@
                                                        int tile_mode, int raster_mode,
                                                        int padding_threshold, int *aligned_w,
                                                        int *aligned_h) = NULL;
-  int (*LINK_adreno_isMacroTilingSupportedByGpu)(void) = NULL;
   void (*LINK_adreno_compute_compressedfmt_aligned_width_and_height)(
       int width, int height, int format, int tile_mode, int raster_mode, int padding_threshold,
       int *aligned_w, int *aligned_h, int *bpp) = NULL;
diff --git a/libgralloc1/gr_allocator.cpp b/libgralloc1/gr_allocator.cpp
index 578b2f6..fefd664 100644
--- a/libgralloc1/gr_allocator.cpp
+++ b/libgralloc1/gr_allocator.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -84,11 +84,6 @@
     return false;
   }
 
-  gpu_support_macrotile = adreno_helper_->IsMacroTilingSupportedByGPU();
-  int supports_macrotile = 0;
-  qdutils::querySDEInfo(qdutils::HAS_MACRO_TILE, &supports_macrotile);
-  display_support_macrotile = !!supports_macrotile;
-
   return true;
 }
 
@@ -221,34 +216,6 @@
   return true;
 }
 
-bool Allocator::IsMacroTileEnabled(int format, gralloc1_producer_usage_t prod_usage,
-                                   gralloc1_consumer_usage_t cons_usage) {
-  bool tile_enabled = false;
-
-  // Check whether GPU & MDSS supports MacroTiling feature
-  if (!adreno_helper_->IsMacroTilingSupportedByGPU() || !display_support_macrotile) {
-    return tile_enabled;
-  }
-
-  // check the format
-  switch (format) {
-    case HAL_PIXEL_FORMAT_RGBA_8888:
-    case HAL_PIXEL_FORMAT_RGBX_8888:
-    case HAL_PIXEL_FORMAT_BGRA_8888:
-    case HAL_PIXEL_FORMAT_RGB_565:
-    case HAL_PIXEL_FORMAT_BGR_565:
-      if (!CpuCanAccess(prod_usage, cons_usage)) {
-        // not touched by CPU
-        tile_enabled = true;
-      }
-      break;
-    default:
-      break;
-  }
-
-  return tile_enabled;
-}
-
 // helper function
 unsigned int Allocator::GetSize(const BufferDescriptor &descriptor, unsigned int alignedw,
                                 unsigned int alignedh) {
@@ -361,22 +328,6 @@
   *size = GetSize(descriptor, *alignedw, *alignedh);
 }
 
-void Allocator::GetBufferAttributes(const BufferDescriptor &descriptor, unsigned int *alignedw,
-                                    unsigned int *alignedh, int *tiled, unsigned int *size) {
-  int format = descriptor.GetFormat();
-  gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage();
-  gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage();
-
-  *tiled = false;
-  if (IsUBwcEnabled(format, prod_usage, cons_usage) ||
-      IsMacroTileEnabled(format, prod_usage, cons_usage)) {
-    *tiled = true;
-  }
-
-  GetAlignedWidthAndHeight(descriptor, alignedw, alignedh);
-  *size = GetSize(descriptor, *alignedw, *alignedh);
-}
-
 void Allocator::GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height,
                                       int color_format, struct android_ycbcr *ycbcr) {
   // UBWC buffer has these 4 planes in the following sequence:
@@ -820,7 +771,7 @@
 
   // Currently surface padding is only computed for RGB* surfaces.
   bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage);
-  int tile = ubwc_enabled || IsMacroTileEnabled(format, prod_usage, cons_usage);
+  int tile = ubwc_enabled;
 
   if (IsUncompressedRGBFormat(format)) {
     adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh);
diff --git a/libgralloc1/gr_allocator.h b/libgralloc1/gr_allocator.h
index 583b2d7..da4fbee 100644
--- a/libgralloc1/gr_allocator.h
+++ b/libgralloc1/gr_allocator.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -54,8 +54,6 @@
   int CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op);
   int AllocateMem(AllocData *data, gralloc1_producer_usage_t prod_usage,
                   gralloc1_consumer_usage_t cons_usage);
-  bool IsMacroTileEnabled(int format, gralloc1_producer_usage_t prod_usage,
-                          gralloc1_consumer_usage_t cons_usage);
   // @return : index of the descriptor with maximum buffer size req
   bool CheckForBufferSharing(uint32_t num_descriptors, const BufferDescriptor *descriptors,
                              int *max_index);
@@ -68,8 +66,6 @@
                                   unsigned int *alignedw, unsigned int *alignedh);
   void GetAlignedWidthAndHeight(const BufferDescriptor &d, unsigned int *aligned_w,
                                 unsigned int *aligned_h);
-  void GetBufferAttributes(const BufferDescriptor &d, unsigned int *alignedw,
-                           unsigned int *alignedh, int *tiled, unsigned int *size);
   int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr);
   int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data);
   bool UseUncached(gralloc1_producer_usage_t usage);
@@ -92,8 +88,6 @@
   void GetIonHeapInfo(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
                       unsigned int *ion_heap_id, unsigned int *alloc_type, unsigned int *ion_flags);
 
-  bool gpu_support_macrotile = false;
-  bool display_support_macrotile = false;
   IonAlloc *ion_allocator_ = NULL;
   AdrenoMemInfo *adreno_helper_ = NULL;
 };
diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp
index 9650583..cea8ac9 100644
--- a/libgralloc1/gr_buf_mgr.cpp
+++ b/libgralloc1/gr_buf_mgr.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2016 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2011-2017 The Linux Foundation. All rights reserved.
  * Not a Contribution
  *
  * Copyright (C) 2010 The Android Open Source Project
@@ -332,10 +332,6 @@
     flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
   }
 
-  if (allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage)) {
-    flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
-  }
-
   if (allocator_->IsUBwcEnabled(format, prod_usage, cons_usage)) {
     flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
   }
@@ -573,8 +569,7 @@
       int *tile_enabled = va_arg(args, int *);
       unsigned int alignedw, alignedh;
       BufferDescriptor descriptor(width, height, format, prod_usage, cons_usage);
-      *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage) ||
-                      allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage);
+      *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage);
 
       allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh);
       *aligned_width = INT(alignedw);
diff --git a/libqdutils/qd_utils.cpp b/libqdutils/qd_utils.cpp
index b84ae87..170b1d8 100644
--- a/libqdutils/qd_utils.cpp
+++ b/libqdutils/qd_utils.cpp
@@ -27,6 +27,7 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <unistd.h>
 #include "qd_utils.h"
 
 namespace qdutils {
@@ -81,7 +82,31 @@
     return -1;
 }
 
-int querySDEInfo(HWQueryType type, int *value) {
+static int querySDEInfoDRM(HWQueryType type, int *value) {
+    char property[PROPERTY_VALUE_MAX] = {0};
+
+    // TODO(user): If future targets don't support WB UBWC, add separate
+    // properties in target specific system.prop and have clients like WFD
+    // directly rely on those.
+    switch(type) {
+    case HAS_UBWC:
+    case HAS_WB_UBWC:  // WFD stack still uses this
+        *value = 1;
+        property_get("debug.gralloc.gfx_ubwc_disable", property, "0");
+        if(!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
+                !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
+            *value = 0;
+        }
+        break;
+    default:
+        ALOGE("Invalid query type %d", type);
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static int querySDEInfoFB(HWQueryType type, int *value) {
     FILE *fileptr = NULL;
     const char *featureName;
     char stringBuffer[MAX_STRING_LENGTH];
@@ -90,9 +115,6 @@
     char *tokens[maxCount] = { NULL };
 
     switch(type) {
-    case HAS_MACRO_TILE:
-        featureName = "tile_format";
-        break;
     case HAS_UBWC:
         featureName = "ubwc";
         break;
@@ -134,6 +156,18 @@
     return 0;
 }
 
+int querySDEInfo(HWQueryType type, int *value) {
+    if (!value) {
+        return -EINVAL;
+    }
+
+    if (getDriverType() ==  DriverType::DRM) {
+        return querySDEInfoDRM(type, value);
+    }
+
+    return querySDEInfoFB(type, value);
+}
+
 int getHDMINode(void) {
     return getExternalNode("dtv panel");
 }
@@ -241,4 +275,10 @@
     return 0;
 }
 
+DriverType getDriverType() {
+    const char *fb_caps = "/sys/devices/virtual/graphics/fb0/mdp/caps";
+    // 0 - File exists
+    return access(fb_caps, F_OK) ? DriverType::DRM : DriverType::FB;
+}
+
 }; //namespace qdutils
diff --git a/libqdutils/qd_utils.h b/libqdutils/qd_utils.h
index 5e8d1bf..7b3ae40 100644
--- a/libqdutils/qd_utils.h
+++ b/libqdutils/qd_utils.h
@@ -47,7 +47,6 @@
 namespace qdutils {
 
 enum HWQueryType {
-    HAS_MACRO_TILE = 0,
     HAS_UBWC = 1,
     HAS_WB_UBWC = 2
 };
@@ -65,5 +64,11 @@
 bool isDPConnected();
 int getDPTestConfig(uint32_t *panelBpp, uint32_t *patternType);
 
+enum class DriverType {
+    FB = 0,
+    DRM,
+};
+DriverType getDriverType();
+
 }; //namespace qdutils
 #endif