Snap for 5598178 from 1614f9726443bd11e0575aa0b9effeee7a9ec3f0 to emu-29.0-release

Change-Id: Ie91ae625be1ebee218aff86d86610dfcd8e26725
diff --git a/BUILD.gn b/BUILD.gn
index ccb8fe2..e240ad6 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -97,7 +97,7 @@
 
     include_dirs += [
       "fuchsia/include",
-      "../../../external/qemu/android/android-emugl/host/include"
+      "//third_party/vulkan_loader_and_validation_layers/include"
     ]
 
     libs = [
diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index ee4bd9a..bff33f3 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -32,7 +32,6 @@
     FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
     FRAMEWORK_FORMAT_YV12 = 1,
     FRAMEWORK_FORMAT_YUV_420_888 = 2,              // (Y+)(U+)(V+)
-    FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED = 3,  // (Y+)(UV)+
 };
 
 //
diff --git a/system/egl/egl.cpp b/system/egl/egl.cpp
index b0b510b..60d17e5 100644
--- a/system/egl/egl.cpp
+++ b/system/egl/egl.cpp
@@ -920,7 +920,11 @@
 
 const char* eglQueryString(EGLDisplay dpy, EGLint name)
 {
-    VALIDATE_DISPLAY_INIT(dpy, NULL);
+    // EGL_BAD_DISPLAY is generated if display is not an EGL display connection, unless display is
+    // EGL_NO_DISPLAY and name is EGL_EXTENSIONS.
+    if (dpy || name != EGL_EXTENSIONS) {
+        VALIDATE_DISPLAY_INIT(dpy, NULL);
+    }
 
     return s_display.queryString(name);
 }
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index d39d99a..3d08158 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -114,6 +114,10 @@
     typedef std::map<void*, uint32_t> MemRegionMap;  // base -> refCount
     typedef MemRegionMap::const_iterator mem_region_handle_t;
 
+    gralloc_memregions_t() {
+        pthread_mutex_init(&lock, NULL);
+    }
+
     MemRegionMap ashmemRegions;
     pthread_mutex_t lock;
 };
@@ -139,10 +143,9 @@
 static gralloc_dmaregion_t* s_grdma = NULL;
 
 static gralloc_memregions_t* init_gralloc_memregions() {
-    if (s_memregions) return s_memregions;
-
-    s_memregions = new gralloc_memregions_t;
-    pthread_mutex_init(&s_memregions->lock, NULL);
+    if (!s_memregions) {
+        s_memregions = new gralloc_memregions_t;
+    }
     return s_memregions;
 }
 
@@ -479,6 +482,7 @@
     bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK));
     bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER);
     bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK));
+    const bool hw_texture = usage & GRALLOC_USAGE_HW_TEXTURE;
 #if PLATFORM_SDK_VERSION >= 17
     bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE);
     bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ);
@@ -541,6 +545,15 @@
             glFormat = GL_RGBA;
             glType = GL_UNSIGNED_BYTE;
             break;
+        case HAL_PIXEL_FORMAT_RGB_888:
+            if (hw_texture) {
+                return -EINVAL;  // we dont support RGB_888 for HW textures
+            } else {
+                bpp = 3;
+                glFormat = GL_RGB;
+                glType = GL_UNSIGNED_BYTE;
+                break;
+            }
         case HAL_PIXEL_FORMAT_RGB_565:
             bpp = 2;
             // Workaround: distinguish vs the RGB8/RGBA8
@@ -613,13 +626,7 @@
             // We are going to use RGB888 on the host
             glFormat = GL_RGB;
             glType = GL_UNSIGNED_BYTE;
-
-            if (usage & (GRALLOC_USAGE_HW_CAMERA_READ | GRALLOC_USAGE_HW_CAMERA_WRITE)) {
-                // EmulatedFakeCamera3.cpp assumes it is NV21
-                selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED;
-            } else {
-                selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
-            }
+            selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
             break;
         default:
             ALOGE("gralloc_alloc: Unknown format %d", format);
@@ -1195,7 +1202,9 @@
             return -EBUSY;
         }
 
