Codec2: separate platform dependency

- Move C2* objects out of android namespace
  - keep some stubs in place to not break dependencies
- Separate usage flags from gralloc usage flags
- Remove some unused classes

Bug: 64121714
Test: Build
Change-Id: Ibd0a5c2b4c3835a46aaa9c75c467bc728c2d85eb
diff --git a/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp b/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
index 32d6404..08b5d65 100644
--- a/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
+++ b/media/libstagefright/codec2/1.0/InputSurfaceConnection.cpp
@@ -22,7 +22,6 @@
 #include <C2BlockInternal.h>
 #include <C2PlatformSupport.h>
 
-#include <gui/Surface.h>
 #include <media/stagefright/codec2/1.0/InputSurfaceConnection.h>
 #include <system/window.h>
 
diff --git a/media/libstagefright/codec2/C2.cpp b/media/libstagefright/codec2/C2.cpp
index a51b073..359d4e5 100644
--- a/media/libstagefright/codec2/C2.cpp
+++ b/media/libstagefright/codec2/C2.cpp
@@ -22,8 +22,6 @@
 #include <C2ParamDef.h>
 #include <C2Work.h>
 
-namespace android {
-
 /**
  * There is nothing here yet. This library is built to see what symbols and methods get
  * defined as part of the API include files.
@@ -32,5 +30,4 @@
  * Codec2 clients.
  */
 
-} // namespace android
 
diff --git a/media/libstagefright/codec2/include/C2.h b/media/libstagefright/codec2/include/C2.h
index 00e3924..508f9ae 100644
--- a/media/libstagefright/codec2/include/C2.h
+++ b/media/libstagefright/codec2/include/C2.h
@@ -17,40 +17,18 @@
 #ifndef C2_H_
 #define C2_H_
 
