1 /*
2  * Copyright (C) 2019 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 "ECOService"
19 
20 #include "eco/ECOService.h"
21 
22 #include <binder/BinderService.h>
23 #include <cutils/atomic.h>
24 #include <inttypes.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 
29 #include <algorithm>
30 #include <climits>
31 #include <cstring>
32 #include <ctime>
33 #include <string>
34 
35 #include "eco/ECODebug.h"
36 
37 namespace android {
38 namespace media {
39 namespace eco {
40 
ECOService()41 ECOService::ECOService() : BnECOService() {
42     ALOGD("ECOService created");
43     updateLogLevel();
44 }
45 
obtainSession(int32_t width,int32_t height,bool isCameraRecording,::android::sp<::android::media::eco::IECOSession> * _aidl_return)46 /*virtual*/ ::android::binder::Status ECOService::obtainSession(
47         int32_t width, int32_t height, bool isCameraRecording,
48         ::android::sp<::android::media::eco::IECOSession>* _aidl_return) {
49     ECOLOGI("ECOService::obtainSession w: %d, h: %d, isCameraRecording: %d", width, height,
50             isCameraRecording);
51 
52     bool disable = property_get_bool(kDisableEcoServiceProperty, false);
53     if (disable) {
54         ECOLOGE("ECOService:: Failed to obtainSession as ECOService is disable");
55         return STATUS_ERROR(ERROR_UNSUPPORTED, "ECOService is disable");
56     }
57 
58     if (width <= 0) {
59         return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Width can not be <= 0");
60     }
61 
62     if (height <= 0) {
63         return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Height can not be <= 0");
64     }
65 
66     SessionConfig newCfg(width, height, isCameraRecording);
67 
68     ECOLOGD("session count before is %zu", mSessionConfigToSessionMap.size());
69 
70     Mutex::Autolock lock(mServiceLock);
71     bool foundSession = false;
72     // Instead of looking up the map directly, take the chance to scan the map and evict all the
73     // invalid sessions.
74     SanitizeSession([&](MapIterType iter) {
75         if (iter->first == newCfg) {
76             sp<ECOSession> session = iter->second.promote();
77             foundSession = true;
78             *_aidl_return = session;
79         }
80     });
81 
82     if (foundSession) {
83         return binder::Status::ok();
84     }
85 
86     // Create a new session and add it to the record.
87     sp<ECOSession> newSession = ECOSession::createECOSession(width, height, isCameraRecording);
88     if (newSession == nullptr) {
89         ECOLOGE("ECOService failed to create ECOSession w: %d, h: %d, isCameraRecording: %d", width,
90                 height, isCameraRecording);
91         return STATUS_ERROR(ERROR_UNSUPPORTED, "Failed to create eco session");
92     }
93     *_aidl_return = newSession;
94     // Insert the new session into the map.
95     mSessionConfigToSessionMap[newCfg] = newSession;
96     ECOLOGD("session count after is %zu", mSessionConfigToSessionMap.size());
97 
98     return binder::Status::ok();
99 }
100 
getNumOfSessions(int32_t * _aidl_return)101 /*virtual*/ ::android::binder::Status ECOService::getNumOfSessions(int32_t* _aidl_return) {
102     Mutex::Autolock lock(mServiceLock);
103     SanitizeSession(std::function<void(MapIterType it)>());  // empty callback
104     *_aidl_return = mSessionConfigToSessionMap.size();
105     return binder::Status::ok();
106 }
107 
getSessions(::std::vector<::android::sp<::android::IBinder>> * _aidl_return)108 /*virtual*/ ::android::binder::Status ECOService::getSessions(
109         ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) {
110     // Clear all the entries in the vector.
111     _aidl_return->clear();
112 
113     Mutex::Autolock lock(mServiceLock);
114     SanitizeSession([&](MapIterType iter) {
115         sp<ECOSession> session = iter->second.promote();
116         _aidl_return->push_back(IInterface::asBinder(session));
117     });
118     return binder::Status::ok();
119 }
120 
isEmptySession(const android::wp<ECOSession> & entry)121 inline bool isEmptySession(const android::wp<ECOSession>& entry) {
122     sp<ECOSession> session = entry.promote();
123     return session == nullptr;
124 }
125 
SanitizeSession(const std::function<void (std::unordered_map<SessionConfig,wp<ECOSession>,SessionConfigHash>::iterator it)> & callback)126 void ECOService::SanitizeSession(
127         const std::function<void(std::unordered_map<SessionConfig, wp<ECOSession>,
128                                                     SessionConfigHash>::iterator it)>& callback) {
129     for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
130          it != end;) {
131         if (isEmptySession(it->second)) {
132             it = mSessionConfigToSessionMap.erase(it);
133         } else {
134             if (callback != nullptr) {
135                 callback(it);
136             };
137             it++;
138         }
139     }
140 }
141 
binderDied(const wp<IBinder> &)142 /*virtual*/ void ECOService::binderDied(const wp<IBinder>& /*who*/) {}
143 
dump(int fd,const Vector<String16> & args)144 status_t ECOService::dump(int fd, const Vector<String16>& args) {
145     Mutex::Autolock lock(mServiceLock);
146     dprintf(fd, "\n== ECO Service info: ==\n\n");
147     dprintf(fd, "Number of ECOServices: %zu\n", mSessionConfigToSessionMap.size());
148     for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
149          it != end; it++) {
150         sp<ECOSession> session = it->second.promote();
151         if (session != nullptr) {
152             session->dump(fd, args);
153         }
154     }
155 
156     return NO_ERROR;
157 }
158 
159 }  // namespace eco
160 }  // namespace media
161 }  // namespace android