blob: b7e4efac02eff94ea4d98869864fe677f75bb50a [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 Jo6caf74b2019-12-02 09:50:41 -0800243Return<void> EvsCamera::getPhysicalCameraInfo(const hidl_string& id,
244 getCameraInfo_1_1_cb _hidl_cb) {
245 ALOGD("%s", __FUNCTION__);
246
247 // This works exactly same as getCameraInfo_1_1() in default implementation.
248 (void)id;
249 _hidl_cb(mDescription);
250 return Void();
251}
252
253
Changyeon Jo56c9b372019-10-09 14:04:31 -0700254Return<EvsResult> EvsCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc_1_1>& buffers) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700255 std::lock_guard <std::mutex> lock(mAccessLock);
Changyeon Jo56c9b372019-10-09 14:04:31 -0700256
257 for (auto&& buffer : buffers) {
258 returnBuffer(buffer.bufferId, buffer.buffer.nativeHandle);
259 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700260
261 return EvsResult::OK;
262}
263
264
265Return<EvsResult> EvsCamera::pauseVideoStream() {
266 // Default implementation does not support this.
267 return EvsResult::UNDERLYING_SERVICE_ERROR;
268}
269
270
271Return<EvsResult> EvsCamera::resumeVideoStream() {
272 // Default implementation does not support this.
273 return EvsResult::UNDERLYING_SERVICE_ERROR;
274}
275
276
Changyeon Jod2a82462019-07-30 11:57:17 -0700277Return<EvsResult> EvsCamera::setMaster() {
278 // Default implementation does not expect multiple subscribers and therefore
279 // return a success code always.
280 return EvsResult::OK;
281}
282
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700283Return<EvsResult> EvsCamera::forceMaster(const sp<IEvsDisplay>& ) {
284 // Default implementation does not expect multiple subscribers and therefore
285 // return a success code always.
286 return EvsResult::OK;
287}
288
Changyeon Jod2a82462019-07-30 11:57:17 -0700289
290Return<EvsResult> EvsCamera::unsetMaster() {
291 // Default implementation does not expect multiple subscribers and therefore
292 // return a success code always.
293 return EvsResult::OK;
294}
295
296
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700297Return<void> EvsCamera::getParameterList(getParameterList_cb _hidl_cb) {
298 hidl_vec<CameraParam> hidlCtrls;
299 hidlCtrls.resize(mCameraInfo->controls.size());
300 unsigned idx = 0;
301 for (auto& [cid, cfg] : mCameraInfo->controls) {
302 hidlCtrls[idx++] = cid;
303 }
304
305 _hidl_cb(hidlCtrls);
306 return Void();
307}
308
309
310Return<void> EvsCamera::getIntParameterRange(CameraParam id,
311 getIntParameterRange_cb _hidl_cb) {
312 auto range = mCameraInfo->controls[id];
313 _hidl_cb(get<0>(range), get<1>(range), get<2>(range));
314 return Void();
315}
316
317
318Return<void> EvsCamera::setIntParameter(CameraParam id, int32_t value,
319 setIntParameter_cb _hidl_cb) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700320 // Default implementation does not support this.
321 (void)id;
322 (void)value;
323 _hidl_cb(EvsResult::INVALID_ARG, 0);
324 return Void();
325}
326
327
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700328Return<void> EvsCamera::getIntParameter(CameraParam id,
329 getIntParameter_cb _hidl_cb) {
Changyeon Jod2a82462019-07-30 11:57:17 -0700330 // Default implementation does not support this.
331 (void)id;
332 _hidl_cb(EvsResult::INVALID_ARG, 0);
333 return Void();
334}
335
336
Changyeon Jo2400b692019-07-18 21:32:48 -0700337bool EvsCamera::setAvailableFrames_Locked(unsigned bufferCount) {
338 if (bufferCount < 1) {
339 ALOGE("Ignoring request to set buffer count to zero");
340 return false;
341 }
342 if (bufferCount > MAX_BUFFERS_IN_FLIGHT) {
343 ALOGE("Rejecting buffer request in excess of internal limit");
344 return false;
345 }
346
347 // Is an increase required?
348 if (mFramesAllowed < bufferCount) {
349 // An increase is required
350 unsigned needed = bufferCount - mFramesAllowed;
351 ALOGI("Allocating %d buffers for camera frames", needed);
352
353 unsigned added = increaseAvailableFrames_Locked(needed);
354 if (added != needed) {
355 // If we didn't add all the frames we needed, then roll back to the previous state
356 ALOGE("Rolling back to previous frame queue size");
357 decreaseAvailableFrames_Locked(added);
358 return false;
359 }
360 } else if (mFramesAllowed > bufferCount) {
361 // A decrease is required
362 unsigned framesToRelease = mFramesAllowed - bufferCount;
363 ALOGI("Returning %d camera frame buffers", framesToRelease);
364
365 unsigned released = decreaseAvailableFrames_Locked(framesToRelease);
366 if (released != framesToRelease) {
367 // This shouldn't happen with a properly behaving client because the client
368 // should only make this call after returning sufficient outstanding buffers
369 // to allow a clean resize.
370 ALOGE("Buffer queue shrink failed -- too many buffers currently in use?");
371 }
372 }
373
374 return true;
375}
376
377
378unsigned EvsCamera::increaseAvailableFrames_Locked(unsigned numToAdd) {
379 // Acquire the graphics buffer allocator
380 GraphicBufferAllocator &alloc(GraphicBufferAllocator::get());
381
382 unsigned added = 0;
383
384 while (added < numToAdd) {
385 buffer_handle_t memHandle = nullptr;
386 status_t result = alloc.allocate(mWidth, mHeight, mFormat, 1, mUsage,
387 &memHandle, &mStride, 0, "EvsCamera");
388 if (result != NO_ERROR) {
389 ALOGE("Error %d allocating %d x %d graphics buffer", result, mWidth, mHeight);
390 break;
391 }
392 if (!memHandle) {
393 ALOGE("We didn't get a buffer handle back from the allocator");
394 break;
395 }
396
397 // Find a place to store the new buffer
398 bool stored = false;
399 for (auto&& rec : mBuffers) {
400 if (rec.handle == nullptr) {
401 // Use this existing entry
402 rec.handle = memHandle;
403 rec.inUse = false;
404 stored = true;
405 break;
406 }
407 }
408 if (!stored) {
409 // Add a BufferRecord wrapping this handle to our set of available buffers
410 mBuffers.emplace_back(memHandle);
411 }
412
413 mFramesAllowed++;
414 added++;
415 }
416
417 return added;
418}
419
420
421unsigned EvsCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) {
422 // Acquire the graphics buffer allocator
423 GraphicBufferAllocator &alloc(GraphicBufferAllocator::get());
424
425 unsigned removed = 0;
426
427 for (auto&& rec : mBuffers) {
428 // Is this record not in use, but holding a buffer that we can free?
429 if ((rec.inUse == false) && (rec.handle != nullptr)) {
430 // Release buffer and update the record so we can recognize it as "empty"
431 alloc.free(rec.handle);
432 rec.handle = nullptr;
433
434 mFramesAllowed--;
435 removed++;
436
437 if (removed == numToRemove) {
438 break;
439 }
440 }
441 }
442
443 return removed;
444}
445
446
447// This is the asynchronous frame generation thread that runs in parallel with the
448// main serving thread. There is one for each active camera instance.
449void EvsCamera::generateFrames() {
450 ALOGD("Frame generation loop started");
451
452 unsigned idx;
453
454 while (true) {
455 bool timeForFrame = false;
456 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
457
458 // Lock scope for updating shared state
459 {
460 std::lock_guard<std::mutex> lock(mAccessLock);
461
462 if (mStreamState != RUNNING) {
463 // Break out of our main thread loop
464 break;
465 }
466
467 // Are we allowed to issue another buffer?
468 if (mFramesInUse >= mFramesAllowed) {
469 // Can't do anything right now -- skip this frame
470 ALOGW("Skipped a frame because too many are in flight\n");
471 } else {
472 // Identify an available buffer to fill
473 for (idx = 0; idx < mBuffers.size(); idx++) {
474 if (!mBuffers[idx].inUse) {
475 if (mBuffers[idx].handle != nullptr) {
476 // Found an available record, so stop looking
477 break;
478 }
479 }
480 }
481 if (idx >= mBuffers.size()) {
482 // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed
483 ALOGE("Failed to find an available buffer slot\n");
484 } else {
485 // We're going to make the frame busy
486 mBuffers[idx].inUse = true;
487 mFramesInUse++;
488 timeForFrame = true;
489 }
490 }
491 }
492
493 if (timeForFrame) {
494 // Assemble the buffer description we'll transmit below
495 BufferDesc_1_1 newBuffer = {};
496 AHardwareBuffer_Desc* pDesc =
497 reinterpret_cast<AHardwareBuffer_Desc *>(&newBuffer.buffer.description);
498 pDesc->width = mWidth;
499 pDesc->height = mHeight;
500 pDesc->layers = 1;
501 pDesc->format = mFormat;
502 pDesc->usage = mUsage;
503 pDesc->stride = mStride;
504 newBuffer.buffer.nativeHandle = mBuffers[idx].handle;
505 newBuffer.pixelSize = sizeof(uint32_t);
506 newBuffer.bufferId = idx;
Changyeon Jo56c9b372019-10-09 14:04:31 -0700507 newBuffer.deviceId = mDescription.v1.cameraId;
508 newBuffer.timestamp = elapsedRealtimeNano();
Changyeon Jo2400b692019-07-18 21:32:48 -0700509
510 // Write test data into the image buffer
511 fillTestFrame(newBuffer);
512
513 // Issue the (asynchronous) callback to the client -- can't be holding the lock
Changyeon Jo56c9b372019-10-09 14:04:31 -0700514 hidl_vec<BufferDesc_1_1> frames;
515 frames.resize(1);
516 frames[0] = newBuffer;
517 auto result = mStream->deliverFrame_1_1(frames);
Changyeon Jo2400b692019-07-18 21:32:48 -0700518 if (result.isOk()) {
519 ALOGD("Delivered %p as id %d",
520 newBuffer.buffer.nativeHandle.getNativeHandle(), newBuffer.bufferId);
521 } else {
522 // This can happen if the client dies and is likely unrecoverable.
523 // To avoid consuming resources generating failing calls, we stop sending
524 // frames. Note, however, that the stream remains in the "STREAMING" state
525 // until cleaned up on the main thread.
526 ALOGE("Frame delivery call failed in the transport layer.");
527
528 // Since we didn't actually deliver it, mark the frame as available
529 std::lock_guard<std::mutex> lock(mAccessLock);
530 mBuffers[idx].inUse = false;
531 mFramesInUse--;
532
533 break;
534 }
535 }
536
537 // We arbitrarily choose to generate frames at 12 fps to ensure we pass the 10fps test requirement
538 static const int kTargetFrameRate = 12;
539 static const nsecs_t kTargetFrameTimeUs = 1000*1000 / kTargetFrameRate;
540 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
541 const nsecs_t workTimeUs = (now - startTime) / 1000;
542 const nsecs_t sleepDurationUs = kTargetFrameTimeUs - workTimeUs;
543 if (sleepDurationUs > 0) {
544 usleep(sleepDurationUs);
545 }
546 }
547
548 // If we've been asked to stop, send an event to signal the actual end of stream
Changyeon Jo56c9b372019-10-09 14:04:31 -0700549 EvsEventDesc event;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700550 event.aType = EvsEventType::STREAM_STOPPED;
551 auto result = mStream->notify(event);
Changyeon Jo2400b692019-07-18 21:32:48 -0700552 if (!result.isOk()) {
553 ALOGE("Error delivering end of stream marker");
554 }
555
556 return;
557}
558
559
560void EvsCamera::fillTestFrame(const BufferDesc_1_1& buff) {
561 // Lock our output buffer for writing
562 uint32_t *pixels = nullptr;
563 const AHardwareBuffer_Desc* pDesc =
564 reinterpret_cast<const AHardwareBuffer_Desc *>(&buff.buffer.description);
565 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
566 mapper.lock(buff.buffer.nativeHandle,
567 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER,
568 android::Rect(pDesc->width, pDesc->height),
569 (void **) &pixels);
570
571 // If we failed to lock the pixel buffer, we're about to crash, but log it first
572 if (!pixels) {
573 ALOGE("Camera failed to gain access to image buffer for writing");
574 }
575
576 // Fill in the test pixels
577 for (unsigned row = 0; row < pDesc->height; row++) {
578 for (unsigned col = 0; col < pDesc->width; col++) {
579 // Index into the row to check the pixel at this column.
580 // We expect 0xFF in the LSB channel, a vertical gradient in the
581 // second channel, a horitzontal gradient in the third channel, and
582 // 0xFF in the MSB.
583 // The exception is the very first 32 bits which is used for the
584 // time varying frame signature to avoid getting fooled by a static image.
585 uint32_t expectedPixel = 0xFF0000FF | // MSB and LSB
586 ((row & 0xFF) << 8) | // vertical gradient
587 ((col & 0xFF) << 16); // horizontal gradient
588 if ((row | col) == 0) {
589 static uint32_t sFrameTicker = 0;
590 expectedPixel = (sFrameTicker) & 0xFF;
591 sFrameTicker++;
592 }
593 pixels[col] = expectedPixel;
594 }
595 // Point to the next row
596 // NOTE: stride retrieved from gralloc is in units of pixels
597 pixels = pixels + pDesc->stride;
598 }
599
600 // Release our output buffer
601 mapper.unlock(buff.buffer.nativeHandle);
602}
603
604
605void EvsCamera::fillTestFrame(const BufferDesc_1_0& buff) {
606 BufferDesc_1_1 newBufDesc = {};
607 AHardwareBuffer_Desc desc = {
608 buff.width, // width
609 buff.height, // height
610 1, // layers, always 1 for EVS
611 buff.format, // One of AHardwareBuffer_Format
612 buff.usage, // Combination of AHardwareBuffer_UsageFlags
613 buff.stride, // Row stride in pixels
614 0, // Reserved
615 0 // Reserved
616 };
617 memcpy(&desc, &newBufDesc.buffer.description, sizeof(desc));
618 newBufDesc.buffer.nativeHandle = buff.memHandle;
619 newBufDesc.pixelSize = buff.pixelSize;
620 newBufDesc.bufferId = buff.bufferId;
621
622 return fillTestFrame(newBufDesc);
623}
624
625
626void EvsCamera::returnBuffer(const uint32_t bufferId, const buffer_handle_t memHandle) {
627 std::lock_guard <std::mutex> lock(mAccessLock);
628
629 if (memHandle == nullptr) {
630 ALOGE("ignoring doneWithFrame called with null handle");
631 } else if (bufferId >= mBuffers.size()) {
632 ALOGE("ignoring doneWithFrame called with invalid bufferId %d (max is %zu)",
633 bufferId, mBuffers.size()-1);
634 } else if (!mBuffers[bufferId].inUse) {
635 ALOGE("ignoring doneWithFrame called on frame %d which is already free",
636 bufferId);
637 } else {
638 // Mark the frame as available
639 mBuffers[bufferId].inUse = false;
640 mFramesInUse--;
641
642 // If this frame's index is high in the array, try to move it down
643 // to improve locality after mFramesAllowed has been reduced.
644 if (bufferId >= mFramesAllowed) {
645 // Find an empty slot lower in the array (which should always exist in this case)
646 for (auto&& rec : mBuffers) {
647 if (rec.handle == nullptr) {
648 rec.handle = mBuffers[bufferId].handle;
649 mBuffers[bufferId].handle = nullptr;
650 break;
651 }
652 }
653 }
654 }
655}
656
657
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700658sp<EvsCamera> EvsCamera::Create(const char *deviceName) {
659 unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr;
660
661 return Create(deviceName, nullCamInfo);
662}
663
664
665sp<EvsCamera> EvsCamera::Create(const char *deviceName,
666 unique_ptr<ConfigManager::CameraInfo> &camInfo,
667 const Stream *streamCfg) {
668 sp<EvsCamera> evsCamera = new EvsCamera(deviceName, camInfo);
669 if (evsCamera == nullptr) {
670 return nullptr;
671 }
672
673 /* default implementation does not use a given configuration */
674 (void)streamCfg;
675
676 /* Use the first resolution from the list for the testing */
677 auto it = camInfo->streamConfigurations.begin();
678 evsCamera->mWidth = it->second[1];
679 evsCamera->mHeight = it->second[2];
680 evsCamera->mDescription.v1.vendorFlags = 0xFFFFFFFF; // Arbitrary test value
681
682 evsCamera->mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
683 evsCamera->mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_CAMERA_WRITE |
684 GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY;
685
686 return evsCamera;
687}
688
689
Changyeon Jo2400b692019-07-18 21:32:48 -0700690} // namespace implementation
691} // namespace V1_0
692} // namespace evs
693} // namespace automotive
694} // namespace hardware
695} // namespace android