blob: 82958a27cbec7c2b94bc316ffb33b74e2678eb4f [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,
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 Wang610d7b82022-02-08 14:37:22 -080038 int mirrorMode) :
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,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -080041 consumerUsage, timestampOffset, setId, /*isMultiResolution*/false,
Shuzhen Wang610d7b82022-02-08 14:37:22 -080042 dynamicProfile, streamUseCase, deviceTimeBaseIsRealtime, timestampBase,
43 mirrorMode),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070044 mUseHalBufManager(useHalBufManager) {
Emilian Peev40ead602017-09-26 15:46:36 +010045 size_t consumerCount = std::min(surfaces.size(), kMaxOutputs);
46 if (surfaces.size() > consumerCount) {
47 ALOGE("%s: Trying to add more consumers than the maximum ", __func__);
48 }
49 for (size_t i = 0; i < consumerCount; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070050 mSurfaceUniqueIds[i] = std::make_pair(surfaces[i], mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +010051 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070052}
53
54Camera3SharedOutputStream::~Camera3SharedOutputStream() {
55 disconnectLocked();
56}
57
58status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
59 status_t res = OK;
60
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070061 mStreamSplitter = new Camera3StreamSplitter(mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -070062
Shuzhen Wang92653952019-05-07 15:11:43 -070063 uint64_t usage = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -070064 getEndpointUsage(&usage);
65
Emilian Peev40ead602017-09-26 15:46:36 +010066 std::unordered_map<size_t, sp<Surface>> initialSurfaces;
67 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070068 if (mSurfaceUniqueIds[i].first != nullptr) {
69 initialSurfaces.emplace(i, mSurfaceUniqueIds[i].first);
Emilian Peev40ead602017-09-26 15:46:36 +010070 }
71 }
72
Emilian Peevf4816702020-04-03 15:44:51 -070073 res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera_stream::max_buffers,
Emilian Peev2295df72021-11-12 18:14:10 -080074 getWidth(), getHeight(), getFormat(), &mConsumer, camera_stream::dynamic_range_profile);
Shuzhen Wang0129d522016-10-30 22:43:41 -070075 if (res != OK) {
76 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
77 __FUNCTION__, strerror(-res), res);
78 return res;
79 }
80
81 return res;
82}
83
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -070084status_t Camera3SharedOutputStream::attachBufferToSplitterLocked(
85 ANativeWindowBuffer* anb,
86 const std::vector<size_t>& surface_ids) {
87 status_t res = OK;
88
89 // Attach the buffer to the splitter output queues. This could block if
90 // the output queue doesn't have any empty slot. So unlock during the course
91 // of attachBufferToOutputs.
92 sp<Camera3StreamSplitter> splitter = mStreamSplitter;
93 mLock.unlock();
94 res = splitter->attachBufferToOutputs(anb, surface_ids);
95 mLock.lock();
96 if (res != OK) {
97 ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)",
98 __FUNCTION__, mId, strerror(-res), res);
99 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
100 // let prepareNextBuffer handle the error.)
101 if (res == NO_INIT && mState == STATE_CONFIGURED) {
102 mState = STATE_ABANDONED;
103 }
104 }
105 return res;
106}
107
108
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800109status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700110 Mutex::Autolock l(mLock);
111 status_t res = OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800112 const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700113
114 if (mStreamSplitter != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800115 res = mStreamSplitter->notifyBufferReleased(buffer);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700116 }
117
118 return res;
119}
120
121bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
122 Mutex::Autolock l(mLock);
Emilian Peev40ead602017-09-26 15:46:36 +0100123 if (surface_id >= kMaxOutputs) {
124 return true;
125 }
126
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700127 return (mSurfaceUniqueIds[surface_id].first == nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128}
129
Shuzhen Wang758c2152017-01-10 18:26:18 -0800130status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800131 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800132 if (surfaces.size() == 0) {
133 ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700134 return INVALID_OPERATION;
135 }
136
Shuzhen Wang758c2152017-01-10 18:26:18 -0800137 status_t ret = OK;
138 for (auto& surface : surfaces) {
139 if (surface == nullptr) {
140 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
141 return INVALID_OPERATION;
142 }
143
Emilian Peev40ead602017-09-26 15:46:36 +0100144 ssize_t id = getNextSurfaceIdLocked();
145 if (id < 0) {
146 ALOGE("%s: No surface ids available!", __func__);
147 return NO_MEMORY;
148 }
149
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700150 mSurfaceUniqueIds[id] = std::make_pair(surface, mNextUniqueSurfaceId++);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800151
152 // Only call addOutput if the splitter has been connected.
153 if (mStreamSplitter != nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100154 ret = mStreamSplitter->addOutput(id, surface);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800155 if (ret != OK) {
156 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
157 return ret;
158
159 }
160 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700161 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800162 return ret;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700163}
164
Emilian Peevf4816702020-04-03 15:44:51 -0700165status_t Camera3SharedOutputStream::getBufferLocked(camera_stream_buffer *buffer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700166 const std::vector<size_t>& surfaceIds) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800167 ANativeWindowBuffer* anb;
168 int fenceFd = -1;
169
170 status_t res;
171 res = getBufferLockedCommon(&anb, &fenceFd);
172 if (res != OK) {
173 return res;
174 }
175
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700176 if (!mUseHalBufManager) {
177 res = attachBufferToSplitterLocked(anb, surfaceIds);
178 if (res != OK) {
179 return res;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800180 }
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800181 }
182
183 /**
184 * FenceFD now owned by HAL except in case of error,
185 * in which case we reassign it to acquire_fence
186 */
187 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
Emilian Peevf4816702020-04-03 15:44:51 -0700188 /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800189
190 return OK;
191}
192
193status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700194 ANativeWindowBuffer* buffer, int anwReleaseFence,
195 const std::vector<size_t>& uniqueSurfaceIds) {
196 status_t res = OK;
197 if (mUseHalBufManager) {
198 if (uniqueSurfaceIds.size() == 0) {
199 ALOGE("%s: uniqueSurfaceIds must not be empty!", __FUNCTION__);
200 return BAD_VALUE;
201 }
202 Mutex::Autolock l(mLock);
203 std::vector<size_t> surfaceIds;
204 for (const auto& uniqueId : uniqueSurfaceIds) {
205 bool uniqueIdFound = false;
206 for (size_t i = 0; i < kMaxOutputs; i++) {
207 if (mSurfaceUniqueIds[i].second == uniqueId) {
208 surfaceIds.push_back(i);
209 uniqueIdFound = true;
210 break;
211 }
212 }
213 if (!uniqueIdFound) {
214 ALOGV("%s: unknown unique surface ID %zu for stream %d: "
215 "output might have been removed.",
216 __FUNCTION__, uniqueId, mId);
217 }
218 }
219 res = attachBufferToSplitterLocked(buffer, surfaceIds);
220 if (res != OK) {
221 return res;
222 }
223 }
224
225 res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800226
227 // After queuing buffer to the internal consumer queue, check whether the buffer is
228 // successfully queued to the output queues.
229 if (res == OK) {
230 res = mStreamSplitter->getOnFrameAvailableResult();
231 if (res != OK) {
232 ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res);
233 }
234 } else {
235 ALOGE("%s: queueBufer failed %d", __FUNCTION__, res);
236 }
237
238 return res;
239}
240
Shuzhen Wang0129d522016-10-30 22:43:41 -0700241status_t Camera3SharedOutputStream::configureQueueLocked() {
242 status_t res;
243
244 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
245 return res;
246 }
247
248 res = connectStreamSplitterLocked();
249 if (res != OK) {
250 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
251 return res;
252 }
253
Shuzhen Wangba92d772022-04-11 11:47:24 -0700254 res = configureConsumerQueueLocked(false/*allowPreviewRespace*/);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700255 if (res != OK) {
256 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
257 return res;
258 }
259
260 return OK;
261}
262
263status_t Camera3SharedOutputStream::disconnectLocked() {
264 status_t res;
265 res = Camera3OutputStream::disconnectLocked();
266
267 if (mStreamSplitter != nullptr) {
268 mStreamSplitter->disconnect();
269 }
270
271 return res;
272}
273
Emilian Peev050f5dc2017-05-18 14:43:56 +0100274status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700275
Shuzhen Wang758c2152017-01-10 18:26:18 -0800276 status_t res = OK;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100277 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700278
279 if (mConsumer == nullptr) {
280 // Called before shared buffer queue is constructed.
281 *usage = getPresetConsumerUsage();
282
Emilian Peev40ead602017-09-26 15:46:36 +0100283 for (size_t id = 0; id < kMaxOutputs; id++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700284 if (mSurfaceUniqueIds[id].first != nullptr) {
285 res = getEndpointUsageForSurface(&u, mSurfaceUniqueIds[id].first);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700286 *usage |= u;
287 }
288 }
289 } else {
290 // Called after shared buffer queue is constructed.
291 res = getEndpointUsageForSurface(&u, mConsumer);
292 *usage |= u;
293 }
294
295 return res;
296}
297
Emilian Peev40ead602017-09-26 15:46:36 +0100298ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() {
299 ssize_t id = -1;
300 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700301 if (mSurfaceUniqueIds[i].first == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100302 id = i;
303 break;
304 }
305 }
306
307 return id;
308}
309
310ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) {
311 Mutex::Autolock l(mLock);
312 ssize_t id = -1;
313 for (size_t i = 0; i < kMaxOutputs; i++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700314 if (mSurfaceUniqueIds[i].first == surface) {
Emilian Peev40ead602017-09-26 15:46:36 +0100315 id = i;
316 break;
317 }
318 }
319
320 return id;
321}
322
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700323status_t Camera3SharedOutputStream::getUniqueSurfaceIds(
324 const std::vector<size_t>& surfaceIds,
325 /*out*/std::vector<size_t>* outUniqueIds) {
326 Mutex::Autolock l(mLock);
327 if (outUniqueIds == nullptr || surfaceIds.size() > kMaxOutputs) {
328 return BAD_VALUE;
329 }
330
331 outUniqueIds->clear();
332 outUniqueIds->reserve(surfaceIds.size());
333
334 for (const auto& surfaceId : surfaceIds) {
335 if (surfaceId >= kMaxOutputs) {
336 return BAD_VALUE;
337 }
338 outUniqueIds->push_back(mSurfaceUniqueIds[surfaceId].second);
339 }
340 return OK;
341}
342
Emilian Peev40ead602017-09-26 15:46:36 +0100343status_t Camera3SharedOutputStream::revertPartialUpdateLocked(
344 const KeyedVector<sp<Surface>, size_t> &removedSurfaces,
345 const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) {
346 status_t ret = OK;
347
348 for (size_t i = 0; i < attachedSurfaces.size(); i++) {
349 size_t index = attachedSurfaces.valueAt(i);
350 if (mStreamSplitter != nullptr) {
351 ret = mStreamSplitter->removeOutput(index);
352 if (ret != OK) {
353 return UNKNOWN_ERROR;
354 }
355 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700356 mSurfaceUniqueIds[index] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100357 }
358
359 for (size_t i = 0; i < removedSurfaces.size(); i++) {
360 size_t index = removedSurfaces.valueAt(i);
361 if (mStreamSplitter != nullptr) {
362 ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i));
363 if (ret != OK) {
364 return UNKNOWN_ERROR;
365 }
366 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700367 mSurfaceUniqueIds[index] = std::make_pair(
368 removedSurfaces.keyAt(i), mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100369 }
370
371 return ret;
372}
373
374status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces,
375 const std::vector<OutputStreamInfo> &outputInfo,
376 const std::vector<size_t> &removedSurfaceIds,
377 KeyedVector<sp<Surface>, size_t> *outputMap) {
378 status_t ret = OK;
379 Mutex::Autolock l(mLock);
380
381 if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) ||
382 (outputSurfaces.size() > kMaxOutputs)) {
383 return BAD_VALUE;
384 }
385
386 uint64_t usage;
387 getEndpointUsage(&usage);
388 KeyedVector<sp<Surface>, size_t> removedSurfaces;
389 //Check whether the new surfaces are compatible.
390 for (const auto &infoIt : outputInfo) {
391 bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false;
392 bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) ||
393 (static_cast<uint32_t> (infoIt.height) != getHeight())) ?
394 true : false;
Emilian Peev2295df72021-11-12 18:14:10 -0800395 bool dynamicRangeMismatch = dynamic_range_profile != infoIt.dynamicRangeProfile;
396 if ((imgReaderUsage && sizeMismatch) || dynamicRangeMismatch ||
Emilian Peev40ead602017-09-26 15:46:36 +0100397 (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) ||
398 (infoIt.dataSpace != getDataSpace() &&
399 infoIt.dataSpace != getOriginalDataSpace())) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800400 ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x dynamic range 0x%"
401 PRIx64 " don't match source stream format: 0x%x dataSpace: 0x%x dynamic"
402 " range 0x%" PRIx64 , __FUNCTION__, infoIt.format, infoIt.dataSpace,
403 infoIt.dynamicRangeProfile, getFormat(), getDataSpace(), dynamic_range_profile);
Emilian Peev40ead602017-09-26 15:46:36 +0100404 return BAD_VALUE;
405 }
406 }
407
408 //First remove all absent outputs
409 for (const auto &it : removedSurfaceIds) {
410 if (mStreamSplitter != nullptr) {
411 ret = mStreamSplitter->removeOutput(it);
412 if (ret != OK) {
413 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
414 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
415 if (res != OK) {
416 return res;
417 }
418 return ret;
419
420 }
421 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700422 removedSurfaces.add(mSurfaceUniqueIds[it].first, it);
423 mSurfaceUniqueIds[it] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100424 }
425
426 //Next add the new outputs
427 for (const auto &it : outputSurfaces) {
428 ssize_t surfaceId = getNextSurfaceIdLocked();
429 if (surfaceId < 0) {
430 ALOGE("%s: No more available output slots!", __FUNCTION__);
431 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
432 if (res != OK) {
433 return res;
434 }
435 return NO_MEMORY;
436 }
437 if (mStreamSplitter != nullptr) {
438 ret = mStreamSplitter->addOutput(surfaceId, it);
439 if (ret != OK) {
440 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
441 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
442 if (res != OK) {
443 return res;
444 }
445 return ret;
446 }
447 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -0700448 mSurfaceUniqueIds[surfaceId] = std::make_pair(it, mNextUniqueSurfaceId++);
Emilian Peev40ead602017-09-26 15:46:36 +0100449 outputMap->add(it, surfaceId);
450 }
451
452 return ret;
453}
454
Shuzhen Wang0129d522016-10-30 22:43:41 -0700455} // namespace camera3
456
457} // namespace android