blob: 1191f05140917404ccced977b32c77a3492642be [file] [log] [blame]
Shuzhen Wang0129d522016-10-30 22:43:41 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2016-2018 The Android Open Source Project
Shuzhen Wang0129d522016-10-30 22:43:41 -07003 *
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
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070017#define LOG_TAG "Camera3-SharedOuStrm"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20
Shuzhen Wang0129d522016-10-30 22:43:41 -070021#include "Camera3SharedOutputStream.h"
22
23namespace android {
24
25namespace camera3 {
26
Emilian Peev40ead602017-09-26 15:46:36 +010027const size_t Camera3SharedOutputStream::kMaxOutputs;
28
Shuzhen Wang0129d522016-10-30 22:43:41 -070029Camera3SharedOutputStream::Camera3SharedOutputStream(int id,
30 const std::vector<sp<Surface>>& surfaces,
Shuzhen Wang0129d522016-10-30 22:43:41 -070031 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +010032 uint64_t consumerUsage, android_dataspace dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -070033 camera_stream_rotation_t rotation,
Austin Borger1c1bee02023-06-01 16:51:35 -070034 nsecs_t timestampOffset, const std::string& physicalCameraId,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +000035 const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport,
Emilian Peevc81a7592022-02-14 17:38:18 -080036 int setId, bool useHalBufManager, int64_t dynamicProfile,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -080037 int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000038 int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) :
Emilian Peevf4816702020-04-03 15:44:51 -070039 Camera3OutputStream(id, CAMERA_STREAM_OUTPUT, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080040 format, dataSpace, rotation, physicalCameraId, sensorPixelModesUsed,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +000041 transport, consumerUsage, timestampOffset, setId,
42 /*isMultiResolution*/false, dynamicProfile, streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000043 deviceTimeBaseIsRealtime, timestampBase, mirrorMode, colorSpace,
44 useReadoutTimestamp),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070045 mUseHalBufManager(useHalBufManager) {
Emilian Peev40ead602017-09-26 15:46:36 +010046 size_t consumerCount = std::min(surfaces.size(), kMaxOutputs);
47 if (surfaces.size() > consumerCount) {
48 ALOGE("%s: Trying to add more consumers than the maximum ", __func__);
49 }
50 for (size_t i = 0; i < consumerCount; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070051 mSurfaceUniqueIds[i] = std::make_pair(surfaces[i], mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +010052 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070053}
54
55Camera3SharedOutputStream::~Camera3SharedOutputStream() {
56 disconnectLocked();
57}
58
59status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
60 status_t res = OK;
61
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070062 mStreamSplitter = new Camera3StreamSplitter(mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -070063
Shuzhen Wang92653952019-05-07 15:11:43 -070064 uint64_t usage = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -070065 getEndpointUsage(&usage);
66
Emilian Peev40ead602017-09-26 15:46:36 +010067 std::unordered_map<size_t, sp<Surface>> initialSurfaces;
68 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070069 if (mSurfaceUniqueIds[i].first != nullptr) {
70 initialSurfaces.emplace(i, mSurfaceUniqueIds[i].first);
Emilian Peev40ead602017-09-26 15:46:36 +010071 }
72 }
73
Emilian Peevf4816702020-04-03 15:44:51 -070074 res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera_stream::max_buffers,
Emilian Peev2295df72021-11-12 18:14:10 -080075 getWidth(), getHeight(), getFormat(), &mConsumer, camera_stream::dynamic_range_profile);
Shuzhen Wang0129d522016-10-30 22:43:41 -070076 if (res != OK) {
77 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
78 __FUNCTION__, strerror(-res), res);
79 return res;
80 }
81
82 return res;
83}
84
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070085status_t Camera3SharedOutputStream::attachBufferToSplitterLocked(
86 ANativeWindowBuffer* anb,
87 const std::vector<size_t>& surface_ids) {
88 status_t res = OK;
89
90 // Attach the buffer to the splitter output queues. This could block if
91 // the output queue doesn't have any empty slot. So unlock during the course
92 // of attachBufferToOutputs.
93 sp<Camera3StreamSplitter> splitter = mStreamSplitter;
94 mLock.unlock();
95 res = splitter->attachBufferToOutputs(anb, surface_ids);
96 mLock.lock();
97 if (res != OK) {
98 ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)",
99 __FUNCTION__, mId, strerror(-res), res);
100 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
101 // let prepareNextBuffer handle the error.)
102 if (res == NO_INIT && mState == STATE_CONFIGURED) {
103 mState = STATE_ABANDONED;
104 }
105 }
106 return res;
107}
108
109
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800110status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700111 Mutex::Autolock l(mLock);
112 status_t res = OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800113 const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700114
115 if (mStreamSplitter != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800116 res = mStreamSplitter->notifyBufferReleased(buffer);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700117 }
118
119 return res;
120}
121
122bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
123 Mutex::Autolock l(mLock);
Emilian Peev40ead602017-09-26 15:46:36 +0100124 if (surface_id >= kMaxOutputs) {
125 return true;
126 }
127
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700128 return (mSurfaceUniqueIds[surface_id].first == nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700129}
130
Shuzhen Wang758c2152017-01-10 18:26:18 -0800131status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800132 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800133 if (surfaces.size() == 0) {
134 ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700135 return INVALID_OPERATION;
136 }
137
Shuzhen Wang758c2152017-01-10 18:26:18 -0800138 status_t ret = OK;
139 for (auto& surface : surfaces) {
140 if (surface == nullptr) {
141 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
142 return INVALID_OPERATION;
143 }
144
Emilian Peev40ead602017-09-26 15:46:36 +0100145 ssize_t id = getNextSurfaceIdLocked();
146 if (id < 0) {
147 ALOGE("%s: No surface ids available!", __func__);
148 return NO_MEMORY;
149 }
150
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700151 mSurfaceUniqueIds[id] = std::make_pair(surface, mNextUniqueSurfaceId++);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800152
153 // Only call addOutput if the splitter has been connected.
154 if (mStreamSplitter != nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100155 ret = mStreamSplitter->addOutput(id, surface);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800156 if (ret != OK) {
157 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
158 return ret;
159
160 }
161 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700162 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800163 return ret;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700164}
165
Emilian Peevf4816702020-04-03 15:44:51 -0700166status_t Camera3SharedOutputStream::getBufferLocked(camera_stream_buffer *buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700167 const std::vector<size_t>& surfaceIds) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800168 ANativeWindowBuffer* anb;
169 int fenceFd = -1;
170
171 status_t res;
172 res = getBufferLockedCommon(&anb, &fenceFd);
173 if (res != OK) {
174 return res;
175 }
176
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700177 if (!mUseHalBufManager) {
178 res = attachBufferToSplitterLocked(anb, surfaceIds);
179 if (res != OK) {
180 return res;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800181 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800182 }
183
184 /**
185 * FenceFD now owned by HAL except in case of error,
186 * in which case we reassign it to acquire_fence
187 */
188 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Emilian Peevf4816702020-04-03 15:44:51 -0700189 /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800190
191 return OK;
192}
193
194status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700195 ANativeWindowBuffer* buffer, int anwReleaseFence,
196 const std::vector<size_t>& uniqueSurfaceIds) {
197 status_t res = OK;
198 if (mUseHalBufManager) {
199 if (uniqueSurfaceIds.size() == 0) {
200 ALOGE("%s: uniqueSurfaceIds must not be empty!", __FUNCTION__);
201 return BAD_VALUE;
202 }
203 Mutex::Autolock l(mLock);
204 std::vector<size_t> surfaceIds;
205 for (const auto& uniqueId : uniqueSurfaceIds) {
206 bool uniqueIdFound = false;
207 for (size_t i = 0; i < kMaxOutputs; i++) {
208 if (mSurfaceUniqueIds[i].second == uniqueId) {
209 surfaceIds.push_back(i);
210 uniqueIdFound = true;
211 break;
212 }
213 }
214 if (!uniqueIdFound) {
215 ALOGV("%s: unknown unique surface ID %zu for stream %d: "
216 "output might have been removed.",
217 __FUNCTION__, uniqueId, mId);
218 }
219 }
220 res = attachBufferToSplitterLocked(buffer, surfaceIds);
221 if (res != OK) {
222 return res;
223 }
224 }
225
226 res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800227
228 // After queuing buffer to the internal consumer queue, check whether the buffer is
229 // successfully queued to the output queues.
230 if (res == OK) {
231 res = mStreamSplitter->getOnFrameAvailableResult();
232 if (res != OK) {
233 ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res);
234 }
235 } else {
236 ALOGE("%s: queueBufer failed %d", __FUNCTION__, res);
237 }
238
239 return res;
240}
241
Shuzhen Wang0129d522016-10-30 22:43:41 -0700242status_t Camera3SharedOutputStream::configureQueueLocked() {
243 status_t res;
244
245 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
246 return res;
247 }
248
249 res = connectStreamSplitterLocked();
250 if (res != OK) {
251 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
252 return res;
253 }
254
Shuzhen Wangba92d772022-04-11 11:47:24 -0700255 res = configureConsumerQueueLocked(false/*allowPreviewRespace*/);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700256 if (res != OK) {
257 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
258 return res;
259 }
260
261 return OK;
262}
263
264status_t Camera3SharedOutputStream::disconnectLocked() {
265 status_t res;
266 res = Camera3OutputStream::disconnectLocked();
267
268 if (mStreamSplitter != nullptr) {
269 mStreamSplitter->disconnect();
270 }
271
272 return res;
273}
274
Emilian Peev050f5dc2017-05-18 14:43:56 +0100275status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700276
Shuzhen Wang758c2152017-01-10 18:26:18 -0800277 status_t res = OK;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100278 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700279
280 if (mConsumer == nullptr) {
281 // Called before shared buffer queue is constructed.
282 *usage = getPresetConsumerUsage();
283
Emilian Peev40ead602017-09-26 15:46:36 +0100284 for (size_t id = 0; id < kMaxOutputs; id++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700285 if (mSurfaceUniqueIds[id].first != nullptr) {
286 res = getEndpointUsageForSurface(&u, mSurfaceUniqueIds[id].first);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700287 *usage |= u;
288 }
289 }
290 } else {
291 // Called after shared buffer queue is constructed.
292 res = getEndpointUsageForSurface(&u, mConsumer);
293 *usage |= u;
294 }
295
296 return res;
297}
298
Emilian Peev40ead602017-09-26 15:46:36 +0100299ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() {
300 ssize_t id = -1;
301 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700302 if (mSurfaceUniqueIds[i].first == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100303 id = i;
304 break;
305 }
306 }
307
308 return id;
309}
310
311ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) {
312 Mutex::Autolock l(mLock);
313 ssize_t id = -1;
314 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700315 if (mSurfaceUniqueIds[i].first == surface) {
Emilian Peev40ead602017-09-26 15:46:36 +0100316 id = i;
317 break;
318 }
319 }
320
321 return id;
322}
323
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700324status_t Camera3SharedOutputStream::getUniqueSurfaceIds(
325 const std::vector<size_t>& surfaceIds,
326 /*out*/std::vector<size_t>* outUniqueIds) {
327 Mutex::Autolock l(mLock);
328 if (outUniqueIds == nullptr || surfaceIds.size() > kMaxOutputs) {
329 return BAD_VALUE;
330 }
331
332 outUniqueIds->clear();
333 outUniqueIds->reserve(surfaceIds.size());
334
335 for (const auto& surfaceId : surfaceIds) {
336 if (surfaceId >= kMaxOutputs) {
337 return BAD_VALUE;
338 }
339 outUniqueIds->push_back(mSurfaceUniqueIds[surfaceId].second);
340 }
341 return OK;
342}
343
Emilian Peev40ead602017-09-26 15:46:36 +0100344status_t Camera3SharedOutputStream::revertPartialUpdateLocked(
345 const KeyedVector<sp<Surface>, size_t> &removedSurfaces,
346 const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) {
347 status_t ret = OK;
348
349 for (size_t i = 0; i < attachedSurfaces.size(); i++) {
350 size_t index = attachedSurfaces.valueAt(i);
351 if (mStreamSplitter != nullptr) {
352 ret = mStreamSplitter->removeOutput(index);
353 if (ret != OK) {
354 return UNKNOWN_ERROR;
355 }
356 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700357 mSurfaceUniqueIds[index] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100358 }
359
360 for (size_t i = 0; i < removedSurfaces.size(); i++) {
361 size_t index = removedSurfaces.valueAt(i);
362 if (mStreamSplitter != nullptr) {
363 ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i));
364 if (ret != OK) {
365 return UNKNOWN_ERROR;
366 }
367 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700368 mSurfaceUniqueIds[index] = std::make_pair(
369 removedSurfaces.keyAt(i), mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100370 }
371
372 return ret;
373}
374
375status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces,
376 const std::vector<OutputStreamInfo> &outputInfo,
377 const std::vector<size_t> &removedSurfaceIds,
378 KeyedVector<sp<Surface>, size_t> *outputMap) {
379 status_t ret = OK;
380 Mutex::Autolock l(mLock);
381
382 if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) ||
383 (outputSurfaces.size() > kMaxOutputs)) {
384 return BAD_VALUE;
385 }
386
387 uint64_t usage;
388 getEndpointUsage(&usage);
389 KeyedVector<sp<Surface>, size_t> removedSurfaces;
390 //Check whether the new surfaces are compatible.
391 for (const auto &infoIt : outputInfo) {
392 bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false;
393 bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) ||
394 (static_cast<uint32_t> (infoIt.height) != getHeight())) ?
395 true : false;
Emilian Peev2295df72021-11-12 18:14:10 -0800396 bool dynamicRangeMismatch = dynamic_range_profile != infoIt.dynamicRangeProfile;
397 if ((imgReaderUsage && sizeMismatch) || dynamicRangeMismatch ||
Emilian Peev40ead602017-09-26 15:46:36 +0100398 (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) ||
399 (infoIt.dataSpace != getDataSpace() &&
400 infoIt.dataSpace != getOriginalDataSpace())) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800401 ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x dynamic range 0x%"
402 PRIx64 " don't match source stream format: 0x%x dataSpace: 0x%x dynamic"
403 " range 0x%" PRIx64 , __FUNCTION__, infoIt.format, infoIt.dataSpace,
404 infoIt.dynamicRangeProfile, getFormat(), getDataSpace(), dynamic_range_profile);
Emilian Peev40ead602017-09-26 15:46:36 +0100405 return BAD_VALUE;
406 }
407 }
408
409 //First remove all absent outputs
410 for (const auto &it : removedSurfaceIds) {
411 if (mStreamSplitter != nullptr) {
412 ret = mStreamSplitter->removeOutput(it);
413 if (ret != OK) {
414 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
415 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
416 if (res != OK) {
417 return res;
418 }
419 return ret;
420
421 }
422 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700423 removedSurfaces.add(mSurfaceUniqueIds[it].first, it);
424 mSurfaceUniqueIds[it] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100425 }
426
427 //Next add the new outputs
428 for (const auto &it : outputSurfaces) {
429 ssize_t surfaceId = getNextSurfaceIdLocked();
430 if (surfaceId < 0) {
431 ALOGE("%s: No more available output slots!", __FUNCTION__);
432 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
433 if (res != OK) {
434 return res;
435 }
436 return NO_MEMORY;
437 }
438 if (mStreamSplitter != nullptr) {
439 ret = mStreamSplitter->addOutput(surfaceId, it);
440 if (ret != OK) {
441 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
442 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
443 if (res != OK) {
444 return res;
445 }
446 return ret;
447 }
448 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700449 mSurfaceUniqueIds[surfaceId] = std::make_pair(it, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100450 outputMap->add(it, surfaceId);
451 }
452
453 return ret;
454}
455
Shuzhen Wang0129d522016-10-30 22:43:41 -0700456} // namespace camera3
457
458} // namespace android