-#include <string>
-#include <vector>
-#include <list>
-
-#ifdef __ANDROID__
-
-#include <utils/Errors.h>       // for status_t
-#include <utils/Timers.h>       // for nsecs_t
-
-namespace android {
-
-#else
-
 #include <errno.h>
-typedef int64_t nsecs_t;
 
-enum {
-    GRALLOC_USAGE_SW_READ_OFTEN,
-    GRALLOC_USAGE_RENDERSCRIPT,
-    GRALLOC_USAGE_HW_TEXTURE,
-    GRALLOC_USAGE_HW_COMPOSER,
-    GRALLOC_USAGE_HW_VIDEO_ENCODER,
-    GRALLOC_USAGE_PROTECTED,
-    GRALLOC_USAGE_SW_WRITE_OFTEN,
-    GRALLOC_USAGE_HW_RENDER,
-};
+#include <string>
 
-#endif
+/** nanoseconds with arbitrary origin. */
+typedef int64_t c2_nsecs_t;
 
 /** \mainpage Codec2
  *
- * Codec2 is a frame-based data processing API used by android.
+ * Codec2 is a generic frame-based data processing API.
  *
- * The framework accesses components via the \ref API.
+ * The media subsystem accesses components via the \ref API.
  */
 
 /** \ingroup API
@@ -109,53 +87,35 @@
  * c2_status_t: status codes used.
  */
 enum c2_status_t : int32_t {
-
 /*
- * Use android status constants if available. Otherwise, define the android status constants as
- * additional enum values using POSIX errno constants.
+ * Use POSIX errno constants.
  */
-#ifndef __ANDROID__
-    ALREADY_EXISTS      = -EEXIST,
-    BAD_VALUE           = -EINVAL,
-    BAD_INDEX           = -EOVERFLOW,
-    FAILED_TRANSACTION  = -ENOTSUP,
-    INVALID_OPERATION   = -ENOSYS,
-    NAME_NOT_FOUND      = -ENOENT,
-    NO_MEMORY           = -ENOMEM,
-    NO_INIT             = -ENODEV,
-    OK                  = 0,
-    PERMISSION_DENIED   = -EPERM,
-    TIMED_OUT           = -ETIMEDOUT,
-    UNKNOWN_ERROR       = -EFAULT,
-    UNKNOWN_TRANSACTION = -EBADMSG,
-    WOULD_BLOCK         = -EWOULDBLOCK,
-#endif
-
-    C2_OK        = OK,                   ///< operation completed successfully
+    C2_OK        = 0,            ///< operation completed successfully
 
     // bad input
-    C2_BAD_VALUE = BAD_VALUE,            ///< argument has invalid value (user error)
-    C2_BAD_INDEX = BAD_INDEX,            ///< argument uses invalid index (user error)
-    C2_CANNOT_DO = FAILED_TRANSACTION,   ///< argument/index is valid but not possible
+    C2_BAD_VALUE = EINVAL,       ///< argument has invalid value (user error)
+    C2_BAD_INDEX = ENXIO,        ///< argument uses invalid index (user error)
+    C2_CANNOT_DO = ENOTSUP,      ///< argument/index is valid but not possible
 
     // bad sequencing of events
-    C2_DUPLICATE = ALREADY_EXISTS,       ///< object already exists
-    C2_NOT_FOUND = NAME_NOT_FOUND,       ///< object not found
-    C2_BAD_STATE = INVALID_OPERATION,    ///< operation is not permitted in the current state
-    C2_BLOCKING  = WOULD_BLOCK,          ///< operation would block but blocking is not permitted
+    C2_DUPLICATE = EEXIST,       ///< object already exists
+    C2_NOT_FOUND = ENOENT,       ///< object not found
+    C2_BAD_STATE = EPERM,        ///< operation is not permitted in the current state
+    C2_BLOCKING  = EWOULDBLOCK,  ///< operation would block but blocking is not permitted
+    C2_CANCELED  = EINTR,        ///< operation interrupted/canceled
 
     // bad environment
-    C2_NO_MEMORY = NO_MEMORY,            ///< not enough memory to complete operation
-    C2_REFUSED   = PERMISSION_DENIED,    ///< missing permission to complete operation
+    C2_NO_MEMORY = ENOMEM,       ///< not enough memory to complete operation
+    C2_REFUSED   = EACCES,       ///< missing permission to complete operation
 
-    C2_TIMED_OUT = TIMED_OUT,            ///< operation did not complete within timeout
+    C2_TIMED_OUT = ETIMEDOUT,    ///< operation did not complete within timeout
 
     // bad versioning
-    C2_OMITTED   = UNKNOWN_TRANSACTION,  ///< operation is not implemented/supported (optional only)
+    C2_OMITTED   = ENOSYS,       ///< operation is not implemented/supported (optional only)
 
     // unknown fatal
-    C2_CORRUPTED = UNKNOWN_ERROR,        ///< some unexpected error prevented the operation
-    C2_NO_INIT   = NO_INIT,              ///< status has not been initialized
+    C2_CORRUPTED = EFAULT,       ///< some unexpected error prevented the operation
+    C2_NO_INIT   = ENODEV,       ///< status has not been initialized
 };
 
 /**
@@ -185,11 +145,12 @@
     type args& operator=(const type args&) = delete; \
     type(const type args&) = delete; \
 
-#define C2_PURE __attribute__((pure))
-#define C2_CONST __attribute__((const))
-#define C2_HIDE __attribute__((visibility("hidden")))
-#define C2_INTERNAL __attribute__((internal_linkage))
 #define C2_ALLOW_OVERFLOW __attribute__((no_sanitize("integer")))
+#define C2_CONST    __attribute__((const))
+#define C2_HIDE     __attribute__((visibility("hidden")))
+#define C2_INTERNAL __attribute__((internal_linkage))
+#define C2_PACK     __attribute__((packed))
+#define C2_PURE     __attribute__((pure))
 
 #define DEFINE_OTHER_COMPARISON_OPERATORS(type) \
     inline bool operator!=(const type &other) const { return !(*this == other); } \
@@ -548,32 +509,28 @@
 
 /// @}
 
-#ifdef __ANDROID__
-} // namespace android
-#endif
-
 #include <functional>
 template<typename T>
-struct std::less<::android::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
+struct std::less<::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
         return lh.peeku() < rh.peeku();
     }
 };
 template<typename T>
-struct std::less_equal<::android::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
+struct std::less_equal<::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
         return lh.peeku() <= rh.peeku();
     }
 };
 template<typename T>
-struct std::greater<::android::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
+struct std::greater<::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
         return lh.peeku() > rh.peeku();
     }
 };
 template<typename T>
-struct std::greater_equal<::android::c2_cntr_t<T>> {
-    constexpr bool operator()(const ::android::c2_cntr_t<T> &lh, const ::android::c2_cntr_t<T> &rh) const {
+struct std::greater_equal<::c2_cntr_t<T>> {
+    constexpr bool operator()(const ::c2_cntr_t<T> &lh, const ::c2_cntr_t<T> &rh) const {
         return lh.peeku() >= rh.peeku();
     }
 };
diff --git a/media/libstagefright/codec2/include/C2Buffer.h b/media/libstagefright/codec2/include/C2Buffer.h
index 034075f..e49f82b 100644
--- a/media/libstagefright/codec2/include/C2Buffer.h
+++ b/media/libstagefright/codec2/include/C2Buffer.h
@@ -18,27 +18,20 @@
 #define C2BUFFER_H_
 
 #include <C2.h>
+#include <C2BufferBase.h>
 #include <C2Param.h> // for C2Info
 
-#include <list>
 #include <memory>
+#include <vector>
 
 #ifdef __ANDROID__
-
-// #include <system/window.h>
-#include <cutils/native_handle.h>
-#include <hardware/gralloc.h> // TODO: remove
-
-typedef native_handle_t C2Handle;
-
+#include <android-C2Buffer.h>
 #else
 
 typedef void* C2Handle;
 
 #endif
 
-namespace android {
-
 /// \defgroup buffer Buffers
 /// @{
 
@@ -89,7 +82,7 @@
      * \retval C2_REFUSED       no permission to wait for the fence (unexpected - system)
      * \retval C2_CORRUPTED     some unknown error prevented waiting for the fence (unexpected)
      */
-    c2_status_t wait(nsecs_t timeoutNs);
+    c2_status_t wait(c2_nsecs_t timeoutNs);
 
     /**
      * Used to check if this fence is valid (if there is a chance for it to be signaled.)
@@ -550,41 +543,9 @@
   ALLOCATIONS
 **************************************************************************************************/
 
-/// \defgroup allocator Allocation and memory placement
+/// \ingroup allocator Allocation and memory placement
 /// @{
 
-/**
- * Buffer/memory usage bits. These are used by the allocators to select optimal memory type/pool and
- * buffer layout.
- *
- * \note This struct has public fields without getters/setters. All methods are inline.
- */
-struct C2MemoryUsage {
-// public:
-    // TODO: match these to gralloc1.h
-    enum Consumer : uint64_t {
-        // \todo do we need to distinguish often from rarely?
-        CPU_READ          = GRALLOC_USAGE_SW_READ_OFTEN,
-        RENDERSCRIPT_READ = GRALLOC_USAGE_RENDERSCRIPT,
-        HW_TEXTURE_READ   = GRALLOC_USAGE_HW_TEXTURE,
-        HW_COMPOSER_READ  = GRALLOC_USAGE_HW_COMPOSER,
-        HW_CODEC_READ     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
-        READ_PROTECTED    = GRALLOC_USAGE_PROTECTED,
-    };
-
-    enum Producer : uint64_t {
-        CPU_WRITE          = GRALLOC_USAGE_SW_WRITE_OFTEN,
-        RENDERSCRIPT_WRITE = GRALLOC_USAGE_RENDERSCRIPT,
-        HW_TEXTURE_WRITE   = GRALLOC_USAGE_HW_RENDER,
-        HW_COMPOSER_WRITE  = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER,
-        HW_CODEC_WRITE     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
-        WRITE_PROTECTED    = GRALLOC_USAGE_PROTECTED,
-    };
-
-    uint64_t consumer; // e.g. input
-    uint64_t producer; // e.g. output
-};
-
 class C2LinearAllocation;
 class C2GraphicAllocation;
 
@@ -2301,6 +2262,10 @@
 
 /// @}
 
-}  // namespace android
+// expose some objects in android namespace
+namespace android {
+    /// \deprecated
+    typedef ::C2Fence C2Fence;
+}
 
 #endif  // C2BUFFER_H_
