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/Android.bp b/Android.bp
index c2efee6..170bd31 100644
--- a/Android.bp
+++ b/Android.bp
@@ -82,22 +82,25 @@
     srcs: [
         "DrmHwcTwo.cpp",
 
+        "bufferinfo/BufferInfoGetter.cpp",
+        "bufferinfo/BufferInfoMapperMetadata.cpp",
+
         "compositor/DrmDisplayComposition.cpp",
         "compositor/DrmDisplayCompositor.cpp",
+        "compositor/Planner.cpp",
 
         "drm/DrmConnector.cpp",
         "drm/DrmCrtc.cpp",
         "drm/DrmDevice.cpp",
         "drm/DrmEncoder.cpp",
         "drm/DrmEventListener.cpp",
+        "drm/DrmGenericImporter.cpp",
         "drm/DrmMode.cpp",
         "drm/DrmPlane.cpp",
         "drm/DrmProperty.cpp",
         "drm/ResourceManager.cpp",
         "drm/VSyncWorker.cpp",
 
-        "platform/platform.cpp",
-
         "utils/autolock.cpp",
         "utils/hwcutils.cpp",
 
@@ -112,53 +115,37 @@
     name: "hwcomposer.drm",
     defaults: ["hwcomposer.drm_defaults"],
     whole_static_libs: ["drm_hwcomposer"],
-    srcs: ["platform/platformdrmgeneric.cpp",
-           "platform/platformlibdrm.cpp"],
+    srcs: ["bufferinfo/legacy/BufferInfoLibdrm.cpp"],
 }
 
 cc_library_shared {
     name: "hwcomposer.drm_minigbm",
     defaults: ["hwcomposer.drm_defaults"],
     whole_static_libs: ["drm_hwcomposer"],
-    srcs: [
-        "platform/platformdrmgeneric.cpp",
-        "platform/platformminigbm.cpp",
-    ],
+    srcs: ["bufferinfo/legacy/BufferInfoMinigbm.cpp"],
     include_dirs: ["external/minigbm/cros_gralloc"],
 }
 
 // Used by hwcomposer.drm_imagination
 filegroup {
     name: "drm_hwcomposer_platformimagination",
-    srcs: [
-        "platform/platformdrmgeneric.cpp",
-        "platform/platformimagination.cpp",
-    ],
+    srcs: ["bufferinfo/legacy/BufferInfoImagination.cpp"],
 }
 
 // Used by hwcomposer.drm_hikey and hwcomposer.drm_hikey960
 filegroup {
     name: "drm_hwcomposer_platformhisi",
-    srcs: [
-        "platform/platformdrmgeneric.cpp",
-        "platform/platformhisi.cpp",
-    ],
+    srcs: ["bufferinfo/legacy/BufferInfoMaliHisi.cpp"],
 }
 
 // Used by hwcomposer.drm_meson
 filegroup {
     name: "drm_hwcomposer_platformmeson",
-    srcs: [
-        "platform/platformdrmgeneric.cpp",
-        "platform/platformmeson.cpp",
-    ],
+    srcs: ["bufferinfo/legacy/BufferInfoMaliMeson.cpp"],
 }
 
 // Used by hwcomposer.drm_mediatek
 filegroup {
     name: "drm_hwcomposer_platformmediatek",
-    srcs: [
-        "platform/platformdrmgeneric.cpp",
-        "platform/platformmediatek.cpp",
-    ],
+    srcs: ["bufferinfo/legacy/BufferInfoMaliMediatek.cpp"],
 }
diff --git a/DrmHwcTwo.h b/DrmHwcTwo.h
index 7c3b856..d489113 100644
--- a/DrmHwcTwo.h
+++ b/DrmHwcTwo.h
@@ -24,10 +24,11 @@
 #include <map>
 
 #include "compositor/DrmDisplayCompositor.h"
+#include "compositor/Planner.h"
+#include "drm/DrmGenericImporter.h"
 #include "drm/ResourceManager.h"
 #include "drm/VSyncWorker.h"
 #include "drmhwcomposer.h"
