Add computepipe example implementation

Fix: 148017613

Change-Id: I0b579cd5ce45deff0e723634fdf4a09513715e83
diff --git a/computepipe/example/Android.bp b/computepipe/example/Android.bp
new file mode 100644
index 0000000..4314838
--- /dev/null
+++ b/computepipe/example/Android.bp
@@ -0,0 +1,151 @@
+// Copyright 2020 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.
+
+cc_binary {
+    name: "computepipe_face_runner",
+    srcs: [
+        "Runner.cpp",
+    ],
+    static_libs: [
+        "libcomputepipeprotos",
+        "computepipe_prebuilt_graph",
+        "computepipe_client_interface",
+    ],
+    shared_libs: [
+        "computepipe_runner_engine",
+        "libprotobuf-cpp-lite",
+        "libbase",
+        "libbinder_ndk",
+        "android.automotive.computepipe.runner-ndk_platform",
+        "android.automotive.computepipe.registry-ndk_platform",
+        "liblog",
+        "libutils",
+        "libbase",
+        "libdl",
+        "libfacegraph",
+    ],
+    cflags: ["-DLOG_TAG=\"ComputepipeRunner\""] + [
+        "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
+        "-Wall",
+        "-Werror",
+        "-Wunused",
+        "-Wunreachable-code",
+        "-Wthread-safety",
+    ],
+    header_libs: [
+        "computepipe_runner_includes",
+    ],
+    include_dirs: [
+        "packages/services/Car/computepipe",
+    ],
+}
+
+cc_prebuilt_library_shared {
+    name: "libfacegraph",
+    strip: {
+        keep_symbols: true,
+    },
+    target: {
+        android_arm64: {
+            srcs: ["lib_arm64/libfacegraph.so"],
+        },
+        android_arm: {
+            srcs: ["lib_arm/libfacegraph.so"],
+        },
+        android_x86_64: {
+            srcs: ["lib_x86_64/libfacegraph.so"],
+        },
+        android_x86: {
+            srcs: ["lib_x86_64/libfacegraph.so"],
+        },
+    },
+    shared_libs: [
+        "libc",
+        "libdl",
+        "libEGL",
+        "libGLESv2",
+        "liblog",
+        "libm",
+        "libz",
+    ],
+}
+
+cc_binary {
+    name: "computepipe_face_tracker",
+    srcs: [
+        "FaceTracker.cpp",
+	"ClientSvc.cpp",
+    ],
+    vendor: true,
+    static_libs: [
+        "libcomputepipefaceproto",
+    ],
+    shared_libs: [
+        "liblog",
+        "libbase",
+        "libbinder_ndk",
+        "libutils",
+        "android.hardware.automotive.occupant_awareness-ndk_platform",
+        "libprotobuf-cpp-lite",
+        "android.automotive.computepipe.runner-ndk_platform",
+        "android.automotive.computepipe.registry-ndk_platform",
+    ],
+    cflags: ["-DLOG_TAG=\"FaceTrackerClient\""] + [
+        "-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
+        "-Wall",
+        "-Werror",
+        "-Wunused",
+        "-Wunreachable-code",
+        "-Wthread-safety",
+    ],
+}
+
+cc_defaults {
+    name: "libcomputepipeface-defaults",
+
+    proto: {
+        export_proto_headers: true,
+        include_dirs: ["external/protobuf/src"],
+    },
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-unused-parameter",
+    ],
+
+    srcs: [
+        "*.proto",
+    ],
+}
+
+cc_library {
+    name: "libcomputepipefaceproto",
+    defaults: ["libcomputepipeface-defaults"],
+    host_supported: false,
+    vendor_available: true,
+    target: {
+        android: {
+            proto: {
+                type: "lite",
+            },
+            static_libs: [
+                "libprotobuf-cpp-lite",
+            ],
+            shared: {
+                enabled: false,
+            },
+        },
+    },
+}
diff --git a/computepipe/example/ClientSvc.cpp b/computepipe/example/ClientSvc.cpp
new file mode 100644
index 0000000..59af7df
--- /dev/null
+++ b/computepipe/example/ClientSvc.cpp
@@ -0,0 +1,47 @@
+// Copyright 2020 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.
+
+#include <android-base/logging.h>
+#include <android/binder_process.h>
+#include <utils/Log.h>
+
+#include "FaceTracker.h"
+
+using android::automotive::computepipe::FaceTracker;
+
+namespace {
+
+void terminationCallback(bool error, std::string errorMsg) {
+    if (error) {
+        LOG(ERROR) << errorMsg;
+        exit(2);
+    }
+    LOG(ERROR) << "Test completed";
+    exit(0);
+}
+}  // namespace
+int main(int /* argc */, char** /* argv */) {
+    std::function<void(bool, std::string)> cb = terminationCallback;
+    FaceTracker client;
+
+    ABinderProcess_startThreadPool();
+    ndk::ScopedAStatus status = client.init(std::move(cb));
+    if (!status.isOk()) {
+        LOG(ERROR) << "Unable to init client connection";
+        return -1;
+    }
+    ABinderProcess_joinThreadPool();
+
+    return 0;
+}
diff --git a/computepipe/example/FaceOutput.proto b/computepipe/example/FaceOutput.proto
new file mode 100644
index 0000000..59487b1
--- /dev/null
+++ b/computepipe/example/FaceOutput.proto
@@ -0,0 +1,21 @@
+syntax = "proto2";
+
+package android.automotive.computepipe.example;
+
+message BoundingBox {
+  optional int32 top_x = 1;
+  optional int32 top_y = 2;
+  optional int32 width = 3;
+  optional int32 height = 4;
+};
+
+message Pose {
+  optional float pan  = 1;
+  optional float tilt = 2;
+}
+
+message FaceOutput {
+  optional BoundingBox box = 1;
+
+  optional Pose pose = 2;
+};
diff --git a/computepipe/example/FaceTracker.cpp b/computepipe/example/FaceTracker.cpp
new file mode 100644
index 0000000..47e237b
--- /dev/null
+++ b/computepipe/example/FaceTracker.cpp
@@ -0,0 +1,168 @@
+// Copyright 2020 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.
+
+#include "FaceTracker.h"
+
+#include <android-base/logging.h>
+#include <android/binder_manager.h>
+#include <utils/Log.h>
+
+#include <memory>
+#include <mutex>
+#include <thread>
+
+namespace android {
+namespace automotive {
+namespace computepipe {
+
+using ::android::automotive::computepipe::example::FaceOutput;
+namespace {
+constexpr char kReigstryInterface[] = "router";
+constexpr char kGraphName[] = "Face Tracker Graph";
+}  // namespace
+
+/**
+ * RemoteState monitor
+ */
+PipeState RemoteState::GetCurrentState() {
+    std::unique_lock<std::mutex> lock(mStateLock);
+    mWait.wait(lock, [this]() { return hasChanged; });
+    hasChanged = false;
+    return mState;
+}
+
+void RemoteState::UpdateCurrentState(const PipeState& state) {
+    std::lock_guard<std::mutex> lock(mStateLock);
+    mState = state;
+    if (mState == PipeState::ERR_HALT) {
+        mTerminationCb(true, "Received error from runner");
+    } else if (mState == PipeState::DONE) {
+        mTerminationCb(false, "");
+    } else {
+        hasChanged = true;
+        mWait.notify_all();
+    }
+}
+
+RemoteState::RemoteState(std::function<void(bool, std::string)>& cb) : mTerminationCb(cb) {
+}
+
+/**
+ * StateCallback methods
+ */
+StateCallback::StateCallback(std::shared_ptr<RemoteState> s) : mStateTracker(s) {
+}
+
+ndk::ScopedAStatus StateCallback::handleState(PipeState state) {
+    mStateTracker->UpdateCurrentState(state);
+    return ndk::ScopedAStatus::ok();
+}
+
+/**
+ * FaceTracker methods
+ */
+ndk::ScopedAStatus FaceTracker::init(std::function<void(bool, std::string)>&& cb) {
+    auto termination = cb;
+    mRemoteState = std::make_shared<RemoteState>(termination);
+    std::string instanceName = std::string() + IPipeQuery::descriptor + "/" + kReigstryInterface;
+
+    ndk::SpAIBinder binder(AServiceManager_getService(instanceName.c_str()));
+    CHECK(binder.get());
+
+    std::shared_ptr<IPipeQuery> queryService = IPipeQuery::fromBinder(binder);
+    mClientInfo = ndk::SharedRefBase::make<ClientInfo>();
+    ndk::ScopedAStatus status = queryService->getPipeRunner(kGraphName, mClientInfo, &mPipeRunner);
+    if (!status.isOk()) {
+        LOG(ERROR) << "Failed to get handle to runner";
+        return status;
+    }
+    mStreamCallback = ndk::SharedRefBase::make<StreamCallback>();
+    mStateCallback = ndk::SharedRefBase::make<StateCallback>(mRemoteState);
+    return setupConfig();
+}
+
+ndk::ScopedAStatus FaceTracker::setupConfig() {
+    ndk::ScopedAStatus status = mPipeRunner->init(mStateCallback);
+    if (!status.isOk()) {
+        LOG(ERROR) << "Failed to init runner";
+        return status;
+    }
+    status = mPipeRunner->setPipeInputSource(0);
+    if (!status.isOk()) {
+        LOG(ERROR) << "Failed to set pipe input config";
+        return status;
+    }
+    status = mPipeRunner->setPipeOutputConfig(0, 10, mStreamCallback);
+    if (!status.isOk()) {
+        LOG(ERROR) << "Failed to set pipe output config";
+        return status;
+    }
+    status = mPipeRunner->applyPipeConfigs();
+    if (!status.isOk()) {
+        LOG(ERROR) << "Failed to set apply configs";
+        return status;
+    }
+    std::thread t(&FaceTracker::start, this);
+    t.detach();
+    return ndk::ScopedAStatus::ok();
+}
+
+void FaceTracker::start() {
+    PipeState state = mRemoteState->GetCurrentState();
+    CHECK(state == PipeState::CONFIG_DONE);
+    ndk::ScopedAStatus status = mPipeRunner->startPipe();
+    CHECK(status.isOk());
+    state = mRemoteState->GetCurrentState();
+    CHECK(state == PipeState::RUNNING);
+}
+
+void FaceTracker::stop() {
+    ndk::ScopedAStatus status = mPipeRunner->startPipe();
+    CHECK(status.isOk());
+}
+
+/**
+ * Stream Callback implementation
+ */
+
+ndk::ScopedAStatus StreamCallback::deliverPacket(const PacketDescriptor& in_packet) {
+    std::string output(in_packet.data.begin(), in_packet.data.end());
+
+    FaceOutput faceData;
+    faceData.ParseFromString(output);
+
+    BoundingBox currentBox = faceData.box();
+
+    if (!faceData.has_box()) {
+        mLastBox = BoundingBox();
+        return ndk::ScopedAStatus::ok();
+    }
+
+    if (!mLastBox.has_top_x()) {
+        mLastBox = currentBox;
+        return ndk::ScopedAStatus::ok();
+    }
+
+    if (currentBox.top_x() > mLastBox.top_x() + 1) {
+        LOG(ERROR) << "Face moving left";
+    } else if (currentBox.top_x() + 1 < mLastBox.top_x()) {
+        LOG(ERROR) << "Face moving right";
+    }
+    mLastBox = currentBox;
+    return ndk::ScopedAStatus::ok();
+}
+
+}  // namespace computepipe
+}  // namespace automotive
+}  // namespace android
diff --git a/computepipe/example/FaceTracker.h b/computepipe/example/FaceTracker.h
new file mode 100644
index 0000000..ea6ba8b
--- /dev/null
+++ b/computepipe/example/FaceTracker.h
@@ -0,0 +1,106 @@
+// Copyright 2020 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.
+#ifndef COMPUTEPIPE_EXAMPLE_FACE_TRACKER_H
+#define COMPUTEPIPE_EXAMPLE_FACE_TRACKER_H
+
+#include <aidl/android/automotive/computepipe/registry/BnClientInfo.h>
+#include <aidl/android/automotive/computepipe/registry/IPipeQuery.h>
+#include <aidl/android/automotive/computepipe/registry/IPipeRegistration.h>
+#include <aidl/android/automotive/computepipe/runner/BnPipeStateCallback.h>
+#include <aidl/android/automotive/computepipe/runner/BnPipeStream.h>
+#include <aidl/android/automotive/computepipe/runner/PipeState.h>
+
+#include <condition_variable>
+#include <iostream>
+#include <memory>
+
+#include "FaceOutput.pb.h"
+
+namespace android {
+namespace automotive {
+namespace computepipe {
+
+using ::aidl::android::automotive::computepipe::registry::BnClientInfo;
+using ::aidl::android::automotive::computepipe::registry::IPipeQuery;
+using ::aidl::android::automotive::computepipe::runner::BnPipeStateCallback;
+using ::aidl::android::automotive::computepipe::runner::BnPipeStream;
+using ::aidl::android::automotive::computepipe::runner::IPipeRunner;
+using ::aidl::android::automotive::computepipe::runner::IPipeStream;
+using ::aidl::android::automotive::computepipe::runner::PacketDescriptor;
+using ::aidl::android::automotive::computepipe::runner::PipeState;
+using ::android::automotive::computepipe::example::BoundingBox;
+
+class RemoteState {
+  public:
+    explicit RemoteState(std::function<void(bool, std::string)>& cb);
+    PipeState GetCurrentState();
+    void UpdateCurrentState(const PipeState& state);
+
+  private:
+    bool hasChanged = false;
+    PipeState mState = PipeState::RESET;
+    std::mutex mStateLock;
+    std::condition_variable mWait;
+    std::function<void(bool, std::string)> mTerminationCb;
+};
+
+class ClientInfo : public BnClientInfo {
+  public:
+    ndk::ScopedAStatus getClientId(int32_t* _aidl_return) {
+        if (_aidl_return) {
+            *_aidl_return = 0;
+            return ndk::ScopedAStatus::ok();
+        }
+        return ndk::ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED);
+    }
+};
+
+class StreamCallback : public BnPipeStream {
+  public:
+    StreamCallback() = default;
+    ndk::ScopedAStatus deliverPacket(const PacketDescriptor& in_packet) override;
+
+  private:
+    BoundingBox mLastBox;
+};
+
+class StateCallback : public BnPipeStateCallback {
+  public:
+    explicit StateCallback(std::shared_ptr<RemoteState> s);
+    ndk::ScopedAStatus handleState(PipeState state) override;
+
+  private:
+    std::shared_ptr<RemoteState> mStateTracker = nullptr;
+};
+
+class FaceTracker {
+  public:
+    FaceTracker() = default;
+    ndk::ScopedAStatus init(std::function<void(bool, std::string)>&& termination);
+    void start();
+    void stop();
+
+  private:
+    ndk::ScopedAStatus setupConfig();
+    std::shared_ptr<IPipeRunner> mPipeRunner = nullptr;
+    std::shared_ptr<ClientInfo> mClientInfo = nullptr;
+    std::shared_ptr<StreamCallback> mStreamCallback = nullptr;
+    std::shared_ptr<StateCallback> mStateCallback = nullptr;
+    std::shared_ptr<RemoteState> mRemoteState = nullptr;
+};
+
+}  // namespace computepipe
+}  // namespace automotive
+}  // namespace android
+#endif
diff --git a/computepipe/example/Runner.cpp b/computepipe/example/Runner.cpp
new file mode 100644
index 0000000..b46345e
--- /dev/null
+++ b/computepipe/example/Runner.cpp
@@ -0,0 +1,75 @@
+// Copyright 2020 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.
+#include <android-base/logging.h>
+#include <android/binder_process.h>
+#include <utils/Errors.h>
+#include <utils/Log.h>
+
+#include <iostream>
+#include <memory>
+#include <thread>
+
+#include "ClientConfig.pb.h"
+#include "ClientInterface.h"
+#include "MemHandle.h"
+#include "Options.pb.h"
+#include "PrebuiltGraph.h"
+#include "RunnerEngine.h"
+#include "types/Status.h"
+
+using android::automotive::computepipe::Status;
+using android::automotive::computepipe::graph::PrebuiltGraph;
+using android::automotive::computepipe::proto::ClientConfig;
+using android::automotive::computepipe::proto::Options;
+using android::automotive::computepipe::runner::client_interface::ClientInterface;
+using android::automotive::computepipe::runner::client_interface::ClientInterfaceFactory;
+using android::automotive::computepipe::runner::engine::RunnerEngine;
+using android::automotive::computepipe::runner::engine::RunnerEngineFactory;
+
+namespace {
+RunnerEngineFactory sEngineFactory;
+ClientInterfaceFactory sClientFactory;
+}  // namespace
+void terminate(bool isError, std::string msg) {
+    if (isError) {
+        LOG(ERROR) << "Error msg " << msg;
+        exit(2);
+    }
+    LOG(ERROR) << "Test complete";
+    exit(0);
+}
+
+int main(int /* argc */, char** /* argv */) {
+    std::shared_ptr<RunnerEngine> engine =
+        sEngineFactory.createRunnerEngine(RunnerEngineFactory::kDefault, "");
+
+    std::unique_ptr<PrebuiltGraph> graph;
+    graph.reset(PrebuiltGraph::GetPrebuiltGraphFromLibrary("libfacegraph.so", engine));
+
+    Options options = graph->GetSupportedGraphConfigs();
+    engine->setPrebuiltGraph(std::move(graph));
+
+    std::function<void(bool, std::string)> cb = terminate;
+    std::unique_ptr<ClientInterface> client =
+        sClientFactory.createClientInterface("aidl", options, engine);
+    if (!client) {
+        std::cerr << "Unable to allocate client";
+        return -1;
+    }
+    engine->setClientInterface(std::move(client));
+    ABinderProcess_startThreadPool();
+    engine->activate();
+    ABinderProcess_joinThreadPool();
+    return 0;
+}
diff --git a/computepipe/example/lib_arm/libfacegraph.so b/computepipe/example/lib_arm/libfacegraph.so
new file mode 100644
index 0000000..3988416
--- /dev/null
+++ b/computepipe/example/lib_arm/libfacegraph.so
Binary files differ
diff --git a/computepipe/example/lib_arm64/libfacegraph.so b/computepipe/example/lib_arm64/libfacegraph.so
new file mode 100644
index 0000000..c890bde
--- /dev/null
+++ b/computepipe/example/lib_arm64/libfacegraph.so
Binary files differ
diff --git a/computepipe/example/lib_x86/libfacegraph.so b/computepipe/example/lib_x86/libfacegraph.so
new file mode 100644
index 0000000..f77fe74
--- /dev/null
+++ b/computepipe/example/lib_x86/libfacegraph.so
Binary files differ
diff --git a/computepipe/example/lib_x86_64/libfacegraph.so b/computepipe/example/lib_x86_64/libfacegraph.so
new file mode 100644
index 0000000..cc52bc2
--- /dev/null
+++ b/computepipe/example/lib_x86_64/libfacegraph.so
Binary files differ