blob: 6d53652f866864232e8616988df342b2403b3c09 [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
83 std::unique_lock<std::mutex> lock(mLock);
84 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() {
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
107bool FrameHandler::isRunning() {
108 std::unique_lock<std::mutex> lock(mLock);
109 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) {
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
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 Joc6fa0ab2019-10-12 05:25:44 -0700141Return<void> FrameHandler::deliverFrame_1_1(const BufferDesc_1_1& bufDesc) {
142 const AHardwareBuffer_Desc* pDesc =
143 reinterpret_cast<const AHardwareBuffer_Desc *>(&bufDesc.buffer.description);
144 ALOGD("Received a frame from the camera (%p)",
145 bufDesc.buffer.nativeHandle.getNativeHandle());
146
147 // Store a dimension of a received frame.
148 mFrameWidth = pDesc->width;
149 mFrameHeight = pDesc->height;
150
151 // If we were given an opened display at construction time, then send the received
152 // image back down the camera.
153 if (mDisplay.get()) {
154 // Get the output buffer we'll use to display the imagery
155 BufferDesc_1_0 tgtBuffer = {};
156 mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc_1_0& buff) {
157 tgtBuffer = buff;
158 }
159 );
160
161 if (tgtBuffer.memHandle == nullptr) {
162 printf("Didn't get target buffer - frame lost\n");
163 ALOGE("Didn't get requested output buffer -- skipping this frame.");
Changyeon Jo2400b692019-07-18 21:32:48 -0700164 } else {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700165 // Copy the contents of the of buffer.memHandle into tgtBuffer
166 copyBufferContents(tgtBuffer, bufDesc);
Changyeon Jo2400b692019-07-18 21:32:48 -0700167
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700168 // Send the target buffer back for display
169 Return<EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer);
170 if (!result.isOk()) {
171 printf("HIDL error on display buffer (%s)- frame lost\n",
172 result.description().c_str());
173 ALOGE("Error making the remote function call. HIDL said %s",
174 result.description().c_str());
175 } else if (result != EvsResult::OK) {
176 printf("Display reported error - frame lost\n");
177 ALOGE("We encountered error %d when returning a buffer to the display!",
178 (EvsResult) result);
Changyeon Jo2400b692019-07-18 21:32:48 -0700179 } else {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700180 // Everything looks good!
181 // Keep track so tests or watch dogs can monitor progress
182 mLock.lock();
183 mFramesDisplayed++;
184 mLock.unlock();
Changyeon Jo2400b692019-07-18 21:32:48 -0700185 }
186 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700187 }
188
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700189
190 switch (mReturnMode) {
191 case eAutoReturn:
192 // Send the camera buffer back now that the client has seen it
193 ALOGD("Calling doneWithFrame");
194 mCamera->doneWithFrame_1_1(bufDesc);
195 break;
196 case eNoAutoReturn:
197 // Hang onto the buffer handle for now -- the client will return it explicitly later
198 mHeldBuffers.push(bufDesc);
199 }
200
201 mLock.lock();
202 ++mFramesReceived;
203 mLock.unlock();
204 mFrameSignal.notify_all();
205
206 ALOGD("Frame handling complete");
207
208 return Void();
209}
210
211
212Return<void> FrameHandler::notify(const EvsEvent& event) {
213 // Local flag we use to keep track of when the stream is stopping
214 mLock.lock();
215 mLatestEventDesc = event;
216 if (mLatestEventDesc.aType == EvsEventType::STREAM_STOPPED) {
217 // Signal that the last frame has been received and the stream is stopped
218 mRunning = false;
219 } else if (mLatestEventDesc.aType == EvsEventType::PARAMETER_CHANGED) {
220 ALOGD("Camera parameter 0x%X is changed to 0x%X",
221 mLatestEventDesc.payload[0], mLatestEventDesc.payload[1]);
222 } else {
223 ALOGD("Received an event %s", eventToString(mLatestEventDesc.aType));
224 }
225 mLock.unlock();
226 mEventSignal.notify_all();
227
Changyeon Jo2400b692019-07-18 21:32:48 -0700228 return Void();
229}
230
231
232bool 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
335void FrameHandler::getFrameDimension(unsigned* width, unsigned* height) {
336 if (width) {
337 *width = mFrameWidth;
338 }
339
340 if (height) {
341 *height = mFrameHeight;
342 }
343}
Changyeon Jod2a82462019-07-30 11:57:17 -0700344
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700345bool FrameHandler::waitForEvent(const EvsEventType aTargetEvent,
346 EvsEvent &event) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700347 // Wait until we get an expected parameter change event.
348 std::unique_lock<std::mutex> lock(mLock);
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700349 auto now = std::chrono::system_clock::now();
350 bool result = mEventSignal.wait_until(lock, now + 5s,
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700351 [this, aTargetEvent, &event](){
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700352 bool flag = mLatestEventDesc.aType == aTargetEvent;
353 if (flag) {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700354 event.aType = mLatestEventDesc.aType;
355 event.payload[0] = mLatestEventDesc.payload[0];
356 event.payload[1] = mLatestEventDesc.payload[1];
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700357 }
Changyeon Jod2a82462019-07-30 11:57:17 -0700358
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700359 return flag;
360 }
361 );
362
363 return !result;
Changyeon Jod2a82462019-07-30 11:57:17 -0700364}
365
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700366const char *FrameHandler::eventToString(const EvsEventType aType) {
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700367 switch (aType) {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700368 case EvsEventType::STREAM_STARTED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700369 return "STREAM_STARTED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700370 case EvsEventType::STREAM_STOPPED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700371 return "STREAM_STOPPED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700372 case EvsEventType::FRAME_DROPPED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700373 return "FRAME_DROPPED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700374 case EvsEventType::TIMEOUT:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700375 return "TIMEOUT";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700376 case EvsEventType::PARAMETER_CHANGED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700377 return "PARAMETER_CHANGED";
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700378 case EvsEventType::MASTER_RELEASED:
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700379 return "MASTER_RELEASED";
380 default:
381 return "Unknown";
382 }
383}
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700384