blob: 0834aeeaed3b6cf26aa0004a437e943332b48dcb [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 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 Ray8a8f86b2013-03-01 01:32:21 -080074 unregisterBuffers_L();
Alex Raybcaf7882013-02-28 16:04:35 -080075 }
76 pthread_mutex_unlock(&mMutex);
77}
78
79int Stream::getType()
80{
81 return mType;
82}
83
84bool Stream::isInputType()
85{
86 return mType & (CAMERA3_STREAM_INPUT | CAMERA3_STREAM_BIDIRECTIONAL);
87}
88
89bool Stream::isOutputType()
90{
91 return mType & (CAMERA3_STREAM_OUTPUT | CAMERA3_STREAM_BIDIRECTIONAL);
92}
93
Alex Ray8a8f86b2013-03-01 01:32:21 -080094bool Stream::isRegistered()
95{
96 return mRegistered;
97}
98
Alex Raybcaf7882013-02-28 16:04:35 -080099bool Stream::isValidReuseStream(int id, camera3_stream_t *s)
100{
101 if (id != mId) {
102 ALOGE("%s:%d: Invalid camera id for reuse. Got %d expect %d",
103 __func__, mId, id, mId);
104 return false;
105 }
106 if (s != mStream) {
107 ALOGE("%s:%d: Invalid stream handle for reuse. Got %p expect %p",
108 __func__, mId, s, mStream);
109 return false;
110 }
111 if (s->stream_type != mType) {
112 // TODO: prettyprint type string
113 ALOGE("%s:%d: Mismatched type in reused stream. Got %d expect %d",
114 __func__, mId, s->stream_type, mType);
115 return false;
116 }
117 if (s->format != mFormat) {
118 // TODO: prettyprint format string
119 ALOGE("%s:%d: Mismatched format in reused stream. Got %d expect %d",
120 __func__, mId, s->format, mFormat);
121 return false;
122 }
123 if (s->width != mWidth) {
124 ALOGE("%s:%d: Mismatched width in reused stream. Got %d expect %d",
125 __func__, mId, s->width, mWidth);
126 return false;
127 }
128 if (s->height != mHeight) {
129 ALOGE("%s:%d: Mismatched height in reused stream. Got %d expect %d",
130 __func__, mId, s->height, mHeight);
131 return false;
132 }
133 return true;
134}
135
Alex Ray8a8f86b2013-03-01 01:32:21 -0800136int Stream::registerBuffers(const camera3_stream_buffer_set_t *buf_set)
137{
138 CAMTRACE_CALL();
139
140 if (buf_set->stream != mStream) {
141 ALOGE("%s:%d: Buffer set for invalid stream. Got %p expect %p",
142 __func__, mId, buf_set->stream, mStream);
143 return -EINVAL;
144 }
145
146 pthread_mutex_lock(&mMutex);
147
148 mNumBuffers = buf_set->num_buffers;
149 mBuffers = new buffer_handle_t*[mNumBuffers];
150
151 for (unsigned int i = 0; i < mNumBuffers; i++) {
152 ALOGV("%s:%d: Registering buffer %p", __func__, mId,
153 buf_set->buffers[i]);
154 mBuffers[i] = buf_set->buffers[i];
155 // TODO: register buffers with hw, handle error cases
156 }
157 mRegistered = true;
158
159 pthread_mutex_unlock(&mMutex);
160
161 return 0;
162}
163
164// This must only be called with mMutex held
165void Stream::unregisterBuffers_L()
166{
167 mRegistered = false;
168 mNumBuffers = 0;
169 delete [] mBuffers;
170 // TODO: unregister buffers from hw
171}
172
Alex Raybcaf7882013-02-28 16:04:35 -0800173} // namespace default_camera_hal