diff --git a/media/libstagefright/codec2/include/C2BufferBase.h b/media/libstagefright/codec2/include/C2BufferBase.h
new file mode 100644
index 0000000..68411f2
--- /dev/null
+++ b/media/libstagefright/codec2/include/C2BufferBase.h
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+
+#ifndef C2BUFFER_BASE_H_
+#define C2BUFFER_BASE_H_
+
+/// \defgroup allocator Allocation and memory placement
+/// @{
+
+/**
+ * Buffer/memory usage bits. These shall be used by the allocators to select optimal memory type/
+ * pool and buffer layout. Usage bits are conceptually separated into read and write usage, while
+ * the buffer use life-cycle is separated into producers (writers) and consumers (readers).
+ * These two concepts are related but not equivalent: consumers may only read buffers and only
+ * producers may write to buffers; note, however, that buffer producers may also want or need to
+ * read the buffers.
+ *
+ * Read and write buffer usage bits shall be or-ed to arrive at the full buffer usage. Admittedly,
+ * this does not account for the amount of reading and writing (e.g. a buffer may have one or more
+ * readers); however, the proper information necessary to properly weigh the various usages would be
+ * the amount of data read/written for each usage type. This would result in an integer array of
+ * size 64 (or the number of distinct usages) for memory usage, and likely such detailed information
+ * would not always be available.
+ *
+ * That platform-agnostic Codec 2.0 API only defines the bare minimum usages. Platforms shall define
+ * usage bits that are appropriate for the platform.
+ */
+struct C2MemoryUsage {
+// public:
+    /**
+     * Buffer read usage.
+     */
+    enum Read : uint64_t {
+        /** Buffer is read by the CPU. */
+        CPU_READ        = 1 << 0,
+        /**
+         * Buffer shall only be read by trusted hardware. The definition of trusted hardware is
+         * platform specific, but this flag is reserved to prevent mapping this block into CPU
+         * readable memory resulting in bus fault. This flag can be used when buffer access must be
+         * protected.
+         */
+        READ_PROTECTED  = 1 << 1,
+    };
+
+    /**
+     * Buffer write usage.
+     */
+    enum Write : uint64_t {
+        /** Buffer is writted to by the CPU. */
+        CPU_WRITE        = 1 << 2,
+        /**
+         * Buffer shall only be written to by trusted hardware. The definition of trusted hardware
+         * is platform specific, but this flag is reserved to prevent mapping this block into CPU
+         * writable memory resulting in bus fault. This flag can be used when buffer integrity must
+         * be protected.
+         */
+        WRITE_PROTECTED  = 1 << 3,
+    };
+
+    enum : uint64_t {
+        /**
+         * Buffer usage bits reserved for the platform. We don't separately reserve read and
+         * write usages as platforms may have asymmetric distribution between them.
+         */
+        PLATFORM_MASK     = ~(CPU_READ | CPU_WRITE | READ_PROTECTED | WRITE_PROTECTED),
+    };
+
+    /** Create a usage from separate consumer and producer usage mask. \deprecated */
+    inline C2MemoryUsage(uint64_t consumer, uint64_t producer)
+        : expected(consumer | producer) { }
+
+    inline explicit C2MemoryUsage(uint64_t expected_)
+        : expected(expected_) { }
+
+    uint64_t expected; // expected buffer usage
+};
+
+/// @}
+
+#endif  // C2BUFFER_BASE_H_
+
diff --git a/media/libstagefright/codec2/include/C2Component.h b/media/libstagefright/codec2/include/C2Component.h
index 721966b..64bd1cb 100644
--- a/media/libstagefright/codec2/include/C2Component.h
+++ b/media/libstagefright/codec2/include/C2Component.h
@@ -29,13 +29,9 @@
 #include <C2Param.h>
 #include <C2Work.h>
 
