Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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_TAG "CameraSessionStatsBuilder" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include "SessionStatsBuilder.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | status_t SessionStatsBuilder::addStream(int id) { |
| 28 | std::lock_guard<std::mutex> l(mLock); |
| 29 | StreamStats stats; |
| 30 | mStatsMap.emplace(id, stats); |
| 31 | return OK; |
| 32 | } |
| 33 | |
| 34 | status_t SessionStatsBuilder::removeStream(int id) { |
| 35 | std::lock_guard<std::mutex> l(mLock); |
| 36 | mStatsMap.erase(id); |
| 37 | return OK; |
| 38 | } |
| 39 | |
| 40 | void SessionStatsBuilder::buildAndReset(int64_t* requestCount, |
| 41 | int64_t* errorResultCount, bool* deviceError, |
| 42 | std::map<int, StreamStats> *statsMap) { |
| 43 | std::lock_guard<std::mutex> l(mLock); |
| 44 | *requestCount = mRequestCount; |
| 45 | *errorResultCount = mErrorResultCount; |
| 46 | *deviceError = mDeviceError; |
| 47 | *statsMap = mStatsMap; |
| 48 | |
| 49 | // Reset internal states |
| 50 | mRequestCount = 0; |
| 51 | mErrorResultCount = 0; |
| 52 | mCounterStopped = false; |
| 53 | mDeviceError = false; |
| 54 | for (auto& streamStats : mStatsMap) { |
| 55 | streamStats.second.mRequestedFrameCount = 0; |
| 56 | streamStats.second.mDroppedFrameCount = 0; |
| 57 | streamStats.second.mCounterStopped = false; |
| 58 | streamStats.second.mStartLatencyMs = 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void SessionStatsBuilder::startCounter(int id) { |
| 63 | std::lock_guard<std::mutex> l(mLock); |
| 64 | mStatsMap[id].mCounterStopped = false; |
| 65 | } |
| 66 | |
| 67 | void SessionStatsBuilder::stopCounter(int id) { |
| 68 | std::lock_guard<std::mutex> l(mLock); |
| 69 | mStatsMap[id].mCounterStopped = true; |
| 70 | } |
| 71 | |
| 72 | void SessionStatsBuilder::incCounter(int id, bool dropped, int32_t captureLatencyMs) { |
| 73 | std::lock_guard<std::mutex> l(mLock); |
| 74 | auto it = mStatsMap.find(id); |
| 75 | if (it != mStatsMap.end()) { |
| 76 | if (!it->second.mCounterStopped) { |
| 77 | it->second.mRequestedFrameCount++; |
| 78 | if (dropped) { |
| 79 | it->second.mDroppedFrameCount++; |
| 80 | } else if (it->second.mRequestedFrameCount == 1) { |
| 81 | // The capture latency for the first request. |
| 82 | it->second.mStartLatencyMs = captureLatencyMs; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void SessionStatsBuilder::stopCounter() { |
| 89 | std::lock_guard<std::mutex> l(mLock); |
| 90 | mCounterStopped = true; |
| 91 | for (auto& streamStats : mStatsMap) { |
| 92 | streamStats.second.mCounterStopped = true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void SessionStatsBuilder::incResultCounter(bool dropped) { |
| 97 | std::lock_guard<std::mutex> l(mLock); |
| 98 | if (!mCounterStopped) { |
| 99 | mRequestCount ++; |
| 100 | if (dropped) mErrorResultCount++; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void SessionStatsBuilder::onDeviceError() { |
| 105 | std::lock_guard<std::mutex> l(mLock); |
| 106 | mDeviceError = true; |
| 107 | } |
| 108 | |
| 109 | }; // namespace android |