Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | namespace android { |
| 20 | |
| 21 | namespace camera3 { |
| 22 | |
| 23 | Camera3SharedOutputStream::Camera3SharedOutputStream(int id, |
| 24 | const std::vector<sp<Surface>>& surfaces, |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 25 | 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 Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 32 | mSurfaces(surfaces) { |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | Camera3SharedOutputStream::~Camera3SharedOutputStream() { |
| 36 | disconnectLocked(); |
| 37 | } |
| 38 | |
| 39 | status_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 | |
| 57 | status_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 | |
| 69 | bool Camera3SharedOutputStream::isConsumerConfigurationDeferred(size_t surface_id) const { |
| 70 | Mutex::Autolock l(mLock); |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 71 | return (surface_id >= mSurfaces.size()); |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 74 | status_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 Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 77 | return INVALID_OPERATION; |
| 78 | } |
| 79 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 80 | 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 Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 98 | } |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 99 | return ret; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | status_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 | |
| 124 | status_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 | |
| 135 | status_t Camera3SharedOutputStream::getEndpointUsage(uint32_t *usage) const { |
| 136 | |
Shuzhen Wang | 758c215 | 2017-01-10 18:26:18 -0800 | [diff] [blame^] | 137 | status_t res = OK; |
Shuzhen Wang | 0129d52 | 2016-10-30 22:43:41 -0700 | [diff] [blame] | 138 | 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 |