Merge 24Q3 (ab/11976889) to aosp-main-future

Bug: 347831320
Merged-In: I8aab2ecb53c89facd1ae6d499f0733617bfb505e
Change-Id: I6ca36cc7836e175ef04fe77d8fdaec112e7bbd97
diff --git a/gralloc4/Android.bp b/gralloc4/Android.bp
index 7cdc78f..33fa1db 100644
--- a/gralloc4/Android.bp
+++ b/gralloc4/Android.bp
@@ -72,6 +72,33 @@
 	],
 }
 
+soong_config_module_type {
+    name: "gralloc_defaults",
+    module_type: "cc_defaults",
+    config_namespace: "arm_gralloc",
+    variables: [
+        "mapper_version",
+    ],
+    properties: [
+        "srcs",
+        "target",
+        "cflags",
+        "shared_libs",
+        "static_libs",
+        "header_libs",
+        "defaults",
+        "stem",
+    ],
+}
+
+soong_config_string_variable {
+    name: "mapper_version",
+    values: [
+        "mapper4",
+        "mapper5",
+    ],
+}
+
 arm_gralloc_cc_defaults {
 	name: "arm_gralloc_defaults",
 	owner: "arm",
diff --git a/gralloc4/service/aidl/Android.bp b/gralloc4/service/aidl/Android.bp
index a3d7a87..038801a 100644
--- a/gralloc4/service/aidl/Android.bp
+++ b/gralloc4/service/aidl/Android.bp
@@ -2,23 +2,73 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+soong_config_module_type_import {
+    from: "hardware/google/gchips/gralloc4/Android.bp",
+    module_types: ["gralloc_defaults"],
+}
+
+cc_defaults {
+    name: "allocator_gralloc4_defaults",
+    init_rc: [
+        "android.hardware.graphics.allocator-aidl-service.rc",
+    ],
+    vintf_fragments: ["manifest_gralloc_aidl.xml"],
+    shared_libs: [
+        "android.hardware.graphics.allocator-V1-ndk",
+        "android.hardware.graphics.allocator-aidl-impl",
+    ],
+    required: [
+        "android.hardware.graphics.allocator-aidl-impl",
+    ],
+}
+
+cc_defaults {
+    name: "allocator_gralloc5_defaults",
+    init_rc: [
+        "android.hardware.graphics.allocator2-aidl-service.rc",
+    ],
+    vintf_fragments: ["manifest_gralloc_aidl2.xml"],
+    shared_libs: [
+        "android.hardware.graphics.allocator-V2-ndk",
+        "android.hardware.graphics.allocator-aidl-impl",
+    ],
+    required: [
+        "android.hardware.graphics.allocator-aidl-impl",
+    ],
+}
+
+// Should only be used in pixel_gralloc_allocator because of 'stem'
+gralloc_defaults {
+    name: "allocator_version_defaults",
+    soong_config_variables: {
+        mapper_version: {
+            mapper4: {
+                stem: "android.hardware.graphics.allocator-V1-service",
+                defaults: ["allocator_gralloc4_defaults"],
+            },
+            mapper5: {
+                stem: "android.hardware.graphics.allocator-V2-service",
+                defaults: ["allocator_gralloc5_defaults"],
+            },
+        },
+    },
+}
+
 cc_binary {
-    name: "android.hardware.graphics.allocator-V1-service",
+    name: "pixel_gralloc_allocator",
     proprietary: true,
     relative_install_path: "hw",
     srcs: [
         "service.cpp",
     ],
-    init_rc: [
-        "android.hardware.graphics.allocator-aidl-service.rc",
+    defaults: [
+        "allocator_version_defaults",
+        "arm_gralloc_version_defaults",
     ],
-    vintf_fragments: ["manifest_gralloc_aidl.xml"],
     header_libs: [
         "libgralloc_headers",
     ],
     shared_libs: [
-        "android.hardware.graphics.allocator-V1-ndk",
-        "android.hardware.graphics.allocator-aidl-impl",
         "libbinder_ndk",
         "liblog",
         "libutils",
@@ -26,7 +76,4 @@
     static_libs: [
         "libaidlcommonsupport",
     ],
-    required: [
-        "android.hardware.graphics.allocator-aidl-impl",
-    ],
 }
diff --git a/gralloc4/service/aidl/android.hardware.graphics.allocator2-aidl-service.rc b/gralloc4/service/aidl/android.hardware.graphics.allocator2-aidl-service.rc
new file mode 100644
index 0000000..723fab6
--- /dev/null
+++ b/gralloc4/service/aidl/android.hardware.graphics.allocator2-aidl-service.rc
@@ -0,0 +1,6 @@
+service vendor.graphics.allocator-default /vendor/bin/hw/android.hardware.graphics.allocator-V2-service
+    class hal animation
+    user system
+    group graphics drmrpc
+    capabilities SYS_NICE
+    priority -20
diff --git a/gralloc4/service/aidl/manifest_gralloc_aidl2.xml b/gralloc4/service/aidl/manifest_gralloc_aidl2.xml
new file mode 100644
index 0000000..0eab17a
--- /dev/null
+++ b/gralloc4/service/aidl/manifest_gralloc_aidl2.xml
@@ -0,0 +1,7 @@
+<manifest version="1.0" type="device">
+    <hal format="aidl">
+        <name>android.hardware.graphics.allocator</name>
+        <version>2</version>
+        <fqname>IAllocator/default</fqname>
+    </hal>
+</manifest>
diff --git a/gralloc4/service/aidl/service.cpp b/gralloc4/service/aidl/service.cpp
index 515a31d..69c63f8 100644
--- a/gralloc4/service/aidl/service.cpp
+++ b/gralloc4/service/aidl/service.cpp
@@ -1,4 +1,5 @@
-#define LOG_TAG "gralloc-V1-service"
+#undef LOG_TAG
+#define LOG_TAG "gralloc-allocator"
 
 #include <android/binder_ibinder_platform.h>
 #include <android/binder_manager.h>
@@ -6,7 +7,11 @@
 #include <android/binder_status.h>
 #include <log/log.h>
 
+#if defined(GRALLOC_MAPPER_4)
 #include "aidl/GrallocAllocator.h"
+#elif defined (GRALLOC_MAPPER_5)
+#include "aidl/GrallocAllocator2.h"
+#endif
 
 using namespace android;
 
diff --git a/gralloc4/src/4.x/Android.bp b/gralloc4/src/4.x/Android.bp
index d37fdf0..d2f75d7 100644
--- a/gralloc4/src/4.x/Android.bp
+++ b/gralloc4/src/4.x/Android.bp
@@ -20,71 +20,27 @@
 }
 
 cc_defaults {
-	name: "arm_gralloc_version_defaults",
-	target: {
-		android: {
-			shared_libs: [
-				"android.hardware.graphics.common@1.2",
-			],
-		},
-	},
-	cflags: [
-		"-DLOG_TAG=\"gralloc4\"",
-		"-DGRALLOC_VERSION_MAJOR=4",
-		"-DHIDL_ALLOCATOR_VERSION_SCALED=400",
-		"-DHIDL_MAPPER_VERSION_SCALED=400",
-		"-DHIDL_COMMON_VERSION_SCALED=120",
-	],
-}
-
-cc_defaults {
-	name: "arm_gralloc_api_4x_defaults",
+	name: "gralloc_mapper4_defaults",
 	defaults: [
+		"arm_gralloc_api_defaults",
 		"arm_gralloc_defaults",
 		"arm_gralloc_version_defaults",
 	],
-	compile_multilib: "both",
 	relative_install_path: "hw/",
-	shared_libs: [
-		"libhardware",
-		"liblog",
-		"libcutils",
-		"libdmabufheap",
-		"libsync",
-		"libutils",
-		"libnativewindow",
-		"libhidlbase",
-		"libgralloctypes",
-	],
-	static_libs: [
-		"libarect",
-		"libgralloc_core",
-		"libgralloc_allocator",
-		"libgralloc_capabilities",
-		"libdrm",
-	],
-	header_libs: [
-		"libnativebase_headers",
-	],
 	export_shared_lib_headers: [
 		"libhidlbase",
 	],
-}
-
-cc_library_shared {
-	name: "android.hardware.graphics.mapper@4.0-impl",
-	defaults: [
-		"arm_gralloc_api_4x_defaults",
-	],
+	compile_multilib: "both",
 	static_libs: [
 		"libgralloc_drmutils",
 	],
 	shared_libs: [
 		"arm.graphics-V1-ndk",
 		"android.hardware.graphics.mapper@4.0",
+		"libhidlbase",
 	],
 	srcs: [
-		"GrallocMapper.cpp",
+		"4.x/GrallocMapper.cpp",
 		":libgralloc_hidl_common_mapper",
 		":libgralloc_hidl_common_mapper_metadata",
 		":libgralloc_hidl_common_shared_metadata",
@@ -92,7 +48,4 @@
 	include_dirs: [
 		"hardware/google/gchips/include",
 	],
-	cflags: [
-		"-DGRALLOC_MAPPER_4",
-	],
 }
diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp
index 1f640ea..93d9bf5 100644
--- a/gralloc4/src/Android.bp
+++ b/gralloc4/src/Android.bp
@@ -19,6 +19,101 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+soong_config_module_type_import {
+    from: "hardware/google/gchips/gralloc4/Android.bp",
+    module_types: ["gralloc_defaults"],
+}
+
+// Only to be used in pixel_gralloc_mapper because of 'stem'
+gralloc_defaults {
+    name: "gralloc_mapper_defaults",
+    soong_config_variables: {
+        mapper_version: {
+            mapper4: {
+                stem: "android.hardware.graphics.mapper@4.0-impl",
+                defaults: ["gralloc_mapper4_defaults"],
+            },
+            mapper5: {
+                stem: "mapper.pixel",
+                defaults: ["gralloc_mapper5_defaults"],
+            },
+        },
+    },
+}
+
+cc_library_shared {
+    name: "pixel_gralloc_mapper",
+    defaults: ["gralloc_mapper_defaults"],
+}
+
+gralloc_defaults {
+    name: "arm_gralloc_version_defaults",
+    soong_config_variables: {
+        mapper_version: {
+            mapper4: {
+                cflags: [
+                    "-DLOG_TAG=\"gralloc4\"",
+                    "-DGRALLOC_VERSION_MAJOR=4",
+                    "-DHIDL_ALLOCATOR_VERSION_SCALED=400",
+                    "-DHIDL_MAPPER_VERSION_SCALED=400",
+                    "-DHIDL_COMMON_VERSION_SCALED=120",
+                    "-DGRALLOC_MAPPER_4",
+                ],
+            },
+            mapper5: {
+                cflags: [
+                    "-DGRALLOC_VERSION_MAJOR=5",
+                    "-DHIDL_MAPPER_VERSION_SCALED=500",
+                    "-DLOG_TAG=\"gralloc5\"",
+                    "-DGRALLOC_MAPPER_5",
+                ],
+            },
+        },
+    },
+}
+
+gralloc_defaults {
+    name: "arm_gralloc_api_defaults",
+    shared_libs: [
+        "liblog",
+        "libcutils",
+        "libdmabufheap",
+        "libsync",
+        "libutils",
+        "libnativewindow",
+        "libgralloctypes",
+        "libhardware",
+        "android.hardware.graphics.common@1.2",
+    ],
+    static_libs: [
+        "libarect",
+        "libgralloc_core",
+        "libgralloc_allocator",
+        "libgralloc_capabilities",
+        "libdrm",
+    ],
+    header_libs: [
+        "libnativebase_headers",
+    ],
+    soong_config_variables: {
+        mapper_version: {
+            mapper4: {
+                shared_libs: [
+                    "libhidlbase",
+                ],
+            },
+            mapper5: {
+                header_libs: [
+                    "libbase_headers",
+                    "libimapper_stablec",
+                    "libimapper_providerutils",
+                    "pixel-gralloc-headers",
+                ],
+            },
+        },
+    },
+}
+
 cc_library_headers {
     name: "libgralloc_headers",
     vendor: true,
diff --git a/gralloc4/src/aidl/Android.bp b/gralloc4/src/aidl/Android.bp
index afa0e6d..f31fe8b 100644
--- a/gralloc4/src/aidl/Android.bp
+++ b/gralloc4/src/aidl/Android.bp
@@ -2,31 +2,56 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+soong_config_module_type_import {
+    from: "hardware/google/gchips/gralloc4/Android.bp",
+    module_types: ["gralloc_defaults"],
+}
+
+gralloc_defaults {
+    name: "allocator_srcs",
+    soong_config_variables: {
+        mapper_version: {
+            mapper4: {
+                srcs: ["GrallocAllocator.cpp"],
+                shared_libs: [
+                    "android.hardware.graphics.allocator-V1-ndk",
+                    "android.hardware.graphics.mapper@4.0",
+                ],
+            },
+            mapper5: {
+                srcs: ["GrallocAllocator2.cpp"],
+                shared_libs: [
+                    "android.hardware.graphics.allocator-V2-ndk",
+                ],
+
+            },
+        },
+    },
+}
+
 cc_library_shared {
     name: "android.hardware.graphics.allocator-aidl-impl",
     proprietary: true,
     relative_install_path: "hw",
     defaults: [
-        "arm_gralloc_api_4x_defaults",
+        "arm_gralloc_api_defaults",
+        "arm_gralloc_defaults",
+        "arm_gralloc_version_defaults",
+        "allocator_srcs",
     ],
     shared_libs: [
-        "android.hardware.graphics.allocator-V1-ndk",
         "android.hardware.graphics.allocator@4.0",
         "libbinder_ndk",
+        "libhidlbase",
     ],
     static_libs: [
         "libaidlcommonsupport",
     ],
     srcs: [
-        "GrallocAllocator.cpp",
-        ":libgralloc_hidl_common_allocator",
         ":libgralloc_hidl_common_shared_metadata",
+        ":libgralloc_hidl_common_allocator",
     ],
     include_dirs: [
         "hardware/google/gchips/include",
     ],
-    cflags: [
-        "-DGRALLOC_MAPPER_4",
-    ],
 }
-
diff --git a/gralloc4/src/aidl/GrallocAllocator2.cpp b/gralloc4/src/aidl/GrallocAllocator2.cpp
new file mode 100644
index 0000000..a4ff6e9
--- /dev/null
+++ b/gralloc4/src/aidl/GrallocAllocator2.cpp
@@ -0,0 +1,193 @@
+#include "GrallocAllocator2.h"
+
+#include <aidl/android/hardware/graphics/allocator/AllocationError.h>
+#include <aidlcommonsupport/NativeHandle.h>
+#include <android/binder_ibinder.h>
+#include <android/binder_status.h>
+#include <cutils/android_filesystem_config.h>
+#include <hidl/HidlSupport.h>
+#include "hidl_common/Allocator.h"
+
+namespace pixel::allocator {
+
+namespace AidlAllocator = aidl::android::hardware::graphics::allocator;
+
+using android::hardware::hidl_handle;
+using android::hardware::hidl_vec;
+using HidlError = android::hardware::graphics::mapper::V4_0::Error;
+
+unsigned long callingPid() {
+    return static_cast<unsigned long>(AIBinder_getCallingPid());
+}
+
+unsigned long callingUid() {
+    return static_cast<unsigned long>(AIBinder_getCallingUid());
+}
+
+GrallocAllocator::GrallocAllocator() {}
+
+GrallocAllocator::~GrallocAllocator() {}
+
+ndk::ScopedAStatus GrallocAllocator::allocate(const std::vector<uint8_t>& descriptor, int32_t count,
+                                              AidlAllocator::AllocationResult* result) {
+    MALI_GRALLOC_LOGV("Allocation request from process: %lu", callingPid());
+
+    buffer_descriptor_t bufferDescriptor;
+    if (!arm::mapper::common::grallocDecodeBufferDescriptor(hidl_vec(descriptor),
+                                                            bufferDescriptor)) {
+        return ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(AidlAllocator::AllocationError::BAD_DESCRIPTOR));
+    }
+
+    // TODO(layog@): This dependency between AIDL and HIDL backends is not good.
+    // Ideally common::allocate should return the result and it should be encoded
+    // by this interface into HIDL or AIDL.
+    HidlError error = HidlError::NONE;
+    auto hidl_cb = [&](HidlError _error, int _stride, hidl_vec<hidl_handle> _buffers) {
+        if (_error != HidlError::NONE) {
+            error = _error;
+            return;
+        }
+
+        const uint32_t size = _buffers.size();
+
+        result->stride = _stride;
+        result->buffers.resize(size);
+        for (uint32_t i = 0; i < size; i++) {
+            // Dup here is necessary. After this callback returns common::allocate
+            // will free the buffer which will destroy the older fd.
+            result->buffers[i] = android::dupToAidl(static_cast<const native_handle*>(_buffers[i]));
+        }
+    };
+
+    arm::allocator::common::allocate(bufferDescriptor, count, hidl_cb);
+
+    switch (error) {
+        case HidlError::NONE:
+            break;
+
+        case HidlError::BAD_DESCRIPTOR:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::BAD_DESCRIPTOR));
+
+        case HidlError::NO_RESOURCES:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::NO_RESOURCES));
+
+        case HidlError::UNSUPPORTED:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::UNSUPPORTED));
+
+        default:
+            return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR);
+    }
+
+    return ndk::ScopedAStatus::ok();
+}
+
+buffer_descriptor_t toInternalDescriptor(
+        const AidlAllocator::BufferDescriptorInfo& descriptor) {
+    buffer_descriptor_t bufferDescriptor;
+    bufferDescriptor.width = descriptor.width;
+    bufferDescriptor.height = descriptor.height;
+    bufferDescriptor.layer_count = descriptor.layerCount;
+    bufferDescriptor.hal_format = static_cast<uint64_t>(descriptor.format);
+    bufferDescriptor.producer_usage = static_cast<uint64_t>(descriptor.usage);
+    bufferDescriptor.consumer_usage = bufferDescriptor.producer_usage;
+    bufferDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
+    bufferDescriptor.signature = sizeof(buffer_descriptor_t);
+    bufferDescriptor.reserved_size = descriptor.reservedSize;
+    const char *str = (const char*) descriptor.name.data();
+    bufferDescriptor.name = std::string(str);
+    return bufferDescriptor;
+}
+
+ndk::ScopedAStatus GrallocAllocator::allocate2(
+        const AidlAllocator::BufferDescriptorInfo& descriptor, int32_t count,
+        AidlAllocator::AllocationResult* result) {
+    MALI_GRALLOC_LOGV("Allocation request from process: %lu", callingPid());
+
+    buffer_descriptor_t bufferDescriptor = toInternalDescriptor(descriptor);
+
+    HidlError error = HidlError::NONE;
+    auto hidl_cb = [&](HidlError _error, int _stride, hidl_vec<hidl_handle> _buffers) {
+        if (_error != HidlError::NONE) {
+            error = _error;
+            return;
+        }
+
+        const uint32_t size = _buffers.size();
+
+        result->stride = _stride;
+        result->buffers.resize(size);
+        for (uint32_t i = 0; i < size; i++) {
+            // Dup here is necessary. After this callback returns common::allocate
+            // will free the buffer which will destroy the older fd.
+            result->buffers[i] = android::dupToAidl(static_cast<const native_handle*>(_buffers[i]));
+        }
+    };
+
+    arm::allocator::common::allocate(bufferDescriptor, count, hidl_cb);
+
+    switch (error) {
+        case HidlError::NONE:
+            break;
+
+        case HidlError::BAD_DESCRIPTOR:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::BAD_DESCRIPTOR));
+
+        case HidlError::NO_RESOURCES:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::NO_RESOURCES));
+
+        case HidlError::UNSUPPORTED:
+            return ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(AidlAllocator::AllocationError::UNSUPPORTED));
+
+        default:
+            return ndk::ScopedAStatus::fromStatus(STATUS_UNKNOWN_ERROR);
+    }
+
+    return ndk::ScopedAStatus::ok();
+}
+
+// TODO(b/315883761): isSupported should return false for unknown-to-HAL usage
+ndk::ScopedAStatus GrallocAllocator::isSupported(
+        const AidlAllocator::BufferDescriptorInfo& descriptor, bool* result) {
+    buffer_descriptor_t bufferDescriptor = toInternalDescriptor(descriptor);
+
+    bool isBufferDescriptorSupported = arm::allocator::common::isSupported(&bufferDescriptor);
+    *result = isBufferDescriptorSupported;
+
+    if (isBufferDescriptorSupported) {
+        MALI_GRALLOC_LOGV("Allocation for the given description will not succeed");
+    }
+    return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus GrallocAllocator::getIMapperLibrarySuffix(std::string* result) {
+    *result = "pixel";
+    return ndk::ScopedAStatus::ok();
+}
+
+binder_status_t GrallocAllocator::dump(int fd, const char** /* args */, uint32_t numArgs) {
+    if (callingUid() != AID_ROOT) {
+        const std::string permission_denied = "Permission Denied\n";
+        write(fd, permission_denied.c_str(), permission_denied.size());
+        return STATUS_PERMISSION_DENIED;
+    }
+
+    if (numArgs != 0) {
+        const std::string argument_error = "No argument expected\n";
+        write(fd, argument_error.c_str(), argument_error.size());
+        return STATUS_BAD_VALUE;
+    }
+
+    const std::string dump_info = arm::allocator::common::dump();
+    write(fd, dump_info.c_str(), dump_info.size());
+    return STATUS_OK;
+}
+
+} // namespace pixel::allocator
+
diff --git a/gralloc4/src/aidl/GrallocAllocator2.h b/gralloc4/src/aidl/GrallocAllocator2.h
new file mode 100644
index 0000000..91655a7
--- /dev/null
+++ b/gralloc4/src/aidl/GrallocAllocator2.h
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <aidl/android/hardware/graphics/allocator/AllocationResult.h>
+#include <aidl/android/hardware/graphics/allocator/BnAllocator.h>
+#include <aidl/android/hardware/graphics/allocator/BufferDescriptorInfo.h>
+#include <aidlcommonsupport/NativeHandle.h>
+
+#include <cstdint>
+#include <string>
+#include <vector>
+
+namespace pixel {
+namespace allocator {
+
+namespace AidlAllocator = aidl::android::hardware::graphics::allocator;
+
+class GrallocAllocator : public AidlAllocator::BnAllocator {
+public:
+    GrallocAllocator();
+
+    ~GrallocAllocator();
+
+    virtual ndk::ScopedAStatus allocate(const std::vector<uint8_t>& descriptor, int32_t count,
+                                        AidlAllocator::AllocationResult* result) override;
+
+    virtual ndk::ScopedAStatus allocate2(const AidlAllocator::BufferDescriptorInfo& descriptor,
+                                         int32_t count,
+                                         AidlAllocator::AllocationResult* result) override;
+
+    virtual ndk::ScopedAStatus isSupported(const AidlAllocator::BufferDescriptorInfo& descriptor,
+                                           bool* result) override;
+
+    virtual ndk::ScopedAStatus getIMapperLibrarySuffix(std::string* result) override;
+
+    virtual binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
+};
+
+} // namespace allocator
+} // namespace pixel
diff --git a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp
index 344ab2a..19078c9 100644
--- a/gralloc4/src/core/mali_gralloc_bufferaccess.cpp
+++ b/gralloc4/src/core/mali_gralloc_bufferaccess.cpp
@@ -233,25 +233,21 @@
 		*vaddr = buf_addr.value();
 
 		buffer_sync(hnd, get_tx_direction(usage));