-#include "platform/platform.h"
 
 namespace android {
 
diff --git a/backend/Backend.cpp b/backend/Backend.cpp
index 50ef900..887eb0e 100644
--- a/backend/Backend.cpp
+++ b/backend/Backend.cpp
@@ -17,6 +17,7 @@
 #include "Backend.h"
 
 #include "BackendManager.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
@@ -125,7 +126,7 @@
 bool Backend::IsClientLayer(DrmHwcTwo::HwcDisplay *display,
                             DrmHwcTwo::HwcLayer *layer) {
   return !display->HardwareSupportsLayerType(layer->sf_type()) ||
-         !display->importer()->CanImportBuffer(layer->buffer()) ||
+         !BufferInfoGetter::GetInstance()->IsHandleUsable(layer->buffer()) ||
          display->color_transform_hint() != HAL_COLOR_TRANSFORM_IDENTITY ||
          (layer->RequireScalingOrPhasing() &&
           display->resource_manager()->ForcedScalingWithGpu());
diff --git a/backend/BackendRCarDu.cpp b/backend/BackendRCarDu.cpp
index d52f0c3..e85fa71 100644
--- a/backend/BackendRCarDu.cpp
+++ b/backend/BackendRCarDu.cpp
@@ -17,6 +17,7 @@
 #include "BackendRCarDu.h"
 
 #include "BackendManager.h"
+#include "bufferinfo/BufferInfoGetter.h"
 #include "drm_fourcc.h"
 
 namespace android {
@@ -25,7 +26,8 @@
                                   DrmHwcTwo::HwcLayer *layer) {
   hwc_drm_bo_t bo;
 
-  int ret = display->importer()->ConvertBoInfo(layer->buffer(), &bo);
+  int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(layer->buffer(),
+                                                           &bo);
   if (ret)
     return true;
 
diff --git a/bufferinfo/BufferInfoGetter.cpp b/bufferinfo/BufferInfoGetter.cpp
new file mode 100644
index 0000000..8b3f1a4
--- /dev/null
+++ b/bufferinfo/BufferInfoGetter.cpp
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2020 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-buffer-info-getter"
+
+#include "BufferInfoGetter.h"
+
+#if PLATFORM_SDK_VERSION >= 30
+#include "BufferInfoMapperMetadata.h"
+#endif
+
+#include <cutils/properties.h>
+#include <gralloc_handle.h>
+#include <hardware/gralloc.h>
+#include <log/log.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+namespace android {
+
+BufferInfoGetter *BufferInfoGetter::GetInstance() {
+  static std::unique_ptr<BufferInfoGetter> inst;
+  if (inst == nullptr) {
+#if PLATFORM_SDK_VERSION >= 30
+    inst.reset(BufferInfoMapperMetadata::CreateInstance());
+    if (inst == nullptr) {
+      ALOGW(
+          "Generic buffer getter is not available. Falling back to legacy...");
+#endif
+      inst.reset(LegacyBufferInfoGetter::CreateInstance());
+#if PLATFORM_SDK_VERSION >= 30
+    }
+#endif
+  }
+
+  return inst.get();
+}
+
+bool BufferInfoGetter::IsHandleUsable(buffer_handle_t handle) {
+  hwc_drm_bo_t bo;
+  memset(&bo, 0, sizeof(hwc_drm_bo_t));
+
+  if (ConvertBoInfo(handle, &bo) != 0)
+    return false;
+
+  if (bo.prime_fds[0] == 0)
+    return false;
+
+  return true;
+}
+
+int LegacyBufferInfoGetter::Init() {
+  int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
+                          (const hw_module_t **)&gralloc_);
+  if (ret) {
+    ALOGE("Failed to open gralloc module");
+    return ret;
+  }
+
+  ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
+        gralloc_->common.author);
+
+  return 0;
+}
+
+uint32_t LegacyBufferInfoGetter::ConvertHalFormatToDrm(uint32_t hal_format) {
+  switch (hal_format) {
+    case HAL_PIXEL_FORMAT_RGB_888:
+      return DRM_FORMAT_BGR888;
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+      return DRM_FORMAT_ARGB8888;
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+      return DRM_FORMAT_XBGR8888;
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+      return DRM_FORMAT_ABGR8888;
+    case HAL_PIXEL_FORMAT_RGB_565:
+      return DRM_FORMAT_BGR565;
+    case HAL_PIXEL_FORMAT_YV12:
+      return DRM_FORMAT_YVU420;
+    default:
+      ALOGE("Cannot convert hal format to drm format %u", hal_format);
+      return DRM_FORMAT_INVALID;
+  }
+}
+
+uint32_t BufferInfoGetter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
+  switch (drm_format) {
+    case DRM_FORMAT_ARGB8888:
+    case DRM_FORMAT_XBGR8888:
+    case DRM_FORMAT_ABGR8888:
+      return 32;
+    case DRM_FORMAT_BGR888:
+      return 24;
+    case DRM_FORMAT_BGR565:
+      return 16;
+    case DRM_FORMAT_YVU420:
+      return 12;
+    default:
+      ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
+      return 32;
+  }
+}
+
+bool BufferInfoGetter::IsDrmFormatRgb(uint32_t drm_format) {
+  switch (drm_format) {
+    case DRM_FORMAT_ARGB8888:
+    case DRM_FORMAT_XBGR8888:
+    case DRM_FORMAT_ABGR8888:
+    case DRM_FORMAT_BGR888:
+    case DRM_FORMAT_BGR565:
+      return true;
+    default:
+      return false;
+  }
+}
+
+__attribute__((weak)) LegacyBufferInfoGetter *
+LegacyBufferInfoGetter::CreateInstance() {
+  ALOGE("No legacy buffer info getters available");
+  return nullptr;
+}
+
+}  // namespace android
diff --git a/bufferinfo/BufferInfoGetter.h b/bufferinfo/BufferInfoGetter.h
new file mode 100644
index 0000000..78c29ff
--- /dev/null
+++ b/bufferinfo/BufferInfoGetter.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2020 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_BUFFERINFOGETTER_H_
+#define ANDROID_BUFFERINFOGETTER_H_
+
+#include <drm/drm_fourcc.h>
+#include <hardware/gralloc.h>
+
+#include "drm/DrmDevice.h"
+#include "drmhwcgralloc.h"
+
+#ifndef DRM_FORMAT_INVALID
+#define DRM_FORMAT_INVALID 0
+#endif
+
+namespace android {
+
+class BufferInfoGetter {
+ public:
+  virtual ~BufferInfoGetter() {
+  }
+
+  virtual int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) = 0;
+
+  bool IsHandleUsable(buffer_handle_t handle);
+
+  static BufferInfoGetter *GetInstance();
+
+  static uint32_t DrmFormatToBitsPerPixel(uint32_t drm_format);
+  static bool IsDrmFormatRgb(uint32_t drm_format);
+};
+
+class LegacyBufferInfoGetter : public BufferInfoGetter {
+ public:
+  using BufferInfoGetter::BufferInfoGetter;
+
+  int Init();
+
+  int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override = 0;
+
+  static LegacyBufferInfoGetter *CreateInstance();
+
+  static uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
+  const gralloc_module_t *gralloc_;
+};
+
+#define LEGACY_BUFFER_INFO_GETTER(getter_)                           \
+  LegacyBufferInfoGetter *LegacyBufferInfoGetter::CreateInstance() { \
+    auto *instance = new getter_();                                  \
+    if (!instance)                                                   \
+      return NULL;                                                   \
+                                                                     \
+    int ret = instance->Init();                                      \
+    if (ret) {                                                       \
+      ALOGE("Failed to initialize the " #getter_ " getter %d", ret); \
+      delete instance;                                               \
+      return NULL;                                                   \
+    }                                                                \
+    return instance;                                                 \
+  }
+
+}  // namespace android
+#endif
diff --git a/bufferinfo/BufferInfoMapperMetadata.cpp b/bufferinfo/BufferInfoMapperMetadata.cpp
new file mode 100644
index 0000000..2725802
--- /dev/null
+++ b/bufferinfo/BufferInfoMapperMetadata.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+
+#if PLATFORM_SDK_VERSION >= 30
+
+#define LOG_TAG "hwc-bufferinfo-mappermetadata"
+
+#include "BufferInfoMapperMetadata.h"
+
+#include <drm/drm_fourcc.h>
+#include <inttypes.h>
+#include <log/log.h>
+#include <ui/GraphicBufferMapper.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+using android::hardware::graphics::common::V1_1::BufferUsage;
+
+namespace android {
+
+BufferInfoGetter *BufferInfoMapperMetadata::CreateInstance() {
+  if (GraphicBufferMapper::getInstance().getMapperVersion() <
+      GraphicBufferMapper::GRALLOC_4)
+    return nullptr;
+
+  return new BufferInfoMapperMetadata();
+}
+
+int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t /*handle*/,
+                                            hwc_drm_bo_t * /*bo*/) {
+  return -EINVAL;
+}
+
+}  // namespace android
+
+#endif
diff --git a/platform/platformmediatek.h b/bufferinfo/BufferInfoMapperMetadata.h
similarity index 72%
copy from platform/platformmediatek.h
copy to bufferinfo/BufferInfoMapperMetadata.h
index 61fcf47..49c788a 100644
--- a/platform/platformmediatek.h
+++ b/bufferinfo/BufferInfoMapperMetadata.h
@@ -14,23 +14,21 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_MTK_H_
-#define ANDROID_PLATFORM_MTK_H_
+#ifndef PLATFORMIMAGINATION_H
+#define PLATFORMIMAGINATION_H
 
