blob: 520670a5d622ef364699e1bd0a04124188d85829 [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
Changyeon Jo2400b692019-07-18 21:32:48 -070017#include "EvsCamera.h"
Changyeon Jo33ba66b2022-01-16 16:33:52 -080018#include "ConfigManager.h"
Changyeon Jo2400b692019-07-18 21:32:48 -070019#include "EvsEnumerator.h"
20
21#include <ui/GraphicBufferAllocator.h>
22#include <ui/GraphicBufferMapper.h>
Changyeon Jo56c9b372019-10-09 14:04:31 -070023#include <utils/SystemClock.h>
Changyeon Jo2400b692019-07-18 21:32:48 -070024
Changyeon Jo33ba66b2022-01-16 16:33:52 -080025namespace {
Changyeon Jo2400b692019-07-18 21:32:48 -070026
27// Arbitrary limit on number of graphics buffers allowed to be allocated
28// Safeguards against unreasonable resource consumption and provides a testable limit
Changyeon Jo33ba66b2022-01-16 16:33:52 -080029constexpr unsigned kMaxBuffersInFlight = 100;
Changyeon Jo2400b692019-07-18 21:32:48 -070030
Changyeon Jo33ba66b2022-01-16 16:33:52 -080031// Minimum number of buffers to run a video stream
32constexpr int kMinimumBuffersInFlight = 1;
Changyeon Jo2400b692019-07-18 21:32:48 -070033
Changyeon Jo33ba66b2022-01-16 16:33:52 -080034// Colors for the colorbar test pattern in ABGR format
35constexpr uint32_t kColors[] = {
36 0xFFFFFFFF, // white
37 0xFF00FFFF, // yellow
38 0xFFFFFF00, // cyan
39 0xFF00FF00, // green
40 0xFFFF00FF, // fuchsia
41 0xFF0000FF, // red
42 0xFFFF0000, // blue
43 0xFF000000, // black
44};
45constexpr uint32_t kNumColors = sizeof(kColors) / sizeof(kColors[0]);
Changyeon Jo2400b692019-07-18 21:32:48 -070046
Changyeon Jo33ba66b2022-01-16 16:33:52 -080047} // namespace
48
49namespace android::hardware::automotive::evs::V1_1::implementation {
50
51using V1_0::EvsResult;
52
53EvsCamera::EvsCamera(const char* id, std::unique_ptr<ConfigManager::CameraInfo>& camInfo)
54 : mFramesAllowed(0), mFramesInUse(0), mStreamState(STOPPED), mCameraInfo(camInfo) {
55 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -070056
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070057 /* set a camera id */
58 mDescription.v1.cameraId = id;
Changyeon Jo2400b692019-07-18 21:32:48 -070059
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070060 /* set camera metadata */
Changyeon Jo33ba66b2022-01-16 16:33:52 -080061 mDescription.metadata.setToExternal((uint8_t*)camInfo->characteristics,
Changyeon Joc6fa0ab2019-10-12 05:25:44 -070062 get_camera_metadata_size(camInfo->characteristics));
Changyeon Jo2400b692019-07-18 21:32:48 -070063}
64
Changyeon Jo2400b692019-07-18 21:32:48 -070065EvsCamera::~EvsCamera() {
Changyeon Jo33ba66b2022-01-16 16:33:52 -080066 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -070067 forceShutdown();
68}
69
Changyeon Jo2400b692019-07-18 21:32:48 -070070// This gets called if another caller "steals" ownership of the camera
Changyeon Jo33ba66b2022-01-16 16:33:52 -080071void EvsCamera::forceShutdown() {
72 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -070073
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
Changyeon Jo33ba66b2022-01-16 16:33:52 -080079 std::lock_guard<std::mutex> lock(mAccessLock);
Changyeon Jo2400b692019-07-18 21:32:48 -070080
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
Changyeon Jo2400b692019-07-18 21:32:48 -070099// Methods from ::android::hardware::automotive::evs::V1_0::IEvsCamera follow.
100Return<void> EvsCamera::getCameraInfo(getCameraInfo_cb _hidl_cb) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800101 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -0700102
103 // Send back our self description
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700104 _hidl_cb(mDescription.v1);
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800105 return {};
Changyeon Jo2400b692019-07-18 21:32:48 -0700106}
107
Changyeon Jo2400b692019-07-18 21:32:48 -0700108Return<EvsResult> EvsCamera::setMaxFramesInFlight(uint32_t bufferCount) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800109 ALOGD("%s, bufferCount = %u", __FUNCTION__, bufferCount);
110
Changyeon Jo2400b692019-07-18 21:32:48 -0700111 std::lock_guard<std::mutex> lock(mAccessLock);
112
113 // If we've been displaced by another owner of the camera, then we can't do anything else
114 if (mStreamState == DEAD) {
115 ALOGE("ignoring setMaxFramesInFlight call when camera has been lost.");
116 return EvsResult::OWNERSHIP_LOST;
117 }
118
119 // We cannot function without at least one video buffer to send data
120 if (bufferCount < 1) {
121 ALOGE("Ignoring setMaxFramesInFlight with less than one buffer requested");
122 return EvsResult::INVALID_ARG;
123 }
124
125 // Update our internal state
126 if (setAvailableFrames_Locked(bufferCount)) {
127 return EvsResult::OK;
128 } else {
129 return EvsResult::BUFFER_NOT_AVAILABLE;
130 }
131}
132
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800133Return<EvsResult> EvsCamera::startVideoStream(const ::android::sp<V1_0::IEvsCameraStream>& stream) {
134 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -0700135
Changyeon Jo2400b692019-07-18 21:32:48 -0700136 std::lock_guard<std::mutex> lock(mAccessLock);
137
138 // If we've been displaced by another owner of the camera, then we can't do anything else
139 if (mStreamState == DEAD) {
140 ALOGE("ignoring startVideoStream call when camera has been lost.");
141 return EvsResult::OWNERSHIP_LOST;
142 }
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800143
Changyeon Jo2400b692019-07-18 21:32:48 -0700144 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
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800150 if (mFramesAllowed < kMinimumBuffersInFlight) {
151 if (!setAvailableFrames_Locked(kMinimumBuffersInFlight)) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700152 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
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800158 mStream = IEvsCameraStream::castFrom(stream).withDefault(nullptr);
159 if (!mStream) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700160 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;
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800166 mCaptureThread = std::thread([this]() { generateFrames(); });
Changyeon Jo2400b692019-07-18 21:32:48 -0700167
168 return EvsResult::OK;
169}
170
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800171Return<void> EvsCamera::doneWithFrame(const V1_0::BufferDesc& buffer) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700172 std::lock_guard<std::mutex> lock(mAccessLock);
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800173 returnBufferLocked(buffer.bufferId, buffer.memHandle);
Changyeon Jo2400b692019-07-18 21:32:48 -0700174
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800175 return {};
Changyeon Jo2400b692019-07-18 21:32:48 -0700176}
177
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800178Return<void> EvsCamera::stopVideoStream() {
179 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -0700180
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800181 std::unique_lock<std::mutex> lock(mAccessLock);
182
183 if (mStreamState != RUNNING) {
184 return {};
185 }
186
187 // Tell the GenerateFrames loop we want it to stop
188 mStreamState = STOPPING;
189
190 // Block outside the mutex until the "stop" flag has been acknowledged
191 // We won't send any more frames, but the client might still get some already in flight
192 ALOGD("Waiting for stream thread to end...");
193 lock.unlock();
194 if (mCaptureThread.joinable()) {
195 mCaptureThread.join();
196 }
197 lock.lock();
198
199 mStreamState = STOPPED;
200 mStream = nullptr;
201 ALOGD("Stream marked STOPPED.");
202
203 return {};
204}
205
206Return<int32_t> EvsCamera::getExtendedInfo(uint32_t opaqueIdentifier) {
207 ALOGD("%s", __FUNCTION__);
208
209 std::lock_guard<std::mutex> lock(mAccessLock);
210 const auto it = mExtInfo.find(opaqueIdentifier);
211 if (it == mExtInfo.end()) {
212 // Return zero by default as required by the spec
213 return 0;
214 } else {
215 return it->second[0];
216 }
217}
218
219Return<EvsResult> EvsCamera::setExtendedInfo([[maybe_unused]] uint32_t opaqueIdentifier,
220 [[maybe_unused]] int32_t opaqueValue) {
221 ALOGD("%s", __FUNCTION__);
222
Changyeon Jo2400b692019-07-18 21:32:48 -0700223 std::lock_guard<std::mutex> lock(mAccessLock);
224
225 // If we've been displaced by another owner of the camera, then we can't do anything else
226 if (mStreamState == DEAD) {
227 ALOGE("ignoring setExtendedInfo call when camera has been lost.");
228 return EvsResult::OWNERSHIP_LOST;
229 }
230
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800231 mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue);
232 return EvsResult::OK;
Changyeon Jo2400b692019-07-18 21:32:48 -0700233}
234
Changyeon Jo2400b692019-07-18 21:32:48 -0700235// Methods from ::android::hardware::automotive::evs::V1_1::IEvsCamera follow.
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700236Return<void> EvsCamera::getCameraInfo_1_1(getCameraInfo_1_1_cb _hidl_cb) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800237 ALOGD("%s", __FUNCTION__);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700238
239 // Send back our self description
240 _hidl_cb(mDescription);
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800241 return {};
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700242}
243
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800244Return<void> EvsCamera::getPhysicalCameraInfo([[maybe_unused]] const hidl_string& id,
Changyeon Jo6caf74b2019-12-02 09:50:41 -0800245 getCameraInfo_1_1_cb _hidl_cb) {
246 ALOGD("%s", __FUNCTION__);
247
248 // This works exactly same as getCameraInfo_1_1() in default implementation.
Changyeon Jo6caf74b2019-12-02 09:50:41 -0800249 _hidl_cb(mDescription);
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800250 return {};
Changyeon Jo6caf74b2019-12-02 09:50:41 -0800251}
252
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800253Return<EvsResult> EvsCamera::doneWithFrame_1_1(const hidl_vec<BufferDesc>& buffers) {
254 ALOGD("%s", __FUNCTION__);
Changyeon Jo6caf74b2019-12-02 09:50:41 -0800255
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800256 std::lock_guard<std::mutex> lock(mAccessLock);
Changyeon Jo56c9b372019-10-09 14:04:31 -0700257 for (auto&& buffer : buffers) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800258 returnBufferLocked(buffer.bufferId, buffer.buffer.nativeHandle);
Changyeon Jo56c9b372019-10-09 14:04:31 -0700259 }
Changyeon Jo2400b692019-07-18 21:32:48 -0700260 return EvsResult::OK;
261}
262
Changyeon Jo2400b692019-07-18 21:32:48 -0700263Return<EvsResult> EvsCamera::pauseVideoStream() {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800264 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -0700265 // Default implementation does not support this.
266 return EvsResult::UNDERLYING_SERVICE_ERROR;
267}
268
Changyeon Jo2400b692019-07-18 21:32:48 -0700269Return<EvsResult> EvsCamera::resumeVideoStream() {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800270 ALOGD("%s", __FUNCTION__);
Changyeon Jo2400b692019-07-18 21:32:48 -0700271 // Default implementation does not support this.
272 return EvsResult::UNDERLYING_SERVICE_ERROR;
273}
274
Changyeon Jod2a82462019-07-30 11:57:17 -0700275Return<EvsResult> EvsCamera::setMaster() {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800276 ALOGD("%s", __FUNCTION__);
Changyeon Jod2a82462019-07-30 11:57:17 -0700277 // Default implementation does not expect multiple subscribers and therefore
278 // return a success code always.
279 return EvsResult::OK;
280}
281
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800282Return<EvsResult> EvsCamera::forceMaster(const sp<V1_0::IEvsDisplay>&) {
283 ALOGD("%s", __FUNCTION__);
Changyeon Jo0d0228d2019-08-17 21:40:28 -0700284 // 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 -0700289Return<EvsResult> EvsCamera::unsetMaster() {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800290 ALOGD("%s", __FUNCTION__);
Changyeon Jod2a82462019-07-30 11:57:17 -0700291 // Default implementation does not expect multiple subscribers and therefore
292 // return a success code always.
293 return EvsResult::OK;
294}
295
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700296Return<void> EvsCamera::getParameterList(getParameterList_cb _hidl_cb) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800297 ALOGD("%s", __FUNCTION__);
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700298 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);
Changyeon Jo3e697442020-03-27 14:19:46 -0700306 return {};
307}
308
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800309Return<void> EvsCamera::getIntParameterRange(CameraParam id, getIntParameterRange_cb _hidl_cb) {
310 ALOGD("%s", __FUNCTION__);
311 auto it = mCameraInfo->controls.find(id);
312 if (it == mCameraInfo->controls.end()) {
313 _hidl_cb(0, 0, 0);
314 } else {
315 _hidl_cb(std::get<0>(it->second), std::get<1>(it->second), std::get<2>(it->second));
316 }
317 return {};
318}
319
320Return<void> EvsCamera::setIntParameter(CameraParam id, int32_t value,
321 setIntParameter_cb _hidl_cb) {
322 ALOGD("%s", __FUNCTION__);
323 mParams.insert_or_assign(id, value);
324 _hidl_cb(EvsResult::OK, {value});
325 return {};
326}
327
328Return<void> EvsCamera::getIntParameter([[maybe_unused]] CameraParam id,
329 getIntParameter_cb _hidl_cb) {
330 ALOGD("%s", __FUNCTION__);
331 auto it = mParams.find(id);
332 std::vector<int32_t> values;
333 if (it == mParams.end()) {
334 _hidl_cb(EvsResult::INVALID_ARG, values);
335 } else {
336 values.push_back(it->second);
337 _hidl_cb(EvsResult::OK, values);
338 }
339 return {};
340}
341
342Return<EvsResult> EvsCamera::setExtendedInfo_1_1(uint32_t opaqueIdentifier,
343 const hidl_vec<uint8_t>& opaqueValue) {
344 ALOGD("%s", __FUNCTION__);
345 mExtInfo.insert_or_assign(opaqueIdentifier, opaqueValue);
346 return EvsResult::OK;
347}
348
349Return<void> EvsCamera::getExtendedInfo_1_1(uint32_t opaqueIdentifier,
350 getExtendedInfo_1_1_cb _hidl_cb) {
351 ALOGD("%s", __FUNCTION__);
352 auto status = EvsResult::OK;
353 hidl_vec<uint8_t> value;
354 const auto it = mExtInfo.find(opaqueIdentifier);
355 if (it == mExtInfo.end()) {
356 status = EvsResult::INVALID_ARG;
357 } else {
358 value = it->second;
359 }
360 _hidl_cb(status, value);
361 return {};
362}
363
364Return<void> EvsCamera::importExternalBuffers([[maybe_unused]] const hidl_vec<BufferDesc>& buffers,
365 importExternalBuffers_cb _hidl_cb) {
366 auto numBuffersToAdd = buffers.size();
367 if (numBuffersToAdd < 1) {
368 ALOGD("No buffers to add");
369 _hidl_cb(EvsResult::OK, mFramesAllowed);
370 return {};
371 }
372
373 {
374 std::scoped_lock<std::mutex> lock(mAccessLock);
375
376 if (numBuffersToAdd > (kMaxBuffersInFlight - mFramesAllowed)) {
377 numBuffersToAdd -= (kMaxBuffersInFlight - mFramesAllowed);
378 ALOGW("Exceed the limit on number of buffers. %" PRIu64 " buffers will be added only.",
Changyeon Jo339f0ac2022-01-30 22:19:32 -0800379 static_cast<uint64_t>(numBuffersToAdd));
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800380 }
381
382 GraphicBufferMapper& mapper = GraphicBufferMapper::get();
383 const auto before = mFramesAllowed;
384 for (auto i = 0; i < numBuffersToAdd; ++i) {
385 // TODO: reject if external buffer is configured differently.
386 auto& b = buffers[i];
387 const AHardwareBuffer_Desc* pDesc =
388 reinterpret_cast<const AHardwareBuffer_Desc*>(&b.buffer.description);
389
390 // Import a buffer to add
391 buffer_handle_t memHandle = nullptr;
392 status_t result =
393 mapper.importBuffer(b.buffer.nativeHandle, pDesc->width, pDesc->height, 1,
394 pDesc->format, pDesc->usage, pDesc->stride, &memHandle);
395 if (result != android::NO_ERROR || !memHandle) {
396 ALOGW("Failed to import a buffer %d", b.bufferId);
397 continue;
398 }
399
400 auto stored = false;
401 for (auto&& rec : mBuffers) {
402 if (rec.handle == nullptr) {
403 // Use this existing entry
404 rec.handle = memHandle;
405 rec.inUse = false;
406
407 stored = true;
408 break;
409 }
410 }
411
412 if (!stored) {
413 // Add a BufferRecord wrapping this handle to our set of available buffers
414 mBuffers.emplace_back(memHandle);
415 }
416
417 ++mFramesAllowed;
418 }
419
420 _hidl_cb(EvsResult::OK, mFramesAllowed - before);
421 return {};
422 }
423}
Changyeon Jo93594ab2020-02-17 13:28:56 -0800424
Changyeon Jo2400b692019-07-18 21:32:48 -0700425bool EvsCamera::setAvailableFrames_Locked(unsigned bufferCount) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800426 if (bufferCount < kMinimumBuffersInFlight) {
427 ALOGE("Ignoring request to set buffer count below the minimum number of buffers to run a "
428 "video stream");
Changyeon Jo2400b692019-07-18 21:32:48 -0700429 return false;
430 }
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800431 if (bufferCount > kMaxBuffersInFlight) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700432 ALOGE("Rejecting buffer request in excess of internal limit");
433 return false;
434 }
435
436 // Is an increase required?
437 if (mFramesAllowed < bufferCount) {
438 // An increase is required
439 unsigned needed = bufferCount - mFramesAllowed;
440 ALOGI("Allocating %d buffers for camera frames", needed);
441
442 unsigned added = increaseAvailableFrames_Locked(needed);
443 if (added != needed) {
444 // If we didn't add all the frames we needed, then roll back to the previous state
445 ALOGE("Rolling back to previous frame queue size");
446 decreaseAvailableFrames_Locked(added);
447 return false;
448 }
449 } else if (mFramesAllowed > bufferCount) {
450 // A decrease is required
451 unsigned framesToRelease = mFramesAllowed - bufferCount;
452 ALOGI("Returning %d camera frame buffers", framesToRelease);
453
454 unsigned released = decreaseAvailableFrames_Locked(framesToRelease);
455 if (released != framesToRelease) {
456 // This shouldn't happen with a properly behaving client because the client
457 // should only make this call after returning sufficient outstanding buffers
458 // to allow a clean resize.
459 ALOGE("Buffer queue shrink failed -- too many buffers currently in use?");
460 }
461 }
462
463 return true;
464}
465
Changyeon Jo2400b692019-07-18 21:32:48 -0700466unsigned EvsCamera::increaseAvailableFrames_Locked(unsigned numToAdd) {
467 // Acquire the graphics buffer allocator
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800468 GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Changyeon Jo2400b692019-07-18 21:32:48 -0700469
470 unsigned added = 0;
471
472 while (added < numToAdd) {
473 buffer_handle_t memHandle = nullptr;
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800474 status_t result = alloc.allocate(mWidth, mHeight, mFormat, 1, mUsage, &memHandle, &mStride,
475 0, "EvsCamera");
Changyeon Jo2400b692019-07-18 21:32:48 -0700476 if (result != NO_ERROR) {
477 ALOGE("Error %d allocating %d x %d graphics buffer", result, mWidth, mHeight);
478 break;
479 }
480 if (!memHandle) {
481 ALOGE("We didn't get a buffer handle back from the allocator");
482 break;
483 }
484
485 // Find a place to store the new buffer
486 bool stored = false;
487 for (auto&& rec : mBuffers) {
488 if (rec.handle == nullptr) {
489 // Use this existing entry
490 rec.handle = memHandle;
491 rec.inUse = false;
492 stored = true;
493 break;
494 }
495 }
496 if (!stored) {
497 // Add a BufferRecord wrapping this handle to our set of available buffers
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800498 mBuffers.push_back(std::move(BufferRecord(memHandle)));
Changyeon Jo2400b692019-07-18 21:32:48 -0700499 }
500
501 mFramesAllowed++;
502 added++;
503 }
504
505 return added;
506}
507
Changyeon Jo2400b692019-07-18 21:32:48 -0700508unsigned EvsCamera::decreaseAvailableFrames_Locked(unsigned numToRemove) {
509 // Acquire the graphics buffer allocator
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800510 GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
Changyeon Jo2400b692019-07-18 21:32:48 -0700511
512 unsigned removed = 0;
513
514 for (auto&& rec : mBuffers) {
515 // Is this record not in use, but holding a buffer that we can free?
516 if ((rec.inUse == false) && (rec.handle != nullptr)) {
517 // Release buffer and update the record so we can recognize it as "empty"
518 alloc.free(rec.handle);
519 rec.handle = nullptr;
520
521 mFramesAllowed--;
522 removed++;
523
524 if (removed == numToRemove) {
525 break;
526 }
527 }
528 }
529
530 return removed;
531}
532
Changyeon Jo2400b692019-07-18 21:32:48 -0700533// This is the asynchronous frame generation thread that runs in parallel with the
534// main serving thread. There is one for each active camera instance.
535void EvsCamera::generateFrames() {
536 ALOGD("Frame generation loop started");
537
538 unsigned idx;
Changyeon Jo2400b692019-07-18 21:32:48 -0700539 while (true) {
540 bool timeForFrame = false;
541 nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
542
543 // Lock scope for updating shared state
544 {
545 std::lock_guard<std::mutex> lock(mAccessLock);
546
547 if (mStreamState != RUNNING) {
548 // Break out of our main thread loop
549 break;
550 }
551
552 // Are we allowed to issue another buffer?
553 if (mFramesInUse >= mFramesAllowed) {
554 // Can't do anything right now -- skip this frame
555 ALOGW("Skipped a frame because too many are in flight\n");
556 } else {
557 // Identify an available buffer to fill
558 for (idx = 0; idx < mBuffers.size(); idx++) {
559 if (!mBuffers[idx].inUse) {
560 if (mBuffers[idx].handle != nullptr) {
561 // Found an available record, so stop looking
562 break;
563 }
564 }
565 }
566 if (idx >= mBuffers.size()) {
567 // This shouldn't happen since we already checked mFramesInUse vs mFramesAllowed
568 ALOGE("Failed to find an available buffer slot\n");
569 } else {
570 // We're going to make the frame busy
571 mBuffers[idx].inUse = true;
572 mFramesInUse++;
573 timeForFrame = true;
574 }
575 }
576 }
577
578 if (timeForFrame) {
579 // Assemble the buffer description we'll transmit below
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800580 BufferDesc newBuffer = {};
Changyeon Jo2400b692019-07-18 21:32:48 -0700581 AHardwareBuffer_Desc* pDesc =
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800582 reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description);
Changyeon Jo2400b692019-07-18 21:32:48 -0700583 pDesc->width = mWidth;
584 pDesc->height = mHeight;
585 pDesc->layers = 1;
586 pDesc->format = mFormat;
587 pDesc->usage = mUsage;
588 pDesc->stride = mStride;
589 newBuffer.buffer.nativeHandle = mBuffers[idx].handle;
590 newBuffer.pixelSize = sizeof(uint32_t);
591 newBuffer.bufferId = idx;
Changyeon Jo56c9b372019-10-09 14:04:31 -0700592 newBuffer.deviceId = mDescription.v1.cameraId;
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800593 newBuffer.timestamp = elapsedRealtimeNano() * 1e+3; // timestamps is in microseconds
Changyeon Jo2400b692019-07-18 21:32:48 -0700594
595 // Write test data into the image buffer
596 fillTestFrame(newBuffer);
597
598 // Issue the (asynchronous) callback to the client -- can't be holding the lock
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800599 auto result = mStream->deliverFrame_1_1({newBuffer});
Changyeon Jo2400b692019-07-18 21:32:48 -0700600 if (result.isOk()) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800601 ALOGD("Delivered %p as id %d", newBuffer.buffer.nativeHandle.getNativeHandle(),
602 newBuffer.bufferId);
Changyeon Jo2400b692019-07-18 21:32:48 -0700603 } else {
604 // This can happen if the client dies and is likely unrecoverable.
605 // To avoid consuming resources generating failing calls, we stop sending
606 // frames. Note, however, that the stream remains in the "STREAMING" state
607 // until cleaned up on the main thread.
608 ALOGE("Frame delivery call failed in the transport layer.");
609
610 // Since we didn't actually deliver it, mark the frame as available
611 std::lock_guard<std::mutex> lock(mAccessLock);
612 mBuffers[idx].inUse = false;
613 mFramesInUse--;
614
615 break;
616 }
617 }
618
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800619 // We arbitrarily choose to generate frames at 15 fps to ensure we pass the 10fps test
620 // requirement
621 static const int kTargetFrameRate = 15;
622 static const nsecs_t kTargetFrameIntervalUs = 1000 * 1000 / kTargetFrameRate;
Changyeon Jo2400b692019-07-18 21:32:48 -0700623 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800624 const nsecs_t elapsedTimeUs = (now - startTime) / 1000;
625 const nsecs_t sleepDurationUs = kTargetFrameIntervalUs - elapsedTimeUs;
Changyeon Jo2400b692019-07-18 21:32:48 -0700626 if (sleepDurationUs > 0) {
627 usleep(sleepDurationUs);
628 }
629 }
630
631 // If we've been asked to stop, send an event to signal the actual end of stream
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800632 EvsEventDesc event = {
633 .aType = EvsEventType::STREAM_STOPPED,
634 };
635 if (!mStream->notify(event).isOk()) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700636 ALOGE("Error delivering end of stream marker");
637 }
638
639 return;
640}
641
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800642void EvsCamera::fillTestFrame(const BufferDesc& buff) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700643 // Lock our output buffer for writing
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800644 uint32_t* pixels = nullptr;
Changyeon Jo2400b692019-07-18 21:32:48 -0700645 const AHardwareBuffer_Desc* pDesc =
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800646 reinterpret_cast<const AHardwareBuffer_Desc*>(&buff.buffer.description);
647 GraphicBufferMapper& mapper = GraphicBufferMapper::get();
Changyeon Jo2400b692019-07-18 21:32:48 -0700648 mapper.lock(buff.buffer.nativeHandle,
649 GRALLOC_USAGE_SW_WRITE_OFTEN | GRALLOC_USAGE_SW_READ_NEVER,
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800650 android::Rect(pDesc->width, pDesc->height), (void**)&pixels);
Changyeon Jo2400b692019-07-18 21:32:48 -0700651
652 // If we failed to lock the pixel buffer, we're about to crash, but log it first
653 if (!pixels) {
654 ALOGE("Camera failed to gain access to image buffer for writing");
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800655 return;
Changyeon Jo2400b692019-07-18 21:32:48 -0700656 }
657
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800658 // Fill in the test pixels; the colorbar in ABGR format
Changyeon Jo2400b692019-07-18 21:32:48 -0700659 for (unsigned row = 0; row < pDesc->height; row++) {
660 for (unsigned col = 0; col < pDesc->width; col++) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800661 const uint32_t index = col * kNumColors / pDesc->width;
662 pixels[col] = kColors[index];
Changyeon Jo2400b692019-07-18 21:32:48 -0700663 }
664 // Point to the next row
665 // NOTE: stride retrieved from gralloc is in units of pixels
666 pixels = pixels + pDesc->stride;
667 }
668
669 // Release our output buffer
670 mapper.unlock(buff.buffer.nativeHandle);
671}
672
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800673void EvsCamera::fillTestFrame(const V1_0::BufferDesc& buff) {
674 BufferDesc newBuffer = {
675 .buffer.nativeHandle = buff.memHandle,
676 .pixelSize = buff.pixelSize,
677 .bufferId = buff.bufferId,
Changyeon Jo2400b692019-07-18 21:32:48 -0700678 };
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800679 AHardwareBuffer_Desc* pDesc =
680 reinterpret_cast<AHardwareBuffer_Desc*>(&newBuffer.buffer.description);
681 *pDesc = {
682 buff.width, // width
683 buff.height, // height
684 1, // layers, always 1 for EVS
685 buff.format, // One of AHardwareBuffer_Format
686 buff.usage, // Combination of AHardwareBuffer_UsageFlags
687 buff.stride, // Row stride in pixels
688 0, // Reserved
689 0 // Reserved
690 };
691 return fillTestFrame(newBuffer);
Changyeon Jo2400b692019-07-18 21:32:48 -0700692}
693
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800694void EvsCamera::returnBufferLocked(const uint32_t bufferId, const buffer_handle_t memHandle) {
Changyeon Jo2400b692019-07-18 21:32:48 -0700695 if (memHandle == nullptr) {
696 ALOGE("ignoring doneWithFrame called with null handle");
697 } else if (bufferId >= mBuffers.size()) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800698 ALOGE("ignoring doneWithFrame called with invalid bufferId %d (max is %zu)", bufferId,
699 mBuffers.size() - 1);
Changyeon Jo2400b692019-07-18 21:32:48 -0700700 } else if (!mBuffers[bufferId].inUse) {
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800701 ALOGE("ignoring doneWithFrame called on frame %d which is already free", bufferId);
Changyeon Jo2400b692019-07-18 21:32:48 -0700702 } else {
703 // Mark the frame as available
704 mBuffers[bufferId].inUse = false;
705 mFramesInUse--;
706
707 // If this frame's index is high in the array, try to move it down
708 // to improve locality after mFramesAllowed has been reduced.
709 if (bufferId >= mFramesAllowed) {
710 // Find an empty slot lower in the array (which should always exist in this case)
711 for (auto&& rec : mBuffers) {
712 if (rec.handle == nullptr) {
713 rec.handle = mBuffers[bufferId].handle;
714 mBuffers[bufferId].handle = nullptr;
715 break;
716 }
717 }
718 }
719 }
720}
721
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800722sp<EvsCamera> EvsCamera::Create(const char* deviceName) {
723 std::unique_ptr<ConfigManager::CameraInfo> nullCamInfo = nullptr;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700724
725 return Create(deviceName, nullCamInfo);
726}
727
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800728sp<EvsCamera> EvsCamera::Create(const char* deviceName,
729 std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
730 [[maybe_unused]] const Stream* streamCfg) {
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700731 sp<EvsCamera> evsCamera = new EvsCamera(deviceName, camInfo);
732 if (evsCamera == nullptr) {
733 return nullptr;
734 }
735
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800736 // Use the first resolution from the list for the testing
737 // TODO(b/214835237): Uses a given Stream configuration to choose the best
738 // stream configuration.
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700739 auto it = camInfo->streamConfigurations.begin();
740 evsCamera->mWidth = it->second[1];
741 evsCamera->mHeight = it->second[2];
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800742 evsCamera->mDescription.v1.vendorFlags = 0xFFFFFFFF; // Arbitrary test value
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700743
744 evsCamera->mFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800745 evsCamera->mUsage = GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_CAMERA_WRITE |
746 GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_RARELY;
Changyeon Joc6fa0ab2019-10-12 05:25:44 -0700747
748 return evsCamera;
749}
750
Changyeon Jo33ba66b2022-01-16 16:33:52 -0800751} // namespace android::hardware::automotive::evs::V1_1::implementation