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