graphics: add mapper 2.1 HAL support library

Add android.hardware.graphics.mapper@2.1-hal to make it easier to
write 2.1 HALs.

Test: builds
Change-Id: I36d6b2c85ec623240582788505f29e635960dc0e
diff --git a/graphics/mapper/2.1/utils/hal/Android.bp b/graphics/mapper/2.1/utils/hal/Android.bp
new file mode 100644
index 0000000..2a4cc6e
--- /dev/null
+++ b/graphics/mapper/2.1/utils/hal/Android.bp
@@ -0,0 +1,33 @@
+//
+// Copyright (C) 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.
+
+cc_library_headers {
+    name: "android.hardware.graphics.mapper@2.1-hal",
+    defaults: ["hidl_defaults"],
+    vendor: true,
+    shared_libs: [
+        "android.hardware.graphics.mapper@2.1",
+    ],
+    export_shared_lib_headers: [
+        "android.hardware.graphics.mapper@2.1",
+    ],
+    header_libs: [
+        "android.hardware.graphics.mapper@2.0-hal",
+    ],
+    export_header_lib_headers: [
+        "android.hardware.graphics.mapper@2.0-hal",
+    ],
+    export_include_dirs: ["include"],
+}
diff --git a/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/Mapper.h b/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/Mapper.h
new file mode 100644
index 0000000..038f572
--- /dev/null
+++ b/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/Mapper.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 "Mapper.h included without LOG_TAG"
+#endif
+
+#include <android/hardware/graphics/mapper/2.1/IMapper.h>
+#include <mapper-hal/2.0/Mapper.h>
+#include <mapper-hal/2.1/MapperHal.h>
+
+namespace android {
+namespace hardware {
+namespace graphics {
+namespace mapper {
+namespace V2_1 {
+namespace hal {
+
+namespace detail {
+
+// MapperImpl implements V2_*::IMapper on top of V2_*::hal::MapperHal
+template <typename Interface, typename Hal>
+class MapperImpl : public V2_0::hal::detail::MapperImpl<Interface, Hal> {
+   public:
+    // IMapper 2.1 interface
+    Return<Error> validateBufferSize(void* buffer,
+                                     const IMapper::BufferDescriptorInfo& descriptorInfo,
+                                     uint32_t stride) {
+        const native_handle_t* bufferHandle = getImportedBuffer(buffer);
+        if (!bufferHandle) {
+            return Error::BAD_BUFFER;
+        }
+
+        return mHal->validateBufferSize(bufferHandle, descriptorInfo, stride);
+    }
+
+    Return<void> getTransportSize(void* buffer, IMapper::getTransportSize_cb hidl_cb) {
+        const native_handle_t* bufferHandle = getImportedBuffer(buffer);
+        if (!bufferHandle) {
+            hidl_cb(Error::BAD_BUFFER, 0, 0);
+            return Void();
+        }
+
+        uint32_t numFds = 0;
+        uint32_t numInts = 0;
+        Error error = mHal->getTransportSize(bufferHandle, &numFds, &numInts);
+        hidl_cb(error, numFds, numInts);
+        return Void();
+    }
+
+    Return<void> createDescriptor_2_1(const IMapper::BufferDescriptorInfo& descriptorInfo,
+                                      IMapper::createDescriptor_2_1_cb hidl_cb) override {
+        BufferDescriptor descriptor;
+        Error error = mHal->createDescriptor_2_1(descriptorInfo, &descriptor);
+        hidl_cb(error, descriptor);
+        return Void();
+    }
+
+   private:
+    using BaseType2_0 = V2_0::hal::detail::MapperImpl<Interface, Hal>;
+    using BaseType2_0::getImportedBuffer;
+    using BaseType2_0::mHal;
+};
+
+}  // namespace detail
+
+using Mapper = detail::MapperImpl<IMapper, MapperHal>;
+
+}  // namespace hal
+}  // namespace V2_1
+}  // namespace mapper
+}  // namespace graphics
+}  // namespace hardware
+}  // namespace android
diff --git a/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/MapperHal.h b/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/MapperHal.h
new file mode 100644
index 0000000..e488fab
--- /dev/null
+++ b/graphics/mapper/2.1/utils/hal/include/mapper-hal/2.1/MapperHal.h
@@ -0,0 +1,68 @@
+/*
+ * 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 <android/hardware/graphics/mapper/2.1/IMapper.h>
+#include <mapper-hal/2.0/MapperHal.h>
+
+namespace android {
+namespace hardware {
+namespace graphics {
+namespace mapper {
+namespace V2_1 {
+namespace hal {
+
+using V2_0::BufferDescriptor;
+using V2_0::Error;
+
+class MapperHal : public V2_0::hal::MapperHal {
+   public:
+    virtual ~MapperHal() = default;
+
+    // superceded by createDescriptor_2_1
+    Error createDescriptor(const V2_0::IMapper::BufferDescriptorInfo& descriptorInfo,
+                           BufferDescriptor* outDescriptor) override {
+        return createDescriptor_2_1(
+            IMapper::BufferDescriptorInfo{
+                descriptorInfo.width, descriptorInfo.height, descriptorInfo.layerCount,
+                static_cast<common::V1_1::PixelFormat>(descriptorInfo.format), descriptorInfo.usage,
+            },
+            outDescriptor);
+    }
+
+    // validate the buffer can be safely accessed with the specified
+    // descriptorInfo and stride
+    virtual Error validateBufferSize(const native_handle_t* bufferHandle,
+                                     const IMapper::BufferDescriptorInfo& descriptorInfo,
+                                     uint32_t stride) = 0;
+
+    // get the transport size of a buffer handle.  It can be smaller than or
+    // equal to the size of the buffer handle.
+    virtual Error getTransportSize(const native_handle_t* bufferHandle, uint32_t* outNumFds,
+                                   uint32_t* outNumInts) = 0;
+
+    // create a BufferDescriptor
+    virtual Error createDescriptor_2_1(const IMapper::BufferDescriptorInfo& descriptorInfo,
+                                       BufferDescriptor* outDescriptor) = 0;
+};
+
+}  // namespace hal
+}  // namespace V2_1
+}  // namespace mapper
+}  // namespace graphics
+}  // namespace hardware
+}  // namespace android