1 /*
2  * Copyright (C) 2013 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-OutputStream"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 
21 #include <utils/Log.h>
22 #include <utils/Trace.h>
23 #include "Camera3OutputStream.h"
24 
25 #ifndef container_of
26 #define container_of(ptr, type, member) \
27     (type *)((char*)(ptr) - offsetof(type, member))
28 #endif
29 
30 namespace android {
31 
32 namespace camera3 {
33 
Camera3OutputStream(int id,sp<Surface> consumer,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation)34 Camera3OutputStream::Camera3OutputStream(int id,
35         sp<Surface> consumer,
36         uint32_t width, uint32_t height, int format,
37         android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
38         Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height,
39                             /*maxSize*/0, format, dataSpace, rotation),
40         mConsumer(consumer),
41         mTransform(0),
42         mTraceFirstBuffer(true) {
43 
44     if (mConsumer == NULL) {
45         ALOGE("%s: Consumer is NULL!", __FUNCTION__);
46         mState = STATE_ERROR;
47     }
48 }
49 
Camera3OutputStream(int id,sp<Surface> consumer,uint32_t width,uint32_t height,size_t maxSize,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation)50 Camera3OutputStream::Camera3OutputStream(int id,
51         sp<Surface> consumer,
52         uint32_t width, uint32_t height, size_t maxSize, int format,
53         android_dataspace dataSpace, camera3_stream_rotation_t rotation) :
54         Camera3IOStreamBase(id, CAMERA3_STREAM_OUTPUT, width, height, maxSize,
55                             format, dataSpace, rotation),
56         mConsumer(consumer),
57         mTransform(0),
58         mTraceFirstBuffer(true) {
59 
60     if (format != HAL_PIXEL_FORMAT_BLOB) {
61         ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
62                 format);
63         mState = STATE_ERROR;
64     }
65 
66     if (mConsumer == NULL) {
67         ALOGE("%s: Consumer is NULL!", __FUNCTION__);
68         mState = STATE_ERROR;
69     }
70 }
71 
Camera3OutputStream(int id,camera3_stream_type_t type,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera3_stream_rotation_t rotation)72 Camera3OutputStream::Camera3OutputStream(int id, camera3_stream_type_t type,
73                                          uint32_t width, uint32_t height,
74                                          int format,
75                                          android_dataspace dataSpace,
76                                          camera3_stream_rotation_t rotation) :
77         Camera3IOStreamBase(id, type, width, height,
78                             /*maxSize*/0,
79                             format, dataSpace, rotation),
80         mTransform(0) {
81 
82     // Subclasses expected to initialize mConsumer themselves
83 }
84 
85 
~Camera3OutputStream()86 Camera3OutputStream::~Camera3OutputStream() {
87     disconnectLocked();
88 }
89 
getBufferLocked(camera3_stream_buffer * buffer)90 status_t Camera3OutputStream::getBufferLocked(camera3_stream_buffer *buffer) {
91     ATRACE_CALL();
92     status_t res;
93 
94     if ((res = getBufferPreconditionCheckLocked()) != OK) {
95         return res;
96     }
97 
98     ANativeWindowBuffer* anb;
99     int fenceFd;
100 
101     /**
102      * Release the lock briefly to avoid deadlock for below scenario:
103      * Thread 1: StreamingProcessor::startStream -> Camera3Stream::isConfiguring().
104      * This thread acquired StreamingProcessor lock and try to lock Camera3Stream lock.
105      * Thread 2: Camera3Stream::returnBuffer->StreamingProcessor::onFrameAvailable().
106      * This thread acquired Camera3Stream lock and bufferQueue lock, and try to lock
107      * StreamingProcessor lock.
108      * Thread 3: Camera3Stream::getBuffer(). This thread acquired Camera3Stream lock
109      * and try to lock bufferQueue lock.
110      * Then there is circular locking dependency.
111      */
112     sp<ANativeWindow> currentConsumer = mConsumer;
113     mLock.unlock();
114 
115     res = currentConsumer->dequeueBuffer(currentConsumer.get(), &anb, &fenceFd);
116     mLock.lock();
117     if (res != OK) {
118         ALOGE("%s: Stream %d: Can't dequeue next output buffer: %s (%d)",
119                 __FUNCTION__, mId, strerror(-res), res);
120         return res;
121     }
122 
123     /**
124      * FenceFD now owned by HAL except in case of error,
125      * in which case we reassign it to acquire_fence
126      */
127     handoutBufferLocked(*buffer, &(anb->handle), /*acquireFence*/fenceFd,
128                         /*releaseFence*/-1, CAMERA3_BUFFER_STATUS_OK, /*output*/true);
129 
130     return OK;
131 }
132 
returnBufferLocked(const camera3_stream_buffer & buffer,nsecs_t timestamp)133 status_t Camera3OutputStream::returnBufferLocked(
134         const camera3_stream_buffer &buffer,
135         nsecs_t timestamp) {
136     ATRACE_CALL();
137 
138     status_t res = returnAnyBufferLocked(buffer, timestamp, /*output*/true);
139 
140     if (res != OK) {
141         return res;
142     }
143 
144     mLastTimestamp = timestamp;
145 
146     return OK;
147 }
148 
returnBufferCheckedLocked(const camera3_stream_buffer & buffer,nsecs_t timestamp,bool output,sp<Fence> * releaseFenceOut)149 status_t Camera3OutputStream::returnBufferCheckedLocked(
150             const camera3_stream_buffer &buffer,
151             nsecs_t timestamp,
152             bool output,
153             /*out*/
154             sp<Fence> *releaseFenceOut) {
155 
156     (void)output;
157     ALOG_ASSERT(output, "Expected output to be true");
158 
159     status_t res;
160 
161     // Fence management - always honor release fence from HAL
162     sp<Fence> releaseFence = new Fence(buffer.release_fence);
163     int anwReleaseFence = releaseFence->dup();
164 
165     /**
166      * Release the lock briefly to avoid deadlock with
167      * StreamingProcessor::startStream -> Camera3Stream::isConfiguring (this
168      * thread will go into StreamingProcessor::onFrameAvailable) during
169      * queueBuffer
170      */
171     sp<ANativeWindow> currentConsumer = mConsumer;
172     mLock.unlock();
173 
174     /**
175      * Return buffer back to ANativeWindow
176      */
177     if (buffer.status == CAMERA3_BUFFER_STATUS_ERROR) {
178         // Cancel buffer
179         res = currentConsumer->cancelBuffer(currentConsumer.get(),
180                 container_of(buffer.buffer, ANativeWindowBuffer, handle),
181                 anwReleaseFence);
182         if (res != OK) {
183             ALOGE("%s: Stream %d: Error cancelling buffer to native window:"
184                   " %s (%d)", __FUNCTION__, mId, strerror(-res), res);
185         }
186     } else {
187         if (mTraceFirstBuffer && (stream_type == CAMERA3_STREAM_OUTPUT)) {
188             {
189                 char traceLog[48];
190                 snprintf(traceLog, sizeof(traceLog), "Stream %d: first full buffer\n", mId);
191                 ATRACE_NAME(traceLog);
192             }
193             mTraceFirstBuffer = false;
194         }
195 
196         res = native_window_set_buffers_timestamp(mConsumer.get(), timestamp);
197         if (res != OK) {
198             ALOGE("%s: Stream %d: Error setting timestamp: %s (%d)",
199                   __FUNCTION__, mId, strerror(-res), res);
200             return res;
201         }
202 
203         res = currentConsumer->queueBuffer(currentConsumer.get(),
204                 container_of(buffer.buffer, ANativeWindowBuffer, handle),
205                 anwReleaseFence);
206         if (res != OK) {
207             ALOGE("%s: Stream %d: Error queueing buffer to native window: "
208                   "%s (%d)", __FUNCTION__, mId, strerror(-res), res);
209         }
210     }
211     mLock.lock();
212 
213     // Once a valid buffer has been returned to the queue, can no longer
214     // dequeue all buffers for preallocation.
215     if (buffer.status != CAMERA3_BUFFER_STATUS_ERROR) {
216         mStreamUnpreparable = true;
217     }
218 
219     if (res != OK) {
220         close(anwReleaseFence);
221     }
222 
223     *releaseFenceOut = releaseFence;
224 
225     return res;
226 }
227 
dump(int fd,const Vector<String16> & args) const228 void Camera3OutputStream::dump(int fd, const Vector<String16> &args) const {
229     (void) args;
230     String8 lines;
231     lines.appendFormat("    Stream[%d]: Output\n", mId);
232     lines.appendFormat("      Consumer name: %s\n", mConsumerName.string());
233     write(fd, lines.string(), lines.size());
234 
235     Camera3IOStreamBase::dump(fd, args);
236 }
237 
setTransform(int transform)238 status_t Camera3OutputStream::setTransform(int transform) {
239     ATRACE_CALL();
240     Mutex::Autolock l(mLock);
241     return setTransformLocked(transform);
242 }
243 
setTransformLocked(int transform)244 status_t Camera3OutputStream::setTransformLocked(int transform) {
245     status_t res = OK;
246     if (mState == STATE_ERROR) {
247         ALOGE("%s: Stream in error state", __FUNCTION__);
248         return INVALID_OPERATION;
249     }
250 
251     mTransform = transform;
252     if (mState == STATE_CONFIGURED) {
253         res = native_window_set_buffers_transform(mConsumer.get(),
254                 transform);
255         if (res != OK) {
256             ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
257                     __FUNCTION__, transform, strerror(-res), res);
258         }
259     }
260     return res;
261 }
262 
configureQueueLocked()263 status_t Camera3OutputStream::configureQueueLocked() {
264     status_t res;
265 
266     mTraceFirstBuffer = true;
267     if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
268         return res;
269     }
270 
271     ALOG_ASSERT(mConsumer != 0, "mConsumer should never be NULL");
272 
273     // Configure consumer-side ANativeWindow interface
274     res = native_window_api_connect(mConsumer.get(),
275             NATIVE_WINDOW_API_CAMERA);
276     if (res != OK) {
277         ALOGE("%s: Unable to connect to native window for stream %d",
278                 __FUNCTION__, mId);
279         return res;
280     }
281 
282     mConsumerName = mConsumer->getConsumerName();
283 
284     res = native_window_set_usage(mConsumer.get(), camera3_stream::usage);
285     if (res != OK) {
286         ALOGE("%s: Unable to configure usage %08x for stream %d",
287                 __FUNCTION__, camera3_stream::usage, mId);
288         return res;
289     }
290 
291     res = native_window_set_scaling_mode(mConsumer.get(),
292             NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
293     if (res != OK) {
294         ALOGE("%s: Unable to configure stream scaling: %s (%d)",
295                 __FUNCTION__, strerror(-res), res);
296         return res;
297     }
298 
299     if (mMaxSize == 0) {
300         // For buffers of known size
301         res = native_window_set_buffers_dimensions(mConsumer.get(),
302                 camera3_stream::width, camera3_stream::height);
303     } else {
304         // For buffers with bounded size
305         res = native_window_set_buffers_dimensions(mConsumer.get(),
306                 mMaxSize, 1);
307     }
308     if (res != OK) {
309         ALOGE("%s: Unable to configure stream buffer dimensions"
310                 " %d x %d (maxSize %zu) for stream %d",
311                 __FUNCTION__, camera3_stream::width, camera3_stream::height,
312                 mMaxSize, mId);
313         return res;
314     }
315     res = native_window_set_buffers_format(mConsumer.get(),
316             camera3_stream::format);
317     if (res != OK) {
318         ALOGE("%s: Unable to configure stream buffer format %#x for stream %d",
319                 __FUNCTION__, camera3_stream::format, mId);
320         return res;
321     }
322 
323     res = native_window_set_buffers_data_space(mConsumer.get(),
324             camera3_stream::data_space);
325     if (res != OK) {
326         ALOGE("%s: Unable to configure stream dataspace %#x for stream %d",
327                 __FUNCTION__, camera3_stream::data_space, mId);
328         return res;
329     }
330 
331     int maxConsumerBuffers;
332     res = static_cast<ANativeWindow*>(mConsumer.get())->query(
333             mConsumer.get(),
334             NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
335     if (res != OK) {
336         ALOGE("%s: Unable to query consumer undequeued"
337                 " buffer count for stream %d", __FUNCTION__, mId);
338         return res;
339     }
340 
341     ALOGV("%s: Consumer wants %d buffers, HAL wants %d", __FUNCTION__,
342             maxConsumerBuffers, camera3_stream::max_buffers);
343     if (camera3_stream::max_buffers == 0) {
344         ALOGE("%s: Camera HAL requested max_buffer count: %d, requires at least 1",
345                 __FUNCTION__, camera3_stream::max_buffers);
346         return INVALID_OPERATION;
347     }
348 
349     mTotalBufferCount = maxConsumerBuffers + camera3_stream::max_buffers;
350     mHandoutTotalBufferCount = 0;
351     mFrameCount = 0;
352     mLastTimestamp = 0;
353 
354     res = native_window_set_buffer_count(mConsumer.get(),
355             mTotalBufferCount);
356     if (res != OK) {
357         ALOGE("%s: Unable to set buffer count for stream %d",
358                 __FUNCTION__, mId);
359         return res;
360     }
361 
362     res = native_window_set_buffers_transform(mConsumer.get(),
363             mTransform);
364     if (res != OK) {
365         ALOGE("%s: Unable to configure stream transform to %x: %s (%d)",
366                 __FUNCTION__, mTransform, strerror(-res), res);
367     }
368 
369     return OK;
370 }
371 
disconnectLocked()372 status_t Camera3OutputStream::disconnectLocked() {
373     status_t res;
374 
375     if ((res = Camera3IOStreamBase::disconnectLocked()) != OK) {
376         return res;
377     }
378 
379     res = native_window_api_disconnect(mConsumer.get(),
380                                        NATIVE_WINDOW_API_CAMERA);
381 
382     /**
383      * This is not an error. if client calling process dies, the window will
384      * also die and all calls to it will return DEAD_OBJECT, thus it's already
385      * "disconnected"
386      */
387     if (res == DEAD_OBJECT) {
388         ALOGW("%s: While disconnecting stream %d from native window, the"
389                 " native window died from under us", __FUNCTION__, mId);
390     }
391     else if (res != OK) {
392         ALOGE("%s: Unable to disconnect stream %d from native window "
393               "(error %d %s)",
394               __FUNCTION__, mId, res, strerror(-res));
395         mState = STATE_ERROR;
396         return res;
397     }
398 
399     mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
400                                            : STATE_CONSTRUCTED;
401     return OK;
402 }
403 
getEndpointUsage(uint32_t * usage) const404 status_t Camera3OutputStream::getEndpointUsage(uint32_t *usage) const {
405 
406     status_t res;
407     int32_t u = 0;
408     res = static_cast<ANativeWindow*>(mConsumer.get())->query(mConsumer.get(),
409             NATIVE_WINDOW_CONSUMER_USAGE_BITS, &u);
410 
411     // If an opaque output stream's endpoint is ImageReader, add
412     // GRALLOC_USAGE_HW_CAMERA_ZSL to the usage so HAL knows it will be used
413     // for the ZSL use case.
414     // Assume it's for ImageReader if the consumer usage doesn't have any of these bits set:
415     //     1. GRALLOC_USAGE_HW_TEXTURE
416     //     2. GRALLOC_USAGE_HW_RENDER
417     //     3. GRALLOC_USAGE_HW_COMPOSER
418     //     4. GRALLOC_USAGE_HW_VIDEO_ENCODER
419     if (camera3_stream::format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
420             (u & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_COMPOSER |
421             GRALLOC_USAGE_HW_VIDEO_ENCODER)) == 0) {
422         u |= GRALLOC_USAGE_HW_CAMERA_ZSL;
423     }
424 
425     *usage = u;
426     return res;
427 }
428 
429 }; // namespace camera3
430 
431 }; // namespace android
432