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