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 | |
| 17 | #define LOG_TAG "VtsHalEvsTest" |
| 18 | |
| 19 | #include "FrameHandler.h" |
| 20 | #include "FormatConvert.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 24 | #include <chrono> |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 25 | |
| 26 | #include <android/log.h> |
| 27 | #include <cutils/native_handle.h> |
| 28 | #include <ui/GraphicBuffer.h> |
| 29 | |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 30 | using namespace std::chrono_literals; |
| 31 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 32 | FrameHandler::FrameHandler(android::sp <IEvsCamera> pCamera, CameraDesc cameraInfo, |
| 33 | android::sp <IEvsDisplay> pDisplay, |
| 34 | BufferControlFlag mode) : |
| 35 | mCamera(pCamera), |
| 36 | mCameraInfo(cameraInfo), |
| 37 | mDisplay(pDisplay), |
| 38 | mReturnMode(mode) { |
| 39 | // Nothing but member initialization here... |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void FrameHandler::shutdown() |
| 44 | { |
| 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 | |
| 55 | bool FrameHandler::startStream() { |
| 56 | // Tell the camera to start streaming |
| 57 | Return<EvsResult> result = mCamera->startVideoStream(this); |
| 58 | if (result != EvsResult::OK) { |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Mark ourselves as running |
| 63 | mLock.lock(); |
| 64 | mRunning = true; |
| 65 | mLock.unlock(); |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void FrameHandler::asyncStopStream() { |
| 72 | // Tell the camera to stop streaming. |
| 73 | // This will result in a null frame being delivered when the stream actually stops. |
| 74 | mCamera->stopVideoStream(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void FrameHandler::blockingStopStream() { |
| 79 | // Tell the stream to stop |
| 80 | asyncStopStream(); |
| 81 | |
| 82 | // Wait until the stream has actually stopped |
| 83 | std::unique_lock<std::mutex> lock(mLock); |
| 84 | if (mRunning) { |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 85 | mEventSignal.wait(lock, [this]() { return !mRunning; }); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | bool FrameHandler::returnHeldBuffer() { |
| 91 | std::unique_lock<std::mutex> lock(mLock); |
| 92 | |
| 93 | // Return the oldest buffer we're holding |
| 94 | if (mHeldBuffers.empty()) { |
| 95 | // No buffers are currently held |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | BufferDesc_1_1 buffer = mHeldBuffers.front(); |
| 100 | mHeldBuffers.pop(); |
| 101 | mCamera->doneWithFrame_1_1(buffer); |
| 102 | |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | bool FrameHandler::isRunning() { |
| 108 | std::unique_lock<std::mutex> lock(mLock); |
| 109 | return mRunning; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void FrameHandler::waitForFrameCount(unsigned frameCount) { |
| 114 | // Wait until we've seen at least the requested number of frames (could be more) |
| 115 | std::unique_lock<std::mutex> lock(mLock); |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 116 | mFrameSignal.wait(lock, [this, frameCount](){ |
| 117 | return mFramesReceived >= frameCount; |
| 118 | }); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | void FrameHandler::getFramesCounters(unsigned* received, unsigned* displayed) { |
| 123 | std::unique_lock<std::mutex> lock(mLock); |
| 124 | |
| 125 | if (received) { |
| 126 | *received = mFramesReceived; |
| 127 | } |
| 128 | if (displayed) { |
| 129 | *displayed = mFramesDisplayed; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | Return<void> FrameHandler::deliverFrame(const BufferDesc_1_0& bufferArg) { |
| 135 | ALOGW("A frame delivered via v1.0 method is rejected."); |
| 136 | mCamera->doneWithFrame(bufferArg); |
| 137 | return Void(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | Return<void> FrameHandler::notifyEvent(const EvsEvent& event) { |
| 142 | // Local flag we use to keep track of when the stream is stopping |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 143 | auto type = event.getDiscriminator(); |
| 144 | if (type == EvsEvent::hidl_discriminator::info) { |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 145 | mLock.lock(); |
| 146 | mLatestEventDesc = event.info(); |
| 147 | if (mLatestEventDesc.aType == InfoEventType::STREAM_STOPPED) { |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 148 | // Signal that the last frame has been received and the stream is stopped |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 149 | mRunning = false; |
| 150 | } else if (mLatestEventDesc.aType == InfoEventType::PARAMETER_CHANGED) { |
| 151 | ALOGD("Camera parameter 0x%X is changed to 0x%X", |
| 152 | mLatestEventDesc.payload[0], mLatestEventDesc.payload[1]); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 153 | } else { |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 154 | ALOGD("Received an event %s", eventToString(mLatestEventDesc.aType)); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 155 | } |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 156 | mLock.unlock(); |
| 157 | mEventSignal.notify_all(); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 158 | } else { |
| 159 | auto bufDesc = event.buffer(); |
| 160 | const AHardwareBuffer_Desc* pDesc = |
| 161 | reinterpret_cast<const AHardwareBuffer_Desc *>(&bufDesc.buffer.description); |
| 162 | ALOGD("Received a frame from the camera (%p)", |
| 163 | bufDesc.buffer.nativeHandle.getNativeHandle()); |
| 164 | |
| 165 | // Store a dimension of a received frame. |
| 166 | mFrameWidth = pDesc->width; |
| 167 | mFrameHeight = pDesc->height; |
| 168 | |
| 169 | // If we were given an opened display at construction time, then send the received |
| 170 | // image back down the camera. |
| 171 | if (mDisplay.get()) { |
| 172 | // Get the output buffer we'll use to display the imagery |
| 173 | BufferDesc_1_0 tgtBuffer = {}; |
| 174 | mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc_1_0& buff) { |
| 175 | tgtBuffer = buff; |
| 176 | } |
| 177 | ); |
| 178 | |
| 179 | if (tgtBuffer.memHandle == nullptr) { |
| 180 | printf("Didn't get target buffer - frame lost\n"); |
| 181 | ALOGE("Didn't get requested output buffer -- skipping this frame."); |
| 182 | } else { |
| 183 | // Copy the contents of the of buffer.memHandle into tgtBuffer |
| 184 | copyBufferContents(tgtBuffer, bufDesc); |
| 185 | |
| 186 | // Send the target buffer back for display |
| 187 | Return<EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer); |
| 188 | if (!result.isOk()) { |
| 189 | printf("HIDL error on display buffer (%s)- frame lost\n", |
| 190 | result.description().c_str()); |
| 191 | ALOGE("Error making the remote function call. HIDL said %s", |
| 192 | result.description().c_str()); |
| 193 | } else if (result != EvsResult::OK) { |
| 194 | printf("Display reported error - frame lost\n"); |
| 195 | ALOGE("We encountered error %d when returning a buffer to the display!", |
| 196 | (EvsResult) result); |
| 197 | } else { |
| 198 | // Everything looks good! |
| 199 | // Keep track so tests or watch dogs can monitor progress |
| 200 | mLock.lock(); |
| 201 | mFramesDisplayed++; |
| 202 | mLock.unlock(); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | |
| 208 | switch (mReturnMode) { |
| 209 | case eAutoReturn: |
| 210 | // Send the camera buffer back now that the client has seen it |
| 211 | ALOGD("Calling doneWithFrame"); |
| 212 | // TODO: Why is it that we get a HIDL crash if we pass back the cloned buffer? |
| 213 | mCamera->doneWithFrame_1_1(bufDesc); |
| 214 | break; |
| 215 | case eNoAutoReturn: |
| 216 | // Hang onto the buffer handle for now -- the client will return it explicitly later |
| 217 | mHeldBuffers.push(bufDesc); |
| 218 | } |
| 219 | |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 220 | mLock.lock(); |
| 221 | ++mFramesReceived; |
| 222 | mLock.unlock(); |
| 223 | mFrameSignal.notify_all(); |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 224 | |
| 225 | ALOGD("Frame handling complete"); |
| 226 | } |
| 227 | |
Changyeon Jo | 2400b69 | 2019-07-18 21:32:48 -0700 | [diff] [blame] | 228 | return Void(); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | bool FrameHandler::copyBufferContents(const BufferDesc_1_0& tgtBuffer, |
| 233 | const BufferDesc_1_1& srcBuffer) { |
| 234 | bool success = true; |
| 235 | const AHardwareBuffer_Desc* pSrcDesc = |
| 236 | reinterpret_cast<const AHardwareBuffer_Desc *>(&srcBuffer.buffer.description); |
| 237 | |
| 238 | // Make sure we don't run off the end of either buffer |
| 239 | const unsigned width = std::min(tgtBuffer.width, |
| 240 | pSrcDesc->width); |
| 241 | const unsigned height = std::min(tgtBuffer.height, |
| 242 | pSrcDesc->height); |
| 243 | |
| 244 | sp<android::GraphicBuffer> tgt = new android::GraphicBuffer(tgtBuffer.memHandle, |
| 245 | android::GraphicBuffer::CLONE_HANDLE, |
| 246 | tgtBuffer.width, |
| 247 | tgtBuffer.height, |
| 248 | tgtBuffer.format, |
| 249 | 1, |
| 250 | tgtBuffer.usage, |
| 251 | tgtBuffer.stride); |
| 252 | sp<android::GraphicBuffer> src = new android::GraphicBuffer(srcBuffer.buffer.nativeHandle, |
| 253 | android::GraphicBuffer::CLONE_HANDLE, |
| 254 | pSrcDesc->width, |
| 255 | pSrcDesc->height, |
| 256 | pSrcDesc->format, |
| 257 | pSrcDesc->layers, |
| 258 | pSrcDesc->usage, |
| 259 | pSrcDesc->stride); |
| 260 | |
| 261 | // Lock our source buffer for reading (current expectation are for this to be NV21 format) |
| 262 | uint8_t* srcPixels = nullptr; |
| 263 | src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels); |
| 264 | |
| 265 | // Lock our target buffer for writing (should be either RGBA8888 or BGRA8888 format) |
| 266 | uint32_t* tgtPixels = nullptr; |
| 267 | tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels); |
| 268 | |
| 269 | if (srcPixels && tgtPixels) { |
| 270 | using namespace ::android::hardware::automotive::evs::common; |
| 271 | if (tgtBuffer.format == HAL_PIXEL_FORMAT_RGBA_8888) { |
| 272 | if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21 |
| 273 | Utils::copyNV21toRGB32(width, height, |
| 274 | srcPixels, |
| 275 | tgtPixels, tgtBuffer.stride); |
| 276 | } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12 |
| 277 | Utils::copyYV12toRGB32(width, height, |
| 278 | srcPixels, |
| 279 | tgtPixels, tgtBuffer.stride); |
| 280 | } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV |
| 281 | Utils::copyYUYVtoRGB32(width, height, |
| 282 | srcPixels, pSrcDesc->stride, |
| 283 | tgtPixels, tgtBuffer.stride); |
| 284 | } else if (pSrcDesc->format == tgtBuffer.format) { // 32bit RGBA |
| 285 | Utils::copyMatchedInterleavedFormats(width, height, |
| 286 | srcPixels, pSrcDesc->stride, |
| 287 | tgtPixels, tgtBuffer.stride, |
| 288 | tgtBuffer.pixelSize); |
| 289 | } else { |
| 290 | ALOGE("Camera buffer format is not supported"); |
| 291 | success = false; |
| 292 | } |
| 293 | } else if (tgtBuffer.format == HAL_PIXEL_FORMAT_BGRA_8888) { |
| 294 | if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21 |
| 295 | Utils::copyNV21toBGR32(width, height, |
| 296 | srcPixels, |
| 297 | tgtPixels, tgtBuffer.stride); |
| 298 | } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12 |
| 299 | Utils::copyYV12toBGR32(width, height, |
| 300 | srcPixels, |
| 301 | tgtPixels, tgtBuffer.stride); |
| 302 | } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV |
| 303 | Utils::copyYUYVtoBGR32(width, height, |
| 304 | srcPixels, pSrcDesc->stride, |
| 305 | tgtPixels, tgtBuffer.stride); |
| 306 | } else if (pSrcDesc->format == tgtBuffer.format) { // 32bit RGBA |
| 307 | Utils::copyMatchedInterleavedFormats(width, height, |
| 308 | srcPixels, pSrcDesc->stride, |
| 309 | tgtPixels, tgtBuffer.stride, |
| 310 | tgtBuffer.pixelSize); |
| 311 | } else { |
| 312 | ALOGE("Camera buffer format is not supported"); |
| 313 | success = false; |
| 314 | } |
| 315 | } else { |
| 316 | // We always expect 32 bit RGB for the display output for now. Is there a need for 565? |
| 317 | ALOGE("Diplay buffer is always expected to be 32bit RGBA"); |
| 318 | success = false; |
| 319 | } |
| 320 | } else { |
| 321 | ALOGE("Failed to lock buffer contents for contents transfer"); |
| 322 | success = false; |
| 323 | } |
| 324 | |
| 325 | if (srcPixels) { |
| 326 | src->unlock(); |
| 327 | } |
| 328 | if (tgtPixels) { |
| 329 | tgt->unlock(); |
| 330 | } |
| 331 | |
| 332 | return success; |
| 333 | } |
| 334 | |
| 335 | void FrameHandler::getFrameDimension(unsigned* width, unsigned* height) { |
| 336 | if (width) { |
| 337 | *width = mFrameWidth; |
| 338 | } |
| 339 | |
| 340 | if (height) { |
| 341 | *height = mFrameHeight; |
| 342 | } |
| 343 | } |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 344 | |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 345 | bool FrameHandler::waitForEvent(const InfoEventType aTargetEvent, |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 346 | InfoEventDesc &eventDesc) { |
| 347 | // Wait until we get an expected parameter change event. |
| 348 | std::unique_lock<std::mutex> lock(mLock); |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 349 | auto now = std::chrono::system_clock::now(); |
| 350 | bool result = mEventSignal.wait_until(lock, now + 5s, |
| 351 | [this, aTargetEvent, &eventDesc](){ |
| 352 | bool flag = mLatestEventDesc.aType == aTargetEvent; |
| 353 | if (flag) { |
| 354 | eventDesc.aType = mLatestEventDesc.aType; |
| 355 | eventDesc.payload[0] = mLatestEventDesc.payload[0]; |
| 356 | eventDesc.payload[1] = mLatestEventDesc.payload[1]; |
| 357 | } |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 358 | |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 359 | return flag; |
| 360 | } |
| 361 | ); |
| 362 | |
| 363 | return !result; |
Changyeon Jo | d2a8246 | 2019-07-30 11:57:17 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Changyeon Jo | 0d0228d | 2019-08-17 21:40:28 -0700 | [diff] [blame] | 366 | const char *FrameHandler::eventToString(const InfoEventType aType) { |
| 367 | switch (aType) { |
| 368 | case InfoEventType::STREAM_STARTED: |
| 369 | return "STREAM_STARTED"; |
| 370 | case InfoEventType::STREAM_STOPPED: |
| 371 | return "STREAM_STOPPED"; |
| 372 | case InfoEventType::FRAME_DROPPED: |
| 373 | return "FRAME_DROPPED"; |
| 374 | case InfoEventType::TIMEOUT: |
| 375 | return "TIMEOUT"; |
| 376 | case InfoEventType::PARAMETER_CHANGED: |
| 377 | return "PARAMETER_CHANGED"; |
| 378 | case InfoEventType::MASTER_RELEASED: |
| 379 | return "MASTER_RELEASED"; |
| 380 | default: |
| 381 | return "Unknown"; |
| 382 | } |
| 383 | } |