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