Move the gralloc code into position

Change-Id: Ie6510248756aa7a8624910c7b9472101e070baa5
diff --git a/Android.bp b/Android.bp
index d4d86a5..5ad19cf 100644
--- a/Android.bp
+++ b/Android.bp
@@ -98,11 +98,13 @@
         "libcuttlefish_fs",
         "cuttlefish_auto_resources",
         "libbase",
+        "liblog",
     ],
     target: {
         //TODO(ghartman): linux_glibc on master
         linux: {
             srcs: [
+                "host/vsoc/lib/gralloc_buffer_region_view.cpp",
                 "host/vsoc/lib/host_lock.cpp",
                 "host/vsoc/lib/region_control.cpp",
                 "host/vsoc/lib/region_view.cpp",
@@ -110,6 +112,7 @@
         },
         android: {
             srcs: [
+                "guest/vsoc/lib/gralloc_region_view.cpp",
                 "guest/vsoc/lib/guest_lock.cpp",
                 "guest/vsoc/lib/region_control.cpp",
                 "guest/vsoc/lib/region_view.cpp",
@@ -119,19 +122,6 @@
     defaults: ["cuttlefish_host_and_guest", "cuttlefish_native_isa"],
 }
 
-cc_library_host_static {
-    name: "libvsoc_gralloc",
-    srcs: [
-        "host/vsoc/gralloc/gralloc_buffer_region.cpp",
-    ],
-    header_libs: ["cuttlefish_glog"],
-    shared_libs: [
-        "vsoc_lib",
-        "libbase",
-    ],
-    defaults: ["cuttlefish_host_only"],
-}
-
 cc_test_host {
     name: "circqueue_test",
     srcs: [
diff --git a/guest/hals/gralloc/Android.mk b/guest/hals/gralloc/Android.mk
index 5b5da68..5e4a648 100644
--- a/guest/hals/gralloc/Android.mk
+++ b/guest/hals/gralloc/Android.mk
@@ -40,8 +40,7 @@
     cuttlefish_auto_resources \
     libcuttlefish_fs \
     liblog \
-    vsoc_lib \
-    libvsoc_gralloc
+    vsoc_lib
 
 ifeq (0, $(shell test $(PLATFORM_SDK_VERSION) -ge 21; echo $$?))
 LOCAL_MODULE_RELATIVE_PATH := hw
diff --git a/guest/hals/gralloc/gralloc.cpp b/guest/hals/gralloc/gralloc.cpp
index d6c6110..32bcc07 100644
--- a/guest/hals/gralloc/gralloc.cpp
+++ b/guest/hals/gralloc/gralloc.cpp
@@ -267,7 +267,7 @@
     dev->device.alloc   = gralloc_alloc;
     dev->device.free    = gralloc_free;
 
-    dev->gralloc_region = vsoc::gralloc::GrallocRegion::GetInstance();
+    dev->gralloc_region = vsoc::gralloc::GrallocRegionView::GetInstance();
     if (!dev->gralloc_region) {
       LOG_FATAL("Unable to instantiate the gralloc region");
       free(dev);
diff --git a/guest/hals/gralloc/gralloc_vsoc_priv.h b/guest/hals/gralloc/gralloc_vsoc_priv.h
index 8fb0ed6..2f4f287 100644
--- a/guest/hals/gralloc/gralloc_vsoc_priv.h
+++ b/guest/hals/gralloc/gralloc_vsoc_priv.h
@@ -16,13 +16,14 @@
  */
 
 #include <cutils/native_handle.h>
-#include <guest/vsoc/gralloc/gralloc_region.h>
 #include <hardware/gralloc.h>
 #include <log/log.h>
 
+#include "guest/vsoc/lib/gralloc_region_view.h"
+
 struct vsoc_alloc_device_t {
   alloc_device_t device;
-  vsoc::gralloc::GrallocRegion* gralloc_region;
+  vsoc::gralloc::GrallocRegionView* gralloc_region;
 };
 
 struct vsoc_gralloc_module_t {
diff --git a/guest/vsoc/gralloc/Android.mk b/guest/vsoc/gralloc/Android.mk
deleted file mode 100644
index b073e53..0000000
--- a/guest/vsoc/gralloc/Android.mk
+++ /dev/null
@@ -1,51 +0,0 @@
-# Copyright (C) 2017 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.
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libvsoc_gralloc
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_SRC_FILES := \
-    ../../../common/vsoc/lib/gralloc_layout.cpp \
-    gralloc_region.cpp
-
-LOCAL_C_INCLUDES += \
-    device/google/cuttlefish_common/guest/vsoc/gralloc \
-    device/google/cuttlefish_common \
-    device/google/cuttlefish_kernel \
-    system/core/base/include
-
-LOCAL_CFLAGS := \
-    -DLOG_TAG=\"libvsoc_gralloc\" \
-    -Wno-missing-field-initializers \
-    -Wall -Werror
-
-LOCAL_SHARED_LIBRARIES := \
-    libbase \
-    cuttlefish_auto_resources \
-    libcuttlefish_fs \
-    liblog \
-    vsoc_lib
-
-LOCAL_VENDOR_MODULE := true
-
-# See b/67109557
-ifeq (true, $(TARGET_TRANSLATE_2ND_ARCH))
-LOCAL_MULTILIB := first
-endif
-
-include $(BUILD_SHARED_LIBRARY)
diff --git a/guest/vsoc/gralloc/gralloc_region.cpp b/guest/vsoc/lib/gralloc_region_view.cpp
similarity index 93%
rename from guest/vsoc/gralloc/gralloc_region.cpp
rename to guest/vsoc/lib/gralloc_region_view.cpp
index 0d38782..2ec1aba 100644
--- a/guest/vsoc/gralloc/gralloc_region.cpp
+++ b/guest/vsoc/lib/gralloc_region_view.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "gralloc_region.h"
+#include "guest/vsoc/lib/gralloc_region_view.h"
 
 #include <atomic>
 #include <common/vsoc/lib/lock_guard.h>
@@ -22,7 +22,7 @@
 #include <sys/types.h>
 #include <uapi/vsoc_shm.h>
 
-using vsoc::gralloc::GrallocRegion;
+using vsoc::gralloc::GrallocRegionView;
 using vsoc::layout::gralloc::BufferEntry;
 using vsoc::layout::gralloc::GrallocBufferLayout;
 using vsoc::layout::gralloc::GrallocManagerLayout;
@@ -40,14 +40,14 @@
 
 }  // namespace
 
-GrallocRegion::GrallocRegion() {
+GrallocRegionView::GrallocRegionView() {
   // The construction in the singleton is thread safe, so we call Open here to
   // make sure it opens thread safe too. The singleton will return null if the
   // region failed to open.
   Open();
 }
 
-bool GrallocRegion::Open() {
+bool GrallocRegionView::Open() {
   if (is_open_) {
     return true;
   }
@@ -71,7 +71,7 @@
   return true;
 }
 
-int GrallocRegion::AllocateBuffer(size_t size, uint32_t* begin_offset) {
+int GrallocRegionView::AllocateBuffer(size_t size, uint32_t* begin_offset) {
   size = gralloc_align<size_t>(size);
   // Cache the value of buffer_count in shared memory.
   uint32_t buffer_count_local = 0;
@@ -156,8 +156,8 @@
 // The C++03 standard does not guarantee this singleton implemention to be
 // thread safe, however magic statics are part of the gcc compiler since
 // version 4.3.
-GrallocRegion* GrallocRegion::GetInstance() {
-  static GrallocRegion singleton;
+GrallocRegionView* GrallocRegionView::GetInstance() {
+  static GrallocRegionView singleton;
   if (!singleton.is_open_) {
     return NULL;
   }
diff --git a/guest/vsoc/gralloc/gralloc_region.h b/guest/vsoc/lib/gralloc_region_view.h
similarity index 85%
rename from guest/vsoc/gralloc/gralloc_region.h
rename to guest/vsoc/lib/gralloc_region_view.h
index 927c231..2432e8c 100644
--- a/guest/vsoc/gralloc/gralloc_region.h
+++ b/guest/vsoc/lib/gralloc_region_view.h
@@ -23,7 +23,7 @@
 namespace vsoc {
 namespace gralloc {
 
-class GrallocRegion : public vsoc::ManagerRegionView<
+class GrallocRegionView : public vsoc::ManagerRegionView<
                           vsoc::layout::gralloc::GrallocManagerLayout> {
  public:
   // Allocates a gralloc buffer of (at least) the specified size. Returns a file
@@ -33,11 +33,11 @@
   // TODO(jemoreira): Include debug info like stride, width, height, etc
   int AllocateBuffer(size_t size, uint32_t* begin_offset = nullptr);
 
-  static GrallocRegion* GetInstance();
+  static GrallocRegionView* GetInstance();
  protected:
-  GrallocRegion();
-  GrallocRegion(const GrallocRegion&);
-  GrallocRegion& operator=(const GrallocRegion&);
+  GrallocRegionView();
+  GrallocRegionView(const GrallocRegionView&) = delete;
+  GrallocRegionView & operator=(const GrallocRegionView&) = delete;
 
   bool Open();
 
diff --git a/host/frontend/vnc_server/Android.bp b/host/frontend/vnc_server/Android.bp
index dd4f356..b5ec78b 100644
--- a/host/frontend/vnc_server/Android.bp
+++ b/host/frontend/vnc_server/Android.bp
@@ -40,7 +40,6 @@
     static_libs: [
         "libcuttlefish_host_config",
         "libjpeg",
-        "libvsoc_gralloc",
         "libgflags",
     ],
     defaults: ["cuttlefish_host_only"],
diff --git a/host/frontend/vnc_server/simulated_hw_composer.cpp b/host/frontend/vnc_server/simulated_hw_composer.cpp
index 3b21e7b..001006b 100644
--- a/host/frontend/vnc_server/simulated_hw_composer.cpp
+++ b/host/frontend/vnc_server/simulated_hw_composer.cpp
@@ -18,10 +18,11 @@
 
 #include "common/vsoc/lib/typed_region_view.h"
 #include "host/frontend/vnc_server/vnc_utils.h"
-#include "host/vsoc/gralloc/gralloc_buffer_region.h"
+#include "host/libs/config/host_config.h"
+#include "host/vsoc/lib/gralloc_buffer_region_view.h"
 
 using cvd::vnc::SimulatedHWComposer;
-using vsoc::gralloc::GrallocBufferRegion;
+using vsoc::gralloc::GrallocBufferRegionView;
 
 SimulatedHWComposer::SimulatedHWComposer(BlackBoard* bb)
     :
@@ -30,7 +31,7 @@
 #endif
       bb_{bb},
       stripes_(kMaxQueueElements, &SimulatedHWComposer::EraseHalfOfElements) {
-        stripe_maker_ = std::thread(&SimulatedHWComposer::MakeStripes, this);
+  stripe_maker_ = std::thread(&SimulatedHWComposer::MakeStripes, this);
 }
 
 SimulatedHWComposer::~SimulatedHWComposer() {
@@ -80,7 +81,8 @@
         GetFBBroadcastRegionView()->WaitForNewFrameSince(&previous_seq_num);
 
     const auto* frame_start =
-        GrallocBufferRegion::GetInstance()->OffsetToBufferPtr(buffer_offset);
+        GrallocBufferRegionView::GetInstance(vsoc::GetDomain().c_str())
+            ->OffsetToBufferPtr(buffer_offset);
     raw_screen.assign(frame_start, frame_start + ScreenSizeInBytes());
 
     for (int i = 0; i < kNumStripes; ++i) {
diff --git a/host/vsoc/gralloc/gralloc_buffer_region.cpp b/host/vsoc/lib/gralloc_buffer_region_view.cpp
similarity index 60%
rename from host/vsoc/gralloc/gralloc_buffer_region.cpp
rename to host/vsoc/lib/gralloc_buffer_region_view.cpp
index 14b02e3..c2a8d87 100644
--- a/host/vsoc/gralloc/gralloc_buffer_region.cpp
+++ b/host/vsoc/lib/gralloc_buffer_region_view.cpp
@@ -14,21 +14,26 @@
  * limitations under the License.
  */
 
-#include "host/vsoc/gralloc/gralloc_buffer_region.h"
+#include "host/vsoc/lib/gralloc_buffer_region_view.h"
+
+#include <mutex>
 #include "glog/logging.h"
 
-using vsoc::gralloc::GrallocBufferRegion;
+using vsoc::gralloc::GrallocBufferRegionView;
 
 // static
-GrallocBufferRegion* GrallocBufferRegion::GetInstance() {
-  // TODO(jemoreira): Get the domain from somewhere
-  static GrallocBufferRegion instance(nullptr);
-  if (!instance.is_open_)
-    return nullptr;
-  return &instance;
+GrallocBufferRegionView* GrallocBufferRegionView::GetInstance(const char* domain) {
+  static std::mutex mutex;
+  static std::map<std::string, GrallocBufferRegionView*> gralloc_regions;
+
+  std::lock_guard<std::mutex> guard(mutex);
+  if (!gralloc_regions.count(domain)) {
+    gralloc_regions[domain] = new GrallocBufferRegionView(domain);
+  }
+  return gralloc_regions[domain];
 }
 
-uint8_t* GrallocBufferRegion::OffsetToBufferPtr(vsoc_reg_off_t offset) {
+uint8_t* GrallocBufferRegionView::OffsetToBufferPtr(vsoc_reg_off_t offset) {
   if (offset <= control_->region_desc().offset_of_region_data ||
       offset >= control_->region_size()) {
     LOG(FATAL)
@@ -39,6 +44,6 @@
   return region_offset_to_pointer<uint8_t>(offset);
 }
 
-GrallocBufferRegion::GrallocBufferRegion(char* domain) {
+GrallocBufferRegionView::GrallocBufferRegionView(const char* domain) {
   is_open_ = Open(domain);
 }
diff --git a/host/vsoc/gralloc/gralloc_buffer_region.h b/host/vsoc/lib/gralloc_buffer_region_view.h
similarity index 90%
rename from host/vsoc/gralloc/gralloc_buffer_region.h
rename to host/vsoc/lib/gralloc_buffer_region_view.h
index f339d3f..f2d10b4 100644
--- a/host/vsoc/gralloc/gralloc_buffer_region.h
+++ b/host/vsoc/lib/gralloc_buffer_region_view.h
@@ -17,7 +17,6 @@
 
 #include "common/vsoc/lib/typed_region_view.h"
 #include "common/vsoc/shm/gralloc_layout.h"
-#include "uapi/vsoc_shm.h"
 
 #include <string>
 
@@ -32,14 +31,14 @@
 // (which gets all other information from the guest side hwcomposer) and by the
 // VNC server (which uses only the frame buffer and gets the information it
 // needs from the framebuffer region).
-class GrallocBufferRegion
+class GrallocBufferRegionView
     : vsoc::TypedRegionView<vsoc::layout::gralloc::GrallocBufferLayout> {
  public:
-  static GrallocBufferRegion* GetInstance();
+  static GrallocBufferRegionView* GetInstance(const char* domain);
 
   uint8_t* OffsetToBufferPtr(vsoc_reg_off_t offset);
  protected:
-  GrallocBufferRegion(char* domain);
+  GrallocBufferRegionView(const char* domain);
   bool is_open_{};
 };