drm_hwcomposer: Add checking if we can import a buffer

Add CanImportBuffer() function to the Importer interface.
Platform specific importer should check in this function if it can
import given buffer_handle_t. For example platformhisi will return
false for buffers without GRALLOC_USAGE_HW_FB.

This function should be used on ValidateDisplay step to avoid the
need of 'fake-importing' of buffers.

Signed-off-by: Alexey Firago <alexey_firago@mentor.com>
diff --git a/drmhwctwo.cpp b/drmhwctwo.cpp
index c801f2e..cd79e7b 100644
--- a/drmhwctwo.cpp
+++ b/drmhwctwo.cpp
@@ -491,9 +491,13 @@
   std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
   for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
     HWC2::Composition comp_type;
-    if (test)
+    if (test) {
       comp_type = l.second.sf_type();
-    else
+      if (comp_type == HWC2::Composition::Device) {
+        if (!importer_->CanImportBuffer(l.second.buffer()))
+          comp_type = HWC2::Composition::Client;
+      }
+    } else
       comp_type = l.second.validated_type();
 
     switch (comp_type) {
@@ -735,7 +739,8 @@
   for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
     if (comp_failed || !avail_planes--)
       break;
-    l.second->set_validated_type(HWC2::Composition::Device);
+    if (importer_->CanImportBuffer(l.second->buffer()))
+      l.second->set_validated_type(HWC2::Composition::Device);
   }
 
   for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
diff --git a/platform.h b/platform.h
index 547a297..a58d62e 100644
--- a/platform.h
+++ b/platform.h
@@ -49,6 +49,9 @@
   // Note: This can be called from a different thread than ImportBuffer. The
   //       implementation is responsible for ensuring thread safety.
   virtual int ReleaseBuffer(hwc_drm_bo_t *bo) = 0;
+
+  // Checks if importer can import the buffer.
+  virtual bool CanImportBuffer(buffer_handle_t handle) = 0;
 };
 
 class Planner {
diff --git a/platformdrmgeneric.cpp b/platformdrmgeneric.cpp
index 24d0650..503c04a 100644
--- a/platformdrmgeneric.cpp
+++ b/platformdrmgeneric.cpp
@@ -161,6 +161,12 @@
   return 0;
 }
 
+bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
+  if (handle == NULL)
+    return false;
+  return true;
+}
+
 #ifdef USE_DRM_GENERIC_IMPORTER
 std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
   std::unique_ptr<Planner> planner(new Planner);
diff --git a/platformdrmgeneric.h b/platformdrmgeneric.h
index d46e8b0..233ba55 100644
--- a/platformdrmgeneric.h
+++ b/platformdrmgeneric.h
@@ -33,6 +33,7 @@
 
   int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
   int ReleaseBuffer(hwc_drm_bo_t *bo) override;
+  bool CanImportBuffer(buffer_handle_t handle) override;
 
   uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
   uint32_t DrmFormatToBitsPerPixel(uint32_t drm_format);
diff --git a/platformhisi.cpp b/platformhisi.cpp
index bf4033b..c5cadf6 100644
--- a/platformhisi.cpp
+++ b/platformhisi.cpp
@@ -139,6 +139,12 @@
   return ret;
 }
 
+bool HisiImporter::CanImportBuffer(buffer_handle_t handle) {
+  private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
+      handle);
+  return hnd && (hnd->usage & GRALLOC_USAGE_HW_FB);
+}
+
 class PlanStageHiSi : public Planner::PlanStage {
  public:
   int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
diff --git a/platformhisi.h b/platformhisi.h
index 2b2d439..927da17 100644
--- a/platformhisi.h
+++ b/platformhisi.h
@@ -36,6 +36,8 @@
 
   int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 
+  bool CanImportBuffer(buffer_handle_t handle) override;
+
  private:
   DrmDevice *drm_;