+		return mali_gralloc_reference_lock_retain(buffer);
 	}
 
 	return 0;
+
 }
 
 
 /*
  *  Unlocks the given buffer.
  *
- * @param m           [in]   Gralloc module.
  * @param buffer      [in]   The buffer to unlock.
  *
  * @return 0, when the locking is successful;
  *         Appropriate error, otherwise
- *
- * Note: unlocking a buffer which is not locked results in an unexpected behaviour.
- *       Though it is possible to create a state machine to track the buffer state to
- *       recognize erroneous conditions, it is expected of client to adhere to API
- *       call sequence
  */
 int mali_gralloc_unlock(buffer_handle_t buffer)
 {
@@ -264,5 +260,5 @@
 	private_handle_t *hnd = (private_handle_t *)buffer;
 	buffer_sync(hnd, TX_NONE);
 
-	return 0;
+	return mali_gralloc_reference_lock_release(buffer);
 }
diff --git a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
index 0c1389f..aa489b9 100644
--- a/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
+++ b/gralloc4/src/core/mali_gralloc_bufferallocation.cpp
@@ -52,6 +52,7 @@
 #define BIG_BYTE_ALIGN_DEFAULT 64
 #ifdef SOC_ZUMA
 #define CAMERA_RAW_BUFFER_BYTE_ALIGN 32