-        if (sw_read) {
+        // camera delivers bits to the buffer directly and does not require
+        // an explicit read, it also writes in YUV_420 (interleaved)
+        if (sw_read & !(usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
             void* rgb_addr = cpu_addr;
             char* tmpBuf = 0;
             if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
@@ -1319,8 +1328,10 @@
         return -EINVAL;
     }
 
+    usage |= (cb->usage & GRALLOC_USAGE_HW_CAMERA_MASK);
+
     void *vaddr;
-    int ret = gralloc_lock(module, handle, usage | GRALLOC_USAGE_SW_WRITE_MASK, l, t, w, h, &vaddr);
+    int ret = gralloc_lock(module, handle, usage, l, t, w, h, &vaddr);
     if (ret) {
         return ret;
     }
@@ -1357,7 +1368,7 @@
             cStep = 1;
             break;
         case HAL_PIXEL_FORMAT_YCbCr_420_888:
-            if (cb->emuFrameworkFormat == FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED) {
+            if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
                 yStride = cb->width;
                 cStride = cb->width;
                 yOffset = 0;
diff --git a/system/vulkan/goldfish_vulkan.cpp b/system/vulkan/goldfish_vulkan.cpp
index bf170e1..f7d288c 100644
--- a/system/vulkan/goldfish_vulkan.cpp
+++ b/system/vulkan/goldfish_vulkan.cpp
@@ -17,6 +17,9 @@
 
 #include <errno.h>
 #include <string.h>
+#ifdef VK_USE_PLATFORM_FUCHSIA
+#include <unistd.h>
+#endif
 
 #include "HostConnection.h"
 #include "ResourceTracker.h"
@@ -568,7 +571,7 @@
 
 class VulkanDevice {
 public:
-    VulkanDevice() {
+    VulkanDevice() : mHostSupportsGoldfish(access(QEMU_PIPE_PATH, F_OK) != -1) {
         goldfish_vk::ResourceTracker::get();
     }
 
@@ -578,8 +581,14 @@
     }
 
     PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* name) {
+        if (!mHostSupportsGoldfish) {
+            return vkstubhal::GetInstanceProcAddr(instance, name);
+        }
         return ::GetInstanceProcAddr(instance, name);
     }
+
+private:
+    const bool mHostSupportsGoldfish;
 };
 
 extern "C" __attribute__((visibility("default"))) PFN_vkVoidFunction
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 98f5527..7dd8834 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -554,7 +554,10 @@
                 abort();
             }
             mControlDevice.Bind(std::move(channel));
-            status = mControlDevice->ConnectSysmem(mSysmemAllocator.NewRequest().TakeChannel());
+
+            status = fdio_service_connect(
+                "/svc/fuchsia.sysmem.Allocator",
+                mSysmemAllocator.NewRequest().TakeChannel().release());
             if (status != ZX_OK) {
                 ALOGE("failed to get sysmem connection, status %d", status);
                 abort();
@@ -1303,10 +1306,12 @@
         buffer_constraints.max_size_bytes = 0xffffffff;
         buffer_constraints.physically_contiguous_required = false;
         buffer_constraints.secure_required = false;
-        buffer_constraints.secure_permitted = false;
         buffer_constraints.ram_domain_supported = false;
         buffer_constraints.cpu_domain_supported = false;
-        buffer_constraints.gpu_domain_supported = true;
+        buffer_constraints.inaccessible_domain_supported = true;
+        buffer_constraints.heap_permitted_count = 1;
+        buffer_constraints.heap_permitted[0] =
+            fuchsia::sysmem::HeapType::GoldfishDeviceLocal;
         constraints.image_format_constraints_count = 1;
         fuchsia::sysmem::ImageFormatConstraints& image_constraints =
             constraints.image_format_constraints[0];
@@ -1702,9 +1707,7 @@
         }
 
 #ifdef VK_USE_PLATFORM_FUCHSIA
-        if (vmo_handle == ZX_HANDLE_INVALID &&
-            !isHostVisibleMemoryTypeIndexForGuest(
-                &mHostVisibleMemoryVirtInfo, finalAllocInfo.memoryTypeIndex)) {
+        if (exportVmo) {
             bool hasDedicatedImage = dedicatedAllocInfoPtr &&
                 (dedicatedAllocInfoPtr->image != VK_NULL_HANDLE);
             VkImageCreateInfo imageCreateInfo = {};
@@ -1719,7 +1722,10 @@
                 imageCreateInfo = imageInfo.createInfo;
             }
 
-            if (imageCreateInfo.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
+            if (imageCreateInfo.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
+                                         VK_IMAGE_USAGE_TRANSFER_DST_BIT |
+                                         VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
+                                         VK_IMAGE_USAGE_SAMPLED_BIT)) {
                 fuchsia::sysmem::BufferCollectionTokenSyncPtr token;
                 zx_status_t status = mSysmemAllocator->AllocateSharedCollection(
                     token.NewRequest());
@@ -1763,6 +1769,8 @@
                     ALOGE("Failed to duplicate VMO: %d", status);
                     abort();
                 }
+                // TODO(reveman): Use imageCreateInfo.format to determine color
+                // buffer format.
                 status = mControlDevice->CreateColorBuffer(
                     std::move(vmo_copy),
                     imageCreateInfo.extent.width,
@@ -1795,8 +1803,6 @@
         }
 #endif
 
-        // TODO if (exportVmo) { }
-
         if (!isHostVisibleMemoryTypeIndexForGuest(
                 &mHostVisibleMemoryVirtInfo,
                 finalAllocInfo.memoryTypeIndex)) {
@@ -2203,6 +2209,9 @@
         }
 
         // Allow external memory for all color attachments on fuchsia.
+        // Note: This causes external to be set to true below. The result
+        // is that a dedicated memory allocation is required for the image,
+        // which allows the memory to be exported.
         if (localCreateInfo.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
             if (!extImgCiPtr) {
                 localExtImgCi.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;