blob: b419e060108c0c78ef91d3eaa3f24740630aec0b [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,
25 bool hasDeferredSurface,
26 uint32_t width, uint32_t height, int format,
27 uint32_t consumerUsage, android_dataspace dataSpace,
28 camera3_stream_rotation_t rotation,
29 nsecs_t timestampOffset, int setId) :
30 Camera3OutputStream(id, CAMERA3_STREAM_OUTPUT, width, height,
31 format, dataSpace, rotation, consumerUsage,
32 timestampOffset, setId),
33 mSurfaces(surfaces),
34 mDeferred(hasDeferredSurface) {
35}
36
37Camera3SharedOutputStream::~Camera3SharedOutputStream() {
38 disconnectLocked();
39}
40
41status_t Camera3SharedOutputStream::connectStreamSplitterLocked() {
42 status_t res = OK;
43
44 mStreamSplitter = new Camera3StreamSplitter();
45
46 uint32_t usage;
47 getEndpointUsage(&usage);
48
49 res = mStreamSplitter->connect(mSurfaces, usage, camera3_stream::max_buffers, mConsumer);
50 if (res != OK) {
51 ALOGE("%s: Failed to connect to stream splitter: %s(%d)",
52 __FUNCTION__, strerror(-res), res);
53 return res;
54 }
55
56 return res;
57}
58
59status_t Camera3SharedOutputStream::notifyRequestedSurfaces(uint32_t /*frame_number*/,
60 const std::vector<size_t>& surface_ids) {
61 Mutex::Autolock l(mLock);
62 status_t res = OK;
63
64 if (mStreamSplitter != nullptr) {
65 res = mStreamSplitter->notifyRequestedSurfaces(surface_ids);
66 }
67
68 return res;
69}
70
71bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const {
72 Mutex::Autolock l(mLock);
73 return (mDeferred && surface_id >= mSurfaces.size());
74}
75
76status_t Camera3SharedOutputStream::setConsumer(sp<Surface> surface) {
77 if (surface == nullptr) {
78 ALOGE("%s: it's illegal to set a null consumer surface!", __FUNCTION__);
79 return INVALID_OPERATION;
80 }
81
82 if (!mDeferred) {
83 ALOGE("%s: Current stream isn't deferred!", __FUNCTION__);
84 return INVALID_OPERATION;
85 }
86
87 mSurfaces.push_back(surface);
88
89 return mStreamSplitter->addOutput(surface, camera3_stream::max_buffers);
90}
91
92status_t Camera3SharedOutputStream::configureQueueLocked() {
93 status_t res;
94
95 if ((res = Camera3IOStreamBase::configureQueueLocked()) != OK) {
96 return res;
97 }
98
99 res = connectStreamSplitterLocked();
100 if (res != OK) {
101 ALOGE("Cannot connect to stream splitter: %s(%d)", strerror(-res), res);
102 return res;
103 }
104
105 res = configureConsumerQueueLocked();
106 if (res != OK) {
107 ALOGE("Failed to configureConsumerQueueLocked: %s(%d)", strerror(-res), res);
108 return res;
109 }
110
111 return OK;
112}
113
114status_t Camera3SharedOutputStream::disconnectLocked() {
115 status_t res;
116 res = Camera3OutputStream::disconnectLocked();
117
118 if (mStreamSplitter != nullptr) {
119 mStreamSplitter->disconnect();
120 }
121
122 return res;
123}
124
125status_t Camera3SharedOutputStream::getEndpointUsage(uint32_t *usage) const {
126
127 status_t res;
128 uint32_t u = 0;
129
130 if (mConsumer == nullptr) {
131 // Called before shared buffer queue is constructed.
132 *usage = getPresetConsumerUsage();
133
134 for (auto surface : mSurfaces) {
135 if (surface != nullptr) {
136 res = getEndpointUsageForSurface(&u, surface);
137 *usage |= u;
138 }
139 }
140 } else {
141 // Called after shared buffer queue is constructed.
142 res = getEndpointUsageForSurface(&u, mConsumer);
143 *usage |= u;
144 }
145
146 return res;
147}
148
149} // namespace camera3
150
151} // namespace android