Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | #include "request_tracker.h" |
| 18 | |
| 19 | // #define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "RequestTracker" |
| 21 | #include <cutils/log.h> |
| 22 | |
| 23 | namespace default_camera_hal { |
| 24 | |
| 25 | RequestTracker::RequestTracker() {} |
| 26 | |
| 27 | RequestTracker::~RequestTracker() {} |
| 28 | |
| 29 | void RequestTracker::SetStreamConfiguration( |
| 30 | const camera3_stream_configuration_t& config) { |
| 31 | // Clear the old configuration. |
| 32 | ClearStreamConfiguration(); |
| 33 | // Add an entry to the buffer tracking map for each configured stream. |
| 34 | for (size_t i = 0; i < config.num_streams; ++i) { |
| 35 | buffers_in_flight_.emplace(config.streams[i], 0); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void RequestTracker::ClearStreamConfiguration() { |
| 40 | // The keys of the in flight buffer map are the configured streams. |
| 41 | buffers_in_flight_.clear(); |
| 42 | } |
| 43 | |
| 44 | // Helper: get the streams used by a request. |
| 45 | std::set<camera3_stream_t*> RequestStreams(const CaptureRequest& request) { |
| 46 | std::set<camera3_stream_t*> result; |
| 47 | if (request.input_buffer) { |
| 48 | result.insert(request.input_buffer->stream); |
| 49 | } |
| 50 | for (const auto& output_buffer : request.output_buffers) { |
| 51 | result.insert(output_buffer.stream); |
| 52 | } |
| 53 | return std::move(result); |
| 54 | } |
| 55 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 56 | bool RequestTracker::Add(std::shared_ptr<CaptureRequest> request) { |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 57 | if (!CanAddRequest(*request)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Add to the count for each stream used. |
| 62 | for (const auto stream : RequestStreams(*request)) { |
| 63 | ++buffers_in_flight_[stream]; |
| 64 | } |
| 65 | |
| 66 | // Store the request. |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 67 | frames_in_flight_[request->frame_number] = request; |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 72 | bool RequestTracker::Remove(std::shared_ptr<CaptureRequest> request) { |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 73 | // Get the request. |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 74 | const auto frame_number_request = frames_in_flight_.find(request->frame_number); |
| 75 | if (frame_number_request == frames_in_flight_.end()) { |
| 76 | ALOGE("%s: Frame %u is not in flight.", __func__, request->frame_number); |
| 77 | return false; |
| 78 | } else if (request != frame_number_request->second) { |
| 79 | ALOGE( |
| 80 | "%s: Request for frame %u cannot be removed: " |
| 81 | "does not matched the stored request.", |
| 82 | __func__, |
| 83 | request->frame_number); |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 86 | |
| 87 | frames_in_flight_.erase(frame_number_request); |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 88 | |
| 89 | // Decrement the counts of used streams. |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 90 | for (const auto stream : RequestStreams(*request)) { |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 91 | --buffers_in_flight_[stream]; |
| 92 | } |
| 93 | |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | void RequestTracker::Clear( |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 98 | std::set<std::shared_ptr<CaptureRequest>>* requests) { |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 99 | // If desired, extract all the currently in-flight requests. |
| 100 | if (requests) { |
| 101 | for (auto& frame_number_request : frames_in_flight_) { |
Ari Hausman-Cohen | 0b2113c | 2016-10-03 13:43:13 -0700 | [diff] [blame^] | 102 | requests->insert(frame_number_request.second); |
Ari Hausman-Cohen | cb11a4d | 2016-09-19 17:57:37 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | // Clear out all tracking. |
| 107 | frames_in_flight_.clear(); |
| 108 | buffers_in_flight_.clear(); |
| 109 | } |
| 110 | |
| 111 | bool RequestTracker::CanAddRequest(const CaptureRequest& request) const { |
| 112 | // Check that it's not a duplicate. |
| 113 | if (frames_in_flight_.count(request.frame_number) > 0) { |
| 114 | ALOGE("%s: Already tracking a request with frame number %d.", |
| 115 | __func__, |
| 116 | request.frame_number); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | // Check that each stream has space |
| 121 | // (which implicitly checks if it is configured). |
| 122 | bool result = true; |
| 123 | for (const auto stream : RequestStreams(request)) { |
| 124 | if (StreamFull(stream)) { |
| 125 | ALOGE("%s: Stream %p is full.", __func__, stream); |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool RequestTracker::StreamFull(const camera3_stream_t* handle) const { |
| 133 | const auto it = buffers_in_flight_.find(handle); |
| 134 | if (it == buffers_in_flight_.end()) { |
| 135 | // Unconfigured streams are implicitly full. |
| 136 | ALOGV("%s: Stream %p is not a configured stream.", __func__, handle); |
| 137 | return true; |
| 138 | } else { |
| 139 | return it->second >= it->first->max_buffers; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | bool RequestTracker::InFlight(uint32_t frame_number) const { |
| 144 | return frames_in_flight_.count(frame_number) > 0; |
| 145 | } |
| 146 | |
| 147 | bool RequestTracker::Empty() const { |
| 148 | return frames_in_flight_.empty(); |
| 149 | } |
| 150 | |
| 151 | } // namespace default_camera_hal |