• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GRPC_GRAPH_H
16 #define COMPUTEPIPE_RUNNER_GRAPH_GRPC_GRAPH_H
17 
18 #include <functional>
19 #include <shared_mutex>
20 #include <string>
21 
22 #include "ClientConfig.pb.h"
23 #include "InputFrame.h"
24 #include "Options.pb.h"
25 #include "PrebuiltEngineInterface.h"
26 #include "PrebuiltGraph.h"
27 #include "RunnerComponent.h"
28 #include "types/Status.h"
29 
30 namespace android {
31 namespace automotive {
32 namespace computepipe {
33 namespace graph {
34 
35 class LocalPrebuiltGraph : public PrebuiltGraph {
36 
37   private:
38     // Private constructor
LocalPrebuiltGraph()39     LocalPrebuiltGraph() {
40     }
41 
42   public:
43     ~LocalPrebuiltGraph();
44 
45     // No copy or move constructors or operators are available.
46     LocalPrebuiltGraph(const LocalPrebuiltGraph&) = delete;
47     LocalPrebuiltGraph& operator=(const LocalPrebuiltGraph&) = delete;
48 
49     // Override RunnerComponent interface functions for applying configs,
50     // starting the graph and stopping the graph.
51     Status handleConfigPhase(const runner::ClientConfig& e) override;
52     Status handleExecutionPhase(const runner::RunnerEvent& e) override;
53     Status handleStopWithFlushPhase(const runner::RunnerEvent& e) override;
54     Status handleStopImmediatePhase(const runner::RunnerEvent& e) override;
55     Status handleResetPhase(const runner::RunnerEvent& e) override;
56 
57     static LocalPrebuiltGraph* GetPrebuiltGraphFromLibrary(
58         const std::string& prebuiltLib, std::weak_ptr<PrebuiltEngineInterface> engineInterface);
59 
GetGraphType()60     PrebuiltGraphType GetGraphType() const override {
61         return PrebuiltGraphType::LOCAL;
62     }
63 
GetGraphState()64     PrebuiltGraphState GetGraphState() const override {
65         return mGraphState;
66     }
67 
68     Status GetStatus() const override;
69 
70     std::string GetErrorMessage() const override;
71 
72     // Gets the supported graph config options.
GetSupportedGraphConfigs()73     const proto::Options& GetSupportedGraphConfigs() const override {
74         return mGraphConfig;
75     }
76 
77     // Sets input stream data. The string is expected to be a serialized proto
78     // the definition of which is known to the graph.
79     Status SetInputStreamData(int streamIndex, int64_t timestamp,
80                               const std::string& streamData) override;
81 
82     // Sets pixel data to the specified input stream index.
83     Status SetInputStreamPixelData(int streamIndex, int64_t timestamp,
84                                    const runner::InputFrame& inputFrame) override;
85 
86     Status StartGraphProfiling() override;
87 
88     Status StopGraphProfiling() override;
89 
90     // Collects debugging and profiling information for the graph. The graph
91     // needs to be started with debugging enabled in order to get valid info.
92     std::string GetDebugInfo() override;
93 
94   private:
95     // Starts the graph execution.
96     Status StartGraphExecution(bool debuggingEnabled);
97 
98     // Stops the graph execution.
99     Status StopGraphExecution(bool flushOutputFrames);
100 
101     // Callback functions. The class has a C++ function callback interface while it deals with pure
102     // C functions underneath that do not have object context. We need to have these static
103     // functions that need to be passed to the C interface.
104     static void OutputPixelStreamCallbackFunction(void* cookie, int streamIndex, int64_t timestamp,
105                                                   const uint8_t* pixels, int width, int height,
106                                                   int step, int format);
107     static void OutputStreamCallbackFunction(void* cookie, int streamIndex, int64_t timestamp,
108                                              const unsigned char* data, size_t dataSize);
109     static void GraphTerminationCallbackFunction(void* cookie,
110                                                  const unsigned char* terminationMessage,
111                                                  size_t terminationMessageSize);
112 
113     // Cached callback interface that is passed in from the runner.
114     std::weak_ptr<PrebuiltEngineInterface> mEngineInterface;
115 
116     static std::mutex mCreationMutex;
117     static LocalPrebuiltGraph* mPrebuiltGraphInstance;
118 
119     // Even though mutexes are generally preferred over atomics, the only varialble in this class
120     // that changes after initialization is graph state and that is the only vairable that needs
121     // to be guarded. The prebuilt is internally assumed to be thread safe, so that concurrent
122     // calls into the library will automatically be handled in a thread safe manner by the it.
123     std::atomic<PrebuiltGraphState> mGraphState = PrebuiltGraphState::UNINITIALIZED;
124 
125     // Dynamic library handle
126     void* mHandle;
127 
128     // Repeated function calls need not be made to get the graph version and the config is this is
129     // constant through the operation of the graph. These values are just cached as strings.
130     std::string mGraphVersion;
131     proto::Options mGraphConfig;
132 
133     // Cached functions from the dynamic library.
134     void* mFnGetErrorCode;
135     void* mFnGetErrorMessage;
136     void* mFnUpdateGraphConfig;
137     void* mFnResetGraph;
138     void* mFnSetInputStreamData;
139     void* mFnSetInputStreamPixelData;
140     void* mFnSetOutputStreamCallback;
141     void* mFnSetOutputPixelStreamCallback;
142     void* mFnSetGraphTerminationCallback;
143     void* mFnStartGraphExecution;
144     void* mFnStopGraphExecution;
145     void* mFnStartGraphProfiling;
146     void* mFnStopGraphProfiling;
147     void* mFnGetDebugInfo;
148 };
149 
150 }  // namespace graph
151 }  // namespace computepipe
152 }  // namespace automotive
153 }  // namespace android
154 
155 #endif  // #define COMPUTEPIPE_RUNNER_GRAPH_GRPC_GRAPH_H
156