-namespace android {
-
 /// \defgroup components Components
 /// @{
 
-class C2Component;
-
 struct C2FieldSupportedValuesQuery {
     enum type_t : uint32_t {
         POSSIBLE, ///< query all possible values regardless of other settings
@@ -947,6 +943,11 @@
 
 /// @}
 
-}  // namespace android
+namespace android {
+    /// \deprecated
+    typedef ::C2Component C2Component;
+    /// \deprecated
+    typedef ::C2ComponentInterface C2ComponentInterface;
+}
 
 #endif  // C2COMPONENT_H_
diff --git a/media/libstagefright/codec2/include/C2Config.h b/media/libstagefright/codec2/include/C2Config.h
index 2a2b9de..3f149bb 100644
--- a/media/libstagefright/codec2/include/C2Config.h
+++ b/media/libstagefright/codec2/include/C2Config.h
@@ -19,8 +19,6 @@
 
 #include <C2ParamDef.h>
 
-namespace android {
-
 /// \defgroup config Component configuration
 /// @{
 
@@ -262,6 +260,4 @@
 
 /// @}
 
-} // namespace android
-
 #endif
diff --git a/media/libstagefright/codec2/include/C2Param.h b/media/libstagefright/codec2/include/C2Param.h
index 4d9f707..181697d 100644
--- a/media/libstagefright/codec2/include/C2Param.h
+++ b/media/libstagefright/codec2/include/C2Param.h
@@ -23,13 +23,10 @@
 #include <stdint.h>
 
 #include <algorithm>
-#include <list>
 #include <string>
 #include <type_traits>
-
-#define C2_PACK __attribute__((packed))
-
-namespace android {
+#include <utility>
+#include <vector>
 
 /// \addtogroup Parameters
 /// @{
@@ -89,7 +86,6 @@
  */
 
 /// \ingroup internal
-struct _C2ParamManipulator;
 
 /**
  * Parameter base class.
@@ -733,17 +729,6 @@
 };
 
 /**
- * Structure uniquely specifying a field, an array element of a field, or a
- * parameter in a configuration
- */
-struct C2ParamOrField : public C2ParamField {
-//public:
-    template<typename S>
-    inline C2ParamOrField(S* param)
-        : C2ParamField(param->index(), 0u, param->size()) {}
-};
-
-/**
  * A shared (union) representation of numeric values
  */
 class C2Value {
@@ -1376,6 +1361,4 @@
 
 /// @}
 
-}  // namespace android
-
 #endif  // C2PARAM_H_
diff --git a/media/libstagefright/codec2/include/C2ParamDef.h b/media/libstagefright/codec2/include/C2ParamDef.h
index 3691e01..f0b6223 100644
--- a/media/libstagefright/codec2/include/C2ParamDef.h
+++ b/media/libstagefright/codec2/include/C2ParamDef.h
@@ -24,8 +24,6 @@
 
 #include <C2Param.h>
 
-namespace android {
-
 /// \addtogroup Parameters
 /// @{
 
@@ -905,6 +903,4 @@
 
 /// @}
 
-}  // namespace android
-
 #endif  // C2PARAM_DEF_H_
diff --git a/media/libstagefright/codec2/include/C2Work.h b/media/libstagefright/codec2/include/C2Work.h
index b6c5814..a2f02e5 100644
--- a/media/libstagefright/codec2/include/C2Work.h
+++ b/media/libstagefright/codec2/include/C2Work.h
@@ -28,8 +28,6 @@
 #include <list>
 #include <vector>
 
-namespace android {
-
 /// \defgroup work Work and data processing
 /// @{
 
@@ -75,9 +73,8 @@
 //  WORK
 // ================================================================================================
 
-// c2_node_id_t-s
+/** Unique ID for a processing node. */
 typedef uint32_t c2_node_id_t;
-typedef c2_node_id_t c2_node_id_t;
 
 enum {
     kParamIndexWorkOrdinal,
@@ -211,6 +208,4 @@
 
 /// @}
 
-}  // namespace android
-
 #endif  // C2WORK_H_
diff --git a/media/libstagefright/codec2/include/android-C2Buffer.h b/media/libstagefright/codec2/include/android-C2Buffer.h
new file mode 100644
index 0000000..c71f2cf
--- /dev/null
+++ b/media/libstagefright/codec2/include/android-C2Buffer.h
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+#ifndef ANDROID_C2BUFFER_H_
+#define ANDROID_C2BUFFER_H_
+
+#include <cutils/native_handle.h>
+#include <hardware/gralloc.h>
+
+/* Use android native handle for C2Handle */
+typedef ::native_handle_t C2Handle;
+
+namespace android {
+
+/**
+ * Android platform buffer/memory usage bits.
+ */
+struct C2AndroidMemoryUsage : public C2MemoryUsage {
+// public:
+    /**
+     * Reuse gralloc flags where possible, as Codec 2.0 API only uses bits 0 and 1.
+     */
+    enum Consumer : uint64_t {
+        RENDERSCRIPT_READ = GRALLOC_USAGE_RENDERSCRIPT,
+        HW_TEXTURE_READ   = GRALLOC_USAGE_HW_TEXTURE,
+        HW_COMPOSER_READ  = GRALLOC_USAGE_HW_COMPOSER,
+        HW_CODEC_READ     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
+        READ_PROTECTED    = GRALLOC_USAGE_PROTECTED,
+    };
+
+    enum Producer : uint64_t {
+        RENDERSCRIPT_WRITE = GRALLOC_USAGE_RENDERSCRIPT,
+        HW_TEXTURE_WRITE   = GRALLOC_USAGE_HW_RENDER,
+        HW_COMPOSER_WRITE  = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER,
+        HW_CODEC_WRITE     = GRALLOC_USAGE_HW_VIDEO_ENCODER,
+        WRITE_PROTECTED    = GRALLOC_USAGE_PROTECTED,
+    };
+
+    /**
+     * Convert from gralloc usage.
+     */
+    static C2MemoryUsage FromGrallocUsage(uint64_t usage);
+
+    /**
+     * Convert to gralloc usage.
+     */
+    uint64_t asGrallocUsage() const;
+};
+
+}  // namespace android
+
+#endif  // ANDROID_C2BUFFER_H_
diff --git a/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h b/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
index fc19acd..b24a416 100644
--- a/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
+++ b/media/libstagefright/codec2/include/media/stagefright/codec2/1.0/InputSurfaceConnection.h
@@ -25,9 +25,6 @@
 #include <media/stagefright/codec2/1.0/InputSurfaceConnection.h>
 
 namespace android {
-
-class C2Allocator;
-
 namespace hardware {
 namespace media {
 namespace c2 {
diff --git a/media/libstagefright/codec2/tests/C2Param_test.cpp b/media/libstagefright/codec2/tests/C2Param_test.cpp
index 1a29add..168b889 100644
--- a/media/libstagefright/codec2/tests/C2Param_test.cpp
+++ b/media/libstagefright/codec2/tests/C2Param_test.cpp
@@ -23,8 +23,6 @@
 #include <util/C2ParamUtils.h>
 #include <C2ParamDef.h>
 
-namespace android {
-
 void PrintTo(const _C2FieldId &id, ::std::ostream* os) {
     *os << "@" << id._mOffset << "+" << id._mSize;
 }
@@ -2397,15 +2395,11 @@
 
 // ***********************
 
-}
-
 #include <util/C2ParamUtils.h>
 #include <C2Config.h>
 #include <C2Component.h>
 #include <unordered_map>
 
-namespace android {
-
 C2ENUM(
     MetadataType, int32_t,
     kInvalid = -1,
@@ -2935,4 +2929,3 @@
     EXPECT_EQ(15.25f, fp);
 }
 
-} // namespace android
diff --git a/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp b/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
index b287ca8..b255eec 100644
--- a/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
+++ b/media/libstagefright/codec2/vndk/C2AllocatorGralloc.cpp
@@ -29,6 +29,36 @@
 
 namespace android {
 
+namespace {
+    enum : uint64_t {
+        /**
+         * Usage mask that is passed through from gralloc to Codec 2.0 usage.
+         */
+        PASSTHROUGH_USAGE_MASK =
+            ~(GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK | GRALLOC_USAGE_PROTECTED)
+    };
+
+    // verify that passthrough mask is within the platform mask
+    static_assert((~C2MemoryUsage::PLATFORM_MASK & PASSTHROUGH_USAGE_MASK) == 0, "");
+}
+
+C2MemoryUsage C2AndroidMemoryUsage::FromGrallocUsage(uint64_t usage) {
+    // gralloc does not support WRITE_PROTECTED
+    return C2MemoryUsage(
+            ((usage & GRALLOC_USAGE_SW_READ_MASK) ? C2MemoryUsage::CPU_READ : 0) |
+            ((usage & GRALLOC_USAGE_SW_WRITE_MASK) ? C2MemoryUsage::CPU_WRITE : 0) |
+            ((usage & GRALLOC_USAGE_PROTECTED) ? C2MemoryUsage::READ_PROTECTED : 0) |
+            (usage & PASSTHROUGH_USAGE_MASK));
+}
+
+uint64_t C2AndroidMemoryUsage::asGrallocUsage() const {
+    // gralloc does not support WRITE_PROTECTED
+    return (((expected & C2MemoryUsage::CPU_READ) ? GRALLOC_USAGE_SW_READ_MASK : 0) |
+            ((expected & C2MemoryUsage::CPU_WRITE) ? GRALLOC_USAGE_SW_WRITE_MASK : 0) |
+            ((expected & C2MemoryUsage::READ_PROTECTED) ? GRALLOC_USAGE_PROTECTED : 0) |
+            (expected & PASSTHROUGH_USAGE_MASK));
+}
+
 using ::android::hardware::graphics::allocator::V2_0::IAllocator;
 using ::android::hardware::graphics::common::V1_0::BufferUsage;
 using ::android::hardware::graphics::common::V1_0::PixelFormat;
diff --git a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
index 4328a8d..a9613e4 100644
--- a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
+++ b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
@@ -20,6 +20,7 @@
 
 #include <ion/ion.h>
 #include <sys/mman.h>
+#include <unistd.h>
 
 #include <C2AllocatorIon.h>
 #include <C2Buffer.h>
@@ -232,10 +233,10 @@
 
         int prot = PROT_NONE;
         int flags = MAP_PRIVATE;
-        if (usage.consumer & C2MemoryUsage::CPU_READ) {
+        if (usage.expected & C2MemoryUsage::CPU_READ) {
             prot |= PROT_READ;
         }
-        if (usage.producer & C2MemoryUsage::CPU_WRITE) {
+        if (usage.expected & C2MemoryUsage::CPU_WRITE) {
             prot |= PROT_WRITE;
             flags = MAP_SHARED;
         }
diff --git a/media/libstagefright/codec2/vndk/C2Buffer.cpp b/media/libstagefright/codec2/vndk/C2Buffer.cpp
index 511ffe0..47fdca1 100644
--- a/media/libstagefright/codec2/vndk/C2Buffer.cpp
+++ b/media/libstagefright/codec2/vndk/C2Buffer.cpp
@@ -18,14 +18,13 @@
 #define LOG_TAG "C2Buffer"
 #include <utils/Log.h>
 
+#include <list>
 #include <map>
 #include <mutex>
 
 #include <C2BufferPriv.h>
 #include <C2BlockInternal.h>
 
-namespace android {
-
 namespace {
 
 // This anonymous namespace contains the helper classes that allow our implementation to create
@@ -34,63 +33,63 @@
 // Inherit from the parent, share with the friend.
 class ReadViewBuddy : public C2ReadView {
     using C2ReadView::C2ReadView;
-    friend class ::android::C2ConstLinearBlock;
+    friend class ::C2ConstLinearBlock;
 };
 
 class WriteViewBuddy : public C2WriteView {
     using C2WriteView::C2WriteView;
-    friend class ::android::C2LinearBlock;
+    friend class ::C2LinearBlock;
 };
 
 class ConstLinearBlockBuddy : public C2ConstLinearBlock {
     using C2ConstLinearBlock::C2ConstLinearBlock;
-    friend class ::android::C2LinearBlock;
+    friend class ::C2LinearBlock;
 };
 
 class LinearBlockBuddy : public C2LinearBlock {
     using C2LinearBlock::C2LinearBlock;
-    friend class ::android::C2BasicLinearBlockPool;
+    friend class ::C2BasicLinearBlockPool;
 };
 
 class AcquirableReadViewBuddy : public C2Acquirable<C2ReadView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::android::C2ConstLinearBlock;
+    friend class ::C2ConstLinearBlock;
 };
 
 class AcquirableWriteViewBuddy : public C2Acquirable<C2WriteView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::android::C2LinearBlock;
+    friend class ::C2LinearBlock;
 };
 
 class GraphicViewBuddy : public C2GraphicView {
     using C2GraphicView::C2GraphicView;
-    friend class ::android::C2ConstGraphicBlock;
-    friend class ::android::C2GraphicBlock;
+    friend class ::C2ConstGraphicBlock;
+    friend class ::C2GraphicBlock;
 };
 
 class AcquirableConstGraphicViewBuddy : public C2Acquirable<const C2GraphicView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::android::C2ConstGraphicBlock;
+    friend class ::C2ConstGraphicBlock;
 };
 
 class AcquirableGraphicViewBuddy : public C2Acquirable<C2GraphicView> {
     using C2Acquirable::C2Acquirable;
-    friend class ::android::C2GraphicBlock;
+    friend class ::C2GraphicBlock;
 };
 
 class ConstGraphicBlockBuddy : public C2ConstGraphicBlock {
     using C2ConstGraphicBlock::C2ConstGraphicBlock;
-    friend class ::android::C2GraphicBlock;
+    friend class ::C2GraphicBlock;
 };
 
 class GraphicBlockBuddy : public C2GraphicBlock {
     using C2GraphicBlock::C2GraphicBlock;
-    friend class ::android::C2BasicGraphicBlockPool;
+    friend class ::C2BasicGraphicBlockPool;
 };
 
 class BufferDataBuddy : public C2BufferData {
     using C2BufferData::C2BufferData;
-    friend class ::android::C2Buffer;
+    friend class ::C2Buffer;
 };
 
 }  // namespace
@@ -803,4 +802,3 @@
     return std::shared_ptr<C2Buffer>(new C2Buffer({ block }));
 }
 
-} // namespace android
diff --git a/media/libstagefright/codec2/vndk/C2Store.cpp b/media/libstagefright/codec2/vndk/C2Store.cpp
index 05b46c3..c4ed2f4 100644
--- a/media/libstagefright/codec2/vndk/C2Store.cpp
+++ b/media/libstagefright/codec2/vndk/C2Store.cpp
@@ -136,13 +136,13 @@
     switch (id) {
     case C2BlockPool::BASIC_LINEAR:
         res = allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &allocator);
-        if (res == OK) {
+        if (res == C2_OK) {
             *pool = std::make_shared<C2BasicLinearBlockPool>(allocator);
         }
         break;
     case C2BlockPool::BASIC_GRAPHIC:
         res = allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_GRAPHIC, &allocator);
-        if (res == OK) {
+        if (res == C2_OK) {
             *pool = std::make_shared<C2BasicGraphicBlockPool>(allocator);
         }
         break;
@@ -345,7 +345,7 @@
 
 c2_status_t C2PlatformComponentStore::ComponentModule::createInterface(
         c2_node_id_t id, std::shared_ptr<C2ComponentInterface> *interface,
-        std::function<void(::android::C2ComponentInterface*)> deleter) {
+        std::function<void(::C2ComponentInterface*)> deleter) {
     interface->reset();
     if (mInit != C2_OK) {
         return mInit;
@@ -362,7 +362,7 @@
 
 c2_status_t C2PlatformComponentStore::ComponentModule::createComponent(
         c2_node_id_t id, std::shared_ptr<C2Component> *component,
-        std::function<void(::android::C2Component*)> deleter) {
+        std::function<void(::C2Component*)> deleter) {
     component->reset();
     if (mInit != C2_OK) {
         return mInit;
diff --git a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
index 875a8c2..977cf7b 100644
--- a/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
+++ b/media/libstagefright/codec2/vndk/include/C2BufferPriv.h
@@ -21,8 +21,6 @@
 
 #include <C2Buffer.h>
 
-namespace android {
-
 class C2BasicLinearBlockPool : public C2BlockPool {
 public:
     explicit C2BasicLinearBlockPool(const std::shared_ptr<C2Allocator> &allocator);
@@ -73,6 +71,4 @@
     const std::shared_ptr<C2Allocator> mAllocator;
 };
 
-} // namespace android
-
 #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_
diff --git a/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h b/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
index cfea104..7168498 100644
--- a/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
+++ b/media/libstagefright/codec2/vndk/include/C2ComponentFactory.h
@@ -22,8 +22,6 @@
 #include <functional>
 #include <memory>
 
-namespace android {
-
 /**
  * Component factory object that enables to create a component and/or interface from a dynamically
  * linked library. This is needed because the component/interfaces are managed objects, but we
@@ -36,8 +34,8 @@
  */
 class C2ComponentFactory {
 public:
-    typedef std::function<void(::android::C2Component*)> ComponentDeleter;
-    typedef std::function<void(::android::C2ComponentInterface*)> InterfaceDeleter;
+    typedef std::function<void(::C2Component*)> ComponentDeleter;
+    typedef std::function<void(::C2ComponentInterface*)> InterfaceDeleter;
 
     /**
      * Creates a component.
@@ -81,9 +79,12 @@
 
     virtual ~C2ComponentFactory() = default;
 
-    typedef ::android::C2ComponentFactory* (*CreateCodec2FactoryFunc)(void);
-    typedef void (*DestroyCodec2FactoryFunc)(::android::C2ComponentFactory*);
+    typedef ::C2ComponentFactory* (*CreateCodec2FactoryFunc)(void);
+    typedef void (*DestroyCodec2FactoryFunc)(::C2ComponentFactory*);
 };
-} // namespace android
+
+namespace android {
+    typedef ::C2ComponentFactory C2ComponentFactory;
+}
 
 #endif // STAGEFRIGHT_CODEC2_COMPONENT_FACTORY_H_
diff --git a/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h b/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
index 41132b9..5b995f6 100644
--- a/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
+++ b/media/libstagefright/codec2/vndk/include/C2ErrnoUtils.h
@@ -20,8 +20,6 @@
 #include <errno.h>
 #include <C2.h>
 
-namespace android {
-
 // standard ERRNO mappings
 template<int N> constexpr c2_status_t _c2_errno2status_impl();
 template<> constexpr c2_status_t _c2_errno2status_impl<0>()       { return C2_OK; }
@@ -52,7 +50,5 @@
     return _c2_map_errno_impl<N...>::map(result);
 }
 
-} // namespace android
-
 #endif // STAGEFRIGHT_CODEC2_ERRNO_UTILS_H_
 
diff --git a/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h b/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
index 76b02ed..afa51ee 100644
--- a/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
+++ b/media/libstagefright/codec2/vndk/include/C2PlatformSupport.h
@@ -86,6 +86,7 @@
  * \retval nullptr if the platform component store could not be obtained
  */
 std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore();
+
 } // namespace android
 
 #endif // STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_
diff --git a/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h b/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
index 3168248..1accc2c 100644
--- a/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
+++ b/media/libstagefright/codec2/vndk/include/util/C2ParamUtils.h
@@ -21,13 +21,14 @@
 #include <util/_C2MacroUtils.h>
 
 #include <iostream>
+#include <list>
+#include <utility>
+#include <vector>
 
 /** \file
  * Utilities for parameter handling to be used by Codec2 implementations.
  */
 
-namespace android {
-
 /// \cond INTERNAL
 
 /* ---------------------------- UTILITIES FOR ENUMERATION REFLECTION ---------------------------- */
@@ -313,7 +314,5 @@
 
 /* ---------------------------- UTILITIES FOR ENUMERATION REFLECTION ---------------------------- */
 
-}  // namespace android
-
 #endif  // C2UTILS_PARAM_UTILS_H_
 
diff --git a/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h b/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
index 9c68369..25003cb 100644
--- a/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
+++ b/media/libstagefright/codec2/vndk/internal/C2BlockInternal.h
@@ -19,8 +19,6 @@
 
 #include <C2Buffer.h>
 
-namespace android {
-
 struct _C2BlockPoolData;
 
 /**
@@ -64,7 +62,5 @@
             const C2Rect &allottedCrop = C2Rect(~0u, ~0u));
 };
 
-}
-
 #endif // ANDROID_STAGEFRIGHT_C2BLOCK_INTERNAL_H_
 
diff --git a/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h b/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
index 5bf3009..c805830 100644
--- a/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
+++ b/media/libstagefright/codec2/vndk/internal/C2ParamInternal.h
@@ -19,8 +19,6 @@
 
 #include <C2Param.h>
 
-namespace android {
-
 struct C2_HIDE _C2ParamInspector {
     inline static uint32_t getIndex(const C2ParamField &pf) {
         return pf._mIndex;
@@ -44,7 +42,6 @@
     }
 };
 
-}
 
 #endif // ANDROID_STAGEFRIGHT_C2PARAM_INTERNAL_H_
 
diff --git a/media/libstagefright/codecs/cmds/codec2.cpp b/media/libstagefright/codecs/cmds/codec2.cpp
index 8022b84..95b0c61 100644
--- a/media/libstagefright/codecs/cmds/codec2.cpp
+++ b/media/libstagefright/codecs/cmds/codec2.cpp
@@ -59,11 +59,6 @@
 #include <C2PlatformSupport.h>
 #include <C2Work.h>
 
-extern "C" ::android::C2ComponentFactory *CreateCodec2Factory();
-extern "C" void DestroyCodec2Factory(::android::C2ComponentFactory *);
-
-#include "../avcdec/C2SoftAvcDec.h"
-
 using namespace android;
 using namespace std::chrono_literals;