Snap for 5934975 from 754c3eb64199b70fca0465b5d214a4f0ab1be6f5 to sdk-release

Change-Id: I4a8d1cd34bdac893707650830300f6fbcbf4ff0f
diff --git a/shared/OpenglCodecCommon/goldfish_address_space.h b/shared/OpenglCodecCommon/goldfish_address_space.h
index f527152..6b2576d 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space.h
+++ b/shared/OpenglCodecCommon/goldfish_address_space.h
@@ -35,9 +35,23 @@
 
 #endif
 
+#if defined(__Fuchsia__)
+    typedef void* address_space_handle_t;
+#elif defined(HOST_BUILD)
+    typedef uint32_t address_space_handle_t;
+#else
+    typedef int address_space_handle_t;
+#endif
+
 class GoldfishAddressSpaceBlockProvider {
 public:
+#ifdef __Fuchsia__
     GoldfishAddressSpaceBlockProvider();
+#else
+    static const uint64_t SUBDEVICE_TYPE_NO_SUBDEVICE_ID = -1;
+    static const uint64_t SUBDEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID = 5;
+    GoldfishAddressSpaceBlockProvider(uint64_t subdevice);
+#endif  // __Fuchsia__
     ~GoldfishAddressSpaceBlockProvider();
 
 private:
@@ -46,15 +60,13 @@
 
     bool is_opened() const;
     void close();
-#ifdef HOST_BUILD
-    uint32_t m_handle;
-#else // HOST_BUILD
+    address_space_handle_t release();
+
 #ifdef __Fuchsia__
     fuchsia::hardware::goldfish::address::space::DeviceSyncPtr m_device;
 #else // __Fuchsia__
-    int m_fd;
+    address_space_handle_t m_handle;
 #endif // !__Fuchsia__
-#endif // !HOST_BUILD
 
     friend class GoldfishAddressSpaceBlock;
     friend class GoldfishAddressSpaceHostMemoryAllocator;
@@ -73,25 +85,18 @@
     void *mmap(uint64_t opaque);
     void *guestPtr() const;
     void replace(GoldfishAddressSpaceBlock *other);
+    void release();
 
 private:
     void destroy();
     GoldfishAddressSpaceBlock &operator=(const GoldfishAddressSpaceBlock &);
 
-#ifdef HOST_BUILD
-    uint32_t m_handle;
-#else // HOST_BUILD
-
 #ifdef __Fuchsia__
     fuchsia::hardware::goldfish::address::space::DeviceSyncPtr* m_device;
     uint32_t  m_vmo;
-
 #else // __Fuchsia__
-
-    int       m_fd;
-
+    address_space_handle_t m_handle;
 #endif // !__Fuchsia__
-#endif // !HOST_BUILD
 
     void     *m_mmaped_ptr;
     uint64_t  m_phys_addr;
@@ -107,6 +112,9 @@
     long hostMalloc(GoldfishAddressSpaceBlock *block, size_t size);
     void hostFree(GoldfishAddressSpaceBlock *block);
 
+    bool is_opened() const;
+    address_space_handle_t release() { return m_provider.release(); }
+
 private:
     GoldfishAddressSpaceBlockProvider m_provider;
 };
diff --git a/shared/OpenglCodecCommon/goldfish_address_space_android.impl b/shared/OpenglCodecCommon/goldfish_address_space_android.impl
index 7e4743c..f0abf04 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space_android.impl
+++ b/shared/OpenglCodecCommon/goldfish_address_space_android.impl
@@ -59,10 +59,14 @@
 
 const char GOLDFISH_ADDRESS_SPACE_DEVICE_NAME[] = "/dev/goldfish_address_space";
 
-const int DEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID = 5;
 const int HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID = 1;
 const int HOST_MEMORY_ALLOCATOR_COMMAND_UNALLOCATE_ID = 2;
 
+int create_address_space_fd()
+{
+    return ::open(GOLDFISH_ADDRESS_SPACE_DEVICE_NAME, O_RDWR);
+}
+
 long ioctl_allocate(int fd, struct goldfish_address_space_allocate_block *request)
 {
     return ::ioctl(fd, GOLDFISH_ADDRESS_SPACE_IOCTL_ALLOCATE_BLOCK, request);
@@ -78,31 +82,60 @@
     return ::ioctl(fd, GOLDFISH_ADDRESS_SPACE_IOCTL_PING, request);
 }
 
