Camera3IOStreamBase.cpp revision e5729fac81c8a984e984fefc90afc64135817d4f
164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina/* 264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * Copyright (C) 2013 The Android Open Source Project 364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * 464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * Licensed under the Apache License, Version 2.0 (the "License"); 564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * you may not use this file except in compliance with the License. 664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * You may obtain a copy of the License at 764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * 864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * http://www.apache.org/licenses/LICENSE-2.0 964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * 1064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * Unless required by applicable law or agreed to in writing, software 1164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * distributed under the License is distributed on an "AS IS" BASIS, 1264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * See the License for the specific language governing permissions and 1464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina * limitations under the License. 1564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina */ 1664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 1764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#define LOG_TAG "Camera3-IOStreamBase" 1864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#define ATRACE_TAG ATRACE_TAG_CAMERA 1964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina//#define LOG_NDEBUG 0 2064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 2164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#include <inttypes.h> 2264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 2364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#include <utils/Log.h> 2464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#include <utils/Trace.h> 2564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#include "device3/Camera3IOStreamBase.h" 2664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina#include "device3/StatusTracker.h" 2764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 2864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghinanamespace android { 2964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 3064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghinanamespace camera3 { 3164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 3264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra GherghinaCamera3IOStreamBase::Camera3IOStreamBase(int id, camera3_stream_type_t type, 3364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina uint32_t width, uint32_t height, size_t maxSize, int format) : 3464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina Camera3Stream(id, type, 3564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina width, height, maxSize, format), 3664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mTotalBufferCount(0), 3764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mDequeuedBufferCount(0), 3864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mFrameCount(0), 3964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mLastTimestamp(0) { 4064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 4164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mCombinedFence = new Fence(); 4264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 4364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina if (maxSize > 0 && format != HAL_PIXEL_FORMAT_BLOB) { 4464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina ALOGE("%s: Bad format for size-only stream: %d", __FUNCTION__, 4564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina format); 4664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina mState = STATE_ERROR; 4764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina } 4864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina} 4964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 5064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra GherghinaCamera3IOStreamBase::~Camera3IOStreamBase() { 5164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina disconnectLocked(); 5264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina} 5364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 5464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghinabool Camera3IOStreamBase::hasOutstandingBuffersLocked() const { 5564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina nsecs_t signalTime = mCombinedFence->getSignalTime(); 5664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina ALOGV("%s: Stream %d: Has %zu outstanding buffers," 5764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina " buffer signal time is %" PRId64, 5864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina __FUNCTION__, mId, mDequeuedBufferCount, signalTime); 5964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina if (mDequeuedBufferCount > 0 || signalTime == INT64_MAX) { 6064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina return true; 6164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina } 6264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina return false; 6364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina} 6464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 6564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghinavoid Camera3IOStreamBase::dump(int fd, const Vector<String16> &args) const { 6664d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina (void) args; 6764d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina String8 lines; 6864d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina lines.appendFormat(" State: %d\n", mState); 6964d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina lines.appendFormat(" Dims: %d x %d, format 0x%x\n", 7064d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina camera3_stream::width, camera3_stream::height, 7164d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina camera3_stream::format); 7264d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina lines.appendFormat(" Max size: %zu\n", mMaxSize); 7364d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina lines.appendFormat(" Usage: %d, max HAL buffers: %d\n", 7464d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina camera3_stream::usage, camera3_stream::max_buffers); 7564d4dca63f65e4c7d4a829c85ff6670bdd34e2ebAlexandra Gherghina 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