1 /*
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
32 namespace usb_camera_hal {
33
Stream(int id,camera3_stream_t * s)34 Stream::Stream(int id, camera3_stream_t *s)
35 : mReuse(false),
36 mId(id),
37 mStream(s){
38 }
39
~Stream()40 Stream::~Stream() {
41 for (size_t i = 0; i < mBuffers.size(); i++) {
42 delete mBuffers[i];
43 }
44
45 mBuffers.clear();
46 }
47
setUsage(uint32_t usage)48 void Stream::setUsage(uint32_t usage) {
49 android::Mutex::Autolock al(mLock);
50 if (usage != mStream->usage) {
51 mStream->usage = usage;
52 }
53 }
54
setMaxBuffers(uint32_t max_buffers)55 void 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
getType()62 int Stream::getType() {
63 return mStream->stream_type;
64 }
65
isInputType()66 bool Stream::isInputType() {
67 return mStream->stream_type == CAMERA3_STREAM_INPUT ||
68 mStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
69 }
70
isOutputType()71 bool Stream::isOutputType() {
72 return mStream->stream_type == CAMERA3_STREAM_OUTPUT ||
73 mStream->stream_type == CAMERA3_STREAM_BIDIRECTIONAL;
74 }
75
typeToString(int type)76 const 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
formatToString(int format)88 const 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_Y8:
102 return "Y8";
103 case HAL_PIXEL_FORMAT_Y16:
104 return "Y16";
105 case HAL_PIXEL_FORMAT_YV12:
106 return "YV12";
107 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
108 return "NV16";
109 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
110 return "NV21";
111 case HAL_PIXEL_FORMAT_YCbCr_422_I:
112 return "YUY2";
113 case HAL_PIXEL_FORMAT_RAW10:
114 return "RAW10";
115 case HAL_PIXEL_FORMAT_RAW16:
116 return "RAW16";
117 case HAL_PIXEL_FORMAT_BLOB:
118 return "BLOB";
119 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
120 return "IMPLEMENTATION DEFINED";
121 case HAL_PIXEL_FORMAT_YCbCr_420_888:
122 return "FLEXIBLE YCbCr 420 888";
123 }
124 return "Invalid stream format!";
125 }
126
isValidReuseStream(int id,camera3_stream_t * s)127 bool Stream::isValidReuseStream(int id, camera3_stream_t *s) {
128 if (id != mId) {
129 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
130 __func__, mId, id, mId);
131 return false;
132 }
133 if (s != mStream) {
134 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
135 __func__, mId, s, mStream);
136 return false;
137 }
138 if (s->stream_type != mStream->stream_type) {
139 ALOGE("%s:%d: Mismatched type in reused stream. Got %s(%d) "
140 "expect %s(%d)", __func__, mId, typeToString(s->stream_type),
141 s->stream_type, typeToString(mStream->stream_type), mStream->stream_type);
142 return false;
143 }
144 if (s->format != mStream->format) {
145 ALOGE("%s:%d: Mismatched format in reused stream. Got %s(%d) "
146 "expect %s(%d)", __func__, mId, formatToString(s->format),
147 s->format, formatToString(mStream->format), mStream->format);
148 return false;
149 }
150 if (s->width != mStream->width) {
151 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
152 __func__, mId, s->width, mStream->width);
153 return false;
154 }
155 if (s->height != mStream->height) {
156 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
157 __func__, mId, s->height, mStream->height);
158 return false;
159 }
160 return true;
161 }
162
dump(int fd)163 void Stream::dump(int fd) {
164 android::Mutex::Autolock al(mLock);
165
166 dprintf(fd, "Stream ID: %d (%p)\n", mId, mStream);
167 dprintf(fd, "Stream Type: %s (%d)\n", typeToString(mStream->stream_type), mStream->stream_type);
168 dprintf(fd, "Width: %" PRIu32 " Height: %" PRIu32 "\n", mStream->width, mStream->height);
169 dprintf(fd, "Stream Format: %s (%d)", formatToString(mStream->format), mStream->format);
170 // ToDo: prettyprint usage mask flags
171 dprintf(fd, "Gralloc Usage Mask: %#" PRIx32 "\n", mStream->usage);
172 dprintf(fd, "Max Buffer Count: %" PRIu32 "\n", mStream->max_buffers);
173 dprintf(fd, "Number of Buffers in use by HAL: %zu\n", mBuffers.size());
174 for (size_t i = 0; i < mBuffers.size(); i++) {
175 dprintf(fd, "Buffer %zu/%zu: %p\n", i, mBuffers.size(), mBuffers[i]);
176 }
177 }
178
179 } // namespace usb_camera_hal
180