Add HAL definition for graphics buffer allocator

It differs from gralloc1.h in that

 - buffer descriptors are created from a struct, BufferDescriptorInfo, to
   reduce round trips
 - testAllocate is a function of its own
 - buffer allocation and export are two different steps
 - reference counting and buffer mapping are moved to gralloc-mapper
 - BAD_HANDLE is renamed to BAD_BUFFER
 - GRALLOC1_CONSUMER_USAGE_FOREIGN_BUFFERS is removed
 - CPU_{READ,WRITE}_OFTEN no longer implies CPU_{READ,WRITE}

Test: make android.hardware.graphics.allocator@2.0

Change-Id: Ibe9367d5b1701c0e1009da829f27fed0f7d98828
diff --git a/Android.bp b/Android.bp
index 65febd3..727054c 100644
--- a/Android.bp
+++ b/Android.bp
@@ -3,6 +3,7 @@
     "audio/common/2.0",
     "audio/effect/2.0",
     "benchmarks/msgq/1.0",
+    "graphics/allocator/2.0",
     "memtrack/1.0",
     "memtrack/1.0/default",
     "nfc/1.0",
diff --git a/graphics/allocator/2.0/Android.bp b/graphics/allocator/2.0/Android.bp
new file mode 100644
index 0000000..f29c50b
--- /dev/null
+++ b/graphics/allocator/2.0/Android.bp
@@ -0,0 +1,46 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+
+genrule {
+    name: "android.hardware.graphics.allocator@2.0_genc++",
+    tool: "hidl-gen",
+    cmd: "$tool -o $genDir -Lc++ -randroid.hardware:hardware/interfaces android.hardware.graphics.allocator@2.0",
+    srcs: [
+        "types.hal",
+        "IAllocator.hal",
+    ],
+    out: [
+        "android/hardware/graphics/allocator/2.0/types.cpp",
+        "android/hardware/graphics/allocator/2.0/AllocatorAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.hardware.graphics.allocator@2.0_genc++_headers",
+    tool: "hidl-gen",
+    cmd: "$tool -o $genDir -Lc++ -randroid.hardware:hardware/interfaces android.hardware.graphics.allocator@2.0",
+    srcs: [
+        "types.hal",
+        "IAllocator.hal",
+    ],
+    out: [
+        "android/hardware/graphics/allocator/2.0/types.h",
+        "android/hardware/graphics/allocator/2.0/IAllocator.h",
+        "android/hardware/graphics/allocator/2.0/IHwAllocator.h",
+        "android/hardware/graphics/allocator/2.0/BnAllocator.h",
+        "android/hardware/graphics/allocator/2.0/BpAllocator.h",
+        "android/hardware/graphics/allocator/2.0/BsAllocator.h",
+    ],
+}
+
+cc_library_shared {
+    name: "android.hardware.graphics.allocator@2.0",
+    generated_sources: ["android.hardware.graphics.allocator@2.0_genc++"],
+    generated_headers: ["android.hardware.graphics.allocator@2.0_genc++_headers"],
+    export_generated_headers: ["android.hardware.graphics.allocator@2.0_genc++_headers"],
+    shared_libs: [
+        "libhidl",
+        "libhwbinder",
+        "libutils",
+        "libcutils",
+    ],
+}
diff --git a/graphics/allocator/2.0/IAllocator.hal b/graphics/allocator/2.0/IAllocator.hal
new file mode 100644
index 0000000..8accb82
--- /dev/null
+++ b/graphics/allocator/2.0/IAllocator.hal
@@ -0,0 +1,177 @@
+/*
+ * 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.
+ */
+
+package android.hardware.graphics.allocator@2.0;
+
+interface IAllocator {
+    enum Capability : int32_t {
+        /* reserved */
+        INVALID = 0,
+
+        /*
+         * testAllocate will always return UNDEFINED unless this capability
+         * is supported.
+         */
+        TEST_ALLOCATE = 1,
+    };
+
+    struct BufferDescriptorInfo {
+        /*
+         * The width specifies how many columns of pixels should be in the
+         * allocated buffer, but does not necessarily represent the offset in
+         * columns between the same column in adjacent rows. The rows may be
+         * padded.
+         */
+        uint32_t width;
+
+       /*
+        * The height specifies how many rows of pixels should be in the
+        * allocated buffer.
+        */
+        uint32_t height;
+
+        /* Buffer pixel format. */
+        PixelFormat format;
+
+        /*
+         * Buffer producer usage mask; valid flags can be found in the
+         * definition of ProducerUsage.
+         */
+        uint64_t producerUsageMask;
+
+        /*
+         * Buffer consumer usage mask; valid flags can be found in the
+         * definition of ConsumerUsage.
+         */
+        uint64_t consumerUsageMask;
+    };
+
+    /*
+     * Provides a list of supported capabilities (as described in the
+     * definition of Capability above). This list must not change after
+     * initialization.
+     *
+     * @return capabilities is a list of supported capabilities.
+     */
+    getCapabilities() generates (vec<Capability> capabilities);
+
+    /*
+     * Retrieves implementation-defined debug information, which will be
+     * displayed during, for example, `dumpsys SurfaceFlinger`.
+     *
+     * @return debugInfo is a string of debug information.
+     */
+    dumpDebugInfo() generates (string debugInfo);
+
+    /*
+     * Creates a new, opaque buffer descriptor.
+     *
+     * @param descriptorInfo specifies the attributes of the buffer
+     *        descriptor.
+     * @return error is NONE upon success. Otherwise,
+     *         BAD_VALUE when any attribute in descriptorInfo is invalid.
+     *         NO_RESOURCES when no more descriptors can currently be created.
+     * @return descriptor is the newly created buffer descriptor.
+     */
+    createDescriptor(BufferDescriptorInfo descriptorInfo)
+          generates (Error error,
+                     BufferDescriptor descriptor);
+
+    /*
+     * Destroys an existing buffer descriptor.
+     *
+     * @param descriptor is the descriptor to destroy.
+     * @return error is either NONE or BAD_DESCRIPTOR.
+     */
+    destroyDescriptor(BufferDescriptor descriptor) generates (Error error);
+
+    /*
+     * Tests whether a buffer allocation can succeed, ignoring potential
+     * resource contention which might lead to a NO_RESOURCES error.
+     *
+     * @param descriptors is a list of buffer descriptors.
+     * @return error is NONE or NOT_SHARED upon success;
+     *         NONE when buffers can be created and share a backing store.
+     *         NOT_SHARED when buffers can be created but require more than a
+     *                    backing store.
+     *         Otherwise,
+     *         BAD_DESCRIPTOR when any of the descriptors is invalid.
+     *         UNSUPPORTED when any of the descriptors can never be satisfied.
+     *         UNDEFINED when TEST_ALLOCATE is not listed in getCapabilities.
+     */
+    testAllocate(vec<BufferDescriptor> descriptors) generates (Error error);
+
+    /*
+     * Attempts to allocate a list of buffers sharing a backing store.
+     *
+     * Each buffer will correspond to one of the descriptors passed into the
+     * function and will hold a reference to its backing store. If the device
+     * is unable to share the backing store between the buffers, it must
+     * attempt to allocate the buffers with different backing stores and
+     * return NOT_SHARED if it is successful.
+     *
+     * @param descriptors is the buffer descriptors to attempt to allocate.
+     * @return error is NONE or NOT_SHARED upon success;
+     *         NONE when buffers can be created and share a backing store.
+     *         NOT_SHARED when buffers can be created but require more than a
+     *                    backing store.
+     *         Otherwise,
+     *         BAD_DESCRIPTOR when any of the descriptors is invalid.
+     *         UNSUPPORTED when any of the descriptors can never be satisfied.
+     *         NO_RESOURCES when any of the buffers cannot be created at this
+     *                      time.
+     * @return buffers is the allocated buffers.
+     */
+    allocate(vec<BufferDescriptor> descriptors)
+        generates (Error error,
+                   vec<Buffer> buffers);
+
+    /*
+     * Frees a buffer.
+     *
+     * @param buffer is the buffer to be freed.
+     * @return error is NONE upon success. Otherwise,
+     *         BAD_BUFFER when the buffer is invalid.
+     */
+    free(Buffer buffer) generates (Error error);
+
+    /*
+     * Exports a buffer for use in other client libraries or for cross-process
+     * sharing.
+     *
+     * The exported handle is a handle to the backing store of the buffer, not
+     * to the buffer itself. It however may not hold any reference to the
+     * backing store and may be considered invalid by client libraries. To use
+     * it and, in most cases, to save it for later use, a client must make a
+     * clone of the handle and have the cloned handle hold a reference to the
+     * backing store. Such a cloned handle will stay valid even after the
+     * original buffer is freed. Refer to native_handle_clone and IMapper for
+     * how a handle is cloned and how a reference is added.
+     *
+     * @param descriptor is the descriptor used to allocate the buffer.
+     * @param buffer is the buffer to be exported.
+     * @return error is NONE upon success. Otherwise,
+     *         BAD_DESCRIPTOR when the descriptor is invalid.
+     *         BAD_BUFFER when the buffer is invalid.
+     *         BAD_VALUE when descriptor and buffer do not match.
+     *         NO_RESOURCES when the buffer cannot be exported at this time.
+     * @return bufferHandle is the exported handle.
+     */
+    exportHandle(BufferDescriptor descriptor,
+                 Buffer buffer)
+      generates (Error error,
+                 handle bufferHandle);
+};
diff --git a/graphics/allocator/2.0/types.hal b/graphics/allocator/2.0/types.hal
new file mode 100644
index 0000000..23b7345
--- /dev/null
+++ b/graphics/allocator/2.0/types.hal
@@ -0,0 +1,171 @@
+/*
+ * 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.
+ */
+
+package android.hardware.graphics.allocator@2.0;
+
+enum Error : int32_t {
+    NONE            = 0, /* no error */
+    BAD_DESCRIPTOR  = 1, /* invalid BufferDescriptor */
+    BAD_BUFFER      = 2, /* invalid Buffer */
+    BAD_VALUE       = 3, /* invalid width, height, etc. */
+    NOT_SHARED      = 4, /* buffers not sharing backing store */
+    NO_RESOURCES    = 5, /* temporary failure due to resource contention */
+    UNDEFINED       = 6, /* an operation has no defined meaning */
+    UNSUPPORTED     = 7, /* permanent failure */
+};
+
+enum ProducerUsage : uint64_t {
+    /* bit 0 is reserved */
+
+    /* buffer will be read by CPU occasionally */
+    CPU_READ        = 1ULL << 1,
+    /* buffer will be read by CPU frequently */
+    CPU_READ_OFTEN  = 1ULL << 2,
+
+    /* bit 3 is reserved */
+    /* bit 4 is reserved */
+
+    /* buffer will be written by CPU occasionally */
+    CPU_WRITE       = 1ULL << 5,
+    /* buffer will be written by CPU frequently */
+    CPU_WRITE_OFTEN = 1ULL << 6,
+
+    /* bit 7 is reserved */
+    /* bit 8 is reserved */
+
+    /* buffer will be used as a GPU render target */
+    GPU_RENDER_TARGET = 1ULL << 9,
+
+    /* bit 10 is reserved */
+    /* bit 11 is reserved */
+    /* bit 12 is reserved */
+    /* bit 13 is reserved */
+
+    /*
+     * Buffer is allocated with hardware-level protection against copying the
+     * contents (or information derived from the contents) into unprotected
+     * memory.
+     */
+    PROTECTED         = 1ULL << 14,
+
+    /* bit 15 is reserved */
+    /* bit 16 is reserved */
+
+    /* buffer will be used as a camera HAL output */
+    CAMERA            = 1ULL << 17,
+
+    /* bit 18 is reserved */
+    /* bit 19 is reserved */
+    /* bit 20 is reserved */
+    /* bit 21 is reserved */
+
+    /* buffer will be used as a video decoder output */
+    VIDEO_DECODER     = 1ULL << 22,
+
+    /* bits 23-27 are reserved for future versions */
+    /* bits 28-31 are reserved for vendor extensions */
+
+    /* bits 32-47 are reserved for future versions */
+    /* bits 48-63 are reserved for vendor extensions */
+};
+
+enum ConsumerUsage : uint64_t {
+    /* bit 0 is reserved */
+
+    /* buffer will be read by CPU occasionally */
+    CPU_READ          = 1ULL << 1,
+    /* buffer will be read by CPU frequently */
+    CPU_READ_OFTEN    = 1ULL << 2,
+
+    /* bit 3 is reserved */
+    /* bit 4 is reserved */
+    /* bit 5 is reserved */
+    /* bit 6 is reserved */
+    /* bit 7 is reserved */
+
+    /* buffer will be used as a GPU texture */
+    GPU_TEXTURE       = 1ULL << 8,
+
+    /* bit 9 is reserved */
+    /* bit 10 is reserved */
+
+    /* buffer will be used by hwcomposer HAL */
+    HWCOMPOSER        = 1ULL << 11,
+    /* buffer will be as a hwcomposer HAL client target */
+    CLIENT_TARGET     = 1ULL << 12,
+
+    /* bit 13 is reserved */
+    /* bit 14 is reserved */
+
+    /* buffer will be used as a hwcomposer HAL cursor */
+    CURSOR            = 1ULL << 15,
+
+    /* buffer will be used as a video encoder input */
+    VIDEO_ENCODER     = 1ULL << 16,
+
+    /* bit 17 is reserved */
+
+    /* buffer will be used as a camera HAL input */
+    CAMERA            = 1ULL << 18,
+
+    /* bit 19 is reserved */
+
+    /* buffer will be used as a renderscript allocation */
+    RENDERSCRIPT      = 1ULL << 20,
+
+    /* bit 21 is reserved */
+    /* bit 22 is reserved */
+
+    /* bits 23-27 are reserved for future versions */
+    /* bits 28-31 are reserved for vendor extensions */
+
+    /* bits 32-47 are reserved for future versions */
+    /* bits 48-63 are reserved for vendor extensions */
+};
+
+/*
+ * Copied from android_pixel_format_t.
+ *
+ * TODO(olv) copy comments as well and have android_pixel_format_t generated
+ */
+@export(name="android_pixel_format", value_prefix="HAL_PIXEL_FORMAT_")
+enum PixelFormat : int32_t {
+    RGBA_8888              = 1,
+    RGBX_8888              = 2,
+    RGB_888                = 3,
+    RGB_565                = 4,
+    BGRA_8888              = 5,
+    YV12                   = 0x32315659,
+    Y8                     = 0x20203859,
+    Y16                    = 0x20363159,
+    RAW16                  = 0x20,
+    RAW10                  = 0x25,
+    RAW12                  = 0x26,
+    RAW_OPAQUE             = 0x24,
+    BLOB                   = 0x21,
+    IMPLEMENTATION_DEFINED = 0x22,
+    YCbCr_420_888          = 0x23,
+    YCbCr_422_888          = 0x27,
+    YCbCr_444_888          = 0x28,
+    FLEX_RGB_888           = 0x29,
+    FLEX_RGBA_8888         = 0x2A,
+    YCbCr_422_SP           = 0x10,
+    YCrCb_420_SP           = 0x11,
+    YCbCr_422_I            = 0x14,
+};
+
+typedef uint64_t BufferDescriptor;
+typedef uint64_t Buffer;