Merge "[SurfaceFlinger] Avoid comparing to identity matrix."
diff --git a/include/android/sharedmem.h b/include/android/sharedmem.h
index 8c3ff74..7f5177b 100644
--- a/include/android/sharedmem.h
+++ b/include/android/sharedmem.h
@@ -94,7 +94,8 @@
  *     int fd = ASharedMemory_create("memory", 128);
  *
  *     // By default it has PROT_READ | PROT_WRITE | PROT_EXEC.
- *     char *buffer = (char *) mmap(NULL, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ *     size_t memSize = ASharedMemory_getSize(fd);
+ *     char *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  *
  *     strcpy(buffer, "This is an example."); // trivially initialize content
  *
diff --git a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
index 1a9018a..e37c388 100644
--- a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
@@ -42,8 +42,8 @@
 /**
  * analog using std::shared_ptr for internally held refcount
  *
- * ref must be called at least one time during the lifetime of this object. The recommended way to construct
- * this object is with SharedRefBase::make.
+ * ref must be called at least one time during the lifetime of this object. The recommended way to
+ * construct this object is with SharedRefBase::make.
  */
 class SharedRefBase {
 public:
@@ -77,7 +77,7 @@
     /**
      * Convenience method for making an object directly with a reference.
      */
-    template<class T, class... Args>
+    template <class T, class... Args>
     static std::shared_ptr<T> make(Args&&... args) {
         T* t = new T(std::forward<Args>(args)...);
         return t->template ref<T>();
diff --git a/libs/ui/BufferHubBuffer.cpp b/libs/ui/BufferHubBuffer.cpp
index 606386c..2696c6c 100644
--- a/libs/ui/BufferHubBuffer.cpp
+++ b/libs/ui/BufferHubBuffer.cpp
@@ -41,7 +41,6 @@
 
 #include <poll.h>
 
-using android::dvr::BufferHubMetadata;
 using android::dvr::BufferTraits;
 using android::dvr::DetachedBufferRPC;
 using android::dvr::NativeHandleWrapper;
@@ -160,7 +159,7 @@
 
     // If all imports succeed, replace the previous buffer and id.
     mId = bufferId;
-    mBfferStateBit = bufferTraits.buffer_state_bit();
+    mBufferStateBit = bufferTraits.buffer_state_bit();
 
     // TODO(b/112012161) Set up shared fences.
     ALOGD("BufferHubBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(),
diff --git a/libs/ui/BufferHubMetadata.cpp b/libs/ui/BufferHubMetadata.cpp
index b0c4510..1e08ed1 100644
--- a/libs/ui/BufferHubMetadata.cpp
+++ b/libs/ui/BufferHubMetadata.cpp
@@ -22,7 +22,6 @@
 #include <ui/BufferHubMetadata.h>
 
 namespace android {
-namespace dvr {
 
 namespace {
 
@@ -30,8 +29,8 @@
 
 } // namespace
 
-using BufferHubDefs::kMetadataHeaderSize;
-using BufferHubDefs::MetadataHeader;
+using dvr::BufferHubDefs::kMetadataHeaderSize;
+using dvr::BufferHubDefs::MetadataHeader;
 
 /* static */
 BufferHubMetadata BufferHubMetadata::Create(size_t userMetadataSize) {
@@ -101,5 +100,4 @@
     }
 }
 
-} // namespace dvr
 } // namespace android
diff --git a/libs/ui/include/ui/BufferHubBuffer.h b/libs/ui/include/ui/BufferHubBuffer.h
index e8ae244..d33be8c 100644
--- a/libs/ui/include/ui/BufferHubBuffer.h
+++ b/libs/ui/include/ui/BufferHubBuffer.h
@@ -88,7 +88,7 @@
 
     // A state mask which is unique to a buffer hub client among all its siblings sharing the same
     // concrete graphic buffer.
-    uint64_t buffer_state_bit() const { return mBfferStateBit; }
+    uint64_t buffer_state_bit() const { return mBufferStateBit; }
 
     size_t user_metadata_size() const { return mMetadata.user_metadata_size(); }
 
@@ -130,15 +130,15 @@
     int ImportGraphicBuffer();
 
     // Global id for the buffer that is consistent across processes.
-    int mId;
-    uint64_t mBfferStateBit;
+    int mId = -1;
+    uint64_t mBufferStateBit = 0;
 
     // Wrapps the gralloc buffer handle of this buffer.
     dvr::NativeHandleWrapper<pdx::LocalHandle> mBufferHandle;
 
     // An ashmem-based metadata object. The same shared memory are mapped to the
     // bufferhubd daemon and all buffer clients.
-    dvr::BufferHubMetadata mMetadata;
+    BufferHubMetadata mMetadata;
 
     // PDX backend.
     BufferHubClient mClient;
diff --git a/libs/ui/include/ui/BufferHubMetadata.h b/libs/ui/include/ui/BufferHubMetadata.h
index 46adc6a..94a9000 100644
--- a/libs/ui/include/ui/BufferHubMetadata.h
+++ b/libs/ui/include/ui/BufferHubMetadata.h
@@ -38,7 +38,6 @@
 #pragma clang diagnostic pop
 
 namespace android {
-namespace dvr {
 
 class BufferHubMetadata {
 public:
@@ -83,24 +82,25 @@
     bool IsValid() const { return mAshmemHandle.IsValid() && mMetadataHeader != nullptr; }
 
     size_t user_metadata_size() const { return mUserMetadataSize; }
-    size_t metadata_size() const { return mUserMetadataSize + BufferHubDefs::kMetadataHeaderSize; }
+    size_t metadata_size() const {
+        return mUserMetadataSize + dvr::BufferHubDefs::kMetadataHeaderSize;
+    }
 
     const pdx::LocalHandle& ashmem_handle() const { return mAshmemHandle; }
-    BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; }
+    dvr::BufferHubDefs::MetadataHeader* metadata_header() { return mMetadataHeader; }
 
 private:
     BufferHubMetadata(size_t userMetadataSize, pdx::LocalHandle ashmemHandle,
-                      BufferHubDefs::MetadataHeader* metadataHeader);
+                      dvr::BufferHubDefs::MetadataHeader* metadataHeader);
 
     BufferHubMetadata(const BufferHubMetadata&) = delete;
     void operator=(const BufferHubMetadata&) = delete;
 
     size_t mUserMetadataSize = 0;
     pdx::LocalHandle mAshmemHandle;
-    BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr;
+    dvr::BufferHubDefs::MetadataHeader* mMetadataHeader = nullptr;
 };
 
-} // namespace dvr
 } // namespace android
 
 #endif // ANDROID_BUFFER_HUB_METADATA_H_
diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp
index 6e73960..228d202 100644
--- a/libs/ui/tests/Android.bp
+++ b/libs/ui/tests/Android.bp
@@ -36,6 +36,14 @@
 }
 
 cc_test {
+    name: "BufferHubBuffer_test",
+    header_libs: ["libbufferhub_headers", "libdvr_headers"],
+    shared_libs: ["libpdx_default_transport", "libui", "libutils"],
+    srcs: ["BufferHubBuffer_test.cpp"],
+    cflags: ["-Wall", "-Werror"],
+}
+
+cc_test {
     name: "BufferHubMetadata_test",
     header_libs: ["libbufferhub_headers", "libdvr_headers"],
     shared_libs: ["libpdx_default_transport", "libui", "libutils"],
diff --git a/libs/ui/tests/BufferHubBuffer_test.cpp b/libs/ui/tests/BufferHubBuffer_test.cpp
new file mode 100644
index 0000000..e71c99c
--- /dev/null
+++ b/libs/ui/tests/BufferHubBuffer_test.cpp
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2018 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.
+ */
+
+#define LOG_TAG "BufferHubBufferTest"
+
+#include <gtest/gtest.h>
+#include <ui/BufferHubBuffer.h>
+
+namespace android {
+
+namespace {
+
+const int kWidth = 640;
+const int kHeight = 480;
+const int kLayerCount = 1;
+const int kFormat = HAL_PIXEL_FORMAT_RGBA_8888;
+const int kUsage = 0;
+const size_t kUserMetadataSize = 0;
+
+} // namespace
+
+using BufferHubBufferTest = ::testing::Test;
+
+using dvr::BufferHubDefs::IsBufferGained;
+using dvr::BufferHubDefs::kMetadataHeaderSize;
+using dvr::BufferHubDefs::kProducerStateBit;
+using pdx::LocalChannelHandle;
+
+TEST_F(BufferHubBufferTest, CreateBufferHubBufferFails) {
+    // Buffer Creation will fail: BLOB format requires height to be 1.
+    auto b1 = BufferHubBuffer::Create(kWidth, /*height=*/2, kLayerCount,
+                                      /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage, kUserMetadataSize);
+
+    EXPECT_FALSE(b1->IsConnected());
+    EXPECT_FALSE(b1->IsValid());
+
+    // Buffer Creation will fail: user metadata size too large.
+    auto b2 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage,
+                                      /*userMetadataSize=*/std::numeric_limits<size_t>::max());
+
+    EXPECT_FALSE(b2->IsConnected());
+    EXPECT_FALSE(b2->IsValid());
+
+    // Buffer Creation will fail: user metadata size too large.
+    const size_t userMetadataSize = std::numeric_limits<size_t>::max() - kMetadataHeaderSize;
+    auto b3 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage,
+                                      userMetadataSize);
+
+    EXPECT_FALSE(b3->IsConnected());
+    EXPECT_FALSE(b3->IsValid());
+}
+
+TEST_F(BufferHubBufferTest, CreateBufferHubBuffer) {
+    auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage,
+                                      kUserMetadataSize);
+    EXPECT_TRUE(b1->IsConnected());
+    EXPECT_TRUE(b1->IsValid());
+    EXPECT_NE(b1->id(), 0);
+}
+
+TEST_F(BufferHubBufferTest, DuplicateBufferHubBuffer) {
+    auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat, kUsage,
+                                      kUserMetadataSize);
+    int id1 = b1->id();
+    EXPECT_TRUE(b1->IsValid());
+    EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize);
+
+    auto statusOrHandle = b1->Duplicate();
+    EXPECT_TRUE(statusOrHandle);
+
+    // The detached buffer should still be valid.
+    EXPECT_TRUE(b1->IsConnected());
+    EXPECT_TRUE(b1->IsValid());
+
+    // Gets the channel handle for the duplicated buffer.
+    LocalChannelHandle h2 = statusOrHandle.take();
+    EXPECT_TRUE(h2.valid());
+
+    std::unique_ptr<BufferHubBuffer> b2 = BufferHubBuffer::Import(std::move(h2));
+    EXPECT_FALSE(h2.valid());
+    ASSERT_TRUE(b2 != nullptr);
+    EXPECT_TRUE(b2->IsValid());
+    EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize);
+
+    int id2 = b2->id();
+
+    // These two buffer instances are based on the same physical buffer under the
+    // hood, so they should share the same id.
+    EXPECT_EQ(id1, id2);
+    // We use buffer_state_bit() to tell those two instances apart.
+    EXPECT_NE(b1->buffer_state_bit(), b2->buffer_state_bit());
+    EXPECT_NE(b1->buffer_state_bit(), 0ULL);
+    EXPECT_NE(b2->buffer_state_bit(), 0ULL);
+    EXPECT_NE(b1->buffer_state_bit(), kProducerStateBit);
+    EXPECT_NE(b2->buffer_state_bit(), kProducerStateBit);
+
+    // Both buffer instances should be in gained state.
+    EXPECT_TRUE(IsBufferGained(b1->buffer_state()));
+    EXPECT_TRUE(IsBufferGained(b2->buffer_state()));
+
+    // TODO(b/112338294): rewrite test after migration
+    return;
+
+    // Promote the detached buffer should fail as b1 is no longer the exclusive
+    // owner of the buffer..
+    statusOrHandle = b1->Promote();
+    EXPECT_FALSE(statusOrHandle.ok());
+    EXPECT_EQ(statusOrHandle.error(), EINVAL);
+}
+
+} // namespace android
diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp
index b477f1a..571d382 100644
--- a/libs/vr/libbufferhub/buffer_hub-test.cpp
+++ b/libs/vr/libbufferhub/buffer_hub-test.cpp
@@ -794,41 +794,6 @@
   EXPECT_EQ(d->id(), p_id);
 }
 
