blob: e0099b6e388f1fcd1196c56f3dec82d831f542e2 [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
Alex Ray69f1f912013-10-21 00:46:44 -070017#include <stdio.h>
Alex Raybcaf7882013-02-28 16:04:35 -080018#include <hardware/camera3.h>
19#include <hardware/gralloc.h>
20#include <system/graphics.h>
Alex Ray55567642013-11-12 12:58:39 -080021#include <utils/Mutex.h>
Alex Raybcaf7882013-02-28 16:04:35 -080022
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{
Alex Raybcaf7882013-02-28 16:04:35 -080048}
49
50Stream::~Stream()
51{
Alex Ray55567642013-11-12 12:58:39 -080052 android::Mutex::Autolock al(mLock);
Alex Ray8a8f86b2013-03-01 01:32:21 -080053 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080054}
55
56void Stream::setUsage(uint32_t usage)
57{
Alex Ray55567642013-11-12 12:58:39 -080058 android::Mutex::Autolock al(mLock);
Alex Raybcaf7882013-02-28 16:04:35 -080059 if (usage != mUsage) {
60 mUsage = usage;
Alex Ray2b286da2013-05-29 15:08:29 -070061 mStream->usage = usage;
Alex Ray8a8f86b2013-03-01 01:32:21 -080062 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080063 }
Alex Raybcaf7882013-02-28 16:04:35 -080064}
65
66void Stream::setMaxBuffers(uint32_t max_buffers)
67{
Alex Ray55567642013-11-12 12:58:39 -080068 android::Mutex::Autolock al(mLock);
Alex Raybcaf7882013-02-28 16:04:35 -080069 if (max_buffers != mMaxBuffers) {
70 mMaxBuffers = max_buffers;
Alex Ray2b286da2013-05-29 15:08:29 -070071 mStream->max_buffers = max_buffers;
Alex Ray8a8f86b2013-03-01 01:32:21 -080072 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080073 }
Alex Raybcaf7882013-02-28 16:04:35 -080074}
75
76int Stream::getType()
77{
78 return mType;
79}
80
81bool Stream::isInputType()
82{
Alex Ray768216e2013-07-02 16:56:14 -070083 return mType == CAMERA3_STREAM_INPUT ||
84 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080085}
86
87bool Stream::isOutputType()
88{
Alex Ray768216e2013-07-02 16:56:14 -070089 return mType == CAMERA3_STREAM_OUTPUT ||
90 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080091}
92
Alex Ray69f1f912013-10-21 00:46:44 -070093const char* Stream::typeToString(int type)
94{
95 switch (type) {
96 case CAMERA3_STREAM_INPUT:
97 return "CAMERA3_STREAM_INPUT";
98 case CAMERA3_STREAM_OUTPUT:
99 return "CAMERA3_STREAM_OUTPUT";
100 case CAMERA3_STREAM_BIDIRECTIONAL:
101 return "CAMERA3_STREAM_BIDIRECTIONAL";
102 }
103 return "Invalid stream type!";
104}
105
106const char* Stream::formatToString(int format)
107{
108 // See <system/graphics.h> for full list
109 switch (format) {
110 case HAL_PIXEL_FORMAT_BGRA_8888:
111 return "BGRA 8888";
112 case HAL_PIXEL_FORMAT_RGBA_8888:
113 return "RGBA 8888";
114 case HAL_PIXEL_FORMAT_RGBX_8888:
115 return "RGBX 8888";
116 case HAL_PIXEL_FORMAT_RGB_888:
117 return "RGB 888";
118 case HAL_PIXEL_FORMAT_RGB_565:
119 return "RGB 565";
Alex Ray69f1f912013-10-21 00:46:44 -0700120 case HAL_PIXEL_FORMAT_Y8:
121 return "Y8";
122 case HAL_PIXEL_FORMAT_Y16:
123 return "Y16";
124 case HAL_PIXEL_FORMAT_YV12:
125 return "YV12";
126 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
127 return "NV16";
128 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
129 return "NV21";
130 case HAL_PIXEL_FORMAT_YCbCr_422_I:
131 return "YUY2";
Eino-Ville Talvalae69efbb2015-03-06 13:19:36 -0800132 case HAL_PIXEL_FORMAT_RAW10:
133 return "RAW10";
134 case HAL_PIXEL_FORMAT_RAW16:
135 return "RAW16";
Alex Ray69f1f912013-10-21 00:46:44 -0700136 case HAL_PIXEL_FORMAT_BLOB:
137 return "BLOB";
138 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
139 return "IMPLEMENTATION DEFINED";
140 case HAL_PIXEL_FORMAT_YCbCr_420_888:
141 return "FLEXIBLE YCbCr 420 888";
142 }
143 return "Invalid stream format!";
144}
145
Alex Ray8a8f86b2013-03-01 01:32:21 -0800146bool Stream::isRegistered()
147{
148 return mRegistered;
149}
150
Alex Raybcaf7882013-02-28 16:04:35 -0800151bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
152{
153 if (id != mId) {
154 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
155 __func__, mId, id, mId);
156 return false;
157 }
158 if (s != mStream) {
159 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
160 __func__, mId, s, mStream);
161 return false;
162 }
163 if (s->stream_type != mType) {
Alex Ray69f1f912013-10-21 00:46:44 -0700164 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
165 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
166 s->stream_type, typeToString(mType), mType);
Alex Raybcaf7882013-02-28 16:04:35 -0800167 return false;
168 }
169 if (s->format != mFormat) {
Alex Ray69f1f912013-10-21 00:46:44 -0700170 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
171 "expect %s(%d)", __func__, mId, formatToString(s->format),
172 s->format, formatToString(mFormat), mFormat);
Alex Raybcaf7882013-02-28 16:04:35 -0800173 return false;
174 }
175 if (s->width != mWidth) {
176 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
177 __func__, mId, s->width, mWidth);
178 return false;
179 }
180 if (s->height != mHeight) {
181 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
182 __func__, mId, s->height, mHeight);
183 return false;
184 }
185 return true;
186}
187
Alex Ray8a8f86b2013-03-01 01:32:21 -0800188int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
189{
Alex Rayea803822013-10-14 15:56:43 -0700190 ATRACE_CALL();
Alex Ray55567642013-11-12 12:58:39 -0800191 android::Mutex::Autolock al(mLock);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800192
193 if (buf_set->stream != mStream) {
194 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
195 __func__, mId, buf_set->stream, mStream);
196 return -EINVAL;
197 }
198
Alex Ray8a8f86b2013-03-01 01:32:21 -0800199 mNumBuffers = buf_set->num_buffers;
200 mBuffers = new buffer_handle_t*[mNumBuffers];
201
202 for (unsigned int i = 0; i < mNumBuffers; i++) {
203 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
204 buf_set->buffers[i]);
205 mBuffers[i] = buf_set->buffers[i];
206 // TODO: register buffers with hw, handle error cases
207 }
208 mRegistered = true;
209
Alex Ray8a8f86b2013-03-01 01:32:21 -0800210 return 0;
211}
212
Alex Ray55567642013-11-12 12:58:39 -0800213// This must only be called with mLock held
Alex Ray8a8f86b2013-03-01 01:32:21 -0800214void Stream::unregisterBuffers_L()
215{
216 mRegistered = false;
217 mNumBuffers = 0;
218 delete [] mBuffers;
219 // TODO: unregister buffers from hw
220}
221
Alex Ray69f1f912013-10-21 00:46:44 -0700222void Stream::dump(int fd)
223{
Alex Ray55567642013-11-12 12:58:39 -0800224 android::Mutex::Autolock al(mLock);
Alex Ray69f1f912013-10-21 00:46:44 -0700225
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700226 dprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
227 dprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
Dan Albert9a8c57a2014-11-27 21:39:46 -0800228 dprintf(fd, "Width: %" PRIu32 " Height: %" PRIu32 "\n", mWidth, mHeight);
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700229 dprintf(fd, "Stream Format: %s (%d)", formatToString(mFormat), mFormat);
Alex Ray69f1f912013-10-21 00:46:44 -0700230 // ToDo: prettyprint usage mask flags
Dan Albert9a8c57a2014-11-27 21:39:46 -0800231 dprintf(fd, "Gralloc Usage Mask: %#" PRIx32 "\n", mUsage);
232 dprintf(fd, "Max Buffer Count: %" PRIu32 "\n", mMaxBuffers);
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700233 dprintf(fd, "Buffers Registered: %s\n", mRegistered ? "true" : "false");
Dan Albert9a8c57a2014-11-27 21:39:46 -0800234 dprintf(fd, "Number of Buffers: %" PRIu32 "\n", mNumBuffers);
Sasha Levitskiya82f4562014-04-21 17:20:17 -0700235 for (uint32_t i = 0; i < mNumBuffers; i++) {
Dan Albert9a8c57a2014-11-27 21:39:46 -0800236 dprintf(fd, "Buffer %" PRIu32 "/%" PRIu32 ": %p\n", i, mNumBuffers,
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700237 mBuffers[i]);
Alex Ray69f1f912013-10-21 00:46:44 -0700238 }
Alex Ray69f1f912013-10-21 00:46:44 -0700239}
240
Alex Raybcaf7882013-02-28 16:04:35 -0800241} // namespace default_camera_hal