+#define CAMERA_YUV_BUFFER_BYTE_ALIGN 64
 #endif
 
 /* Realign YV12 format so that chroma stride is half of luma stride */
@@ -660,6 +661,11 @@
 				 */
 				hw_align = std::max(hw_align, static_cast<uint32_t>(CAMERA_RAW_BUFFER_BYTE_ALIGN));
 			}
+
+			if (has_camera_usage && format.is_yuv) {
+				/* Camera ISP requires YUV buffers to have 64-byte aligned stride */
+				hw_align = lcm(hw_align, static_cast<uint32_t>(CAMERA_YUV_BUFFER_BYTE_ALIGN));
+			}
 #endif
 
 			if (has_BIG_usage) {
diff --git a/gralloc4/src/core/mali_gralloc_reference.cpp b/gralloc4/src/core/mali_gralloc_reference.cpp
index c685c13..880e8b5 100644
--- a/gralloc4/src/core/mali_gralloc_reference.cpp
+++ b/gralloc4/src/core/mali_gralloc_reference.cpp
@@ -43,6 +43,7 @@
         size_t metadata_size;
 
         uint64_t ref_count = 0;
+        uint64_t locked_ref_count = 0;
     };
 
     BufferManager() = default;
@@ -224,6 +225,27 @@
     }
 
 public:
