1 // Copyright (C) 2019 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_INCLUDE_RUNNERCOMPONENT_H_
16 #define COMPUTEPIPE_RUNNER_INCLUDE_RUNNERCOMPONENT_H_
17 #include <map>
18 #include <memory>
19 #include <string>
20 
21 #include "types/Status.h"
22 #include "ProfilingType.pb.h"
23 
24 namespace android {
25 namespace automotive {
26 namespace computepipe {
27 namespace runner {
28 
29 class RunnerComponentInterface;
30 
31 /**
32  * Represents the state of the config phase a particular client config is in
33  */
34 enum PhaseState {
35     ENTRY = 0,
36     TRANSITION_COMPLETE,
37     ABORTED,
38 };
39 
40 /**
41  * RunnerEvent represents an event corresponding to a runner phase
42  * Along with start, abort or transition complete query methods.
43  */
44 class RunnerEvent {
45   public:
46     /* Is this a notification to enter the phase */
47     virtual bool isPhaseEntry() const;
48     /* Is this a notification that all components have transitioned to the phase */
49     virtual bool isTransitionComplete() const;
50     /* Is this a notification to abort the transition to the started phase */
51     virtual bool isAborted() const;
52     /* Dispatch event to component */
53     virtual Status dispatchToComponent(const std::shared_ptr<RunnerComponentInterface>& iface) = 0;
54     /* Destructor */
55     virtual ~RunnerEvent() = default;
56 };
57 
58 /**
59  * Configuration that gets emitted once client has completely specified config
60  * options
61  */
62 class ClientConfig : public RunnerEvent {
63   public:
64     static const int kInvalidId = -1;
65 
66     /**
67      * Override relevant methods from RunnerEvent
68      */
isPhaseEntry()69     bool isPhaseEntry() const override {
70         return mState == ENTRY;
71     }
isTransitionComplete()72     bool isTransitionComplete() const override {
73         return mState == TRANSITION_COMPLETE;
74     }
isAborted()75     bool isAborted() const override {
76         return mState == ABORTED;
77     }
78 
79     Status dispatchToComponent(const std::shared_ptr<RunnerComponentInterface>& iface) override;
80     /**
81      * Accessor methods
82      */
83     Status getInputConfigId(int* outId) const;
84     Status getOffloadId(int* outId) const;
85     Status getTerminationId(int* outId) const;
86     Status getOptionalConfigs(std::string& outOptional) const;
87     Status getOutputStreamConfigs(std::map<int, int>& outputConfig) const;
88     Status getProfilingType(proto::ProfilingType* profilingType) const;
89     std::string getSerializedClientConfig() const;
90 
91     /**
92      * Constructors
93      */
94     ClientConfig& operator=(ClientConfig&& r) {
95         mInputConfigId = r.mInputConfigId;
96         mTerminationId = r.mTerminationId;
97         mOffloadId = r.mOffloadId;
98         mOptionalConfigs = std::move(r.mOptionalConfigs);
99         mOutputConfigs = std::move(r.mOutputConfigs);
100         return *this;
101     }
ClientConfig(ClientConfig && c)102     ClientConfig(ClientConfig&& c) {
103         *this = std::move(c);
104     }
105     ClientConfig(int inputConfigId, int offload, int termination, std::map<int, int>& outputConfigs,
106                  proto::ProfilingType profilingType, std::string opt = "")
mInputConfigId(inputConfigId)107         : mInputConfigId(inputConfigId),
108           mOutputConfigs(outputConfigs),
109           mTerminationId(termination),
110           mOffloadId(offload),
111           mProfilingType(profilingType),
112           mOptionalConfigs(opt) {
113     }
114 
setPhaseState(PhaseState state)115     void setPhaseState(PhaseState state) {
116         mState = state;
117     }
118 
119   private:
120     /**
121      * input streamd id from the graph descriptor options
122      */
123     int mInputConfigId = kInvalidId;
124     /**
125      * Options for different output streams
126      */
127     std::map<int, int> mOutputConfigs;
128     /**
129      * Termination Option
130      */
131     int mTerminationId = kInvalidId;
132     /**
133      * offload option
134      */
135     int mOffloadId = kInvalidId;
136 
137     proto::ProfilingType mProfilingType = proto::ProfilingType::DISABLED;
138     /**
139      * serialized optional config
140      */
141     std::string mOptionalConfigs = "";
142     /**
143      * The state of the client config corresponding
144      * to entry, transition complete or aborted
145      */
146     PhaseState mState = ENTRY;
147 };
148 
149 /**
150  * A component of the Runner Engine implements this interface to receive
151  * RunnerEvents.
152  * A SUCCESS return value indicates the component has handled the particular
153  * event. A failure return value will result in a subsequent abort call
154  * that should be ignored by the component that reported failure.
155  */
156 class RunnerComponentInterface {
157   public:
158     /* handle a ConfigPhase related event notification from Runner Engine */
159     virtual Status handleConfigPhase(const ClientConfig& e);
160     /* handle execution phase notification from Runner Engine */
161     virtual Status handleExecutionPhase(const RunnerEvent& e);
162     /* handle a stop with flushing semantics phase notification from the engine */
163     virtual Status handleStopWithFlushPhase(const RunnerEvent& e);
164     /* handle an immediate stop phase notification from the engine */
165     virtual Status handleStopImmediatePhase(const RunnerEvent& e);
166     /* handle an engine notification to return to reset state */
167     virtual Status handleResetPhase(const RunnerEvent& e);
168     virtual ~RunnerComponentInterface() = default;
169 };
170 
171 }  // namespace runner
172 }  // namespace computepipe
173 }  // namespace automotive
174 }  // namespace android
175 #endif  // COMPUTEPIPE_RUNNER_INCLUDE_RUNNERCOMPONENT_H_
176