Move detached buffer to libui

Move it into libui early so that new modifications towards it can be
coded in the libui style

This CL only moves the file and updates Android.bp with proper
dependencies. Will re-format the coding style in a follow up CL. The
reason behind this is to keep this CL small enough so that the "git
mv" operation will be considered as an renaming rather than a complete
rewrite.

Note that DetachedBuffer is not exposed to VNDK, so that we won't
need to worry about ABI compatibility. Also, it temporarily introduces
some clang warning exceptions, we should be able to remove them very
soon once pdx to binder refactor is done for detached buffer.

Bug: 112010261
Test: atest BufferHubMetadata_test
Change-Id: I63659b9a9b7cb56f30fc2ae8cc5b87977d79b59c
diff --git a/libs/gui/BufferHubProducer.cpp b/libs/gui/BufferHubProducer.cpp
index 06d597c..0623a8e 100644
--- a/libs/gui/BufferHubProducer.cpp
+++ b/libs/gui/BufferHubProducer.cpp
@@ -18,8 +18,8 @@
 #include <gui/BufferHubProducer.h>
 #include <inttypes.h>
 #include <log/log.h>
-#include <private/dvr/detached_buffer.h>
 #include <system/window.h>
+#include <ui/BufferHubBuffer.h>
 #include <ui/DetachedBufferHandle.h>
 
 namespace android {
diff --git a/libs/ui/Android.bp b/libs/ui/Android.bp
index 1605050..24b1986 100644
--- a/libs/ui/Android.bp
+++ b/libs/ui/Android.bp
@@ -54,6 +54,8 @@
 
     srcs: [
         "ColorSpace.cpp",
+        "BufferHubBuffer.cpp",
+        "BufferHubMetadata.cpp",
         "DebugUtils.cpp",
         "Fence.cpp",
         "FenceTime.cpp",
@@ -91,6 +93,7 @@
         "libutils",
         "libutilscallstack",
         "liblog",
+        "libpdx_default_transport",  // TODO(b/112338294): Remove this once BufferHub moved to use Binder.
     ],
 
     export_shared_lib_headers: [
@@ -103,8 +106,27 @@
         "libmath",
     ],
 
+    // bufferhub is not used when building libgui for vendors
+    target: {
+        vendor: {
+            exclude_srcs: [
+                "BufferHubBuffer.cpp",
+                "BufferHubMetadata.cpp",
+            ],
+            exclude_header_libs: [
+                "libbufferhub_headers",
+                "libdvr_headers",
+            ],
+            exclude_shared_libs: [
+                "libpdx_default_transport",
+            ],
+        },
+    },
+
     header_libs: [
         "libbase_headers",
+        "libbufferhub_headers",
+        "libdvr_headers",
         "libnativebase_headers",
         "libhardware_headers",
         "libui_headers",
diff --git a/libs/vr/libbufferhub/detached_buffer.cpp b/libs/ui/BufferHubBuffer.cpp
similarity index 67%
rename from libs/vr/libbufferhub/detached_buffer.cpp
rename to libs/ui/BufferHubBuffer.cpp
index 795ad19..9293711 100644
--- a/libs/vr/libbufferhub/detached_buffer.cpp
+++ b/libs/ui/BufferHubBuffer.cpp
@@ -1,12 +1,50 @@
+/*
+ * Copyright (C) 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.
+ */
+
+// We would eliminate the clang warnings introduced by libdpx.
+// TODO(b/112338294): Remove those once BufferHub moved to use Binder
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconversion"
+#pragma clang diagnostic ignored "-Wdouble-promotion"
+#pragma clang diagnostic ignored "-Wgnu-case-range"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
+#pragma clang diagnostic ignored "-Wnested-anon-types"
+#pragma clang diagnostic ignored "-Wpacked"
+#pragma clang diagnostic ignored "-Wshadow"
+#pragma clang diagnostic ignored "-Wsign-conversion"
+#pragma clang diagnostic ignored "-Wswitch-enum"
+#pragma clang diagnostic ignored "-Wundefined-func-template"
+#pragma clang diagnostic ignored "-Wunused-template"
+#pragma clang diagnostic ignored "-Wweak-vtables"
 #include <pdx/default_transport/client_channel.h>
 #include <pdx/default_transport/client_channel_factory.h>
 #include <pdx/file_handle.h>
 #include <private/dvr/bufferhub_rpc.h>
-#include <private/dvr/detached_buffer.h>
+#pragma clang diagnostic pop
+
+#include <ui/BufferHubBuffer.h>
 #include <ui/DetachedBufferHandle.h>
 
 #include <poll.h>
 
+using android::dvr::BufferHubMetadata;
+using android::dvr::BufferTraits;
+using android::dvr::DetachedBufferRPC;
+using android::dvr::NativeHandleWrapper;
 using android::pdx::LocalChannelHandle;
 using android::pdx::LocalHandle;
 using android::pdx::Status;
@@ -14,7 +52,6 @@
 using android::pdx::default_transport::ClientChannelFactory;
 
 namespace android {
-namespace dvr {
 
 namespace {
 
@@ -30,6 +67,8 @@
 BufferHubClient::BufferHubClient(LocalChannelHandle channel_handle)
     : Client(ClientChannel::Create(std::move(channel_handle))) {}
 
+BufferHubClient::~BufferHubClient() {}
+
 bool BufferHubClient::IsValid() const {
   return IsConnected() && GetChannelHandle().valid();
 }
@@ -46,10 +85,9 @@
                                uint32_t layer_count, uint32_t format,
                                uint64_t usage, size_t user_metadata_size) {
   ATRACE_NAME("DetachedBuffer::DetachedBuffer");
-  ALOGD_IF(TRACE,
-           "DetachedBuffer::DetachedBuffer: width=%u height=%u layer_count=%u, "
-           "format=%u usage=%" PRIx64 " user_metadata_size=%zu",
-           width, height, layer_count, format, usage, user_metadata_size);
+  ALOGD("DetachedBuffer::DetachedBuffer: width=%u height=%u layer_count=%u, format=%u "
+        "usage=%" PRIx64 " user_metadata_size=%zu",
+        width, height, layer_count, format, usage, user_metadata_size);
 
   auto status = client_.InvokeRemoteMethod<DetachedBufferRPC::Create>(
       width, height, layer_count, format, usage, user_metadata_size);
@@ -113,8 +151,8 @@
     return -ENOMEM;
   }
 
-  size_t metadata_buf_size = buffer_traits.metadata_size();
-  if (metadata_buf_size < BufferHubDefs::kMetadataHeaderSize) {
+  size_t metadata_buf_size = static_cast<size_t>(buffer_traits.metadata_size());
+  if (metadata_buf_size < dvr::BufferHubDefs::kMetadataHeaderSize) {
     ALOGE("DetachedBuffer::ImportGraphicBuffer: metadata too small: %zu",
           metadata_buf_size);
     return -EINVAL;
@@ -129,12 +167,8 @@
   buffer_state_bit_ = buffer_traits.buffer_state_bit();
 
   // TODO(b/112012161) Set up shared fences.
-  ALOGD_IF(TRACE,
-           "DetachedBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64
-           ".",
-           id(),
-           metadata_.metadata_header()->buffer_state.load(
-               std::memory_order_acquire));
+  ALOGD("DetachedBuffer::ImportGraphicBuffer: id=%d, buffer_state=%" PRIx64 ".", id(),
+        metadata_.metadata_header()->buffer_state.load(std::memory_order_acquire));
   return 0;
 }
 
@@ -150,7 +184,7 @@
   return {};
 
   ATRACE_NAME("DetachedBuffer::Promote");
-  ALOGD_IF(TRACE, "DetachedBuffer::Promote: id=%d.", id_);
+  ALOGD("DetachedBuffer::Promote: id=%d.", id_);
 
   auto status_or_handle =
       client_.InvokeRemoteMethod<DetachedBufferRPC::Promote>();
@@ -166,7 +200,7 @@
 
 Status<LocalChannelHandle> DetachedBuffer::Duplicate() {
   ATRACE_NAME("DetachedBuffer::Duplicate");
-  ALOGD_IF(TRACE, "DetachedBuffer::Duplicate: id=%d.", id_);
+  ALOGD("DetachedBuffer::Duplicate: id=%d.", id_);
 
   auto status_or_handle =
       client_.InvokeRemoteMethod<DetachedBufferRPC::Duplicate>();
@@ -178,5 +212,4 @@
   return status_or_handle;
 }
 
-}  // namespace dvr
 }  // namespace android
diff --git a/libs/vr/libbufferhub/buffer_hub_metadata.cpp b/libs/ui/BufferHubMetadata.cpp
similarity index 96%
rename from libs/vr/libbufferhub/buffer_hub_metadata.cpp
rename to libs/ui/BufferHubMetadata.cpp
index 788c22c..0e5fce0 100644
--- a/libs/vr/libbufferhub/buffer_hub_metadata.cpp
+++ b/libs/ui/BufferHubMetadata.cpp
@@ -19,7 +19,7 @@
 
 #include <cutils/ashmem.h>
 #include <log/log.h>
-#include <private/dvr/buffer_hub_metadata.h>
+#include <ui/BufferHubMetadata.h>
 
 namespace android {
 namespace dvr {
@@ -71,7 +71,7 @@
     return {};
   }
 
-  size_t metadata_size = ashmem_get_size_region(ashmem_handle.Get());
+  size_t metadata_size = static_cast<size_t>(ashmem_get_size_region(ashmem_handle.Get()));
   size_t user_metadata_size = metadata_size - kMetadataHeaderSize;
 
   // Note that here the buffer state is mapped from shared memory as an atomic
diff --git a/libs/vr/libbufferhub/include/private/dvr/detached_buffer.h b/libs/ui/include/ui/BufferHubBuffer.h
similarity index 77%
rename from libs/vr/libbufferhub/include/private/dvr/detached_buffer.h
rename to libs/ui/include/ui/BufferHubBuffer.h
index ff61145..8cf1a6d 100644
--- a/libs/vr/libbufferhub/include/private/dvr/detached_buffer.h
+++ b/libs/ui/include/ui/BufferHubBuffer.h
@@ -1,17 +1,36 @@
 #ifndef ANDROID_DVR_DETACHED_BUFFER_H_
 #define ANDROID_DVR_DETACHED_BUFFER_H_
 
+// We would eliminate the clang warnings introduced by libdpx.
+// TODO(b/112338294): Remove those once BufferHub moved to use Binder
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconversion"
+#pragma clang diagnostic ignored "-Wdouble-promotion"
+#pragma clang diagnostic ignored "-Wgnu-case-range"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
+#pragma clang diagnostic ignored "-Wnested-anon-types"
+#pragma clang diagnostic ignored "-Wpacked"
+#pragma clang diagnostic ignored "-Wshadow"
+#pragma clang diagnostic ignored "-Wsign-conversion"
+#pragma clang diagnostic ignored "-Wswitch-enum"
+#pragma clang diagnostic ignored "-Wundefined-func-template"
+#pragma clang diagnostic ignored "-Wunused-template"
+#pragma clang diagnostic ignored "-Wweak-vtables"
 #include <pdx/client.h>
 #include <private/dvr/buffer_hub_defs.h>
-#include <private/dvr/buffer_hub_metadata.h>
-#include <private/dvr/ion_buffer.h>
+#include <private/dvr/native_handle_wrapper.h>
+#include <pdx/client.h>
+#pragma clang diagnostic pop
+
+#include <ui/BufferHubMetadata.h>
 
 namespace android {
-namespace dvr {
 
 class BufferHubClient : public pdx::Client {
  public:
   BufferHubClient();
+  virtual ~BufferHubClient();
   explicit BufferHubClient(pdx::LocalChannelHandle channel_handle);
 
   bool IsValid() const;
@@ -107,17 +126,16 @@
   uint64_t buffer_state_bit_;
 
   // Wrapps the gralloc buffer handle of this buffer.
-  NativeHandleWrapper<pdx::LocalHandle> buffer_handle_;
+  dvr::NativeHandleWrapper<pdx::LocalHandle> buffer_handle_;
 
   // An ashmem-based metadata object. The same shared memory are mapped to the
   // bufferhubd daemon and all buffer clients.
-  BufferHubMetadata metadata_;
+  dvr::BufferHubMetadata metadata_;
 
   // PDX backend.
   BufferHubClient client_;
 };
 
-}  // namespace dvr
 }  // namespace android
 
 #endif  // ANDROID_DVR_DETACHED_BUFFER_H_
diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_metadata.h b/libs/ui/include/ui/BufferHubMetadata.h
similarity index 77%
rename from libs/vr/libbufferhub/include/private/dvr/buffer_hub_metadata.h
rename to libs/ui/include/ui/BufferHubMetadata.h
index 980ade7..52d156d 100644
--- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_metadata.h
+++ b/libs/ui/include/ui/BufferHubMetadata.h
@@ -17,8 +17,25 @@
 #ifndef ANDROID_DVR_BUFFER_HUB_METADATA_H_
 #define ANDROID_DVR_BUFFER_HUB_METADATA_H_
 
+// We would eliminate the clang warnings introduced by libdpx.
+// TODO(b/112338294): Remove those once BufferHub moved to use Binder
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconversion"
+#pragma clang diagnostic ignored "-Wdouble-promotion"
+#pragma clang diagnostic ignored "-Wgnu-case-range"
+#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
+#pragma clang diagnostic ignored "-Winconsistent-missing-destructor-override"
+#pragma clang diagnostic ignored "-Wnested-anon-types"
+#pragma clang diagnostic ignored "-Wpacked"
+#pragma clang diagnostic ignored "-Wshadow"
+#pragma clang diagnostic ignored "-Wsign-conversion"
+#pragma clang diagnostic ignored "-Wswitch-enum"
+#pragma clang diagnostic ignored "-Wundefined-func-template"
+#pragma clang diagnostic ignored "-Wunused-template"
+#pragma clang diagnostic ignored "-Wweak-vtables"
 #include <pdx/file_handle.h>
 #include <private/dvr/buffer_hub_defs.h>
+#pragma clang diagnostic pop
 
 namespace android {
 namespace dvr {
diff --git a/libs/ui/tests/Android.bp b/libs/ui/tests/Android.bp
index aef6428..6e73960 100644
--- a/libs/ui/tests/Android.bp
+++ b/libs/ui/tests/Android.bp
@@ -34,3 +34,11 @@
     srcs: ["GraphicBuffer_test.cpp"],
     cflags: ["-Wall", "-Werror"],
 }
+
+cc_test {
+    name: "BufferHubMetadata_test",
+    header_libs: ["libbufferhub_headers", "libdvr_headers"],
+    shared_libs: ["libpdx_default_transport", "libui", "libutils"],
+    srcs: ["BufferHubMetadata_test.cpp"],
+    cflags: ["-Wall", "-Werror"],
+}
diff --git a/libs/vr/libbufferhub/buffer_hub_metadata-test.cpp b/libs/ui/tests/BufferHubMetadata_test.cpp
similarity index 97%
rename from libs/vr/libbufferhub/buffer_hub_metadata-test.cpp
rename to libs/ui/tests/BufferHubMetadata_test.cpp
index 8c1c01c..4209392 100644
--- a/libs/vr/libbufferhub/buffer_hub_metadata-test.cpp
+++ b/libs/ui/tests/BufferHubMetadata_test.cpp
@@ -15,7 +15,7 @@
  */
 
 #include <gtest/gtest.h>
-#include <private/dvr/buffer_hub_metadata.h>
+#include <ui/BufferHubMetadata.h>
 
 using android::dvr::BufferHubDefs::IsBufferGained;
 
diff --git a/libs/vr/libbufferhub/Android.bp b/libs/vr/libbufferhub/Android.bp
index 4bc46e8..ba5cfcd 100644
--- a/libs/vr/libbufferhub/Android.bp
+++ b/libs/vr/libbufferhub/Android.bp
@@ -12,20 +12,20 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+cc_library_headers {
+    name: "libbufferhub_headers",
+    export_include_dirs: ["include"],
+    vendor_available: true,  // TODO(b/112338314): Does shouldn't be available to vendor.
+}
+
 sourceFiles = [
     "buffer_hub_base.cpp",
-    "buffer_hub_metadata.cpp",
     "buffer_hub_rpc.cpp",
     "consumer_buffer.cpp",
-    "detached_buffer.cpp",
     "ion_buffer.cpp",
     "producer_buffer.cpp",
 ]
 
-localIncludeFiles = [
-    "include",
-]
-
 sharedLibraries = [
     "libbase",
     "libbinder",
@@ -39,6 +39,7 @@
 ]
 
 headerLibraries = [
+    "libbufferhub_headers",
     "libdvr_headers",
     "libnativebase_headers",
 ]
@@ -52,11 +53,11 @@
         "-Wall",
         "-Werror",
     ],
-    export_include_dirs: localIncludeFiles,
     shared_libs: sharedLibraries,
     header_libs: headerLibraries,
     name: "libbufferhub",
     export_header_lib_headers: [
+        "libbufferhub_headers",
         "libnativebase_headers",
     ],
 
@@ -74,14 +75,3 @@
     // TODO(b/117568153): Temporarily opt out using libcrt.
     no_libcrt: true,
 }
-
-cc_test {
-    srcs: ["buffer_hub_metadata-test.cpp"],
-    static_libs: ["libbufferhub"],
-    shared_libs: sharedLibraries,
-    header_libs: headerLibraries,
-    name: "buffer_hub_metadata-test",
-
-    // TODO(b/117568153): Temporarily opt out using libcrt.
-    no_libcrt: true,
-}
diff --git a/libs/vr/libbufferhub/buffer_hub-test.cpp b/libs/vr/libbufferhub/buffer_hub-test.cpp
index 5fb0cb2..08c6bed 100644
--- a/libs/vr/libbufferhub/buffer_hub-test.cpp
+++ b/libs/vr/libbufferhub/buffer_hub-test.cpp
@@ -2,9 +2,9 @@
 #include <poll.h>
 #include <private/dvr/buffer_hub_client.h>
 #include <private/dvr/bufferhub_rpc.h>
-#include <private/dvr/detached_buffer.h>
 #include <sys/epoll.h>
 #include <sys/eventfd.h>
+#include <ui/BufferHubBuffer.h>
 #include <ui/DetachedBufferHandle.h>
 
 #include <mutex>
@@ -19,10 +19,10 @@
     return result;                            \
   })()
 
+using android::DetachedBuffer;
 using android::GraphicBuffer;
 using android::sp;
 using android::dvr::ConsumerBuffer;
-using android::dvr::DetachedBuffer;
 using android::dvr::ProducerBuffer;
 using android::dvr::BufferHubDefs::IsBufferAcquired;
 using android::dvr::BufferHubDefs::IsBufferGained;
diff --git a/services/vr/bufferhubd/include/private/dvr/buffer_node.h b/services/vr/bufferhubd/include/private/dvr/buffer_node.h
index ebab97c..d6c6105 100644
--- a/services/vr/bufferhubd/include/private/dvr/buffer_node.h
+++ b/services/vr/bufferhubd/include/private/dvr/buffer_node.h
@@ -1,8 +1,8 @@
 #ifndef ANDROID_DVR_BUFFERHUBD_BUFFER_NODE_H_
 #define ANDROID_DVR_BUFFERHUBD_BUFFER_NODE_H_
 
-#include <private/dvr/buffer_hub_metadata.h>
 #include <private/dvr/ion_buffer.h>
+#include <ui/BufferHubMetadata.h>
 
 namespace android {
 namespace dvr {