blob: a38b5903131e7c9852f7f78c658ec70632445cda [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 */
Alex Raybcaf7882013-02-28 16:04:35 -080016//#define LOG_NDEBUG 0
17#define LOG_TAG "Stream"
Mark Salyzynd88dfe82017-04-11 08:56:09 -070018
19#include <stdio.h>
20
21#include <log/log.h>
22#include <utils/Mutex.h>
Alex Raybcaf7882013-02-28 16:04:35 -080023
24#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
Alex Rayea803822013-10-14 15:56:43 -070025#include <utils/Trace.h>
Alex Raybcaf7882013-02-28 16:04:35 -080026
Mark Salyzynd88dfe82017-04-11 08:56:09 -070027#include <hardware/camera3.h>
28#include <hardware/gralloc.h>
29#include <system/graphics.h>
30
Alex Raybcaf7882013-02-28 16:04:35 -080031#include "Stream.h"
32
33namespace default_camera_hal {
34
35Stream::Stream(int id, camera3_stream_t *s)
36 : mReuse(false),
37 mId(id),
38 mStream(s),
39 mType(s->stream_type),
40 mWidth(s->width),
41 mHeight(s->height),
42 mFormat(s->format),
43 mUsage(0),
44 mMaxBuffers(0),
Alex Ray8a8f86b2013-03-01 01:32:21 -080045 mRegistered(false),
46 mBuffers(0),
47 mNumBuffers(0)
Alex Raybcaf7882013-02-28 16:04:35 -080048{
Alex Raybcaf7882013-02-28 16:04:35 -080049}
50
51Stream::~Stream()
52{
Alex Ray55567642013-11-12 12:58:39 -080053 android::Mutex::Autolock al(mLock);
Alex Ray8a8f86b2013-03-01 01:32:21 -080054 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080055}
56
57void Stream::setUsage(uint32_t usage)
58{
Alex Ray55567642013-11-12 12:58:39 -080059 android::Mutex::Autolock al(mLock);
Alex Raybcaf7882013-02-28 16:04:35 -080060 if (usage != mUsage) {
61 mUsage = usage;
Alex Ray2b286da2013-05-29 15:08:29 -070062 mStream->usage = usage;
Alex Ray8a8f86b2013-03-01 01:32:21 -080063 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080064 }
Alex Raybcaf7882013-02-28 16:04:35 -080065}
66
67void Stream::setMaxBuffers(uint32_t max_buffers)
68{
Alex Ray55567642013-11-12 12:58:39 -080069 android::Mutex::Autolock al(mLock);
Alex Raybcaf7882013-02-28 16:04:35 -080070 if (max_buffers != mMaxBuffers) {
71 mMaxBuffers = max_buffers;
Alex Ray2b286da2013-05-29 15:08:29 -070072 mStream->max_buffers = max_buffers;
Alex Ray8a8f86b2013-03-01 01:32:21 -080073 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080074 }
Alex Raybcaf7882013-02-28 16:04:35 -080075}
76
77int Stream::getType()
78{
79 return mType;
80}
81
82bool Stream::isInputType()
83{
Alex Ray768216e2013-07-02 16:56:14 -070084 return mType == CAMERA3_STREAM_INPUT ||
85 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080086}
87
88bool Stream::isOutputType()
89{
Alex Ray768216e2013-07-02 16:56:14 -070090 return mType == CAMERA3_STREAM_OUTPUT ||
91 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080092}
93
Alex Ray69f1f912013-10-21 00:46:44 -070094const char* Stream::typeToString(int type)
95{
96 switch (type) {
97 case CAMERA3_STREAM_INPUT:
98 return "CAMERA3_STREAM_INPUT";
99 case CAMERA3_STREAM_OUTPUT:
100 return "CAMERA3_STREAM_OUTPUT";
101 case CAMERA3_STREAM_BIDIRECTIONAL:
102 return "CAMERA3_STREAM_BIDIRECTIONAL";
103 }
104 return "Invalid stream type!";
105}
106
107const char* Stream::formatToString(int format)
108{
109 // See <system/graphics.h> for full list
110 switch (format) {
111 case HAL_PIXEL_FORMAT_BGRA_8888:
112 return "BGRA 8888";
113 case HAL_PIXEL_FORMAT_RGBA_8888:
114 return "RGBA 8888";
115 case HAL_PIXEL_FORMAT_RGBX_8888:
116 return "RGBX 8888";
117 case HAL_PIXEL_FORMAT_RGB_888:
118 return "RGB 888";
119 case HAL_PIXEL_FORMAT_RGB_565:
120 return "RGB 565";
Alex Ray69f1f912013-10-21 00:46:44 -0700121 case HAL_PIXEL_FORMAT_Y8:
122 return "Y8";
123 case HAL_PIXEL_FORMAT_Y16:
124 return "Y16";
125 case HAL_PIXEL_FORMAT_YV12:
126 return "YV12";
127 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
128 return "NV16";
129 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
130 return "NV21";
131 case HAL_PIXEL_FORMAT_YCbCr_422_I:
132 return "YUY2";
Eino-Ville Talvalae69efbb2015-03-06 13:19:36 -0800133 case HAL_PIXEL_FORMAT_RAW10:
134 return "RAW10";
135 case HAL_PIXEL_FORMAT_RAW16:
136 return "RAW16";
Alex Ray69f1f912013-10-21 00:46:44 -0700137 case HAL_PIXEL_FORMAT_BLOB:
138 return "BLOB";
139 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
140 return "IMPLEMENTATION DEFINED";
141 case HAL_PIXEL_FORMAT_YCbCr_420_888:
142 return "FLEXIBLE YCbCr 420 888";
143 }
144 return "Invalid stream format!";
145}
146
Alex Ray8a8f86b2013-03-01 01:32:21 -0800147bool Stream::isRegistered()
148{
149 return mRegistered;
150}
151
Alex Raybcaf7882013-02-28 16:04:35 -0800152bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
153{
154 if (id != mId) {
155 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
156 __func__, mId, id, mId);
157 return false;
158 }
159 if (s != mStream) {
160 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
161 __func__, mId, s, mStream);
162 return false;
163 }
164 if (s->stream_type != mType) {
Alex Ray69f1f912013-10-21 00:46:44 -0700165 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
166 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
167 s->stream_type, typeToString(mType), mType);
Alex Raybcaf7882013-02-28 16:04:35 -0800168 return false;
169 }
170 if (s->format != mFormat) {
Alex Ray69f1f912013-10-21 00:46:44 -0700171 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
172 "expect %s(%d)", __func__, mId, formatToString(s->format),
173 s->format, formatToString(mFormat), mFormat);
Alex Raybcaf7882013-02-28 16:04:35 -0800174 return false;
175 }
176 if (s->width != mWidth) {
177 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
178 __func__, mId, s->width, mWidth);
179 return false;
180 }
181 if (s->height != mHeight) {
182 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
183 __func__, mId, s->height, mHeight);
184 return false;
185 }
186 return true;
187}
188
Alex Ray8a8f86b2013-03-01 01:32:21 -0800189int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
190{
Alex Rayea803822013-10-14 15:56:43 -0700191 ATRACE_CALL();
Alex Ray55567642013-11-12 12:58:39 -0800192 android::Mutex::Autolock al(mLock);
Alex Ray8a8f86b2013-03-01 01:32:21 -0800193
194 if (buf_set->stream != mStream) {
195 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
196 __func__, mId, buf_set->stream, mStream);
197 return -EINVAL;
198 }
199
Alex Ray8a8f86b2013-03-01 01:32:21 -0800200 mNumBuffers = buf_set->num_buffers;
201 mBuffers = new buffer_handle_t*[mNumBuffers];
202
203 for (unsigned int i = 0; i < mNumBuffers; i++) {
204 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
205 buf_set->buffers[i]);
206 mBuffers[i] = buf_set->buffers[i];
207 // TODO: register buffers with hw, handle error cases
208 }
209 mRegistered = true;
210
Alex Ray8a8f86b2013-03-01 01:32:21 -0800211 return 0;
212}
213
Alex Ray55567642013-11-12 12:58:39 -0800214// This must only be called with mLock held
Alex Ray8a8f86b2013-03-01 01:32:21 -0800215void Stream::unregisterBuffers_L()
216{
217 mRegistered = false;
218 mNumBuffers = 0;
219 delete [] mBuffers;
220 // TODO: unregister buffers from hw
221}
222
Alex Ray69f1f912013-10-21 00:46:44 -0700223void Stream::dump(int fd)
224{
Alex Ray55567642013-11-12 12:58:39 -0800225 android::Mutex::Autolock al(mLock);
Alex Ray69f1f912013-10-21 00:46:44 -0700226
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700227 dprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
228 dprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
Dan Albert9a8c57a2014-11-27 21:39:46 -0800229 dprintf(fd, "Width: %" PRIu32 " Height: %" PRIu32 "\n", mWidth, mHeight);
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700230 dprintf(fd, "Stream Format: %s (%d)", formatToString(mFormat), mFormat);
Alex Ray69f1f912013-10-21 00:46:44 -0700231 // ToDo: prettyprint usage mask flags
Dan Albert9a8c57a2014-11-27 21:39:46 -0800232 dprintf(fd, "Gralloc Usage Mask: %#" PRIx32 "\n", mUsage);
233 dprintf(fd, "Max Buffer Count: %" PRIu32 "\n", mMaxBuffers);
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700234 dprintf(fd, "Buffers Registered: %s\n", mRegistered ? "true" : "false");
Dan Albert9a8c57a2014-11-27 21:39:46 -0800235 dprintf(fd, "Number of Buffers: %" PRIu32 "\n", mNumBuffers);
Sasha Levitskiya82f4562014-04-21 17:20:17 -0700236 for (uint32_t i = 0; i < mNumBuffers; i++) {
Dan Albert9a8c57a2014-11-27 21:39:46 -0800237 dprintf(fd, "Buffer %" PRIu32 "/%" PRIu32 ": %p\n", i, mNumBuffers,
Elliott Hughes0d1c2a42014-05-22 11:12:12 -0700238 mBuffers[i]);
Alex Ray69f1f912013-10-21 00:46:44 -0700239 }
Alex Ray69f1f912013-10-21 00:46:44 -0700240}
241
Alex Raybcaf7882013-02-28 16:04:35 -0800242} // namespace default_camera_hal