Snap for 5611628 from bb99e446eee7e28327d8b28add0cc5bb08a465bd to sdk-release

Change-Id: Ibf658a46bffdd2806b7c55c141a66e37f1d6e0a2
diff --git a/fuchsia/releasepackage.py b/fuchsia/releasepackage.py
index 8f18d68..2d4df50 100644
--- a/fuchsia/releasepackage.py
+++ b/fuchsia/releasepackage.py
@@ -98,7 +98,7 @@
 git_rev = subprocess.check_output(
     ["git", "-C", git_repo_location, "rev-parse", "HEAD"]).strip()
 
-cipd_command = "%s create -in %s -name %s -ref latest -tag git_revision:%s" % (
+cipd_command = "%s create -in %s -name %s -ref latest -install-mode copy -tag git_revision:%s" % (
     cipd_path, package_dir, package_name, git_rev)
 print cipd_command
 if not args.dry_run:
@@ -108,4 +108,4 @@
   <package name="%s"
            version="git_revision:%s"
            path="prebuild/third_party/%s"/>
-""" % (package_name, git_rev, package_dir))[1:-1]
\ No newline at end of file
+""" % (package_name, git_rev, package_dir))[1:-1]
diff --git a/shared/OpenglCodecCommon/GLSharedGroup.h b/shared/OpenglCodecCommon/GLSharedGroup.h
index 8a35a89..9ef92ea 100755
--- a/shared/OpenglCodecCommon/GLSharedGroup.h
+++ b/shared/OpenglCodecCommon/GLSharedGroup.h
@@ -39,7 +39,6 @@
 #include <utils/threads.h>
 #include "FixedBuffer.h"
 #include "auto_goldfish_dma_context.h"
-#include "goldfish_address_space.h"
 #include "IndexRangeCache.h"
 #include "SmartPtr.h"
 
@@ -64,8 +63,6 @@
 
     // DMA support
     AutoGoldfishDmaContext dma_buffer;
