Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include <android/log.h> |
| 25 | #include <cutils/native_handle.h> |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 26 | #include <ui/GraphicBuffer.h> |
| 27 | |
| 28 | #include <algorithm> // std::min |
| 29 | |
| 30 | |
| 31 | // For the moment, we're assuming that the underlying EVS driver we're working with |
| 32 | // is providing 4 byte RGBx data. This is fine for loopback testing, although |
| 33 | // real hardware is expected to provide YUV data -- most likly formatted as YV12 |
| 34 | static const unsigned kBytesPerPixel = 4; // assuming 4 byte RGBx pixels |
| 35 | |
| 36 | |
| 37 | FrameHandler::FrameHandler(android::sp <IEvsCamera> pCamera, CameraDesc cameraInfo, |
| 38 | android::sp <IEvsDisplay> pDisplay, |
| 39 | BufferControlFlag mode) : |
| 40 | mCamera(pCamera), |
| 41 | mCameraInfo(cameraInfo), |
| 42 | mDisplay(pDisplay), |
| 43 | mReturnMode(mode) { |
| 44 | // Nothing but member initialization here... |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void FrameHandler::shutdown() |
| 49 | { |
| 50 | // Make sure we're not still streaming |
| 51 | blockingStopStream(); |
| 52 | |
| 53 | // At this point, the receiver thread is no longer running, so we can safely drop |
| 54 | // our remote object references so they can be freed |
| 55 | mCamera = nullptr; |
| 56 | mDisplay = nullptr; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | bool FrameHandler::startStream() { |
| 61 | // Mark ourselves as running |
| 62 | mLock.lock(); |
| 63 | mRunning = true; |
| 64 | mLock.unlock(); |
| 65 | |
| 66 | // Tell the camera to start streaming |
| 67 | Return<EvsResult> result = mCamera->startVideoStream(this); |
| 68 | return (result == EvsResult::OK); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void FrameHandler::asyncStopStream() { |
| 73 | // Tell the camera to stop streaming. |
| 74 | // This will result in a null frame being delivered when the stream actually stops. |
| 75 | mCamera->stopVideoStream(); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void FrameHandler::blockingStopStream() { |
| 80 | // Tell the stream to stop |
| 81 | asyncStopStream(); |
| 82 | |
| 83 | // Wait until the stream has actually stopped |
| 84 | std::unique_lock<std::mutex> lock(mLock); |
| 85 | mSignal.wait(lock, [this](){ return !mRunning; }); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | bool FrameHandler::returnHeldBuffer() { |
| 90 | std::unique_lock<std::mutex> lock(mLock); |
| 91 | |
| 92 | // Return the oldest buffer we're holding |
| 93 | if (mHeldBuffers.empty()) { |
| 94 | // No buffers are currently held |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | BufferDesc buffer = mHeldBuffers.front(); |
| 99 | mHeldBuffers.pop(); |
| 100 | mCamera->doneWithFrame(buffer); |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool FrameHandler::isRunning() { |
| 107 | std::unique_lock<std::mutex> lock(mLock); |
| 108 | return mRunning; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void FrameHandler::waitForFrameCount(unsigned frameCount) { |
| 113 | // Wait until we've seen at least the requested number of frames (could be more) |
| 114 | std::unique_lock<std::mutex> lock(mLock); |
| 115 | mSignal.wait(lock, [this, frameCount](){ return mFramesReceived >= frameCount; }); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void FrameHandler::getFramesCounters(unsigned* received, unsigned* displayed) { |
| 120 | std::unique_lock<std::mutex> lock(mLock); |
| 121 | |
| 122 | if (received) { |
| 123 | *received = mFramesReceived; |
| 124 | } |
| 125 | if (displayed) { |
| 126 | *displayed = mFramesDisplayed; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | Return<void> FrameHandler::deliverFrame(const BufferDesc& bufferArg) { |
| 132 | ALOGD("Received a frame from the camera (%p)", bufferArg.memHandle.getNativeHandle()); |
| 133 | |
| 134 | // Local flag we use to keep track of when the stream is stopping |
| 135 | bool timeToStop = false; |
| 136 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 137 | if (bufferArg.memHandle.getNativeHandle() == nullptr) { |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 138 | // Signal that the last frame has been received and the stream is stopped |
| 139 | timeToStop = true; |
| 140 | } else { |
| 141 | // If we were given an opened display at construction time, then send the received |
| 142 | // image back down the camera. |
| 143 | if (mDisplay.get()) { |
| 144 | // Get the output buffer we'll use to display the imagery |
| 145 | BufferDesc tgtBuffer = {}; |
| 146 | mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc& buff) { |
| 147 | tgtBuffer = buff; |
| 148 | } |
| 149 | ); |
| 150 | |
| 151 | if (tgtBuffer.memHandle == nullptr) { |
| 152 | printf("Didn't get target buffer - frame lost\n"); |
| 153 | ALOGE("Didn't get requested output buffer -- skipping this frame."); |
| 154 | } else { |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 155 | // Copy the contents of the of buffer.memHandle into tgtBuffer |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 156 | copyBufferContents(tgtBuffer, bufferArg); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 157 | |
| 158 | // Send the target buffer back for display |
| 159 | Return <EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer); |
| 160 | if (!result.isOk()) { |
| 161 | printf("HIDL error on display buffer (%s)- frame lost\n", |
| 162 | result.description().c_str()); |
| 163 | ALOGE("Error making the remote function call. HIDL said %s", |
| 164 | result.description().c_str()); |
| 165 | } else if (result != EvsResult::OK) { |
| 166 | printf("Display reported error - frame lost\n"); |
| 167 | ALOGE("We encountered error %d when returning a buffer to the display!", |
| 168 | (EvsResult) result); |
| 169 | } else { |
| 170 | // Everything looks good! |
| 171 | // Keep track so tests or watch dogs can monitor progress |
| 172 | mLock.lock(); |
| 173 | mFramesDisplayed++; |
| 174 | mLock.unlock(); |
| 175 | } |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | switch (mReturnMode) { |
| 181 | case eAutoReturn: |
| 182 | // Send the camera buffer back now that we're done with it |
| 183 | ALOGD("Calling doneWithFrame"); |
| 184 | // TODO: Why is it that we get a HIDL crash if we pass back the cloned buffer? |
| 185 | mCamera->doneWithFrame(bufferArg); |
| 186 | break; |
| 187 | case eNoAutoReturn: |
| 188 | // Hang onto the buffer handle for now -- we'll return it explicitly later |
| 189 | mHeldBuffers.push(bufferArg); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | ALOGD("Frame handling complete"); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | // Update our received frame count and notify anybody who cares that things have changed |
| 198 | mLock.lock(); |
| 199 | if (timeToStop) { |
| 200 | mRunning = false; |
| 201 | } else { |
| 202 | mFramesReceived++; |
| 203 | } |
| 204 | mLock.unlock(); |
| 205 | mSignal.notify_all(); |
| 206 | |
| 207 | |
| 208 | return Void(); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | bool FrameHandler::copyBufferContents(const BufferDesc& tgtBuffer, |
| 213 | const BufferDesc& srcBuffer) { |
| 214 | bool success = true; |
| 215 | |
| 216 | // Make sure we don't run off the end of either buffer |
| 217 | const unsigned width = std::min(tgtBuffer.width, |
| 218 | srcBuffer.width); |
| 219 | const unsigned height = std::min(tgtBuffer.height, |
| 220 | srcBuffer.height); |
| 221 | |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 222 | sp<android::GraphicBuffer> tgt = new android::GraphicBuffer( |
| 223 | tgtBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE, |
| 224 | tgtBuffer.width, tgtBuffer.height, tgtBuffer.format, 1, tgtBuffer.usage, |
| 225 | tgtBuffer.stride); |
| 226 | sp<android::GraphicBuffer> src = new android::GraphicBuffer( |
| 227 | srcBuffer.memHandle, android::GraphicBuffer::CLONE_HANDLE, |
| 228 | srcBuffer.width, srcBuffer.height, srcBuffer.format, 1, srcBuffer.usage, |
| 229 | srcBuffer.stride); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 230 | |
| 231 | // Lock our source buffer for reading |
| 232 | unsigned char* srcPixels = nullptr; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 233 | src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 234 | |
| 235 | // Lock our target buffer for writing |
| 236 | unsigned char* tgtPixels = nullptr; |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 237 | tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 238 | |
| 239 | if (srcPixels && tgtPixels) { |
| 240 | for (unsigned row = 0; row < height; row++) { |
| 241 | // Copy the entire row of pixel data |
| 242 | memcpy(tgtPixels, srcPixels, width * kBytesPerPixel); |
| 243 | |
| 244 | // Advance to the next row (keeping in mind that stride here is in units of pixels) |
| 245 | tgtPixels += tgtBuffer.stride * kBytesPerPixel; |
| 246 | srcPixels += srcBuffer.stride * kBytesPerPixel; |
| 247 | } |
| 248 | } else { |
| 249 | ALOGE("Failed to copy buffer contents"); |
| 250 | success = false; |
| 251 | } |
| 252 | |
| 253 | if (srcPixels) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 254 | src->unlock(); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 255 | } |
| 256 | if (tgtPixels) { |
Chia-I Wu | 79d13ff | 2017-03-31 12:48:11 -0700 | [diff] [blame] | 257 | tgt->unlock(); |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 258 | } |
Scott Randolph | 6c08558 | 2017-03-30 14:24:14 -0700 | [diff] [blame] | 259 | |
| 260 | return success; |
| 261 | } |