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