Split readFromParcel into fromBinder.

Before, readFromParcel figures out if the item in question is local or
remote and returns the appropriate object based on that. This avoids
wrapping a local object with a proxy when possible (and so, it prevents
parceling code locally in a process).

Bug: 111445392
Test: atest android.binder.cts
Change-Id: I87a2ba53f539e0fea08d4e27315b33607dedf927
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index e3f9eb2..6099550 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -551,6 +551,20 @@
   GenerateConstantDefinitions(out, defined_type);
   out << "\n";
 
+  out << "std::shared_ptr<" << clazz << "> " << clazz
+      << "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
+  out << data_clazz << "* data = static_cast<" << data_clazz
+      << "*>(AIBinder_getUserData(binder.get()));\n";
+  out << "if (data) {\n";
+  out.Indent();
+  out << "return data->instance;\n";
+  out.Dedent();
+  out << "}";
+  // If it is local, it is not an 'ndk' instance, and parceling will happen locally.
+  out << "return " << NdkFullClassName(defined_type, ClassNames::CLIENT)
+      << "::associate(binder);\n";
+  out << "}\n\n";
+
   out << "binder_status_t " << clazz << "::writeToParcel(AParcel* parcel, const std::shared_ptr<"
       << clazz << ">& instance) {\n";
   out.Indent();
@@ -565,18 +579,7 @@
   out << "::ndk::SpAIBinder binder;\n";
   out << "binder_status_t status = AParcel_readStrongBinder(parcel, binder.getR());\n";
   out << "if (status != STATUS_OK) return status;\n";
-  out << data_clazz << "* data = static_cast<" << data_clazz
-      << "*>(AIBinder_getUserData(binder.get()));\n";
-  out << "if (data) {\n";
-  out.Indent();
-  out << "*instance = data->instance;\n";
-  out.Dedent();
-  out << "} else {\n";
-  out.Indent();
-  out << "*instance = " << NdkFullClassName(defined_type, ClassNames::CLIENT)
-      << "::associate(binder);\n";
-  out.Dedent();
-  out << "}\n";
+  out << "*instance = " << clazz << "::fromBinder(binder);\n";
   out << "return STATUS_OK;\n";
   out.Dedent();
   out << "}\n";
@@ -743,6 +746,7 @@
         << ";\n";
   }
   out << "\n";
+  out << "static std::shared_ptr<" << clazz << "> fromBinder(const ::ndk::SpAIBinder& binder);";
   out << "static binder_status_t writeToParcel(AParcel* parcel, const std::shared_ptr<" << clazz
       << ">& instance);";
   out << "\n";