blob: 9c5c5b5072816e029303a6da4813c85264ccd13d [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
17#include <pthread.h>
18#include <hardware/camera3.h>
19#include <hardware/gralloc.h>
20#include <system/graphics.h>
21
22//#define LOG_NDEBUG 0
23#define LOG_TAG "Stream"
24#include <cutils/log.h>
25
26#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
Alex Rayea803822013-10-14 15:56:43 -070027#include <utils/Trace.h>
Alex Raybcaf7882013-02-28 16:04:35 -080028
29#include "Stream.h"
30
31namespace default_camera_hal {
32
33Stream::Stream(int id, camera3_stream_t *s)
34 : mReuse(false),
35 mId(id),
36 mStream(s),
37 mType(s->stream_type),
38 mWidth(s->width),
39 mHeight(s->height),
40 mFormat(s->format),
41 mUsage(0),
42 mMaxBuffers(0),
Alex Ray8a8f86b2013-03-01 01:32:21 -080043 mRegistered(false),
44 mBuffers(0),
45 mNumBuffers(0)
Alex Raybcaf7882013-02-28 16:04:35 -080046{
47 // NULL (default) pthread mutex attributes
48 pthread_mutex_init(&mMutex, NULL);
49}
50
51Stream::~Stream()
52{
Alex Ray8a8f86b2013-03-01 01:32:21 -080053 pthread_mutex_lock(&mMutex);
54 unregisterBuffers_L();
55 pthread_mutex_unlock(&mMutex);
Alex Raybcaf7882013-02-28 16:04:35 -080056}
57
58void Stream::setUsage(uint32_t usage)
59{
60 pthread_mutex_lock(&mMutex);
61 if (usage != mUsage) {
62 mUsage = usage;
Alex Ray2b286da2013-05-29 15:08:29 -070063 mStream->usage = usage;
Alex Ray8a8f86b2013-03-01 01:32:21 -080064 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080065 }
66 pthread_mutex_unlock(&mMutex);
67}
68
69void Stream::setMaxBuffers(uint32_t max_buffers)
70{
71 pthread_mutex_lock(&mMutex);
72 if (max_buffers != mMaxBuffers) {
73 mMaxBuffers = max_buffers;
Alex Ray2b286da2013-05-29 15:08:29 -070074 mStream->max_buffers = max_buffers;
Alex Ray8a8f86b2013-03-01 01:32:21 -080075 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080076 }
77 pthread_mutex_unlock(&mMutex);
78}
79
80int Stream::getType()
81{
82 return mType;
83}
84
85bool Stream::isInputType()
86{
Alex Ray768216e2013-07-02 16:56:14 -070087 return mType == CAMERA3_STREAM_INPUT ||
88 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080089}
90
91bool Stream::isOutputType()
92{
Alex Ray768216e2013-07-02 16:56:14 -070093 return mType == CAMERA3_STREAM_OUTPUT ||
94 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080095}
96
Alex Ray8a8f86b2013-03-01 01:32:21 -080097bool Stream::isRegistered()
98{
99 return mRegistered;
100}
101
Alex Raybcaf7882013-02-28 16:04:35 -0800102bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
103{
104 if (id != mId) {
105 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
106 __func__, mId, id, mId);
107 return false;
108 }
109 if (s != mStream) {
110 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
111 __func__, mId, s, mStream);
112 return false;
113 }
114 if (s->stream_type != mType) {
115 // TODO: prettyprint type string
116 ALOGE("%s:%d: Mismatched type in reused stream. Got %d expect %d",
117 __func__, mId, s->stream_type, mType);
118 return false;
119 }
120 if (s->format != mFormat) {
121 // TODO: prettyprint format string
122 ALOGE("%s:%d: Mismatched format in reused stream. Got %d expect %d",
123 __func__, mId, s->format, mFormat);
124 return false;
125 }
126 if (s->width != mWidth) {
127 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
128 __func__, mId, s->width, mWidth);
129 return false;
130 }
131 if (s->height != mHeight) {
132 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
133 __func__, mId, s->height, mHeight);
134 return false;
135 }
136 return true;
137}
138
Alex Ray8a8f86b2013-03-01 01:32:21 -0800139int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
140{
Alex Rayea803822013-10-14 15:56:43 -0700141 ATRACE_CALL();
Alex Ray8a8f86b2013-03-01 01:32:21 -0800142
143 if (buf_set->stream != mStream) {
144 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
145 __func__, mId, buf_set->stream, mStream);
146 return -EINVAL;
147 }
148
149 pthread_mutex_lock(&mMutex);
150
151 mNumBuffers = buf_set->num_buffers;
152 mBuffers = new buffer_handle_t*[mNumBuffers];
153
154 for (unsigned int i = 0; i < mNumBuffers; i++) {
155 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
156 buf_set->buffers[i]);
157 mBuffers[i] = buf_set->buffers[i];
158 // TODO: register buffers with hw, handle error cases
159 }
160 mRegistered = true;
161
162 pthread_mutex_unlock(&mMutex);
163
164 return 0;
165}
166
167// This must only be called with mMutex held
168void Stream::unregisterBuffers_L()
169{
170 mRegistered = false;
171 mNumBuffers = 0;
172 delete [] mBuffers;
173 // TODO: unregister buffers from hw
174}
175
Alex Raybcaf7882013-02-28 16:04:35 -0800176} // namespace default_camera_hal