blob: fb3ce4c619b565bd05b3736f480c6f5d0d2bdcc9 [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
17#include "Camera3SharedOutputStream.h"
18
19namespace android {
20
21namespace camera3 {
22
Emilian Peev40ead602017-09-26 15:46:36 +010023const size_t Camera3SharedOutputStream::kMaxOutputs;
24
Shuzhen Wang0129d522016-10-30 22:43:41 -070025Camera3SharedOutputStream::Camera3SharedOutputStream(int id,
26 const std::vector<sp<Surface>>& surfaces,
Shuzhen Wang0129d522016-10-30 22:43:41 -070027 uint32_t width, uint32_t height, int format,
Emilian Peev050f5dc2017-05-18 14:43:56 +010028 uint64_t consumerUsage, android_dataspace dataSpace,
Shuzhen Wang0129d522016-10-30 22:43:41 -070029 camera3_stream_rotation_t rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080030 nsecs_t timestampOffset, const String8& physicalCameraId,
31 int setId) :
Shuzhen Wang0129d522016-10-30 22:43:41 -070032 Camera3OutputStream(id, CAMERA3_STREAM_OUTPUT, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -080033 format, dataSpace, rotation, physicalCameraId,
34 consumerUsage, timestampOffset, setId) {
Emilian Peev40ead602017-09-26 15:46:36 +010035 size_t consumerCount = std::min(surfaces.size(), kMaxOutputs);
36 if (surfaces.size() > consumerCount) {
37 ALOGE("%s: Trying to add more consumers than the maximum ", __func__);
38 }
39 for (size_t i = 0; i < consumerCount; i++) {
40 mSurfaces[i] = surfaces[i];
41 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070042}
43
44Camera3SharedOutputStream::~Camera3SharedOutputStream() {
45 disconnectLocked();
46}
47
48status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
49 status_t res = OK;
50
51 mStreamSplitter = new Camera3StreamSplitter();
52
Emilian Peev050f5dc2017-05-18 14:43:56 +010053 uint64_t usage;
Shuzhen Wang0129d522016-10-30 22:43:41 -070054 getEndpointUsage(&usage);
55
Emilian Peev40ead602017-09-26 15:46:36 +010056 std::unordered_map<size_t, sp<Surface>> initialSurfaces;
57 for (size_t i = 0; i < kMaxOutputs; i++) {
58 if (mSurfaces[i] != nullptr) {
59 initialSurfaces.emplace(i, mSurfaces[i]);
60 }
61 }
62
Emilian Peev40ead602017-09-26 15:46:36 +010063 res = mStreamSplitter->connect(initialSurfaces, usage, mUsage, camera3_stream::max_buffers,
Shuzhen Wang2f074ce2018-08-27 11:39:53 -070064 getWidth(), getHeight(), getFormat(), &mConsumer);
Shuzhen Wang0129d522016-10-30 22:43:41 -070065 if (res != OK) {
66 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
67 __FUNCTION__, strerror(-res), res);
68 return res;
69 }
70
71 return res;
72}
73
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080074status_t Camera3SharedOutputStream::notifyBufferReleased(ANativeWindowBuffer *anwBuffer) {
Shuzhen Wang0129d522016-10-30 22:43:41 -070075 Mutex::Autolock l(mLock);
76 status_t res = OK;
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080077 const sp<GraphicBuffer> buffer(static_cast<GraphicBuffer*>(anwBuffer));
Shuzhen Wang0129d522016-10-30 22:43:41 -070078
79 if (mStreamSplitter != nullptr) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080080 res = mStreamSplitter->notifyBufferReleased(buffer);
Shuzhen Wang0129d522016-10-30 22:43:41 -070081 }
82
83 return res;
84}
85
86bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
87 Mutex::Autolock l(mLock);
Emilian Peev40ead602017-09-26 15:46:36 +010088 if (surface_id >= kMaxOutputs) {
89 return true;
90 }
91
92 return (mSurfaces[surface_id] == nullptr);
Shuzhen Wang0129d522016-10-30 22:43:41 -070093}
94
Shuzhen Wang758c2152017-01-10 18:26:18 -080095status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -080096 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -080097 if (surfaces.size() == 0) {
98 ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070099 return INVALID_OPERATION;
100 }
101
Shuzhen Wang758c2152017-01-10 18:26:18 -0800102 status_t ret = OK;
103 for (auto& surface : surfaces) {
104 if (surface == nullptr) {
105 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
106 return INVALID_OPERATION;
107 }
108
Emilian Peev40ead602017-09-26 15:46:36 +0100109 ssize_t id = getNextSurfaceIdLocked();
110 if (id < 0) {
111 ALOGE("%s: No surface ids available!", __func__);
112 return NO_MEMORY;
113 }
114
115 mSurfaces[id] = surface;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800116
117 // Only call addOutput if the splitter has been connected.
118 if (mStreamSplitter != nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +0100119 ret = mStreamSplitter->addOutput(id, surface);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800120 if (ret != OK) {
121 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
122 return ret;
123
124 }
125 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700126 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800127 return ret;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128}
129
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800130status_t Camera3SharedOutputStream::getBufferLocked(camera3_stream_buffer *buffer,
131 const std::vector<size_t>& surface_ids) {
132 ANativeWindowBuffer* anb;
133 int fenceFd = -1;
134
135 status_t res;
136 res = getBufferLockedCommon(&anb, &fenceFd);
137 if (res != OK) {
138 return res;
139 }
140
141 // Attach the buffer to the splitter output queues. This could block if
142 // the output queue doesn't have any empty slot. So unlock during the course
143 // of attachBufferToOutputs.
144 sp<Camera3StreamSplitter> splitter = mStreamSplitter;
145 mLock.unlock();
146 res = splitter->attachBufferToOutputs(anb, surface_ids);
147 mLock.lock();
148 if (res != OK) {
149 ALOGE("%s: Stream %d: Cannot attach stream splitter buffer to outputs: %s (%d)",
150 __FUNCTION__, mId, strerror(-res), res);
151 // Only transition to STATE_ABANDONED from STATE_CONFIGURED. (If it is STATE_PREPARING,
152 // let prepareNextBuffer handle the error.)
153 if (res == NO_INIT && mState == STATE_CONFIGURED) {
154 mState = STATE_ABANDONED;
155 }
156
157 return res;
158 }
159
160 /**
161 * FenceFD now owned by HAL except in case of error,
162 * in which case we reassign it to acquire_fence
163 */
164 handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
165 /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
166
167 return OK;
168}
169
170status_t Camera3SharedOutputStream::queueBufferToConsumer(sp<ANativeWindow>& consumer,
171 ANativeWindowBuffer* buffer, int anwReleaseFence) {
172 status_t res = consumer->queueBuffer(consumer.get(), buffer, anwReleaseFence);
173
174 // After queuing buffer to the internal consumer queue, check whether the buffer is
175 // successfully queued to the output queues.
176 if (res == OK) {
177 res = mStreamSplitter->getOnFrameAvailableResult();
178 if (res != OK) {
179 ALOGE("%s: getOnFrameAvailable returns %d", __FUNCTION__, res);
180 }
181 } else {
182 ALOGE("%s: queueBufer failed %d", __FUNCTION__, res);
183 }
184
185 return res;
186}
187
Shuzhen Wang0129d522016-10-30 22:43:41 -0700188status_t Camera3SharedOutputStream::configureQueueLocked() {
189 status_t res;
190
191 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
192 return res;
193 }
194
195 res = connectStreamSplitterLocked();
196 if (res != OK) {
197 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
198 return res;
199 }
200
201 res = configureConsumerQueueLocked();
202 if (res != OK) {
203 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
204 return res;
205 }
206
207 return OK;
208}
209
210status_t Camera3SharedOutputStream::disconnectLocked() {
211 status_t res;
212 res = Camera3OutputStream::disconnectLocked();
213
214 if (mStreamSplitter != nullptr) {
215 mStreamSplitter->disconnect();
216 }
217
218 return res;
219}
220
Emilian Peev050f5dc2017-05-18 14:43:56 +0100221status_t Camera3SharedOutputStream::getEndpointUsage(uint64_t *usage) const {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700222
Shuzhen Wang758c2152017-01-10 18:26:18 -0800223 status_t res = OK;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100224 uint64_t u = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700225
226 if (mConsumer == nullptr) {
227 // Called before shared buffer queue is constructed.
228 *usage = getPresetConsumerUsage();
229
Emilian Peev40ead602017-09-26 15:46:36 +0100230 for (size_t id = 0; id < kMaxOutputs; id++) {
231 if (mSurfaces[id] != nullptr) {
232 res = getEndpointUsageForSurface(&u, mSurfaces[id]);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700233 *usage |= u;
234 }
235 }
236 } else {
237 // Called after shared buffer queue is constructed.
238 res = getEndpointUsageForSurface(&u, mConsumer);
239 *usage |= u;
240 }
241
242 return res;
243}
244
Emilian Peev40ead602017-09-26 15:46:36 +0100245ssize_t Camera3SharedOutputStream::getNextSurfaceIdLocked() {
246 ssize_t id = -1;
247 for (size_t i = 0; i < kMaxOutputs; i++) {
248 if (mSurfaces[i] == nullptr) {
249 id = i;
250 break;
251 }
252 }
253
254 return id;
255}
256
257ssize_t Camera3SharedOutputStream::getSurfaceId(const sp<Surface> &surface) {
258 Mutex::Autolock l(mLock);
259 ssize_t id = -1;
260 for (size_t i = 0; i < kMaxOutputs; i++) {
261 if (mSurfaces[i] == surface) {
262 id = i;
263 break;
264 }
265 }
266
267 return id;
268}
269
270status_t Camera3SharedOutputStream::revertPartialUpdateLocked(
271 const KeyedVector<sp<Surface>, size_t> &removedSurfaces,
272 const KeyedVector<sp<Surface>, size_t> &attachedSurfaces) {
273 status_t ret = OK;
274
275 for (size_t i = 0; i < attachedSurfaces.size(); i++) {
276 size_t index = attachedSurfaces.valueAt(i);
277 if (mStreamSplitter != nullptr) {
278 ret = mStreamSplitter->removeOutput(index);
279 if (ret != OK) {
280 return UNKNOWN_ERROR;
281 }
282 }
283 mSurfaces[index] = nullptr;
284 }
285
286 for (size_t i = 0; i < removedSurfaces.size(); i++) {
287 size_t index = removedSurfaces.valueAt(i);
288 if (mStreamSplitter != nullptr) {
289 ret = mStreamSplitter->addOutput(index, removedSurfaces.keyAt(i));
290 if (ret != OK) {
291 return UNKNOWN_ERROR;
292 }
293 }
294 mSurfaces[index] = removedSurfaces.keyAt(i);
295 }
296
297 return ret;
298}
299
300status_t Camera3SharedOutputStream::updateStream(const std::vector<sp<Surface>> &outputSurfaces,
301 const std::vector<OutputStreamInfo> &outputInfo,
302 const std::vector<size_t> &removedSurfaceIds,
303 KeyedVector<sp<Surface>, size_t> *outputMap) {
304 status_t ret = OK;
305 Mutex::Autolock l(mLock);
306
307 if ((outputMap == nullptr) || (outputInfo.size() != outputSurfaces.size()) ||
308 (outputSurfaces.size() > kMaxOutputs)) {
309 return BAD_VALUE;
310 }
311
312 uint64_t usage;
313 getEndpointUsage(&usage);
314 KeyedVector<sp<Surface>, size_t> removedSurfaces;
315 //Check whether the new surfaces are compatible.
316 for (const auto &infoIt : outputInfo) {
317 bool imgReaderUsage = (infoIt.consumerUsage & GRALLOC_USAGE_SW_READ_OFTEN) ? true : false;
318 bool sizeMismatch = ((static_cast<uint32_t>(infoIt.width) != getWidth()) ||
319 (static_cast<uint32_t> (infoIt.height) != getHeight())) ?
320 true : false;
321 if ((imgReaderUsage && sizeMismatch) ||
322 (infoIt.format != getOriginalFormat() && infoIt.format != getFormat()) ||
323 (infoIt.dataSpace != getDataSpace() &&
324 infoIt.dataSpace != getOriginalDataSpace())) {
325 ALOGE("%s: Shared surface parameters format: 0x%x dataSpace: 0x%x "
326 " don't match source stream format: 0x%x dataSpace: 0x%x", __FUNCTION__,
327 infoIt.format, infoIt.dataSpace, getFormat(), getDataSpace());
328 return BAD_VALUE;
329 }
330 }
331
332 //First remove all absent outputs
333 for (const auto &it : removedSurfaceIds) {
334 if (mStreamSplitter != nullptr) {
335 ret = mStreamSplitter->removeOutput(it);
336 if (ret != OK) {
337 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
338 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
339 if (res != OK) {
340 return res;
341 }
342 return ret;
343
344 }
345 }
346 mSurfaces[it] = nullptr;
347 removedSurfaces.add(mSurfaces[it], it);
348 }
349
350 //Next add the new outputs
351 for (const auto &it : outputSurfaces) {
352 ssize_t surfaceId = getNextSurfaceIdLocked();
353 if (surfaceId < 0) {
354 ALOGE("%s: No more available output slots!", __FUNCTION__);
355 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
356 if (res != OK) {
357 return res;
358 }
359 return NO_MEMORY;
360 }
361 if (mStreamSplitter != nullptr) {
362 ret = mStreamSplitter->addOutput(surfaceId, it);
363 if (ret != OK) {
364 ALOGE("%s: failed with error code %d", __FUNCTION__, ret);
365 status_t res = revertPartialUpdateLocked(removedSurfaces, *outputMap);
366 if (res != OK) {
367 return res;
368 }
369 return ret;
370 }
371 }
372 mSurfaces[surfaceId] = it;
373 outputMap->add(it, surfaceId);
374 }
375
376 return ret;
377}
378
Shuzhen Wang0129d522016-10-30 22:43:41 -0700379} // namespace camera3
380
381} // namespace android