+long set_address_space_subdevice_type(int fd, uint64_t type)
+{
+    struct goldfish_address_space_ping request;
+    ::memset(&request, 0, sizeof(request));
+    request.version = sizeof(request);
+    request.metadata = type;
+
+    return ioctl_ping(fd, &request);
+}
+
 }  // namespace
 
-GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider()
-  : m_fd(::open(GOLDFISH_ADDRESS_SPACE_DEVICE_NAME, O_RDWR)) {}
+GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider(uint64_t subdevice)
+  : m_handle(create_address_space_fd())
+{
+    if ((subdevice != SUBDEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID) && is_opened()) {
+        const long ret = set_address_space_subdevice_type(m_handle, subdevice);
+        if (ret) {
+            ALOGE("%s: set_address_space_subdevice_type failed for device_type=%lu, ret=%ld",
+                  __func__, static_cast<unsigned long>(subdevice), ret);
+            close();
+        }
+    }
+}
 
 GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider()
 {
-    ::close(m_fd);
+    if (is_opened()) {
+        ::close(m_handle);
+    }
 }
 
 bool GoldfishAddressSpaceBlockProvider::is_opened() const
 {
-    return m_fd >= 0;
+    return m_handle >= 0;
 }
 
 void GoldfishAddressSpaceBlockProvider::close()
 {
     if (is_opened()) {
-        ::close(m_fd);
-        m_fd = -1;
+        ::close(m_handle);
+        m_handle = -1;
     }
 }
 
+address_space_handle_t GoldfishAddressSpaceBlockProvider::release()
+{
+    address_space_handle_t handle = m_handle;
+    m_handle = -1;
+    return handle;
+}
+
 GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
-    : m_fd(-1)
+    : m_handle(-1)
     , m_mmaped_ptr(NULL)
     , m_phys_addr(0)
     , m_host_addr(0)
@@ -121,7 +154,7 @@
     m_host_addr = rhs.m_host_addr;
     m_offset = rhs.m_offset;
     m_size = rhs.m_size;
-    m_fd = rhs.m_fd;
+    m_handle = rhs.m_handle;
 
     return *this;
 }
@@ -141,14 +174,14 @@
     ::memset(&request, 0, sizeof(request));
     request.size = size;
 
