Update Cuttlefish to Gralloc 4 on Minigbm

Bug: b/123764798
Bug: b/146515640
Test: launch_cvd
Test: launch_cvd --gpu_mode=drm_virgl
Change-Id: I786c23483deae356d1fd2e073b4e1bf6172f23aa
diff --git a/host/libs/vm_manager/crosvm_manager.cpp b/host/libs/vm_manager/crosvm_manager.cpp
index 86bc75b..7fecc34 100644
--- a/host/libs/vm_manager/crosvm_manager.cpp
+++ b/host/libs/vm_manager/crosvm_manager.cpp
@@ -72,7 +72,7 @@
   // HALs.
   if (gpu_mode == vsoc::kGpuModeGuestSwiftshader) {
     return {
-        "androidboot.hardware.gralloc=cutf_ashmem",
+        "androidboot.hardware.gralloc=minigbm",
         "androidboot.hardware.hwcomposer=cutf_hwc2",
         "androidboot.hardware.egl=swiftshader",
         "androidboot.hardware.vulkan=pastel",
@@ -133,8 +133,12 @@
 
   auto gpu_mode = config_->gpu_mode();
 
-  if (gpu_mode == vsoc::kGpuModeDrmVirgl ||
-      gpu_mode == vsoc::kGpuModeGfxStream) {
+  if (gpu_mode == vsoc::kGpuModeGuestSwiftshader) {
+    crosvm_cmd.AddParameter("--gpu=2D,",
+                            "width=", config_->x_res(), ",",
+                            "height=", config_->y_res());
+  } else if (gpu_mode == vsoc::kGpuModeDrmVirgl ||
+             gpu_mode == vsoc::kGpuModeGfxStream) {
     crosvm_cmd.AddParameter(gpu_mode == vsoc::kGpuModeGfxStream ?
                                 "--gpu=gfxstream," : "--gpu=",
                             "width=", config_->x_res(), ",",
diff --git a/host/libs/vm_manager/qemu_manager.cpp b/host/libs/vm_manager/qemu_manager.cpp
index 7841f0e..cfc073c 100644
--- a/host/libs/vm_manager/qemu_manager.cpp
+++ b/host/libs/vm_manager/qemu_manager.cpp
@@ -96,7 +96,7 @@
   // with properities lead to non-deterministic behavior while loading the
   // HALs.
   return {
-      "androidboot.hardware.gralloc=cutf_ashmem",
+      "androidboot.hardware.gralloc=minigbm",
       "androidboot.hardware.hwcomposer=cutf_cvm_ashmem",
       "androidboot.hardware.egl=swiftshader",
       "androidboot.hardware.vulkan=pastel",
@@ -199,6 +199,9 @@
   qemu_cmd.AddParameter("-device");
   qemu_cmd.AddParameter("virtio-balloon-pci,id=balloon0");
 
+  qemu_cmd.AddParameter("-device");
+  qemu_cmd.AddParameter("virtio-gpu-pci,id=gpu0");
+
   qemu_cmd.AddParameter("-object");
   qemu_cmd.AddParameter("rng-random,id=objrng0,filename=/dev/urandom");
 
diff --git a/shared/config/CleanSpec.mk b/shared/config/CleanSpec.mk
index 8c33800..a74a710 100644
--- a/shared/config/CleanSpec.mk
+++ b/shared/config/CleanSpec.mk
@@ -48,3 +48,5 @@
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.drm@1.2-service.widevine)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.gnss@2.0-service)
 $(call add-clean-step, find $(PRODUCT_OUT)/vendor/bin/hw/ -type f -name "android.hardware.drm@*" -print0 | xargs -0 rm -f)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/etc/init/android.hardware.graphics.allocator@4.0-service.minigbm.rc)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hw/android.hardware.graphics.allocator@4.0-service.minigbm)
diff --git a/shared/config/manifest.xml b/shared/config/manifest.xml
index e69cb8c..484952b 100644
--- a/shared/config/manifest.xml
+++ b/shared/config/manifest.xml
@@ -107,11 +107,10 @@
         </interface>
     </hal>
     -->
-    <!-- TODO (b/130074193): -->
     <hal format="hidl">
         <name>android.hardware.graphics.allocator</name>
         <transport>hwbinder</transport>
-        <version>2.0</version>
+        <version>4.0</version>
         <interface>
             <name>IAllocator</name>
             <instance>default</instance>
@@ -127,11 +126,10 @@
             <instance>default</instance>
         </interface>
     </hal>
-    <!-- TODO (b/130076969): -->
     <hal format="hidl">
         <name>android.hardware.graphics.mapper</name>
         <transport arch="32+64">passthrough</transport>
-        <version>2.1</version>
+        <version>4.0</version>
         <interface>
             <name>IMapper</name>
             <instance>default</instance>
diff --git a/shared/config/media_codecs_performance.xml b/shared/config/media_codecs_performance.xml
index 0880224..4c44a23 100644
--- a/shared/config/media_codecs_performance.xml
+++ b/shared/config/media_codecs_performance.xml
@@ -75,6 +75,58 @@
         </MediaCodec>
     </Encoders>
     <Decoders>
+        <MediaCodec name="c2.android.avc.decoder" type="video/avc" update="true">
+            <!-- 4 runs, min 196 max 297 gmean 265 -->
+            <Limit name="measured-frame-rate-320x240" range="196-297" />
+            <!-- 4 runs, min 138 max 158 gmean 151 -->
+            <Limit name="measured-frame-rate-720x480" range="138-158" />
+            <!-- 4 runs, min 60 max 65 gmean 62 -->
+            <Limit name="measured-frame-rate-1280x720" range="60-65" />
+            <!-- 4 runs, min 27 max 31 gmean 29 -->
+            <Limit name="measured-frame-rate-1920x1080" range="27-31" />
+        </MediaCodec>
+        <MediaCodec name="c2.android.h263.decoder" type="video/3gpp" update="true">
+            <!-- 4 runs, min 247 max 354 gmean 307 -->
+            <Limit name="measured-frame-rate-176x144" range="247-354" />
+            <!-- 4 runs, min 230 max 333 gmean 277 -->
+            <Limit name="measured-frame-rate-352x288" range="230-333" />
+        </MediaCodec>
+        <MediaCodec name="c2.android.hevc.decoder" type="video/hevc" update="true">
+            <!-- 4 runs, min 234 max 315 gmean 276 -->
+            <Limit name="measured-frame-rate-352x288" range="234-315" />
+            <!-- 4 runs, min 215 max 267 gmean 242 -->
+            <Limit name="measured-frame-rate-640x360" range="215-267" />
+            <!-- 4 runs, min 209 max 249 gmean 236 -->
+            <Limit name="measured-frame-rate-720x480" range="209-249" />
+            <!-- 4 runs, min 125 max 146 gmean 139 -->
+            <Limit name="measured-frame-rate-1280x720" range="125-146" />
+            <!-- 4 runs, min 80 max 89 gmean 84 -->
+            <Limit name="measured-frame-rate-1920x1080" range="80-89" />
+        </MediaCodec>
+        <MediaCodec name="c2.android.mpeg4.decoder" type="video/mp4v-es" update="true">
+            <!-- 4 runs, min 285 max 404 gmean 344 -->
+            <Limit name="measured-frame-rate-176x144" range="285-404" />
+        </MediaCodec>
+        <MediaCodec name="c2.android.vp8.decoder" type="video/x-vnd.on2.vp8" update="true">
+            <!-- 4 runs, min 253 max 340 gmean 288 -->
+            <Limit name="measured-frame-rate-320x180" range="253-340" />
+            <!-- 4 runs, min 183 max 243 gmean 221 -->
+            <Limit name="measured-frame-rate-640x360" range="183-243" />
+            <!-- 4 runs, min 107 max 113 gmean 110 -->
+            <Limit name="measured-frame-rate-1280x720" range="107-113" />
+            <!-- 4 runs, min 50 max 59 gmean 53 -->
+            <Limit name="measured-frame-rate-1920x1080" range="50-59" />
+        </MediaCodec>
+        <MediaCodec name="c2.android.vp9.decoder" type="video/x-vnd.on2.vp9" update="true">
+            <!-- 4 runs, min 264 max 311 gmean 283 -->
+            <Limit name="measured-frame-rate-320x180" range="264-311" />
+            <!-- 4 runs, min 189 max 255 gmean 224 -->
+            <Limit name="measured-frame-rate-640x360" range="189-255" />
+            <!-- 4 runs, min 127 max 138 gmean 224 -->
+            <Limit name="measured-frame-rate-1280x720" range="127-138" />
+            <!-- 4 runs, min 65 max 88 gmean 77 -->
+            <Limit name="measured-frame-rate-1920x1080" range="65-88" />
+        </MediaCodec>
         <MediaCodec name="OMX.google.h263.decoder" type="video/3gpp" update="true">
             <!-- 3 runs, min 1246 max 1390 gmean 1342 -->
             <Limit name="measured-frame-rate-176x144" range="1246-1390" />
diff --git a/shared/device.mk b/shared/device.mk
index ae27e3a..a14610e 100644
--- a/shared/device.mk
+++ b/shared/device.mk
@@ -268,11 +268,8 @@
 # Gralloc HAL
 #
 PRODUCT_PACKAGES += \
-    gralloc.minigbm \
-    gralloc.cutf_ashmem \
-    android.hardware.graphics.mapper@2.0-impl-2.1 \
-    android.hardware.graphics.allocator@2.0-impl \
-    android.hardware.graphics.allocator@2.0-service
+    android.hardware.graphics.allocator@4.0-service.minigbm \
+    android.hardware.graphics.mapper@4.0-impl.minigbm
 
 #
 # Bluetooth HAL and Compatibility Bluetooth library (for older revs).
diff --git a/shared/sepolicy/vendor/file_contexts b/shared/sepolicy/vendor/file_contexts
index 87f3b28..13ab517 100644
--- a/shared/sepolicy/vendor/file_contexts
+++ b/shared/sepolicy/vendor/file_contexts
@@ -71,6 +71,7 @@
 /vendor/bin/hw/android\.hardware\.drm@[0-9]+\.[0-9]+-service-lazy\.clearkey  u:object_r:hal_drm_clearkey_exec:s0
 /vendor/bin/hw/android\.hardware\.drm@[0-9]+\.[0-9]+-service\.widevine  u:object_r:hal_drm_widevine_exec:s0
 /vendor/bin/hw/android\.hardware\.drm@[0-9]+\.[0-9]+-service-lazy\.widevine  u:object_r:hal_drm_widevine_exec:s0
+/vendor/bin/hw/android\.hardware\.graphics\.allocator@4\.0-service\.minigbm   u:object_r:hal_graphics_allocator_default_exec:s0
 /vendor/bin/hw/android\.hardware\.gatekeeper@1\.0-service\.software  u:object_r:hal_gatekeeper_default_exec:s0
 /vendor/bin/hw/android\.hardware\.health\.storage@1\.0-service\.cuttlefish u:object_r:hal_health_storage_default_exec:s0
 /vendor/bin/hw/android\.hardware\.lights-service\.example u:object_r:hal_light_default_exec:s0
@@ -88,10 +89,8 @@
 /vendor/lib(64)?/libdrm.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/libglapi.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/dri/.* u:object_r:same_process_hal_file:s0
-/vendor/lib(64)?/hw/android\.hardware\.graphics\.mapper@2\.0-impl-2\.1\.so u:object_r:same_process_hal_file:s0
+/vendor/lib(64)?/hw/android\.hardware\.graphics\.mapper@4\.0-impl\.minigbm\.so u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/android\.hardware\.health@2\.0-impl-2\.1-cuttlefish\.so  u:object_r:same_process_hal_file:s0
-/vendor/lib(64)?/hw/gralloc.cutf_ashmem.so  u:object_r:same_process_hal_file:s0
-/vendor/lib(64)?/hw/gralloc.minigbm.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/hw/vulkan.pastel.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/libcuttlefish_fs.so  u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/vsoc_lib.so  u:object_r:same_process_hal_file:s0
diff --git a/tests/hal/hal_implementation_test.cpp b/tests/hal/hal_implementation_test.cpp
index 2c33483..8c2ae50 100644
--- a/tests/hal/hal_implementation_test.cpp
+++ b/tests/hal/hal_implementation_test.cpp
@@ -53,14 +53,14 @@
     "android.hardware.fastboot@1.0",
     "android.hardware.gnss.measurement_corrections@1.1", // is sub-interface of gnss
     "android.hardware.gnss.visibility_control@1.0",
+    "android.hardware.graphics.allocator@2.0",
     "android.hardware.graphics.allocator@3.0",
-    "android.hardware.graphics.allocator@4.0",
     "android.hardware.graphics.bufferqueue@1.0",
     "android.hardware.graphics.bufferqueue@2.0",
     "android.hardware.graphics.composer@2.3",
     "android.hardware.graphics.composer@2.4",
+    "android.hardware.graphics.mapper@2.1",
     "android.hardware.graphics.mapper@3.0",
-    "android.hardware.graphics.mapper@4.0",
     "android.hardware.health@1.0",
     "android.hardware.ir@1.0",
     "android.hardware.keymaster@3.0",