1 /*
2  * Copyright (C) 2016 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 #include "EvsV4lCamera.h"
18 
19 #include "EvsEnumerator.h"
20 #include "bufferCopy.h"
21 
22 #include <android-base/logging.h>
23 #include <android-base/unique_fd.h>
24 #include <android/hardware_buffer.h>
25 #include <ui/GraphicBufferAllocator.h>
26 #include <ui/GraphicBufferMapper.h>
27 #include <utils/SystemClock.h>
28 
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 
32 namespace {
33 
34 // The size of a pixel of RGBA format data in bytes
35 constexpr auto kBytesPerPixelRGBA = 4;
36 
37 // Default camera output image resolution
38 const std::array<int32_t, 2> kDefaultResolution = {640, 480};
39 
40 // Arbitrary limit on number of graphics buffers allowed to be allocated
41 // Safeguards against unreasonable resource consumption and provides a testable limit
42 static const unsigned MAX_BUFFERS_IN_FLIGHT = 100;
43 
44 };  // anonymous namespace
45 
46 namespace android {
47 namespace hardware {
48 namespace automotive {
49 namespace evs {
50 namespace V1_1 {
51 namespace implementation {
52 
EvsV4lCamera(const char * deviceName,std::unique_ptr<ConfigManager::CameraInfo> & camInfo)53 EvsV4lCamera::EvsV4lCamera(const char* deviceName,
54                            std::unique_ptr<ConfigManager::CameraInfo>& camInfo) :
55       mFramesAllowed(0), mFramesInUse(0), mCameraInfo(camInfo) {
56     LOG(DEBUG) << "EvsV4lCamera instantiated";
57 
58     mDescription.v1.cameraId = deviceName;
59     if (camInfo != nullptr) {
60         mDescription.metadata.setToExternal((uint8_t*)camInfo->characteristics,
61                                             get_camera_metadata_size(camInfo->characteristics));
62     }
63 
64     // Default output buffer format.
65     mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
66 
67     // How we expect to use the gralloc buffers we'll exchange with our client
68     mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
69 }
70 
~EvsV4lCamera()71 EvsV4lCamera::~EvsV4lCamera() {
72     LOG(DEBUG) << "EvsV4lCamera being destroyed";
73     shutdown();
74 }
75 
76 //
77 // This gets called if another caller "steals" ownership of the camera
78 //
shutdown()79 void EvsV4lCamera::shutdown() {
80     LOG(DEBUG) << "EvsV4lCamera shutdown";
81 
82     // Make sure our output stream is cleaned up
83     // (It really should be already)
84     stopVideoStream();
85 
86     // Note:  Since stopVideoStream is blocking, no other threads can now be running
87 
88     // Close our video capture device
89     mVideo.close();
90 
91     // Drop all the graphics buffers we've been using
92     if (mBuffers.size() > 0) {
93         GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
94         for (auto&& rec : mBuffers) {
95             if (rec.inUse) {
96                 LOG(WARNING) << "Releasing buffer despite remote ownership";
97             }
98             alloc.free(rec.handle);
99             rec.handle = nullptr;
100         }
101         mBuffers.clear();
102     }
103 }
104 
105 // Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
getCameraInfo(getCameraInfo_cb _hidl_cb)106 Return<void> EvsV4lCamera::getCameraInfo(getCameraInfo_cb _hidl_cb) {
107     LOG(DEBUG) << __FUNCTION__;
108 
109     // Send back our self description
110     _hidl_cb(mDescription.v1);
111     return Void();
112 }
113 
setMaxFramesInFlight(uint32_t bufferCount)114 Return<EvsResult> EvsV4lCamera::setMaxFramesInFlight(uint32_t bufferCount) {
115     LOG(DEBUG) << __FUNCTION__;
116     std::lock_guard<std::mutex> lock(mAccessLock);
117 
118     // If we've been displaced by another owner of the camera, then we can't do anything else
119     if (!mVideo.isOpen()) {
120         LOG(WARNING) << "Ignoring setMaxFramesInFlight call when camera has been lost.";
121         return EvsResult::OWNERSHIP_LOST;
122     }
123 
124     // We cannot function without at least one video buffer to send data
125     if (bufferCount < 1) {
126         LOG(ERROR) << "Ignoring setMaxFramesInFlight with less than one buffer requested";
127         return EvsResult::INVALID_ARG;
128     }
129 
130     // Update our internal state
131     if (setAvailableFrames_Locked(bufferCount)) {
132         return EvsResult::OK;
133     } else {
134         return EvsResult::BUFFER_NOT_AVAILABLE;
135     }
136 }
137 
startVideoStream(const sp<IEvsCameraStream_1_0> & stream)138 Return<EvsResult> EvsV4lCamera::startVideoStream(const sp<IEvsCameraStream_1_0>& stream) {
139     LOG(DEBUG) << __FUNCTION__;
140     std::lock_guard<std::mutex> lock(mAccessLock);
141 
142     // If we've been displaced by another owner of the camera, then we can't do anything else
143     if (!mVideo.isOpen()) {
144         LOG(WARNING) << "Ignoring startVideoStream call when camera has been lost.";
145         return EvsResult::OWNERSHIP_LOST;
146     }
147     if (mStream.get() != nullptr) {
148         LOG(ERROR) << "Ignoring startVideoStream call when a stream is already running.";
149         return EvsResult::STREAM_ALREADY_RUNNING;
150     }
151 
152     // If the client never indicated otherwise, configure ourselves for a single streaming buffer
153     if (mFramesAllowed < 1) {
154         if (!setAvailableFrames_Locked(1)) {
155             LOG(ERROR) << "Failed to start stream because we couldn't get a graphics buffer";
156             return EvsResult::BUFFER_NOT_AVAILABLE;
157         }
158     }
159 
160     // Choose which image transfer function we need
161     // Map from V4L2 to Android graphic buffer format
162     const uint32_t videoSrcFormat = mVideo.getV4LFormat();
163     LOG(INFO) << "Configuring to accept " << (char*)&videoSrcFormat
164               << " camera data and convert to " << std::hex << mFormat;
165 
166     switch (mFormat) {
167         case HAL_PIXEL_FORMAT_YCRCB_420_SP:
168             switch (videoSrcFormat) {
169                 case V4L2_PIX_FMT_NV21:
170                     mFillBufferFromVideo = fillNV21FromNV21;
171                     break;
172                 case V4L2_PIX_FMT_YUYV:
173                     mFillBufferFromVideo = fillNV21FromYUYV;
174                     break;
175                 default:
176                     LOG(ERROR) << "Unhandled camera output format: " << ((char*)&videoSrcFormat)[0]
177                                << ((char*)&videoSrcFormat)[1] << ((char*)&videoSrcFormat)[2]
178                                << ((char*)&videoSrcFormat)[3] << std::hex << videoSrcFormat;
179             }
180             break;
181         case HAL_PIXEL_FORMAT_RGBA_8888:
182             switch (videoSrcFormat) {
183                 case V4L2_PIX_FMT_YUYV:
184                     mFillBufferFromVideo = fillRGBAFromYUYV;
185                     break;
186                 default:
187                     LOG(ERROR) << "Unhandled camera format " << (char*)&videoSrcFormat;
188             }
189             break;
190         case HAL_PIXEL_FORMAT_YCBCR_422_I:
191             switch (videoSrcFormat) {
192                 case V4L2_PIX_FMT_YUYV:
193                     mFillBufferFromVideo = fillYUYVFromYUYV;
194                     break;
195                 case V4L2_PIX_FMT_UYVY:
196                     mFillBufferFromVideo = fillYUYVFromUYVY;
197                     break;
198                 default:
199                     LOG(ERROR) << "Unhandled camera format " << (char*)&videoSrcFormat;
200             }
201             break;
202         default:
203             LOG(ERROR) << "Unhandled camera format " << (char*)&mFormat;
204     }
205 
206     // Record the user's callback for use when we have a frame ready
207     mStream = stream;
208     mStream_1_1 = IEvsCameraStream_1_1::castFrom(mStream).withDefault(nullptr);
209 
210     // Set up the video stream with a callback to our member function forwardFrame()
211     if (!mVideo.startStream([this](VideoCapture*, imageBuffer* tgt, void* data) {
212             this->forwardFrame(tgt, data);
213         })) {
214         // No need to hold onto this if we failed to start
215         mStream = nullptr;
216         mStream_1_1 = nullptr;
217         LOG(ERROR) << "Underlying camera start stream failed";
218         return EvsResult::UNDERLYING_SERVICE_ERROR;
219     }
220 
221     return EvsResult::OK;
222 }
223 
doneWithFrame(const BufferDesc_1_0 & buffer)224 Return<void> EvsV4lCamera::doneWithFrame(const BufferDesc_1_0& buffer) {
225     LOG(DEBUG) << __FUNCTION__;
226     doneWithFrame_impl(buffer.bufferId, buffer.memHandle);
227 
228     return Void();
229 }
230 
stopVideoStream()231 Return<void> EvsV4lCamera::stopVideoStream() {
232     LOG(DEBUG) << __FUNCTION__;
233 
234     // Tell the capture device to stop (and block until it does)
235     mVideo.stopStream();
236 
237     if (mStream_1_1 != nullptr) {
238         // V1.1 client is waiting on STREAM_STOPPED event.
239         std::unique_lock<std::mutex> lock(mAccessLock);
240 
241         EvsEventDesc event;
242         event.aType = EvsEventType::STREAM_STOPPED;
243         auto result = mStream_1_1->notify(event);
244         if (!result.isOk()) {
245             LOG(ERROR) << "Error delivering end of stream event";
246         }
247 
248         // Drop our reference to the client's stream receiver
249         mStream_1_1 = nullptr;
250         mStream = nullptr;
251     } else if (mStream != nullptr) {
252         std::unique_lock<std::mutex> lock(mAccessLock);
253 
254         // Send one last NULL frame to signal the actual end of stream
255         BufferDesc_1_0 nullBuff = {};
256         auto result = mStream->deliverFrame(nullBuff);
257         if (!result.isOk()) {
258             LOG(ERROR) << "Error delivering end of stream marker";
259         }
260 
261         // Drop our reference to the client's stream receiver
262         mStream = nullptr;
263     }
264 
265     return Void();
266 }
267 
getExtendedInfo(uint32_t)268 Return<int32_t> EvsV4lCamera::getExtendedInfo(uint32_t /*opaqueIdentifier*/) {
269     LOG(DEBUG) << __FUNCTION__;
270     // Return zero by default as required by the spec
271     return 0;
272 }
273 
setExtendedInfo(uint32_t,int32_t)274 Return<EvsResult> EvsV4lCamera::setExtendedInfo(uint32_t /*opaqueIdentifier*/,
275                                                 int32_t /*opaqueValue*/) {
276     LOG(DEBUG) << __FUNCTION__;
277     std::lock_guard<std::mutex> lock(mAccessLock);
278 
279     // If we've been displaced by another owner of the camera, then we can't do anything else
280     if (!mVideo.isOpen()) {
281         LOG(WARNING) << "Ignoring setExtendedInfo call when camera has been lost.";
282         return EvsResult::OWNERSHIP_LOST;
283     }
284 
285     // We don't store any device specific information in this implementation
286     return EvsResult::INVALID_ARG;
287 }
288 
289 // Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow.
getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb)290 Return<void> EvsV4lCamera::getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) {
291     LOG(DEBUG) << __FUNCTION__;
292 
293     // Send back our self description
294     _hidl_cb(mDescription);
295     return Void();
296 }
297 
getPhysicalCameraInfo(const hidl_string & id,getPhysicalCameraInfo_cb _hidl_cb)298 Return<void> EvsV4lCamera::getPhysicalCameraInfo(const hidl_string& id,
299                                                  getPhysicalCameraInfo_cb _hidl_cb) {
300     LOG(DEBUG) << __FUNCTION__;
301 
302     // This method works exactly same as getCameraInfo_1_1() in EVS HW module.
303     (void)id;
304     _hidl_cb(mDescription);
305     return Void();
306 }
307 
doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1> & buffers)308 Return<EvsResult> EvsV4lCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffers) {
309     LOG(DEBUG) << __FUNCTION__;
310 
311     for (auto&& buffer : buffers) {
312         doneWithFrame_impl(buffer.bufferId, buffer.buffer.nativeHandle);
313     }
314 
315     return EvsResult::OK;
316 }
317 
pauseVideoStream()318 Return<EvsResult> EvsV4lCamera::pauseVideoStream() {
319     return EvsResult::UNDERLYING_SERVICE_ERROR;
320 }
321 
resumeVideoStream()322 Return<EvsResult> EvsV4lCamera::resumeVideoStream() {
323     return EvsResult::UNDERLYING_SERVICE_ERROR;
324 }
325 
setMaster()326 Return<EvsResult> EvsV4lCamera::setMaster() {
327     /* Because EVS HW module reference implementation expects a single client at
328      * a time, this returns a success code always.
329      */
330     return EvsResult::OK;
331 }
332 
forceMaster(const sp<IEvsDisplay_1_0> &)333 Return<EvsResult> EvsV4lCamera::forceMaster(const sp<IEvsDisplay_1_0>&) {
334     /* Because EVS HW module reference implementation expects a single client at
335      * a time, this returns a success code always.
336      */
337     return EvsResult::OK;
338 }
339 
unsetMaster()340 Return<EvsResult> EvsV4lCamera::unsetMaster() {
341     /* Because EVS HW module reference implementation expects a single client at
342      * a time, there is no chance that this is called by the secondary client and
343      * therefore returns a success code always.
344      */
345     return EvsResult::OK;
346 }
347 
getParameterList(getParameterList_cb _hidl_cb)348 Return<void> EvsV4lCamera::getParameterList(getParameterList_cb _hidl_cb) {
349     hidl_vec<CameraParam> hidlCtrls;
350     if (mCameraInfo != nullptr) {
351         hidlCtrls.resize(mCameraInfo->controls.size());
352         unsigned idx = 0;
353         for (auto& [cid, range] : mCameraInfo->controls) {
354             hidlCtrls[idx++] = cid;
355         }
356     }
357 
358     _hidl_cb(hidlCtrls);
359     return Void();
360 }
361 
getIntParameterRange(CameraParam id,getIntParameterRange_cb _hidl_cb)362 Return<void> EvsV4lCamera::getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) {
363     if (mCameraInfo != nullptr) {
364         auto range = mCameraInfo->controls[id];
365         _hidl_cb(std::get<0>(range), std::get<1>(range), std::get<2>(range));
366     } else {
367         _hidl_cb(0, 0, 0);
368     }
369 
370     return Void();
371 }
372 
setIntParameter(CameraParam id,int32_t value,setIntParameter_cb _hidl_cb)373 Return<void> EvsV4lCamera::setIntParameter(CameraParam id, int32_t value,
374                                            setIntParameter_cb _hidl_cb) {
375     uint32_t v4l2cid = V4L2_CID_BASE;
376     hidl_vec<int32_t> values;
377     values.resize(1);
378     if (!convertToV4l2CID(id, v4l2cid)) {
379         _hidl_cb(EvsResult::INVALID_ARG, values);
380     } else {
381         EvsResult result = EvsResult::OK;
382         v4l2_control control = {v4l2cid, value};
383         if (mVideo.setParameter(control) < 0 || mVideo.getParameter(control) < 0) {
384             result = EvsResult::UNDERLYING_SERVICE_ERROR;
385         }
386 
387         values[0] = control.value;
388         _hidl_cb(result, values);
389     }
390 
391     return Void();
392 }
393 
getIntParameter(CameraParam id,getIntParameter_cb _hidl_cb)394 Return<void> EvsV4lCamera::getIntParameter(CameraParam id, getIntParameter_cb _hidl_cb) {
395     uint32_t v4l2cid = V4L2_CID_BASE;
396     hidl_vec<int32_t> values;
397     values.resize(1);
398     if (!convertToV4l2CID(id, v4l2cid)) {
399         _hidl_cb(EvsResult::INVALID_ARG, values);
400     } else {
401         EvsResult result = EvsResult::OK;
402         v4l2_control control = {v4l2cid, 0};
403         if (mVideo.getParameter(control) < 0) {
404             result = EvsResult::INVALID_ARG;
405         }
406 
407         // Report a result
408         values[0] = control.value;
409         _hidl_cb(result, values);
410     }
411 
412     return Void();
413 }
414 
setExtendedInfo_1_1(uint32_t opaqueIdentifier,const hidl_vec<uint8_t> & opaqueValue)415 Return<EvsResult> EvsV4lCamera::setExtendedInfo_1_1(uint32_t opaqueIdentifier,
416                                                     const hidl_vec<uint8_t>& opaqueValue) {
417     mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue);
418     return EvsResult::OK;
419 }
420 
getExtendedInfo_1_1(uint32_t opaqueIdentifier,getExtendedInfo_1_1_cb _hidl_cb)421 Return<void> EvsV4lCamera::getExtendedInfo_1_1(uint32_t opaqueIdentifier,
422                                                getExtendedInfo_1_1_cb _hidl_cb) {
423     const auto it = mExtInfo.find(opaqueIdentifier);
424     hidl_vec<uint8_t> value;
425     auto status = EvsResult::OK;
426     if (it == mExtInfo.end()) {
427         status = EvsResult::INVALID_ARG;
428     } else {
429         value = mExtInfo[opaqueIdentifier];
430     }
431 
432     _hidl_cb(status, value);
433     return Void();
434 }
435 
importExternalBuffers(const hidl_vec<BufferDesc_1_1> & buffers,importExternalBuffers_cb _hidl_cb)436 Return<void> EvsV4lCamera::importExternalBuffers(const hidl_vec<BufferDesc_1_1>& buffers,
437                                                  importExternalBuffers_cb _hidl_cb) {
438     LOG(DEBUG) << __FUNCTION__;
439 
440     // If we've been displaced by another owner of the camera, then we can't do anything else
441     if (!mVideo.isOpen()) {
442         LOG(WARNING) << "Ignoring a request add external buffers " << "when camera has been lost.";
443         _hidl_cb(EvsResult::UNDERLYING_SERVICE_ERROR, mFramesAllowed);
444         return {};
445     }
446 
447     auto numBuffersToAdd = buffers.size();
448     if (numBuffersToAdd < 1) {
449         LOG(DEBUG) << "No buffers to add.";
450         _hidl_cb(EvsResult::OK, mFramesAllowed);
451         return {};
452     }
453 
454     {
455         std::scoped_lock<std::mutex> lock(mAccessLock);
456 
457         if (numBuffersToAdd > (MAX_BUFFERS_IN_FLIGHT - mFramesAllowed)) {
458             numBuffersToAdd -= (MAX_BUFFERS_IN_FLIGHT - mFramesAllowed);
459             LOG(WARNING) << "Exceed the limit on number of buffers.  " << numBuffersToAdd
460                          << " buffers will be added only.";
461         }
462 
463         GraphicBufferMapper& mapper = GraphicBufferMapper::get();
464         const auto before = mFramesAllowed;
465         for (auto i = 0; i < numBuffersToAdd; ++i) {
466             // TODO: reject if external buffer is configured differently.
467             auto& b = buffers[i];
468             const AHardwareBuffer_Desc* pDesc =
469                     reinterpret_cast<const AHardwareBuffer_Desc*>(&b.buffer.description);
470 
471             // Import a buffer to add
472             buffer_handle_t memHandle = nullptr;
473             status_t result =
474                     mapper.importBuffer(b.buffer.nativeHandle, pDesc->width, pDesc->height, 1,
475                                         pDesc->format, pDesc->usage, pDesc->stride, &memHandle);
476             if (result != android::NO_ERROR || !memHandle) {
477                 LOG(WARNING) << "Failed to import a buffer " << b.bufferId;
478                 continue;
479             }
480 
481             auto stored = false;
482             for (auto&& rec : mBuffers) {
483                 if (rec.handle == nullptr) {
484                     // Use this existing entry
485                     rec.handle = memHandle;
486                     rec.inUse = false;
487 
488                     stored = true;
489                     break;
490                 }
491             }
492 
493             if (!stored) {
494                 // Add a BufferRecord wrapping this handle to our set of available buffers
495                 mBuffers.emplace_back(memHandle);
496             }
497 
498             ++mFramesAllowed;
499         }
500 
501         _hidl_cb(EvsResult::OK, mFramesAllowed - before);
502         return {};
503     }
504 }
505 
doneWithFrame_impl(const uint32_t bufferId,const buffer_handle_t memHandle)506 EvsResult EvsV4lCamera::doneWithFrame_impl(const uint32_t bufferId,
507                                            const buffer_handle_t memHandle) {
508     std::lock_guard<std::mutex> lock(mAccessLock);
509 
510     // If we've been displaced by another owner of the camera, then we can't do anything else
511     if (!mVideo.isOpen()) {
512         LOG(WARNING) << "Ignoring doneWithFrame call when camera has been lost.";
513     } else {
514         if (memHandle == nullptr) {
515             LOG(ERROR) << "Ignoring doneWithFrame called with null handle";
516         } else if (bufferId >= mBuffers.size()) {
517             LOG(ERROR) << "Ignoring doneWithFrame called with invalid bufferId " << bufferId
518                        << " (max is " << mBuffers.size() - 1 << ")";
519         } else if (!mBuffers[bufferId].inUse) {
520             LOG(ERROR) << "Ignoring doneWithFrame called on frame " << bufferId
521                        << " which is already free";
522         } else {
523             // Mark the frame as available
524             mBuffers[bufferId].inUse = false;
525             mFramesInUse--;
526 
527             // If this frame's index is high in the array, try to move it down
528             // to improve locality after mFramesAllowed has been reduced.
529             if (bufferId >= mFramesAllowed) {
530                 // Find an empty slot lower in the array (which should always exist in this case)
531                 for (auto&& rec : mBuffers) {
532                     if (rec.handle == nullptr) {
533                         rec.handle = mBuffers[bufferId].handle;
534                         mBuffers[bufferId].handle = nullptr;
535                         break;
536                     }
537                 }
538             }
539         }
540     }
541 
542     return EvsResult::OK;
543 }
544 
setAvailableFrames_Locked(unsigned bufferCount)545 bool EvsV4lCamera::setAvailableFrames_Locked(unsigned bufferCount) {
546     if (bufferCount < 1) {
547         LOG(ERROR) << "Ignoring request to set buffer count to zero";
548         return false;
549     }
550     if (bufferCount > MAX_BUFFERS_IN_FLIGHT) {
551         LOG(ERROR) << "Rejecting buffer request in excess of internal limit";
552         return false;
553     }
554 
555     // Is an increase required?
556     if (mFramesAllowed < bufferCount) {
557         // An increase is required
558         unsigned needed = bufferCount - mFramesAllowed;
559         LOG(INFO) << "Allocating " << needed << " buffers for camera frames";
560 
561         unsigned added = increaseAvailableFrames_Locked(needed);
562         if (added != needed) {
563             // If we didn't add all the frames we needed, then roll back to the previous state
564             LOG(ERROR) << "Rolling back to previous frame queue size";
565             decreaseAvailableFrames_Locked(added);
566             return false;
567         }
568     } else if (mFramesAllowed > bufferCount) {
569         // A decrease is required
570         unsigned framesToRelease = mFramesAllowed - bufferCount;
571         LOG(INFO) << "Returning " << framesToRelease << " camera frame buffers";
572 
573         unsigned released = decreaseAvailableFrames_Locked(framesToRelease);
574         if (released != framesToRelease) {
575             // This shouldn't happen with a properly behaving client because the client
576             // should only make this call after returning sufficient outstanding buffers
577             // to allow a clean resize.
578             LOG(ERROR) << "Buffer queue shrink failed -- too many buffers currently in use?";
579         }
580     }
581 
582     return true;
583 }
584 
increaseAvailableFrames_Locked(unsigned numToAdd)585 unsigned EvsV4lCamera::increaseAvailableFrames_Locked(unsigned numToAdd) {
586     // Acquire the graphics buffer allocator
587     GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
588 
589     unsigned added = 0;
590 
591     while (added < numToAdd) {
592         unsigned pixelsPerLine;
593         buffer_handle_t memHandle = nullptr;
594         status_t result = alloc.allocate(mVideo.getWidth(), mVideo.getHeight(), mFormat, 1, mUsage,
595                                          &memHandle, &pixelsPerLine, 0, "EvsV4lCamera");
596         if (result != NO_ERROR) {
597             LOG(ERROR) << "Error " << result << " allocating " << mVideo.getWidth() << " x "
598                        << mVideo.getHeight() << " graphics buffer";
599             break;
600         }
601         if (!memHandle) {
602             LOG(ERROR) << "We didn't get a buffer handle back from the allocator";
603             break;
604         }
605         if (mStride) {
606             if (mStride != pixelsPerLine) {
607                 LOG(ERROR) << "We did not expect to get buffers with different strides!";
608             }
609         } else {
610             // Gralloc defines stride in terms of pixels per line
611             mStride = pixelsPerLine;
612         }
613 
614         // Find a place to store the new buffer
615         bool stored = false;
616         for (auto&& rec : mBuffers) {
617             if (rec.handle == nullptr) {
618                 // Use this existing entry
619                 rec.handle = memHandle;
620                 rec.inUse = false;
621                 stored = true;
622                 break;
623             }
624         }
625         if (!stored) {
626             // Add a BufferRecord wrapping this handle to our set of available buffers
627             mBuffers.emplace_back(memHandle);
628         }
629 
630         mFramesAllowed++;
631         added++;
632     }
633 
634     return added;
635 }
636 
decreaseAvailableFrames_Locked(unsigned numToRemove)637 unsigned EvsV4lCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) {
638     // Acquire the graphics buffer allocator
639     GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
640 
641     unsigned removed = 0;
642 
643     for (auto&& rec : mBuffers) {
644         // Is this record not in use, but holding a buffer that we can free?
645         if ((rec.inUse == false) && (rec.handle != nullptr)) {
646             // Release buffer and update the record so we can recognize it as "empty"
647             alloc.free(rec.handle);
648             rec.handle = nullptr;
649 
650             mFramesAllowed--;
651             removed++;
652 
653             if (removed == numToRemove) {
654                 break;
655             }
656         }
657     }
658 
659     return removed;
660 }
661 
662 // This is the async callback from the video camera that tells us a frame is ready
forwardFrame(imageBuffer * pV4lBuff,void * pData)663 void EvsV4lCamera::forwardFrame(imageBuffer* pV4lBuff, void* pData) {
664     bool readyForFrame = false;
665     size_t idx = 0;
666 
667     // Lock scope for updating shared state
668     {
669         std::lock_guard<std::mutex> lock(mAccessLock);
670 
671         // Are we allowed to issue another buffer?
672         if (mFramesInUse >= mFramesAllowed) {
673             // Can't do anything right now -- skip this frame
674             LOG(WARNING) << "Skipped a frame because too many are in flight";
675         } else {
676             // Identify an available buffer to fill
677             for (idx = 0; idx < mBuffers.size(); idx++) {
678                 if (!mBuffers[idx].inUse) {
679                     if (mBuffers[idx].handle != nullptr) {
680                         // Found an available record, so stop looking
681                         break;
682                     }
683                 }
684             }
685             if (idx >= mBuffers.size()) {
686                 // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed
687                 LOG(ERROR) << "Failed to find an available buffer slot";
688             } else {
689                 // We're going to make the frame busy
690                 mBuffers[idx].inUse = true;
691                 mFramesInUse++;
692                 readyForFrame = true;
693             }
694         }
695     }
696 
697     if (mDumpFrame) {
698         // Construct a target filename with the device identifier
699         std::string filename = std::string(mDescription.v1.cameraId);
700         std::replace(filename.begin(), filename.end(), '/', '_');
701         filename = mDumpPath + filename + "_" + std::to_string(mFrameCounter) + ".bin";
702 
703         android::base::unique_fd fd(
704                 open(filename.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP));
705         LOG(INFO) << filename << ", " << fd;
706         if (fd == -1) {
707             PLOG(ERROR) << "Failed to open a file, " << filename;
708         } else {
709             auto width = mVideo.getWidth();
710             auto height = mVideo.getHeight();
711             auto len = write(fd.get(), &width, sizeof(width));
712             len += write(fd.get(), &height, sizeof(height));
713             len += write(fd.get(), &mStride, sizeof(mStride));
714             len += write(fd.get(), &mFormat, sizeof(mFormat));
715             len += write(fd.get(), pData, pV4lBuff->length);
716             LOG(INFO) << len << " bytes are written to " << filename;
717         }
718     }
719 
720     if (!readyForFrame) {
721         // We need to return the video buffer so it can capture a new frame
722         mVideo.markFrameConsumed(pV4lBuff->index);
723     } else {
724         // Assemble the buffer description we'll transmit below
725         BufferDesc_1_1 bufDesc_1_1 = {};
726         AHardwareBuffer_Desc* pDesc =
727                 reinterpret_cast<AHardwareBuffer_Desc*>(&bufDesc_1_1.buffer.description);
728         pDesc->width = mVideo.getWidth();
729         pDesc->height = mVideo.getHeight();
730         pDesc->layers = 1;
731         pDesc->format = mFormat;
732         pDesc->usage = mUsage;
733         pDesc->stride = mStride;
734         bufDesc_1_1.buffer.nativeHandle = mBuffers[idx].handle;
735         bufDesc_1_1.bufferId = idx;
736         bufDesc_1_1.deviceId = mDescription.v1.cameraId;
737         // timestamp in microseconds.
738         bufDesc_1_1.timestamp = pV4lBuff->timestamp.tv_sec * 1e+6 + pV4lBuff->timestamp.tv_usec;
739 
740         const auto sizeInRGBA = pDesc->width * pDesc->height * kBytesPerPixelRGBA;
741         if (mColorSpaceConversionBuffer.size() < sizeInRGBA) {
742             mColorSpaceConversionBuffer.resize(sizeInRGBA);
743         }
744 
745         // Lock our output buffer for writing
746         // TODO(b/145459970): Sometimes, physical camera device maps a buffer
747         // into the address that is about to be unmapped by another device; this
748         // causes SEGV_MAPPER.
749         void* targetPixels = nullptr;
750         GraphicBufferMapper& mapper = GraphicBufferMapper::get();
751         status_t result =
752                 mapper.lock(bufDesc_1_1.buffer.nativeHandle,
753                             GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER,
754                             android::Rect(pDesc->width, pDesc->height), (void**)&targetPixels);
755 
756         // If we failed to lock the pixel buffer, we're about to crash, but log it first
757         if (!targetPixels) {
758             // TODO(b/145457727): When EvsHidlTest::CameraToDisplayRoundTrip
759             // test case was repeatedly executed, EVS occasionally fails to map
760             // a buffer.
761             LOG(ERROR) << "Camera failed to gain access to image buffer for writing - "
762                        << " status: " << statusToString(result) << " , error: " << strerror(errno);
763         }
764 
765         // Transfer the video image into the output buffer, making any needed
766         // format conversion along the way
767         mFillBufferFromVideo(bufDesc_1_1, (uint8_t*)targetPixels, pData,
768                              mColorSpaceConversionBuffer.data(), mStride);
769 
770         // Unlock the output buffer
771         mapper.unlock(bufDesc_1_1.buffer.nativeHandle);
772 
773         // Give the video frame back to the underlying device for reuse
774         // Note that we do this before making the client callback to give the
775         // underlying camera more time to capture the next frame
776         mVideo.markFrameConsumed(pV4lBuff->index);
777 
778         // Issue the (asynchronous) callback to the client -- can't be holding
779         // the lock
780         bool flag = false;
781         if (mStream_1_1 != nullptr) {
782             hidl_vec<BufferDesc_1_1> frames;
783             frames.resize(1);
784             frames[0] = bufDesc_1_1;
785             auto result = mStream_1_1->deliverFrame_1_1(frames);
786             flag = result.isOk();
787         } else {
788             BufferDesc_1_0 bufDesc_1_0 = {pDesc->width,
789                                           pDesc->height,
790                                           pDesc->stride,
791                                           bufDesc_1_1.pixelSize,
792                                           static_cast<uint32_t>(pDesc->format),
793                                           static_cast<uint32_t>(pDesc->usage),
794                                           bufDesc_1_1.bufferId,
795                                           bufDesc_1_1.buffer.nativeHandle};
796 
797             auto result = mStream->deliverFrame(bufDesc_1_0);
798             flag = result.isOk();
799         }
800 
801         if (flag) {
802             LOG(DEBUG) << "Delivered " << bufDesc_1_1.buffer.nativeHandle.getNativeHandle()
803                        << " as id " << bufDesc_1_1.bufferId;
804         } else {
805             // This can happen if the client dies and is likely unrecoverable.
806             // To avoid consuming resources generating failing calls, we stop sending
807             // frames.  Note, however, that the stream remains in the "STREAMING" state
808             // until cleaned up on the main thread.
809             LOG(ERROR) << "Frame delivery call failed in the transport layer.";
810 
811             // Since we didn't actually deliver it, mark the frame as available
812             std::lock_guard<std::mutex> lock(mAccessLock);
813             mBuffers[idx].inUse = false;
814 
815             mFramesInUse--;
816         }
817     }
818 
819     // Increse a frame counter
820     ++mFrameCounter;
821 }
822 
convertToV4l2CID(CameraParam id,uint32_t & v4l2cid)823 bool EvsV4lCamera::convertToV4l2CID(CameraParam id, uint32_t& v4l2cid) {
824     switch (id) {
825         case CameraParam::BRIGHTNESS:
826             v4l2cid = V4L2_CID_BRIGHTNESS;
827             break;
828         case CameraParam::CONTRAST:
829             v4l2cid = V4L2_CID_CONTRAST;
830             break;
831         case CameraParam::AUTO_WHITE_BALANCE:
832             v4l2cid = V4L2_CID_AUTO_WHITE_BALANCE;
833             break;
834         case CameraParam::WHITE_BALANCE_TEMPERATURE:
835             v4l2cid = V4L2_CID_WHITE_BALANCE_TEMPERATURE;
836             break;
837         case CameraParam::SHARPNESS:
838             v4l2cid = V4L2_CID_SHARPNESS;
839             break;
840         case CameraParam::AUTO_EXPOSURE:
841             v4l2cid = V4L2_CID_EXPOSURE_AUTO;
842             break;
843         case CameraParam::ABSOLUTE_EXPOSURE:
844             v4l2cid = V4L2_CID_EXPOSURE_ABSOLUTE;
845             break;
846         case CameraParam::AUTO_FOCUS:
847             v4l2cid = V4L2_CID_FOCUS_AUTO;
848             break;
849         case CameraParam::ABSOLUTE_FOCUS:
850             v4l2cid = V4L2_CID_FOCUS_ABSOLUTE;
851             break;
852         case CameraParam::ABSOLUTE_ZOOM:
853             v4l2cid = V4L2_CID_ZOOM_ABSOLUTE;
854             break;
855         default:
856             LOG(ERROR) << "Camera parameter " << static_cast<unsigned>(id) << " is unknown.";
857             return false;
858     }
859 
860     return mCameraControls.find(v4l2cid) != mCameraControls.end();
861 }
862 
Create(const char * deviceName)863 sp<EvsV4lCamera> EvsV4lCamera::Create(const char* deviceName) {
864     std::unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr;
865 
866     return Create(deviceName, nullCamInfo);
867 }
868 
Create(const char * deviceName,std::unique_ptr<ConfigManager::CameraInfo> & camInfo,const Stream * requestedStreamCfg)869 sp<EvsV4lCamera> EvsV4lCamera::Create(const char* deviceName,
870                                       std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
871                                       const Stream* requestedStreamCfg) {
872     LOG(INFO) << "Create " << deviceName;
873     sp<EvsV4lCamera> evsCamera = new EvsV4lCamera(deviceName, camInfo);
874     if (evsCamera == nullptr) {
875         return nullptr;
876     }
877 
878     // Initialize the video device
879     bool success = false;
880     if (camInfo != nullptr && requestedStreamCfg != nullptr) {
881         // Validate a given stream configuration.  If there is no exact match,
882         // this will try to find the best match based on:
883         // 1) same output format
884         // 2) the largest resolution that is smaller that a given configuration.
885         int32_t streamId = -1, area = INT_MIN;
886         for (auto& [id, cfg] : camInfo->streamConfigurations) {
887             // RawConfiguration has id, width, height, format, direction, and
888             // fps.
889             if (cfg[3] == static_cast<uint32_t>(requestedStreamCfg->format)) {
890                 if (cfg[1] == requestedStreamCfg->width && cfg[2] == requestedStreamCfg->height) {
891                     // Find exact match.
892                     streamId = id;
893                     break;
894                 } else if (requestedStreamCfg->width > cfg[1] &&
895                            requestedStreamCfg->height > cfg[2] && cfg[1] * cfg[2] > area) {
896                     streamId = id;
897                     area = cfg[1] * cfg[2];
898                 }
899             }
900         }
901 
902         if (streamId >= 0) {
903             LOG(INFO) << "Try to open a video with "
904                       << "width: " << camInfo->streamConfigurations[streamId][1]
905                       << ", height: " << camInfo->streamConfigurations[streamId][2]
906                       << ", format: " << camInfo->streamConfigurations[streamId][3];
907             success = evsCamera->mVideo.open(deviceName, camInfo->streamConfigurations[streamId][1],
908                                              camInfo->streamConfigurations[streamId][2]);
909             evsCamera->mFormat = static_cast<uint32_t>(camInfo->streamConfigurations[streamId][3]);
910         }
911     }
912 
913     if (!success) {
914         // Create a camera object with the default resolution and format
915         // , HAL_PIXEL_FORMAT_RGBA_8888.
916         LOG(INFO) << "Open a video with default parameters";
917         success = evsCamera->mVideo.open(deviceName, kDefaultResolution[0], kDefaultResolution[1]);
918         if (!success) {
919             LOG(ERROR) << "Failed to open a video stream";
920             return nullptr;
921         }
922     }
923 
924     // List available camera parameters
925     evsCamera->mCameraControls = evsCamera->mVideo.enumerateCameraControls();
926 
927     // Please note that the buffer usage flag does not come from a given stream
928     // configuration.
929     evsCamera->mUsage =
930             GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
931 
932     return evsCamera;
933 }
934 
935 using android::base::Error;
936 using android::base::Result;
startDumpFrames(const std::string & path)937 Result<void> EvsV4lCamera::startDumpFrames(const std::string& path) {
938     struct stat info;
939     if (stat(path.c_str(), &info) != 0) {
940         return Error(BAD_VALUE) << "Cannot access " << path;
941     } else if (!(info.st_mode & S_IFDIR)) {
942         return Error(BAD_VALUE) << path << " is not a directory";
943     }
944 
945     mDumpPath = path;
946     mDumpFrame = true;
947 
948     return {};
949 }
950 
stopDumpFrames()951 Result<void> EvsV4lCamera::stopDumpFrames() {
952     if (!mDumpFrame) {
953         return Error(INVALID_OPERATION) << "Device is not dumping frames";
954     }
955 
956     mDumpFrame = false;
957     return {};
958 }
959 
960 }  // namespace implementation
961 }  // namespace V1_1
962 }  // namespace evs
963 }  // namespace automotive
964 }  // namespace hardware
965 }  // namespace android
966