1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMPUTEPIPE_RUNNER_GRAPH_INCLUDE_LOCALPREBUILTGRAPH_H
16 #define COMPUTEPIPE_RUNNER_GRAPH_INCLUDE_LOCALPREBUILTGRAPH_H
17 
18 #include <condition_variable>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <shared_mutex>
23 #include <string>
24 #include <thread>
25 
26 #include "ClientConfig.pb.h"
27 #include "GrpcPrebuiltGraphService.grpc.pb.h"
28 #include "GrpcPrebuiltGraphService.pb.h"
29 #include "InputFrame.h"
30 #include "Options.pb.h"
31 #include "OutputConfig.pb.h"
32 #include "PrebuiltEngineInterface.h"
33 #include "PrebuiltGraph.h"
34 #include "RunnerComponent.h"
35 #include "StreamSetObserver.h"
36 #include "types/Status.h"
37 
38 namespace android {
39 namespace automotive {
40 namespace computepipe {
41 namespace graph {
42 
43 class GrpcGraph : public PrebuiltGraph, public StreamGraphInterface {
44 
45   public:
GrpcGraph()46     GrpcGraph() {}
47 
~GrpcGraph()48     virtual ~GrpcGraph() {}
49 
50     Status initialize(const std::string& address,
51                       std::weak_ptr<PrebuiltEngineInterface> engineInterface);
52 
53     // No copy or move constructors or operators are available.
54     GrpcGraph(const GrpcGraph&) = delete;
55     GrpcGraph& operator=(const GrpcGraph&) = delete;
56 
57     // Override RunnerComponent interface functions for applying configs,
58     // starting the graph and stopping the graph.
59     Status handleConfigPhase(const runner::ClientConfig& e) override;
60     Status handleExecutionPhase(const runner::RunnerEvent& e) override;
61     Status handleStopWithFlushPhase(const runner::RunnerEvent& e) override;
62     Status handleStopImmediatePhase(const runner::RunnerEvent& e) override;
63     Status handleResetPhase(const runner::RunnerEvent& e) override;
64 
GetGraphType()65     PrebuiltGraphType GetGraphType() const override {
66         return PrebuiltGraphType::REMOTE;
67     }
68 
69     PrebuiltGraphState GetGraphState() const override;
70     Status GetStatus() const override;
71     std::string GetErrorMessage() const override;
72 
73     // Gets the supported graph config options.
GetSupportedGraphConfigs()74     const proto::Options& GetSupportedGraphConfigs() const override {
75         return mGraphConfig;
76     }
77 
78     // Sets input stream data. The string is expected to be a serialized proto
79     // the definition of which is known to the graph.
80     Status SetInputStreamData(int streamIndex, int64_t timestamp,
81                               const std::string& streamData) override;
82 
83     // Sets pixel data to the specified input stream index.
84     Status SetInputStreamPixelData(int streamIndex, int64_t timestamp,
85                                    const runner::InputFrame& inputFrame) override;
86 
87     // Starts graph profiling at some point after starting the graph with profiling enabled.
88     Status StartGraphProfiling() override;
89 
90     // Stops graph profiling.
91     Status StopGraphProfiling() override;
92 
93     // Collects debugging and profiling information for the graph. The graph
94     // needs to be started with debugging enabled in order to get valid info.
95     std::string GetDebugInfo() override;
96 
97     // Stream Graph interface
getServiceStub()98     proto::GrpcGraphService::Stub* getServiceStub() override {
99         return mGraphStub.get();
100     }
101 
102     void dispatchPixelData(int streamId, int64_t timestamp_us,
103                            const runner::InputFrame& frame) override;
104 
105     void dispatchSerializedData(int streamId, int64_t timestamp_us,
106                                 std::string&& serialized_data) override;
107 
108     void dispatchGraphTerminationMessage(Status, std::string&&) override;
109   private:
110     mutable std::mutex mLock;
111 
112     PrebuiltGraphState mGraphState = PrebuiltGraphState::UNINITIALIZED;
113 
114     Status mStatus = Status::SUCCESS;
115 
116     std::string mErrorMessage = "";
117 
118     // Cached callback interface that is passed in from the runner.
119     std::weak_ptr<PrebuiltEngineInterface> mEngineInterface;
120 
121     // Cached graph config.
122     proto::Options mGraphConfig;
123 
124     std::unique_ptr<proto::GrpcGraphService::Stub> mGraphStub;
125 
126     std::unique_ptr<StreamSetObserver> mStreamSetObserver;
127 };
128 
129 }  // namespace graph
130 }  // namespace computepipe
131 }  // namespace automotive
132 }  // namespace android
133 
134 #endif  // #define COMPUTEPIPE_RUNNER_GRAPH_INCLUDE_LOCALPREBUILTGRAPH_H
135