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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "android.hardware.media.c2@1.2-service"
19
20 #include <android-base/logging.h>
21 #include <binder/ProcessState.h>
22 #include <codec2/hidl/1.2/ComponentStore.h>
23 #include <hidl/HidlTransportSupport.h>
24 #include <minijail.h>
25
26 #include <util/C2InterfaceHelper.h>
27 #include <C2Component.h>
28 #include <C2Config.h>
29
30 // This is the absolute on-device path of the prebuild_etc module
31 // "android.hardware.media.c2@1.1-default-seccomp_policy" in Android.bp.
32 static constexpr char kBaseSeccompPolicyPath[] =
33 "/vendor/etc/seccomp_policy/"
34 "android.hardware.media.c2@1.2-default-seccomp-policy";
35
36 // Additional seccomp permissions can be added in this file.
37 // This file does not exist by default.
38 static constexpr char kExtSeccompPolicyPath[] =
39 "/vendor/etc/seccomp_policy/"
40 "android.hardware.media.c2@1.2-extended-seccomp-policy";
41
42 class StoreImpl : public C2ComponentStore {
43 public:
StoreImpl()44 StoreImpl()
45 : mReflectorHelper(std::make_shared<C2ReflectorHelper>()),
46 mInterface(mReflectorHelper) {
47 }
48
49 virtual ~StoreImpl() override = default;
50
getName() const51 virtual C2String getName() const override {
52 return "default";
53 }
54
createComponent(C2String,std::shared_ptr<C2Component> * const)55 virtual c2_status_t createComponent(
56 C2String /*name*/,
57 std::shared_ptr<C2Component>* const /*component*/) override {
58 return C2_NOT_FOUND;
59 }
60
createInterface(C2String,std::shared_ptr<C2ComponentInterface> * const)61 virtual c2_status_t createInterface(
62 C2String /* name */,
63 std::shared_ptr<C2ComponentInterface>* const /* interface */) override {
64 return C2_NOT_FOUND;
65 }
66
67 virtual std::vector<std::shared_ptr<const C2Component::Traits>>
listComponents()68 listComponents() override {
69 return {};
70 }
71
copyBuffer(std::shared_ptr<C2GraphicBuffer>,std::shared_ptr<C2GraphicBuffer>)72 virtual c2_status_t copyBuffer(
73 std::shared_ptr<C2GraphicBuffer> /* src */,
74 std::shared_ptr<C2GraphicBuffer> /* dst */) override {
75 return C2_OMITTED;
76 }
77
query_sm(const std::vector<C2Param * > & stackParams,const std::vector<C2Param::Index> & heapParamIndices,std::vector<std::unique_ptr<C2Param>> * const heapParams) const78 virtual c2_status_t query_sm(
79 const std::vector<C2Param*>& stackParams,
80 const std::vector<C2Param::Index>& heapParamIndices,
81 std::vector<std::unique_ptr<C2Param>>* const heapParams) const override {
82 return mInterface.query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams);
83 }
84
config_sm(const std::vector<C2Param * > & params,std::vector<std::unique_ptr<C2SettingResult>> * const failures)85 virtual c2_status_t config_sm(
86 const std::vector<C2Param*>& params,
87 std::vector<std::unique_ptr<C2SettingResult>>* const failures) override {
88 return mInterface.config(params, C2_MAY_BLOCK, failures);
89 }
90
getParamReflector() const91 virtual std::shared_ptr<C2ParamReflector> getParamReflector() const override {
92 return mReflectorHelper;
93 }
94
querySupportedParams_nb(std::vector<std::shared_ptr<C2ParamDescriptor>> * const params) const95 virtual c2_status_t querySupportedParams_nb(
96 std::vector<std::shared_ptr<C2ParamDescriptor>>* const params) const override {
97 return mInterface.querySupportedParams(params);
98 }
99
querySupportedValues_sm(std::vector<C2FieldSupportedValuesQuery> & fields) const100 virtual c2_status_t querySupportedValues_sm(
101 std::vector<C2FieldSupportedValuesQuery>& fields) const override {
102 return mInterface.querySupportedValues(fields, C2_MAY_BLOCK);
103 }
104
105 private:
106 class Interface : public C2InterfaceHelper {
107 public:
Interface(const std::shared_ptr<C2ReflectorHelper> & helper)108 Interface(const std::shared_ptr<C2ReflectorHelper> &helper)
109 : C2InterfaceHelper(helper) {
110 setDerivedInstance(this);
111
112 addParameter(
113 DefineParam(mIonUsageInfo, "ion-usage")
114 .withDefault(new C2StoreIonUsageInfo())
115 .withFields({
116 C2F(mIonUsageInfo, usage).flags(
117 {C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE}),
118 C2F(mIonUsageInfo, capacity).inRange(0, UINT32_MAX, 1024),
119 C2F(mIonUsageInfo, heapMask).any(),
120 C2F(mIonUsageInfo, allocFlags).flags({}),
121 C2F(mIonUsageInfo, minAlignment).equalTo(0)
122 })
123 .withSetter(SetIonUsage)
124 .build());
125
126 addParameter(
127 DefineParam(mDmaBufUsageInfo, "dmabuf-usage")
128 .withDefault(new C2StoreDmaBufUsageInfo())
129 .withFields({
130 C2F(mDmaBufUsageInfo, usage).flags({C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE}),
131 C2F(mDmaBufUsageInfo, capacity).inRange(0, UINT32_MAX, 1024),
132 C2F(mDmaBufUsageInfo, heapName).any(),
133 C2F(mDmaBufUsageInfo, allocFlags).flags({}),
134 })
135 .withSetter(SetDmaBufUsage)
136 .build());
137 }
138
139 virtual ~Interface() = default;
140
141 private:
SetIonUsage(bool,C2P<C2StoreIonUsageInfo> & me)142 static C2R SetIonUsage(bool /* mayBlock */, C2P<C2StoreIonUsageInfo> &me) {
143 // Vendor's TODO: put appropriate mapping logic
144 me.set().heapMask = ~0;
145 me.set().allocFlags = 0;
146 me.set().minAlignment = 0;
147 return C2R::Ok();
148 }
149
SetDmaBufUsage(bool,C2P<C2StoreDmaBufUsageInfo> & me)150 static C2R SetDmaBufUsage(bool /* mayBlock */, C2P<C2StoreDmaBufUsageInfo> &me) {
151 // Vendor's TODO: put appropriate mapping logic
152 strncpy(me.set().m.heapName, "system", me.v.flexCount());
153 me.set().m.allocFlags = 0;
154 return C2R::Ok();
155 }
156
157
158 std::shared_ptr<C2StoreIonUsageInfo> mIonUsageInfo;
159 std::shared_ptr<C2StoreDmaBufUsageInfo> mDmaBufUsageInfo;
160 };
161 std::shared_ptr<C2ReflectorHelper> mReflectorHelper;
162 Interface mInterface;
163 };
164
main(int,char **)165 int main(int /* argc */, char** /* argv */) {
166 using namespace ::android;
167 LOG(DEBUG) << "android.hardware.media.c2@1.2-service starting...";
168
169 // Set up minijail to limit system calls.
170 signal(SIGPIPE, SIG_IGN);
171 SetUpMinijail(kBaseSeccompPolicyPath, kExtSeccompPolicyPath);
172
173 // Enable vndbinder to allow vendor-to-vendor binder calls.
174 ProcessState::initWithDriver("/dev/vndbinder");
175
176 ProcessState::self()->startThreadPool();
177 // Extra threads may be needed to handle a stacked IPC sequence that
178 // contains alternating binder and hwbinder calls. (See b/35283480.)
179 hardware::configureRpcThreadpool(8, true /* callerWillJoin */);
180
181 // Create IComponentStore service.
182 {
183 using namespace ::android::hardware::media::c2::V1_2;
184 sp<IComponentStore> store;
185
186 // TODO: Replace this with
187 // store = new utils::ComponentStore(
188 // /* implementation of C2ComponentStore */);
189 LOG(DEBUG) << "Instantiating Codec2's IComponentStore service...";
190 store = new utils::ComponentStore(
191 std::make_shared<StoreImpl>());
192
193 if (store == nullptr) {
194 LOG(ERROR) << "Cannot create Codec2's IComponentStore service.";
195 } else {
196 constexpr char const* serviceName = "default";
197 if (store->registerAsService(serviceName) != OK) {
198 LOG(ERROR) << "Cannot register Codec2's IComponentStore service"
199 " with instance name << \""
200 << serviceName << "\".";
201 } else {
202 LOG(DEBUG) << "Codec2's IComponentStore service registered. "
203 "Instance name: \"" << serviceName << "\".";
204 }
205 }
206 }
207
208 hardware::joinRpcThreadpool();
209 return 0;
210 }
211