• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-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-IOStreamBase"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20 
21 #include <inttypes.h>
22 
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25 #include "device3/Camera3IOStreamBase.h"
26 #include "device3/StatusTracker.h"
27 
28 namespace android {
29 
30 namespace camera3 {
31 
Camera3IOStreamBase(int id,camera_stream_type_t type,uint32_t width,uint32_t height,size_t maxSize,int format,android_dataspace dataSpace,camera_stream_rotation_t rotation,const String8 & physicalCameraId,const std::unordered_set<int32_t> & sensorPixelModesUsed,int setId,bool isMultiResolution,int64_t dynamicRangeProfile,int64_t streamUseCase,bool deviceTimeBaseIsRealtime,int timestampBase,int32_t colorSpace)32 Camera3IOStreamBase::Camera3IOStreamBase(int id, camera_stream_type_t type,
33         uint32_t width, uint32_t height, size_t maxSize, int format,
34         android_dataspace dataSpace, camera_stream_rotation_t rotation,
35         const String8& physicalCameraId,
36         const std::unordered_set<int32_t> &sensorPixelModesUsed,
37         int setId, bool isMultiResolution, int64_t dynamicRangeProfile, int64_t streamUseCase,
38         bool deviceTimeBaseIsRealtime, int timestampBase, int32_t colorSpace) :
39         Camera3Stream(id, type,
40                 width, height, maxSize, format, dataSpace, rotation,
41                 physicalCameraId, sensorPixelModesUsed, setId, isMultiResolution,
42                 dynamicRangeProfile, streamUseCase, deviceTimeBaseIsRealtime, timestampBase,
43                 colorSpace),
44         mTotalBufferCount(0),
45         mMaxCachedBufferCount(0),
46         mHandoutTotalBufferCount(0),
47         mHandoutOutputBufferCount(0),
48         mCachedOutputBufferCount(0),
49         mFrameCount(0),
50         mLastTimestamp(0) {
51 
52     mCombinedFence = new Fence();
53 
54     if (maxSize > 0 &&
55             (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE)) {
56         ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__,
57                 format);
58         mState = STATE_ERROR;
59     }
60 }
61 
~Camera3IOStreamBase()62 Camera3IOStreamBase::~Camera3IOStreamBase() {
63     disconnectLocked();
64 }
65 
hasOutstandingBuffersLocked() const66 bool Camera3IOStreamBase::hasOutstandingBuffersLocked() const {
67     nsecs_t signalTime = mCombinedFence->getSignalTime();
68     ALOGV("%s: Stream %d: Has %zu outstanding buffers,"
69             " buffer signal time is %" PRId64,
70             __FUNCTION__, mId, mHandoutTotalBufferCount, signalTime);
71     if (mHandoutTotalBufferCount > 0 || signalTime == INT64_MAX) {
72         return true;
73     }
74     return false;
75 }
76 
dump(int fd,const Vector<String16> & args) const77 void Camera3IOStreamBase::dump(int fd, [[maybe_unused]] const Vector<String16> &args) const {
78     String8 lines;
79 
80     uint64_t consumerUsage = 0;
81     status_t res = getEndpointUsage(&consumerUsage);
82     if (res != OK) consumerUsage = 0;
83 
84     lines.appendFormat("      State: %d\n", mState);
85     lines.appendFormat("      Dims: %d x %d, format 0x%x, dataspace 0x%x\n",
86             camera_stream::width, camera_stream::height,
87             camera_stream::format, camera_stream::data_space);
88     lines.appendFormat("      Max size: %zu\n", mMaxSize);
89     lines.appendFormat("      Combined usage: 0x%" PRIx64 ", max HAL buffers: %d\n",
90             mUsage | consumerUsage, camera_stream::max_buffers);
91     if (strlen(camera_stream::physical_camera_id) > 0) {
92         lines.appendFormat("      Physical camera id: %s\n", camera_stream::physical_camera_id);
93     }
94     lines.appendFormat("      Dynamic Range Profile: 0x%" PRIx64 "\n",
95             camera_stream::dynamic_range_profile);
96     lines.appendFormat("      Color Space: %d\n", camera_stream::color_space);
97     lines.appendFormat("      Stream use case: %" PRId64 "\n", camera_stream::use_case);
98     lines.appendFormat("      Timestamp base: %d\n", getTimestampBase());
99     lines.appendFormat("      Frames produced: %d, last timestamp: %" PRId64 " ns\n",
100             mFrameCount, mLastTimestamp);
101     lines.appendFormat("      Total buffers: %zu, currently dequeued: %zu, currently cached: %zu\n",
102             mTotalBufferCount, mHandoutTotalBufferCount, mCachedOutputBufferCount);
103     write(fd, lines.string(), lines.size());
104 
105     Camera3Stream::dump(fd, args);
106 }
107 
configureQueueLocked()108 status_t Camera3IOStreamBase::configureQueueLocked() {
109     status_t res;
110 
111     switch (mState) {
112         case STATE_IN_RECONFIG:
113             res = disconnectLocked();
114             if (res != OK) {
115                 return res;
116             }
117             break;
118         case STATE_IN_CONFIG:
119             // OK
120             break;
121         default:
122             ALOGE("%s: Bad state: %d", __FUNCTION__, mState);
123             return INVALID_OPERATION;
124     }
125 
126     return OK;
127 }
128 
getBufferCountLocked()129 size_t Camera3IOStreamBase::getBufferCountLocked() {
130     return mTotalBufferCount;
131 }
132 
getHandoutOutputBufferCountLocked() const133 size_t Camera3IOStreamBase::getHandoutOutputBufferCountLocked() const {
134     return mHandoutOutputBufferCount;
135 }
136 
getHandoutInputBufferCountLocked()137 size_t Camera3IOStreamBase::getHandoutInputBufferCountLocked() {
138     return (mHandoutTotalBufferCount - mHandoutOutputBufferCount);
139 }
140 
getCachedOutputBufferCountLocked() const141 size_t Camera3IOStreamBase::getCachedOutputBufferCountLocked() const {
142     return mCachedOutputBufferCount;
143 }
144 
getMaxCachedOutputBuffersLocked() const145 size_t Camera3IOStreamBase::getMaxCachedOutputBuffersLocked() const {
146     return mMaxCachedBufferCount;
147 }
148 
disconnectLocked()149 status_t Camera3IOStreamBase::disconnectLocked() {
150     switch (mState) {
151         case STATE_IN_RECONFIG:
152         case STATE_CONFIGURED:
153         case STATE_ABANDONED:
154             // OK
155             break;
156         default:
157             // No connection, nothing to do
158             ALOGV("%s: Stream %d: Already disconnected",
159                   __FUNCTION__, mId);
160             return -ENOTCONN;
161     }
162 
163     if (mHandoutTotalBufferCount > 0) {
164         ALOGE("%s: Can't disconnect with %zu buffers still dequeued!",
165                 __FUNCTION__, mHandoutTotalBufferCount);
166         return INVALID_OPERATION;
167     }
168 
169    return OK;
170 }
171 
handoutBufferLocked(camera_stream_buffer & buffer,buffer_handle_t * handle,int acquireFence,int releaseFence,camera_buffer_status_t status,bool output)172 void Camera3IOStreamBase::handoutBufferLocked(camera_stream_buffer &buffer,
173                                               buffer_handle_t *handle,
174                                               int acquireFence,
175                                               int releaseFence,
176                                               camera_buffer_status_t status,
177                                               bool output) {
178     /**
179      * Note that all fences are now owned by HAL.
180      */
181 
182     // Handing out a raw pointer to this object. Increment internal refcount.
183     incStrong(this);
184     buffer.stream = this;
185     buffer.buffer = handle;
186     buffer.acquire_fence = acquireFence;
187     buffer.release_fence = releaseFence;
188     buffer.status = status;
189 
190     // Inform tracker about becoming busy
191     if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
192             mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
193         /**
194          * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
195          * before/after register_stream_buffers during initial configuration
196          * or re-configuration, or during prepare pre-allocation
197          */
198         sp<StatusTracker> statusTracker = mStatusTracker.promote();
199         if (statusTracker != 0) {
200             statusTracker->markComponentActive(mStatusId);
201         }
202     }
203     mHandoutTotalBufferCount++;
204 
205     if (output) {
206         mHandoutOutputBufferCount++;
207     }
208 }
209 
getBufferPreconditionCheckLocked() const210 status_t Camera3IOStreamBase::getBufferPreconditionCheckLocked() const {
211     // Allow dequeue during IN_[RE]CONFIG for registration, in
212     // PREPARING for pre-allocation
213     if (mState != STATE_CONFIGURED &&
214             mState != STATE_IN_CONFIG && mState != STATE_IN_RECONFIG &&
215             mState != STATE_PREPARING) {
216         ALOGE("%s: Stream %d: Can't get buffers in unconfigured state %d",
217                 __FUNCTION__, mId, mState);
218         return INVALID_OPERATION;
219     }
220 
221     return OK;
222 }
223 
returnBufferPreconditionCheckLocked() const224 status_t Camera3IOStreamBase::returnBufferPreconditionCheckLocked() const {
225     // Allow buffers to be returned in the error state, to allow for disconnect
226     // and in the in-config states for registration
227     if (mState == STATE_CONSTRUCTED) {
228         ALOGE("%s: Stream %d: Can't return buffers in unconfigured state %d",
229                 __FUNCTION__, mId, mState);
230         return INVALID_OPERATION;
231     }
232     if (mHandoutTotalBufferCount == 0) {
233         ALOGE("%s: Stream %d: No buffers outstanding to return", __FUNCTION__,
234                 mId);
235         return INVALID_OPERATION;
236     }
237 
238     return OK;
239 }
240 
returnAnyBufferLocked(const camera_stream_buffer & buffer,nsecs_t timestamp,nsecs_t readoutTimestamp,bool output,int32_t transform,const std::vector<size_t> & surface_ids)241 status_t Camera3IOStreamBase::returnAnyBufferLocked(
242         const camera_stream_buffer &buffer,
243         nsecs_t timestamp,
244         nsecs_t readoutTimestamp,
245         bool output,
246         int32_t transform,
247         const std::vector<size_t>& surface_ids) {
248     status_t res;
249 
250     // returnBuffer may be called from a raw pointer, not a sp<>, and we'll be
251     // decrementing the internal refcount next. In case this is the last ref, we
252     // might get destructed on the decStrong(), so keep an sp around until the
253     // end of the call - otherwise have to sprinkle the decStrong on all exit
254     // points.
255     sp<Camera3IOStreamBase> keepAlive(this);
256     decStrong(this);
257 
258     if ((res = returnBufferPreconditionCheckLocked()) != OK) {
259         return res;
260     }
261 
262     sp<Fence> releaseFence;
263     res = returnBufferCheckedLocked(buffer, timestamp, readoutTimestamp,
264                                     output, transform, surface_ids,
265                                     &releaseFence);
266     // Res may be an error, but we still want to decrement our owned count
267     // to enable clean shutdown. So we'll just return the error but otherwise
268     // carry on
269 
270     if (releaseFence != 0) {
271         mCombinedFence = Fence::merge(mName, mCombinedFence, releaseFence);
272     }
273 
274     if (output) {
275         mHandoutOutputBufferCount--;
276     }
277 
278     mHandoutTotalBufferCount--;
279     if (mHandoutTotalBufferCount == 0 && mState != STATE_IN_CONFIG &&
280             mState != STATE_IN_RECONFIG && mState != STATE_PREPARING) {
281         /**
282          * Avoid a spurious IDLE->ACTIVE->IDLE transition when using buffers
283          * before/after register_stream_buffers during initial configuration
284          * or re-configuration, or during prepare pre-allocation
285          */
286         ALOGV("%s: Stream %d: All buffers returned; now idle", __FUNCTION__,
287                 mId);
288         sp<StatusTracker> statusTracker = mStatusTracker.promote();
289         if (statusTracker != 0) {
290             statusTracker->markComponentIdle(mStatusId, mCombinedFence);
291         }
292     }
293 
294     if (output) {
295         mLastTimestamp = timestamp;
296     }
297 
298     return res;
299 }
300 
301 
302 
303 }; // namespace camera3
304 
305 }; // namespace android
306