blob: 77039101a6311052dfeb381887fcc067d46f05e1 [file] [log] [blame]
Alex Raybcaf7882013-02-28 16:04:35 -08001/*
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>
Alex Ray69f1f912013-10-21 00:46:44 -070018#include <stdio.h>
Alex Raybcaf7882013-02-28 16:04:35 -080019#include <hardware/camera3.h>
20#include <hardware/gralloc.h>
21#include <system/graphics.h>
22
23//#define LOG_NDEBUG 0
24#define LOG_TAG "Stream"
25#include <cutils/log.h>
26
27#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
Alex Rayea803822013-10-14 15:56:43 -070028#include <utils/Trace.h>
Alex Raybcaf7882013-02-28 16:04:35 -080029
30#include "Stream.h"
31
32namespace default_camera_hal {
33
34Stream::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),
Alex Ray8a8f86b2013-03-01 01:32:21 -080044 mRegistered(false),
45 mBuffers(0),
46 mNumBuffers(0)
Alex Raybcaf7882013-02-28 16:04:35 -080047{
48 // NULL (default) pthread mutex attributes
49 pthread_mutex_init(&mMutex, NULL);
50}
51
52Stream::~Stream()
53{
Alex Ray8a8f86b2013-03-01 01:32:21 -080054 pthread_mutex_lock(&mMutex);
55 unregisterBuffers_L();
56 pthread_mutex_unlock(&mMutex);
Alex Raybcaf7882013-02-28 16:04:35 -080057}
58
59void Stream::setUsage(uint32_t usage)
60{
61 pthread_mutex_lock(&mMutex);
62 if (usage != mUsage) {
63 mUsage = usage;
Alex Ray2b286da2013-05-29 15:08:29 -070064 mStream->usage = usage;
Alex Ray8a8f86b2013-03-01 01:32:21 -080065 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080066 }
67 pthread_mutex_unlock(&mMutex);
68}
69
70void Stream::setMaxBuffers(uint32_t max_buffers)
71{
72 pthread_mutex_lock(&mMutex);
73 if (max_buffers != mMaxBuffers) {
74 mMaxBuffers = max_buffers;
Alex Ray2b286da2013-05-29 15:08:29 -070075 mStream->max_buffers = max_buffers;
Alex Ray8a8f86b2013-03-01 01:32:21 -080076 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080077 }
78 pthread_mutex_unlock(&mMutex);
79}
80
81int Stream::getType()
82{
83 return mType;
84}
85
86bool Stream::isInputType()
87{
Alex Ray768216e2013-07-02 16:56:14 -070088 return mType == CAMERA3_STREAM_INPUT ||
89 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080090}
91
92bool Stream::isOutputType()
93{
Alex Ray768216e2013-07-02 16:56:14 -070094 return mType == CAMERA3_STREAM_OUTPUT ||
95 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080096}
97
Alex Ray69f1f912013-10-21 00:46:44 -070098const char* Stream::typeToString(int type)
99{
100 switch (type) {
101 case CAMERA3_STREAM_INPUT:
102 return "CAMERA3_STREAM_INPUT";
103 case CAMERA3_STREAM_OUTPUT:
104 return "CAMERA3_STREAM_OUTPUT";
105 case CAMERA3_STREAM_BIDIRECTIONAL:
106 return "CAMERA3_STREAM_BIDIRECTIONAL";
107 }
108 return "Invalid stream type!";
109}
110
111const char* Stream::formatToString(int format)
112{
113 // See <system/graphics.h> for full list
114 switch (format) {
115 case HAL_PIXEL_FORMAT_BGRA_8888:
116 return "BGRA 8888";
117 case HAL_PIXEL_FORMAT_RGBA_8888:
118 return "RGBA 8888";
119 case HAL_PIXEL_FORMAT_RGBX_8888:
120 return "RGBX 8888";
121 case HAL_PIXEL_FORMAT_RGB_888:
122 return "RGB 888";
123 case HAL_PIXEL_FORMAT_RGB_565:
124 return "RGB 565";
125 case HAL_PIXEL_FORMAT_sRGB_A_8888:
126 return "sRGB A 8888";
127 case HAL_PIXEL_FORMAT_sRGB_X_8888:
128 return "sRGB B 8888";
129 case HAL_PIXEL_FORMAT_Y8:
130 return "Y8";
131 case HAL_PIXEL_FORMAT_Y16:
132 return "Y16";
133 case HAL_PIXEL_FORMAT_YV12:
134 return "YV12";
135 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
136 return "NV16";
137 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
138 return "NV21";
139 case HAL_PIXEL_FORMAT_YCbCr_422_I:
140 return "YUY2";
141 case HAL_PIXEL_FORMAT_RAW_SENSOR:
142 return "RAW SENSOR";
143 case HAL_PIXEL_FORMAT_BLOB:
144 return "BLOB";
145 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
146 return "IMPLEMENTATION DEFINED";
147 case HAL_PIXEL_FORMAT_YCbCr_420_888:
148 return "FLEXIBLE YCbCr 420 888";
149 }
150 return "Invalid stream format!";
151}
152
Alex Ray8a8f86b2013-03-01 01:32:21 -0800153bool Stream::isRegistered()
154{
155 return mRegistered;
156}
157
Alex Raybcaf7882013-02-28 16:04:35 -0800158bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
159{
160 if (id != mId) {
161 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
162 __func__, mId, id, mId);
163 return false;
164 }
165 if (s != mStream) {
166 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
167 __func__, mId, s, mStream);
168 return false;
169 }
170 if (s->stream_type != mType) {
Alex Ray69f1f912013-10-21 00:46:44 -0700171 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
172 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
173 s->stream_type, typeToString(mType), mType);
Alex Raybcaf7882013-02-28 16:04:35 -0800174 return false;
175 }
176 if (s->format != mFormat) {
Alex Ray69f1f912013-10-21 00:46:44 -0700177 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
178 "expect %s(%d)", __func__, mId, formatToString(s->format),
179 s->format, formatToString(mFormat), mFormat);
Alex Raybcaf7882013-02-28 16:04:35 -0800180 return false;
181 }
182 if (s->width != mWidth) {
183 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
184 __func__, mId, s->width, mWidth);
185 return false;
186 }
187 if (s->height != mHeight) {
188 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
189 __func__, mId, s->height, mHeight);
190 return false;
191 }
192 return true;
193}
194
Alex Ray8a8f86b2013-03-01 01:32:21 -0800195int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
196{
Alex Rayea803822013-10-14 15:56:43 -0700197 ATRACE_CALL();
Alex Ray8a8f86b2013-03-01 01:32:21 -0800198
199 if (buf_set->stream != mStream) {
200 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
201 __func__, mId, buf_set->stream, mStream);
202 return -EINVAL;
203 }
204
205 pthread_mutex_lock(&mMutex);
206
207 mNumBuffers = buf_set->num_buffers;
208 mBuffers = new buffer_handle_t*[mNumBuffers];
209
210 for (unsigned int i = 0; i < mNumBuffers; i++) {
211 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
212 buf_set->buffers[i]);
213 mBuffers[i] = buf_set->buffers[i];
214 // TODO: register buffers with hw, handle error cases
215 }
216 mRegistered = true;
217
218 pthread_mutex_unlock(&mMutex);
219
220 return 0;
221}
222
223// This must only be called with mMutex held
224void Stream::unregisterBuffers_L()
225{
226 mRegistered = false;
227 mNumBuffers = 0;
228 delete [] mBuffers;
229 // TODO: unregister buffers from hw
230}
231
Alex Ray69f1f912013-10-21 00:46:44 -0700232void Stream::dump(int fd)
233{
234 pthread_mutex_lock(&mMutex);
235
236 fdprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
237 fdprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
238 fdprintf(fd, "Width: %u Height: %u\n", mWidth, mHeight);
239 fdprintf(fd, "Stream Format: %s (%d)", formatToString(mFormat), mFormat);
240 // ToDo: prettyprint usage mask flags
241 fdprintf(fd, "Gralloc Usage Mask: 0x%x\n", mUsage);
242 fdprintf(fd, "Max Buffer Count: %d\n", mMaxBuffers);
243 fdprintf(fd, "Buffers Registered: %s\n", mRegistered ? "true" : "false");
244 fdprintf(fd, "Number of Buffers: %d\n", mNumBuffers);
245 for (int i = 0; i < mNumBuffers; i++) {
246 fdprintf(fd, "Buffer %d/%d: %p\n", i, mNumBuffers, mBuffers[i]);
247 }
248
249 pthread_mutex_unlock(&mMutex);
250}
251
Alex Raybcaf7882013-02-28 16:04:35 -0800252} // namespace default_camera_hal