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