Snap for 10360508 from 63dd02e53bee711a72f741b46adae887276c8827 to udc-release

Change-Id: I11bbbc9a3af01140c007e36b1244fd6c589f3b5d
diff --git a/system/codecs/c2/decoders/avcdec/Android.bp b/system/codecs/c2/decoders/avcdec/Android.bp
index 5d840b0..672a80f 100644
--- a/system/codecs/c2/decoders/avcdec/Android.bp
+++ b/system/codecs/c2/decoders/avcdec/Android.bp
@@ -27,6 +27,7 @@
 
    header_libs: [
     "libgralloc_cb.ranchu",
+    "libgralloc_cb3.ranchu",
     ],
 
    static_libs: ["libavcdec",
diff --git a/system/codecs/c2/decoders/avcdec/C2GoldfishAvcDec.cpp b/system/codecs/c2/decoders/avcdec/C2GoldfishAvcDec.cpp
index 74d971f..4a89613a 100644
--- a/system/codecs/c2/decoders/avcdec/C2GoldfishAvcDec.cpp
+++ b/system/codecs/c2/decoders/avcdec/C2GoldfishAvcDec.cpp
@@ -748,8 +748,15 @@
     const bool isGraphic = (pool->getAllocatorId() & C2Allocator::GRAPHIC);
     DDD("buffer pool allocator id %x",  (int)(pool->getAllocatorId()));
     if (isGraphic) {
-        DDD("decoding to host color buffer");
-        mEnableAndroidNativeBuffers = true;
+        uint64_t client_usage = getClientUsage(pool);
+        DDD("client has usage as 0x%llx", client_usage);
+        if (client_usage & BufferUsage::CPU_READ_MASK) {
+            DDD("decoding to guest byte buffer as client has read usage");
+            mEnableAndroidNativeBuffers = false;
+        } else {
+            DDD("decoding to host color buffer");
+            mEnableAndroidNativeBuffers = true;
+        }
     } else {
         DDD("decoding to guest byte buffer");
         mEnableAndroidNativeBuffers = false;
diff --git a/system/codecs/c2/decoders/base/Android.bp b/system/codecs/c2/decoders/base/Android.bp
index 348334b..8dc1596 100644
--- a/system/codecs/c2/decoders/base/Android.bp
+++ b/system/codecs/c2/decoders/base/Android.bp
@@ -47,6 +47,7 @@
 
     header_libs: [
         "libgralloc_cb.ranchu",
+        "libgralloc_cb3.ranchu",
     ],
 
     sanitize: {
diff --git a/system/codecs/c2/decoders/base/color_buffer_utils.cpp b/system/codecs/c2/decoders/base/color_buffer_utils.cpp
index a36ba32..99563f4 100644
--- a/system/codecs/c2/decoders/base/color_buffer_utils.cpp
+++ b/system/codecs/c2/decoders/base/color_buffer_utils.cpp
@@ -19,8 +19,11 @@
 #include <android-base/strings.h>
 #include <log/log.h>
 #include <gralloc_cb_bp.h>
+#include <cb_handle_30.h>
 #include <xf86drm.h>
 
+#include <C2AllocatorGralloc.h>
+
 #include "cros_gralloc_handle.h"
 #include "virtgpu_drm.h"
 
@@ -114,3 +117,17 @@
 uint32_t getColorBufferHandle(native_handle_t const* handle) {
     return getGlobals()->getColorBufferHandle(handle);
 }
+
+uint64_t getClientUsage(const std::shared_ptr<C2BlockPool> &pool) {
+      std::shared_ptr<C2GraphicBlock> myOutBlock;
+      const C2MemoryUsage usage = {0, 0};
+      const uint32_t format = HAL_PIXEL_FORMAT_YCBCR_420_888;
+      pool->fetchGraphicBlock(2, 2, format, usage, &myOutBlock);
+      auto myc2Handle = myOutBlock->handle();
+      native_handle_t *mygrallocHandle =
+      android::UnwrapNativeCodec2GrallocHandle(myc2Handle);
+      cb_handle_30_t* mycb = (cb_handle_30_t*)(mygrallocHandle);
+      ALOGV("%s %s %d: client usage 0x%x", __FILE__, __func__, __LINE__, mycb->usage);
+      return mycb->usage;
+}
+
diff --git a/system/codecs/c2/decoders/base/include/color_buffer_utils.h b/system/codecs/c2/decoders/base/include/color_buffer_utils.h
index d0a7876..ebb8527 100644
--- a/system/codecs/c2/decoders/base/include/color_buffer_utils.h
+++ b/system/codecs/c2/decoders/base/include/color_buffer_utils.h
@@ -14,5 +14,7 @@
  * limitations under the License.
  */
 #include <cutils/native_handle.h>
+#include <SimpleC2Interface.h>
 
 uint32_t getColorBufferHandle(native_handle_t const* handle);
+uint64_t getClientUsage(const std::shared_ptr<C2BlockPool> &pool);
diff --git a/system/codecs/c2/decoders/hevcdec/C2GoldfishHevcDec.cpp b/system/codecs/c2/decoders/hevcdec/C2GoldfishHevcDec.cpp
index c60138b..14eed1f 100644
--- a/system/codecs/c2/decoders/hevcdec/C2GoldfishHevcDec.cpp
+++ b/system/codecs/c2/decoders/hevcdec/C2GoldfishHevcDec.cpp
@@ -696,8 +696,15 @@
     const bool isGraphic = (pool->getAllocatorId() & C2Allocator::GRAPHIC);
     DDD("buffer pool allocator id %x",  (int)(pool->getAllocatorId()));
     if (isGraphic) {
-        DDD("decoding to host color buffer");
-        mEnableAndroidNativeBuffers = true;
+        uint64_t client_usage = getClientUsage(pool);
+        DDD("client has usage as 0x%llx", client_usage);
+        if (client_usage & BufferUsage::CPU_READ_MASK) {
+            DDD("decoding to guest byte buffer as client has read usage");
+            mEnableAndroidNativeBuffers = false;
+        } else {
+            DDD("decoding to host color buffer");
+            mEnableAndroidNativeBuffers = true;
+        }
     } else {
         DDD("decoding to guest byte buffer");
         mEnableAndroidNativeBuffers = false;
diff --git a/system/codecs/c2/decoders/vpxdec/C2GoldfishVpxDec.cpp b/system/codecs/c2/decoders/vpxdec/C2GoldfishVpxDec.cpp
index 59030d4..1655138 100644
--- a/system/codecs/c2/decoders/vpxdec/C2GoldfishVpxDec.cpp
+++ b/system/codecs/c2/decoders/vpxdec/C2GoldfishVpxDec.cpp
@@ -499,8 +499,15 @@
     const bool isGraphic = (pool->getAllocatorId() & C2Allocator::GRAPHIC);
     DDD("buffer pool allocator id %x",  (int)(pool->getAllocatorId()));
     if (isGraphic) {
-        DDD("decoding to host color buffer");
-        mEnableAndroidNativeBuffers = true;
+        uint64_t client_usage = getClientUsage(pool);
+        DDD("client has usage as 0x%llx", client_usage);
+        if (client_usage & BufferUsage::CPU_READ_MASK) {
+            DDD("decoding to guest byte buffer as client has read usage");
+            mEnableAndroidNativeBuffers = false;
+        } else {
+            DDD("decoding to host color buffer");
+            mEnableAndroidNativeBuffers = true;
+        }
     } else {
         DDD("decoding to guest byte buffer");
         mEnableAndroidNativeBuffers = false;
diff --git a/system/hals/Android.bp b/system/hals/Android.bp
new file mode 100644
index 0000000..cec9efb
--- /dev/null
+++ b/system/hals/Android.bp
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2023 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 {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "device_generic_goldfish-opengl_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["device_generic_goldfish-opengl_license"],
+}
+
+cc_library_headers {
+    name: "libgralloc_cb3.ranchu",
+    vendor_available: true,
+    export_include_dirs: ["."],
+    header_libs: [
+        "libcutils_headers",
+        "libqemupipe-types.ranchu",
+    ],
+    export_header_lib_headers: [
+        "libcutils_headers",
+        "libqemupipe-types.ranchu"
+    ],
+}