-    long res = ioctl_allocate(provider->m_fd, &request);
+    long res = ioctl_allocate(provider->m_handle, &request);
     if (res) {
         return false;
     } else {
         m_phys_addr = request.phys_addr;
         m_offset = request.offset;
         m_size = request.size;
-        m_fd = provider->m_fd;
+        m_handle = provider->m_handle;
 
         ALOGD("%s: ioctl allocate returned offset 0x%llx size 0x%llx\n", __func__,
                 (unsigned long long)m_offset,
@@ -179,7 +212,7 @@
         ::abort();
     }
 
-    void *result = ::mmap64(NULL, m_size, PROT_WRITE, MAP_SHARED, m_fd, m_offset);
+    void *result = ::mmap64(NULL, m_size, PROT_WRITE, MAP_SHARED, m_handle, m_offset);
     if (result == MAP_FAILED) {
         ALOGE("%s: host memory map failed with size 0x%llx "
               "off 0x%llx errno %d\n",
@@ -207,7 +240,7 @@
     }
 
     if (m_size) {
-        const long res = ioctl_deallocate(m_fd, m_offset);
+        const long res = ioctl_deallocate(m_handle, m_offset);
         if (res) {
             ALOGE("ioctl_deallocate failed, res=%ld", res);
             ::abort();
@@ -220,23 +253,21 @@
     }
 }
 
-GoldfishAddressSpaceHostMemoryAllocator::GoldfishAddressSpaceHostMemoryAllocator()
+void GoldfishAddressSpaceBlock::release()
 {
-    if (m_provider.is_opened()) {
-        struct goldfish_address_space_ping request;
-        ::memset(&request, 0, sizeof(request));
-        request.version = sizeof(request);
-        request.metadata = DEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID;
-
-        const long ret = ioctl_ping(m_provider.m_fd, &request);
-        if (ret) {
-            ALOGE("%s: ioctl_ping failed for device_type=%d, ret=%ld",
-                  __func__, DEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID, ret);
-            m_provider.close();
-        }
-    }
+    m_handle = -1;
+    m_mmaped_ptr = NULL;
+    m_phys_addr = 0;
+    m_host_addr = 0;
+    m_offset = 0;
+    m_size = 0;
 }
 
+GoldfishAddressSpaceHostMemoryAllocator::GoldfishAddressSpaceHostMemoryAllocator()
+  : m_provider(GoldfishAddressSpaceBlockProvider::SUBDEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID) {}
+
+bool GoldfishAddressSpaceHostMemoryAllocator::is_opened() const { return m_provider.is_opened(); }
+
 long GoldfishAddressSpaceHostMemoryAllocator::hostMalloc(GoldfishAddressSpaceBlock *block, size_t size)
 {
     if (size == 0) {
@@ -259,7 +290,7 @@
     request.size = block->size();
     request.metadata = HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID;
 
-    long ret = ioctl_ping(m_provider.m_fd, &request);
+    long ret = ioctl_ping(m_provider.m_handle, &request);
     if (ret) {
         return ret;
     }
@@ -290,7 +321,7 @@
         request.offset = block->offset();
         request.metadata = HOST_MEMORY_ALLOCATOR_COMMAND_UNALLOCATE_ID;
 
-        const long ret = ioctl_ping(m_provider.m_fd, &request);
+        const long ret = ioctl_ping(m_provider.m_handle, &request);
         if (ret) {
             ALOGE("%s: ioctl_ping failed, ret=%ld", __func__, ret);
             ::abort();
diff --git a/shared/OpenglCodecCommon/goldfish_address_space_fuchsia.impl b/shared/OpenglCodecCommon/goldfish_address_space_fuchsia.impl
index 6a10ca6..711dfa4 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space_fuchsia.impl
+++ b/shared/OpenglCodecCommon/goldfish_address_space_fuchsia.impl
@@ -52,6 +52,7 @@
 }
 
 // void GoldfishAddressSpaceBlockProvider::close() - not implemented
+// address_space_handle_t GoldfishAddressSpaceBlockProvider::release() - not imeplemented
 
 GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
     : m_device(NULL)
diff --git a/shared/OpenglCodecCommon/goldfish_address_space_host.impl b/shared/OpenglCodecCommon/goldfish_address_space_host.impl
index 1c7b4e2..99e1b0c 100644
--- a/shared/OpenglCodecCommon/goldfish_address_space_host.impl
+++ b/shared/OpenglCodecCommon/goldfish_address_space_host.impl
@@ -26,7 +26,6 @@
 
 namespace {
 
-const int DEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID = 5;
 const int HOST_MEMORY_ALLOCATOR_COMMAND_ALLOCATE_ID = 1;
 const int HOST_MEMORY_ALLOCATOR_COMMAND_UNALLOCATE_ID = 2;
 
@@ -35,12 +34,23 @@
 using android::HostAddressSpaceDevice;
 using android::emulation::AddressSpaceDevicePingInfo;
 
-GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider()
-    : m_handle(HostAddressSpaceDevice::get()->open()) {}
+GoldfishAddressSpaceBlockProvider::GoldfishAddressSpaceBlockProvider(uint64_t subdevice)
+    : m_handle(HostAddressSpaceDevice::get()->open())
+{
+    if ((subdevice != SUBDEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID) && is_opened()) {
+        AddressSpaceDevicePingInfo request;
+        ::memset(&request, 0, sizeof(request));
+        request.metadata = subdevice;
+
+        HostAddressSpaceDevice::get()->ping(m_handle, &request);
+    }
+}
 
 GoldfishAddressSpaceBlockProvider::~GoldfishAddressSpaceBlockProvider()
 {
-    HostAddressSpaceDevice::get()->close(m_handle);
+    if (is_opened()) {
+        HostAddressSpaceDevice::get()->close(m_handle);
+    }
 }
 
 bool GoldfishAddressSpaceBlockProvider::is_opened() const
@@ -56,6 +66,13 @@
     }
 }
 
+address_space_handle_t GoldfishAddressSpaceBlockProvider::release()
+{
+    address_space_handle_t handle = m_handle;
+    m_handle = 0;
+    return handle;
+}
+
 GoldfishAddressSpaceBlock::GoldfishAddressSpaceBlock()
     : m_handle(0)
     , m_mmaped_ptr(NULL)
@@ -148,17 +165,21 @@
     }
 }
 
-GoldfishAddressSpaceHostMemoryAllocator::GoldfishAddressSpaceHostMemoryAllocator()
+void GoldfishAddressSpaceBlock::release()
 {
-    if (m_provider.is_opened()) {
-        AddressSpaceDevicePingInfo request;
-        ::memset(&request, 0, sizeof(request));
-        request.metadata = DEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID;
-
-        HostAddressSpaceDevice::get()->ping(m_provider.m_handle, &request);
-    }
+    m_handle = 0;
+    m_mmaped_ptr = NULL;
+    m_phys_addr = 0;
+    m_host_addr = 0;
+    m_offset = 0;
+    m_size = 0;
 }
 
+GoldfishAddressSpaceHostMemoryAllocator::GoldfishAddressSpaceHostMemoryAllocator()
+  : m_provider(GoldfishAddressSpaceBlockProvider::SUBDEVICE_TYPE_HOST_MEMORY_ALLOCATOR_ID) {}
+
+bool GoldfishAddressSpaceHostMemoryAllocator::is_opened() const { return m_provider.is_opened(); }
+
 long GoldfishAddressSpaceHostMemoryAllocator::hostMalloc(GoldfishAddressSpaceBlock *block, size_t size)
 {
     if (size == 0) {
diff --git a/shared/OpenglCodecCommon/gralloc_cb_old.h b/shared/OpenglCodecCommon/gralloc_cb_old.h
index 1db5878..ab01537 100644
--- a/shared/OpenglCodecCommon/gralloc_cb_old.h
+++ b/shared/OpenglCodecCommon/gralloc_cb_old.h
@@ -33,7 +33,7 @@
 struct cb_handle_old_t : public native_handle {
 
     cb_handle_old_t(int p_fd, int p_ashmemSize, int p_usage,
-                    int p_width, int p_height, int p_frameworkFormat,
+                    int p_width, int p_height,
                     int p_format, int p_glFormat, int p_glType,
                     EmulatorFrameworkFormat p_emuFrameworkFormat) :
         fd(p_fd),
@@ -41,7 +41,6 @@
         usage(p_usage),
         width(p_width),
         height(p_height),
-        frameworkFormat(p_frameworkFormat),
         format(p_format),
         glFormat(p_glFormat),
         glType(p_glType),
@@ -126,7 +125,6 @@
     int usage;              // usage bits the buffer was created with
     int width;              // buffer width
     int height;             // buffer height
-    int frameworkFormat;    // format requested by the Android framework
     int format;             // real internal pixel format format
     int glFormat;           // OpenGL format enum used for host h/w color buffer
     int glType;             // OpenGL type enum used when uploading to host
diff --git a/system/OpenglSystemCommon/FormatConversions.cpp b/system/OpenglSystemCommon/FormatConversions.cpp
index d44fc13..83aff9c 100644
--- a/system/OpenglSystemCommon/FormatConversions.cpp
+++ b/system/OpenglSystemCommon/FormatConversions.cpp
@@ -12,6 +12,7 @@
  *
  */
 
+#include <hardware/gralloc.h>
 #include "FormatConversions.h"
 
 #if PLATFORM_SDK_VERSION < 26
@@ -34,6 +35,18 @@
     return row * width * rgbStride;
 }
 
+bool gralloc_is_yuv_format(const int format) {
+    switch (format) {
+    case HAL_PIXEL_FORMAT_YV12:
+    case HAL_PIXEL_FORMAT_YCbCr_420_888:
+    case HAL_PIXEL_FORMAT_YCrCb_420_SP:
+        return true;
+
+    default:
+        return false;
+    }
+}
+
 void get_yv12_offsets(int width, int height,
                              uint32_t* yStride_out,
                              uint32_t* cStride_out,
@@ -423,14 +436,13 @@
 }
 
 void copy_rgb_buffer_from_unlocked(
-        char* _dst, char* raw_data,
+        char* dst, const char* raw_data,
         int unlockedWidth,
         int width, int height, int top, int left,
         int bpp) {
-    char* dst = _dst;
     int dst_line_len = width * bpp;
     int src_line_len = unlockedWidth * bpp;
-    char *src = (char *)raw_data + top*src_line_len + left*bpp;
+    const char *src = raw_data + top*src_line_len + left*bpp;
     for (int y = 0; y < height; y++) {
         memcpy(dst, src, dst_line_len);
         src += src_line_len;
diff --git a/system/OpenglSystemCommon/FormatConversions.h b/system/OpenglSystemCommon/FormatConversions.h
index 196ca37..2a8da2b 100644
--- a/system/OpenglSystemCommon/FormatConversions.h
+++ b/system/OpenglSystemCommon/FormatConversions.h
@@ -18,6 +18,8 @@
 #include <inttypes.h>
 
 // format conversions and helper functions
+bool gralloc_is_yuv_format(int format); // e.g. HAL_PIXEL_FORMAT_YCbCr_420_888
+
 void get_yv12_offsets(int width, int height,
                       uint32_t* yStride_out,
                       uint32_t* cStride_out,
@@ -43,7 +45,7 @@
                        int left, int top, int right, int bottom);
 void nv21_to_rgb888(char* dest, char* src, int width, int height,
                     int left, int top, int right, int bottom);
-void copy_rgb_buffer_from_unlocked(char* _dst, char* raw_data,
+void copy_rgb_buffer_from_unlocked(char* _dst, const char* raw_data,
                                    int unlockedWidth,
                                    int width, int height, int top, int left,
                                    int bpp);
diff --git a/system/OpenglSystemCommon/HostConnection.h b/system/OpenglSystemCommon/HostConnection.h
index df111fd..a6424ab 100644
--- a/system/OpenglSystemCommon/HostConnection.h
+++ b/system/OpenglSystemCommon/HostConnection.h
@@ -21,7 +21,6 @@
 #include "renderControl_enc.h"
 #include "ChecksumCalculator.h"
 #include "goldfish_dma.h"
-#include "goldfish_address_space.h"
 
 #include <cutils/native_handle.h>
 
@@ -48,7 +47,7 @@
 public:
     ExtendedRCEncoderContext(IOStream *stream, ChecksumCalculator *checksumCalculator)
         : renderControl_encoder_context_t(stream, checksumCalculator),
-          m_dmaCxt(NULL), m_addressSpaceBlock(NULL) { }
+          m_dmaCxt(NULL), m_dmaPtr(NULL), m_dmaPhysAddr(0) { }
     void setSyncImpl(SyncImpl syncImpl) { m_featureInfo.syncImpl = syncImpl; }
     void setDmaImpl(DmaImpl dmaImpl) { m_featureInfo.dmaImpl = dmaImpl; }
     void setHostComposition(HostComposition hostComposition) {
@@ -65,12 +64,14 @@
         return m_featureInfo.hasYUVCache; }
     DmaImpl getDmaVersion() const { return m_featureInfo.dmaImpl; }
     void bindDmaContext(struct goldfish_dma_context* cxt) { m_dmaCxt = cxt; }
-    void bindAddressSpaceBlock(GoldfishAddressSpaceBlock* block) {
-        m_addressSpaceBlock = block;
+    void bindDmaDirectly(void* dmaPtr, uint64_t dmaPhysAddr) {
+        m_dmaPtr = dmaPtr;
+        m_dmaPhysAddr = dmaPhysAddr;
     }
     virtual uint64_t lockAndWriteDma(void* data, uint32_t size) {
-        if (m_addressSpaceBlock) {
-            return writeAddressSpaceBlock(data, size, m_addressSpaceBlock);
+        if (m_dmaPtr && m_dmaPhysAddr) {
+            memcpy(m_dmaPtr, data, size);
+            return m_dmaPhysAddr;
         } else if (m_dmaCxt) {
             return writeGoldfishDma(data, size, m_dmaCxt);
         } else {
@@ -103,20 +104,10 @@
         return paddr;
     }
 
-    static uint64_t writeAddressSpaceBlock(void* data, uint32_t size,
-                                           GoldfishAddressSpaceBlock* block) {
-        ALOGV("%s(data=%p, size=%u): call", __func__, data, size);
-
-        memcpy(block->guestPtr(), data, size);
-        const uint64_t paddr = block->physAddr();
-
-        ALOGV("%s: paddr=0x%llx", __func__, (unsigned long long)paddr);
-        return paddr;
-    }
-
     EmulatorFeatureInfo m_featureInfo;
     struct goldfish_dma_context* m_dmaCxt;
-    GoldfishAddressSpaceBlock* m_addressSpaceBlock;
+    void* m_dmaPtr;
+    uint64_t m_dmaPhysAddr;
 };
 
 // Abstraction for gralloc handle conversion
diff --git a/system/gralloc/gralloc_common.h b/system/gralloc/gralloc_common.h
new file mode 100644
index 0000000..574e659
--- /dev/null
+++ b/system/gralloc/gralloc_common.h
@@ -0,0 +1,41 @@
+/*
+* Copyright 2019 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.
+*/
+
+#ifndef __GRALLOC_COMMON_H__
+#define __GRALLOC_COMMON_H__
+
+#ifndef GL_RGBA16F
+#define GL_RGBA16F                        0x881A
+#endif // GL_RGBA16F
+
+#ifndef GL_HALF_FLOAT
+#define GL_HALF_FLOAT                     0x140B
+#endif // GL_HALF_FLOAT
+
+#ifndef GL_RGB10_A2
+#define GL_RGB10_A2                       0x8059
+#endif // GL_RGB10_A2
+
+#ifndef GL_UNSIGNED_INT_2_10_10_10_REV
+#define GL_UNSIGNED_INT_2_10_10_10_REV    0x8368
+#endif // GL_UNSIGNED_INT_2_10_10_10_REV
+
+// defined in hardware/interfaces/graphics/common/1.0/types.hal
+#ifndef GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER
+#define GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER (1ULL << 24)
+#endif // GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER
+
+#endif //__GRALLOC_COMMON_H__
diff --git a/system/gralloc/gralloc_old.cpp b/system/gralloc/gralloc_old.cpp
index c451a3b..773f786 100644
--- a/system/gralloc/gralloc_old.cpp
+++ b/system/gralloc/gralloc_old.cpp
@@ -28,6 +28,8 @@
 #include "../../shared/OpenglCodecCommon/gralloc_cb_old.h"
 #endif
 
+#include "gralloc_common.h"
+
 #include "goldfish_dma.h"
 #include "goldfish_address_space.h"
 #include "FormatConversions.h"
@@ -433,8 +435,8 @@
     uint32_t rgbSz = width * height * bpp;
     uint32_t send_buffer_size = rgbSz;
     bool is_rgb_format =
-        cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
-        cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888;
+        cb->format != HAL_PIXEL_FORMAT_YV12 &&
+        cb->format != HAL_PIXEL_FORMAT_YCbCr_420_888;
 
     std::vector<char> convertedBuf;
 
@@ -454,7 +456,7 @@
     }
 
     if (hasDMA && !grdma->bigbufCount) {
-        switch (cb->frameworkFormat) {
+        switch (cb->format) {
         case HAL_PIXEL_FORMAT_YV12:
             get_yv12_offsets(width, height, NULL, NULL, &send_buffer_size);
             break;
@@ -465,7 +467,8 @@
         }
 
         if (grdma->address_space_block.guestPtr()) {
-            rcEnc->bindAddressSpaceBlock(&grdma->address_space_block);
+            rcEnc->bindDmaDirectly(grdma->address_space_block.guestPtr(),
+                                   grdma->address_space_block.physAddr());
         } else if (grdma->goldfish_dma.mapped_addr) {
             rcEnc->bindDmaContext(&grdma->goldfish_dma);
         } else {
@@ -480,7 +483,7 @@
                 to_send, send_buffer_size);
         pthread_mutex_unlock(&grdma->lock);
     } else {
-        switch (cb->frameworkFormat) {
+        switch (cb->format) {
         case HAL_PIXEL_FORMAT_YV12:
             convertedBuf.resize(rgbSz);
             to_send = &convertedBuf.front();
@@ -510,28 +513,44 @@
     }
 }
 
-#ifndef GL_RGBA16F
-#define GL_RGBA16F                        0x881A
-#endif // GL_RGBA16F
-#ifndef GL_HALF_FLOAT
-#define GL_HALF_FLOAT                     0x140B
-#endif // GL_HALF_FLOAT
-#ifndef GL_RGB10_A2
-#define GL_RGB10_A2                       0x8059
-#endif // GL_RGB10_A2
-#ifndef GL_UNSIGNED_INT_2_10_10_10_REV
-#define GL_UNSIGNED_INT_2_10_10_10_REV    0x8368
-#endif // GL_UNSIGNED_INT_2_10_10_10_REV
 //
 // gralloc device functions (alloc interface)
 //
 static void gralloc_dump(struct alloc_device_t* /*dev*/, char* /*buff*/, int /*buff_len*/) {}
 
+static int gralloc_get_buffer_format(const int frameworkFormat, const int usage) {
+    // Pick the right concrete pixel format given the endpoints as encoded in
+    // the usage bits.  Every end-point pair needs explicit listing here.
+#if PLATFORM_SDK_VERSION >= 17
+    if (frameworkFormat == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
+        // Camera as producer
+        if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
+            if (usage & GRALLOC_USAGE_HW_TEXTURE) {
+                // Camera-to-display is RGBA
+                return HAL_PIXEL_FORMAT_RGBA_8888;
+            } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
+                // Camera-to-encoder is NV21
+                return HAL_PIXEL_FORMAT_YCrCb_420_SP;
+            }
+        }
+
+        ALOGE("gralloc_alloc: Requested auto format selection, "
+              "but no known format for this usage=%x", usage);
+        return -EINVAL;
+    } else if (frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
+        ALOGW("gralloc_alloc: Requested YCbCr_420_888, taking experimental path. "
+              "usage=%x", usage);
+    }
+#endif // PLATFORM_SDK_VERSION >= 17
+
+    return frameworkFormat;
+}
+
 static int gralloc_alloc(alloc_device_t* dev,
-                         int w, int h, int format, int usage,
+                         int w, int h, const int frameworkFormat, int usage,
                          buffer_handle_t* pHandle, int* pStride)
 {
-    D("gralloc_alloc w=%d h=%d usage=0x%x format=0x%x\n", w, h, usage, format);
+    D("gralloc_alloc w=%d h=%d usage=0x%x frameworkFormat=0x%x\n", w, h, usage, frameworkFormat);
 
     gralloc_device_t *grdev = (gralloc_device_t *)dev;
     if (!grdev || !pHandle || !pStride) {
@@ -540,6 +559,11 @@
         return -EINVAL;
     }
 
+    const int format = gralloc_get_buffer_format(frameworkFormat, usage);
+    if (format < 0) {
+        return format;
+    }
+
     //
     // Note: in screen capture mode, both sw_write and hw_write will be on
     // and this is a valid usage
@@ -567,38 +591,7 @@
     bool hw_vid_enc_read = false;
 #endif // PLATFORM_SDK_VERSION
 
-    // Keep around original requested format for later validation
-    int frameworkFormat = format;
-    // Pick the right concrete pixel format given the endpoints as encoded in
-    // the usage bits.  Every end-point pair needs explicit listing here.
-#if PLATFORM_SDK_VERSION >= 17
-    if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
-        // Camera as producer
-        if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
-            if (usage & GRALLOC_USAGE_HW_TEXTURE) {
-                // Camera-to-display is RGBA
-                format = HAL_PIXEL_FORMAT_RGBA_8888;
-            } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
-                // Camera-to-encoder is NV21
-                format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
-            }
-        }
-
-        if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
-            ALOGE("gralloc_alloc: Requested auto format selection, "
-                    "but no known format for this usage: %d x %d, usage %x",
-                    w, h, usage);
-            return -EINVAL;
-        }
-    }
-    else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
-        ALOGW("gralloc_alloc: Requested YCbCr_420_888, taking experimental path. "
-                "usage: %d x %d, usage %x",
-                w, h, usage);
-    }
-#endif // PLATFORM_SDK_VERSION >= 17
     bool yuv_format = false;
-
     int ashmem_size = 0;
     int stride = w;
 
@@ -713,10 +706,8 @@
     //
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 #if PLATFORM_SDK_VERSION >= 17
-    // GPU_DATA_BUFFER is defined in hardware/interfaces/graphics/common/1.0/types.hal
-#   define _GRALLOC_USAGE_GPU_DATA_BUFFER 0x1000000
     bool needHostCb = ((!yuv_format && frameworkFormat != HAL_PIXEL_FORMAT_BLOB) ||
-                      usage & _GRALLOC_USAGE_GPU_DATA_BUFFER ||
+                      usage & GOLDFISH_GRALLOC_USAGE_GPU_DATA_BUFFER ||
 #else
     bool needHostCb = (!yuv_format ||
 #endif // !(PLATFORM_SDK_VERSION >= 17)
@@ -788,7 +779,7 @@
     }
 
     cb_handle_old_t *cb = new cb_handle_old_t(fd, ashmem_size, usage,
-                                              w, h, frameworkFormat, format,
+                                              w, h, format,
                                               glFormat, glType,
                                               selectedEmuFrameworkFormat);
 
@@ -1069,8 +1060,8 @@
         return -EINVAL;
     }
 
-    D("gralloc_register_buffer(%p) w %d h %d format 0x%x framworkFormat 0x%x",
-        handle, cb->width, cb->height, cb->format, cb->frameworkFormat);
+    D("gralloc_register_buffer(%p) w %d h %d format 0x%x",
+        handle, cb->width, cb->height, cb->format);
 
     if (cb->hostHandle != 0 && !cb->hasRefcountPipe()) {
         D("Opening host ColorBuffer 0x%x\n", cb->hostHandle);
@@ -1230,7 +1221,6 @@
         //return -EINVAL;
     }
 
-    intptr_t postCount = 0;
     void *cpu_addr = NULL;
 
     //
@@ -1255,13 +1245,12 @@
         // flush color buffer write cache on host and get its sync status.
         //
         int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle,
-                                                            postCount,
+                                                            0,
                                                             sw_read);
         if (hostSyncStatus < 0) {
             // host failed the color buffer sync - probably since it was already
             // locked for write access. fail the lock.
-            ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n",
-                 (int)postCount, sw_read);
+            ALOGE("gralloc_lock cacheFlush failed sw_read=%d\n", sw_read);
             return -EBUSY;
         }
 
@@ -1272,11 +1261,11 @@
               cb->width, cb->height, cb->ashmemBase, cb->ashmemSize);
             void* rgb_addr = cpu_addr;
             char* tmpBuf = 0;
