drm_hwcomposer: refactor platform directory

Motivation:

Platform term meaning used in drm_hwcomposer does not correspond to the
content of the platform directory. Platform directory consists of:
1. Buffer information getters for different gralloc (currently called platform).
2. Composition planner logic (which has flaws and should be reworked into
   layer->plane mapping during validation stage logic).
3. DrmGenericImpoter with reference counting logic.

Android-11 IMapper@4 metadata API offers a generic way to access buffer
information which makes other gralloc buffer information getters obsolete.
Legacy getters should be maintained for some time until all known users
will migrate to Mapper@4 API.

Implementation:

1. Split 'PlatformImporter' logic to 'Importer' only and 'Buffer Getter' logic.
   a. Remove buffer_handle_t parameter from ImportBuffer(). Instead user should
      get BufferInfo using ConvertBoInfo to struct hwc_drm_bo_t, then use it for
      ImportBuffer().
   b. Move DrmGenericImporter.{cpp/h} into the drm directory.

2. Isolate planner code in single file and move it to compositor directory as
   compositor/Planner.{cpp/h}

3. Rename platform definition
   a. Rename platform directory to bufferinfo.
   b. Rename/move bufferinfo/platorm*.{cpp,h} getters to
      bufferinfo/legacy/BufferInfo*.{cpp,h}. Align class names/includes.

4. Split legacy/metadata getters logic.
   a. Apply existing bufferinfogetter base class only for legacy getters.
   b. Combine legacy/generic gettera under new base class.
   c. Create a placeholder for generic(metadata) getter.

Signed-off-by: Roman Stratiienko <r.stratiienko@gmail.com>
diff --git a/compositor/DrmDisplayComposition.cpp b/compositor/DrmDisplayComposition.cpp
index 79dd470..4d2e19a 100644
--- a/compositor/DrmDisplayComposition.cpp
+++ b/compositor/DrmDisplayComposition.cpp
@@ -27,8 +27,8 @@
 #include <unordered_set>
 
 #include "DrmDisplayCompositor.h"
+#include "Planner.h"
 #include "drm/DrmDevice.h"
