blob: fdd08d6e6efbde296abee8e148ab706b6ac7e014 [file] [log] [blame]
Ari Hausman-Cohen73442152016-06-08 15:50:49 -07001/*
2 * Copyright (C) 2016 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// Modified from hardware/libhardware/modules/camera/Stream.cpp
18
19#include <stdio.h>
20#include <hardware/camera3.h>
21#include <hardware/gralloc.h>
22#include <system/graphics.h>
23#include <utils/Mutex.h>
24
25//#define LOG_NDEBUG 0
26#define LOG_TAG "Stream"
27#include <cutils/log.h>
28
29#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
30#include <utils/Trace.h>
31
32#include "Stream.h"
33
34namespace default_camera_hal {
35
36Stream::Stream(int id, camera3_stream_t *s)
37 : mReuse(false),
38 mId(id),
39 mStream(s),
40 mType(s->stream_type),
41 mWidth(s->width),
42 mHeight(s->height),
43 mFormat(s->format),
44 mUsage(0),
45 mMaxBuffers(0),
46 mRegistered(false),
47 mBuffers(0),
48 mNumBuffers(0)
49{
50}
51
52Stream::~Stream()
53{
54 android::Mutex::Autolock al(mLock);
55 unregisterBuffers_L();
56}
57
58void Stream::setUsage(uint32_t usage)
59{
60 android::Mutex::Autolock al(mLock);
61 if (usage != mUsage) {
62 mUsage = usage;
63 mStream->usage = usage;
64 unregisterBuffers_L();
65 }
66}
67
68void Stream::setMaxBuffers(uint32_t max_buffers)
69{
70 android::Mutex::Autolock al(mLock);
71 if (max_buffers != mMaxBuffers) {
72 mMaxBuffers = max_buffers;
73 mStream->max_buffers = max_buffers;
74 unregisterBuffers_L();
75 }
76}
77
78int Stream::getType()
79{
80 return mType;
81}
82
83bool Stream::isInputType()
84{
85 return mType == CAMERA3_STREAM_INPUT ||
86 mType == CAMERA3_STREAM_BIDIRECTIONAL;
87}
88
89bool Stream::isOutputType()
90{
91 return mType == CAMERA3_STREAM_OUTPUT ||
92 mType == CAMERA3_STREAM_BIDIRECTIONAL;
93}
94
95const char* Stream::typeToString(int type)
96{
97 switch (type) {
98 case CAMERA3_STREAM_INPUT:
99 return "CAMERA3_STREAM_INPUT";
100 case CAMERA3_STREAM_OUTPUT:
101 return "CAMERA3_STREAM_OUTPUT";
102 case CAMERA3_STREAM_BIDIRECTIONAL:
103 return "CAMERA3_STREAM_BIDIRECTIONAL";
104 }
105 return "Invalid stream type!";
106}
107
108const char* Stream::formatToString(int format)
109{
110 // See <system/graphics.h> for full list
111 switch (format) {
112 case HAL_PIXEL_FORMAT_BGRA_8888:
113 return "BGRA 8888";
114 case HAL_PIXEL_FORMAT_RGBA_8888:
115 return "RGBA 8888";
116 case HAL_PIXEL_FORMAT_RGBX_8888:
117 return "RGBX 8888";
118 case HAL_PIXEL_FORMAT_RGB_888:
119 return "RGB 888";
120 case HAL_PIXEL_FORMAT_RGB_565:
121 return "RGB 565";
122 case HAL_PIXEL_FORMAT_Y8:
123 return "Y8";
124 case HAL_PIXEL_FORMAT_Y16:
125 return "Y16";
126 case HAL_PIXEL_FORMAT_YV12:
127 return "YV12";
128 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
129 return "NV16";
130 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
131 return "NV21";
132 case HAL_PIXEL_FORMAT_YCbCr_422_I:
133 return "YUY2";
134 case HAL_PIXEL_FORMAT_RAW10:
135 return "RAW10";
136 case HAL_PIXEL_FORMAT_RAW16:
137 return "RAW16";
138 case HAL_PIXEL_FORMAT_BLOB:
139 return "BLOB";
140 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
141 return "IMPLEMENTATION DEFINED";
142 case HAL_PIXEL_FORMAT_YCbCr_420_888:
143 return "FLEXIBLE YCbCr 420 888";
144 }
145 return "Invalid stream format!";
146}
147
148bool Stream::isRegistered()
149{
150 return mRegistered;
151}
152
153bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
154{
155 if (id != mId) {
156 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
157 __func__, mId, id, mId);
158 return false;
159 }
160 if (s != mStream) {
161 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
162 __func__, mId, s, mStream);
163 return false;
164 }
165 if (s->stream_type != mType) {
166 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
167 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
168 s->stream_type, typeToString(mType), mType);
169 return false;
170 }
171 if (s->format != mFormat) {
172 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
173 "expect %s(%d)", __func__, mId, formatToString(s->format),
174 s->format, formatToString(mFormat), mFormat);
175 return false;
176 }
177 if (s->width != mWidth) {
178 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
179 __func__, mId, s->width, mWidth);
180 return false;
181 }
182 if (s->height != mHeight) {
183 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
184 __func__, mId, s->height, mHeight);
185 return false;
186 }
187 return true;
188}
189
190int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
191{
192 ATRACE_CALL();
193 android::Mutex::Autolock al(mLock);
194
195 if (buf_set->stream != mStream) {
196 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
197 __func__, mId, buf_set->stream, mStream);
198 return -EINVAL;
199 }
200
201 mNumBuffers = buf_set->num_buffers;
202 mBuffers = new buffer_handle_t*[mNumBuffers];
203
204 for (unsigned int i = 0; i < mNumBuffers; i++) {
205 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
206 buf_set->buffers[i]);
207 mBuffers[i] = buf_set->buffers[i];
208 // TODO: register buffers with hw, handle error cases
209 }
210 mRegistered = true;
211
212 return 0;
213}
214
215// This must only be called with mLock held
216void Stream::unregisterBuffers_L()
217{
218 mRegistered = false;
219 mNumBuffers = 0;
220 delete [] mBuffers;
221 // TODO: unregister buffers from hw
222}
223
224void Stream::dump(int fd)
225{
226 android::Mutex::Autolock al(mLock);
227
228 dprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
229 dprintf(fd, "Stream Type: %s (%d)\n", typeToString(mType), mType);
230 dprintf(fd, "Width: %" PRIu32 " Height: %" PRIu32 "\n", mWidth, mHeight);
231 dprintf(fd, "Stream Format: %s (%d)", formatToString(mFormat), mFormat);
232 // ToDo: prettyprint usage mask flags
233 dprintf(fd, "Gralloc Usage Mask: %#" PRIx32 "\n", mUsage);
234 dprintf(fd, "Max Buffer Count: %" PRIu32 "\n", mMaxBuffers);
235 dprintf(fd, "Buffers Registered: %s\n", mRegistered ? "true" : "false");
236 dprintf(fd, "Number of Buffers: %" PRIu32 "\n", mNumBuffers);
237 for (uint32_t i = 0; i < mNumBuffers; i++) {
238 dprintf(fd, "Buffer %" PRIu32 "/%" PRIu32 ": %p\n", i, mNumBuffers,
239 mBuffers[i]);
240 }
241}
242
243} // namespace default_camera_hal