-TEST_F(LibBufferHubTest, TestCreateBufferHubBufferFails) {
-  // Buffer Creation will fail: BLOB format requires height to be 1.
-  auto b1 = BufferHubBuffer::Create(kWidth, /*height=2*/ 2, kLayerCount,
-                                    /*format=*/HAL_PIXEL_FORMAT_BLOB, kUsage,
-                                    kUserMetadataSize);
-
-  EXPECT_FALSE(b1->IsConnected());
-  EXPECT_FALSE(b1->IsValid());
-
-  // Buffer Creation will fail: user metadata size too large.
-  auto b2 = BufferHubBuffer::Create(
-      kWidth, kHeight, kLayerCount, kFormat, kUsage,
-      /*user_metadata_size=*/std::numeric_limits<size_t>::max());
-
-  EXPECT_FALSE(b2->IsConnected());
-  EXPECT_FALSE(b2->IsValid());
-
-  // Buffer Creation will fail: user metadata size too large.
-  auto b3 = BufferHubBuffer::Create(
-      kWidth, kHeight, kLayerCount, kFormat, kUsage,
-      /*user_metadata_size=*/std::numeric_limits<size_t>::max() -
-          kMetadataHeaderSize);
-
-  EXPECT_FALSE(b3->IsConnected());
-  EXPECT_FALSE(b3->IsValid());
-}
-
-TEST_F(LibBufferHubTest, TestCreateBufferHubBuffer) {
-  auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat,
-                                    kUsage, kUserMetadataSize);
-  EXPECT_TRUE(b1->IsConnected());
-  EXPECT_TRUE(b1->IsValid());
-  EXPECT_NE(b1->id(), 0);
-}
-
 TEST_F(LibBufferHubTest, TestPromoteBufferHubBuffer) {
   // TODO(b/112338294) rewrite test after migration
   return;
@@ -908,53 +873,3 @@
   EXPECT_EQ(b1_id, p2_id);
   EXPECT_TRUE(IsBufferGained(p2->buffer_state()));
 }