-#include "platform/platform.h"
 
 namespace android {
 
diff --git a/compositor/DrmDisplayCompositor.h b/compositor/DrmDisplayCompositor.h
index 29afc66..ab3f867 100644
--- a/compositor/DrmDisplayCompositor.h
+++ b/compositor/DrmDisplayCompositor.h
@@ -27,6 +27,7 @@
 
 #include "DrmDisplayComposition.h"
 #include "DrmFramebuffer.h"
+#include "Planner.h"
 #include "drm/ResourceManager.h"
 #include "drm/VSyncWorker.h"
 #include "drmhwcomposer.h"
diff --git a/compositor/Planner.cpp b/compositor/Planner.cpp
new file mode 100644
index 0000000..f4b5c51
--- /dev/null
+++ b/compositor/Planner.cpp
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2016 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-platform"
+
+#include "Planner.h"
+
+#include <log/log.h>
+
+#include "drm/DrmDevice.h"
+
+namespace android {
+
+std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
+  std::unique_ptr<Planner> planner(new Planner);
+  planner->AddStage<PlanStageGreedy>();
+  return planner;
+}
+
+std::vector<DrmPlane *> Planner::GetUsablePlanes(
+    DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes,
+    std::vector<DrmPlane *> *overlay_planes) {
+  std::vector<DrmPlane *> usable_planes;
+  std::copy_if(primary_planes->begin(), primary_planes->end(),
+               std::back_inserter(usable_planes),
+               [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
+  std::copy_if(overlay_planes->begin(), overlay_planes->end(),
+               std::back_inserter(usable_planes),
+               [=](DrmPlane *plane) { return plane->GetCrtcSupported(*crtc); });
+  return usable_planes;
+}
+
+int Planner::PlanStage::ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer) {
+  int ret = 0;
+  uint64_t blend;
+
+  if ((plane->rotation_property().id() == 0) &&
+      layer->transform != DrmHwcTransform::kIdentity) {
+    ALOGE("Rotation is not supported on plane %d", plane->id());
+    return -EINVAL;
+  }
+
+  if (plane->alpha_property().id() == 0 && layer->alpha != 0xffff) {
+    ALOGE("Alpha is not supported on plane %d", plane->id());
+    return -EINVAL;
+  }
+
+  if (plane->blend_property().id() == 0) {
+    if ((layer->blending != DrmHwcBlending::kNone) &&
+        (layer->blending != DrmHwcBlending::kPreMult)) {
+      ALOGE("Blending is not supported on plane %d", plane->id());
+      return -EINVAL;
+    }
+  } else {
+    switch (layer->blending) {
+      case DrmHwcBlending::kPreMult:
+        std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
+            "Pre-multiplied");
+        break;
+      case DrmHwcBlending::kCoverage:
+        std::tie(blend, ret) = plane->blend_property().GetEnumValueWithName(
+            "Coverage");
+        break;
+      case DrmHwcBlending::kNone:
+      default:
+        std::tie(blend,
+                 ret) = plane->blend_property().GetEnumValueWithName("None");
+        break;
+    }
+    if (ret)
+      ALOGE("Expected a valid blend mode on plane %d", plane->id());
+  }
+
+  uint32_t format = layer->buffer->format;
+  if (!plane->IsFormatSupported(format)) {
+    ALOGE("Plane %d does not supports %c%c%c%c format", plane->id(), format,
+          format >> 8, format >> 16, format >> 24);
+    return -EINVAL;
+  }
+
+  return ret;
+}
+
+std::tuple<int, std::vector<DrmCompositionPlane>> Planner::ProvisionPlanes(
+    std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+    std::vector<DrmPlane *> *primary_planes,
+    std::vector<DrmPlane *> *overlay_planes) {
+  std::vector<DrmCompositionPlane> composition;
+  std::vector<DrmPlane *> planes = GetUsablePlanes(crtc, primary_planes,
+                                                   overlay_planes);
+  if (planes.empty()) {
+    ALOGE("Display %d has no usable planes", crtc->display());
+    return std::make_tuple(-ENODEV, std::vector<DrmCompositionPlane>());
+  }
+
+  // Go through the provisioning stages and provision planes
+  for (auto &i : stages_) {
+    int ret = i->ProvisionPlanes(&composition, layers, crtc, &planes);
+    if (ret) {
+      ALOGE("Failed provision stage with ret %d", ret);
+      return std::make_tuple(ret, std::vector<DrmCompositionPlane>());
+    }
+  }
+
+  return std::make_tuple(0, std::move(composition));
+}
+
+int PlanStageProtected::ProvisionPlanes(
+    std::vector<DrmCompositionPlane> *composition,
+    std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+    std::vector<DrmPlane *> *planes) {
+  int ret;
+  int protected_zorder = -1;
+  for (auto i = layers.begin(); i != layers.end();) {
+    if (!i->second->protected_usage()) {
+      ++i;
+      continue;
+    }
+
+    ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer, crtc,
+                  std::make_pair(i->first, i->second));
+    if (ret) {
+      ALOGE("Failed to dedicate protected layer! Dropping it.");
+      return ret;
+    }
+
+    protected_zorder = i->first;
+    i = layers.erase(i);
+  }
+
+  return 0;
+}
+
+int PlanStageGreedy::ProvisionPlanes(
+    std::vector<DrmCompositionPlane> *composition,
+    std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+    std::vector<DrmPlane *> *planes) {
+  // Fill up the remaining planes
+  for (auto i = layers.begin(); i != layers.end(); i = layers.erase(i)) {
+    int ret = Emplace(composition, planes, DrmCompositionPlane::Type::kLayer,
+                      crtc, std::make_pair(i->first, i->second));
+    // We don't have any planes left
+    if (ret == -ENOENT)
+      break;
+    else if (ret) {
+      ALOGE("Failed to emplace layer %zu, dropping it", i->first);
+      return ret;
+    }
+  }
+
+  return 0;
+}
+}  // namespace android
diff --git a/compositor/Planner.h b/compositor/Planner.h
new file mode 100644
index 0000000..09034ff
--- /dev/null
+++ b/compositor/Planner.h
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2015 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_DRM_PLATFORM_H_
+#define ANDROID_DRM_PLATFORM_H_
+
+#include <hardware/hardware.h>
+#include <hardware/hwcomposer.h>
+
+#include <map>
+#include <vector>
+
+#include "compositor/DrmDisplayComposition.h"
+#include "drmhwcomposer.h"
+
+namespace android {
+
+class DrmDevice;
+
+class Planner {
+ public:
+  class PlanStage {
+   public:
+    virtual ~PlanStage() {
+    }
+
+    virtual int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
+                                std::map<size_t, DrmHwcLayer *> &layers,
+                                DrmCrtc *crtc,
+                                std::vector<DrmPlane *> *planes) = 0;
+
+   protected:
+    // Removes and returns the next available plane from planes
+    static DrmPlane *PopPlane(std::vector<DrmPlane *> *planes) {
+      if (planes->empty())
+        return NULL;
+      DrmPlane *plane = planes->front();
+      planes->erase(planes->begin());
+      return plane;
+    }
+
+    static int ValidatePlane(DrmPlane *plane, DrmHwcLayer *layer);
+
+    // Inserts the given layer:plane in the composition at the back
+    static int Emplace(std::vector<DrmCompositionPlane> *composition,
+                       std::vector<DrmPlane *> *planes,
+                       DrmCompositionPlane::Type type, DrmCrtc *crtc,
+                       std::pair<size_t, DrmHwcLayer *> layer) {
+      DrmPlane *plane = PopPlane(planes);
+      std::vector<DrmPlane *> unused_planes;
+      int ret = -ENOENT;
+      while (plane) {
+        ret = ValidatePlane(plane, layer.second);
+        if (!ret)
+          break;
+        if (!plane->zpos_property().is_immutable())
+          unused_planes.push_back(plane);
+        plane = PopPlane(planes);
+      }
+
+      if (!ret) {
+        composition->emplace_back(type, plane, crtc, layer.first);
+        planes->insert(planes->begin(), unused_planes.begin(),
+                       unused_planes.end());
+      }
+
+      return ret;
+    }
+  };
+
+  // Creates a planner instance with platform-specific planning stages
+  static std::unique_ptr<Planner> CreateInstance(DrmDevice *drm);
+
+  // Takes a stack of layers and provisions hardware planes for them. If the
+  // entire stack can't fit in hardware, FIXME
+  //
+  // @layers: a map of index:layer of layers to composite
+  // @primary_planes: a vector of primary planes available for this frame
+  // @overlay_planes: a vector of overlay planes available for this frame
+  //
+  // Returns: A tuple with the status of the operation (0 for success) and
+  //          a vector of the resulting plan (ie: layer->plane mapping).
+  std::tuple<int, std::vector<DrmCompositionPlane>> ProvisionPlanes(
+      std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+      std::vector<DrmPlane *> *primary_planes,
+      std::vector<DrmPlane *> *overlay_planes);
+
+  template <typename T, typename... A>
+  void AddStage(A &&... args) {
+    stages_.emplace_back(
+        std::unique_ptr<PlanStage>(new T(std::forward(args)...)));
+  }
+
+ private:
+  std::vector<DrmPlane *> GetUsablePlanes(
+      DrmCrtc *crtc, std::vector<DrmPlane *> *primary_planes,
+      std::vector<DrmPlane *> *overlay_planes);
+
+  std::vector<std::unique_ptr<PlanStage>> stages_;
+};
+
+// This plan stage extracts all protected layers and places them on dedicated
+// planes.
+class PlanStageProtected : public Planner::PlanStage {
+ public:
+  int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
+                      std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+                      std::vector<DrmPlane *> *planes);
+};
+
+// This plan stage places as many layers on dedicated planes as possible (first
+// come first serve), and then sticks the rest in a precomposition plane (if
+// needed).
+class PlanStageGreedy : public Planner::PlanStage {
+ public:
+  int ProvisionPlanes(std::vector<DrmCompositionPlane> *composition,
+                      std::map<size_t, DrmHwcLayer *> &layers, DrmCrtc *crtc,
+                      std::vector<DrmPlane *> *planes);
+};
+}  // namespace android
+#endif