blob: 0d6a96c7cd57604dae94f14aa5873aad7d522ab2 [file] [log] [blame]
Shuzhen Wang0129d522016-10-30 22:43:41 -07001/*
2 * Copyright (C) 2016 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 "Camera3SharedOutputStream.h"
18
19namespace android {
20
21namespace camera3 {
22
23Camera3SharedOutputStream::Camera3SharedOutputStream(int id,
24 const std::vector<sp<Surface>>& surfaces,
Shuzhen Wang0129d522016-10-30 22:43:41 -070025 uint32_t width, uint32_t height, int format,
26 uint32_t consumerUsage, android_dataspace dataSpace,
27 camera3_stream_rotation_t rotation,
28 nsecs_t timestampOffset, int setId) :
29 Camera3OutputStream(id, CAMERA3_STREAM_OUTPUT, width, height,
30 format, dataSpace, rotation, consumerUsage,
31 timestampOffset, setId),
Shuzhen Wang758c2152017-01-10 18:26:18 -080032 mSurfaces(surfaces) {
Shuzhen Wang0129d522016-10-30 22:43:41 -070033}
34
35Camera3SharedOutputStream::~Camera3SharedOutputStream() {
36 disconnectLocked();
37}
38
39status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
40 status_t res = OK;
41
42 mStreamSplitter = new Camera3StreamSplitter();
43
44 uint32_t usage;
45 getEndpointUsage(&usage);
46
47 res = mStreamSplitter->connect(mSurfaces, usage, camera3_stream::max_buffers, mConsumer);
48 if (res != OK) {
49 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
50 __FUNCTION__, strerror(-res), res);
51 return res;
52 }
53
54 return res;
55}
56
57status_t Camera3SharedOutputStream::notifyRequestedSurfaces(uint32_t /*frame_number*/,
58 const std::vector<size_t>& surface_ids) {
59 Mutex::Autolock l(mLock);
60 status_t res = OK;
61
62 if (mStreamSplitter != nullptr) {
63 res = mStreamSplitter->notifyRequestedSurfaces(surface_ids);
64 }
65
66 return res;
67}
68
69bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
70 Mutex::Autolock l(mLock);
Shuzhen Wang758c2152017-01-10 18:26:18 -080071 return (surface_id >= mSurfaces.size());
Shuzhen Wang0129d522016-10-30 22:43:41 -070072}
73
Shuzhen Wang758c2152017-01-10 18:26:18 -080074status_t Camera3SharedOutputStream::setConsumers(const std::vector<sp<Surface>>& surfaces) {
75 if (surfaces.size() == 0) {
76 ALOGE("%s: it's illegal to set zero consumer surfaces!", __FUNCTION__);
Shuzhen Wang0129d522016-10-30 22:43:41 -070077 return INVALID_OPERATION;
78 }
79
Shuzhen Wang758c2152017-01-10 18:26:18 -080080 status_t ret = OK;
81 for (auto& surface : surfaces) {
82 if (surface == nullptr) {
83 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
84 return INVALID_OPERATION;
85 }
86
87 mSurfaces.push_back(surface);
88
89 // Only call addOutput if the splitter has been connected.
90 if (mStreamSplitter != nullptr) {
91 ret = mStreamSplitter->addOutput(surface, camera3_stream::max_buffers);
92 if (ret != OK) {
93 ALOGE("%s: addOutput failed with error code %d", __FUNCTION__, ret);
94 return ret;
95
96 }
97 }
Shuzhen Wang0129d522016-10-30 22:43:41 -070098 }
Shuzhen Wang758c2152017-01-10 18:26:18 -080099 return ret;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700100}
101
102status_t Camera3SharedOutputStream::configureQueueLocked() {
103 status_t res;
104
105 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
106 return res;
107 }
108
109 res = connectStreamSplitterLocked();
110 if (res != OK) {
111 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
112 return res;
113 }
114
115 res = configureConsumerQueueLocked();
116 if (res != OK) {
117 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
118 return res;
119 }
120
121 return OK;
122}
123
124status_t Camera3SharedOutputStream::disconnectLocked() {
125 status_t res;
126 res = Camera3OutputStream::disconnectLocked();
127
128 if (mStreamSplitter != nullptr) {
129 mStreamSplitter->disconnect();
130 }
131
132 return res;
133}
134
135status_t Camera3SharedOutputStream::getEndpointUsage(uint32_t *usage) const {
136
Shuzhen Wang758c2152017-01-10 18:26:18 -0800137 status_t res = OK;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138 uint32_t u = 0;
139
140 if (mConsumer == nullptr) {
141 // Called before shared buffer queue is constructed.
142 *usage = getPresetConsumerUsage();
143
144 for (auto surface : mSurfaces) {
145 if (surface != nullptr) {
146 res = getEndpointUsageForSurface(&u, surface);
147 *usage |= u;
148 }
149 }
150 } else {
151 // Called after shared buffer queue is constructed.
152 res = getEndpointUsageForSurface(&u, mConsumer);
153 *usage |= u;
154 }
155
156 return res;
157}
158
159} // namespace camera3
160
161} // namespace android