Merge "hwc: Support IMapper/IAllocator 3.0 in hwc_buffer_allocator" into qt-qpr1-dev am: 4bb89193b9

Change-Id: Ia2b2f08c3bdcf4942e75e191c2a86ad018cd33a7
diff --git a/sdm/libs/hwc2/Android.mk b/sdm/libs/hwc2/Android.mk
index 4d42327..7dd763f 100644
--- a/sdm/libs/hwc2/Android.mk
+++ b/sdm/libs/hwc2/Android.mk
@@ -25,7 +25,9 @@
                                  vendor.display.config@1.0 \
                                  android.hardware.graphics.mapper@2.0 \
                                  android.hardware.graphics.mapper@2.1 \
+                                 android.hardware.graphics.mapper@3.0 \
                                  android.hardware.graphics.allocator@2.0 \
+                                 android.hardware.graphics.allocator@3.0 \
                                  android.hardware.graphics.composer@2.2 \
                                  android.hardware.graphics.composer@2.3 \
                                  hardware.google.light@1.0 \
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.cpp b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
index 896c527..3ea5a61 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.cpp
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.cpp
@@ -1,31 +1,31 @@
 /*
-* Copyright (c) 2015-2018, 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
-* met:
-*  * Redistributions of source code must retain the above copyright
-*    notice, this list of conditions and the following disclaimer.
-*  * Redistributions in binary form must reproduce the above
-*    copyright notice, this list of conditions and the following
-*    disclaimer in the documentation and/or other materials provided
-*    with the distribution.
-*  * Neither the name of The Linux Foundation nor the names of its
-*    contributors may be used to endorse or promote products derived
-*    from this software without specific prior written permission.
-*
-* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
-* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
-* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
-* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
-* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
-* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
-* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * Copyright (c) 2015-2019, 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
+ * met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials provided
+ *    with the distribution.
+ *  * Neither the name of The Linux Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include <gralloc_priv.h>
 
@@ -39,9 +39,10 @@
 
 #define __CLASS__ "HWCBufferAllocator"
 
-using android::hardware::graphics::common::V1_0::PixelFormat;
-using android::hardware::graphics::mapper::V2_0::BufferDescriptor;
 using android::hardware::graphics::mapper::V2_0::Error;
+using MapperV3Error = android::hardware::graphics::mapper::V3_0::Error;
+using android::hardware::graphics::mapper::V2_0::BufferDescriptor;
+using MapperV3BufferDescriptor = android::hardware::graphics::mapper::V3_0::BufferDescriptor;
 using android::hardware::hidl_handle;
 using android::hardware::hidl_vec;
 
@@ -49,17 +50,29 @@
 
 DisplayError HWCBufferAllocator::GetGrallocInstance() {
   // Lazy initialization of gralloc HALs
-  if (mapper_ != nullptr || allocator_ != nullptr) {
+  if (mapper_V3_ != nullptr || mapper_V2_ != nullptr || allocator_V3_ != nullptr ||
+      allocator_V2_ != nullptr) {
     return kErrorNone;
   }
 
-  allocator_ = IAllocator::getService();
-  mapper_ = IMapper::getService();
-
-  if (mapper_ == nullptr || allocator_ == nullptr) {
-    DLOGE("Unable to get mapper or allocator");
-    return kErrorCriticalResource;
+  allocator_V3_ = IAllocatorV3::getService();
+  if (allocator_V3_ == nullptr) {
+    allocator_V2_ = IAllocatorV2::getService();
+    if (allocator_V2_ == nullptr) {
+      DLOGE("Unable to get allocator");
+      return kErrorCriticalResource;
+    }
   }
+
+  mapper_V3_ = IMapperV3::getService();
+  if (mapper_V3_ == nullptr) {
+    mapper_V2_ = IMapperV2::getService();
+    if (mapper_V2_ == nullptr) {
+      DLOGE("Unable to get mapper");
+      return kErrorCriticalResource;
+    }
+  }
+
   return kErrorNone;
 }
 
@@ -96,48 +109,99 @@
 
   alloc_flags |= BufferUsage::COMPOSER_OVERLAY;
 
-  IMapper::BufferDescriptorInfo descriptor_info;
-  descriptor_info.width = buffer_config.width;
-  descriptor_info.height = buffer_config.height;
-  descriptor_info.layerCount = 1;
-  descriptor_info.format = static_cast<PixelFormat>(format);
-  descriptor_info.usage = alloc_flags;
-  auto descriptor = BufferDescriptor();
-  auto hidl_err = Error::NONE;
-
-  mapper_->createDescriptor(descriptor_info, [&](const auto &_error, const auto &_descriptor) {
-    hidl_err = _error;
-    descriptor = _descriptor;
-  });
-  if (hidl_err != Error::NONE) {
-    DLOGE("Failed to create descriptor");
-    return kErrorMemory;
-  }
-
-  hidl_handle raw_handle = nullptr;
-  private_handle_t *hnd = nullptr;
-
-  allocator_->allocate(descriptor, 1,
-                       [&](const auto &_error, const auto &_stride, const auto &_buffers) {
-                         hidl_err = _error;
-                         raw_handle = _buffers[0];
-                       });
-  if (hidl_err != Error::NONE) {
-    DLOGE("Failed to allocate buffer");
-    return kErrorMemory;
-  }
-
   const native_handle_t *buf = nullptr;
-  mapper_->importBuffer(raw_handle, [&](const auto &_error, const auto &_buffer) {
-    hidl_err = _error;
-    buf = static_cast<const native_handle_t *>(_buffer);
-  });
 
-  if (hidl_err != Error::NONE) {
-    DLOGE("Failed to import buffer into HWC");
-    return kErrorMemory;
+  if (mapper_V3_ != nullptr) {
+    IMapperV3::BufferDescriptorInfo descriptor_info;
+    descriptor_info.width = buffer_config.width;
+    descriptor_info.height = buffer_config.height;
+    descriptor_info.layerCount = 1;
+    descriptor_info.format =
+        static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
+    descriptor_info.usage = alloc_flags;
+
+    auto hidl_err = MapperV3Error::NONE;
+
+    auto descriptor = BufferDescriptor();
+    mapper_V3_->createDescriptor(descriptor_info, [&](const auto &_error, const auto &_descriptor) {
+      hidl_err = _error;
+      descriptor = _descriptor;
+    });
+
+    if (hidl_err != MapperV3Error::NONE) {
+      DLOGE("Failed to create descriptor");
+      return kErrorMemory;
+    }
+
+    hidl_handle raw_handle = nullptr;
+
+    allocator_V3_->allocate(descriptor, 1,
+                            [&](const auto &_error, const auto &_stride, const auto &_buffers) {
+                              hidl_err = _error;
+                              raw_handle = _buffers[0];
+                            });
+
+    if (hidl_err != MapperV3Error::NONE) {
+      DLOGE("Failed to allocate buffer");
+      return kErrorMemory;
+    }
+
+    mapper_V3_->importBuffer(raw_handle, [&](const auto &_error, const auto &_buffer) {
+      hidl_err = _error;
+      buf = static_cast<const native_handle_t *>(_buffer);
+    });
+
+    if (hidl_err != MapperV3Error::NONE) {
+      DLOGE("Failed to import buffer into HWC");
+      return kErrorMemory;
+    }
+  } else {
+    IMapperV2::BufferDescriptorInfo descriptor_info;
+    descriptor_info.width = buffer_config.width;
+    descriptor_info.height = buffer_config.height;
+    descriptor_info.layerCount = 1;
+    descriptor_info.format =
+        static_cast<android::hardware::graphics::common::V1_0::PixelFormat>(format);
+    descriptor_info.usage = alloc_flags;
+
+    auto hidl_err = Error::NONE;
+
+    auto descriptor = BufferDescriptor();
+    mapper_V2_->createDescriptor(descriptor_info, [&](const auto &_error, const auto &_descriptor) {
+      hidl_err = _error;
+      descriptor = _descriptor;
+    });
+
+    if (hidl_err != Error::NONE) {
+      DLOGE("Failed to create descriptor");
+      return kErrorMemory;
+    }
+
+    hidl_handle raw_handle = nullptr;
+
+    allocator_V2_->allocate(descriptor, 1,
+                            [&](const auto &_error, const auto &_stride, const auto &_buffers) {
+                              hidl_err = _error;
+                              raw_handle = _buffers[0];
+                            });
+
+    if (hidl_err != Error::NONE) {
+      DLOGE("Failed to allocate buffer");
+      return kErrorMemory;
+    }
+
+    mapper_V2_->importBuffer(raw_handle, [&](const auto &_error, const auto &_buffer) {
+      hidl_err = _error;
+      buf = static_cast<const native_handle_t *>(_buffer);
+    });
+
+    if (hidl_err != Error::NONE) {
+      DLOGE("Failed to import buffer into HWC");
+      return kErrorMemory;
+    }
   }
 
+  private_handle_t *hnd = nullptr;
   hnd = (private_handle_t *)buf;  // NOLINT
   alloc_buffer_info->fd = hnd->fd;
   alloc_buffer_info->stride = UINT32(hnd->width);
@@ -153,7 +217,11 @@
 DisplayError HWCBufferAllocator::FreeBuffer(BufferInfo *buffer_info) {
   DisplayError err = kErrorNone;
   auto hnd = reinterpret_cast<void *>(buffer_info->private_data);
-  mapper_->freeBuffer(hnd);
+  if (mapper_V3_ != nullptr) {
+    mapper_V3_->freeBuffer(hnd);
+  } else {
+    mapper_V2_->freeBuffer(hnd);
+  }
   AllocatedBufferInfo &alloc_buffer_info = buffer_info->alloc_buffer_info;
 
   alloc_buffer_info.fd = -1;
@@ -411,9 +479,6 @@
   if (err != kErrorNone) {
     return err;
   }
-  void *buffer_ptr = NULL;
-  const IMapper::Rect access_region = {.left = 0, .top = 0, .width = 0, .height = 0};
-
   NATIVE_HANDLE_DECLARE_STORAGE(acquire_fence_storage, 1, 0);
   hidl_handle acquire_fence_handle;
   if (acquire_fence >= 0) {
@@ -423,13 +488,27 @@
   }
 
   auto hnd = const_cast<private_handle_t *>(handle);
-  mapper_->lock(reinterpret_cast<void *>(hnd), (uint64_t)BufferUsage::CPU_READ_OFTEN,
-                access_region, acquire_fence_handle, [&](const auto &_error, const auto &_buffer) {
-                  if (_error == Error::NONE) {
-                    buffer_ptr = _buffer;
-                  }
-                });
-
+  void *buffer_ptr = NULL;
+  if (mapper_V3_ != nullptr) {
+    const IMapperV3::Rect access_region = {.left = 0, .top = 0, .width = 0, .height = 0};
+    mapper_V3_->lock(
+        reinterpret_cast<void *>(hnd), (uint64_t)BufferUsage::CPU_READ_OFTEN, access_region,
+        acquire_fence_handle,
+        [&](const auto &_error, const auto &_buffer, const auto &_bpp, const auto &_stride) {
+          if (_error == MapperV3Error::NONE) {
+            buffer_ptr = _buffer;
+          }
+        });
+  } else {
+    const IMapperV2::Rect access_region = {.left = 0, .top = 0, .width = 0, .height = 0};
+    mapper_V2_->lock(reinterpret_cast<void *>(hnd), (uint64_t)BufferUsage::CPU_READ_OFTEN,
+                     access_region, acquire_fence_handle,
+                     [&](const auto &_error, const auto &_buffer) {
+                       if (_error == Error::NONE) {
+                         buffer_ptr = _buffer;
+                       }
+                     });
+  }
   if (!buffer_ptr) {
     return kErrorUndefined;
   }
@@ -440,12 +519,21 @@
   DisplayError err = kErrorNone;
   *release_fence = -1;
   auto hnd = const_cast<private_handle_t *>(handle);
-  mapper_->unlock(reinterpret_cast<void *>(hnd),
-                  [&](const auto &_error, const auto &_release_fence) {
-                    if (_error != Error::NONE) {
-                      err = kErrorUndefined;
-                    }
-                  });
+  if (mapper_V3_ != nullptr) {
+    mapper_V3_->unlock(reinterpret_cast<void *>(hnd),
+                       [&](const auto &_error, const auto &_release_fence) {
+                         if (_error != MapperV3Error::NONE) {
+                           err = kErrorUndefined;
+                         }
+                       });
+  } else {
+    mapper_V2_->unlock(reinterpret_cast<void *>(hnd),
+                       [&](const auto &_error, const auto &_release_fence) {
+                         if (_error != Error::NONE) {
+                           err = kErrorUndefined;
+                         }
+                       });
+  }
   return err;
 }
 
diff --git a/sdm/libs/hwc2/hwc_buffer_allocator.h b/sdm/libs/hwc2/hwc_buffer_allocator.h
index cec5614..b412542 100644
--- a/sdm/libs/hwc2/hwc_buffer_allocator.h
+++ b/sdm/libs/hwc2/hwc_buffer_allocator.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015-2019, 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
@@ -33,12 +33,15 @@
 #include <sys/mman.h>
 
 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
+#include <android/hardware/graphics/allocator/3.0/IAllocator.h>
 #include <android/hardware/graphics/mapper/2.1/IMapper.h>
+#include <android/hardware/graphics/mapper/3.0/IMapper.h>
 #include "gralloc_priv.h"
 
-using android::hardware::graphics::allocator::V2_0::IAllocator;
-using android::hardware::graphics::mapper::V2_0::IMapper;
-
+using IAllocatorV3 = android::hardware::graphics::allocator::V3_0::IAllocator;
+using IAllocatorV2 = android::hardware::graphics::allocator::V2_0::IAllocator;
+using IMapperV3 = android::hardware::graphics::mapper::V3_0::IMapper;
+using IMapperV2 = android::hardware::graphics::mapper::V2_0::IMapper;
 namespace sdm {
 
 template <class Type>
@@ -65,8 +68,10 @@
 
  private:
   DisplayError GetGrallocInstance();
-  android::sp<IMapper> mapper_;
-  android::sp<IAllocator> allocator_;
+  android::sp<IMapperV2> mapper_V2_;
+  android::sp<IMapperV3> mapper_V3_;
+  android::sp<IAllocatorV2> allocator_V2_;
+  android::sp<IAllocatorV3> allocator_V3_;
 };
 
 }  // namespace sdm