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