Snap for 9051867 from c99754ef8addff057b67e43e89f14da28b1f39b8 to mainline-sdkext-release

Change-Id: Ie437b9eca705c2e378532f2f47aa7f9dfb43f094
diff --git a/gralloc/gr_buf_mgr.cpp b/gralloc/gr_buf_mgr.cpp
index 172083e..72b72fb 100644
--- a/gralloc/gr_buf_mgr.cpp
+++ b/gralloc/gr_buf_mgr.cpp
@@ -17,6 +17,43 @@
  * limitations under the License.
  */
 
+/*
+* Changes from Qualcomm Innovation Center are provided under the following license:
+*
+* Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted (subject to the limitations in the
+* disclaimer below) provided that the following conditions are met:
+*
+*    * Redistributions of source code must retain the above copyright
+*      notice, this list of conditions and the following disclaimer.
+*
+*    * Redistributions in binary form must reproduce the above
+*      copyright notice, this list of conditions and the following
+*      disclaimer in the documentation and/or other materials provided
+*      with the distribution.
+*
+*    * Neither the name of Qualcomm Innovation Center, Inc. nor the
+*      names of its contributors may be used to endorse or promote
+*      products derived from this software without specific prior
+*      written permission.
+*
+* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
+* GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
+* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
 #define DEBUG 0
 
 #include "gr_buf_mgr.h"
@@ -67,7 +104,8 @@
                                static_cast<uint32_t>(reserved_region_size)));
 }
 
-static void unmapAndReset(private_handle_t *handle, uint64_t reserved_region_size = 0) {
+static void unmapAndReset(private_handle_t *handle) {
+  uint64_t reserved_region_size = handle->reserved_size;
   if (private_handle_t::validate(handle) == 0 && handle->base_metadata) {
     munmap(reinterpret_cast<void *>(handle->base_metadata),
            static_cast<uint32_t>(getMetaDataSize(reserved_region_size)));
@@ -75,7 +113,7 @@
   }
 }
 
-static int validateAndMap(private_handle_t *handle, uint64_t reserved_region_size = 0) {
+static int validateAndMap(private_handle_t *handle) {
   if (private_handle_t::validate(handle)) {
     ALOGE("%s: Private handle is invalid - handle:%p", __func__, handle);
     return -1;
@@ -86,6 +124,7 @@
   }
 
   if (!handle->base_metadata) {
+    uint64_t reserved_region_size = handle->reserved_size;
     uint64_t size = getMetaDataSize(reserved_region_size);
     void *base = mmap(NULL, static_cast<uint32_t>(size), PROT_READ | PROT_WRITE,
                       MAP_SHARED, handle->fd_metadata, 0);
@@ -95,23 +134,6 @@
       return -1;
     }
     handle->base_metadata = (uintptr_t)base;
-#ifdef METADATA_V2
-    // The allocator process gets the reserved region size from the BufferDescriptor.
-    // When importing to another process, the reserved size is unknown until mapping the metadata,
-    // hence the re-mapping below
-    auto metadata = reinterpret_cast<MetaData_t *>(handle->base_metadata);
-    if (reserved_region_size == 0 && metadata->reservedSize) {
-      size = getMetaDataSize(metadata->reservedSize);
-      unmapAndReset(handle);
-      void *new_base = mmap(NULL, static_cast<uint32_t>(size), PROT_READ | PROT_WRITE, MAP_SHARED, handle->fd_metadata, 0);
-      if (new_base == reinterpret_cast<void *>(MAP_FAILED)) {
-        ALOGE("%s: metadata mmap failed - handle:%p fd: %d err: %s", __func__, handle,
-              handle->fd_metadata, strerror(errno));
-        return -1;
-      }
-      handle->base_metadata = (uintptr_t)new_base;
-    }
-#endif
   }
   return 0;
 }
@@ -743,7 +765,7 @@
     return Error::BAD_BUFFER;
   }
 
-  auto meta_size = getMetaDataSize(buf->reserved_size);
+  auto meta_size = getMetaDataSize(hnd->reserved_size);
 
   if (allocator_->FreeBuffer(reinterpret_cast<void *>(hnd->base), hnd->size, hnd->offset, hnd->fd,
                              buf->ion_handle_main) != 0) {
@@ -1068,6 +1090,7 @@
       data.fd, e_data.fd, INT(flags), INT(alignedw), INT(alignedh), descriptor.GetWidth(),
       descriptor.GetHeight(), format, buffer_type, data.size, usage);
 
+  hnd->reserved_size = static_cast<unsigned int>(descriptor.GetReservedSize());
   hnd->id = ++next_id_;
   hnd->base = 0;
   hnd->base_metadata = 0;
@@ -1078,11 +1101,7 @@
     setMetaDataAndUnmap(hnd, SET_GRAPHICS_METADATA, reinterpret_cast<void *>(&graphics_metadata));
   }
 
-#ifdef METADATA_V2
-  auto error = validateAndMap(hnd, descriptor.GetReservedSize());
-#else
   auto error = validateAndMap(hnd);
-#endif
 
   if (error != 0) {
     ALOGE("validateAndMap failed");
@@ -1104,7 +1123,7 @@
   metadata->crop.right = hnd->width;
   metadata->crop.bottom = hnd->height;
 
-  unmapAndReset(hnd, descriptor.GetReservedSize());
+  unmapAndReset(hnd);
 
   *handle = hnd;