blob: aae7adb859f0488bfe623d34eac57b40d4f5f957 [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)
27#include <cutils/trace.h>
28#include "ScopedTrace.h"
29
30#include "Stream.h"
31
32namespace default_camera_hal {
33
34Stream::Stream(int id, camera3_stream_t *s)
35 : mReuse(false),
36 mId(id),
37 mStream(s),
38 mType(s->stream_type),
39 mWidth(s->width),
40 mHeight(s->height),
41 mFormat(s->format),
42 mUsage(0),
43 mMaxBuffers(0),
Alex Ray8a8f86b2013-03-01 01:32:21 -080044 mRegistered(false),
45 mBuffers(0),
46 mNumBuffers(0)
Alex Raybcaf7882013-02-28 16:04:35 -080047{
48 // NULL (default) pthread mutex attributes
49 pthread_mutex_init(&mMutex, NULL);
50}
51
52Stream::~Stream()
53{
Alex Ray8a8f86b2013-03-01 01:32:21 -080054 pthread_mutex_lock(&mMutex);
55 unregisterBuffers_L();
56 pthread_mutex_unlock(&mMutex);
Alex Raybcaf7882013-02-28 16:04:35 -080057}
58
59void Stream::setUsage(uint32_t usage)
60{
61 pthread_mutex_lock(&mMutex);
62 if (usage != mUsage) {
63 mUsage = usage;
Alex Ray2b286da2013-05-29 15:08:29 -070064 mStream->usage = usage;
Alex Ray8a8f86b2013-03-01 01:32:21 -080065 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080066 }
67 pthread_mutex_unlock(&mMutex);
68}
69
70void Stream::setMaxBuffers(uint32_t max_buffers)
71{
72 pthread_mutex_lock(&mMutex);
73 if (max_buffers != mMaxBuffers) {
74 mMaxBuffers = max_buffers;
Alex Ray2b286da2013-05-29 15:08:29 -070075 mStream->max_buffers = max_buffers;
Alex Ray8a8f86b2013-03-01 01:32:21 -080076 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080077 }
78 pthread_mutex_unlock(&mMutex);
79}
80
81int Stream::getType()
82{
83 return mType;
84}
85
86bool Stream::isInputType()
87{
Alex Ray768216e2013-07-02 16:56:14 -070088 return mType == CAMERA3_STREAM_INPUT ||
89 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080090}
91
92bool Stream::isOutputType()
93{
Alex Ray768216e2013-07-02 16:56:14 -070094 return mType == CAMERA3_STREAM_OUTPUT ||
95 mType == CAMERA3_STREAM_BIDIRECTIONAL;
Alex Raybcaf7882013-02-28 16:04:35 -080096}
97
Alex Ray8a8f86b2013-03-01 01:32:21 -080098bool Stream::isRegistered()
99{
100 return mRegistered;
101}
102
Alex Raybcaf7882013-02-28 16:04:35 -0800103bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
104{
105 if (id != mId) {
106 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
107 __func__, mId, id, mId);
108 return false;
109 }
110 if (s != mStream) {
111 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
112 __func__, mId, s, mStream);
113 return false;
114 }
115 if (s->stream_type != mType) {
116 // TODO: prettyprint type string
117 ALOGE("%s:%d: Mismatched type in reused stream. Got %d expect %d",
118 __func__, mId, s->stream_type, mType);
119 return false;
120 }
121 if (s->format != mFormat) {
122 // TODO: prettyprint format string
123 ALOGE("%s:%d: Mismatched format in reused stream. Got %d expect %d",
124 __func__, mId, s->format, mFormat);
125 return false;
126 }
127 if (s->width != mWidth) {
128 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
129 __func__, mId, s->width, mWidth);
130 return false;
131 }
132 if (s->height != mHeight) {
133 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
134 __func__, mId, s->height, mHeight);
135 return false;
136 }
137 return true;
138}
139
Alex Ray8a8f86b2013-03-01 01:32:21 -0800140int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
141{
142 CAMTRACE_CALL();
143
144 if (buf_set->stream != mStream) {
145 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
146 __func__, mId, buf_set->stream, mStream);
147 return -EINVAL;
148 }
149
150 pthread_mutex_lock(&mMutex);
151
152 mNumBuffers = buf_set->num_buffers;
153 mBuffers = new buffer_handle_t*[mNumBuffers];
154
155 for (unsigned int i = 0; i < mNumBuffers; i++) {
156 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
157 buf_set->buffers[i]);
158 mBuffers[i] = buf_set->buffers[i];
159 // TODO: register buffers with hw, handle error cases
160 }
161 mRegistered = true;
162
163 pthread_mutex_unlock(&mMutex);
164
165 return 0;
166}
167
168// This must only be called with mMutex held
169void Stream::unregisterBuffers_L()
170{
171 mRegistered = false;
172 mNumBuffers = 0;
173 delete [] mBuffers;
174 // TODO: unregister buffers from hw
175}
176
Alex Raybcaf7882013-02-28 16:04:35 -0800177} // namespace default_camera_hal