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_CLIENT_INTERFACE_AIDLCLIENTIMPL_H_
16 #define COMPUTEPIPE_RUNNER_CLIENT_INTERFACE_AIDLCLIENTIMPL_H_
17 #include <aidl/android/automotive/computepipe/runner/BnPipeRunner.h>
18 #include <aidl/android/automotive/computepipe/runner/BnPipeDebugger.h>
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include "ClientEngineInterface.h"
25 #include "MemHandle.h"
26 #include "Options.pb.h"
27 #include "types/GraphState.h"
28 #include "types/Status.h"
29 
30 namespace android {
31 namespace automotive {
32 namespace computepipe {
33 namespace runner {
34 namespace client_interface {
35 namespace aidl_client {
36 
37 // RunnerInterface registers an IPipeRunner interface with computepipe router.
38 // RunnerInterface handles binder IPC calls and invokes appropriate callbacks.
39 class AidlClientImpl : public aidl::android::automotive::computepipe::runner::BnPipeRunner {
40   public:
AidlClientImpl(const proto::Options graphOptions,const std::shared_ptr<ClientEngineInterface> & engine)41     explicit AidlClientImpl(const proto::Options graphOptions,
42                             const std::shared_ptr<ClientEngineInterface>& engine)
43         : mGraphOptions(graphOptions), mEngine(engine) {
44     }
45 
~AidlClientImpl()46     ~AidlClientImpl() {
47     }
48 
49     Status dispatchPacketToClient(int32_t streamId, const std::shared_ptr<MemHandle>& packetHandle);
50     void setPipeDebugger(
51         const std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeDebugger>&
52         pipeDebugger);
53 
54     Status stateUpdateNotification(const GraphState newState);
55 
56     // Methods from android::automotive::computepipe::runner::BnPipeRunner
57     ndk::ScopedAStatus init(
58         const std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeStateCallback>&
59             stateCb) override;
60     ndk::ScopedAStatus getPipeDescriptor(
61         aidl::android::automotive::computepipe::runner::PipeDescriptor* _aidl_return) override;
62     ndk::ScopedAStatus setPipeInputSource(int32_t configId) override;
63     ndk::ScopedAStatus setPipeOffloadOptions(int32_t configId) override;
64     ndk::ScopedAStatus setPipeTermination(int32_t configId) override;
65     ndk::ScopedAStatus setPipeOutputConfig(
66         int32_t streamId, int32_t maxInFlightCount,
67         const std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeStream>& handler)
68         override;
69     ndk::ScopedAStatus applyPipeConfigs() override;
70     ndk::ScopedAStatus resetPipeConfigs() override;
71     ndk::ScopedAStatus startPipe() override;
72     ndk::ScopedAStatus stopPipe() override;
73     ndk::ScopedAStatus doneWithPacket(int32_t bufferId, int32_t streamId) override;
74 
75     ndk::ScopedAStatus getPipeDebugger(
76         std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeDebugger>*
77         _aidl_return) override;
78 
79     ndk::ScopedAStatus releaseRunner() override;
80 
81     void clientDied();
82 
83   private:
84     // Dispatch semantic data to client. Has copy semantics and does not expect
85     // client to invoke doneWithPacket.
86     Status DispatchSemanticData(int32_t streamId, const std::shared_ptr<MemHandle>& packetHandle);
87 
88     // Dispatch pixel data to client. Expects the client to invoke done with
89     // packet.
90     Status DispatchPixelData(int32_t streamId, const std::shared_ptr<MemHandle>& packetHandle);
91 
92     bool isClientInitDone();
93 
94     const proto::Options mGraphOptions;
95     std::shared_ptr<ClientEngineInterface> mEngine;
96 
97     // If value of mClientStateChangeCallback is null pointer, client has not
98     // invoked init.
99     std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeStateCallback>
100         mClientStateChangeCallback = nullptr;
101 
102     std::map<int, std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeStream>>
103         mPacketHandlers;
104 
105     std::shared_ptr<aidl::android::automotive::computepipe::runner::IPipeDebugger> mPipeDebugger =
106         nullptr;
107 };
108 
109 }  // namespace aidl_client
110 }  // namespace client_interface
111 }  // namespace runner
112 }  // namespace computepipe
113 }  // namespace automotive
114 }  // namespace android
115 
116 #endif  // COMPUTEPIPE_RUNNER_CLIENT_INTERFACE_AIDLCLIENTIMPL_H_
117