blob: 44783f65c3c65a7ec98132316f378ac1322e5b7f [file] [log] [blame]
Changyeon Jo2400b692019-07-18 21:32:48 -07001/*
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 Jo0d0228d2019-08-17 21:40:28 -070024#include <chrono>
Changyeon Jo2400b692019-07-18 21:32:48 -070025
26#include <android/log.h>
27#include <cutils/native_handle.h>
28#include <ui/GraphicBuffer.h>
29
Changyeon Jo0d0228d2019-08-17 21:40:28 -070030using namespace std::chrono_literals;
31
Changyeon Jo2400b692019-07-18 21:32:48 -070032FrameHandler::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
43void 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
55bool 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
71void 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
78void FrameHandler::blockingStopStream() {
79 // Tell the stream to stop
80 asyncStopStream();
81
82 // Wait until the stream has actually stopped
Changyeon Jo56c9b372019-10-09 14:04:31 -070083 std::unique_lock<std::mutex> lock(mEventLock);
Changyeon Jo2400b692019-07-18 21:32:48 -070084 if (mRunning) {
Changyeon Jod2a82462019-07-30 11:57:17 -070085 mEventSignal.wait(lock, [this]() { return !mRunning; });
Changyeon Jo2400b692019-07-18 21:32:48 -070086 }
87}
88
89
90bool FrameHandler::returnHeldBuffer() {
Changyeon Jo3e80b3b2019-11-25 18:14:00 -080091 std::lock_guard<std::mutex> lock(mLock);
Changyeon Jo2400b692019-07-18 21:32:48 -070092
93 // Return the oldest buffer we're holding
94 if (mHeldBuffers.empty()) {
95 // No buffers are currently held
96 return false;
97 }
98
Changyeon Jo56c9b372019-10-09 14:04:31 -070099 hidl_vec<BufferDesc_1_1> buffers = mHeldBuffers.front();
Changyeon Jo2400b692019-07-18 21:32:48 -0700100 mHeldBuffers.pop();
Changyeon Jo56c9b372019-10-09 14:04:31 -0700101 mCamera->doneWithFrame_1_1(buffers);
Changyeon Jo2400b692019-07-18 21:32:48 -0700102
103 return true;
104}
105
106
107bool FrameHandler::isRunning() {
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800108 std::lock_guard<std::mutex> lock(mLock);
Changyeon Jo2400b692019-07-18 21:32:48 -0700109 return mRunning;
110}
111
112
113void 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 Jod2a82462019-07-30 11:57:17 -0700116 mFrameSignal.wait(lock, [this, frameCount](){
117 return mFramesReceived >= frameCount;
118 });
Changyeon Jo2400b692019-07-18 21:32:48 -0700119}
120
121
122void FrameHandler::getFramesCounters(unsigned* received, unsigned* displayed) {
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800123 std::lock_guard<std::mutex> lock(mLock);
Changyeon Jo2400b692019-07-18 21:32:48 -0700124
125 if (received) {
126 *received = mFramesReceived;
127 }
128 if (displayed) {
129 *displayed = mFramesDisplayed;
130 }
131}
132
133
134Return<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
Changyeon Jo56c9b372019-10-09 14:04:31 -0700141Return<void> FrameHandler::deliverFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffers) {
142 for (auto&& buffer : buffers) {
143 const AHardwareBuffer_Desc* pDesc =
144 reinterpret_cast<const AHardwareBuffer_Desc *>(&buffer.buffer.description);
145 ALOGD("Received a frame from the camera (%p)",
146 buffer.buffer.nativeHandle.getNativeHandle());
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700147
Changyeon Jo56c9b372019-10-09 14:04:31 -0700148 // Store a dimension of a received frame.
149 mFrameWidth = pDesc->width;
150 mFrameHeight = pDesc->height;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700151
Changyeon Jo56c9b372019-10-09 14:04:31 -0700152 // If we were given an opened display at construction time, then send the received
153 // image back down the camera.
154 if (mDisplay.get()) {
155 // Get the output buffer we'll use to display the imagery
156 BufferDesc_1_0 tgtBuffer = {};
157 mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc_1_0& buff) {
158 tgtBuffer = buff;
159 }
160 );
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700161
Changyeon Jo56c9b372019-10-09 14:04:31 -0700162 if (tgtBuffer.memHandle == nullptr) {
163 printf("Didn't get target buffer - frame lost\n");
164 ALOGE("Didn't get requested output buffer -- skipping this frame.");
Changyeon Jo2400b692019-07-18 21:32:48 -0700165 } else {
Changyeon Jo56c9b372019-10-09 14:04:31 -0700166 // Copy the contents of the of buffer.memHandle into tgtBuffer
167 copyBufferContents(tgtBuffer, buffer);
168
169 // Send the target buffer back for display
170 Return<EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer);
171 if (!result.isOk()) {
172 printf("HIDL error on display buffer (%s)- frame lost\n",
173 result.description().c_str());
174 ALOGE("Error making the remote function call. HIDL said %s",
175 result.description().c_str());
176 } else if (result != EvsResult::OK) {
177 printf("Display reported error - frame lost\n");
178 ALOGE("We encountered error %d when returning a buffer to the display!",
179 (EvsResult) result);
180 } else {
181 // Everything looks good!
182 // Keep track so tests or watch dogs can monitor progress
183 mLock.lock();
184 mFramesDisplayed++;
185 mLock.unlock();
186 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700187 }
188 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700189 }
190
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700191
192 switch (mReturnMode) {
193 case eAutoReturn:
194 // Send the camera buffer back now that the client has seen it
195 ALOGD("Calling doneWithFrame");
Changyeon Jo56c9b372019-10-09 14:04:31 -0700196 mCamera->doneWithFrame_1_1(buffers);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700197 break;
198 case eNoAutoReturn:
Changyeon Jo56c9b372019-10-09 14:04:31 -0700199 // Hang onto the buffer handles for now -- the client will return it explicitly later
200 mHeldBuffers.push(buffers);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700201 }
202
203 mLock.lock();
204 ++mFramesReceived;
205 mLock.unlock();
206 mFrameSignal.notify_all();
207
208 ALOGD("Frame handling complete");
209
210 return Void();
211}
212
213
Changyeon Jo56c9b372019-10-09 14:04:31 -0700214Return<void> FrameHandler::notify(const EvsEventDesc& event) {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700215 // Local flag we use to keep track of when the stream is stopping
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800216 std::unique_lock<std::mutex> lock(mEventLock);
217 mLatestEventDesc.aType = event.aType;
218 mLatestEventDesc.payload[0] = event.payload[0];
219 mLatestEventDesc.payload[1] = event.payload[1];
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700220 if (mLatestEventDesc.aType == EvsEventType::STREAM_STOPPED) {
221 // Signal that the last frame has been received and the stream is stopped
222 mRunning = false;
223 } else if (mLatestEventDesc.aType == EvsEventType::PARAMETER_CHANGED) {
224 ALOGD("Camera parameter 0x%X is changed to 0x%X",
225 mLatestEventDesc.payload[0], mLatestEventDesc.payload[1]);
226 } else {
227 ALOGD("Received an event %s", eventToString(mLatestEventDesc.aType));
228 }
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800229 lock.unlock();
Changyeon Jo56c9b372019-10-09 14:04:31 -0700230 mEventSignal.notify_one();
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700231
Changyeon Jo2400b692019-07-18 21:32:48 -0700232 return Void();
233}
234
235
236bool FrameHandler::copyBufferContents(const BufferDesc_1_0& tgtBuffer,
237 const BufferDesc_1_1& srcBuffer) {
238 bool success = true;
239 const AHardwareBuffer_Desc* pSrcDesc =
240 reinterpret_cast<const AHardwareBuffer_Desc *>(&srcBuffer.buffer.description);
241
242 // Make sure we don't run off the end of either buffer
243 const unsigned width = std::min(tgtBuffer.width,
244 pSrcDesc->width);
245 const unsigned height = std::min(tgtBuffer.height,
246 pSrcDesc->height);
247
248 sp<android::GraphicBuffer> tgt = new android::GraphicBuffer(tgtBuffer.memHandle,
249 android::GraphicBuffer::CLONE_HANDLE,
250 tgtBuffer.width,
251 tgtBuffer.height,
252 tgtBuffer.format,
253 1,
254 tgtBuffer.usage,
255 tgtBuffer.stride);
256 sp<android::GraphicBuffer> src = new android::GraphicBuffer(srcBuffer.buffer.nativeHandle,
257 android::GraphicBuffer::CLONE_HANDLE,
258 pSrcDesc->width,
259 pSrcDesc->height,
260 pSrcDesc->format,
261 pSrcDesc->layers,
262 pSrcDesc->usage,
263 pSrcDesc->stride);
264
265 // Lock our source buffer for reading (current expectation are for this to be NV21 format)
266 uint8_t* srcPixels = nullptr;
267 src->lock(GRALLOC_USAGE_SW_READ_OFTEN, (void**)&srcPixels);
268
269 // Lock our target buffer for writing (should be either RGBA8888 or BGRA8888 format)
270 uint32_t* tgtPixels = nullptr;
271 tgt->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)&tgtPixels);
272
273 if (srcPixels && tgtPixels) {
274 using namespace ::android::hardware::automotive::evs::common;
275 if (tgtBuffer.format == HAL_PIXEL_FORMAT_RGBA_8888) {
276 if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21
277 Utils::copyNV21toRGB32(width, height,
278 srcPixels,
279 tgtPixels, tgtBuffer.stride);
280 } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12
281 Utils::copyYV12toRGB32(width, height,
282 srcPixels,
283 tgtPixels, tgtBuffer.stride);
284 } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV
285 Utils::copyYUYVtoRGB32(width, height,
286 srcPixels, pSrcDesc->stride,
287 tgtPixels, tgtBuffer.stride);
288 } else if (pSrcDesc->format == tgtBuffer.format) { // 32bit RGBA
289 Utils::copyMatchedInterleavedFormats(width, height,
290 srcPixels, pSrcDesc->stride,
291 tgtPixels, tgtBuffer.stride,
292 tgtBuffer.pixelSize);
293 } else {
294 ALOGE("Camera buffer format is not supported");
295 success = false;
296 }
297 } else if (tgtBuffer.format == HAL_PIXEL_FORMAT_BGRA_8888) {
298 if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCRCB_420_SP) { // 420SP == NV21
299 Utils::copyNV21toBGR32(width, height,
300 srcPixels,
301 tgtPixels, tgtBuffer.stride);
302 } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YV12) { // YUV_420P == YV12
303 Utils::copyYV12toBGR32(width, height,
304 srcPixels,
305 tgtPixels, tgtBuffer.stride);
306 } else if (pSrcDesc->format == HAL_PIXEL_FORMAT_YCBCR_422_I) { // YUYV
307 Utils::copyYUYVtoBGR32(width, height,
308 srcPixels, pSrcDesc->stride,
309 tgtPixels, tgtBuffer.stride);
310 } else if (pSrcDesc->format == tgtBuffer.format) { // 32bit RGBA
311 Utils::copyMatchedInterleavedFormats(width, height,
312 srcPixels, pSrcDesc->stride,
313 tgtPixels, tgtBuffer.stride,
314 tgtBuffer.pixelSize);
315 } else {
316 ALOGE("Camera buffer format is not supported");
317 success = false;
318 }
319 } else {
320 // We always expect 32 bit RGB for the display output for now. Is there a need for 565?
321 ALOGE("Diplay buffer is always expected to be 32bit RGBA");
322 success = false;
323 }
324 } else {
325 ALOGE("Failed to lock buffer contents for contents transfer");
326 success = false;
327 }
328
329 if (srcPixels) {
330 src->unlock();
331 }
332 if (tgtPixels) {
333 tgt->unlock();
334 }
335
336 return success;
337}
338
339void FrameHandler::getFrameDimension(unsigned* width, unsigned* height) {
340 if (width) {
341 *width = mFrameWidth;
342 }
343
344 if (height) {
345 *height = mFrameHeight;
346 }
347}
Changyeon Jod2a82462019-07-30 11:57:17 -0700348
Changyeon Jo56c9b372019-10-09 14:04:31 -0700349bool FrameHandler::waitForEvent(const EvsEventDesc& aTargetEvent,
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800350 EvsEventDesc& aReceivedEvent,
351 bool ignorePayload) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700352 // Wait until we get an expected parameter change event.
Changyeon Jo56c9b372019-10-09 14:04:31 -0700353 std::unique_lock<std::mutex> lock(mEventLock);
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700354 auto now = std::chrono::system_clock::now();
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800355 bool found = false;
356 while (!found) {
357 bool result = mEventSignal.wait_until(lock, now + 5s,
358 [this, aTargetEvent, ignorePayload, &aReceivedEvent, &found](){
359 found = (mLatestEventDesc.aType == aTargetEvent.aType) &&
360 (ignorePayload || (mLatestEventDesc.payload[0] == aTargetEvent.payload[0] &&
361 mLatestEventDesc.payload[1] == aTargetEvent.payload[1]));
Changyeon Jo56c9b372019-10-09 14:04:31 -0700362
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800363 aReceivedEvent.aType = mLatestEventDesc.aType;
364 aReceivedEvent.payload[0] = mLatestEventDesc.payload[0];
365 aReceivedEvent.payload[1] = mLatestEventDesc.payload[1];
366 return found;
367 }
368 );
Changyeon Jod2a82462019-07-30 11:57:17 -0700369
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800370 if (!result) {
371 ALOGW("A timer is expired before a target event has happened.");
372 break;
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700373 }
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800374 }
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700375
Changyeon Jo3e80b3b2019-11-25 18:14:00 -0800376 return found;
Changyeon Jod2a82462019-07-30 11:57:17 -0700377}
378
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700379const char *FrameHandler::eventToString(const EvsEventType aType) {
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700380 switch (aType) {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700381 case EvsEventType::STREAM_STARTED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700382 return "STREAM_STARTED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700383 case EvsEventType::STREAM_STOPPED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700384 return "STREAM_STOPPED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700385 case EvsEventType::FRAME_DROPPED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700386 return "FRAME_DROPPED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700387 case EvsEventType::TIMEOUT:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700388 return "TIMEOUT";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700389 case EvsEventType::PARAMETER_CHANGED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700390 return "PARAMETER_CHANGED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700391 case EvsEventType::MASTER_RELEASED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700392 return "MASTER_RELEASED";
393 default:
394 return "Unknown";
395 }
396}
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700397