-#include <hardware/gralloc.h>
-#include <stdatomic.h>
-
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
-class MediatekImporter : public DrmGenericImporter {
+class BufferInfoMapperMetadata : public BufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using BufferInfoGetter::BufferInfoGetter;
 
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
+
+  static BufferInfoGetter *CreateInstance();
 };
 }  // namespace android
 
-#endif
+#endif  // PLATFORMIMAGINATION_H
diff --git a/bufferinfo/legacy/BufferInfoImagination.cpp b/bufferinfo/legacy/BufferInfoImagination.cpp
new file mode 100644
index 0000000..3d04a4b
--- /dev/null
+++ b/bufferinfo/legacy/BufferInfoImagination.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2020 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-bufferinfo-imagination"
+
+#include "BufferInfoImagination.h"
+
+#include <log/log.h>
+#include <xf86drm.h>
+
+#include "img_gralloc1_public.h"
+
+namespace android {
+
+LEGACY_BUFFER_INFO_GETTER(BufferInfoImagination);
+
+int BufferInfoImagination::ConvertBoInfo(buffer_handle_t handle,
+                                         hwc_drm_bo_t *bo) {
+  IMG_native_handle_t *hnd = (IMG_native_handle_t *)handle;
+  if (!hnd)
+    return -EINVAL;
+
+  /* Extra bits are responsible for buffer compression and memory layout */
+  if (hnd->iFormat & ~0x10f) {
+    ALOGV("Special buffer formats are not supported");
+    return -EINVAL;
+  }
+
+  bo->width = hnd->iWidth;
+  bo->height = hnd->iHeight;
+  bo->usage = hnd->usage;
+  bo->prime_fds[0] = hnd->fd[0];
+  bo->pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
+  bo->hal_format = hnd->iFormat;
+  bo->pixel_stride = hnd->aiStride[0];
+
+  switch (hnd->iFormat) {
+#ifdef HAL_PIXEL_FORMAT_BGRX_8888
+    case HAL_PIXEL_FORMAT_BGRX_8888:
+      bo->format = DRM_FORMAT_XRGB8888;
+      break;
+#endif
+    default:
+      bo->format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
+      if (bo->format == DRM_FORMAT_INVALID) {
+        ALOGV("Cannot convert hal format to drm format %u", hnd->iFormat);
+        return -EINVAL;
+      }
+  }
+
+  return 0;
+}
+
+}  // namespace android
diff --git a/platform/platformmediatek.h b/bufferinfo/legacy/BufferInfoImagination.h
similarity index 74%
copy from platform/platformmediatek.h
copy to bufferinfo/legacy/BufferInfoImagination.h
index 61fcf47..765b279 100644
--- a/platform/platformmediatek.h
+++ b/bufferinfo/legacy/BufferInfoImagination.h
@@ -14,23 +14,21 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_MTK_H_
-#define ANDROID_PLATFORM_MTK_H_
+#ifndef BUFFERINFOIMAGINATION_H
+#define BUFFERINFOIMAGINATION_H
 
 #include <hardware/gralloc.h>
-#include <stdatomic.h>
 
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
-class MediatekImporter : public DrmGenericImporter {
+class BufferInfoImagination : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
 
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 };
 }  // namespace android
 
-#endif
+#endif  // PLATFORMIMAGINATION_H
diff --git a/platform/platformlibdrm.cpp b/bufferinfo/legacy/BufferInfoLibdrm.cpp
similarity index 89%
rename from platform/platformlibdrm.cpp
rename to bufferinfo/legacy/BufferInfoLibdrm.cpp
index 59f1be9..872ee19 100644
--- a/platform/platformlibdrm.cpp
+++ b/bufferinfo/legacy/BufferInfoLibdrm.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2020 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.
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "hwc-platform-libdrm"
+#define LOG_TAG "hwc-bufferinfo-libdrm"
 
-#include "platformlibdrm.h"
+#include "BufferInfoLibdrm.h"
 
 #include <cutils/properties.h>
 #include <gralloc_handle.h>