+    int buffer_lock_retain(buffer_handle_t buf) EXCLUDES(lock) {
+        std::lock_guard<std::mutex> _l(lock);
+        private_handle_t *hnd =
+                reinterpret_cast<private_handle_t *>(const_cast<native_handle *>(buf));
+        auto it = buffer_map.find(hnd);
+        if (it == buffer_map.end()) return -EINVAL;
+        it->second->locked_ref_count++;
+        return 0;
+    }
+
+    int buffer_lock_release(buffer_handle_t buf) EXCLUDES(lock) {
+        std::lock_guard<std::mutex> _l(lock);
+        private_handle_t *hnd =
+                reinterpret_cast<private_handle_t *>(const_cast<native_handle *>(buf));
+        auto it = buffer_map.find(hnd);
+        if (it == buffer_map.end()) return -EINVAL;
+        if (it->second->locked_ref_count == 0) return -EINVAL;
+
+        it->second->locked_ref_count--;
+        return 0;
+    }
     static BufferManager &getInstance() {
         static BufferManager instance;
         return instance;
@@ -371,3 +393,11 @@
 std::optional<void *> mali_gralloc_reference_get_metadata_addr(buffer_handle_t handle) {
     return BufferManager::getInstance().get_metadata_addr(handle);
 }
+
+int mali_gralloc_reference_lock_retain(buffer_handle_t handle) {
+    return BufferManager::getInstance().buffer_lock_retain(handle);
+}
+
+int mali_gralloc_reference_lock_release(buffer_handle_t handle) {
+    return BufferManager::getInstance().buffer_lock_release(handle);
+}
diff --git a/gralloc4/src/core/mali_gralloc_reference.h b/gralloc4/src/core/mali_gralloc_reference.h
index bddcead..95a40b2 100644
--- a/gralloc4/src/core/mali_gralloc_reference.h
+++ b/gralloc4/src/core/mali_gralloc_reference.h
@@ -29,5 +29,7 @@
 
 std::optional<void*> mali_gralloc_reference_get_buf_addr(buffer_handle_t handle);
 std::optional<void*> mali_gralloc_reference_get_metadata_addr(buffer_handle_t handle);
+int mali_gralloc_reference_lock_retain(buffer_handle_t handle);
+int mali_gralloc_reference_lock_release(buffer_handle_t handle);
 
 #endif /* MALI_GRALLOC_REFERENCE_H_ */
diff --git a/gralloc4/src/hidl_common/Allocator.cpp b/gralloc4/src/hidl_common/Allocator.cpp
index d854255..b7770a1 100644
--- a/gralloc4/src/hidl_common/Allocator.cpp
+++ b/gralloc4/src/hidl_common/Allocator.cpp
@@ -245,6 +245,11 @@
 	return ss.str();
 }
 
