blob: e200b535391cd1889b570aa5cdca88cc6ca5a865 [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 "android.hardware.automotive.evs@1.1-service"
18
19#include "EvsCamera.h"
20#include "EvsEnumerator.h"
21
22#include <ui/GraphicBufferAllocator.h>
23#include <ui/GraphicBufferMapper.h>
Changyeon Jo56c9b372019-10-09 14:04:31 -070024#include <utils/SystemClock.h>
Changyeon Jo2400b692019-07-18 21:32:48 -070025
26namespace android {
27namespace hardware {
28namespace automotive {
29namespace evs {
30namespace V1_1 {
31namespace implementation {
32
33
34// Special camera names for which we'll initialize alternate test data
35const char EvsCamera::kCameraName_Backup[] = "backup";
36
37
38// Arbitrary limit on number of graphics buffers allowed to be allocated
39// Safeguards against unreasonable resource consumption and provides a testable limit
40const unsigned MAX_BUFFERS_IN_FLIGHT = 100;
41
42
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070043EvsCamera::EvsCamera(const char *id,
44 unique_ptr<ConfigManager::CameraInfo> &camInfo) :
Changyeon Jo2400b692019-07-18 21:32:48 -070045 mFramesAllowed(0),
46 mFramesInUse(0),
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070047 mStreamState(STOPPED),
48 mCameraInfo(camInfo) {
Changyeon Jo2400b692019-07-18 21:32:48 -070049
50 ALOGD("EvsCamera instantiated");
51
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070052 /* set a camera id */
53 mDescription.v1.cameraId = id;
Changyeon Jo2400b692019-07-18 21:32:48 -070054
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070055 /* set camera metadata */
56 mDescription.metadata.setToExternal((uint8_t *)camInfo->characteristics,
57 get_camera_metadata_size(camInfo->characteristics));
Changyeon Jo2400b692019-07-18 21:32:48 -070058}
59
60
61EvsCamera::~EvsCamera() {
62 ALOGD("EvsCamera being destroyed");
63 forceShutdown();
64}
65
66
67//
68// This gets called if another caller "steals" ownership of the camera
69//
70void EvsCamera::forceShutdown()
71{
72 ALOGD("EvsCamera forceShutdown");
73
74 // Make sure our output stream is cleaned up
75 // (It really should be already)
76 stopVideoStream();
77
78 // Claim the lock while we work on internal state
79 std::lock_guard <std::mutex> lock(mAccessLock);
80
81 // Drop all the graphics buffers we've been using
82 if (mBuffers.size() > 0) {
83 GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
84 for (auto&& rec : mBuffers) {
85 if (rec.inUse) {
86 ALOGE("Error - releasing buffer despite remote ownership");
87 }
88 alloc.free(rec.handle);
89 rec.handle = nullptr;
90 }
91 mBuffers.clear();
92 }
93
94 // Put this object into an unrecoverable error state since somebody else
95 // is going to own the underlying camera now
96 mStreamState = DEAD;
97}
98
99
100// Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
101Return<void> EvsCamera::getCameraInfo(getCameraInfo_cb _hidl_cb) {
102 ALOGD("getCameraInfo");
103
104 // Send back our self description
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700105 _hidl_cb(mDescription.v1);
Changyeon Jo2400b692019-07-18 21:32:48 -0700106 return Void();
107}
108
109
110Return<EvsResult> EvsCamera::setMaxFramesInFlight(uint32_t bufferCount) {
111 ALOGD("setMaxFramesInFlight");
112 std::lock_guard<std::mutex> lock(mAccessLock);
113
114 // If we've been displaced by another owner of the camera, then we can't do anything else
115 if (mStreamState == DEAD) {
116 ALOGE("ignoring setMaxFramesInFlight call when camera has been lost.");
117 return EvsResult::OWNERSHIP_LOST;
118 }
119
120 // We cannot function without at least one video buffer to send data
121 if (bufferCount < 1) {
122 ALOGE("Ignoring setMaxFramesInFlight with less than one buffer requested");
123 return EvsResult::INVALID_ARG;
124 }
125
126 // Update our internal state
127 if (setAvailableFrames_Locked(bufferCount)) {
128 return EvsResult::OK;
129 } else {
130 return EvsResult::BUFFER_NOT_AVAILABLE;
131 }
132}
133
134
135Return<EvsResult> EvsCamera::startVideoStream(const ::android::sp<IEvsCameraStream_1_0>& stream) {
136 ALOGD("startVideoStream");
137 std::lock_guard<std::mutex> lock(mAccessLock);
138
139 // If we've been displaced by another owner of the camera, then we can't do anything else
140 if (mStreamState == DEAD) {
141 ALOGE("ignoring startVideoStream call when camera has been lost.");
142 return EvsResult::OWNERSHIP_LOST;
143 }
144 if (mStreamState != STOPPED) {
145 ALOGE("ignoring startVideoStream call when a stream is already running.");
146 return EvsResult::STREAM_ALREADY_RUNNING;
147 }
148
149 // If the client never indicated otherwise, configure ourselves for a single streaming buffer
150 if (mFramesAllowed < 1) {
151 if (!setAvailableFrames_Locked(1)) {
152 ALOGE("Failed to start stream because we couldn't get a graphics buffer");
153 return EvsResult::BUFFER_NOT_AVAILABLE;
154 }
155 }
156
157 // Record the user's callback for use when we have a frame ready
158 mStream = IEvsCameraStream_1_1::castFrom(stream).withDefault(nullptr);
159 if (mStream == nullptr) {
160 ALOGE("Default implementation does not support v1.0 IEvsCameraStream");
161 return EvsResult::INVALID_ARG;
162 }
163
164 // Start the frame generation thread
165 mStreamState = RUNNING;
166 mCaptureThread = std::thread([this](){ generateFrames(); });
167
168 return EvsResult::OK;
169}
170
171
172Return<void> EvsCamera::doneWithFrame(const BufferDesc_1_0& buffer) {
173 std::lock_guard <std::mutex> lock(mAccessLock);
174 returnBuffer(buffer.bufferId, buffer.memHandle);
175
176 return Void();
177}
178
179
180Return<void> EvsCamera::stopVideoStream() {
181 ALOGD("stopVideoStream");
182 std::unique_lock <std::mutex> lock(mAccessLock);
183
184 if (mStreamState == RUNNING) {
185 // Tell the GenerateFrames loop we want it to stop
186 mStreamState = STOPPING;
187
188 // Block outside the mutex until the "stop" flag has been acknowledged
189 // We won't send any more frames, but the client might still get some already in flight
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700190 ALOGD("Waiting for stream thread to end...");
Changyeon Jo2400b692019-07-18 21:32:48 -0700191 lock.unlock();
192 mCaptureThread.join();
193 lock.lock();
194
195 mStreamState = STOPPED;
196 mStream = nullptr;
197 ALOGD("Stream marked STOPPED.");
198 }
199
200 return Void();
201}
202
203
204Return<int32_t> EvsCamera::getExtendedInfo(uint32_t opaqueIdentifier) {
205 ALOGD("getExtendedInfo");
206 std::lock_guard<std::mutex> lock(mAccessLock);
207
208 // For any single digit value, return the index itself as a test value
209 if (opaqueIdentifier <= 9) {
210 return opaqueIdentifier;
211 }
212
213 // Return zero by default as required by the spec
214 return 0;
215}
216
217
218Return<EvsResult> EvsCamera::setExtendedInfo(uint32_t /*opaqueIdentifier*/, int32_t /*opaqueValue*/) {
219 ALOGD("setExtendedInfo");
220 std::lock_guard<std::mutex> lock(mAccessLock);
221
222 // If we've been displaced by another owner of the camera, then we can't do anything else
223 if (mStreamState == DEAD) {
224 ALOGE("ignoring setExtendedInfo call when camera has been lost.");
225 return EvsResult::OWNERSHIP_LOST;
226 }
227
228 // We don't store any device specific information in this implementation
229 return EvsResult::INVALID_ARG;
230}
231
232
233// Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow.
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700234Return<void> EvsCamera::getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) {
235 ALOGD("getCameraInfo_1_1");
236
237 // Send back our self description
238 _hidl_cb(mDescription);
239 return Void();
240}
241
242
Changyeon Jo56c9b372019-10-09 14:04:31 -0700243Return<EvsResult> EvsCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffers) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700244 std::lock_guard <std::mutex> lock(mAccessLock);
Changyeon Jo56c9b372019-10-09 14:04:31 -0700245
246 for (auto&& buffer : buffers) {
247 returnBuffer(buffer.bufferId, buffer.buffer.nativeHandle);
248 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700249
250 return EvsResult::OK;
251}
252
253
254Return<EvsResult> EvsCamera::pauseVideoStream() {
255 // Default implementation does not support this.
256 return EvsResult::UNDERLYING_SERVICE_ERROR;
257}
258
259
260Return<EvsResult> EvsCamera::resumeVideoStream() {
261 // Default implementation does not support this.
262 return EvsResult::UNDERLYING_SERVICE_ERROR;
263}
264
265
Changyeon Jod2a82462019-07-30 11:57:17 -0700266Return<EvsResult> EvsCamera::setMaster() {
267 // Default implementation does not expect multiple subscribers and therefore
268 // return a success code always.
269 return EvsResult::OK;
270}
271
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700272Return<EvsResult> EvsCamera::forceMaster(const sp<IEvsDisplay>& ) {
273 // Default implementation does not expect multiple subscribers and therefore
274 // return a success code always.
275 return EvsResult::OK;
276}
277
Changyeon Jod2a82462019-07-30 11:57:17 -0700278
279Return<EvsResult> EvsCamera::unsetMaster() {
280 // Default implementation does not expect multiple subscribers and therefore
281 // return a success code always.
282 return EvsResult::OK;
283}
284
285
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700286Return<void> EvsCamera::getParameterList(getParameterList_cb _hidl_cb) {
287 hidl_vec<CameraParam> hidlCtrls;
288 hidlCtrls.resize(mCameraInfo->controls.size());
289 unsigned idx = 0;
290 for (auto& [cid, cfg] : mCameraInfo->controls) {
291 hidlCtrls[idx++] = cid;
292 }
293
294 _hidl_cb(hidlCtrls);
295 return Void();
296}
297
298
299Return<void> EvsCamera::getIntParameterRange(CameraParam id,
300 getIntParameterRange_cb _hidl_cb) {
301 auto range = mCameraInfo->controls[id];
302 _hidl_cb(get<0>(range), get<1>(range), get<2>(range));
303 return Void();
304}
305
306
307Return<void> EvsCamera::setIntParameter(CameraParam id, int32_t value,
308 setIntParameter_cb _hidl_cb) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700309 // Default implementation does not support this.
310 (void)id;
311 (void)value;
312 _hidl_cb(EvsResult::INVALID_ARG, 0);
313 return Void();
314}
315
316
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700317Return<void> EvsCamera::getIntParameter(CameraParam id,
318 getIntParameter_cb _hidl_cb) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700319 // Default implementation does not support this.
320 (void)id;
321 _hidl_cb(EvsResult::INVALID_ARG, 0);
322 return Void();
323}
324
325
Changyeon Jo2400b692019-07-18 21:32:48 -0700326bool EvsCamera::setAvailableFrames_Locked(unsigned bufferCount) {
327 if (bufferCount < 1) {
328 ALOGE("Ignoring request to set buffer count to zero");
329 return false;
330 }
331 if (bufferCount > MAX_BUFFERS_IN_FLIGHT) {
332 ALOGE("Rejecting buffer request in excess of internal limit");
333 return false;
334 }
335
336 // Is an increase required?
337 if (mFramesAllowed < bufferCount) {
338 // An increase is required
339 unsigned needed = bufferCount - mFramesAllowed;
340 ALOGI("Allocating %d buffers for camera frames", needed);
341
342 unsigned added = increaseAvailableFrames_Locked(needed);
343 if (added != needed) {
344 // If we didn't add all the frames we needed, then roll back to the previous state
345 ALOGE("Rolling back to previous frame queue size");
346 decreaseAvailableFrames_Locked(added);
347 return false;
348 }
349 } else if (mFramesAllowed > bufferCount) {
350 // A decrease is required
351 unsigned framesToRelease = mFramesAllowed - bufferCount;
352 ALOGI("Returning %d camera frame buffers", framesToRelease);
353
354 unsigned released = decreaseAvailableFrames_Locked(framesToRelease);
355 if (released != framesToRelease) {
356 // This shouldn't happen with a properly behaving client because the client
357 // should only make this call after returning sufficient outstanding buffers
358 // to allow a clean resize.
359 ALOGE("Buffer queue shrink failed -- too many buffers currently in use?");
360 }
361 }
362
363 return true;
364}
365
366
367unsigned EvsCamera::increaseAvailableFrames_Locked(unsigned numToAdd) {
368 // Acquire the graphics buffer allocator
369 GraphicBufferAllocator &alloc(GraphicBufferAllocator::get());
370
371 unsigned added = 0;
372
373 while (added < numToAdd) {
374 buffer_handle_t memHandle = nullptr;
375 status_t result = alloc.allocate(mWidth, mHeight, mFormat, 1, mUsage,
376 &memHandle, &mStride, 0, "EvsCamera");
377 if (result != NO_ERROR) {
378 ALOGE("Error %d allocating %d x %d graphics buffer", result, mWidth, mHeight);
379 break;
380 }
381 if (!memHandle) {
382 ALOGE("We didn't get a buffer handle back from the allocator");
383 break;
384 }
385
386 // Find a place to store the new buffer
387 bool stored = false;
388 for (auto&& rec : mBuffers) {
389 if (rec.handle == nullptr) {
390 // Use this existing entry
391 rec.handle = memHandle;
392 rec.inUse = false;
393 stored = true;
394 break;
395 }
396 }
397 if (!stored) {
398 // Add a BufferRecord wrapping this handle to our set of available buffers
399 mBuffers.emplace_back(memHandle);
400 }
401
402 mFramesAllowed++;
403 added++;
404 }
405
406 return added;
407}
408
409
410unsigned EvsCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) {
411 // Acquire the graphics buffer allocator
412 GraphicBufferAllocator &alloc(GraphicBufferAllocator::get());
413
414 unsigned removed = 0;
415
416 for (auto&& rec : mBuffers) {
417 // Is this record not in use, but holding a buffer that we can free?
418 if ((rec.inUse == false) && (rec.handle != nullptr)) {
419 // Release buffer and update the record so we can recognize it as "empty"
420 alloc.free(rec.handle);
421 rec.handle = nullptr;
422
423 mFramesAllowed--;
424 removed++;
425
426 if (removed == numToRemove) {
427 break;
428 }
429 }
430 }
431
432 return removed;
433}
434
435
436// This is the asynchronous frame generation thread that runs in parallel with the
437// main serving thread. There is one for each active camera instance.
438void EvsCamera::generateFrames() {
439 ALOGD("Frame generation loop started");
440
441 unsigned idx;
442
443 while (true) {
444 bool timeForFrame = false;
445 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
446
447 // Lock scope for updating shared state
448 {
449 std::lock_guard<std::mutex> lock(mAccessLock);
450
451 if (mStreamState != RUNNING) {
452 // Break out of our main thread loop
453 break;
454 }
455
456 // Are we allowed to issue another buffer?
457 if (mFramesInUse >= mFramesAllowed) {
458 // Can't do anything right now -- skip this frame
459 ALOGW("Skipped a frame because too many are in flight\n");
460 } else {
461 // Identify an available buffer to fill
462 for (idx = 0; idx < mBuffers.size(); idx++) {
463 if (!mBuffers[idx].inUse) {
464 if (mBuffers[idx].handle != nullptr) {
465 // Found an available record, so stop looking
466 break;
467 }
468 }
469 }
470 if (idx >= mBuffers.size()) {
471 // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed
472 ALOGE("Failed to find an available buffer slot\n");
473 } else {
474 // We're going to make the frame busy
475 mBuffers[idx].inUse = true;
476 mFramesInUse++;
477 timeForFrame = true;
478 }
479 }
480 }
481
482 if (timeForFrame) {
483 // Assemble the buffer description we'll transmit below
484 BufferDesc_1_1 newBuffer = {};
485 AHardwareBuffer_Desc* pDesc =
486 reinterpret_cast<AHardwareBuffer_Desc *>(&newBuffer.buffer.description);
487 pDesc->width = mWidth;
488 pDesc->height = mHeight;
489 pDesc->layers = 1;
490 pDesc->format = mFormat;
491 pDesc->usage = mUsage;
492 pDesc->stride = mStride;
493 newBuffer.buffer.nativeHandle = mBuffers[idx].handle;
494 newBuffer.pixelSize = sizeof(uint32_t);
495 newBuffer.bufferId = idx;
Changyeon Jo56c9b372019-10-09 14:04:31 -0700496 newBuffer.deviceId = mDescription.v1.cameraId;
497 newBuffer.timestamp = elapsedRealtimeNano();
Changyeon Jo2400b692019-07-18 21:32:48 -0700498
499 // Write test data into the image buffer
500 fillTestFrame(newBuffer);
501
502 // Issue the (asynchronous) callback to the client -- can't be holding the lock
Changyeon Jo56c9b372019-10-09 14:04:31 -0700503 hidl_vec<BufferDesc_1_1> frames;
504 frames.resize(1);
505 frames[0] = newBuffer;
506 auto result = mStream->deliverFrame_1_1(frames);
Changyeon Jo2400b692019-07-18 21:32:48 -0700507 if (result.isOk()) {
508 ALOGD("Delivered %p as id %d",
509 newBuffer.buffer.nativeHandle.getNativeHandle(), newBuffer.bufferId);
510 } else {
511 // This can happen if the client dies and is likely unrecoverable.
512 // To avoid consuming resources generating failing calls, we stop sending
513 // frames. Note, however, that the stream remains in the "STREAMING" state
514 // until cleaned up on the main thread.
515 ALOGE("Frame delivery call failed in the transport layer.");
516
517 // Since we didn't actually deliver it, mark the frame as available
518 std::lock_guard<std::mutex> lock(mAccessLock);
519 mBuffers[idx].inUse = false;
520 mFramesInUse--;
521
522 break;
523 }
524 }
525
526 // We arbitrarily choose to generate frames at 12 fps to ensure we pass the 10fps test requirement
527 static const int kTargetFrameRate = 12;
528 static const nsecs_t kTargetFrameTimeUs = 1000*1000 / kTargetFrameRate;
529 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
530 const nsecs_t workTimeUs = (now - startTime) / 1000;
531 const nsecs_t sleepDurationUs = kTargetFrameTimeUs - workTimeUs;
532 if (sleepDurationUs > 0) {
533 usleep(sleepDurationUs);
534 }
535 }
536
537 // If we've been asked to stop, send an event to signal the actual end of stream
Changyeon Jo56c9b372019-10-09 14:04:31 -0700538 EvsEventDesc event;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700539 event.aType = EvsEventType::STREAM_STOPPED;
540 auto result = mStream->notify(event);
Changyeon Jo2400b692019-07-18 21:32:48 -0700541 if (!result.isOk()) {
542 ALOGE("Error delivering end of stream marker");
543 }
544
545 return;
546}
547
548
549void EvsCamera::fillTestFrame(const BufferDesc_1_1& buff) {
550 // Lock our output buffer for writing
551 uint32_t *pixels = nullptr;
552 const AHardwareBuffer_Desc* pDesc =
553 reinterpret_cast<const AHardwareBuffer_Desc *>(&buff.buffer.description);
554 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
555 mapper.lock(buff.buffer.nativeHandle,
556 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER,
557 android::Rect(pDesc->width, pDesc->height),
558 (void **) &pixels);
559
560 // If we failed to lock the pixel buffer, we're about to crash, but log it first
561 if (!pixels) {
562 ALOGE("Camera failed to gain access to image buffer for writing");
563 }
564
565 // Fill in the test pixels
566 for (unsigned row = 0; row < pDesc->height; row++) {
567 for (unsigned col = 0; col < pDesc->width; col++) {
568 // Index into the row to check the pixel at this column.
569 // We expect 0xFF in the LSB channel, a vertical gradient in the
570 // second channel, a horitzontal gradient in the third channel, and
571 // 0xFF in the MSB.
572 // The exception is the very first 32 bits which is used for the
573 // time varying frame signature to avoid getting fooled by a static image.
574 uint32_t expectedPixel = 0xFF0000FF | // MSB and LSB
575 ((row & 0xFF) << 8) | // vertical gradient
576 ((col & 0xFF) << 16); // horizontal gradient
577 if ((row | col) == 0) {
578 static uint32_t sFrameTicker = 0;
579 expectedPixel = (sFrameTicker) & 0xFF;
580 sFrameTicker++;
581 }
582 pixels[col] = expectedPixel;
583 }
584 // Point to the next row
585 // NOTE: stride retrieved from gralloc is in units of pixels
586 pixels = pixels + pDesc->stride;
587 }
588
589 // Release our output buffer
590 mapper.unlock(buff.buffer.nativeHandle);
591}
592
593
594void EvsCamera::fillTestFrame(const BufferDesc_1_0& buff) {
595 BufferDesc_1_1 newBufDesc = {};
596 AHardwareBuffer_Desc desc = {
597 buff.width, // width
598 buff.height, // height
599 1, // layers, always 1 for EVS
600 buff.format, // One of AHardwareBuffer_Format
601 buff.usage, // Combination of AHardwareBuffer_UsageFlags
602 buff.stride, // Row stride in pixels
603 0, // Reserved
604 0 // Reserved
605 };
606 memcpy(&desc, &newBufDesc.buffer.description, sizeof(desc));
607 newBufDesc.buffer.nativeHandle = buff.memHandle;
608 newBufDesc.pixelSize = buff.pixelSize;
609 newBufDesc.bufferId = buff.bufferId;
610
611 return fillTestFrame(newBufDesc);
612}
613
614
615void EvsCamera::returnBuffer(const uint32_t bufferId, const buffer_handle_t memHandle) {
616 std::lock_guard <std::mutex> lock(mAccessLock);
617
618 if (memHandle == nullptr) {
619 ALOGE("ignoring doneWithFrame called with null handle");
620 } else if (bufferId >= mBuffers.size()) {
621 ALOGE("ignoring doneWithFrame called with invalid bufferId %d (max is %zu)",
622 bufferId, mBuffers.size()-1);
623 } else if (!mBuffers[bufferId].inUse) {
624 ALOGE("ignoring doneWithFrame called on frame %d which is already free",
625 bufferId);
626 } else {
627 // Mark the frame as available
628 mBuffers[bufferId].inUse = false;
629 mFramesInUse--;
630
631 // If this frame's index is high in the array, try to move it down
632 // to improve locality after mFramesAllowed has been reduced.
633 if (bufferId >= mFramesAllowed) {
634 // Find an empty slot lower in the array (which should always exist in this case)
635 for (auto&& rec : mBuffers) {
636 if (rec.handle == nullptr) {
637 rec.handle = mBuffers[bufferId].handle;
638 mBuffers[bufferId].handle = nullptr;
639 break;
640 }
641 }
642 }
643 }
644}
645
646
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700647sp<EvsCamera> EvsCamera::Create(const char *deviceName) {
648 unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr;
649
650 return Create(deviceName, nullCamInfo);
651}
652
653
654sp<EvsCamera> EvsCamera::Create(const char *deviceName,
655 unique_ptr<ConfigManager::CameraInfo> &camInfo,
656 const Stream *streamCfg) {
657 sp<EvsCamera> evsCamera = new EvsCamera(deviceName, camInfo);
658 if (evsCamera == nullptr) {
659 return nullptr;
660 }
661
662 /* default implementation does not use a given configuration */
663 (void)streamCfg;
664
665 /* Use the first resolution from the list for the testing */
666 auto it = camInfo->streamConfigurations.begin();
667 evsCamera->mWidth = it->second[1];
668 evsCamera->mHeight = it->second[2];
669 evsCamera->mDescription.v1.vendorFlags = 0xFFFFFFFF; // Arbitrary test value
670
671 evsCamera->mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
672 evsCamera->mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_CAMERA_WRITE |
673 GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY;
674
675 return evsCamera;
676}
677
678
Changyeon Jo2400b692019-07-18 21:32:48 -0700679} // namespace implementation
680} // namespace V1_0
681} // namespace evs
682} // namespace automotive
683} // namespace hardware
684} // namespace android