-
-TEST_F(LibBufferHubTest, TestDuplicateBufferHubBuffer) {
-  auto b1 = BufferHubBuffer::Create(kWidth, kHeight, kLayerCount, kFormat,
-                                    kUsage, kUserMetadataSize);
-  int b1_id = b1->id();
-  EXPECT_TRUE(b1->IsValid());
-  EXPECT_EQ(b1->user_metadata_size(), kUserMetadataSize);
-
-  auto status_or_handle = b1->Duplicate();
-  EXPECT_TRUE(status_or_handle);
-
-  // The detached buffer should still be valid.
-  EXPECT_TRUE(b1->IsConnected());
-  EXPECT_TRUE(b1->IsValid());
-
-  // Gets the channel handle for the duplicated buffer.
-  LocalChannelHandle h2 = status_or_handle.take();
-  EXPECT_TRUE(h2.valid());
-
-  std::unique_ptr<BufferHubBuffer> b2 = BufferHubBuffer::Import(std::move(h2));
-  EXPECT_FALSE(h2.valid());
-  ASSERT_TRUE(b2 != nullptr);
-  EXPECT_TRUE(b2->IsValid());
-  EXPECT_EQ(b2->user_metadata_size(), kUserMetadataSize);
-
-  int b2_id = b2->id();
-
-  // These two buffer instances are based on the same physical buffer under the
-  // hood, so they should share the same id.
-  EXPECT_EQ(b1_id, b2_id);
-  // We use buffer_state_bit() to tell those two instances apart.
-  EXPECT_NE(b1->buffer_state_bit(), b2->buffer_state_bit());
-  EXPECT_NE(b1->buffer_state_bit(), 0ULL);
-  EXPECT_NE(b2->buffer_state_bit(), 0ULL);
-  EXPECT_NE(b1->buffer_state_bit(), kProducerStateBit);
-  EXPECT_NE(b2->buffer_state_bit(), kProducerStateBit);
-
-  // Both buffer instances should be in gained state.
-  EXPECT_TRUE(IsBufferGained(b1->buffer_state()));
-  EXPECT_TRUE(IsBufferGained(b2->buffer_state()));
-
-  // TODO(b/112338294) rewrite test after migration
-  return;
-
-  // Promote the detached buffer should fail as b1 is no longer the exclusive
-  // owner of the buffer..
-  status_or_handle = b1->Promote();
-  EXPECT_FALSE(status_or_handle.ok());
-  EXPECT_EQ(status_or_handle.error(), EINVAL);
-}
diff --git a/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h b/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h
index 3086982..ae70b77 100644
--- a/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h
+++ b/libs/vr/libbufferhub/include/private/dvr/native_handle_wrapper.h
@@ -40,7 +40,7 @@
   bool IsValid() const { return ints_.size() != 0 || fds_.size() != 0; }
 
   // Duplicate a native handle from the wrapper.
