Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 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 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 17 | #include "EvsCamera.h" |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 18 | #include "ConfigManager.h" |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 19 | #include "EvsEnumerator.h" |
| 20 | |
| 21 | #include <ui/GraphicBufferAllocator.h> |
| 22 | #include <ui/GraphicBufferMapper.h> |
Changyeon Jo | 56c9b37 | 2019-10-09 14:04:31 -0700 | [diff] [blame] | 23 | #include <utils/SystemClock.h> |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 24 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 25 | namespace { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 26 | |
| 27 | // Arbitrary limit on number of graphics buffers allowed to be allocated |
| 28 | // Safeguards against unreasonable resource consumption and provides a testable limit |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 29 | constexpr unsigned kMaxBuffersInFlight = 100; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 30 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 31 | // Minimum number of buffers to run a video stream |
| 32 | constexpr int kMinimumBuffersInFlight = 1; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 33 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 34 | // Colors for the colorbar test pattern in ABGR format |
| 35 | constexpr uint32_t kColors[] = { |
| 36 | 0xFFFFFFFF, // white |
| 37 | 0xFF00FFFF, // yellow |
| 38 | 0xFFFFFF00, // cyan |
| 39 | 0xFF00FF00, // green |
| 40 | 0xFFFF00FF, // fuchsia |
| 41 | 0xFF0000FF, // red |
| 42 | 0xFFFF0000, // blue |
| 43 | 0xFF000000, // black |
| 44 | }; |
| 45 | constexpr uint32_t kNumColors = sizeof(kColors) / sizeof(kColors[0]); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 46 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 47 | } // namespace |
| 48 | |
| 49 | namespace android::hardware::automotive::evs::V1_1::implementation { |
| 50 | |
| 51 | using V1_0::EvsResult; |
| 52 | |
| 53 | EvsCamera::EvsCamera(const char* id, std::unique_ptr<ConfigManager::CameraInfo>& camInfo) |
| 54 | : mFramesAllowed(0), mFramesInUse(0), mStreamState(STOPPED), mCameraInfo(camInfo) { |
| 55 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 56 | |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 57 | /* set a camera id */ |
| 58 | mDescription.v1.cameraId = id; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 59 | |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 60 | /* set camera metadata */ |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 61 | mDescription.metadata.setToExternal((uint8_t*)camInfo->characteristics, |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 62 | get_camera_metadata_size(camInfo->characteristics)); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 65 | EvsCamera::~EvsCamera() { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 66 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 67 | forceShutdown(); |
| 68 | } |
| 69 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 70 | // This gets called if another caller "steals" ownership of the camera |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 71 | void EvsCamera::forceShutdown() { |
| 72 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 73 | |
| 74 | // Make sure our output stream is cleaned up |
| 75 | // (It really should be already) |
| 76 | stopVideoStream(); |
| 77 | |
| 78 | // Claim the lock while we work on internal state |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 79 | std::lock_guard<std::mutex> lock(mAccessLock); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 80 | |
| 81 | // Drop all the graphics buffers we've been using |
| 82 | if (mBuffers.size() > 0) { |
| 83 | GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
| 84 | for (auto&& rec : mBuffers) { |
| 85 | if (rec.inUse) { |
| 86 | ALOGE("Error - releasing buffer despite remote ownership"); |
| 87 | } |
| 88 | alloc.free(rec.handle); |
| 89 | rec.handle = nullptr; |
| 90 | } |
| 91 | mBuffers.clear(); |
| 92 | } |
| 93 | |
| 94 | // Put this object into an unrecoverable error state since somebody else |
| 95 | // is going to own the underlying camera now |
| 96 | mStreamState = DEAD; |
| 97 | } |
| 98 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 99 | // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow. |
| 100 | Return<void> EvsCamera::getCameraInfo(getCameraInfo_cb _hidl_cb) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 101 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 102 | |
| 103 | // Send back our self description |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 104 | _hidl_cb(mDescription.v1); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 105 | return {}; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 108 | Return<EvsResult> EvsCamera::setMaxFramesInFlight(uint32_t bufferCount) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 109 | ALOGD("%s, bufferCount = %u", __FUNCTION__, bufferCount); |
| 110 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 111 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 112 | |
| 113 | // If we've been displaced by another owner of the camera, then we can't do anything else |
| 114 | if (mStreamState == DEAD) { |
| 115 | ALOGE("ignoring setMaxFramesInFlight call when camera has been lost."); |
| 116 | return EvsResult::OWNERSHIP_LOST; |
| 117 | } |
| 118 | |
| 119 | // We cannot function without at least one video buffer to send data |
| 120 | if (bufferCount < 1) { |
| 121 | ALOGE("Ignoring setMaxFramesInFlight with less than one buffer requested"); |
| 122 | return EvsResult::INVALID_ARG; |
| 123 | } |
| 124 | |
| 125 | // Update our internal state |
| 126 | if (setAvailableFrames_Locked(bufferCount)) { |
| 127 | return EvsResult::OK; |
| 128 | } else { |
| 129 | return EvsResult::BUFFER_NOT_AVAILABLE; |
| 130 | } |
| 131 | } |
| 132 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 133 | Return<EvsResult> EvsCamera::startVideoStream(const ::android::sp<V1_0::IEvsCameraStream>& stream) { |
| 134 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 135 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 136 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 137 | |
| 138 | // If we've been displaced by another owner of the camera, then we can't do anything else |
| 139 | if (mStreamState == DEAD) { |
| 140 | ALOGE("ignoring startVideoStream call when camera has been lost."); |
| 141 | return EvsResult::OWNERSHIP_LOST; |
| 142 | } |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 143 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 144 | if (mStreamState != STOPPED) { |
| 145 | ALOGE("ignoring startVideoStream call when a stream is already running."); |
| 146 | return EvsResult::STREAM_ALREADY_RUNNING; |
| 147 | } |
| 148 | |
| 149 | // If the client never indicated otherwise, configure ourselves for a single streaming buffer |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 150 | if (mFramesAllowed < kMinimumBuffersInFlight) { |
| 151 | if (!setAvailableFrames_Locked(kMinimumBuffersInFlight)) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 152 | ALOGE("Failed to start stream because we couldn't get a graphics buffer"); |
| 153 | return EvsResult::BUFFER_NOT_AVAILABLE; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // Record the user's callback for use when we have a frame ready |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 158 | mStream = IEvsCameraStream::castFrom(stream).withDefault(nullptr); |
| 159 | if (!mStream) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 160 | ALOGE("Default implementation does not support v1.0 IEvsCameraStream"); |
| 161 | return EvsResult::INVALID_ARG; |
| 162 | } |
| 163 | |
| 164 | // Start the frame generation thread |
| 165 | mStreamState = RUNNING; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 166 | mCaptureThread = std::thread([this]() { generateFrames(); }); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 167 | |
| 168 | return EvsResult::OK; |
| 169 | } |
| 170 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 171 | Return<void> EvsCamera::doneWithFrame(const V1_0::BufferDesc& buffer) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 172 | std::lock_guard<std::mutex> lock(mAccessLock); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 173 | returnBufferLocked(buffer.bufferId, buffer.memHandle); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 174 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 175 | return {}; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 178 | Return<void> EvsCamera::stopVideoStream() { |
| 179 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 180 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 181 | std::unique_lock<std::mutex> lock(mAccessLock); |
| 182 | |
| 183 | if (mStreamState != RUNNING) { |
| 184 | return {}; |
| 185 | } |
| 186 | |
| 187 | // Tell the GenerateFrames loop we want it to stop |
| 188 | mStreamState = STOPPING; |
| 189 | |
| 190 | // Block outside the mutex until the "stop" flag has been acknowledged |
| 191 | // We won't send any more frames, but the client might still get some already in flight |
| 192 | ALOGD("Waiting for stream thread to end..."); |
| 193 | lock.unlock(); |
| 194 | if (mCaptureThread.joinable()) { |
| 195 | mCaptureThread.join(); |
| 196 | } |
| 197 | lock.lock(); |
| 198 | |
| 199 | mStreamState = STOPPED; |
| 200 | mStream = nullptr; |
| 201 | ALOGD("Stream marked STOPPED."); |
| 202 | |
| 203 | return {}; |
| 204 | } |
| 205 | |
| 206 | Return<int32_t> EvsCamera::getExtendedInfo(uint32_t opaqueIdentifier) { |
| 207 | ALOGD("%s", __FUNCTION__); |
| 208 | |
| 209 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 210 | const auto it = mExtInfo.find(opaqueIdentifier); |
| 211 | if (it == mExtInfo.end()) { |
| 212 | // Return zero by default as required by the spec |
| 213 | return 0; |
| 214 | } else { |
| 215 | return it->second[0]; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | Return<EvsResult> EvsCamera::setExtendedInfo([[maybe_unused]] uint32_t opaqueIdentifier, |
| 220 | [[maybe_unused]] int32_t opaqueValue) { |
| 221 | ALOGD("%s", __FUNCTION__); |
| 222 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 223 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 224 | |
| 225 | // If we've been displaced by another owner of the camera, then we can't do anything else |
| 226 | if (mStreamState == DEAD) { |
| 227 | ALOGE("ignoring setExtendedInfo call when camera has been lost."); |
| 228 | return EvsResult::OWNERSHIP_LOST; |
| 229 | } |
| 230 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 231 | mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue); |
| 232 | return EvsResult::OK; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 235 | // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow. |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 236 | Return<void> EvsCamera::getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 237 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 238 | |
| 239 | // Send back our self description |
| 240 | _hidl_cb(mDescription); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 241 | return {}; |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 244 | Return<void> EvsCamera::getPhysicalCameraInfo([[maybe_unused]] const hidl_string& id, |
Changyeon Jo | 6caf74b | 2019-12-02 09:50:41 -0800 | [diff] [blame] | 245 | getCameraInfo_1_1_cb _hidl_cb) { |
| 246 | ALOGD("%s", __FUNCTION__); |
| 247 | |
| 248 | // This works exactly same as getCameraInfo_1_1() in default implementation. |
Changyeon Jo | 6caf74b | 2019-12-02 09:50:41 -0800 | [diff] [blame] | 249 | _hidl_cb(mDescription); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 250 | return {}; |
Changyeon Jo | 6caf74b | 2019-12-02 09:50:41 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 253 | Return<EvsResult> EvsCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc>& buffers) { |
| 254 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 6caf74b | 2019-12-02 09:50:41 -0800 | [diff] [blame] | 255 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 256 | std::lock_guard<std::mutex> lock(mAccessLock); |
Changyeon Jo | 56c9b37 | 2019-10-09 14:04:31 -0700 | [diff] [blame] | 257 | for (auto&& buffer : buffers) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 258 | returnBufferLocked(buffer.bufferId, buffer.buffer.nativeHandle); |
Changyeon Jo | 56c9b37 | 2019-10-09 14:04:31 -0700 | [diff] [blame] | 259 | } |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 260 | return EvsResult::OK; |
| 261 | } |
| 262 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 263 | Return<EvsResult> EvsCamera::pauseVideoStream() { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 264 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 265 | // Default implementation does not support this. |
| 266 | return EvsResult::UNDERLYING_SERVICE_ERROR; |
| 267 | } |
| 268 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 269 | Return<EvsResult> EvsCamera::resumeVideoStream() { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 270 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 271 | // Default implementation does not support this. |
| 272 | return EvsResult::UNDERLYING_SERVICE_ERROR; |
| 273 | } |
| 274 | |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 275 | Return<EvsResult> EvsCamera::setMaster() { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 276 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 277 | // Default implementation does not expect multiple subscribers and therefore |
| 278 | // return a success code always. |
| 279 | return EvsResult::OK; |
| 280 | } |
| 281 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 282 | Return<EvsResult> EvsCamera::forceMaster(const sp<V1_0::IEvsDisplay>&) { |
| 283 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 284 | // Default implementation does not expect multiple subscribers and therefore |
| 285 | // return a success code always. |
| 286 | return EvsResult::OK; |
| 287 | } |
| 288 | |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 289 | Return<EvsResult> EvsCamera::unsetMaster() { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 290 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 291 | // Default implementation does not expect multiple subscribers and therefore |
| 292 | // return a success code always. |
| 293 | return EvsResult::OK; |
| 294 | } |
| 295 | |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 296 | Return<void> EvsCamera::getParameterList(getParameterList_cb _hidl_cb) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 297 | ALOGD("%s", __FUNCTION__); |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 298 | hidl_vec<CameraParam> hidlCtrls; |
| 299 | hidlCtrls.resize(mCameraInfo->controls.size()); |
| 300 | unsigned idx = 0; |
| 301 | for (auto& [cid, cfg] : mCameraInfo->controls) { |
| 302 | hidlCtrls[idx++] = cid; |
| 303 | } |
| 304 | |
| 305 | _hidl_cb(hidlCtrls); |
Changyeon Jo | 3e69744 | 2020-03-27 14:19:46 -0700 | [diff] [blame] | 306 | return {}; |
| 307 | } |
| 308 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 309 | Return<void> EvsCamera::getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) { |
| 310 | ALOGD("%s", __FUNCTION__); |
| 311 | auto it = mCameraInfo->controls.find(id); |
| 312 | if (it == mCameraInfo->controls.end()) { |
| 313 | _hidl_cb(0, 0, 0); |
| 314 | } else { |
| 315 | _hidl_cb(std::get<0>(it->second), std::get<1>(it->second), std::get<2>(it->second)); |
| 316 | } |
| 317 | return {}; |
| 318 | } |
| 319 | |
| 320 | Return<void> EvsCamera::setIntParameter(CameraParam id, int32_t value, |
| 321 | setIntParameter_cb _hidl_cb) { |
| 322 | ALOGD("%s", __FUNCTION__); |
| 323 | mParams.insert_or_assign(id, value); |
| 324 | _hidl_cb(EvsResult::OK, {value}); |
| 325 | return {}; |
| 326 | } |
| 327 | |
| 328 | Return<void> EvsCamera::getIntParameter([[maybe_unused]] CameraParam id, |
| 329 | getIntParameter_cb _hidl_cb) { |
| 330 | ALOGD("%s", __FUNCTION__); |
| 331 | auto it = mParams.find(id); |
| 332 | std::vector<int32_t> values; |
| 333 | if (it == mParams.end()) { |
| 334 | _hidl_cb(EvsResult::INVALID_ARG, values); |
| 335 | } else { |
| 336 | values.push_back(it->second); |
| 337 | _hidl_cb(EvsResult::OK, values); |
| 338 | } |
| 339 | return {}; |
| 340 | } |
| 341 | |
| 342 | Return<EvsResult> EvsCamera::setExtendedInfo_1_1(uint32_t opaqueIdentifier, |
| 343 | const hidl_vec<uint8_t>& opaqueValue) { |
| 344 | ALOGD("%s", __FUNCTION__); |
| 345 | mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue); |
| 346 | return EvsResult::OK; |
| 347 | } |
| 348 | |
| 349 | Return<void> EvsCamera::getExtendedInfo_1_1(uint32_t opaqueIdentifier, |
| 350 | getExtendedInfo_1_1_cb _hidl_cb) { |
| 351 | ALOGD("%s", __FUNCTION__); |
| 352 | auto status = EvsResult::OK; |
| 353 | hidl_vec<uint8_t> value; |
| 354 | const auto it = mExtInfo.find(opaqueIdentifier); |
| 355 | if (it == mExtInfo.end()) { |
| 356 | status = EvsResult::INVALID_ARG; |
| 357 | } else { |
| 358 | value = it->second; |
| 359 | } |
| 360 | _hidl_cb(status, value); |
| 361 | return {}; |
| 362 | } |
| 363 | |
| 364 | Return<void> EvsCamera::importExternalBuffers([[maybe_unused]] const hidl_vec<BufferDesc>& buffers, |
| 365 | importExternalBuffers_cb _hidl_cb) { |
| 366 | auto numBuffersToAdd = buffers.size(); |
| 367 | if (numBuffersToAdd < 1) { |
| 368 | ALOGD("No buffers to add"); |
| 369 | _hidl_cb(EvsResult::OK, mFramesAllowed); |
| 370 | return {}; |
| 371 | } |
| 372 | |
| 373 | { |
| 374 | std::scoped_lock<std::mutex> lock(mAccessLock); |
| 375 | |
| 376 | if (numBuffersToAdd > (kMaxBuffersInFlight - mFramesAllowed)) { |
| 377 | numBuffersToAdd -= (kMaxBuffersInFlight - mFramesAllowed); |
| 378 | ALOGW("Exceed the limit on number of buffers. %" PRIu64 " buffers will be added only.", |
Changyeon Jo | 339f0ac | 2022-01-30 22:19:32 -0800 | [diff] [blame] | 379 | static_cast<uint64_t>(numBuffersToAdd)); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
| 383 | const auto before = mFramesAllowed; |
| 384 | for (auto i = 0; i < numBuffersToAdd; ++i) { |
| 385 | // TODO: reject if external buffer is configured differently. |
| 386 | auto& b = buffers[i]; |
| 387 | const AHardwareBuffer_Desc* pDesc = |
| 388 | reinterpret_cast<const AHardwareBuffer_Desc*>(&b.buffer.description); |
| 389 | |
| 390 | // Import a buffer to add |
| 391 | buffer_handle_t memHandle = nullptr; |
| 392 | status_t result = |
| 393 | mapper.importBuffer(b.buffer.nativeHandle, pDesc->width, pDesc->height, 1, |
| 394 | pDesc->format, pDesc->usage, pDesc->stride, &memHandle); |
| 395 | if (result != android::NO_ERROR || !memHandle) { |
| 396 | ALOGW("Failed to import a buffer %d", b.bufferId); |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | auto stored = false; |
| 401 | for (auto&& rec : mBuffers) { |
| 402 | if (rec.handle == nullptr) { |
| 403 | // Use this existing entry |
| 404 | rec.handle = memHandle; |
| 405 | rec.inUse = false; |
| 406 | |
| 407 | stored = true; |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (!stored) { |
| 413 | // Add a BufferRecord wrapping this handle to our set of available buffers |
| 414 | mBuffers.emplace_back(memHandle); |
| 415 | } |
| 416 | |
| 417 | ++mFramesAllowed; |
| 418 | } |
| 419 | |
| 420 | _hidl_cb(EvsResult::OK, mFramesAllowed - before); |
| 421 | return {}; |
| 422 | } |
| 423 | } |
Changyeon Jo | 93594ab | 2020-02-17 13:28:56 -0800 | [diff] [blame] | 424 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 425 | bool EvsCamera::setAvailableFrames_Locked(unsigned bufferCount) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 426 | if (bufferCount < kMinimumBuffersInFlight) { |
| 427 | ALOGE("Ignoring request to set buffer count below the minimum number of buffers to run a " |
| 428 | "video stream"); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 429 | return false; |
| 430 | } |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 431 | if (bufferCount > kMaxBuffersInFlight) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 432 | ALOGE("Rejecting buffer request in excess of internal limit"); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // Is an increase required? |
| 437 | if (mFramesAllowed < bufferCount) { |
| 438 | // An increase is required |
| 439 | unsigned needed = bufferCount - mFramesAllowed; |
| 440 | ALOGI("Allocating %d buffers for camera frames", needed); |
| 441 | |
| 442 | unsigned added = increaseAvailableFrames_Locked(needed); |
| 443 | if (added != needed) { |
| 444 | // If we didn't add all the frames we needed, then roll back to the previous state |
| 445 | ALOGE("Rolling back to previous frame queue size"); |
| 446 | decreaseAvailableFrames_Locked(added); |
| 447 | return false; |
| 448 | } |
| 449 | } else if (mFramesAllowed > bufferCount) { |
| 450 | // A decrease is required |
| 451 | unsigned framesToRelease = mFramesAllowed - bufferCount; |
| 452 | ALOGI("Returning %d camera frame buffers", framesToRelease); |
| 453 | |
| 454 | unsigned released = decreaseAvailableFrames_Locked(framesToRelease); |
| 455 | if (released != framesToRelease) { |
| 456 | // This shouldn't happen with a properly behaving client because the client |
| 457 | // should only make this call after returning sufficient outstanding buffers |
| 458 | // to allow a clean resize. |
| 459 | ALOGE("Buffer queue shrink failed -- too many buffers currently in use?"); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return true; |
| 464 | } |
| 465 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 466 | unsigned EvsCamera::increaseAvailableFrames_Locked(unsigned numToAdd) { |
| 467 | // Acquire the graphics buffer allocator |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 468 | GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 469 | |
| 470 | unsigned added = 0; |
| 471 | |
| 472 | while (added < numToAdd) { |
| 473 | buffer_handle_t memHandle = nullptr; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 474 | status_t result = alloc.allocate(mWidth, mHeight, mFormat, 1, mUsage, &memHandle, &mStride, |
| 475 | 0, "EvsCamera"); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 476 | if (result != NO_ERROR) { |
| 477 | ALOGE("Error %d allocating %d x %d graphics buffer", result, mWidth, mHeight); |
| 478 | break; |
| 479 | } |
| 480 | if (!memHandle) { |
| 481 | ALOGE("We didn't get a buffer handle back from the allocator"); |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | // Find a place to store the new buffer |
| 486 | bool stored = false; |
| 487 | for (auto&& rec : mBuffers) { |
| 488 | if (rec.handle == nullptr) { |
| 489 | // Use this existing entry |
| 490 | rec.handle = memHandle; |
| 491 | rec.inUse = false; |
| 492 | stored = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if (!stored) { |
| 497 | // Add a BufferRecord wrapping this handle to our set of available buffers |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 498 | mBuffers.push_back(std::move(BufferRecord(memHandle))); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | mFramesAllowed++; |
| 502 | added++; |
| 503 | } |
| 504 | |
| 505 | return added; |
| 506 | } |
| 507 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 508 | unsigned EvsCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) { |
| 509 | // Acquire the graphics buffer allocator |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 510 | GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 511 | |
| 512 | unsigned removed = 0; |
| 513 | |
| 514 | for (auto&& rec : mBuffers) { |
| 515 | // Is this record not in use, but holding a buffer that we can free? |
| 516 | if ((rec.inUse == false) && (rec.handle != nullptr)) { |
| 517 | // Release buffer and update the record so we can recognize it as "empty" |
| 518 | alloc.free(rec.handle); |
| 519 | rec.handle = nullptr; |
| 520 | |
| 521 | mFramesAllowed--; |
| 522 | removed++; |
| 523 | |
| 524 | if (removed == numToRemove) { |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return removed; |
| 531 | } |
| 532 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 533 | // This is the asynchronous frame generation thread that runs in parallel with the |
| 534 | // main serving thread. There is one for each active camera instance. |
| 535 | void EvsCamera::generateFrames() { |
| 536 | ALOGD("Frame generation loop started"); |
| 537 | |
| 538 | unsigned idx; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 539 | while (true) { |
| 540 | bool timeForFrame = false; |
| 541 | nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC); |
| 542 | |
| 543 | // Lock scope for updating shared state |
| 544 | { |
| 545 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 546 | |
| 547 | if (mStreamState != RUNNING) { |
| 548 | // Break out of our main thread loop |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | // Are we allowed to issue another buffer? |
| 553 | if (mFramesInUse >= mFramesAllowed) { |
| 554 | // Can't do anything right now -- skip this frame |
| 555 | ALOGW("Skipped a frame because too many are in flight\n"); |
| 556 | } else { |
| 557 | // Identify an available buffer to fill |
| 558 | for (idx = 0; idx < mBuffers.size(); idx++) { |
| 559 | if (!mBuffers[idx].inUse) { |
| 560 | if (mBuffers[idx].handle != nullptr) { |
| 561 | // Found an available record, so stop looking |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | if (idx >= mBuffers.size()) { |
| 567 | // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed |
| 568 | ALOGE("Failed to find an available buffer slot\n"); |
| 569 | } else { |
| 570 | // We're going to make the frame busy |
| 571 | mBuffers[idx].inUse = true; |
| 572 | mFramesInUse++; |
| 573 | timeForFrame = true; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | if (timeForFrame) { |
| 579 | // Assemble the buffer description we'll transmit below |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 580 | BufferDesc newBuffer = {}; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 581 | AHardwareBuffer_Desc* pDesc = |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 582 | reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 583 | pDesc->width = mWidth; |
| 584 | pDesc->height = mHeight; |
| 585 | pDesc->layers = 1; |
| 586 | pDesc->format = mFormat; |
| 587 | pDesc->usage = mUsage; |
| 588 | pDesc->stride = mStride; |
| 589 | newBuffer.buffer.nativeHandle = mBuffers[idx].handle; |
| 590 | newBuffer.pixelSize = sizeof(uint32_t); |
| 591 | newBuffer.bufferId = idx; |
Changyeon Jo | 56c9b37 | 2019-10-09 14:04:31 -0700 | [diff] [blame] | 592 | newBuffer.deviceId = mDescription.v1.cameraId; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 593 | newBuffer.timestamp = elapsedRealtimeNano() * 1e+3; // timestamps is in microseconds |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 594 | |
| 595 | // Write test data into the image buffer |
| 596 | fillTestFrame(newBuffer); |
| 597 | |
| 598 | // Issue the (asynchronous) callback to the client -- can't be holding the lock |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 599 | auto result = mStream->deliverFrame_1_1({newBuffer}); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 600 | if (result.isOk()) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 601 | ALOGD("Delivered %p as id %d", newBuffer.buffer.nativeHandle.getNativeHandle(), |
| 602 | newBuffer.bufferId); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 603 | } else { |
| 604 | // This can happen if the client dies and is likely unrecoverable. |
| 605 | // To avoid consuming resources generating failing calls, we stop sending |
| 606 | // frames. Note, however, that the stream remains in the "STREAMING" state |
| 607 | // until cleaned up on the main thread. |
| 608 | ALOGE("Frame delivery call failed in the transport layer."); |
| 609 | |
| 610 | // Since we didn't actually deliver it, mark the frame as available |
| 611 | std::lock_guard<std::mutex> lock(mAccessLock); |
| 612 | mBuffers[idx].inUse = false; |
| 613 | mFramesInUse--; |
| 614 | |
| 615 | break; |
| 616 | } |
| 617 | } |
| 618 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 619 | // We arbitrarily choose to generate frames at 15 fps to ensure we pass the 10fps test |
| 620 | // requirement |
| 621 | static const int kTargetFrameRate = 15; |
| 622 | static const nsecs_t kTargetFrameIntervalUs = 1000 * 1000 / kTargetFrameRate; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 623 | const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 624 | const nsecs_t elapsedTimeUs = (now - startTime) / 1000; |
| 625 | const nsecs_t sleepDurationUs = kTargetFrameIntervalUs - elapsedTimeUs; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 626 | if (sleepDurationUs > 0) { |
| 627 | usleep(sleepDurationUs); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // If we've been asked to stop, send an event to signal the actual end of stream |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 632 | EvsEventDesc event = { |
| 633 | .aType = EvsEventType::STREAM_STOPPED, |
| 634 | }; |
| 635 | if (!mStream->notify(event).isOk()) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 636 | ALOGE("Error delivering end of stream marker"); |
| 637 | } |
| 638 | |
| 639 | return; |
| 640 | } |
| 641 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 642 | void EvsCamera::fillTestFrame(const BufferDesc& buff) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 643 | // Lock our output buffer for writing |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 644 | uint32_t* pixels = nullptr; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 645 | const AHardwareBuffer_Desc* pDesc = |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 646 | reinterpret_cast<const AHardwareBuffer_Desc*>(&buff.buffer.description); |
| 647 | GraphicBufferMapper& mapper = GraphicBufferMapper::get(); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 648 | mapper.lock(buff.buffer.nativeHandle, |
| 649 | GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER, |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 650 | android::Rect(pDesc->width, pDesc->height), (void**)&pixels); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 651 | |
| 652 | // If we failed to lock the pixel buffer, we're about to crash, but log it first |
| 653 | if (!pixels) { |
| 654 | ALOGE("Camera failed to gain access to image buffer for writing"); |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 655 | return; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 658 | // Fill in the test pixels; the colorbar in ABGR format |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 659 | for (unsigned row = 0; row < pDesc->height; row++) { |
| 660 | for (unsigned col = 0; col < pDesc->width; col++) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 661 | const uint32_t index = col * kNumColors / pDesc->width; |
| 662 | pixels[col] = kColors[index]; |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 663 | } |
| 664 | // Point to the next row |
| 665 | // NOTE: stride retrieved from gralloc is in units of pixels |
| 666 | pixels = pixels + pDesc->stride; |
| 667 | } |
| 668 | |
| 669 | // Release our output buffer |
| 670 | mapper.unlock(buff.buffer.nativeHandle); |
| 671 | } |
| 672 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 673 | void EvsCamera::fillTestFrame(const V1_0::BufferDesc& buff) { |
| 674 | BufferDesc newBuffer = { |
| 675 | .buffer.nativeHandle = buff.memHandle, |
| 676 | .pixelSize = buff.pixelSize, |
| 677 | .bufferId = buff.bufferId, |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 678 | }; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 679 | AHardwareBuffer_Desc* pDesc = |
| 680 | reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description); |
| 681 | *pDesc = { |
| 682 | buff.width, // width |
| 683 | buff.height, // height |
| 684 | 1, // layers, always 1 for EVS |
| 685 | buff.format, // One of AHardwareBuffer_Format |
| 686 | buff.usage, // Combination of AHardwareBuffer_UsageFlags |
| 687 | buff.stride, // Row stride in pixels |
| 688 | 0, // Reserved |
| 689 | 0 // Reserved |
| 690 | }; |
| 691 | return fillTestFrame(newBuffer); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 694 | void EvsCamera::returnBufferLocked(const uint32_t bufferId, const buffer_handle_t memHandle) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 695 | if (memHandle == nullptr) { |
| 696 | ALOGE("ignoring doneWithFrame called with null handle"); |
| 697 | } else if (bufferId >= mBuffers.size()) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 698 | ALOGE("ignoring doneWithFrame called with invalid bufferId %d (max is %zu)", bufferId, |
| 699 | mBuffers.size() - 1); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 700 | } else if (!mBuffers[bufferId].inUse) { |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 701 | ALOGE("ignoring doneWithFrame called on frame %d which is already free", bufferId); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 702 | } else { |
| 703 | // Mark the frame as available |
| 704 | mBuffers[bufferId].inUse = false; |
| 705 | mFramesInUse--; |
| 706 | |
| 707 | // If this frame's index is high in the array, try to move it down |
| 708 | // to improve locality after mFramesAllowed has been reduced. |
| 709 | if (bufferId >= mFramesAllowed) { |
| 710 | // Find an empty slot lower in the array (which should always exist in this case) |
| 711 | for (auto&& rec : mBuffers) { |
| 712 | if (rec.handle == nullptr) { |
| 713 | rec.handle = mBuffers[bufferId].handle; |
| 714 | mBuffers[bufferId].handle = nullptr; |
| 715 | break; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 722 | sp<EvsCamera> EvsCamera::Create(const char* deviceName) { |
| 723 | std::unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr; |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 724 | |
| 725 | return Create(deviceName, nullCamInfo); |
| 726 | } |
| 727 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 728 | sp<EvsCamera> EvsCamera::Create(const char* deviceName, |
| 729 | std::unique_ptr<ConfigManager::CameraInfo>& camInfo, |
| 730 | [[maybe_unused]] const Stream* streamCfg) { |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 731 | sp<EvsCamera> evsCamera = new EvsCamera(deviceName, camInfo); |
| 732 | if (evsCamera == nullptr) { |
| 733 | return nullptr; |
| 734 | } |
| 735 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 736 | // Use the first resolution from the list for the testing |
| 737 | // TODO(b/214835237): Uses a given Stream configuration to choose the best |
| 738 | // stream configuration. |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 739 | auto it = camInfo->streamConfigurations.begin(); |
| 740 | evsCamera->mWidth = it->second[1]; |
| 741 | evsCamera->mHeight = it->second[2]; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 742 | evsCamera->mDescription.v1.vendorFlags = 0xFFFFFFFF; // Arbitrary test value |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 743 | |
| 744 | evsCamera->mFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 745 | evsCamera->mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_CAMERA_WRITE | |
| 746 | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY; |
Changyeon Jo | c6fa0ab | 2019-10-12 05:25:44 -0700 | [diff] [blame] | 747 | |
| 748 | return evsCamera; |
| 749 | } |
| 750 | |
Changyeon Jo | 33ba66b | 2022-01-16 16:33:52 -0800 | [diff] [blame] | 751 | } // namespace android::hardware::automotive::evs::V1_1::implementation |