Changyeon Jo | 8018901 | 2021-10-10 16:34:21 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "VtsHalEvsTest" |
| 18 | |
| 19 | #include "FrameHandler.h" |
| 20 | #include "FormatConvert.h" |
| 21 | |
| 22 | #include <aidl/android/hardware/graphics/common/HardwareBufferDescription.h> |
| 23 | #include <aidlcommonsupport/NativeHandle.h> |
| 24 | #include <android-base/logging.h> |
| 25 | #include <ui/GraphicBuffer.h> |
| 26 | #include <ui/GraphicBufferAllocator.h> |
| 27 | |
| 28 | using ::aidl::android::hardware::automotive::evs::BufferDesc; |
| 29 | using ::aidl::android::hardware::automotive::evs::CameraDesc; |
| 30 | using ::aidl::android::hardware::automotive::evs::EvsEventDesc; |
| 31 | using ::aidl::android::hardware::automotive::evs::EvsEventType; |
| 32 | using ::aidl::android::hardware::automotive::evs::IEvsCamera; |
| 33 | using ::aidl::android::hardware::automotive::evs::IEvsDisplay; |
| 34 | using ::aidl::android::hardware::graphics::common::HardwareBufferDescription; |
| 35 | using ::ndk::ScopedAStatus; |
| 36 | using std::chrono_literals::operator""s; |
| 37 | |
| 38 | FrameHandler::FrameHandler(const std::shared_ptr<IEvsCamera>& pCamera, const CameraDesc& cameraInfo, |
| 39 | const std::shared_ptr<IEvsDisplay>& pDisplay, BufferControlFlag mode) |
| 40 | : mCamera(pCamera), mCameraInfo(cameraInfo), mDisplay(pDisplay), mReturnMode(mode) { |
| 41 | // Nothing but member initialization here. |
| 42 | } |
| 43 | |
| 44 | void FrameHandler::shutdown() { |
| 45 | // Make sure we're not still streaming |
| 46 | blockingStopStream(); |
| 47 | |
| 48 | // At this point, the receiver thread is no longer running, so we can safely drop |
| 49 | // our remote object references so they can be freed |
| 50 | mCamera = nullptr; |
| 51 | mDisplay = nullptr; |
| 52 | } |
| 53 | |
| 54 | bool FrameHandler::startStream() { |
| 55 | // Tell the camera to start streaming |
| 56 | auto status = mCamera->startVideoStream(ref<FrameHandler>()); |
| 57 | if (!status.isOk()) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Mark ourselves as running |
| 62 | mLock.lock(); |
| 63 | mRunning = true; |
| 64 | mLock.unlock(); |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | void FrameHandler::asyncStopStream() { |
| 70 | // Tell the camera to stop streaming. |
| 71 | // This will result in a null frame being delivered when the stream actually stops. |
| 72 | mCamera->stopVideoStream(); |
| 73 | } |
| 74 | |
| 75 | void FrameHandler::blockingStopStream() { |
| 76 | // Tell the stream to stop |
| 77 | asyncStopStream(); |
| 78 | |
| 79 | // Wait until the stream has actually stopped |
| 80 | std::unique_lock<std::mutex> lock(mEventLock); |
| 81 | if (mRunning) { |
| 82 | mEventSignal.wait(lock, [this]() { return !mRunning; }); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool FrameHandler::returnHeldBuffer() { |
| 87 | std::lock_guard<std::mutex> lock(mLock); |
| 88 | |
| 89 | // Return the oldest buffer we're holding |
| 90 | if (mHeldBuffers.empty()) { |
| 91 | // No buffers are currently held |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | std::vector<BufferDesc> buffers = std::move(mHeldBuffers.front()); |
| 96 | mHeldBuffers.pop(); |
| 97 | mCamera->doneWithFrame(buffers); |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool FrameHandler::isRunning() { |
| 103 | std::lock_guard<std::mutex> lock(mLock); |
| 104 | return mRunning; |
| 105 | } |
| 106 | |
| 107 | void FrameHandler::waitForFrameCount(unsigned frameCount) { |
| 108 | // Wait until we've seen at least the requested number of frames (could be more) |
| 109 | std::unique_lock<std::mutex> lock(mLock); |
| 110 | mFrameSignal.wait(lock, [this, frameCount]() { return mFramesReceived >= frameCount; }); |
| 111 | } |
| 112 | |
| 113 | void FrameHandler::getFramesCounters(unsigned* received, unsigned* displayed) { |
| 114 | std::lock_guard<std::mutex> lock(mLock); |
| 115 | |
| 116 | if (received) { |
| 117 | *received = mFramesReceived; |
| 118 | } |
| 119 | if (displayed) { |
| 120 | *displayed = mFramesDisplayed; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | ScopedAStatus FrameHandler::deliverFrame(const std::vector<BufferDesc>& buffers) { |
| 125 | mLock.lock(); |
| 126 | // For VTS tests, FrameHandler uses a single frame among delivered frames. |
| 127 | auto bufferIdx = mFramesDisplayed % buffers.size(); |
| 128 | auto& buffer = buffers[bufferIdx]; |
| 129 | mLock.unlock(); |
| 130 | |
| 131 | // Store a dimension of a received frame. |
| 132 | mFrameWidth = buffer.buffer.description.width; |
| 133 | mFrameHeight = buffer.buffer.description.height; |
| 134 | |
| 135 | // If we were given an opened display at construction time, then send the received |
| 136 | // image back down the camera. |
| 137 | bool displayed = false; |
| 138 | if (mDisplay) { |
| 139 | // Get the output buffer we'll use to display the imagery |
| 140 | BufferDesc tgtBuffer; |
| 141 | auto status = mDisplay->getTargetBuffer(&tgtBuffer); |
| 142 | if (!status.isOk()) { |
| 143 | printf("Didn't get target buffer - frame lost\n"); |
| 144 | LOG(ERROR) << "Didn't get requested output buffer -- skipping this frame."; |
| 145 | } else { |
| 146 | // Copy the contents of the of buffer.memHandle into tgtBuffer |
| 147 | copyBufferContents(tgtBuffer, buffer); |
| 148 | |
| 149 | // Send the target buffer back for display |
| 150 | auto status = mDisplay->returnTargetBufferForDisplay(tgtBuffer); |
| 151 | if (!status.isOk()) { |
| 152 | printf("AIDL error on display buffer (%d)- frame lost\n", |
| 153 | status.getServiceSpecificError()); |
| 154 | LOG(ERROR) << "Error making the remote function call. AIDL said " |
| 155 | << status.getServiceSpecificError(); |
| 156 | } else { |
| 157 | // Everything looks good! |
| 158 | // Keep track so tests or watch dogs can monitor progress |
| 159 | displayed = true; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | mLock.lock(); |
| 165 | // increases counters |
| 166 | ++mFramesReceived; |
| 167 | mFramesDisplayed += (int)displayed; |
| 168 | mLock.unlock(); |
| 169 | mFrameSignal.notify_all(); |
| 170 | |
| 171 | switch (mReturnMode) { |
| 172 | case eAutoReturn: |
| 173 | // Send the camera buffer back now that the client has seen it |
| 174 | LOG(DEBUG) << "Calling doneWithFrame"; |
| 175 | mCamera->doneWithFrame(buffers); |
| 176 | break; |
| 177 | case eNoAutoReturn: |
| 178 | // Hang onto the buffer handles for now -- the client will return it explicitly later |
| 179 | // mHeldBuffers.push(buffers); |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | LOG(DEBUG) << "Frame handling complete"; |
| 184 | return ScopedAStatus::ok(); |
| 185 | } |
| 186 | |
| 187 | ScopedAStatus FrameHandler::notify(const EvsEventDesc& event) { |
| 188 | // Local flag we use to keep track of when the stream is stopping |
| 189 | std::unique_lock<std::mutex> lock(mEventLock); |
| 190 | mLatestEventDesc.aType = event.aType; |
| 191 | mLatestEventDesc.payload[0] = event.payload[0]; |
| 192 | mLatestEventDesc.payload[1] = event.payload[1]; |
| 193 | if (mLatestEventDesc.aType == EvsEventType::STREAM_STOPPED) { |
| 194 | // Signal that the last frame has been received and the stream is stopped |
| 195 | mRunning = false; |
| 196 | } else if (mLatestEventDesc.aType == EvsEventType::PARAMETER_CHANGED) { |
| 197 | LOG(DEBUG) << "Camera parameter " << mLatestEventDesc.payload[0] << " is changed to " |
| 198 | << mLatestEventDesc.payload[1]; |
| 199 | } else { |
| 200 | LOG(DEBUG) << "Received an event " << eventToString(mLatestEventDesc.aType); |
| 201 | } |
| 202 | lock.unlock(); |
| 203 | mEventSignal.notify_one(); |
| 204 | |
| 205 | return ScopedAStatus::ok(); |
| 206 | } |
| 207 | |
| 208 | bool FrameHandler::copyBufferContents(const BufferDesc& tgtBuffer, const BufferDesc& srcBuffer) { |
| 209 | bool success = true; |
| 210 | const HardwareBufferDescription* pSrcDesc = |
| 211 | reinterpret_cast<const HardwareBufferDescription*>(&srcBuffer.buffer.description); |
| 212 | const HardwareBufferDescription* pTgtDesc = |
| 213 | reinterpret_cast<const HardwareBufferDescription*>(&tgtBuffer.buffer.description); |
| 214 | |
| 215 | // Make sure we don't run off the end of either buffer |
| 216 | const unsigned width = std::min(pTgtDesc->width, pSrcDesc->width); |
| 217 | const unsigned height = std::min(pTgtDesc->height, pSrcDesc->height); |
| 218 | |
| 219 | // FIXME: We duplicate file descriptors twice below; consider using TAKE_HANDLE |
| 220 | // instead of CLONE_HANDLE. |
| 221 | buffer_handle_t target = ::android::dupFromAidl(tgtBuffer.buffer.handle); |
| 222 | ::android::sp<android::GraphicBuffer> tgt = new android::GraphicBuffer( |
| 223 | target, android::GraphicBuffer::CLONE_HANDLE, pTgtDesc->width, pTgtDesc->height, |
| 224 | static_cast<android::PixelFormat>(pTgtDesc->format), pTgtDesc->layers, |
| 225 | static_cast<uint64_t>(pTgtDesc->usage), pTgtDesc->stride); |
| 226 | |
| 227 | buffer_handle_t source = ::android::dupFromAidl(srcBuffer.buffer.handle); |
| 228 | ::android::sp<android::GraphicBuffer> src = new android::GraphicBuffer( |
| 229 | source, android::GraphicBuffer::CLONE_HANDLE, pSrcDesc->width, pSrcDesc->height, |
| 230 | static_cast<android::PixelFormat>(pSrcDesc->format), pSrcDesc->layers, |
| 231 | static_cast<uint64_t>(pSrcDesc->usage), pSrcDesc->stride); |
| 232 | |
| 233 | // Lock our source buffer for reading (current expectation are for this to be NV21 format) |
| 234 | uint8_t* srcPixels = nullptr; |
| 235 | src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels); |
| 236 | |
| 237 | // Lock our target buffer for writing (should be either RGBA8888 or BGRA8888 format) |
| 238 | uint32_t* tgtPixels = nullptr; |
| 239 | tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels); |
| 240 | |
| 241 | if (srcPixels && tgtPixels) { |
| 242 | using namespace ::android::hardware::automotive::evs::common; |
| 243 | if (static_cast<android_pixel_format_t>(pTgtDesc->format) == HAL_PIXEL_FORMAT_RGBA_8888) { |
| 244 | if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 245 | HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21 |
| 246 | Utils::copyNV21toRGB32(width, height, srcPixels, tgtPixels, pTgtDesc->stride); |
| 247 | } else if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 248 | HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12 |
| 249 | Utils::copyYV12toRGB32(width, height, srcPixels, tgtPixels, pTgtDesc->stride); |
| 250 | } else if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 251 | HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV |
| 252 | Utils::copyYUYVtoRGB32(width, height, srcPixels, pSrcDesc->stride, tgtPixels, |
| 253 | pTgtDesc->stride); |
| 254 | } else if (pSrcDesc->format == pTgtDesc->format) { // 32bit RGBA |
| 255 | Utils::copyMatchedInterleavedFormats(width, height, srcPixels, pSrcDesc->stride, |
| 256 | tgtPixels, pTgtDesc->stride, |
| 257 | tgtBuffer.pixelSizeBytes); |
| 258 | } else { |
| 259 | LOG(ERROR) << "Camera buffer format is not supported"; |
| 260 | success = false; |
| 261 | } |
| 262 | } else if (static_cast<android_pixel_format_t>(pTgtDesc->format) == |
| 263 | HAL_PIXEL_FORMAT_BGRA_8888) { |
| 264 | if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 265 | HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21 |
| 266 | Utils::copyNV21toBGR32(width, height, srcPixels, tgtPixels, pTgtDesc->stride); |
| 267 | } else if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 268 | HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12 |
| 269 | Utils::copyYV12toBGR32(width, height, srcPixels, tgtPixels, pTgtDesc->stride); |
| 270 | } else if (static_cast<android_pixel_format_t>(pSrcDesc->format) == |
| 271 | HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV |
| 272 | Utils::copyYUYVtoBGR32(width, height, srcPixels, pSrcDesc->stride, tgtPixels, |
| 273 | pTgtDesc->stride); |
| 274 | } else if (pSrcDesc->format == pTgtDesc->format) { // 32bit RGBA |
| 275 | Utils::copyMatchedInterleavedFormats(width, height, srcPixels, pSrcDesc->stride, |
| 276 | tgtPixels, pTgtDesc->stride, |
| 277 | tgtBuffer.pixelSizeBytes); |
| 278 | } else { |
| 279 | LOG(ERROR) << "Camera buffer format is not supported"; |
| 280 | success = false; |
| 281 | } |
| 282 | } else { |
| 283 | // We always expect 32 bit RGB for the display output for now. Is there a need for 565? |
| 284 | LOG(ERROR) << "Diplay buffer is always expected to be 32bit RGBA"; |
| 285 | success = false; |
| 286 | } |
| 287 | } else { |
| 288 | LOG(ERROR) << "Failed to lock buffer contents for contents transfer"; |
| 289 | success = false; |
| 290 | } |
| 291 | |
| 292 | if (srcPixels) { |
| 293 | src->unlock(); |
| 294 | } |
| 295 | if (tgtPixels) { |
| 296 | tgt->unlock(); |
| 297 | } |
| 298 | |
| 299 | return success; |
| 300 | } |
| 301 | |
| 302 | void FrameHandler::getFrameDimension(unsigned* width, unsigned* height) { |
| 303 | if (width) { |
| 304 | *width = mFrameWidth; |
| 305 | } |
| 306 | |
| 307 | if (height) { |
| 308 | *height = mFrameHeight; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | bool FrameHandler::waitForEvent(const EvsEventDesc& aTargetEvent, EvsEventDesc& aReceivedEvent, |
| 313 | bool ignorePayload) { |
| 314 | // Wait until we get an expected parameter change event. |
| 315 | std::unique_lock<std::mutex> lock(mEventLock); |
| 316 | auto now = std::chrono::system_clock::now(); |
| 317 | bool found = false; |
| 318 | while (!found) { |
| 319 | bool result = mEventSignal.wait_until( |
| 320 | lock, now + 5s, [this, aTargetEvent, ignorePayload, &aReceivedEvent, &found]() { |
| 321 | found = (mLatestEventDesc.aType == aTargetEvent.aType) && |
| 322 | (ignorePayload || |
| 323 | (mLatestEventDesc.payload[0] == aTargetEvent.payload[0] && |
| 324 | mLatestEventDesc.payload[1] == aTargetEvent.payload[1])); |
| 325 | |
| 326 | aReceivedEvent.aType = mLatestEventDesc.aType; |
| 327 | aReceivedEvent.payload[0] = mLatestEventDesc.payload[0]; |
| 328 | aReceivedEvent.payload[1] = mLatestEventDesc.payload[1]; |
| 329 | return found; |
| 330 | }); |
| 331 | |
| 332 | if (!result) { |
| 333 | LOG(WARNING) << "A timer is expired before a target event has happened."; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return found; |
| 339 | } |
| 340 | |
| 341 | const char* FrameHandler::eventToString(const EvsEventType aType) { |
| 342 | switch (aType) { |
| 343 | case EvsEventType::STREAM_STARTED: |
| 344 | return "STREAM_STARTED"; |
| 345 | case EvsEventType::STREAM_STOPPED: |
| 346 | return "STREAM_STOPPED"; |
| 347 | case EvsEventType::FRAME_DROPPED: |
| 348 | return "FRAME_DROPPED"; |
| 349 | case EvsEventType::TIMEOUT: |
| 350 | return "TIMEOUT"; |
| 351 | case EvsEventType::PARAMETER_CHANGED: |
| 352 | return "PARAMETER_CHANGED"; |
| 353 | case EvsEventType::MASTER_RELEASED: |
| 354 | return "MASTER_RELEASED"; |
| 355 | default: |
| 356 | return "Unknown"; |
| 357 | } |
| 358 | } |