-    // Direct memory access support
-    GoldfishAddressSpaceBlock shared_block;
 };
 
 class ProgramData {
diff --git a/shared/OpenglCodecCommon/goldfish_address_space.cpp b/shared/OpenglCodecCommon/goldfish_address_space.cpp
index da6831e..2502c13 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space.cpp
+++ b/shared/OpenglCodecCommon/goldfish_address_space.cpp
@@ -23,64 +23,61 @@
 
 #ifdef HOST_BUILD
 
-#include "android/base/SubAllocator.h"
-#include "android/base/memory/LazyInstance.h"
+#include "android/emulation/hostdevices/HostAddressSpace.h"
 
-// AddressSpaceHost is a global class for obtaining physical addresses
-// for the host-side build.
-class AddressSpaceHost {
-public:
-    AddressSpaceHost() : mAlloc(0, 16ULL * 1024ULL * 1048576ULL, 4096) { }
-    uint64_t alloc(size_t size) {
-        return (uint64_t)(uintptr_t)mAlloc.alloc(size);
-    }
-    void free(uint64_t addr) {
-        mAlloc.free((void*)(uintptr_t)addr);
-    }
-private:
-    android::base::SubAllocator mAlloc;
-};
+using android::HostAddressSpaceDevice;
 
-static android::base::LazyInstance<AddressSpaceHost> sAddressSpaceHost =
-    LAZY_INSTANCE_INIT;
-
-// It may seem like there should be one allocator per provider,
-// but to properly reflect the guest behavior where there is only one
-// allocator in the system and multiple providers are possible,
-// we need to have a separate global object (sAddressSpaceHost).
 GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider()
-    : mAlloc(sAddressSpaceHost.ptr()) {}
+    : m_handle(HostAddressSpaceDevice::get()->open()) { }
 
-GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider() { }
-
-uint64_t GoldfishAddressSpaceBlockProvider::allocPhys(size_t size) {
-    AddressSpaceHost* hostAlloc = reinterpret_cast<AddressSpaceHost*>(mAlloc);
-    return hostAlloc->alloc(size);
+GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider()
+{
+    HostAddressSpaceDevice::get()->close(m_handle);
 }
 
-void GoldfishAddressSpaceBlockProvider::freePhys(uint64_t phys) {
-    AddressSpaceHost* hostAlloc = reinterpret_cast<AddressSpaceHost*>(mAlloc);
-    return hostAlloc->free(phys);
-}
+GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
+    : m_mmaped_ptr(NULL)
+    , m_phys_addr(0)
+    , m_host_addr(0)
+    , m_offset(0)
+    , m_size(0)
+    , m_handle(0) {}
 
-GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock() :
-    m_alloced(false), m_guest_ptr(NULL), m_phys_addr(0), m_provider(NULL) {}
-GoldfishAddressSpaceBlock::~GoldfishAddressSpaceBlock() { destroy(); }
+GoldfishAddressSpaceBlock::~GoldfishAddressSpaceBlock()
+{
+    destroy();
+}
 
 GoldfishAddressSpaceBlock &GoldfishAddressSpaceBlock::operator=(const GoldfishAddressSpaceBlock &rhs)
 {
-    m_guest_ptr = rhs.m_guest_ptr;
+    m_mmaped_ptr = rhs.m_mmaped_ptr;
     m_phys_addr = rhs.m_phys_addr;
-    m_provider = rhs.m_provider;
+    m_host_addr = rhs.m_host_addr;
+    m_offset = rhs.m_offset;
+    m_size = rhs.m_size;
+    m_handle = rhs.m_handle;
+
     return *this;
 }
 
 bool GoldfishAddressSpaceBlock::allocate(GoldfishAddressSpaceBlockProvider *provider, size_t size)
 {
+
+    ALOGD("%s: Ask for block of size 0x%llx\n", __func__,
+         (unsigned long long)size);
+
     destroy();
-    m_phys_addr = provider->allocPhys(size);
-    m_alloced = true;
-    m_provider = provider;
+
+    if (!provider->is_opened()) {
+        return false;
+    }
+
+    m_size = size;
+    m_offset =
+        HostAddressSpaceDevice::get()->allocBlock(
+            provider->m_handle, size, &m_phys_addr);
+    m_handle = provider->m_handle;
+
     return true;
 }
 
@@ -91,40 +88,62 @@
 
 uint64_t GoldfishAddressSpaceBlock::hostAddr() const
 {
-    return 42;  // some random number, not used
+    return m_host_addr;
 }
 
-void *GoldfishAddressSpaceBlock::mmap(uint64_t opaque)
+void *GoldfishAddressSpaceBlock::mmap(uint64_t host_addr)
 {
-    m_guest_ptr = reinterpret_cast<void *>(opaque);
-    return m_guest_ptr;
+    if (m_size == 0) {
+        ALOGE("%s: called with zero size\n", __func__);
+        return NULL;
+    }
+
+    if (m_mmaped_ptr != nullptr) {
+        ALOGE("'mmap' called for an already mmaped address block 0x%llx %d", (unsigned long long)m_mmaped_ptr, nullptr == m_mmaped_ptr);
+        ::abort();
+    }
+
+    m_mmaped_ptr = (void*)(uintptr_t)(host_addr & (~(PAGE_SIZE - 1)));
+    m_host_addr = host_addr;
+
+    return guestPtr();
 }
 
 void *GoldfishAddressSpaceBlock::guestPtr() const
 {
-    return m_guest_ptr;
+    return reinterpret_cast<char *>(m_mmaped_ptr) + (m_host_addr & (PAGE_SIZE - 1));
 }
 
 void GoldfishAddressSpaceBlock::destroy()
 {
-    if (m_alloced) {
-        m_guest_ptr = NULL;
-        if (m_provider) {
-            m_provider->freePhys(m_phys_addr);
-        }
-        m_alloced = false;
+    if (m_mmaped_ptr && m_size) {
+        m_mmaped_ptr = NULL;
+    }
+
+    if (m_size) {
+        HostAddressSpaceDevice::get()->freeBlock(m_handle, m_offset);
+        m_phys_addr = 0;
+        m_host_addr = 0;
+        m_offset = 0;
+        m_size = 0;
     }
 }
 
 void GoldfishAddressSpaceBlock::replace(GoldfishAddressSpaceBlock *other)
 {
+    destroy();
+
     if (other) {
-        this->m_guest_ptr = other->m_guest_ptr;
-        other->m_guest_ptr = NULL;
-    } else {
-        this->m_guest_ptr = NULL;
+        *this = *other;
+        *other = GoldfishAddressSpaceBlock();
     }
 }
+
+bool GoldfishAddressSpaceBlockProvider::is_opened()
+{
+    return m_handle > 0;
+}
+
 #elif __Fuchsia__
 #include <fcntl.h>
 #include <lib/fdio/fdio.h>
diff --git a/shared/OpenglCodecCommon/goldfish_address_space.h b/shared/OpenglCodecCommon/goldfish_address_space.h
index 250c476..a5c0d77 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space.h
+++ b/shared/OpenglCodecCommon/goldfish_address_space.h
@@ -25,20 +25,15 @@
 class GoldfishAddressSpaceBlock;
 
 #ifdef HOST_BUILD
-class GoldfishAddressSpaceBlockProvider {
-public:
-    GoldfishAddressSpaceBlockProvider();
-    ~GoldfishAddressSpaceBlockProvider();
 
-    uint64_t allocPhys(size_t size);
-    void freePhys(uint64_t phys);
+namespace android {
 
-private:
-   void* mAlloc;
+class HostAddressSpaceDevice;
 
-   friend class GoldfishAddressSpaceBlock;
-};
-#else
+} // namespace android
+
+#endif
+
 class GoldfishAddressSpaceBlockProvider {
 public:
     GoldfishAddressSpaceBlockProvider();
@@ -49,15 +44,18 @@
    GoldfishAddressSpaceBlockProvider &operator=(const GoldfishAddressSpaceBlockProvider &rhs);
 
    bool is_opened();
+#ifdef HOST_BUILD
+   uint32_t m_handle;
+#else // HOST_BUILD
 #ifdef __Fuchsia__
    fuchsia::hardware::goldfish::address::space::DeviceSyncPtr m_device;
-#else
+#else // __Fuchsia__
    int m_fd;
-#endif
+#endif // !__Fuchsia__
+#endif // !HOST_BUILD
 
    friend class GoldfishAddressSpaceBlock;
 };
-#endif
 
 class GoldfishAddressSpaceBlock {
 public:
@@ -76,23 +74,25 @@
     GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &);
 
 #ifdef HOST_BUILD
-    bool        m_alloced;
-    void     *m_guest_ptr;
-    uint64_t  m_phys_addr;
-    GoldfishAddressSpaceBlockProvider* m_provider;
-#else
+    uint32_t m_handle;
+#else // HOST_BUILD
+
 #ifdef __Fuchsia__
     fuchsia::hardware::goldfish::address::space::DeviceSyncPtr* m_device;
     uint32_t  m_vmo;
-#else
+
+#else // __Fuchsia__
+
     int       m_fd;
-#endif
+
+#endif // !__Fuchsia__
+#endif // !HOST_BUILD
+
     void     *m_mmaped_ptr;
     uint64_t  m_phys_addr;
     uint64_t  m_host_addr;
     uint64_t  m_offset;
     size_t    m_size;
-#endif
 };
 
 #endif
diff --git a/shared/OpenglCodecCommon/qemu_pipe_host.cpp b/shared/OpenglCodecCommon/qemu_pipe_host.cpp
index e1a3b45..1249eb1 100644
--- a/shared/OpenglCodecCommon/qemu_pipe_host.cpp
+++ b/shared/OpenglCodecCommon/qemu_pipe_host.cpp
@@ -13,7 +13,7 @@
 // limitations under the License.
 #include "qemu_pipe.h"
 
-#include "android/emulation/hostpipe/HostGoldfishPipe.h"
+#include "android/emulation/hostdevices/HostGoldfishPipe.h"
 
 #if PLATFORM_SDK_VERSION < 26
 #include <cutils/log.h>
diff --git a/system/GLESv2_enc/GL2Encoder.cpp b/system/GLESv2_enc/GL2Encoder.cpp
index d8ea74e..44264d9 100755
--- a/system/GLESv2_enc/GL2Encoder.cpp
+++ b/system/GLESv2_enc/GL2Encoder.cpp
@@ -3041,42 +3041,6 @@
                 buf->m_guest_paddr);
 
         return reinterpret_cast<void*>(buf->dma_buffer.get().mapped_addr);
-    } else if (ctx->hasExtension("ANDROID_EMU_direct_mem_v2")) {
-        GoldfishAddressSpaceBlock new_shared_block;
-
-        if (new_shared_block.allocate(&ctx->m_goldfish_address_block_provider, length)) {
-            uint64_t gpu_addr =
-                ctx->glMapBufferRangeDirect(ctx,
-                                            target,
-                                            offset,
-                                            length,
-                                            access,
-                                            new_shared_block.physAddr());
-            if (gpu_addr) {
-                void *user_ptr = new_shared_block.mmap(gpu_addr);
-                if (user_ptr) {
-                    buf->shared_block.replace(&new_shared_block);
-                    return user_ptr;
-                } else {
-                    GLboolean host_res = GL_TRUE;
-
-                    ctx->glUnmapBufferDirect(
-                        ctx, target,
-                        offset,
-                        length,
-                        access,
-                        new_shared_block.physAddr(),
-                        gpu_addr,
-                        &host_res);
-
-                    return s_glMapBufferRangeAEMUImpl(ctx, target, offset, length, access, buf);
-                }
-            } else {
-                return s_glMapBufferRangeAEMUImpl(ctx, target, offset, length, access, buf);
-            }
-        } else {
-            return s_glMapBufferRangeAEMUImpl(ctx, target, offset, length, access, buf);
-        }
     } else {
         return s_glMapBufferRangeAEMUImpl(ctx, target, offset, length, access, buf);
     }