+bool isSupported(buffer_descriptor_t *const bufDescriptor) {
+        // this is used as the criteria to determine which allocations succeed.
+        return (mali_gralloc_derive_format_and_size(bufDescriptor) == 0);
+}
+
 } // namespace common
 } // namespace allocator
 } // namespace arm
diff --git a/gralloc4/src/hidl_common/Allocator.h b/gralloc4/src/hidl_common/Allocator.h
index d9cd9bd..46e0694 100644
--- a/gralloc4/src/hidl_common/Allocator.h
+++ b/gralloc4/src/hidl_common/Allocator.h
@@ -59,6 +59,8 @@
 
 const std::string dump();
 
+bool isSupported(buffer_descriptor_t *const bufDescriptor);
+
 } // namespace common
 } // namespace allocator
 } // namespace arm
diff --git a/gralloc4/src/hidl_common/RegisteredHandlePool.cpp b/gralloc4/src/hidl_common/RegisteredHandlePool.cpp
index 3c99e4f..7e1afda 100644
--- a/gralloc4/src/hidl_common/RegisteredHandlePool.cpp
+++ b/gralloc4/src/hidl_common/RegisteredHandlePool.cpp
@@ -42,6 +42,7 @@
 
 bool RegisteredHandlePool::isRegistered(buffer_handle_t buffer)
 {
+    std::lock_guard<std::mutex> lock(mutex);
     return (bufPool.find(buffer) != bufPool.end());
 }
 
