blob: 047897e74b550d2e4c89631fadc1585cf7ec95f3 [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,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080034 nsecs_t timestampOffset, const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080035 const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -080036 int setId, bool useHalBufManager, int dynamicProfile,
Shuzhen Wange4208922022-02-01 16:52:48 -080037 int streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase) :
Emilian Peevf4816702020-04-03 15:44:51 -070038 Camera3OutputStream(id, CAMERA_STREAM_OUTPUT, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080039 format, dataSpace, rotation, physicalCameraId, sensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -080040 consumerUsage, timestampOffset, setId, /*isMultiResolution*/false,
Shuzhen Wange4208922022-02-01 16:52:48 -080041 dynamicProfile, streamUseCase, deviceTimeBaseIsRealtime, timestampBase),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070042 mUseHalBufManager(useHalBufManager) {
Emilian Peev40ead602017-09-26 15:46:36 +010043 size_t consumerCount = std::min(surfaces.size(), kMaxOutputs);
44 if (surfaces.size() > consumerCount) {
45 ALOGE("%s: Trying to add more consumers than the maximum ", __func__);
46 }
47 for (size_t i = 0; i < consumerCount; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070048 mSurfaceUniqueIds[i] = std::make_pair(surfaces[i], mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +010049 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070050}
51
52Camera3SharedOutputStream::~Camera3SharedOutputStream() {
53 disconnectLocked();
54}
55
56status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
57 status_t res = OK;
58
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070059 mStreamSplitter = new Camera3StreamSplitter(mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -070060
Shuzhen Wang92653952019-05-07 15:11:43 -070061 uint64_t usage = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -070062 getEndpointUsage(&usage);
63
Emilian Peev40ead602017-09-26 15:46:36 +010064 std::unordered_map<size_t, sp<Surface>> initialSurfaces;
65 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070066 if (mSurfaceUniqueIds[i].first != nullptr) {
67 initialSurfaces.emplace(i, mSurfaceUniqueIds[i].first);
Emilian Peev40ead602017-09-26 15:46:36 +010068 }
69 }
70
Emilian Peevf4816702020-04-03 15:44:51 -070071 res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera_stream::max_buffers,
Emilian Peev2295df72021-11-12 18:14:10 -080072 getWidth(), getHeight(), getFormat(), &mConsumer, camera_stream::dynamic_range_profile);
Shuzhen Wang0129d522016-10-30 22:43:41 -070073 if (res != OK) {
74 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
75 __FUNCTION__, strerror(-res), res);
76 return res;
77 }
78
79 return res;
80}
81
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070082status_t Camera3SharedOutputStream::attachBufferToSplitterLocked(
83 ANativeWindowBuffer* anb,
84 const std::vector<size_t>& surface_ids) {
85 status_t res = OK;
86
87 // Attach the buffer to the splitter output queues. This could block if
88 // the output queue doesn't have any empty slot. So unlock during the course
89 // of attachBufferToOutputs.
90 sp<Camera3StreamSplitter> splitter = mStreamSplitter;
91 mLock.unlock();
92 res = splitter->attachBufferToOutputs(anb, surface_ids);
93 mLock.lock();
94 if (res != OK) {
95 ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)",
96 __FUNCTION__, mId, strerror(-res), res);
97 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
98 // let prepareNextBuffer handle the error.)
99 if (res == NO_INIT && mState == STATE_CONFIGURED) {
100 mState = STATE_ABANDONED;
101 }
102 }
103 return res;
104}
105
106
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800107status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700108 Mutex::Autolock l(mLock);
109 status_t res = OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800110 const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700111
112 if (mStreamSplitter != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800113 res = mStreamSplitter->notifyBufferReleased(buffer);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700114 }
115
116 return res;
117}
118
119bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
120 Mutex::Autolock l(mLock);
Emilian Peev40ead602017-09-26 15:46:36 +0100121 if (surface_id >= kMaxOutputs) {
122 return true;
123 }
124
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700125 return (mSurfaceUniqueIds[surface_id].first == nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700126}
127
Shuzhen Wang758c2152017-01-10 18:26:18 -0800128status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800129 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800130 if (surfaces.size() == 0) {
131 ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700132 return INVALID_OPERATION;
133 }
134
Shuzhen Wang758c2152017-01-10 18:26:18 -0800135 status_t ret = OK;
136 for (auto& surface : surfaces) {
137 if (surface == nullptr) {
138 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
139 return INVALID_OPERATION;
140 }
141
Emilian Peev40ead602017-09-26 15:46:36 +0100142 ssize_t id = getNextSurfaceIdLocked();
143 if (id < 0) {
144 ALOGE("%s: No surface ids available!", __func__);
145 return NO_MEMORY;
146 }
147
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700148 mSurfaceUniqueIds[id] = std::make_pair(surface, mNextUniqueSurfaceId++);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800149
150 // Only call addOutput if the splitter has been connected.
151 if (mStreamSplitter != nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100152 ret = mStreamSplitter->addOutput(id, surface);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800153 if (ret != OK) {
154 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
155 return ret;
156
157 }
158 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700159 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800160 return ret;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700161}
162
Emilian Peevf4816702020-04-03 15:44:51 -0700163status_t Camera3SharedOutputStream::getBufferLocked(camera_stream_buffer *buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700164 const std::vector<size_t>& surfaceIds) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800165 ANativeWindowBuffer* anb;
166 int fenceFd = -1;
167
168 status_t res;
169 res = getBufferLockedCommon(&anb, &fenceFd);
170 if (res != OK) {
171 return res;
172 }
173
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700174 if (!mUseHalBufManager) {
175 res = attachBufferToSplitterLocked(anb, surfaceIds);
176 if (res != OK) {
177 return res;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800178 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800179 }
180
181 /**
182 * FenceFD now owned by HAL except in case of error,
183 * in which case we reassign it to acquire_fence
184 */
185 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Emilian Peevf4816702020-04-03 15:44:51 -0700186 /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800187
188 return OK;
189}
190
191status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700192 ANativeWindowBuffer* buffer, int anwReleaseFence,
193 const std::vector<size_t>& uniqueSurfaceIds) {
194 status_t res = OK;
195 if (mUseHalBufManager) {
196 if (uniqueSurfaceIds.size() == 0) {
197 ALOGE("%s: uniqueSurfaceIds must not be empty!", __FUNCTION__);
198 return BAD_VALUE;
199 }
200 Mutex::Autolock l(mLock);
201 std::vector<size_t> surfaceIds;
202 for (const auto& uniqueId : uniqueSurfaceIds) {
203 bool uniqueIdFound = false;
204 for (size_t i = 0; i < kMaxOutputs; i++) {
205 if (mSurfaceUniqueIds[i].second == uniqueId) {
206 surfaceIds.push_back(i);
207 uniqueIdFound = true;
208 break;
209 }
210 }
211 if (!uniqueIdFound) {
212 ALOGV("%s: unknown unique surface ID %zu for stream %d: "
213 "output might have been removed.",
214 __FUNCTION__, uniqueId, mId);
215 }
216 }
217 res = attachBufferToSplitterLocked(buffer, surfaceIds);
218 if (res != OK) {
219 return res;
220 }
221 }
222
223 res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800224
225 // After queuing buffer to the internal consumer queue, check whether the buffer is
226 // successfully queued to the output queues.
227 if (res == OK) {
228 res = mStreamSplitter->getOnFrameAvailableResult();
229 if (res != OK) {
230 ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res);
231 }
232 } else {
233 ALOGE("%s: queueBufer failed %d", __FUNCTION__, res);
234 }
235
236 return res;
237}
238
Shuzhen Wang0129d522016-10-30 22:43:41 -0700239status_t Camera3SharedOutputStream::configureQueueLocked() {
240 status_t res;
241
242 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
243 return res;
244 }
245
246 res = connectStreamSplitterLocked();
247 if (res != OK) {
248 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
249 return res;
250 }
251
Shuzhen Wange4adddb2021-09-21 15:24:44 -0700252 res = configureConsumerQueueLocked(false/*allowPreviewScheduler*/);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700253 if (res != OK) {
254 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
255 return res;
256 }
257
258 return OK;
259}
260
261status_t Camera3SharedOutputStream::disconnectLocked() {
262 status_t res;
263 res = Camera3OutputStream::disconnectLocked();
264
265 if (mStreamSplitter != nullptr) {
266 mStreamSplitter->disconnect();
267 }
268
269 return res;
270}
271
Emilian Peev050f5dc2017-05-18 14:43:56 +0100272status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700273
Shuzhen Wang758c2152017-01-10 18:26:18 -0800274 status_t res = OK;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100275 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700276
277 if (mConsumer == nullptr) {
278 // Called before shared buffer queue is constructed.
279 *usage = getPresetConsumerUsage();
280
Emilian Peev40ead602017-09-26 15:46:36 +0100281 for (size_t id = 0; id < kMaxOutputs; id++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700282 if (mSurfaceUniqueIds[id].first != nullptr) {
283 res = getEndpointUsageForSurface(&u, mSurfaceUniqueIds[id].first);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700284 *usage |= u;
285 }
286 }
287 } else {
288 // Called after shared buffer queue is constructed.
289 res = getEndpointUsageForSurface(&u, mConsumer);
290 *usage |= u;
291 }
292
293 return res;
294}
295
Emilian Peev40ead602017-09-26 15:46:36 +0100296ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() {
297 ssize_t id = -1;
298 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700299 if (mSurfaceUniqueIds[i].first == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100300 id = i;
301 break;
302 }
303 }
304
305 return id;
306}
307
308ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) {
309 Mutex::Autolock l(mLock);
310 ssize_t id = -1;
311 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700312 if (mSurfaceUniqueIds[i].first == surface) {
Emilian Peev40ead602017-09-26 15:46:36 +0100313 id = i;
314 break;
315 }
316 }
317
318 return id;
319}
320
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700321status_t Camera3SharedOutputStream::getUniqueSurfaceIds(
322 const std::vector<size_t>& surfaceIds,
323 /*out*/std::vector<size_t>* outUniqueIds) {
324 Mutex::Autolock l(mLock);
325 if (outUniqueIds == nullptr || surfaceIds.size() > kMaxOutputs) {
326 return BAD_VALUE;
327 }
328
329 outUniqueIds->clear();
330 outUniqueIds->reserve(surfaceIds.size());
331
332 for (const auto& surfaceId : surfaceIds) {
333 if (surfaceId >= kMaxOutputs) {
334 return BAD_VALUE;
335 }
336 outUniqueIds->push_back(mSurfaceUniqueIds[surfaceId].second);
337 }
338 return OK;
339}
340
Emilian Peev40ead602017-09-26 15:46:36 +0100341status_t Camera3SharedOutputStream::revertPartialUpdateLocked(
342 const KeyedVector<sp<Surface>, size_t> &removedSurfaces,
343 const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) {
344 status_t ret = OK;
345
346 for (size_t i = 0; i < attachedSurfaces.size(); i++) {
347 size_t index = attachedSurfaces.valueAt(i);
348 if (mStreamSplitter != nullptr) {
349 ret = mStreamSplitter->removeOutput(index);
350 if (ret != OK) {
351 return UNKNOWN_ERROR;
352 }
353 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700354 mSurfaceUniqueIds[index] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100355 }
356
357 for (size_t i = 0; i < removedSurfaces.size(); i++) {
358 size_t index = removedSurfaces.valueAt(i);
359 if (mStreamSplitter != nullptr) {
360 ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i));
361 if (ret != OK) {
362 return UNKNOWN_ERROR;
363 }
364 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700365 mSurfaceUniqueIds[index] = std::make_pair(
366 removedSurfaces.keyAt(i), mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100367 }
368
369 return ret;
370}
371
372status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces,
373 const std::vector<OutputStreamInfo> &outputInfo,
374 const std::vector<size_t> &removedSurfaceIds,
375 KeyedVector<sp<Surface>, size_t> *outputMap) {
376 status_t ret = OK;
377 Mutex::Autolock l(mLock);
378
379 if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) ||
380 (outputSurfaces.size() > kMaxOutputs)) {
381 return BAD_VALUE;
382 }
383
384 uint64_t usage;
385 getEndpointUsage(&usage);
386 KeyedVector<sp<Surface>, size_t> removedSurfaces;
387 //Check whether the new surfaces are compatible.
388 for (const auto &infoIt : outputInfo) {
389 bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false;
390 bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) ||
391 (static_cast<uint32_t> (infoIt.height) != getHeight())) ?
392 true : false;
Emilian Peev2295df72021-11-12 18:14:10 -0800393 bool dynamicRangeMismatch = dynamic_range_profile != infoIt.dynamicRangeProfile;
394 if ((imgReaderUsage && sizeMismatch) || dynamicRangeMismatch ||
Emilian Peev40ead602017-09-26 15:46:36 +0100395 (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) ||
396 (infoIt.dataSpace != getDataSpace() &&
397 infoIt.dataSpace != getOriginalDataSpace())) {
Emilian Peev2295df72021-11-12 18:14:10 -0800398 ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x dynamic range 0x%x "
399 " don't match source stream format: 0x%x dataSpace: 0x%x dynamic range 0x%x"
400 , __FUNCTION__, infoIt.format, infoIt.dataSpace, infoIt.dynamicRangeProfile,
401 getFormat(), getDataSpace(), dynamic_range_profile);
Emilian Peev40ead602017-09-26 15:46:36 +0100402 return BAD_VALUE;
403 }
404 }
405
406 //First remove all absent outputs
407 for (const auto &it : removedSurfaceIds) {
408 if (mStreamSplitter != nullptr) {
409 ret = mStreamSplitter->removeOutput(it);
410 if (ret != OK) {
411 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
412 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
413 if (res != OK) {
414 return res;
415 }
416 return ret;
417
418 }
419 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700420 removedSurfaces.add(mSurfaceUniqueIds[it].first, it);
421 mSurfaceUniqueIds[it] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100422 }
423
424 //Next add the new outputs
425 for (const auto &it : outputSurfaces) {
426 ssize_t surfaceId = getNextSurfaceIdLocked();
427 if (surfaceId < 0) {
428 ALOGE("%s: No more available output slots!", __FUNCTION__);
429 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
430 if (res != OK) {
431 return res;
432 }
433 return NO_MEMORY;
434 }
435 if (mStreamSplitter != nullptr) {
436 ret = mStreamSplitter->addOutput(surfaceId, it);
437 if (ret != OK) {
438 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
439 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
440 if (res != OK) {
441 return res;
442 }
443 return ret;
444 }
445 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700446 mSurfaceUniqueIds[surfaceId] = std::make_pair(it, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100447 outputMap->add(it, surfaceId);
448 }
449
450 return ret;
451}
452
Shuzhen Wang0129d522016-10-30 22:43:41 -0700453} // namespace camera3
454
455} // namespace android