@@ -3119,23 +3083,6 @@
             buf->m_mappedAccess,
             goldfish_dma_guest_paddr(&buf->dma_buffer.get()),
             &host_res);
-    } else if (buf->shared_block.guestPtr()) {
-        GoldfishAddressSpaceBlock *shared_block = &buf->shared_block;
-
-        memcpy(static_cast<char*>(buf->m_fixedBuffer.ptr()) + buf->m_mappedOffset,
-               shared_block->guestPtr(),
-               buf->m_mappedLength);
-
-        ctx->glUnmapBufferDirect(
-                ctx, target,
-                buf->m_mappedOffset,
-                buf->m_mappedLength,
-                buf->m_mappedAccess,
-                shared_block->physAddr(),
-                shared_block->hostAddr(),
-                &host_res);
-
-        shared_block->replace(NULL);
     } else {
         ctx->glUnmapBufferAEMU(
                 ctx, target,
@@ -3176,20 +3123,12 @@
 
     buf->m_indexRangeCache.invalidateRange(totalOffset, length);
 
-    if (buf->shared_block.guestPtr()) {
-        ctx->glFlushMappedBufferRangeDirect(
-                ctx, target,
-                offset,
-                length,
-                buf->m_mappedAccess);
-    } else {
-        ctx->glFlushMappedBufferRangeAEMU(
-                ctx, target,
-                totalOffset,
-                length,
-                buf->m_mappedAccess,
-                (void*)((char*)buf->m_fixedBuffer.ptr() + totalOffset));
-    }
+    ctx->glFlushMappedBufferRangeAEMU(
+            ctx, target,
+            totalOffset,
+            length,
+            buf->m_mappedAccess,
+            (void*)((char*)buf->m_fixedBuffer.ptr() + totalOffset));
 }
 
 void GL2Encoder::s_glCompressedTexImage2D(void* self, GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid* data) {
diff --git a/system/GLESv2_enc/GL2Encoder.h b/system/GLESv2_enc/GL2Encoder.h
index fc444c5..c617a36 100644
--- a/system/GLESv2_enc/GL2Encoder.h
+++ b/system/GLESv2_enc/GL2Encoder.h
@@ -104,8 +104,6 @@
     GLSharedGroupPtr m_shared;
     GLenum  m_error;
 
-    GoldfishAddressSpaceBlockProvider m_goldfish_address_block_provider;
-
     GLint *m_compressedTextureFormats;
     GLint m_num_compressedTextureFormats;
     GLint *getCompressedTextureFormats();
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/vulkan/goldfish_vulkan.cpp b/system/vulkan/goldfish_vulkan.cpp
index f7d288c..c694ff8 100644
--- a/system/vulkan/goldfish_vulkan.cpp
+++ b/system/vulkan/goldfish_vulkan.cpp
@@ -106,6 +106,37 @@
     return VK_SUCCESS;
 }
 
+VkResult
+CreateDebugReportCallbackEXT(VkInstance /*instance*/,
+                             const VkDebugReportCallbackCreateInfoEXT* /*pCreateInfo*/,
+                             const VkAllocationCallbacks* /*pAllocator*/,
+                             VkDebugReportCallbackEXT* /*pCallback*/)
+{
+    AEMU_SCOPED_TRACE("vkstubhal::CreateDebugReportCallbackEXT");
+    return VK_SUCCESS;
+}
+
+void
+DestroyDebugReportCallbackEXT(VkInstance /*instance*/,
+                              VkDebugReportCallbackEXT /*callback*/,
+                              const VkAllocationCallbacks* /*pAllocator*/)
+{
+    AEMU_SCOPED_TRACE("vkstubhal::DestroyDebugReportCallbackEXT");
+}
+
+void
+DebugReportMessageEXT(VkInstance /*instance*/,
+                      VkDebugReportFlagsEXT /*flags*/,
+                      VkDebugReportObjectTypeEXT /*objectType*/,
+                      uint64_t /*object*/,
+                      size_t /*location*/,
+                      int32_t /*messageCode*/,
+                      const char* /*pLayerPrefix*/,
+                      const char* /*pMessage*/)
+{
+    AEMU_SCOPED_TRACE("vkstubhal::DebugReportMessageEXT");
+}
+
 #ifdef VK_USE_PLATFORM_FUCHSIA
 VkResult
 GetMemoryZirconHandleFUCHSIA(VkDevice /*device*/,
@@ -189,8 +220,17 @@
     if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
         return reinterpret_cast<PFN_vkVoidFunction>(
             EnumeratePhysicalDeviceGroups);
+    if (strcmp(name, "vkEnumeratePhysicalDeviceGroupsKHR") == 0)
+        return reinterpret_cast<PFN_vkVoidFunction>(
+            EnumeratePhysicalDeviceGroups);
     if (strcmp(name, "vkGetInstanceProcAddr") == 0)
         return reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr);
+    if (strcmp(name, "vkCreateDebugReportCallbackEXT") == 0)
+        return reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT);
+    if (strcmp(name, "vkDestroyDebugReportCallbackEXT") == 0)
+        return reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT);
+    if (strcmp(name, "vkDebugReportMessageEXT") == 0)
+        return reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT);
 #ifdef VK_USE_PLATFORM_FUCHSIA
     if (strcmp(name, "vkGetMemoryZirconHandleFUCHSIA") == 0)
         return reinterpret_cast<PFN_vkVoidFunction>(GetMemoryZirconHandleFUCHSIA);
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index a81022e..f88abde 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -1308,8 +1308,10 @@
         buffer_constraints.secure_required = false;
         buffer_constraints.ram_domain_supported = false;
         buffer_constraints.cpu_domain_supported = false;
+        buffer_constraints.inaccessible_domain_supported = true;
         buffer_constraints.heap_permitted_count = 1;
-        buffer_constraints.heap_permitted[0] = fuchsia::sysmem::HeapType::Goldfish;
+        buffer_constraints.heap_permitted[0] =
+            fuchsia::sysmem::HeapType::GOLDFISH_DEVICE_LOCAL;
         constraints.image_format_constraints_count = 1;
         fuchsia::sysmem::ImageFormatConstraints& image_constraints =
             constraints.image_format_constraints[0];
@@ -1705,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 = {};
@@ -1722,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());
@@ -1766,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,
@@ -1798,8 +1803,6 @@
         }
 #endif
 
-        // TODO if (exportVmo) { }
-
         if (!isHostVisibleMemoryTypeIndexForGuest(
                 &mHostVisibleMemoryVirtInfo,
                 finalAllocInfo.memoryTypeIndex)) {
@@ -2206,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;