diff --git a/gralloc4/src/hidl_common/SharedMetadata.cpp b/gralloc4/src/hidl_common/SharedMetadata.cpp
index 1a8cc2a..5d23e70 100644
--- a/gralloc4/src/hidl_common/SharedMetadata.cpp
+++ b/gralloc4/src/hidl_common/SharedMetadata.cpp
@@ -101,11 +101,6 @@
 
 android::status_t set_smpte2086(const private_handle_t *hnd, const std::optional<Smpte2086> &smpte2086)
 {
-	if (!smpte2086.has_value())
-	{
-		return android::BAD_VALUE;
-	}
-
 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
 	metadata->smpte2086 = aligned_optional(smpte2086);
 
@@ -120,11 +115,6 @@
 
 android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional<Cta861_3> &cta861_3)
 {
-	if (!cta861_3.has_value())
-	{
-		return android::BAD_VALUE;
-	}
-
 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
 	metadata->cta861_3 = aligned_optional(cta861_3);
 
diff --git a/gralloc4/src/stable-c/Android.bp b/gralloc4/src/stable-c/Android.bp
index 3456a94..0195b4b 100644
--- a/gralloc4/src/stable-c/Android.bp
+++ b/gralloc4/src/stable-c/Android.bp
@@ -20,55 +20,14 @@
 }
 
 cc_defaults {
-    name: "arm_gralloc_version_stablec_defaults",
-    cflags: [
-        "-DGRALLOC_VERSION_MAJOR=5",
-        "-DHIDL_MAPPER_VERSION_SCALED=500",
-        "-DLOG_TAG=\"gralloc5\"",
-        "-DGRALLOC_MAPPER_5",
-    ],
-}
-
-cc_defaults {
-    name: "arm_gralloc_api_stablec_defaults",
+    name: "gralloc_mapper5_defaults",
     defaults: [
+        "arm_gralloc_api_defaults",
         "arm_gralloc_defaults",
-        "arm_gralloc_version_stablec_defaults",
+        "arm_gralloc_version_defaults",
     ],
     relative_install_path: "hw/",
-    shared_libs: [
-        "libhardware",
-        "liblog",
-        "android.hardware.graphics.common@1.2",
-        "libcutils",
-        "libdmabufheap",
-        "libsync",
-        "libutils",
-        "libnativewindow",
-        "libgralloctypes",
-    ],
-    static_libs: [
-        "libarect",
-        "libgralloc_core",
-        "libgralloc_allocator",
-        "libgralloc_capabilities",
-        "libdrm",
-    ],
-    header_libs: [
-        "libnativebase_headers",
-        "libbase_headers",
-        "libimapper_stablec",
-        "libimapper_providerutils",
-        "pixel-gralloc-headers",
-    ],
-}
-
-cc_library_shared {
-    name: "mapper.pixel",
-    defaults: [
-        "arm_gralloc_api_stablec_defaults",
-    ],
-    vintf_fragments: ["manifest.xml"],
+    vintf_fragments: ["stable-c/manifest.xml"],
     static_libs: [
         "libgralloc_drmutils",
     ],
@@ -78,7 +37,7 @@
         "libcutils",
     ],
     srcs: [
-        "GrallocMapper.cpp",
+        "stable-c/GrallocMapper.cpp",
         ":libgralloc_hidl_common_mapper",
         ":libgralloc_hidl_common_mapper_metadata",
         ":libgralloc_hidl_common_shared_metadata",
diff --git a/gralloc4/src/stable-c/GrallocMapper.cpp b/gralloc4/src/stable-c/GrallocMapper.cpp
index bf54597..085b3c8 100644
--- a/gralloc4/src/stable-c/GrallocMapper.cpp
+++ b/gralloc4/src/stable-c/GrallocMapper.cpp
@@ -195,7 +195,17 @@
     if constexpr (metadataType == StandardMetadataType::STRIDE) {
         std::vector<PlaneLayout> layouts;
         Error err = static_cast<Error>(common::get_plane_layouts(hnd, &layouts));
-        uint64_t stride = (layouts[0].strideInBytes * 8) / layouts[0].sampleIncrementInBits;
+        uint64_t stride = 0;
+        switch (hnd->get_alloc_format())
+        {
+            case HAL_PIXEL_FORMAT_RAW10:
+            case HAL_PIXEL_FORMAT_RAW12:
+                  stride = layouts[0].strideInBytes;
+                  break;
+            default:
+                  stride = (layouts[0].strideInBytes * 8) / layouts[0].sampleIncrementInBits;
+                  break;
+        }
         return provide(stride);
     }
     return -AIMapper_Error::AIMAPPER_ERROR_UNSUPPORTED;
@@ -225,17 +235,24 @@
             auto result = ::pixel::graphics::utils::encode(
                     common::get_video_hdr(static_cast<const private_handle_t*>(handle)));
 
-            outData = result.data();
-            outDataSize = result.size();
-            return -AIMapper_Error::AIMAPPER_ERROR_NONE;
+            if (result.size() <= outDataSize)
+                std::memcpy(outData, result.data(), result.size());
+            return result.size();
         }
         case PixelMetadataType::VIDEO_ROI: {
             auto result = ::pixel::graphics::utils::encode(
                     common::get_video_roiinfo(static_cast<const private_handle_t*>(handle)));
-            outData = result.data();
-            outDataSize = result.size();
-            return -AIMapper_Error::AIMAPPER_ERROR_NONE;
+            if (result.size() <= outDataSize)
+                std::memcpy(outData, result.data(), result.size());
+            return result.size();
         }
