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 if (width <= 0) {
53 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Width can not be <= 0");
54 }
55
56 if (height <= 0) {
57 return STATUS_ERROR(ERROR_ILLEGAL_ARGUMENT, "Height can not be <= 0");
58 }
59
60 SessionConfig newCfg(width, height, isCameraRecording);
61
62 ECOLOGD("session count before is %zu", mSessionConfigToSessionMap.size());
63
64 Mutex::Autolock lock(mServiceLock);
65 bool foundSession = false;
66 // Instead of looking up the map directly, take the chance to scan the map and evict all the
67 // invalid sessions.
68 SanitizeSession([&](MapIterType iter) {
69 if (iter->first == newCfg) {
70 sp<ECOSession> session = iter->second.promote();
71 foundSession = true;
72 *_aidl_return = session;
73 }
74 });
75
76 if (foundSession) {
77 return binder::Status::ok();
78 }
79
80 // Create a new session and add it to the record.
81 sp<ECOSession> newSession = ECOSession::createECOSession(width, height, isCameraRecording);
82 if (newSession == nullptr) {
83 ECOLOGE("ECOService failed to create ECOSession w: %d, h: %d, isCameraRecording: %d", width,
84 height, isCameraRecording);
85 return STATUS_ERROR(ERROR_UNSUPPORTED, "Failed to create eco session");
86 }
87 *_aidl_return = newSession;
88 // Insert the new session into the map.
89 mSessionConfigToSessionMap[newCfg] = newSession;
90 ECOLOGD("session count after is %zu", mSessionConfigToSessionMap.size());
91
92 return binder::Status::ok();
93 }
94
getNumOfSessions(int32_t * _aidl_return)95 /*virtual*/ ::android::binder::Status ECOService::getNumOfSessions(int32_t* _aidl_return) {
96 Mutex::Autolock lock(mServiceLock);
97 SanitizeSession(std::function<void(MapIterType it)>()); // empty callback
98 *_aidl_return = mSessionConfigToSessionMap.size();
99 return binder::Status::ok();
100 }
101
getSessions(::std::vector<::android::sp<::android::IBinder>> * _aidl_return)102 /*virtual*/ ::android::binder::Status ECOService::getSessions(
103 ::std::vector<::android::sp<::android::IBinder>>* _aidl_return) {
104 // Clear all the entries in the vector.
105 _aidl_return->clear();
106
107 Mutex::Autolock lock(mServiceLock);
108 SanitizeSession([&](MapIterType iter) {
109 sp<ECOSession> session = iter->second.promote();
110 _aidl_return->push_back(IInterface::asBinder(session));
111 });
112 return binder::Status::ok();
113 }
114
isEmptySession(const android::wp<ECOSession> & entry)115 inline bool isEmptySession(const android::wp<ECOSession>& entry) {
116 sp<ECOSession> session = entry.promote();
117 return session == nullptr;
118 }
119
SanitizeSession(const std::function<void (std::unordered_map<SessionConfig,wp<ECOSession>,SessionConfigHash>::iterator it)> & callback)120 void ECOService::SanitizeSession(
121 const std::function<void(std::unordered_map<SessionConfig, wp<ECOSession>,
122 SessionConfigHash>::iterator it)>& callback) {
123 for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
124 it != end;) {
125 if (isEmptySession(it->second)) {
126 it = mSessionConfigToSessionMap.erase(it);
127 } else {
128 if (callback != nullptr) {
129 callback(it);
130 };
131 it++;
132 }
133 }
134 }
135
binderDied(const wp<IBinder> &)136 /*virtual*/ void ECOService::binderDied(const wp<IBinder>& /*who*/) {}
137
dump(int fd,const Vector<String16> & args)138 status_t ECOService::dump(int fd, const Vector<String16>& args) {
139 Mutex::Autolock lock(mServiceLock);
140 dprintf(fd, "\n== ECO Service info: ==\n\n");
141 dprintf(fd, "Number of ECOServices: %zu\n", mSessionConfigToSessionMap.size());
142 for (auto it = mSessionConfigToSessionMap.begin(), end = mSessionConfigToSessionMap.end();
143 it != end; it++) {
144 sp<ECOSession> session = it->second.promote();
145 if (session != nullptr) {
146 session->dump(fd, args);
147 }
148 }
149
150 return NO_ERROR;
151 }
152
153 } // namespace eco
154 } // namespace media
155 } // namespace android