1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CODEC2_HIDL_V1_0_UTILS_COMPONENT_H
18 #define CODEC2_HIDL_V1_0_UTILS_COMPONENT_H
19 
20 #include <codec2/hidl/1.0/ComponentInterface.h>
21 #include <codec2/hidl/1.0/Configurable.h>
22 #include <codec2/hidl/1.0/types.h>
23 
24 #include <android/hardware/media/bufferpool/2.0/IClientManager.h>
25 #include <android/hardware/media/c2/1.0/IComponent.h>
26 #include <android/hardware/media/c2/1.0/IComponentInterface.h>
27 #include <android/hardware/media/c2/1.0/IComponentListener.h>
28 #include <android/hardware/media/c2/1.0/IComponentStore.h>
29 #include <android/hardware/media/c2/1.0/IInputSink.h>
30 #include <hidl/Status.h>
31 #include <hwbinder/IBinder.h>
32 
33 #include <C2Component.h>
34 #include <C2Buffer.h>
35 #include <C2.h>
36 
37 #include <map>
38 #include <memory>
39 #include <mutex>
40 
41 namespace android {
42 namespace hardware {
43 namespace media {
44 namespace c2 {
45 namespace V1_0 {
46 namespace utils {
47 
48 using ::android::hardware::hidl_array;
49 using ::android::hardware::hidl_memory;
50 using ::android::hardware::hidl_string;
51 using ::android::hardware::hidl_vec;
52 using ::android::hardware::Return;
53 using ::android::hardware::Void;
54 using ::android::hardware::IBinder;
55 using ::android::sp;
56 using ::android::wp;
57 
58 struct ComponentStore;
59 
60 struct Component : public IComponent,
61                    public std::enable_shared_from_this<Component> {
62     Component(
63             const std::shared_ptr<C2Component>&,
64             const sp<IComponentListener>& listener,
65             const sp<ComponentStore>& store,
66             const sp<::android::hardware::media::bufferpool::V2_0::
67                 IClientManager>& clientPoolManager);
68     c2_status_t status() const;
69 
70     typedef ::android::hardware::graphics::bufferqueue::V1_0::
71             IGraphicBufferProducer HGraphicBufferProducer1;
72     typedef ::android::hardware::graphics::bufferqueue::V2_0::
73             IGraphicBufferProducer HGraphicBufferProducer2;
74 
75     // Methods from IComponent follow.
76     virtual Return<Status> queue(const WorkBundle& workBundle) override;
77     virtual Return<void> flush(flush_cb _hidl_cb) override;
78     virtual Return<Status> drain(bool withEos) override;
79     virtual Return<Status> setOutputSurface(
80             uint64_t blockPoolId,
81             const sp<HGraphicBufferProducer2>& surface) override;
82     virtual Return<void> connectToInputSurface(
83             const sp<IInputSurface>& inputSurface,
84             connectToInputSurface_cb _hidl_cb) override;
85     virtual Return<void> connectToOmxInputSurface(
86             const sp<HGraphicBufferProducer1>& producer,
87             const sp<::android::hardware::media::omx::V1_0::
88             IGraphicBufferSource>& source,
89             connectToOmxInputSurface_cb _hidl_cb) override;
90     virtual Return<Status> disconnectFromInputSurface() override;
91     virtual Return<void> createBlockPool(
92             uint32_t allocatorId,
93             createBlockPool_cb _hidl_cb) override;
94     virtual Return<Status> destroyBlockPool(uint64_t blockPoolId) override;
95     virtual Return<Status> start() override;
96     virtual Return<Status> stop() override;
97     virtual Return<Status> reset() override;
98     virtual Return<Status> release() override;
99     virtual Return<sp<IComponentInterface>> getInterface() override;
100     virtual Return<sp<IInputSink>> asInputSink() override;
101 
102     // Returns a C2Component associated to the given sink if the sink is indeed
103     // a local component. Returns nullptr otherwise.
104     //
105     // This function is used by InputSurface::connect().
106     static std::shared_ptr<C2Component> findLocalComponent(
107             const sp<IInputSink>& sink);
108 
109 protected:
110     c2_status_t mInit;
111     std::shared_ptr<C2Component> mComponent;
112     sp<ComponentInterface> mInterface;
113     sp<IComponentListener> mListener;
114     sp<ComponentStore> mStore;
115     ::android::hardware::media::c2::V1_0::utils::DefaultBufferPoolSender
116             mBufferPoolSender;
117 
118     struct Sink;
119     std::mutex mSinkMutex;
120     sp<Sink> mSink;
121 
122     std::mutex mBlockPoolsMutex;
123     // This map keeps C2BlockPool objects that are created by createBlockPool()
124     // alive. These C2BlockPool objects can be deleted by calling
125     // destroyBlockPool(), reset() or release(), or by destroying the component.
126     std::map<uint64_t, std::shared_ptr<C2BlockPool>> mBlockPools;
127 
128     void initListener(const sp<Component>& self);
129 
130     virtual ~Component() override;
131 
132     friend struct ComponentStore;
133 
134     struct Listener;
135 };
136 
137 }  // namespace utils
138 }  // namespace V1_0
139 }  // namespace c2
140 }  // namespace media
141 }  // namespace hardware
142 }  // namespace android
143 
144 #endif  // CODEC2_HIDL_V1_0_UTILS_COMPONENT_H
145