+        case PixelMetadataType::VIDEO_GMV: {
+            auto result = ::pixel::graphics::utils::encode(
+                    common::get_video_gmv(static_cast<const private_handle_t*>(handle)));
+            if (result.size() <= outDataSize) std::memcpy(outData, result.data(), result.size());
+            return result.size();
+        }
+
         default:
             return -AIMapper_Error::AIMAPPER_ERROR_BAD_VALUE;
     }
@@ -319,12 +336,29 @@
     return applyStandardMetadata(standardMeta, metadata, metadataSize, applier);
 }
 
+AIMapper_Error setPixelMetadata(buffer_handle_t handle, const PixelMetadataType meta,
+                                const void* metadata, size_t metadataSize) {
+    if (meta == PixelMetadataType::VIDEO_GMV) {
+        std::vector<uint8_t> in_data(metadataSize);
+        std::memcpy(in_data.data(), metadata, metadataSize);
+        auto gmv = ::pixel::graphics::utils::decode<common::VideoGMV>(in_data);
+        if (!gmv.has_value()) return AIMapper_Error::AIMAPPER_ERROR_BAD_VALUE;
+        auto result =
+                common::set_video_gmv(static_cast<const private_handle_t*>(handle), gmv.value());
+        if (result == android::OK) return AIMapper_Error::AIMAPPER_ERROR_NONE;
+    }
+    return AIMapper_Error::AIMAPPER_ERROR_BAD_VALUE;
+}
+
 AIMapper_Error GrallocMapper::setMetadata(buffer_handle_t _Nonnull buffer,
                                           AIMapper_MetadataType metadataType,
                                           const void* _Nonnull metadata, size_t metadataSize) {
     if (buffer == nullptr) return AIMapper_Error::AIMAPPER_ERROR_BAD_BUFFER;
     if (isStandardMetadataType(common::MetadataType(metadataType))) {
         return setStandardMetadata(buffer, metadataType.value, metadata, metadataSize);
+    } else if (isPixelMetadataType(common::MetadataType(metadataType))) {
+        const PixelMetadataType pixelMeta = static_cast<PixelMetadataType>(metadataType.value);
+        return setPixelMetadata(buffer, pixelMeta, metadata, metadataSize);
     } else
         return AIMapper_Error::AIMAPPER_ERROR_NONE;
 }
