blob: a3fefa32d43d70a2f5d4e6d810bb359ccf5aa43d [file] [log] [blame]
Zhijun He046205c2015-01-08 17:40:00 -08001/*
2 * Copyright (C) 2015 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//#define LOG_NDEBUG 0
18#define LOG_TAG "Stream"
19#include <cutils/log.h>
20
21#include <stdio.h>
22#include <hardware/camera3.h>
23#include <hardware/gralloc.h>
24#include <system/graphics.h>
25#include <utils/Mutex.h>
26
27#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
28#include <utils/Trace.h>
29
30#include "Stream.h"
31
32namespace usb_camera_hal {
33
34Stream::Stream(int id, camera3_stream_t *s)
35 : mReuse(false),
36 mId(id),
37 mStream(s){
38}
39
40Stream::~Stream() {
41 for (size_t i = 0; i < mBuffers.size(); i++) {
42 delete mBuffers[i];
43 }
44
45 mBuffers.clear();
46}
47
48void Stream::setUsage(uint32_t usage) {
49 android::Mutex::Autolock al(mLock);
50 if (usage != mStream->usage) {
51 mStream->usage = usage;
52 }
53}
54
55void Stream::setMaxBuffers(uint32_t max_buffers) {
56 android::Mutex::Autolock al(mLock);
57 if (max_buffers != mStream->max_buffers) {
58 mStream->max_buffers = max_buffers;
59 }
60}
61
62int Stream::getType() {
63 return mStream->stream_type;
64}
65
66bool Stream::isInputType() {
67 return mStream->stream_type == CAMERA3_STREAM_INPUT ||
68 mStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
69}
70
71bool Stream::isOutputType() {
72 return mStream->stream_type == CAMERA3_STREAM_OUTPUT ||
73 mStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
74}
75
76const char* Stream::typeToString(int type) {
77 switch (type) {
78 case CAMERA3_STREAM_INPUT:
79 return "CAMERA3_STREAM_INPUT";
80 case CAMERA3_STREAM_OUTPUT:
81 return "CAMERA3_STREAM_OUTPUT";
82 case CAMERA3_STREAM_BIDIRECTIONAL:
83 return "CAMERA3_STREAM_BIDIRECTIONAL";
84 }
85 return "Invalid stream type!";
86}
87
88const char* Stream::formatToString(int format) {
89 // See <system/graphics.h> for full list
90 switch (format) {
91 case HAL_PIXEL_FORMAT_BGRA_8888:
92 return "BGRA 8888";
93 case HAL_PIXEL_FORMAT_RGBA_8888:
94 return "RGBA 8888";
95 case HAL_PIXEL_FORMAT_RGBX_8888:
96 return "RGBX 8888";
97 case HAL_PIXEL_FORMAT_RGB_888:
98 return "RGB 888";
99 case HAL_PIXEL_FORMAT_RGB_565:
100 return "RGB 565";
101 case HAL_PIXEL_FORMAT_sRGB_A_8888:
102 return "sRGB A 8888";
103 case HAL_PIXEL_FORMAT_sRGB_X_8888:
104 return "sRGB B 8888";
105 case HAL_PIXEL_FORMAT_Y8:
106 return "Y8";
107 case HAL_PIXEL_FORMAT_Y16:
108 return "Y16";
109 case HAL_PIXEL_FORMAT_YV12:
110 return "YV12";
111 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
112 return "NV16";
113 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
114 return "NV21";
115 case HAL_PIXEL_FORMAT_YCbCr_422_I:
116 return "YUY2";
117 case HAL_PIXEL_FORMAT_RAW_SENSOR:
118 return "RAW SENSOR";
119 case HAL_PIXEL_FORMAT_BLOB:
120 return "BLOB";
121 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
122 return "IMPLEMENTATION DEFINED";
123 case HAL_PIXEL_FORMAT_YCbCr_420_888:
124 return "FLEXIBLE YCbCr 420 888";
125 }
126 return "Invalid stream format!";
127}
128
129bool Stream::isValidReuseStream(int id, camera3_stream_t *s) {
130 if (id != mId) {
131 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
132 __func__, mId, id, mId);
133 return false;
134 }
135 if (s != mStream) {
136 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
137 __func__, mId, s, mStream);
138 return false;
139 }
140 if (s->stream_type != mStream->stream_type) {
141 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
142 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
143 s->stream_type, typeToString(mStream->stream_type), mStream->stream_type);
144 return false;
145 }
146 if (s->format != mStream->format) {
147 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
148 "expect %s(%d)", __func__, mId, formatToString(s->format),
149 s->format, formatToString(mStream->format), mStream->format);
150 return false;
151 }
152 if (s->width != mStream->width) {
153 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
154 __func__, mId, s->width, mStream->width);
155 return false;
156 }
157 if (s->height != mStream->height) {
158 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
159 __func__, mId, s->height, mStream->height);
160 return false;
161 }
162 return true;
163}
164
165void Stream::dump(int fd) {
166 android::Mutex::Autolock al(mLock);
167
168 dprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
169 dprintf(fd, "Stream Type: %s (%d)\n", typeToString(mStream->stream_type), mStream->stream_type);
170 dprintf(fd, "Width: %" PRIu32 " Height: %" PRIu32 "\n", mStream->width, mStream->height);
171 dprintf(fd, "Stream Format: %s (%d)", formatToString(mStream->format), mStream->format);
172 // ToDo: prettyprint usage mask flags
173 dprintf(fd, "Gralloc Usage Mask: %#" PRIx32 "\n", mStream->usage);
174 dprintf(fd, "Max Buffer Count: %" PRIu32 "\n", mStream->max_buffers);
175 dprintf(fd, "Number of Buffers in use by HAL: %" PRIu32 "\n", mBuffers.size());
176 for (size_t i = 0; i < mBuffers.size(); i++) {
177 dprintf(fd, "Buffer %" PRIu32 "/%" PRIu32 ": %p\n", i, mBuffers.size(),
178 mBuffers[i]);
179 }
180}
181
182} // namespace usb_camera_hal