-            if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
-                cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
+            if (cb->format == HAL_PIXEL_FORMAT_YV12 ||
+                cb->format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
                 if (rcEnc->hasYUVCache()) {
                     uint32_t buffer_size;
-                    if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
+                    if (cb->format == HAL_PIXEL_FORMAT_YV12) {
                        get_yv12_offsets(cb->width, cb->height, NULL, NULL,
                                         &buffer_size);
                     } else {
@@ -1292,10 +1281,10 @@
                     tmpBuf = new char[cb->width * cb->height * 3];
                     rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle,
                                               0, 0, cb->width, cb->height, cb->glFormat, cb->glType, tmpBuf);
-                    if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12) {
+                    if (cb->format == HAL_PIXEL_FORMAT_YV12) {
                         D("convert rgb888 to yv12 here");
                         rgb888_to_yv12((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
-                    } else if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) {
+                    } else if (cb->format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
                         if (rcEnc->hasYUV420toNV21()) {
                             D("convert rgb888 to nv21 here");
                             rgb888_to_nv21((char*)cpu_addr, tmpBuf, cb->width, cb->height, l, t, l+w-1, t+h-1);
@@ -1408,12 +1397,12 @@
         return -EINVAL;
     }
 
-    if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YV12 &&
-        cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) {
+    if (cb->format != HAL_PIXEL_FORMAT_YV12 &&
+        cb->format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
         ALOGE("gralloc_lock_ycbcr can only be used with "
                 "HAL_PIXEL_FORMAT_YCbCr_420_888 or HAL_PIXEL_FORMAT_YV12, got %x instead. "
                 "-EINVAL",
-                cb->frameworkFormat);
+                cb->format);
         return -EINVAL;
     }
 
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 1db44cc..98bf049 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -593,7 +593,8 @@
 
         if (mFeatureInfo->hasDirectMem) {
             mGoldfishAddressSpaceBlockProvider.reset(
-                new GoldfishAddressSpaceBlockProvider);
+                new GoldfishAddressSpaceBlockProvider(
+                    GoldfishAddressSpaceBlockProvider::SUBDEVICE_TYPE_NO_SUBDEVICE_ID));
         }
 
 #ifdef VK_USE_PLATFORM_FUCHSIA