1 /*
2  * Copyright (C) 2016-2018 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 "Camera3-SharedOuStrm"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 
21 #include "Camera3SharedOutputStream.h"
22 
23 namespace android {
24 
25 namespace camera3 {
26 
27 const size_t Camera3SharedOutputStream::kMaxOutputs;
28 
Camera3SharedOutputStream(int id,const std::vector<sp<Surface>> & surfaces,uint32_t width,uint32_t height,int format,uint64_t consumerUsage,android_dataspace dataSpace,camera_stream_rotation_t rotation,nsecs_t timestampOffset,const std::string & physicalCameraId,const std::unordered_set<int32_t> & sensorPixelModesUsed,IPCTransport transport,int setId,bool useHalBufManager,int64_t dynamicProfile,int64_t streamUseCase,bool deviceTimeBaseIsRealtime,int timestampBase,int mirrorMode,int32_t colorSpace,bool useReadoutTimestamp)29 Camera3SharedOutputStream::Camera3SharedOutputStream(int id,
30         const std::vector<sp<Surface>>& surfaces,
31         uint32_t width, uint32_t height, int format,
32         uint64_t consumerUsage, android_dataspace dataSpace,
33         camera_stream_rotation_t rotation,
34         nsecs_t timestampOffset, const std::string& physicalCameraId,
35         const std::unordered_set<int32_t> &sensorPixelModesUsed, IPCTransport transport,
36         int setId, bool useHalBufManager, int64_t dynamicProfile,
37         int64_t streamUseCase, bool deviceTimeBaseIsRealtime, int timestampBase,
38         int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) :
39         Camera3OutputStream(id, CAMERA_STREAM_OUTPUT, width, height,
40                             format, dataSpace, rotation, physicalCameraId, sensorPixelModesUsed,
41                             transport, consumerUsage, timestampOffset, setId,
42                             /*isMultiResolution*/false, dynamicProfile, streamUseCase,
43                             deviceTimeBaseIsRealtime, timestampBase, mirrorMode, colorSpace,
44                             useReadoutTimestamp),
45         mUseHalBufManager(useHalBufManager) {
46     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++) {
51         mSurfaceUniqueIds[i] = std::make_pair(surfaces[i], mNextUniqueSurfaceId++);
52     }
53 }
54 
~Camera3SharedOutputStream()55 Camera3SharedOutputStream::~Camera3SharedOutputStream() {
56     disconnectLocked();
57 }
58 
connectStreamSplitterLocked()59 status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
60     status_t res = OK;
61 
62     mStreamSplitter = new Camera3StreamSplitter(mUseHalBufManager);
63 
64     uint64_t usage = 0;
65     getEndpointUsage(&usage);
66 
67     std::unordered_map<size_t, sp<Surface>> initialSurfaces;
68     for (size_t i = 0; i < kMaxOutputs; i++) {
69         if (mSurfaceUniqueIds[i].first != nullptr) {
70             initialSurfaces.emplace(i, mSurfaceUniqueIds[i].first);
71         }
72     }
73 
74     res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera_stream::max_buffers,
75             getWidth(), getHeight(), getFormat(), &mConsumer, camera_stream::dynamic_range_profile);
76     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 
attachBufferToSplitterLocked(ANativeWindowBuffer * anb,const std::vector<size_t> & surface_ids)85 status_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 
setHalBufferManager(bool enabled)109 void Camera3SharedOutputStream::setHalBufferManager(bool enabled) {
110     Mutex::Autolock l(mLock);
111     mUseHalBufManager = enabled;
112     if (mStreamSplitter != nullptr) {
113         mStreamSplitter->setHalBufferManager(enabled);
114     }
115 }
116 
notifyBufferReleased(ANativeWindowBuffer * anwBuffer)117 status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) {
118     Mutex::Autolock l(mLock);
119     status_t res = OK;
120     const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer));
121 
122     if (mStreamSplitter != nullptr) {
123         res = mStreamSplitter->notifyBufferReleased(buffer);
124     }
125 
126     return res;
127 }
128 
isConsumerConfigurationDeferred(size_t surface_id) const129 bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
130     Mutex::Autolock l(mLock);
131     if (surface_id >= kMaxOutputs) {
132         return true;
133     }
134 
135     return (mSurfaceUniqueIds[surface_id].first == nullptr);
136 }
137 
setConsumers(const std::vector<sp<Surface>> & surfaces)138 status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
139     Mutex::Autolock l(mLock);
140     if (surfaces.size() == 0) {
141         ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
142         return INVALID_OPERATION;
143     }
144 
145     status_t ret = OK;
146     for (auto& surface : surfaces) {
147         if (surface == nullptr) {
148             ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
149             return INVALID_OPERATION;
150         }
151 
152         ssize_t id = getNextSurfaceIdLocked();
153         if (id < 0) {
154             ALOGE("%s: No surface ids available!", __func__);
155             return NO_MEMORY;
156         }
157 
158         mSurfaceUniqueIds[id] = std::make_pair(surface, mNextUniqueSurfaceId++);
159 
160         // Only call addOutput if the splitter has been connected.
161         if (mStreamSplitter != nullptr) {
162             ret = mStreamSplitter->addOutput(id, surface);
163             if (ret != OK) {
164                 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
165                 return ret;
166 
167             }
168         }
169     }
170     return ret;
171 }
172 
getBufferLocked(camera_stream_buffer * buffer,const std::vector<size_t> & surfaceIds)173 status_t Camera3SharedOutputStream::getBufferLocked(camera_stream_buffer *buffer,
174         const std::vector<size_t>& surfaceIds) {
175     ANativeWindowBuffer* anb;
176     int fenceFd = -1;
177 
178     status_t res;
179     res = getBufferLockedCommon(&anb, &fenceFd);
180     if (res != OK) {
181         return res;
182     }
183 
184     if (!mUseHalBufManager) {
185         res = attachBufferToSplitterLocked(anb, surfaceIds);
186         if (res != OK) {
187             return res;
188         }
189     }
190 
191     /**
192      * FenceFD now owned by HAL except in case of error,
193      * in which case we reassign it to acquire_fence
194      */
195     handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
196                         /*releaseFence*/-1, CAMERA_BUFFER_STATUS_OK, /*output*/true);
197 
198     return OK;
199 }
200 
queueBufferToConsumer(sp<ANativeWindow> & consumer,ANativeWindowBuffer * buffer,int anwReleaseFence,const std::vector<size_t> & uniqueSurfaceIds)201 status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
202             ANativeWindowBuffer* buffer, int anwReleaseFence,
203             const std::vector<size_t>& uniqueSurfaceIds) {
204     status_t res = OK;
205     if (mUseHalBufManager) {
206         if (uniqueSurfaceIds.size() == 0) {
207             ALOGE("%s: uniqueSurfaceIds must not be empty!", __FUNCTION__);
208             return BAD_VALUE;
209         }
210         Mutex::Autolock l(mLock);
211         std::vector<size_t> surfaceIds;
212         for (const auto& uniqueId : uniqueSurfaceIds) {
213             bool uniqueIdFound = false;
214             for (size_t i = 0; i < kMaxOutputs; i++) {
215                 if (mSurfaceUniqueIds[i].second == uniqueId) {
216                     surfaceIds.push_back(i);
217                     uniqueIdFound = true;
218                     break;
219                 }
220             }
221             if (!uniqueIdFound) {
222                 ALOGV("%s: unknown unique surface ID %zu for stream %d: "
223                         "output might have been removed.",
224                         __FUNCTION__, uniqueId, mId);
225             }
226         }
227         res = attachBufferToSplitterLocked(buffer, surfaceIds);
228         if (res != OK) {
229             return res;
230         }
231     }
232 
233     res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
234 
235     // After queuing buffer to the internal consumer queue, check whether the buffer is
236     // successfully queued to the output queues.
237     if (res == OK) {
238         res = mStreamSplitter->getOnFrameAvailableResult();
239         if (res != OK) {
240             ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res);
241         }
242     } else {
243         ALOGE("%s: queueBufer failed %d", __FUNCTION__, res);
244     }
245 
246     return res;
247 }
248 
configureQueueLocked()249 status_t Camera3SharedOutputStream::configureQueueLocked() {
250     status_t res;
251 
252     if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
253         return res;
254     }
255 
256     res = connectStreamSplitterLocked();
257     if (res != OK) {
258         ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
259         return res;
260     }
261 
262     res = configureConsumerQueueLocked(false/*allowPreviewRespace*/);
263     if (res != OK) {
264         ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
265         return res;
266     }
267 
268     return OK;
269 }
270 
disconnectLocked()271 status_t Camera3SharedOutputStream::disconnectLocked() {
272     status_t res;
273     res = Camera3OutputStream::disconnectLocked();
274 
275     if (mStreamSplitter != nullptr) {
276         mStreamSplitter->disconnect();
277     }
278 
279     return res;
280 }
281 
getEndpointUsage(uint64_t * usage)282 status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) {
283 
284     status_t res = OK;
285     uint64_t u = 0;
286 
287     if (mConsumer == nullptr) {
288         // Called before shared buffer queue is constructed.
289         *usage = getPresetConsumerUsage();
290 
291         for (size_t id = 0; id < kMaxOutputs; id++) {
292             if (mSurfaceUniqueIds[id].first != nullptr) {
293                 res = getEndpointUsageForSurface(&u, mSurfaceUniqueIds[id].first);
294                 *usage |= u;
295             }
296         }
297     } else {
298         // Called after shared buffer queue is constructed.
299         res = getEndpointUsageForSurface(&u, mConsumer);
300         *usage |= u;
301     }
302 
303     return res;
304 }
305 
getNextSurfaceIdLocked()306 ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() {
307     ssize_t id = -1;
308     for (size_t i = 0; i < kMaxOutputs; i++) {
309         if (mSurfaceUniqueIds[i].first == nullptr) {
310             id = i;
311             break;
312         }
313     }
314 
315     return id;
316 }
317 
getSurfaceId(const sp<Surface> & surface)318 ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) {
319     Mutex::Autolock l(mLock);
320     ssize_t id = -1;
321     for (size_t i = 0; i < kMaxOutputs; i++) {
322         if (mSurfaceUniqueIds[i].first == surface) {
323             id = i;
324             break;
325         }
326     }
327 
328     return id;
329 }
330 
getUniqueSurfaceIds(const std::vector<size_t> & surfaceIds,std::vector<size_t> * outUniqueIds)331 status_t Camera3SharedOutputStream::getUniqueSurfaceIds(
332         const std::vector<size_t>& surfaceIds,
333         /*out*/std::vector<size_t>* outUniqueIds) {
334     Mutex::Autolock l(mLock);
335     if (outUniqueIds == nullptr || surfaceIds.size() > kMaxOutputs) {
336         return BAD_VALUE;
337     }
338 
339     outUniqueIds->clear();
340     outUniqueIds->reserve(surfaceIds.size());
341 
342     for (const auto& surfaceId : surfaceIds) {
343         if (surfaceId >= kMaxOutputs) {
344             return BAD_VALUE;
345         }
346         outUniqueIds->push_back(mSurfaceUniqueIds[surfaceId].second);
347     }
348     return OK;
349 }
350 
revertPartialUpdateLocked(const KeyedVector<sp<Surface>,size_t> & removedSurfaces,const KeyedVector<sp<Surface>,size_t> & attachedSurfaces)351 status_t Camera3SharedOutputStream::revertPartialUpdateLocked(
352         const KeyedVector<sp<Surface>, size_t> &removedSurfaces,
353         const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) {
354     status_t ret = OK;
355 
356     for (size_t i = 0; i < attachedSurfaces.size(); i++) {
357         size_t index = attachedSurfaces.valueAt(i);
358         if (mStreamSplitter != nullptr) {
359             ret = mStreamSplitter->removeOutput(index);
360             if (ret != OK) {
361                 return UNKNOWN_ERROR;
362             }
363         }
364         mSurfaceUniqueIds[index] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
365     }
366 
367     for (size_t i = 0; i < removedSurfaces.size(); i++) {
368         size_t index = removedSurfaces.valueAt(i);
369         if (mStreamSplitter != nullptr) {
370             ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i));
371             if (ret != OK) {
372                 return UNKNOWN_ERROR;
373             }
374         }
375         mSurfaceUniqueIds[index] = std::make_pair(
376                 removedSurfaces.keyAt(i), mNextUniqueSurfaceId++);
377     }
378 
379     return ret;
380 }
381 
updateStream(const std::vector<sp<Surface>> & outputSurfaces,const std::vector<OutputStreamInfo> & outputInfo,const std::vector<size_t> & removedSurfaceIds,KeyedVector<sp<Surface>,size_t> * outputMap)382 status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces,
383         const std::vector<OutputStreamInfo> &outputInfo,
384         const std::vector<size_t> &removedSurfaceIds,
385         KeyedVector<sp<Surface>, size_t> *outputMap) {
386     status_t ret = OK;
387     Mutex::Autolock l(mLock);
388 
389     if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) ||
390             (outputSurfaces.size() > kMaxOutputs)) {
391         return BAD_VALUE;
392     }
393 
394     uint64_t usage;
395     getEndpointUsage(&usage);
396     KeyedVector<sp<Surface>, size_t> removedSurfaces;
397     //Check whether the new surfaces are compatible.
398     for (const auto &infoIt : outputInfo) {
399         bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false;
400         bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) ||
401                                 (static_cast<uint32_t> (infoIt.height) != getHeight())) ?
402                                 true : false;
403         bool dynamicRangeMismatch = dynamic_range_profile != infoIt.dynamicRangeProfile;
404         if ((imgReaderUsage && sizeMismatch) || dynamicRangeMismatch ||
405                 (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) ||
406                 (infoIt.dataSpace != getDataSpace() &&
407                  infoIt.dataSpace != getOriginalDataSpace())) {
408             ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x dynamic range 0x%"
409                     PRIx64 " don't match source stream format: 0x%x  dataSpace: 0x%x dynamic"
410                     " range 0x%" PRIx64 , __FUNCTION__, infoIt.format, infoIt.dataSpace,
411                     infoIt.dynamicRangeProfile, getFormat(), getDataSpace(), dynamic_range_profile);
412             return BAD_VALUE;
413         }
414     }
415 
416     //First remove all absent outputs
417     for (const auto &it : removedSurfaceIds) {
418         if (mStreamSplitter != nullptr) {
419             ret = mStreamSplitter->removeOutput(it);
420             if (ret != OK) {
421                 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
422                 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
423                 if (res != OK) {
424                     return res;
425                 }
426                 return ret;
427 
428             }
429         }
430         removedSurfaces.add(mSurfaceUniqueIds[it].first, it);
431         mSurfaceUniqueIds[it] = std::make_pair(nullptr, mNextUniqueSurfaceId++);
432     }
433 
434     //Next add the new outputs
435     for (const auto &it : outputSurfaces) {
436         ssize_t surfaceId = getNextSurfaceIdLocked();
437         if (surfaceId < 0) {
438             ALOGE("%s: No more available output slots!", __FUNCTION__);
439             status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
440             if (res != OK) {
441                 return res;
442             }
443             return NO_MEMORY;
444         }
445         if (mStreamSplitter != nullptr) {
446             ret = mStreamSplitter->addOutput(surfaceId, it);
447             if (ret != OK) {
448                 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
449                 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
450                 if (res != OK) {
451                     return res;
452                 }
453                 return ret;
454             }
455         }
456         mSurfaceUniqueIds[surfaceId] = std::make_pair(it, mNextUniqueSurfaceId++);
457         outputMap->add(it, surfaceId);
458     }
459 
460     return ret;
461 }
462 
463 } // namespace camera3
464 
465 } // namespace android
466