graphics: add allocator HAL support library

Add a header-only library
android.hardware.graphics.allocator@2.0-hal that can be used by
implementations.  An imlpementation can

  class VendorHal : public AllocatorHal { ... };

  auto allocator = std::make_unique<Allocator>();
  allocator->init(std::make_unique<VendorHal>(...));

Or, if vendor extensions are to be added to the IAllocator,

  class AlocatorHalExt : public AllocatorHal { ... };
  class VendorHal : public AllocatorHalExt { ... };
  class AllocatorExt : public AllocatorImpl<IAllocatorExt, AllocatorHalExt> { ... };

  auto allocator = std::make_unique<AllocatorExt>();
  allocator->init(std::make_unique<VendorHal>(...));

Test: builds
Change-Id: I7cb7a4888316b871e5c49d96524b1642fc708f2d
diff --git a/graphics/allocator/2.0/utils/hal/Android.bp b/graphics/allocator/2.0/utils/hal/Android.bp
new file mode 100644
index 0000000..ac642ce
--- /dev/null
+++ b/graphics/allocator/2.0/utils/hal/Android.bp
@@ -0,0 +1,14 @@
+cc_library_headers {
+    name: "android.hardware.graphics.allocator@2.0-hal",
+    defaults: ["hidl_defaults"],
+    vendor: true,
+    shared_libs: [
+        "android.hardware.graphics.allocator@2.0",
+        "android.hardware.graphics.mapper@2.0",
+    ],
+    export_shared_lib_headers: [
+        "android.hardware.graphics.allocator@2.0",
+        "android.hardware.graphics.mapper@2.0",
+    ],
+    export_include_dirs: ["include"],
+}
diff --git a/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/Allocator.h b/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/Allocator.h
new file mode 100644
index 0000000..2f3022e
--- /dev/null
+++ b/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/Allocator.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2018 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.
+ */
+
+#pragma once
+
+#ifndef LOG_TAG
+#warning "Allocator.h included without LOG_TAG"
+#endif
+
+#include <memory>
+
+#include <allocator-hal/2.0/AllocatorHal.h>
+#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
+#include <android/hardware/graphics/mapper/2.0/IMapper.h>
+#include <log/log.h>
+
+namespace android {
+namespace hardware {
+namespace graphics {
+namespace allocator {
+namespace V2_0 {
+namespace hal {
+
+using mapper::V2_0::BufferDescriptor;
+using mapper::V2_0::Error;
+
+namespace detail {
+
+// AllocatorImpl implements IAllocator on top of AllocatorHal
+template <typename IALLOCATOR, typename ALLOCATOR_HAL>
+class AllocatorImpl : public IALLOCATOR {
+   public:
+    bool init(std::unique_ptr<ALLOCATOR_HAL> hal) {
+        mHal = std::move(hal);
+        return true;
+    }
+
+    // IAllocator 2.0 interface
+    Return<void> dumpDebugInfo(IAllocator::dumpDebugInfo_cb hidl_cb) override {
+        hidl_cb(mHal->dumpDebugInfo());
+        return Void();
+    }
+
+    Return<void> allocate(const BufferDescriptor& descriptor, uint32_t count,
+                          IAllocator::allocate_cb hidl_cb) override {
+        uint32_t stride;
+        std::vector<const native_handle_t*> buffers;
+        Error error = mHal->allocateBuffers(descriptor, count, &stride, &buffers);
+        if (error != Error::NONE) {
+            hidl_cb(error, 0, hidl_vec<hidl_handle>());
+            return Void();
+        }
+
+        hidl_vec<hidl_handle> hidlBuffers(buffers.cbegin(), buffers.cend());
+        hidl_cb(Error::NONE, stride, hidlBuffers);
+
+        // free the local handles
+        mHal->freeBuffers(buffers);
+
+        return Void();
+    }
+
+   protected:
+    std::unique_ptr<ALLOCATOR_HAL> mHal;
+};
+
+}  // namespace detail
+
+using Allocator = detail::AllocatorImpl<IAllocator, AllocatorHal>;
+
+}  // namespace hal
+}  // namespace V2_0
+}  // namespace allocator
+}  // namespace graphics
+}  // namespace hardware
+}  // namespace android
diff --git a/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/AllocatorHal.h b/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/AllocatorHal.h
new file mode 100644
index 0000000..dec377a
--- /dev/null
+++ b/graphics/allocator/2.0/utils/hal/include/allocator-hal/2.0/AllocatorHal.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2018 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.
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
+#include <android/hardware/graphics/mapper/2.0/IMapper.h>
+
+namespace android {
+namespace hardware {
+namespace graphics {
+namespace allocator {
+namespace V2_0 {
+namespace hal {
+
+using mapper::V2_0::BufferDescriptor;
+using mapper::V2_0::Error;
+
+class AllocatorHal {
+   public:
+    virtual ~AllocatorHal() = default;
+
+    // dump the debug information
+    virtual std::string dumpDebugInfo() = 0;
+
+    // allocate buffers
+    virtual Error allocateBuffers(const BufferDescriptor& descriptor, uint32_t count,
+                                  uint32_t* outStride,
+                                  std::vector<const native_handle_t*>* outBuffers) = 0;
+
+    // free buffers
+    virtual void freeBuffers(const std::vector<const native_handle_t*>& buffers) = 0;
+};
+
+}  // namespace hal
+}  // namespace V2_0
+}  // namespace allocator
+}  // namespace graphics
+}  // namespace hardware
+}  // namespace android