-  const native_handle_t* DuplicateHandle() const {
+  native_handle_t* DuplicateHandle() const {
     if (!IsValid()) {
       return nullptr;
     }
@@ -58,7 +58,7 @@
   }
 
   // Takes the native handle out of the wrapper.
-  const native_handle_t* TakeHandle() {
+  native_handle_t* TakeHandle() {
     if (!IsValid()) {
       return nullptr;
     }
@@ -70,8 +70,8 @@
   NativeHandleWrapper(const NativeHandleWrapper&) = delete;
   void operator=(const NativeHandleWrapper&) = delete;
 
-  static const native_handle_t* FromFdsAndInts(std::vector<FileHandleType> fds,
-                                               std::vector<int> ints) {
+  static native_handle_t* FromFdsAndInts(std::vector<FileHandleType> fds,
+                                         std::vector<int> ints) {
     native_handle_t* handle = native_handle_create(fds.size(), ints.size());
     if (!handle) {
       ALOGE("NativeHandleWrapper::TakeHandle: Failed to create new handle.");
diff --git a/services/surfaceflinger/TimeStats/TimeStats.cpp b/services/surfaceflinger/TimeStats/TimeStats.cpp
index 0bc1c0a..2e1231b 100644
--- a/services/surfaceflinger/TimeStats/TimeStats.cpp
+++ b/services/surfaceflinger/TimeStats/TimeStats.cpp
@@ -145,14 +145,15 @@
     return static_cast<int32_t>(delta);
 }
 
+// This regular expression captures the following for instance:
+// StatusBar in StatusBar#0
+// com.appname in com.appname/com.appname.activity#0
+// com.appname in SurfaceView - com.appname/com.appname.activity#0
+static const std::regex packageNameRegex("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
+
 static std::string getPackageName(const std::string& layerName) {
-    // This regular expression captures the following for instance:
-    // StatusBar in StatusBar#0
-    // com.appname in com.appname/com.appname.activity#0
-    // com.appname in SurfaceView - com.appname/com.appname.activity#0
-    const std::regex re("(?:SurfaceView[-\\s\\t]+)?([^/]+).*#\\d+");
     std::smatch match;
-    if (std::regex_match(layerName.begin(), layerName.end(), match, re)) {
+    if (std::regex_match(layerName.begin(), layerName.end(), match, packageNameRegex)) {
         // There must be a match for group 1 otherwise the whole string is not
         // matched and the above will return false
         return match[1];
@@ -223,17 +224,19 @@
     }
 }
 
+// This regular expression captures the following layer names for instance:
+// 1) StatusBat#0
+// 2) NavigationBar#1
+// 3) co(m).*#0
+// 4) SurfaceView - co(m).*#0
+// Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
+// is a bit more robust in case there's a slight change.
+// The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
+static const std::regex layerNameRegex(
+        "(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
+
 static bool layerNameIsValid(const std::string& layerName) {
-    // This regular expression captures the following layer names for instance:
-    // 1) StatusBat#0
-    // 2) NavigationBar#1
-    // 3) co(m).*#0
-    // 4) SurfaceView - co(m).*#0
-    // Using [-\\s\t]+ for the conjunction part between SurfaceView and co(m).*
-    // is a bit more robust in case there's a slight change.
-    // The layer name would only consist of . / $ _ 0-9 a-z A-Z in most cases.
-    std::regex re("(((SurfaceView[-\\s\\t]+)?com?\\.[./$\\w]+)|((Status|Navigation)Bar))#\\d+");
-    return std::regex_match(layerName.begin(), layerName.end(), re);
+    return std::regex_match(layerName.begin(), layerName.end(), layerNameRegex);
 }
 
 void TimeStats::setPostTime(const std::string& layerName, uint64_t frameNumber, nsecs_t postTime) {
diff --git a/vulkan/vkjson/vkjson_instance.cc b/vulkan/vkjson/vkjson_instance.cc
index 313d095..05d4dfe 100644
--- a/vulkan/vkjson/vkjson_instance.cc
+++ b/vulkan/vkjson/vkjson_instance.cc
@@ -353,7 +353,7 @@
                                       1,
                                       "",
                                       0,
-                                      VK_API_VERSION_1_0};
+                                      VK_API_VERSION_1_1};
   VkInstanceCreateInfo instance_info = {
       VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
       nullptr,