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 #include <pthread.h> 18 #include <hardware/camera3.h> 19 #include <hardware/gralloc.h> 20 #include <system/graphics.h> 21 22 //#define LOG_NDEBUG 0 23 #define LOG_TAG "Stream" 24 #include <cutils/log.h> 25 26 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) 27 #include <cutils/trace.h> 28 #include "ScopedTrace.h" 29 30 #include "Stream.h" 31 32 namespace default_camera_hal { 33 34 Stream::Stream(int id, camera3_stream_t *s) 35 : mReuse(false), 36 mId(id), 37 mStream(s), 38 mType(s->stream_type), 39 mWidth(s->width), 40 mHeight(s->height), 41 mFormat(s->format), 42 mUsage(0), 43 mMaxBuffers(0), 44 mRegistered(false), 45 mBuffers(0), 46 mNumBuffers(0) 47 { 48 // NULL (default) pthread mutex attributes 49 pthread_mutex_init(&mMutex, NULL); 50 } 51 52 Stream::~Stream() 53 { 54 pthread_mutex_lock(&mMutex); 55 unregisterBuffers_L(); 56 pthread_mutex_unlock(&mMutex); 57 } 58 59 void Stream::setUsage(uint32_t usage) 60 { 61 pthread_mutex_lock(&mMutex); 62 if (usage != mUsage) { 63 mUsage = usage; 64 mStream->usage = usage; 65 unregisterBuffers_L(); 66 } 67 pthread_mutex_unlock(&mMutex); 68 } 69 70 void Stream::setMaxBuffers(uint32_t max_buffers) 71 { 72 pthread_mutex_lock(&mMutex); 73 if (max_buffers != mMaxBuffers) { 74 mMaxBuffers = max_buffers; 75 mStream->max_buffers = max_buffers; 76 unregisterBuffers_L(); 77 } 78 pthread_mutex_unlock(&mMutex); 79 } 80 81 int Stream::getType() 82 { 83 return mType; 84 } 85 86 bool Stream::isInputType() 87 { 88 return mType == CAMERA3_STREAM_INPUT || 89 mType == CAMERA3_STREAM_BIDIRECTIONAL; 90 } 91 92 bool Stream::isOutputType() 93 { 94 return mType == CAMERA3_STREAM_OUTPUT || 95 mType == CAMERA3_STREAM_BIDIRECTIONAL; 96 } 97 98 bool Stream::isRegistered() 99 { 100 return mRegistered; 101 } 102 103 bool Stream::isValidReuseStream(int id, camera3_stream_t *s) 104 { 105 if (id != mId) { 106 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d", 107 __func__, mId, id, mId); 108 return false; 109 } 110 if (s != mStream) { 111 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p", 112 __func__, mId, s, mStream); 113 return false; 114 } 115 if (s->stream_type != mType) { 116 // TODO: prettyprint type string 117 ALOGE("%s:%d: Mismatched type in reused stream. Got %d expect %d", 118 __func__, mId, s->stream_type, mType); 119 return false; 120 } 121 if (s->format != mFormat) { 122 // TODO: prettyprint format string 123 ALOGE("%s:%d: Mismatched format in reused stream. Got %d expect %d", 124 __func__, mId, s->format, mFormat); 125 return false; 126 } 127 if (s->width != mWidth) { 128 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d", 129 __func__, mId, s->width, mWidth); 130 return false; 131 } 132 if (s->height != mHeight) { 133 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d", 134 __func__, mId, s->height, mHeight); 135 return false; 136 } 137 return true; 138 } 139 140 int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set) 141 { 142 CAMTRACE_CALL(); 143 144 if (buf_set->stream != mStream) { 145 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p", 146 __func__, mId, buf_set->stream, mStream); 147 return -EINVAL; 148 } 149 150 pthread_mutex_lock(&mMutex); 151 152 mNumBuffers = buf_set->num_buffers; 153 mBuffers = new buffer_handle_t*[mNumBuffers]; 154 155 for (unsigned int i = 0; i < mNumBuffers; i++) { 156 ALOGV("%s:%d: Registering buffer %p", __func__, mId, 157 buf_set->buffers[i]); 158 mBuffers[i] = buf_set->buffers[i]; 159 // TODO: register buffers with hw, handle error cases 160 } 161 mRegistered = true; 162 163 pthread_mutex_unlock(&mMutex); 164 165 return 0; 166 } 167 168 // This must only be called with mMutex held 169 void Stream::unregisterBuffers_L() 170 { 171 mRegistered = false; 172 mNumBuffers = 0; 173 delete [] mBuffers; 174 // TODO: unregister buffers from hw 175 } 176 177 } // namespace default_camera_hal 178