1 /*
2  * Copyright 2021 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_2_UTILS_COMPONENT_H
18 #define CODEC2_HIDL_V1_2_UTILS_COMPONENT_H
19 
20 #include <android/hardware/media/bufferpool/2.0/IClientManager.h>
21 #include <android/hardware/media/c2/1.2/IComponent.h>
22 #include <android/hardware/media/c2/1.0/IComponentInterface.h>
23 #include <android/hardware/media/c2/1.0/IComponentListener.h>
24 #include <android/hardware/media/c2/1.2/IComponentStore.h>
25 #include <android/hardware/media/c2/1.0/IInputSink.h>
26 #include <codec2/hidl/1.2/ComponentInterface.h>
27 #include <codec2/hidl/1.2/Configurable.h>
28 #include <codec2/hidl/1.2/types.h>
29 #include <hidl/Status.h>
30 #include <hwbinder/IBinder.h>
31 
32 #include <C2Component.h>
33 #include <C2Buffer.h>
34 #include <C2.h>
35 
36 #include <map>
37 #include <memory>
38 #include <mutex>
39 
40 namespace android {
41 namespace hardware {
42 namespace media {
43 namespace c2 {
44 namespace V1_2 {
45 
46 using ::android::hardware::media::c2::V1_2::IComponent;
47 using ::android::hardware::media::c2::V1_0::IComponentListener;
48 
49 namespace utils {
50 
51 using ::android::hardware::hidl_array;
52 using ::android::hardware::hidl_memory;
53 using ::android::hardware::hidl_string;
54 using ::android::hardware::hidl_vec;
55 using ::android::hardware::Return;
56 using ::android::hardware::Void;
57 using ::android::hardware::IBinder;
58 using ::android::sp;
59 using ::android::wp;
60 
61 struct ComponentStore;
62 
63 struct Component : public IComponent,
64                    public std::enable_shared_from_this<Component> {
65     Component(
66             const std::shared_ptr<C2Component>&,
67             const sp<IComponentListener>& listener,
68             const sp<ComponentStore>& store,
69             const sp<::android::hardware::media::bufferpool::V2_0::
70                 IClientManager>& clientPoolManager);
71     c2_status_t status() const;
72 
73     typedef ::android::hardware::graphics::bufferqueue::V1_0::
74             IGraphicBufferProducer HGraphicBufferProducer1;
75     typedef ::android::hardware::graphics::bufferqueue::V2_0::
76             IGraphicBufferProducer HGraphicBufferProducer2;
77 
78     // Methods from IComponent follow.
79     virtual Return<Status> queue(const WorkBundle& workBundle) override;
80     virtual Return<void> flush(flush_cb _hidl_cb) override;
81     virtual Return<Status> drain(bool withEos) override;
82     virtual Return<Status> setOutputSurface(
83             uint64_t blockPoolId,
84             const sp<HGraphicBufferProducer2>& surface) override;
85     virtual Return<void> connectToInputSurface(
86             const sp<IInputSurface>& inputSurface,
87             connectToInputSurface_cb _hidl_cb) override;
88     virtual Return<void> connectToOmxInputSurface(
89             const sp<HGraphicBufferProducer1>& producer,
90             const sp<::android::hardware::media::omx::V1_0::
91             IGraphicBufferSource>& source,
92             connectToOmxInputSurface_cb _hidl_cb) override;
93     virtual Return<Status> disconnectFromInputSurface() override;
94     virtual Return<void> createBlockPool(
95             uint32_t allocatorId,
96             createBlockPool_cb _hidl_cb) override;
97     virtual Return<Status> destroyBlockPool(uint64_t blockPoolId) override;
98     virtual Return<Status> start() override;
99     virtual Return<Status> stop() override;
100     virtual Return<Status> reset() override;
101     virtual Return<Status> release() override;
102     virtual Return<sp<IComponentInterface>> getInterface() override;
103     virtual Return<sp<IInputSink>> asInputSink() override;
104     virtual Return<void> configureVideoTunnel(
105             uint32_t avSyncHwId, configureVideoTunnel_cb _hidl_cb) override;
106     virtual Return<Status> setOutputSurfaceWithSyncObj(
107             uint64_t blockPoolId,
108             const sp<HGraphicBufferProducer2>& surface,
109             const SurfaceSyncObj& syncObject) override;
110 
111 
112     // Returns a C2Component associated to the given sink if the sink is indeed
113     // a local component. Returns nullptr otherwise.
114     //
115     // This function is used by InputSurface::connect().
116     static std::shared_ptr<C2Component> findLocalComponent(
117             const sp<IInputSink>& sink);
118 
119 protected:
120     c2_status_t mInit;
121     std::shared_ptr<C2Component> mComponent;
122     sp<ComponentInterface> mInterface;
123     sp<IComponentListener> mListener;
124     sp<ComponentStore> mStore;
125     ::android::hardware::media::c2::V1_2::utils::DefaultBufferPoolSender
126             mBufferPoolSender;
127 
128     struct Sink;
129     std::mutex mSinkMutex;
130     sp<Sink> mSink;
131 
132     std::mutex mBlockPoolsMutex;
133     // This map keeps C2BlockPool objects that are created by createBlockPool()
134     // alive. These C2BlockPool objects can be deleted by calling
135     // destroyBlockPool(), reset() or release(), or by destroying the component.
136     std::map<uint64_t, std::shared_ptr<C2BlockPool>> mBlockPools;
137 
138     void initListener(const sp<Component>& self);
139 
140     virtual ~Component() override;
141 
142     friend struct ComponentStore;
143 
144     struct Listener;
145 };
146 
147 } // namespace utils
148 } // namespace V1_2
149 } // namespace c2
150 } // namespace media
151 } // namespace hardware
152 } // namespace android
153 
154 #endif // CODEC2_HIDL_V1_2_UTILS_COMPONENT_H
155