@@ -27,19 +27,7 @@
 
 namespace android {
 
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  DrmGenericImporter *importer = new LibdrmImporter(drm);
-  if (!importer)
-    return NULL;
-
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the libdrm importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
+LEGACY_BUFFER_INFO_GETTER(BufferInfoLibdrm);
 
 enum chroma_order {
   YCbCr,
@@ -94,8 +82,8 @@
   return false;
 }
 
-bool LibdrmImporter::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
-                                     hwc_drm_bo_t *bo) {
+bool BufferInfoLibdrm::GetYuvPlaneInfo(int num_fds, buffer_handle_t handle,
+                                       hwc_drm_bo_t *bo) {
   struct android_ycbcr ycbcr;
   enum chroma_order chroma_order;
   int ret;
@@ -165,7 +153,7 @@
   return true;
 }
 
-int LibdrmImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
+int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
   gralloc_handle_t *gr_handle = gralloc_handle(handle);
   if (!gr_handle)
     return -EINVAL;
diff --git a/platform/platformlibdrm.h b/bufferinfo/legacy/BufferInfoLibdrm.h
similarity index 74%
rename from platform/platformlibdrm.h
rename to bufferinfo/legacy/BufferInfoLibdrm.h
index abec17c..4d37d00 100644
--- a/platform/platformlibdrm.h
+++ b/bufferinfo/legacy/BufferInfoLibdrm.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 The Android Open Source Project
+ * Copyright (C) 2020 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.
@@ -14,19 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_LIBDRM_H_
-#define ANDROID_PLATFORM_LIBDRM_H_
+#ifndef BUFFERINFOLIBDRM_H_
+#define BUFFERINFOLIBDRM_H_
 
 #include <hardware/gralloc.h>
 
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
-class LibdrmImporter : public DrmGenericImporter {
+class BufferInfoLibdrm : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 
  private:
diff --git a/platform/platformhisi.cpp b/bufferinfo/legacy/BufferInfoMaliHisi.cpp
similarity index 67%
rename from platform/platformhisi.cpp
rename to bufferinfo/legacy/BufferInfoMaliHisi.cpp
index 67793db..98b2786 100644
--- a/platform/platformhisi.cpp
+++ b/bufferinfo/legacy/BufferInfoMaliHisi.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2020 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.
@@ -14,39 +14,28 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "hwc-platform-hisi"
+#define LOG_TAG "hwc-bufferinfo-mali-hisi"
 
-#include "platformhisi.h"
-
-#include <xf86drm.h>
-#include <xf86drmMode.h>
-#include <cinttypes>
+#include "BufferInfoMaliHisi.h"
 
 #include <log/log.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+#include <cinttypes>
+
 #include "gralloc_priv.h"
 
 #define MALI_ALIGN(value, base) (((value) + ((base)-1)) & ~((base)-1))
 
 namespace android {
 
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  HisiImporter *importer = new HisiImporter(drm);
-  if (!importer)
-    return NULL;
-
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the hisi importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
+LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliHisi);
 
 #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
     defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
-uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
-                                                          bool is_rgb) {
+uint64_t BufferInfoMaliHisi::ConvertGrallocFormatToDrmModifiers(uint64_t flags,
+                                                                bool is_rgb) {
   uint64_t features = 0UL;
 
   if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC)
@@ -71,29 +60,14 @@
   return 0;
 }
 #else
-uint64_t HisiImporter::ConvertGrallocFormatToDrmModifiers(uint64_t /* flags */,
-                                                          bool /* is_rgb */) {
+uint64_t BufferInfoMaliHisi::ConvertGrallocFormatToDrmModifiers(
+    uint64_t /* flags */, bool /* is_rgb */) {
   return 0;
 }
 #endif
 
-bool HisiImporter::IsDrmFormatRgb(uint32_t drm_format) {
-  switch (drm_format) {
-    case DRM_FORMAT_ARGB8888:
-    case DRM_FORMAT_XBGR8888:
-    case DRM_FORMAT_ABGR8888:
-    case DRM_FORMAT_BGR888:
-    case DRM_FORMAT_BGR565:
-      return true;
-    case DRM_FORMAT_YVU420:
-      return false;
-    default:
-      ALOGV("Unsupported format %u assuming rgb?", drm_format);
-      return true;
-  }
-}
-
-int HisiImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
+int BufferInfoMaliHisi::ConvertBoInfo(buffer_handle_t handle,
+                                      hwc_drm_bo_t *bo) {
   bool is_rgb;
 
   private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
@@ -108,9 +82,9 @@
   if (fmt == DRM_FORMAT_INVALID)
     return -EINVAL;
 
-  is_rgb = HisiImporter::IsDrmFormatRgb(fmt);
-  bo->modifiers[0] = HisiImporter::
-      ConvertGrallocFormatToDrmModifiers(hnd->internal_format, is_rgb);
+  is_rgb = IsDrmFormatRgb(fmt);
+  bo->modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
+                                                        is_rgb);
 
   bo->width = hnd->width;
   bo->height = hnd->height;
diff --git a/platform/platformmediatek.h b/bufferinfo/legacy/BufferInfoMaliHisi.h
similarity index 71%
copy from platform/platformmediatek.h
copy to bufferinfo/legacy/BufferInfoMaliHisi.h
index 61fcf47..698a0d3 100644
--- a/platform/platformmediatek.h
+++ b/bufferinfo/legacy/BufferInfoMaliHisi.h
@@ -14,22 +14,23 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_MTK_H_
-#define ANDROID_PLATFORM_MTK_H_
+#ifndef BUFFERINFOMALIHISI_H_
+#define BUFFERINFOMALIHISI_H_
 
 #include <hardware/gralloc.h>
-#include <stdatomic.h>
 
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
-class MediatekImporter : public DrmGenericImporter {
+class BufferInfoMaliHisi : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
 
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
+
+ private:
+  uint64_t ConvertGrallocFormatToDrmModifiers(uint64_t flags, bool is_rgb);
 };
 }  // namespace android
 
diff --git a/platform/platformmediatek.cpp b/bufferinfo/legacy/BufferInfoMaliMediatek.cpp
similarity index 73%
rename from platform/platformmediatek.cpp
rename to bufferinfo/legacy/BufferInfoMaliMediatek.cpp
index bbf76ea..594b582 100644
--- a/platform/platformmediatek.cpp
+++ b/bufferinfo/legacy/BufferInfoMaliMediatek.cpp
@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "hwc-platform-mediatek"
+#define LOG_TAG "hwc-bufferinfo-mali-mediatek"
 
-#include "platformmediatek.h"
+#include "BufferInfoMaliMediatek.h"
 
 #include <hardware/gralloc.h>
 #include <log/log.h>
@@ -27,25 +27,13 @@
 #include <cinttypes>
 
 #include "gralloc_priv.h"
-#include "platform.h"
 
 namespace android {
 
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  MediatekImporter *importer = new MediatekImporter(drm);
-  if (!importer)
-    return NULL;
+LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliMediatek);
 
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the mediatek importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
-
-int MediatekImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
+int BufferInfoMaliMediatek::ConvertBoInfo(buffer_handle_t handle,
+                                          hwc_drm_bo_t *bo) {
   private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
       handle);
   if (!hnd)
diff --git a/platform/platformmediatek.h b/bufferinfo/legacy/BufferInfoMaliMediatek.h
similarity index 76%
rename from platform/platformmediatek.h
rename to bufferinfo/legacy/BufferInfoMaliMediatek.h
index 61fcf47..1204818 100644
--- a/platform/platformmediatek.h
+++ b/bufferinfo/legacy/BufferInfoMaliMediatek.h
@@ -14,20 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_MTK_H_
-#define ANDROID_PLATFORM_MTK_H_
+#ifndef BUFFERINFOMALIMTK_H_
+#define BUFFERINFOMALIMTK_H_
 
 #include <hardware/gralloc.h>
-#include <stdatomic.h>
 
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#include "bufferinfo/BufferInfoGetter.h"
 
 namespace android {
 
-class MediatekImporter : public DrmGenericImporter {
+class BufferInfoMaliMediatek : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
 
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 };
diff --git a/platform/platformmeson.cpp b/bufferinfo/legacy/BufferInfoMaliMeson.cpp
similarity index 75%
rename from platform/platformmeson.cpp
rename to bufferinfo/legacy/BufferInfoMaliMeson.cpp
index 278eac5..2f1ca21 100644
--- a/platform/platformmeson.cpp
+++ b/bufferinfo/legacy/BufferInfoMaliMeson.cpp
@@ -14,36 +14,26 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "hwc-platform-meson"
+#define LOG_TAG "hwc-bufferinfo-mali-meson"
 
-#include "platformmeson.h"
-
-#include <xf86drm.h>
-#include <xf86drmMode.h>
-#include <cinttypes>
+#include "BufferInfoMaliMeson.h"
 
 #include <log/log.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+#include <cinttypes>
+
 #include "gralloc_priv.h"
 
 namespace android {
 
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  MesonImporter *importer = new MesonImporter(drm);
-  if (!importer)
-    return NULL;
-
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the meson importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
+LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliMeson);
 
 #if defined(MALI_GRALLOC_INTFMT_AFBC_BASIC) && \
     defined(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16)
-uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers(uint64_t flags) {
+uint64_t BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
+    uint64_t flags) {
   uint64_t features = 0UL;
 
   if (flags & MALI_GRALLOC_INTFMT_AFBC_BASIC) {
@@ -65,13 +55,14 @@
   return 0;
 }
 #else
-uint64_t MesonImporter::ConvertGrallocFormatToDrmModifiers(
+uint64_t BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
     uint64_t /* flags */) {
   return 0;
 }
 #endif
 
-int MesonImporter::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
+int BufferInfoMaliMeson::ConvertBoInfo(buffer_handle_t handle,
+                                       hwc_drm_bo_t *bo) {
   private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(
       handle);
   if (!hnd)
@@ -84,7 +75,7 @@
   if (fmt == DRM_FORMAT_INVALID)
     return -EINVAL;
 
-  bo->modifiers[0] = MesonImporter::ConvertGrallocFormatToDrmModifiers(
+  bo->modifiers[0] = BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
       hnd->internal_format);
 
   bo->width = hnd->width;
diff --git a/platform/platformmeson.h b/bufferinfo/legacy/BufferInfoMaliMeson.h
similarity index 77%
rename from platform/platformmeson.h
rename to bufferinfo/legacy/BufferInfoMaliMeson.h
index 1b428a4..ce5d3f9 100644
--- a/platform/platformmeson.h
+++ b/bufferinfo/legacy/BufferInfoMaliMeson.h
@@ -14,22 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_HISI_H_
-#define ANDROID_PLATFORM_HISI_H_
-
-#include "platform.h"
-#include "platformdrmgeneric.h"
-
-#include <stdatomic.h>
+#ifndef BUFFERINFOMALIHISI_H_
+#define BUFFERINFOMALIHISI_H_
 
 #include <hardware/gralloc.h>
 
+#include "bufferinfo/BufferInfoGetter.h"
+
 namespace android {
 
-class MesonImporter : public DrmGenericImporter {
+class BufferInfoMaliMeson : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
-
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 
  private:
diff --git a/platform/platformminigbm.cpp b/bufferinfo/legacy/BufferInfoMinigbm.cpp
similarity index 70%
rename from platform/platformminigbm.cpp
rename to bufferinfo/legacy/BufferInfoMinigbm.cpp
index 4360b0a..67a85cb 100644
--- a/platform/platformminigbm.cpp
+++ b/bufferinfo/legacy/BufferInfoMinigbm.cpp
@@ -14,35 +14,21 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "hwc-platform-drm-minigbm"
+#define LOG_TAG "hwc-bufferinfo-minigbm"
 
-#include "platformminigbm.h"
-
-#include <xf86drm.h>
-#include <xf86drmMode.h>
+#include "BufferInfoMinigbm.h"
 
 #include <log/log.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
 
 #include "cros_gralloc_handle.h"
 
 namespace android {
 
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  DrmMinigbmImporter *importer = new DrmMinigbmImporter(drm);
-  if (!importer)
-    return NULL;
+LEGACY_BUFFER_INFO_GETTER(BufferInfoMinigbm);
 
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the minigbm importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
-
-int DrmMinigbmImporter::ConvertBoInfo(buffer_handle_t handle,
-                                      hwc_drm_bo_t *bo) {
+int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) {
   cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle;
   if (!gr_handle)
     return -EINVAL;
diff --git a/platform/platformminigbm.h b/bufferinfo/legacy/BufferInfoMinigbm.h
similarity index 76%
rename from platform/platformminigbm.h
rename to bufferinfo/legacy/BufferInfoMinigbm.h
index 1eea6ca..bff9d74 100644
--- a/platform/platformminigbm.h
+++ b/bufferinfo/legacy/BufferInfoMinigbm.h
@@ -14,19 +14,18 @@
  * limitations under the License.
  */
 
-#ifndef ANDROID_PLATFORM_DRM_MINIGBM_H_
-#define ANDROID_PLATFORM_DRM_MINIGBM_H_
-
-#include "platform.h"
-#include "platformdrmgeneric.h"
+#ifndef BUFFERINFOMINIGBM_H_
+#define BUFFERINFOMINIGBM_H_
 
 #include <hardware/gralloc.h>
 
+#include "bufferinfo/BufferInfoGetter.h"
+
 namespace android {
 
-class DrmMinigbmImporter : public DrmGenericImporter {
+class BufferInfoMinigbm : public LegacyBufferInfoGetter {
  public:
-  using DrmGenericImporter::DrmGenericImporter;
+  using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
   int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
 };
 
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/platform/platform.cpp b/compositor/Planner.cpp
similarity index 95%
rename from platform/platform.cpp
rename to compositor/Planner.cpp
index a500398..f4b5c51 100644
--- a/platform/platform.cpp
+++ b/compositor/Planner.cpp
@@ -16,7 +16,7 @@
 
 #define LOG_TAG "hwc-platform"
 
-#include "platform.h"
+#include "Planner.h"
 
 #include <log/log.h>
 
@@ -24,6 +24,12 @@
 
 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) {
diff --git a/platform/platform.h b/compositor/Planner.h
similarity index 81%
rename from platform/platform.h
rename to compositor/Planner.h
index 13dc360..09034ff 100644
--- a/platform/platform.h
+++ b/compositor/Planner.h
@@ -30,33 +30,6 @@
 
 class DrmDevice;
 
-class Importer {
- public:
-  virtual ~Importer() {
-  }
-
-  // Creates a platform-specific importer instance
-  static Importer *CreateInstance(DrmDevice *drm);
-
-  // Imports the buffer referred to by handle into bo.
-  //
-  // Note: This can be called from a different thread than ReleaseBuffer. The
-  //       implementation is responsible for ensuring thread safety.
-  virtual int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) = 0;
-
-  // Releases the buffer object (ie: does the inverse of ImportBuffer)
-  //
-  // 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;
-
-  // Convert platform-dependent buffer format to drm_hwc internal format.
-  virtual int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) = 0;
-};
-
 class Planner {
  public:
   class PlanStage {
diff --git a/drm/DrmDevice.cpp b/drm/DrmDevice.cpp
index 28ecfda..bf1a5e2 100644
--- a/drm/DrmDevice.cpp
+++ b/drm/DrmDevice.cpp
@@ -29,6 +29,7 @@
 #include <algorithm>
 #include <array>
 #include <cinttypes>
+#include <sstream>
 #include <string>
 
 static void trim_left(std::string &str) {
diff --git a/drm/DrmDevice.h b/drm/DrmDevice.h
index d7ea359..be68aa6 100644
--- a/drm/DrmDevice.h
+++ b/drm/DrmDevice.h
@@ -19,6 +19,7 @@
 
 #include <stdint.h>
 
+#include <map>
 #include <tuple>
 
 #include "DrmConnector.h"
@@ -26,7 +27,6 @@
 #include "DrmEncoder.h"
 #include "DrmEventListener.h"
 #include "DrmPlane.h"
-#include "platform/platform.h"
 
 namespace android {
 
diff --git a/drm/DrmGenericImporter.cpp b/drm/DrmGenericImporter.cpp
new file mode 100644
index 0000000..b4dc063
--- /dev/null
+++ b/drm/DrmGenericImporter.cpp
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2020 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-drm-generic"
+
+#include "DrmGenericImporter.h"
+
+#include <cutils/properties.h>
+#include <gralloc_handle.h>
+#include <hardware/gralloc.h>
+#include <log/log.h>
+#include <xf86drm.h>
+#include <xf86drmMode.h>
+
+namespace android {
+
+DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
+}
+
+DrmGenericImporter::~DrmGenericImporter() {
+}
+
+int DrmGenericImporter::ImportBuffer(hwc_drm_bo_t *bo) {
+  int ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0],
+                               &bo->gem_handles[0]);
+  if (ret) {
+    ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
+    return ret;
+  }
+
+  for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
+    int fd = bo->prime_fds[i];
+    if (fd != 0) {
+      if (fd != bo->prime_fds[0]) {
+        ALOGE("Multiplanar FBs are not supported by this version of composer");
+        return -ENOTSUP;
+      }
+      bo->gem_handles[i] = bo->gem_handles[0];
+    }
+  }
+
+  if (!bo->with_modifiers)
+    ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
+                        bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
+                        0);
+  else
+    ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
+                                     bo->format, bo->gem_handles, bo->pitches,
+                                     bo->offsets, bo->modifiers, &bo->fb_id,
+                                     bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
+                                                      : 0);
+
+  if (ret) {
+    ALOGE("could not create drm fb %d", ret);
+    return ret;
+  }
+
+  ImportHandle(bo->gem_handles[0]);
+
+  return ret;
+}
+
+int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
+  if (bo->fb_id)
+    if (drmModeRmFB(drm_->fd(), bo->fb_id))
+      ALOGE("Failed to rm fb");
+
+  for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
+    if (!bo->gem_handles[i])
+      continue;
+
+    if (ReleaseHandle(bo->gem_handles[i])) {
+      ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
+    } else {
+      for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
+        if (bo->gem_handles[j] == bo->gem_handles[i])
+          bo->gem_handles[j] = 0;
+      bo->gem_handles[i] = 0;
+    }
+  }
+  return 0;
+}
+
+int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
+  gem_refcount_[gem_handle]++;
+
+  return 0;
+}
+
+int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
+  if (--gem_refcount_[gem_handle])
+    return 0;
+
+  gem_refcount_.erase(gem_handle);
+
+  return CloseHandle(gem_handle);
+}
+
+int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
+  struct drm_gem_close gem_close;
+
+  memset(&gem_close, 0, sizeof(gem_close));
+
+  gem_close.handle = gem_handle;
+  int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
+  if (ret)
+    ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
+
+  return ret;
+}
+}  // namespace android
diff --git a/platform/platformdrmgeneric.h b/drm/DrmGenericImporter.h
similarity index 63%
rename from platform/platformdrmgeneric.h
rename to drm/DrmGenericImporter.h
index 6c15ae6..efda6be 100644
--- a/platform/platformdrmgeneric.h
+++ b/drm/DrmGenericImporter.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2020 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.
@@ -23,7 +23,7 @@
 #include <map>
 
 #include "drm/DrmDevice.h"
-#include "platform.h"
+#include "drmhwcgralloc.h"
 
 #ifndef DRM_FORMAT_INVALID
 #define DRM_FORMAT_INVALID 0
@@ -31,33 +31,42 @@
 
 namespace android {
 
+class Importer {
+ public:
+  virtual ~Importer() {
+  }
+
+  // Imports the buffer referred to by handle into bo.
+  //
+  // Note: This can be called from a different thread than ReleaseBuffer. The
+  //       implementation is responsible for ensuring thread safety.
+  virtual int ImportBuffer(hwc_drm_bo_t *bo) = 0;
+
+  // Releases the buffer object (ie: does the inverse of ImportBuffer)
+  //
+  // 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;
+};
+
 class DrmGenericImporter : public Importer {
  public:
   DrmGenericImporter(DrmDevice *drm);
   ~DrmGenericImporter() override;
 
-  int Init();
-
-  int ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
+  int ImportBuffer(hwc_drm_bo_t *bo) override;
   int ReleaseBuffer(hwc_drm_bo_t *bo) override;
-  bool CanImportBuffer(buffer_handle_t handle) override;
   int ImportHandle(uint32_t gem_handle);
   int ReleaseHandle(uint32_t gem_handle);
 
-  int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override = 0;
-
-  uint32_t ConvertHalFormatToDrm(uint32_t hal_format);
-  uint32_t DrmFormatToBitsPerPixel(uint32_t drm_format);
-
  protected:
   DrmDevice *drm_;
-  const gralloc_module_t *gralloc_;
 
  private:
-
   int CloseHandle(uint32_t gem_handle);
   std::map<uint32_t, int> gem_refcount_;
 };
+
 }  // namespace android
 
 #endif
diff --git a/drm/ResourceManager.cpp b/drm/ResourceManager.cpp
index 67ef7f8..fc24aea 100644
--- a/drm/ResourceManager.cpp
+++ b/drm/ResourceManager.cpp
@@ -24,6 +24,8 @@
 
 #include <sstream>
 
+#include "bufferinfo/BufferInfoGetter.h"
+
 namespace android {
 
 ResourceManager::ResourceManager() : num_displays_(0), gralloc_(NULL) {
@@ -62,6 +64,11 @@
   property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
   scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
 
+  if (!BufferInfoGetter::GetInstance()) {
+    ALOGE("Failed to initialize BufferInfoGetter");
+    return -EINVAL;
+  }
+
   return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
                        (const hw_module_t **)&gralloc_);
 }
@@ -73,7 +80,7 @@
   if (ret)
     return ret;
   std::shared_ptr<Importer> importer;
-  importer.reset(Importer::CreateInstance(drm.get()));
+  importer.reset(new DrmGenericImporter(drm.get()));
   if (!importer) {
     ALOGE("Failed to create importer instance");
     return -ENODEV;
diff --git a/drm/ResourceManager.h b/drm/ResourceManager.h
index 94ba43e..7102cea 100644
--- a/drm/ResourceManager.h
+++ b/drm/ResourceManager.h
@@ -20,7 +20,7 @@
 #include <string.h>
 
 #include "DrmDevice.h"
-#include "platform/platform.h"
+#include "DrmGenericImporter.h"
 
 namespace android {
 
diff --git a/platform/platformdrmgeneric.cpp b/platform/platformdrmgeneric.cpp
deleted file mode 100644
index 8c1adba..0000000
--- a/platform/platformdrmgeneric.cpp
+++ /dev/null
@@ -1,201 +0,0 @@
-/*
- * 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.
- */
-
-#define LOG_TAG "hwc-platform-drm-generic"
-
-#include "platformdrmgeneric.h"
-
-#include <xf86drm.h>
-#include <xf86drmMode.h>
-
-#include <cutils/properties.h>
-#include <gralloc_handle.h>
-#include <hardware/gralloc.h>
-#include <log/log.h>
-
-namespace android {
-
-DrmGenericImporter::DrmGenericImporter(DrmDevice *drm) : drm_(drm) {
-}
-
-DrmGenericImporter::~DrmGenericImporter() {
-}
-
-int DrmGenericImporter::Init() {
-  int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
-                          (const hw_module_t **)&gralloc_);
-  if (ret) {
-    ALOGE("Failed to open gralloc module");
-    return ret;
-  }
-
-  ALOGI("Using %s gralloc module: %s\n", gralloc_->common.name,
-        gralloc_->common.author);
-
-  return 0;
-}
-
-uint32_t DrmGenericImporter::ConvertHalFormatToDrm(uint32_t hal_format) {
-  switch (hal_format) {
-    case HAL_PIXEL_FORMAT_RGB_888:
-      return DRM_FORMAT_BGR888;
-    case HAL_PIXEL_FORMAT_BGRA_8888:
-      return DRM_FORMAT_ARGB8888;
-    case HAL_PIXEL_FORMAT_RGBX_8888:
-      return DRM_FORMAT_XBGR8888;
-    case HAL_PIXEL_FORMAT_RGBA_8888:
-      return DRM_FORMAT_ABGR8888;
-    case HAL_PIXEL_FORMAT_RGB_565:
-      return DRM_FORMAT_BGR565;
-    case HAL_PIXEL_FORMAT_YV12:
-      return DRM_FORMAT_YVU420;
-    default:
-      ALOGE("Cannot convert hal format to drm format %u", hal_format);
-      return DRM_FORMAT_INVALID;
-  }
-}
-
-uint32_t DrmGenericImporter::DrmFormatToBitsPerPixel(uint32_t drm_format) {
-  switch (drm_format) {
-    case DRM_FORMAT_ARGB8888:
-    case DRM_FORMAT_XBGR8888:
-    case DRM_FORMAT_ABGR8888:
-      return 32;
-    case DRM_FORMAT_BGR888:
-      return 24;
-    case DRM_FORMAT_BGR565:
-      return 16;
-    case DRM_FORMAT_YVU420:
-      return 12;
-    default:
-      ALOGE("Cannot convert hal format %u to bpp (returning 32)", drm_format);
-      return 32;
-  }
-}
-
-int DrmGenericImporter::ImportBuffer(buffer_handle_t handle, hwc_drm_bo_t *bo) {
-  memset(bo, 0, sizeof(hwc_drm_bo_t));
-
-  int ret = ConvertBoInfo(handle, bo);
-  if (ret)
-    return ret;
-
-  ret = drmPrimeFDToHandle(drm_->fd(), bo->prime_fds[0], &bo->gem_handles[0]);
-  if (ret) {
-    ALOGE("failed to import prime fd %d ret=%d", bo->prime_fds[0], ret);
-    return ret;
-  }
-
-  for (int i = 1; i < HWC_DRM_BO_MAX_PLANES; i++) {
-    int fd = bo->prime_fds[i];
-    if (fd != 0) {
-      if (fd != bo->prime_fds[0]) {
-        ALOGE("Multiplanar FBs are not supported by this version of composer");
-        return -ENOTSUP;
-      }
-      bo->gem_handles[i] = bo->gem_handles[0];
-    }
-  }
-
-  if (!bo->with_modifiers)
-    ret = drmModeAddFB2(drm_->fd(), bo->width, bo->height, bo->format,
-                        bo->gem_handles, bo->pitches, bo->offsets, &bo->fb_id,
-                        0);
-  else
-    ret = drmModeAddFB2WithModifiers(drm_->fd(), bo->width, bo->height,
-                                     bo->format, bo->gem_handles, bo->pitches,
-                                     bo->offsets, bo->modifiers, &bo->fb_id,
-                                     bo->modifiers[0] ? DRM_MODE_FB_MODIFIERS
-                                                      : 0);
-
-  if (ret) {
-    ALOGE("could not create drm fb %d", ret);
-    return ret;
-  }
-
-  ImportHandle(bo->gem_handles[0]);
-
-  return ret;
-}
-
-int DrmGenericImporter::ReleaseBuffer(hwc_drm_bo_t *bo) {
-  if (bo->fb_id)
-    if (drmModeRmFB(drm_->fd(), bo->fb_id))
-      ALOGE("Failed to rm fb");
-
-  for (int i = 0; i < HWC_DRM_BO_MAX_PLANES; i++) {
-    if (!bo->gem_handles[i])
-      continue;
-
-    if (ReleaseHandle(bo->gem_handles[i])) {
-      ALOGE("Failed to release gem handle %d", bo->gem_handles[i]);
-    } else {
-      for (int j = i + 1; j < HWC_DRM_BO_MAX_PLANES; j++)
-        if (bo->gem_handles[j] == bo->gem_handles[i])
-          bo->gem_handles[j] = 0;
-      bo->gem_handles[i] = 0;
-    }
-  }
-  return 0;
-}
-
-bool DrmGenericImporter::CanImportBuffer(buffer_handle_t handle) {
-  hwc_drm_bo_t bo;
-
-  int ret = ConvertBoInfo(handle, &bo);
-  if (ret)
-    return false;
-
-  if (bo.prime_fds[0] == 0)
-    return false;
-
-  return true;
-}
-
-std::unique_ptr<Planner> Planner::CreateInstance(DrmDevice *) {
-  std::unique_ptr<Planner> planner(new Planner);
-  planner->AddStage<PlanStageGreedy>();
-  return planner;
-}
-
-int DrmGenericImporter::ImportHandle(uint32_t gem_handle) {
-  gem_refcount_[gem_handle]++;
-
-  return 0;
-}
-
-int DrmGenericImporter::ReleaseHandle(uint32_t gem_handle) {
-  if (--gem_refcount_[gem_handle])
-    return 0;
-
-  gem_refcount_.erase(gem_handle);
-
-  return CloseHandle(gem_handle);
-}
-
-int DrmGenericImporter::CloseHandle(uint32_t gem_handle) {
-  struct drm_gem_close gem_close;
-
-  memset(&gem_close, 0, sizeof(gem_close));
-
-  gem_close.handle = gem_handle;
-  int ret = drmIoctl(drm_->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close);
-  if (ret)
-    ALOGE("Failed to close gem handle %d %d", gem_handle, ret);
-
-  return ret;
-}
-}
diff --git a/platform/platformhisi.h b/platform/platformhisi.h
deleted file mode 100644
index 272e547..0000000
--- a/platform/platformhisi.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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_PLATFORM_HISI_H_
-#define ANDROID_PLATFORM_HISI_H_
-
-#include "platform.h"
-#include "platformdrmgeneric.h"
-
-#include <stdatomic.h>
-
-#include <hardware/gralloc.h>
-
-namespace android {
-
-class HisiImporter : public DrmGenericImporter {
- public:
-  using DrmGenericImporter::DrmGenericImporter;
-
-  int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
-
- private:
-  uint64_t ConvertGrallocFormatToDrmModifiers(uint64_t flags, bool is_rgb);
-
-  bool IsDrmFormatRgb(uint32_t drm_format);
-};
-}  // namespace android
-
-#endif
diff --git a/platform/platformimagination.cpp b/platform/platformimagination.cpp
deleted file mode 100644
index 7001d64..0000000
--- a/platform/platformimagination.cpp
+++ /dev/null
@@ -1,63 +0,0 @@
-#define LOG_TAG "hwc-platform-imagination"
-
-#include "platformimagination.h"
-
-#include <log/log.h>
-#include <xf86drm.h>
-
-#include "img_gralloc1_public.h"
-
-namespace android {
-
-Importer *Importer::CreateInstance(DrmDevice *drm) {
-  ImaginationImporter *importer = new ImaginationImporter(drm);
-  if (!importer)
-    return NULL;
-
-  int ret = importer->Init();
-  if (ret) {
-    ALOGE("Failed to initialize the Imagination importer %d", ret);
-    delete importer;
-    return NULL;
-  }
-  return importer;
-}
-
-int ImaginationImporter::ConvertBoInfo(buffer_handle_t handle,
-                                       hwc_drm_bo_t *bo) {
-  IMG_native_handle_t *hnd = (IMG_native_handle_t *)handle;
-  if (!hnd)
-    return -EINVAL;
-
-  /* Extra bits are responsible for buffer compression and memory layout */
-  if (hnd->iFormat & ~0x10f) {
-    ALOGV("Special buffer formats are not supported");
-    return -EINVAL;
-  }
-
-  bo->width = hnd->iWidth;
-  bo->height = hnd->iHeight;
-  bo->usage = hnd->usage;
-  bo->prime_fds[0] = hnd->fd[0];
-  bo->pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
-  bo->hal_format = hnd->iFormat;
-  bo->pixel_stride = hnd->aiStride[0];
-
-  switch (hnd->iFormat) {
-#ifdef HAL_PIXEL_FORMAT_BGRX_8888
-    case HAL_PIXEL_FORMAT_BGRX_8888:
-      bo->format = DRM_FORMAT_XRGB8888;
-      break;
-#endif
-    default:
-      bo->format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
-      if (bo->format == DRM_FORMAT_INVALID) {
-        ALOGV("Cannot convert hal format to drm format %u", hnd->iFormat);
-        return -EINVAL;
-      }
-  }
-
-  return 0;
-}
-
-}  // namespace android
diff --git a/platform/platformimagination.h b/platform/platformimagination.h
deleted file mode 100644
index 4eec698..0000000
--- a/platform/platformimagination.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef PLATFORMIMAGINATION_H
-#define PLATFORMIMAGINATION_H
-
-#include "platform.h"
-#include "platformdrmgeneric.h"
-
-#include <stdatomic.h>
-
-#include <hardware/gralloc.h>
-
-namespace android {
-
-class ImaginationImporter : public DrmGenericImporter {
- public:
-  using DrmGenericImporter::DrmGenericImporter;
-
-  int ConvertBoInfo(buffer_handle_t handle, hwc_drm_bo_t *bo) override;
-};
-}  // namespace android
-
-#endif  // PLATFORMIMAGINATION_H
diff --git a/utils/hwcutils.cpp b/utils/hwcutils.cpp
index 2dc7e7b..2cd46fa 100644
--- a/utils/hwcutils.cpp
+++ b/utils/hwcutils.cpp
@@ -20,8 +20,9 @@
 #include <log/log.h>
 #include <ui/GraphicBufferMapper.h>
 
+#include "bufferinfo/BufferInfoGetter.h"
+#include "drm/DrmGenericImporter.h"
 #include "drmhwcomposer.h"
-#include "platform/platform.h"
 
 #define UNUSED(x) (void)(x)
 
@@ -44,9 +45,11 @@
 }
 
 int DrmHwcBuffer::ImportBuffer(buffer_handle_t handle, Importer *importer) {
-  hwc_drm_bo tmp_bo;
+  hwc_drm_bo tmp_bo{};
 
-  int ret = importer->ImportBuffer(handle, &tmp_bo);
+  BufferInfoGetter::GetInstance()->ConvertBoInfo(handle, &tmp_bo);
+
+  int ret = importer->ImportBuffer(&tmp_bo);
   if (ret)
     return ret;