diff --git a/libvendorgraphicbuffer/Android.bp b/libvendorgraphicbuffer/Android.bp
index 0230bfb..e2957fe 100644
--- a/libvendorgraphicbuffer/Android.bp
+++ b/libvendorgraphicbuffer/Android.bp
@@ -19,10 +19,56 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+soong_config_module_type {
+	name: "libvendorgraphicbuffer_cc_defaults",
+	module_type: "cc_defaults",
+	config_namespace: "arm_gralloc",
+	variables: [
+		"mapper_version",
+	],
+	properties: [
+		"srcs",
+		"shared_libs",
+		"header_libs",
+	],
+}
+
+soong_config_string_variable {
+	name: "mapper_version",
+	values: [
+		"mapper4",
+		"mapper5",
+	],
+}
+
+libvendorgraphicbuffer_cc_defaults {
+	name: "libvendorgraphicbuffer_src_defaults",
+	soong_config_variables: {
+		mapper_version: {
+			mapper5: {
+				srcs: ["gralloc5/vendor_graphicbuffer_meta.cpp"],
+				shared_libs: [
+					"libvndksupport",
+				],
+				header_libs: [
+					"libimapper_providerutils",
+					"libimapper_stablec",
+				],
+			},
+			mapper4: {
+				srcs: ["gralloc4/vendor_graphicbuffer_meta.cpp"],
+			},
+			conditions_default: {
+				srcs: ["gralloc4/vendor_graphicbuffer_meta.cpp"],
+			},
+		},
+	},
+}
+
 cc_library_shared {
 	name: "libvendorgraphicbuffer",
-	srcs: [
-		"gralloc4/vendor_graphicbuffer_meta.cpp",
+	defaults: [
+		"libvendorgraphicbuffer_src_defaults",
 	],
 	shared_libs: [
 		"libdrm",