blob: b7c7f21c2dc3a5d460dff1deca3e4e26616891fd [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) {
82 mSignal.wait(lock, [this]() { return !mRunning; });
83 }
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);
113 mSignal.wait(lock, [this, frameCount](){ return mFramesReceived >= frameCount; });
114}
115
116
117void FrameHandler::getFramesCounters(unsigned* received, unsigned* displayed) {
118 std::unique_lock<std::mutex> lock(mLock);
119
120 if (received) {
121 *received = mFramesReceived;
122 }
123 if (displayed) {
124 *displayed = mFramesDisplayed;
125 }
126}
127
128
129Return<void> FrameHandler::deliverFrame(const BufferDesc_1_0& bufferArg) {
130 ALOGW("A frame delivered via v1.0 method is rejected.");
131 mCamera->doneWithFrame(bufferArg);
132 return Void();
133}
134
135
136Return<void> FrameHandler::notifyEvent(const EvsEvent& event) {
137 // Local flag we use to keep track of when the stream is stopping
138 bool timeToStop = false;
139
140 auto type = event.getDiscriminator();
141 if (type == EvsEvent::hidl_discriminator::info) {
142 if (event.info() == EvsEventType::STREAM_STOPPED) {
143 // Signal that the last frame has been received and the stream is stopped
144 timeToStop = true;
145 } else {
146 ALOGD("Received an event 0x%X", event.info());
147 }
148 } else {
149 auto bufDesc = event.buffer();
150 const AHardwareBuffer_Desc* pDesc =
151 reinterpret_cast<const AHardwareBuffer_Desc *>(&bufDesc.buffer.description);
152 ALOGD("Received a frame from the camera (%p)",
153 bufDesc.buffer.nativeHandle.getNativeHandle());
154
155 // Store a dimension of a received frame.
156 mFrameWidth = pDesc->width;
157 mFrameHeight = pDesc->height;
158
159 // If we were given an opened display at construction time, then send the received
160 // image back down the camera.
161 if (mDisplay.get()) {
162 // Get the output buffer we'll use to display the imagery
163 BufferDesc_1_0 tgtBuffer = {};
164 mDisplay->getTargetBuffer([&tgtBuffer](const BufferDesc_1_0& buff) {
165 tgtBuffer = buff;
166 }
167 );
168
169 if (tgtBuffer.memHandle == nullptr) {
170 printf("Didn't get target buffer - frame lost\n");
171 ALOGE("Didn't get requested output buffer -- skipping this frame.");
172 } else {
173 // Copy the contents of the of buffer.memHandle into tgtBuffer
174 copyBufferContents(tgtBuffer, bufDesc);
175
176 // Send the target buffer back for display
177 Return<EvsResult> result = mDisplay->returnTargetBufferForDisplay(tgtBuffer);
178 if (!result.isOk()) {
179 printf("HIDL error on display buffer (%s)- frame lost\n",
180 result.description().c_str());
181 ALOGE("Error making the remote function call. HIDL said %s",
182 result.description().c_str());
183 } else if (result != EvsResult::OK) {
184 printf("Display reported error - frame lost\n");
185 ALOGE("We encountered error %d when returning a buffer to the display!",
186 (EvsResult) result);
187 } else {
188 // Everything looks good!
189 // Keep track so tests or watch dogs can monitor progress
190 mLock.lock();
191 mFramesDisplayed++;
192 mLock.unlock();
193 }
194 }
195 }
196
197
198 switch (mReturnMode) {
199 case eAutoReturn:
200 // Send the camera buffer back now that the client has seen it
201 ALOGD("Calling doneWithFrame");
202 // TODO: Why is it that we get a HIDL crash if we pass back the cloned buffer?
203 mCamera->doneWithFrame_1_1(bufDesc);
204 break;
205 case eNoAutoReturn:
206 // Hang onto the buffer handle for now -- the client will return it explicitly later
207 mHeldBuffers.push(bufDesc);
208 }
209
210
211 ALOGD("Frame handling complete");
212 }
213
214
215 // Update our received frame count and notify anybody who cares that things have changed
216 mLock.lock();
217 if (timeToStop) {
218 mRunning = false;
219 } else {
220 mFramesReceived++;
221 }
222 mLock.unlock();
223 mSignal.notify_all();
224
225 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}