Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "CCodec" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <sstream> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include <C2Config.h> |
| 25 | #include <C2Debug.h> |
| 26 | #include <C2ParamInternal.h> |
| 27 | #include <C2PlatformSupport.h> |
| 28 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <android/IOMXBufferSource.h> |
Pawin Vongmasa | bf69de9 | 2019-10-29 06:21:27 -0700 | [diff] [blame] | 30 | #include <android/hardware/media/c2/1.0/IInputSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 31 | #include <android/hardware/media/omx/1.0/IGraphicBufferSource.h> |
| 32 | #include <android/hardware/media/omx/1.0/IOmx.h> |
| 33 | #include <android-base/stringprintf.h> |
| 34 | #include <cutils/properties.h> |
| 35 | #include <gui/IGraphicBufferProducer.h> |
| 36 | #include <gui/Surface.h> |
| 37 | #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 38 | #include <media/omx/1.0/WOmxNode.h> |
| 39 | #include <media/openmax/OMX_Core.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 40 | #include <media/openmax/OMX_IndexExt.h> |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 41 | #include <media/stagefright/omx/1.0/WGraphicBufferSource.h> |
| 42 | #include <media/stagefright/omx/OmxGraphicBufferSource.h> |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 43 | #include <media/stagefright/CCodec.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 44 | #include <media/stagefright/BufferProducerWrapper.h> |
| 45 | #include <media/stagefright/MediaCodecConstants.h> |
| 46 | #include <media/stagefright/PersistentSurface.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 47 | |
| 48 | #include "C2OMXNode.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 49 | #include "CCodecBufferChannel.h" |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 50 | #include "CCodecConfig.h" |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 51 | #include "Codec2Mapper.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 52 | #include "InputSurfaceWrapper.h" |
| 53 | |
| 54 | extern "C" android::PersistentSurface *CreateInputSurface(); |
| 55 | |
| 56 | namespace android { |
| 57 | |
| 58 | using namespace std::chrono_literals; |
| 59 | using ::android::hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer; |
| 60 | using android::base::StringPrintf; |
Pawin Vongmasa | d0f0e14 | 2018-11-15 03:36:28 -0800 | [diff] [blame] | 61 | using ::android::hardware::media::c2::V1_0::IInputSurface; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 62 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 63 | typedef hardware::media::omx::V1_0::IGraphicBufferSource HGraphicBufferSource; |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 64 | typedef CCodecConfig Config; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 65 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | class CCodecWatchdog : public AHandler { |
| 69 | private: |
| 70 | enum { |
| 71 | kWhatWatch, |
| 72 | }; |
| 73 | constexpr static int64_t kWatchIntervalUs = 3300000; // 3.3 secs |
| 74 | |
| 75 | public: |
| 76 | static sp<CCodecWatchdog> getInstance() { |
| 77 | static sp<CCodecWatchdog> instance(new CCodecWatchdog); |
| 78 | static std::once_flag flag; |
| 79 | // Call Init() only once. |
| 80 | std::call_once(flag, Init, instance); |
| 81 | return instance; |
| 82 | } |
| 83 | |
| 84 | ~CCodecWatchdog() = default; |
| 85 | |
| 86 | void watch(sp<CCodec> codec) { |
| 87 | bool shouldPost = false; |
| 88 | { |
| 89 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 90 | // If a watch message is in flight, piggy-back this instance as well. |
| 91 | // Otherwise, post a new watch message. |
| 92 | shouldPost = codecs->empty(); |
| 93 | codecs->emplace(codec); |
| 94 | } |
| 95 | if (shouldPost) { |
| 96 | ALOGV("posting watch message"); |
| 97 | (new AMessage(kWhatWatch, this))->post(kWatchIntervalUs); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | protected: |
| 102 | void onMessageReceived(const sp<AMessage> &msg) { |
| 103 | switch (msg->what()) { |
| 104 | case kWhatWatch: { |
| 105 | Mutexed<std::set<wp<CCodec>>>::Locked codecs(mCodecsToWatch); |
| 106 | ALOGV("watch for %zu codecs", codecs->size()); |
| 107 | for (auto it = codecs->begin(); it != codecs->end(); ++it) { |
| 108 | sp<CCodec> codec = it->promote(); |
| 109 | if (codec == nullptr) { |
| 110 | continue; |
| 111 | } |
| 112 | codec->initiateReleaseIfStuck(); |
| 113 | } |
| 114 | codecs->clear(); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | default: { |
| 119 | TRESPASS("CCodecWatchdog: unrecognized message"); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | CCodecWatchdog() : mLooper(new ALooper) {} |
| 126 | |
| 127 | static void Init(const sp<CCodecWatchdog> &thiz) { |
| 128 | ALOGV("Init"); |
| 129 | thiz->mLooper->setName("CCodecWatchdog"); |
| 130 | thiz->mLooper->registerHandler(thiz); |
| 131 | thiz->mLooper->start(); |
| 132 | } |
| 133 | |
| 134 | sp<ALooper> mLooper; |
| 135 | |
| 136 | Mutexed<std::set<wp<CCodec>>> mCodecsToWatch; |
| 137 | }; |
| 138 | |
| 139 | class C2InputSurfaceWrapper : public InputSurfaceWrapper { |
| 140 | public: |
| 141 | explicit C2InputSurfaceWrapper( |
| 142 | const std::shared_ptr<Codec2Client::InputSurface> &surface) : |
| 143 | mSurface(surface) { |
| 144 | } |
| 145 | |
| 146 | ~C2InputSurfaceWrapper() override = default; |
| 147 | |
| 148 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 149 | if (mConnection != nullptr) { |
| 150 | return ALREADY_EXISTS; |
| 151 | } |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 152 | return toStatusT(comp->connectToInputSurface(mSurface, &mConnection)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void disconnect() override { |
| 156 | if (mConnection != nullptr) { |
| 157 | mConnection->disconnect(); |
| 158 | mConnection = nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | status_t start() override { |
| 163 | // InputSurface does not distinguish started state |
| 164 | return OK; |
| 165 | } |
| 166 | |
| 167 | status_t signalEndOfInputStream() override { |
| 168 | C2InputSurfaceEosTuning eos(true); |
| 169 | std::vector<std::unique_ptr<C2SettingResult>> failures; |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 170 | c2_status_t err = mSurface->config({&eos}, C2_MAY_BLOCK, &failures); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 171 | if (err != C2_OK) { |
| 172 | return UNKNOWN_ERROR; |
| 173 | } |
| 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | status_t configure(Config &config __unused) { |
| 178 | // TODO |
| 179 | return OK; |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | std::shared_ptr<Codec2Client::InputSurface> mSurface; |
| 184 | std::shared_ptr<Codec2Client::InputSurfaceConnection> mConnection; |
| 185 | }; |
| 186 | |
| 187 | class GraphicBufferSourceWrapper : public InputSurfaceWrapper { |
| 188 | public: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 189 | typedef hardware::media::omx::V1_0::Status OmxStatus; |
| 190 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 191 | GraphicBufferSourceWrapper( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 192 | const sp<HGraphicBufferSource> &source, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 193 | uint32_t width, |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 194 | uint32_t height, |
| 195 | uint64_t usage) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 196 | : mSource(source), mWidth(width), mHeight(height) { |
| 197 | mDataSpace = HAL_DATASPACE_BT709; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 198 | mConfig.mUsage = usage; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 199 | } |
| 200 | ~GraphicBufferSourceWrapper() override = default; |
| 201 | |
| 202 | status_t connect(const std::shared_ptr<Codec2Client::Component> &comp) override { |
| 203 | mNode = new C2OMXNode(comp); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 204 | mOmxNode = new hardware::media::omx::V1_0::utils::TWOmxNode(mNode); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 205 | mNode->setFrameSize(mWidth, mHeight); |
| 206 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 207 | // Usage is queried during configure(), so setting it beforehand. |
| 208 | OMX_U32 usage = mConfig.mUsage & 0xFFFFFFFF; |
| 209 | (void)mNode->setParameter( |
| 210 | (OMX_INDEXTYPE)OMX_IndexParamConsumerUsageBits, |
| 211 | &usage, sizeof(usage)); |
| 212 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 213 | // NOTE: we do not use/pass through color aspects from GraphicBufferSource as we |
| 214 | // communicate that directly to the component. |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 215 | mSource->configure( |
| 216 | mOmxNode, static_cast<hardware::graphics::common::V1_0::Dataspace>(mDataSpace)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 217 | return OK; |
| 218 | } |
| 219 | |
| 220 | void disconnect() override { |
| 221 | if (mNode == nullptr) { |
| 222 | return; |
| 223 | } |
| 224 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 225 | if (source == nullptr) { |
| 226 | ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource."); |
| 227 | return; |
| 228 | } |
| 229 | source->onOmxIdle(); |
| 230 | source->onOmxLoaded(); |
| 231 | mNode.clear(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 232 | mOmxNode.clear(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 235 | status_t GetStatus(hardware::Return<OmxStatus> &&status) { |
| 236 | if (status.isOk()) { |
| 237 | return static_cast<status_t>(status.withDefault(OmxStatus::UNKNOWN_ERROR)); |
| 238 | } else if (status.isDeadObject()) { |
| 239 | return DEAD_OBJECT; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 240 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 241 | return UNKNOWN_ERROR; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | status_t start() override { |
| 245 | sp<IOMXBufferSource> source = mNode->getSource(); |
| 246 | if (source == nullptr) { |
| 247 | return NO_INIT; |
| 248 | } |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 249 | |
Wonsik Kim | 0f6b61d | 2021-01-05 18:55:22 -0800 | [diff] [blame] | 250 | size_t numSlots = 16; |
| 251 | // WORKAROUND: having more slots improve performance while consuming |
| 252 | // more memory. This is a temporary workaround to reduce memory for |
| 253 | // larger-than-4K scenario. |
| 254 | if (mWidth * mHeight > 4096 * 2340) { |
| 255 | constexpr OMX_U32 kPortIndexInput = 0; |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 256 | |
Wonsik Kim | 0f6b61d | 2021-01-05 18:55:22 -0800 | [diff] [blame] | 257 | OMX_PARAM_PORTDEFINITIONTYPE param; |
| 258 | param.nPortIndex = kPortIndexInput; |
| 259 | status_t err = mNode->getParameter(OMX_IndexParamPortDefinition, |
| 260 | ¶m, sizeof(param)); |
| 261 | if (err == OK) { |
| 262 | numSlots = param.nBufferCountActual; |
| 263 | } |
Taehwan Kim | 8b3bcdd | 2020-11-26 22:40:40 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | for (size_t i = 0; i < numSlots; ++i) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 267 | source->onInputBufferAdded(i); |
| 268 | } |
| 269 | |
| 270 | source->onOmxExecuting(); |
| 271 | return OK; |
| 272 | } |
| 273 | |
| 274 | status_t signalEndOfInputStream() override { |
| 275 | return GetStatus(mSource->signalEndOfInputStream()); |
| 276 | } |
| 277 | |
| 278 | status_t configure(Config &config) { |
| 279 | std::stringstream status; |
| 280 | status_t err = OK; |
| 281 | |
| 282 | // handle each configuration granually, in case we need to handle part of the configuration |
| 283 | // elsewhere |
| 284 | |
| 285 | // TRICKY: we do not unset frame delay repeating |
| 286 | if (config.mMinFps > 0 && config.mMinFps != mConfig.mMinFps) { |
| 287 | int64_t us = 1e6 / config.mMinFps + 0.5; |
| 288 | status_t res = GetStatus(mSource->setRepeatPreviousFrameDelayUs(us)); |
| 289 | status << " minFps=" << config.mMinFps << " => repeatDelayUs=" << us; |
| 290 | if (res != OK) { |
| 291 | status << " (=> " << asString(res) << ")"; |
| 292 | err = res; |
| 293 | } |
| 294 | mConfig.mMinFps = config.mMinFps; |
| 295 | } |
| 296 | |
| 297 | // pts gap |
| 298 | if (config.mMinAdjustedFps > 0 || config.mFixedAdjustedFps > 0) { |
| 299 | if (mNode != nullptr) { |
| 300 | OMX_PARAM_U32TYPE ptrGapParam = {}; |
| 301 | ptrGapParam.nSize = sizeof(OMX_PARAM_U32TYPE); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 302 | float gap = (config.mMinAdjustedFps > 0) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 303 | ? c2_min(INT32_MAX + 0., 1e6 / config.mMinAdjustedFps + 0.5) |
| 304 | : c2_max(0. - INT32_MAX, -1e6 / config.mFixedAdjustedFps - 0.5); |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 305 | // float -> uint32_t is undefined if the value is negative. |
| 306 | // First convert to int32_t to ensure the expected behavior. |
| 307 | ptrGapParam.nU32 = int32_t(gap); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 308 | (void)mNode->setParameter( |
| 309 | (OMX_INDEXTYPE)OMX_IndexParamMaxFrameDurationForBitrateControl, |
| 310 | &ptrGapParam, sizeof(ptrGapParam)); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // max fps |
| 315 | // TRICKY: we do not unset max fps to 0 unless using fixed fps |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 316 | if ((config.mMaxFps > 0 || (config.mFixedAdjustedFps > 0 && config.mMaxFps == -1)) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 317 | && config.mMaxFps != mConfig.mMaxFps) { |
| 318 | status_t res = GetStatus(mSource->setMaxFps(config.mMaxFps)); |
| 319 | status << " maxFps=" << config.mMaxFps; |
| 320 | if (res != OK) { |
| 321 | status << " (=> " << asString(res) << ")"; |
| 322 | err = res; |
| 323 | } |
| 324 | mConfig.mMaxFps = config.mMaxFps; |
| 325 | } |
| 326 | |
| 327 | if (config.mTimeOffsetUs != mConfig.mTimeOffsetUs) { |
| 328 | status_t res = GetStatus(mSource->setTimeOffsetUs(config.mTimeOffsetUs)); |
| 329 | status << " timeOffset " << config.mTimeOffsetUs << "us"; |
| 330 | if (res != OK) { |
| 331 | status << " (=> " << asString(res) << ")"; |
| 332 | err = res; |
| 333 | } |
| 334 | mConfig.mTimeOffsetUs = config.mTimeOffsetUs; |
| 335 | } |
| 336 | |
| 337 | if (config.mCaptureFps != mConfig.mCaptureFps || config.mCodedFps != mConfig.mCodedFps) { |
| 338 | status_t res = |
| 339 | GetStatus(mSource->setTimeLapseConfig(config.mCodedFps, config.mCaptureFps)); |
| 340 | status << " timeLapse " << config.mCaptureFps << "fps as " << config.mCodedFps << "fps"; |
| 341 | if (res != OK) { |
| 342 | status << " (=> " << asString(res) << ")"; |
| 343 | err = res; |
| 344 | } |
| 345 | mConfig.mCaptureFps = config.mCaptureFps; |
| 346 | mConfig.mCodedFps = config.mCodedFps; |
| 347 | } |
| 348 | |
| 349 | if (config.mStartAtUs != mConfig.mStartAtUs |
| 350 | || (config.mStopped != mConfig.mStopped && !config.mStopped)) { |
| 351 | status_t res = GetStatus(mSource->setStartTimeUs(config.mStartAtUs)); |
| 352 | status << " start at " << config.mStartAtUs << "us"; |
| 353 | if (res != OK) { |
| 354 | status << " (=> " << asString(res) << ")"; |
| 355 | err = res; |
| 356 | } |
| 357 | mConfig.mStartAtUs = config.mStartAtUs; |
| 358 | mConfig.mStopped = config.mStopped; |
| 359 | } |
| 360 | |
| 361 | // suspend-resume |
| 362 | if (config.mSuspended != mConfig.mSuspended) { |
| 363 | status_t res = GetStatus(mSource->setSuspend(config.mSuspended, config.mSuspendAtUs)); |
| 364 | status << " " << (config.mSuspended ? "suspend" : "resume") |
| 365 | << " at " << config.mSuspendAtUs << "us"; |
| 366 | if (res != OK) { |
| 367 | status << " (=> " << asString(res) << ")"; |
| 368 | err = res; |
| 369 | } |
| 370 | mConfig.mSuspended = config.mSuspended; |
| 371 | mConfig.mSuspendAtUs = config.mSuspendAtUs; |
| 372 | } |
| 373 | |
| 374 | if (config.mStopped != mConfig.mStopped && config.mStopped) { |
| 375 | status_t res = GetStatus(mSource->setStopTimeUs(config.mStopAtUs)); |
| 376 | status << " stop at " << config.mStopAtUs << "us"; |
| 377 | if (res != OK) { |
| 378 | status << " (=> " << asString(res) << ")"; |
| 379 | err = res; |
| 380 | } else { |
| 381 | status << " delayUs"; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 382 | hardware::Return<void> trans = mSource->getStopTimeOffsetUs( |
| 383 | [&res, &delayUs = config.mInputDelayUs]( |
| 384 | auto status, auto stopTimeOffsetUs) { |
| 385 | res = static_cast<status_t>(status); |
| 386 | delayUs = stopTimeOffsetUs; |
| 387 | }); |
| 388 | if (!trans.isOk()) { |
| 389 | res = trans.isDeadObject() ? DEAD_OBJECT : UNKNOWN_ERROR; |
| 390 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 391 | if (res != OK) { |
| 392 | status << " (=> " << asString(res) << ")"; |
| 393 | } else { |
| 394 | status << "=" << config.mInputDelayUs << "us"; |
| 395 | } |
| 396 | mConfig.mInputDelayUs = config.mInputDelayUs; |
| 397 | } |
| 398 | mConfig.mStopAtUs = config.mStopAtUs; |
| 399 | mConfig.mStopped = config.mStopped; |
| 400 | } |
| 401 | |
| 402 | // color aspects (android._color-aspects) |
| 403 | |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 404 | // consumer usage is queried earlier. |
| 405 | |
Wonsik Kim | bd55793 | 2019-07-02 15:51:20 -0700 | [diff] [blame] | 406 | if (status.str().empty()) { |
| 407 | ALOGD("ISConfig not changed"); |
| 408 | } else { |
| 409 | ALOGD("ISConfig%s", status.str().c_str()); |
| 410 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 411 | return err; |
| 412 | } |
| 413 | |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 414 | void onInputBufferDone(c2_cntr64_t index) override { |
| 415 | mNode->onInputBufferDone(index); |
| 416 | } |
| 417 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 418 | private: |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 419 | sp<HGraphicBufferSource> mSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 420 | sp<C2OMXNode> mNode; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 421 | sp<hardware::media::omx::V1_0::IOmxNode> mOmxNode; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 422 | uint32_t mWidth; |
| 423 | uint32_t mHeight; |
| 424 | Config mConfig; |
| 425 | }; |
| 426 | |
| 427 | class Codec2ClientInterfaceWrapper : public C2ComponentStore { |
| 428 | std::shared_ptr<Codec2Client> mClient; |
| 429 | |
| 430 | public: |
| 431 | Codec2ClientInterfaceWrapper(std::shared_ptr<Codec2Client> client) |
| 432 | : mClient(client) { } |
| 433 | |
| 434 | virtual ~Codec2ClientInterfaceWrapper() = default; |
| 435 | |
| 436 | virtual c2_status_t config_sm( |
| 437 | const std::vector<C2Param *> ¶ms, |
| 438 | std::vector<std::unique_ptr<C2SettingResult>> *const failures) { |
| 439 | return mClient->config(params, C2_MAY_BLOCK, failures); |
| 440 | }; |
| 441 | |
| 442 | virtual c2_status_t copyBuffer( |
| 443 | std::shared_ptr<C2GraphicBuffer>, |
| 444 | std::shared_ptr<C2GraphicBuffer>) { |
| 445 | return C2_OMITTED; |
| 446 | } |
| 447 | |
| 448 | virtual c2_status_t createComponent( |
| 449 | C2String, std::shared_ptr<C2Component> *const component) { |
| 450 | component->reset(); |
| 451 | return C2_OMITTED; |
| 452 | } |
| 453 | |
| 454 | virtual c2_status_t createInterface( |
| 455 | C2String, std::shared_ptr<C2ComponentInterface> *const interface) { |
| 456 | interface->reset(); |
| 457 | return C2_OMITTED; |
| 458 | } |
| 459 | |
| 460 | virtual c2_status_t query_sm( |
| 461 | const std::vector<C2Param *> &stackParams, |
| 462 | const std::vector<C2Param::Index> &heapParamIndices, |
| 463 | std::vector<std::unique_ptr<C2Param>> *const heapParams) const { |
| 464 | return mClient->query(stackParams, heapParamIndices, C2_MAY_BLOCK, heapParams); |
| 465 | } |
| 466 | |
| 467 | virtual c2_status_t querySupportedParams_nb( |
| 468 | std::vector<std::shared_ptr<C2ParamDescriptor>> *const params) const { |
| 469 | return mClient->querySupportedParams(params); |
| 470 | } |
| 471 | |
| 472 | virtual c2_status_t querySupportedValues_sm( |
| 473 | std::vector<C2FieldSupportedValuesQuery> &fields) const { |
| 474 | return mClient->querySupportedValues(fields, C2_MAY_BLOCK); |
| 475 | } |
| 476 | |
| 477 | virtual C2String getName() const { |
| 478 | return mClient->getName(); |
| 479 | } |
| 480 | |
| 481 | virtual std::shared_ptr<C2ParamReflector> getParamReflector() const { |
| 482 | return mClient->getParamReflector(); |
| 483 | } |
| 484 | |
| 485 | virtual std::vector<std::shared_ptr<const C2Component::Traits>> listComponents() { |
| 486 | return std::vector<std::shared_ptr<const C2Component::Traits>>(); |
| 487 | } |
| 488 | }; |
| 489 | |
| 490 | } // namespace |
| 491 | |
| 492 | // CCodec::ClientListener |
| 493 | |
| 494 | struct CCodec::ClientListener : public Codec2Client::Listener { |
| 495 | |
| 496 | explicit ClientListener(const wp<CCodec> &codec) : mCodec(codec) {} |
| 497 | |
| 498 | virtual void onWorkDone( |
| 499 | const std::weak_ptr<Codec2Client::Component>& component, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 500 | std::list<std::unique_ptr<C2Work>>& workItems) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 501 | (void)component; |
| 502 | sp<CCodec> codec(mCodec.promote()); |
| 503 | if (!codec) { |
| 504 | return; |
| 505 | } |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 506 | codec->onWorkDone(workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | virtual void onTripped( |
| 510 | const std::weak_ptr<Codec2Client::Component>& component, |
| 511 | const std::vector<std::shared_ptr<C2SettingResult>>& settingResult |
| 512 | ) override { |
| 513 | // TODO |
| 514 | (void)component; |
| 515 | (void)settingResult; |
| 516 | } |
| 517 | |
| 518 | virtual void onError( |
| 519 | const std::weak_ptr<Codec2Client::Component>& component, |
| 520 | uint32_t errorCode) override { |
Praveen Chavan | 72eff01 | 2020-11-20 23:20:28 -0800 | [diff] [blame^] | 521 | { |
| 522 | // Component is only used for reporting as we use a separate listener for each instance |
| 523 | std::shared_ptr<Codec2Client::Component> comp = component.lock(); |
| 524 | if (!comp) { |
| 525 | ALOGD("Component died with error: 0x%x", errorCode); |
| 526 | } else { |
| 527 | ALOGD("Component \"%s\" returned error: 0x%x", comp->getName().c_str(), errorCode); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // Report to MediaCodec |
| 532 | // Note: for now we do not propagate the error code to MediaCodec as we would need |
| 533 | // to translate to a MediaCodec error. |
| 534 | sp<CCodec> codec(mCodec.promote()); |
| 535 | if (!codec || !codec->mCallback) { |
| 536 | return; |
| 537 | } |
| 538 | codec->mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | virtual void onDeath( |
| 542 | const std::weak_ptr<Codec2Client::Component>& component) override { |
| 543 | { // Log the death of the component. |
| 544 | std::shared_ptr<Codec2Client::Component> comp = component.lock(); |
| 545 | if (!comp) { |
| 546 | ALOGE("Codec2 component died."); |
| 547 | } else { |
| 548 | ALOGE("Codec2 component \"%s\" died.", comp->getName().c_str()); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // Report to MediaCodec. |
| 553 | sp<CCodec> codec(mCodec.promote()); |
| 554 | if (!codec || !codec->mCallback) { |
| 555 | return; |
| 556 | } |
| 557 | codec->mCallback->onError(DEAD_OBJECT, ACTION_CODE_FATAL); |
| 558 | } |
| 559 | |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 560 | virtual void onFrameRendered(uint64_t bufferQueueId, |
| 561 | int32_t slotId, |
| 562 | int64_t timestampNs) override { |
| 563 | // TODO: implement |
| 564 | (void)bufferQueueId; |
| 565 | (void)slotId; |
| 566 | (void)timestampNs; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | virtual void onInputBufferDone( |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 570 | uint64_t frameIndex, size_t arrayIndex) override { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 571 | sp<CCodec> codec(mCodec.promote()); |
| 572 | if (codec) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 573 | codec->onInputBufferDone(frameIndex, arrayIndex); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | private: |
| 578 | wp<CCodec> mCodec; |
| 579 | }; |
| 580 | |
| 581 | // CCodecCallbackImpl |
| 582 | |
| 583 | class CCodecCallbackImpl : public CCodecCallback { |
| 584 | public: |
| 585 | explicit CCodecCallbackImpl(CCodec *codec) : mCodec(codec) {} |
| 586 | ~CCodecCallbackImpl() override = default; |
| 587 | |
| 588 | void onError(status_t err, enum ActionCode actionCode) override { |
| 589 | mCodec->mCallback->onError(err, actionCode); |
| 590 | } |
| 591 | |
| 592 | void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) override { |
| 593 | mCodec->mCallback->onOutputFramesRendered( |
| 594 | {RenderedFrameInfo(mediaTimeUs, renderTimeNs)}); |
| 595 | } |
| 596 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 597 | void onOutputBuffersChanged() override { |
| 598 | mCodec->mCallback->onOutputBuffersChanged(); |
| 599 | } |
| 600 | |
| 601 | private: |
| 602 | CCodec *mCodec; |
| 603 | }; |
| 604 | |
| 605 | // CCodec |
| 606 | |
| 607 | CCodec::CCodec() |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 608 | : mChannel(new CCodecBufferChannel(std::make_shared<CCodecCallbackImpl>(this))), |
| 609 | mConfig(new CCodecConfig) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | CCodec::~CCodec() { |
| 613 | } |
| 614 | |
| 615 | std::shared_ptr<BufferChannelBase> CCodec::getBufferChannel() { |
| 616 | return mChannel; |
| 617 | } |
| 618 | |
| 619 | status_t CCodec::tryAndReportOnError(std::function<status_t()> job) { |
| 620 | status_t err = job(); |
| 621 | if (err != C2_OK) { |
| 622 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 623 | } |
| 624 | return err; |
| 625 | } |
| 626 | |
| 627 | void CCodec::initiateAllocateComponent(const sp<AMessage> &msg) { |
| 628 | auto setAllocating = [this] { |
| 629 | Mutexed<State>::Locked state(mState); |
| 630 | if (state->get() != RELEASED) { |
| 631 | return INVALID_OPERATION; |
| 632 | } |
| 633 | state->set(ALLOCATING); |
| 634 | return OK; |
| 635 | }; |
| 636 | if (tryAndReportOnError(setAllocating) != OK) { |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | sp<RefBase> codecInfo; |
| 641 | CHECK(msg->findObject("codecInfo", &codecInfo)); |
| 642 | // For Codec 2.0 components, componentName == codecInfo->getCodecName(). |
| 643 | |
| 644 | sp<AMessage> allocMsg(new AMessage(kWhatAllocate, this)); |
| 645 | allocMsg->setObject("codecInfo", codecInfo); |
| 646 | allocMsg->post(); |
| 647 | } |
| 648 | |
| 649 | void CCodec::allocate(const sp<MediaCodecInfo> &codecInfo) { |
| 650 | if (codecInfo == nullptr) { |
| 651 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 652 | return; |
| 653 | } |
| 654 | ALOGD("allocate(%s)", codecInfo->getCodecName()); |
| 655 | mClientListener.reset(new ClientListener(this)); |
| 656 | |
| 657 | AString componentName = codecInfo->getCodecName(); |
| 658 | std::shared_ptr<Codec2Client> client; |
| 659 | |
| 660 | // set up preferred component store to access vendor store parameters |
Pawin Vongmasa | 892c81d | 2019-03-12 00:56:50 -0700 | [diff] [blame] | 661 | client = Codec2Client::CreateFromService("default"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 662 | if (client) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 663 | ALOGI("setting up '%s' as default (vendor) store", client->getServiceName().c_str()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 664 | SetPreferredCodec2ComponentStore( |
| 665 | std::make_shared<Codec2ClientInterfaceWrapper>(client)); |
| 666 | } |
| 667 | |
| 668 | std::shared_ptr<Codec2Client::Component> comp = |
| 669 | Codec2Client::CreateComponentByName( |
| 670 | componentName.c_str(), |
| 671 | mClientListener, |
| 672 | &client); |
| 673 | if (!comp) { |
| 674 | ALOGE("Failed Create component: %s", componentName.c_str()); |
| 675 | Mutexed<State>::Locked state(mState); |
| 676 | state->set(RELEASED); |
| 677 | state.unlock(); |
| 678 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 679 | state.lock(); |
| 680 | return; |
| 681 | } |
| 682 | ALOGI("Created component [%s]", componentName.c_str()); |
| 683 | mChannel->setComponent(comp); |
| 684 | auto setAllocated = [this, comp, client] { |
| 685 | Mutexed<State>::Locked state(mState); |
| 686 | if (state->get() != ALLOCATING) { |
| 687 | state->set(RELEASED); |
| 688 | return UNKNOWN_ERROR; |
| 689 | } |
| 690 | state->set(ALLOCATED); |
| 691 | state->comp = comp; |
| 692 | mClient = client; |
| 693 | return OK; |
| 694 | }; |
| 695 | if (tryAndReportOnError(setAllocated) != OK) { |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | // initialize config here in case setParameters is called prior to configure |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 700 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 701 | const std::unique_ptr<Config> &config = *configLocked; |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 702 | status_t err = config->initialize(mClient->getParamReflector(), comp); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 703 | if (err != OK) { |
| 704 | ALOGW("Failed to initialize configuration support"); |
| 705 | // TODO: report error once we complete implementation. |
| 706 | } |
| 707 | config->queryConfiguration(comp); |
| 708 | |
| 709 | mCallback->onComponentAllocated(componentName.c_str()); |
| 710 | } |
| 711 | |
| 712 | void CCodec::initiateConfigureComponent(const sp<AMessage> &format) { |
| 713 | auto checkAllocated = [this] { |
| 714 | Mutexed<State>::Locked state(mState); |
| 715 | return (state->get() != ALLOCATED) ? UNKNOWN_ERROR : OK; |
| 716 | }; |
| 717 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | sp<AMessage> msg(new AMessage(kWhatConfigure, this)); |
| 722 | msg->setMessage("format", format); |
| 723 | msg->post(); |
| 724 | } |
| 725 | |
| 726 | void CCodec::configure(const sp<AMessage> &msg) { |
| 727 | std::shared_ptr<Codec2Client::Component> comp; |
| 728 | auto checkAllocated = [this, &comp] { |
| 729 | Mutexed<State>::Locked state(mState); |
| 730 | if (state->get() != ALLOCATED) { |
| 731 | state->set(RELEASED); |
| 732 | return UNKNOWN_ERROR; |
| 733 | } |
| 734 | comp = state->comp; |
| 735 | return OK; |
| 736 | }; |
| 737 | if (tryAndReportOnError(checkAllocated) != OK) { |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | auto doConfig = [msg, comp, this]() -> status_t { |
| 742 | AString mime; |
| 743 | if (!msg->findString("mime", &mime)) { |
| 744 | return BAD_VALUE; |
| 745 | } |
| 746 | |
| 747 | int32_t encoder; |
| 748 | if (!msg->findInt32("encoder", &encoder)) { |
| 749 | encoder = false; |
| 750 | } |
| 751 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 752 | int32_t flags; |
| 753 | if (!msg->findInt32("flags", &flags)) { |
| 754 | return BAD_VALUE; |
| 755 | } |
| 756 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 757 | // TODO: read from intf() |
| 758 | if ((!encoder) != (comp->getName().find("encoder") == std::string::npos)) { |
| 759 | return UNKNOWN_ERROR; |
| 760 | } |
| 761 | |
| 762 | int32_t storeMeta; |
| 763 | if (encoder |
| 764 | && msg->findInt32("android._input-metadata-buffer-type", &storeMeta) |
| 765 | && storeMeta != kMetadataBufferTypeInvalid) { |
| 766 | if (storeMeta != kMetadataBufferTypeANWBuffer) { |
| 767 | ALOGD("Only ANW buffers are supported for legacy metadata mode"); |
| 768 | return BAD_VALUE; |
| 769 | } |
| 770 | mChannel->setMetaMode(CCodecBufferChannel::MODE_ANW); |
| 771 | } |
| 772 | |
| 773 | sp<RefBase> obj; |
| 774 | sp<Surface> surface; |
| 775 | if (msg->findObject("native-window", &obj)) { |
| 776 | surface = static_cast<Surface *>(obj.get()); |
| 777 | setSurface(surface); |
| 778 | } |
| 779 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 780 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 781 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 782 | config->mUsingSurface = surface != nullptr; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 783 | config->mBuffersBoundToCodec = ((flags & CONFIGURE_FLAG_USE_BLOCK_MODEL) == 0); |
| 784 | ALOGD("[%s] buffers are %sbound to CCodec for this session", |
| 785 | comp->getName().c_str(), config->mBuffersBoundToCodec ? "" : "not "); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 786 | |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 787 | // Enforce required parameters |
| 788 | int32_t i32; |
| 789 | float flt; |
| 790 | if (config->mDomain & Config::IS_AUDIO) { |
| 791 | if (!msg->findInt32(KEY_SAMPLE_RATE, &i32)) { |
| 792 | ALOGD("sample rate is missing, which is required for audio components."); |
| 793 | return BAD_VALUE; |
| 794 | } |
| 795 | if (!msg->findInt32(KEY_CHANNEL_COUNT, &i32)) { |
| 796 | ALOGD("channel count is missing, which is required for audio components."); |
| 797 | return BAD_VALUE; |
| 798 | } |
| 799 | if ((config->mDomain & Config::IS_ENCODER) |
| 800 | && !mime.equalsIgnoreCase(MEDIA_MIMETYPE_AUDIO_FLAC) |
| 801 | && !msg->findInt32(KEY_BIT_RATE, &i32) |
| 802 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 803 | ALOGD("bitrate is missing, which is required for audio encoders."); |
| 804 | return BAD_VALUE; |
| 805 | } |
| 806 | } |
| 807 | if (config->mDomain & (Config::IS_IMAGE | Config::IS_VIDEO)) { |
| 808 | if (!msg->findInt32(KEY_WIDTH, &i32)) { |
| 809 | ALOGD("width is missing, which is required for image/video components."); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | if (!msg->findInt32(KEY_HEIGHT, &i32)) { |
| 813 | ALOGD("height is missing, which is required for image/video components."); |
| 814 | return BAD_VALUE; |
| 815 | } |
| 816 | if ((config->mDomain & Config::IS_ENCODER) && (config->mDomain & Config::IS_VIDEO)) { |
Harish Mahendrakar | 71cbb9d | 2019-05-21 11:21:27 -0700 | [diff] [blame] | 817 | int32_t mode = BITRATE_MODE_VBR; |
| 818 | if (msg->findInt32(KEY_BITRATE_MODE, &mode) && mode == BITRATE_MODE_CQ) { |
Harish Mahendrakar | 817d318 | 2019-03-11 16:37:47 -0700 | [diff] [blame] | 819 | if (!msg->findInt32(KEY_QUALITY, &i32)) { |
| 820 | ALOGD("quality is missing, which is required for video encoders in CQ."); |
| 821 | return BAD_VALUE; |
| 822 | } |
| 823 | } else { |
| 824 | if (!msg->findInt32(KEY_BIT_RATE, &i32) |
| 825 | && !msg->findFloat(KEY_BIT_RATE, &flt)) { |
| 826 | ALOGD("bitrate is missing, which is required for video encoders."); |
| 827 | return BAD_VALUE; |
| 828 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 829 | } |
| 830 | if (!msg->findInt32(KEY_I_FRAME_INTERVAL, &i32) |
| 831 | && !msg->findFloat(KEY_I_FRAME_INTERVAL, &flt)) { |
| 832 | ALOGD("I frame interval is missing, which is required for video encoders."); |
| 833 | return BAD_VALUE; |
| 834 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 835 | if (!msg->findInt32(KEY_FRAME_RATE, &i32) |
| 836 | && !msg->findFloat(KEY_FRAME_RATE, &flt)) { |
| 837 | ALOGD("frame rate is missing, which is required for video encoders."); |
| 838 | return BAD_VALUE; |
| 839 | } |
Wonsik Kim | 1114eea | 2019-02-25 14:35:24 -0800 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 843 | /* |
| 844 | * Handle input surface configuration |
| 845 | */ |
| 846 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
| 847 | && (config->mDomain & Config::IS_ENCODER)) { |
| 848 | config->mISConfig.reset(new InputSurfaceWrapper::Config{}); |
| 849 | { |
| 850 | config->mISConfig->mMinFps = 0; |
| 851 | int64_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 852 | if (msg->findInt64(KEY_REPEAT_PREVIOUS_FRAME_AFTER, &value) && value > 0) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 853 | config->mISConfig->mMinFps = 1e6 / value; |
| 854 | } |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 855 | if (!msg->findFloat( |
| 856 | KEY_MAX_FPS_TO_ENCODER, &config->mISConfig->mMaxFps)) { |
| 857 | config->mISConfig->mMaxFps = -1; |
| 858 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 859 | config->mISConfig->mMinAdjustedFps = 0; |
| 860 | config->mISConfig->mFixedAdjustedFps = 0; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 861 | if (msg->findInt64(KEY_MAX_PTS_GAP_TO_ENCODER, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 862 | if (value < 0 && value >= INT32_MIN) { |
| 863 | config->mISConfig->mFixedAdjustedFps = -1e6 / value; |
Wonsik Kim | 95ba016 | 2019-03-19 15:51:54 -0700 | [diff] [blame] | 864 | config->mISConfig->mMaxFps = -1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 865 | } else if (value > 0 && value <= INT32_MAX) { |
| 866 | config->mISConfig->mMinAdjustedFps = 1e6 / value; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | { |
Wonsik Kim | 8e55f3a | 2019-09-03 14:10:37 -0700 | [diff] [blame] | 872 | bool captureFpsFound = false; |
| 873 | double timeLapseFps; |
| 874 | float captureRate; |
| 875 | if (msg->findDouble("time-lapse-fps", &timeLapseFps)) { |
| 876 | config->mISConfig->mCaptureFps = timeLapseFps; |
| 877 | captureFpsFound = true; |
| 878 | } else if (msg->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) { |
| 879 | config->mISConfig->mCaptureFps = captureRate; |
| 880 | captureFpsFound = true; |
| 881 | } |
| 882 | if (captureFpsFound) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 883 | (void)msg->findAsFloat(KEY_FRAME_RATE, &config->mISConfig->mCodedFps); |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | { |
| 888 | config->mISConfig->mSuspended = false; |
| 889 | config->mISConfig->mSuspendAtUs = -1; |
| 890 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 891 | if (msg->findInt32(KEY_CREATE_INPUT_SURFACE_SUSPENDED, &value) && value) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 892 | config->mISConfig->mSuspended = true; |
| 893 | } |
| 894 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 895 | config->mISConfig->mUsage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Handle desired color format. |
| 900 | */ |
| 901 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
| 902 | int32_t format = -1; |
| 903 | if (!msg->findInt32(KEY_COLOR_FORMAT, &format)) { |
| 904 | /* |
| 905 | * Also handle default color format (encoders require color format, so this is only |
| 906 | * needed for decoders. |
| 907 | */ |
| 908 | if (!(config->mDomain & Config::IS_ENCODER)) { |
| 909 | format = (surface == nullptr) ? COLOR_FormatYUV420Planar : COLOR_FormatSurface; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | if (format >= 0) { |
| 914 | msg->setInt32("android._color-format", format); |
| 915 | } |
| 916 | } |
| 917 | |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 918 | int32_t subscribeToAllVendorParams; |
| 919 | if (msg->findInt32("x-*", &subscribeToAllVendorParams) && subscribeToAllVendorParams) { |
| 920 | if (config->subscribeToAllVendorParams(comp, C2_MAY_BLOCK) != OK) { |
| 921 | ALOGD("[%s] Failed to subscribe to all vendor params", comp->getName().c_str()); |
| 922 | } |
| 923 | } |
| 924 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 925 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 926 | // NOTE: We used to ignore "video-bitrate" at configure; replicate |
| 927 | // the behavior here. |
| 928 | sp<AMessage> sdkParams = msg; |
| 929 | int32_t videoBitrate; |
| 930 | if (sdkParams->findInt32(PARAMETER_KEY_VIDEO_BITRATE, &videoBitrate)) { |
| 931 | sdkParams = msg->dup(); |
| 932 | sdkParams->removeEntryAt(sdkParams->findEntryByName(PARAMETER_KEY_VIDEO_BITRATE)); |
| 933 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 934 | status_t err = config->getConfigUpdateFromSdkParams( |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 935 | comp, sdkParams, Config::IS_CONFIG, C2_DONT_BLOCK, &configUpdate); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 936 | if (err != OK) { |
| 937 | ALOGW("failed to convert configuration to c2 params"); |
| 938 | } |
Wonsik Kim | aab2eea | 2019-05-22 10:37:58 -0700 | [diff] [blame] | 939 | |
| 940 | int32_t maxBframes = 0; |
| 941 | if ((config->mDomain & Config::IS_ENCODER) |
| 942 | && (config->mDomain & Config::IS_VIDEO) |
| 943 | && sdkParams->findInt32(KEY_MAX_B_FRAMES, &maxBframes) |
| 944 | && maxBframes > 0) { |
| 945 | std::unique_ptr<C2StreamGopTuning::output> gop = |
| 946 | C2StreamGopTuning::output::AllocUnique(2 /* flexCount */, 0u /* stream */); |
| 947 | gop->m.values[0] = { P_FRAME, UINT32_MAX }; |
| 948 | gop->m.values[1] = { |
| 949 | C2Config::picture_type_t(P_FRAME | B_FRAME), |
| 950 | uint32_t(maxBframes) |
| 951 | }; |
| 952 | configUpdate.push_back(std::move(gop)); |
| 953 | } |
| 954 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 955 | err = config->setParameters(comp, configUpdate, C2_DONT_BLOCK); |
| 956 | if (err != OK) { |
| 957 | ALOGW("failed to configure c2 params"); |
| 958 | return err; |
| 959 | } |
| 960 | |
| 961 | std::vector<std::unique_ptr<C2Param>> params; |
| 962 | C2StreamUsageTuning::input usage(0u, 0u); |
| 963 | C2StreamMaxBufferSizeInfo::input maxInputSize(0u, 0u); |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 964 | C2PrependHeaderModeSetting prepend(PREPEND_HEADER_TO_NONE); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 965 | |
| 966 | std::initializer_list<C2Param::Index> indices { |
| 967 | }; |
| 968 | c2_status_t c2err = comp->query( |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 969 | { &usage, &maxInputSize, &prepend }, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 970 | indices, |
| 971 | C2_DONT_BLOCK, |
| 972 | ¶ms); |
| 973 | if (c2err != C2_OK && c2err != C2_BAD_INDEX) { |
| 974 | ALOGE("Failed to query component interface: %d", c2err); |
| 975 | return UNKNOWN_ERROR; |
| 976 | } |
| 977 | if (params.size() != indices.size()) { |
| 978 | ALOGE("Component returns wrong number of params: expected %zu actual %zu", |
| 979 | indices.size(), params.size()); |
| 980 | return UNKNOWN_ERROR; |
| 981 | } |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 982 | if (usage) { |
| 983 | if (usage.value & C2MemoryUsage::CPU_READ) { |
| 984 | config->mInputFormat->setInt32("using-sw-read-often", true); |
| 985 | } |
| 986 | if (config->mISConfig) { |
| 987 | C2AndroidMemoryUsage androidUsage(C2MemoryUsage(usage.value)); |
| 988 | config->mISConfig->mUsage = androidUsage.asGrallocUsage(); |
| 989 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | // NOTE: we don't blindly use client specified input size if specified as clients |
| 993 | // at times specify too small size. Instead, mimic the behavior from OMX, where the |
| 994 | // client specified size is only used to ask for bigger buffers than component suggested |
| 995 | // size. |
| 996 | int32_t clientInputSize = 0; |
| 997 | bool clientSpecifiedInputSize = |
| 998 | msg->findInt32(KEY_MAX_INPUT_SIZE, &clientInputSize) && clientInputSize > 0; |
| 999 | // TEMP: enforce minimum buffer size of 1MB for video decoders |
| 1000 | // and 16K / 4K for audio encoders/decoders |
| 1001 | if (maxInputSize.value == 0) { |
| 1002 | if (config->mDomain & Config::IS_AUDIO) { |
| 1003 | maxInputSize.value = encoder ? 16384 : 4096; |
| 1004 | } else if (!encoder) { |
| 1005 | maxInputSize.value = 1048576u; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | // verify that CSD fits into this size (if defined) |
| 1010 | if ((config->mDomain & Config::IS_DECODER) && maxInputSize.value > 0) { |
| 1011 | sp<ABuffer> csd; |
| 1012 | for (size_t ix = 0; msg->findBuffer(StringPrintf("csd-%zu", ix).c_str(), &csd); ++ix) { |
| 1013 | if (csd && csd->size() > maxInputSize.value) { |
| 1014 | maxInputSize.value = csd->size(); |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | // TODO: do this based on component requiring linear allocator for input |
| 1020 | if ((config->mDomain & Config::IS_DECODER) || (config->mDomain & Config::IS_AUDIO)) { |
| 1021 | if (clientSpecifiedInputSize) { |
| 1022 | // Warn that we're overriding client's max input size if necessary. |
| 1023 | if ((uint32_t)clientInputSize < maxInputSize.value) { |
| 1024 | ALOGD("client requested max input size %d, which is smaller than " |
| 1025 | "what component recommended (%u); overriding with component " |
| 1026 | "recommendation.", clientInputSize, maxInputSize.value); |
| 1027 | ALOGW("This behavior is subject to change. It is recommended that " |
| 1028 | "app developers double check whether the requested " |
| 1029 | "max input size is in reasonable range."); |
| 1030 | } else { |
| 1031 | maxInputSize.value = clientInputSize; |
| 1032 | } |
| 1033 | } |
| 1034 | // Pass max input size on input format to the buffer channel (if supplied by the |
| 1035 | // component or by a default) |
| 1036 | if (maxInputSize.value) { |
| 1037 | config->mInputFormat->setInt32( |
| 1038 | KEY_MAX_INPUT_SIZE, |
| 1039 | (int32_t)(c2_min(maxInputSize.value, uint32_t(INT32_MAX)))); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Wonsik Kim | 9ca01d3 | 2019-04-01 14:45:47 -0700 | [diff] [blame] | 1043 | int32_t clientPrepend; |
| 1044 | if ((config->mDomain & Config::IS_VIDEO) |
| 1045 | && (config->mDomain & Config::IS_ENCODER) |
| 1046 | && msg->findInt32(KEY_PREPEND_HEADERS_TO_SYNC_FRAMES, &clientPrepend) |
| 1047 | && clientPrepend |
| 1048 | && (!prepend || prepend.value != PREPEND_HEADER_TO_ALL_SYNC)) { |
| 1049 | ALOGE("Failed to set KEY_PREPEND_HEADERS_TO_SYNC_FRAMES"); |
| 1050 | return BAD_VALUE; |
| 1051 | } |
| 1052 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1053 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE))) { |
| 1054 | // propagate HDR static info to output format for both encoders and decoders |
| 1055 | // if component supports this info, we will update from component, but only the raw port, |
| 1056 | // so don't propagate if component already filled it in. |
| 1057 | sp<ABuffer> hdrInfo; |
| 1058 | if (msg->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo) |
| 1059 | && !config->mOutputFormat->findBuffer(KEY_HDR_STATIC_INFO, &hdrInfo)) { |
| 1060 | config->mOutputFormat->setBuffer(KEY_HDR_STATIC_INFO, hdrInfo); |
| 1061 | } |
| 1062 | |
| 1063 | // Set desired color format from configuration parameter |
| 1064 | int32_t format; |
| 1065 | if (msg->findInt32("android._color-format", &format)) { |
| 1066 | if (config->mDomain & Config::IS_ENCODER) { |
| 1067 | config->mInputFormat->setInt32(KEY_COLOR_FORMAT, format); |
| 1068 | } else { |
| 1069 | config->mOutputFormat->setInt32(KEY_COLOR_FORMAT, format); |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | // propagate encoder delay and padding to output format |
| 1075 | if ((config->mDomain & Config::IS_DECODER) && (config->mDomain & Config::IS_AUDIO)) { |
| 1076 | int delay = 0; |
| 1077 | if (msg->findInt32("encoder-delay", &delay)) { |
| 1078 | config->mOutputFormat->setInt32("encoder-delay", delay); |
| 1079 | } |
| 1080 | int padding = 0; |
| 1081 | if (msg->findInt32("encoder-padding", &padding)) { |
| 1082 | config->mOutputFormat->setInt32("encoder-padding", padding); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | // set channel-mask |
| 1087 | if (config->mDomain & Config::IS_AUDIO) { |
| 1088 | int32_t mask; |
| 1089 | if (msg->findInt32(KEY_CHANNEL_MASK, &mask)) { |
| 1090 | if (config->mDomain & Config::IS_ENCODER) { |
| 1091 | config->mInputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1092 | } else { |
| 1093 | config->mOutputFormat->setInt32(KEY_CHANNEL_MASK, mask); |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | ALOGD("setup formats input: %s and output: %s", |
| 1099 | config->mInputFormat->debugString().c_str(), |
| 1100 | config->mOutputFormat->debugString().c_str()); |
| 1101 | return OK; |
| 1102 | }; |
| 1103 | if (tryAndReportOnError(doConfig) != OK) { |
| 1104 | return; |
| 1105 | } |
| 1106 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1107 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1108 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1109 | |
| 1110 | mCallback->onComponentConfigured(config->mInputFormat, config->mOutputFormat); |
| 1111 | } |
| 1112 | |
| 1113 | void CCodec::initiateCreateInputSurface() { |
| 1114 | status_t err = [this] { |
| 1115 | Mutexed<State>::Locked state(mState); |
| 1116 | if (state->get() != ALLOCATED) { |
| 1117 | return UNKNOWN_ERROR; |
| 1118 | } |
| 1119 | // TODO: read it from intf() properly. |
| 1120 | if (state->comp->getName().find("encoder") == std::string::npos) { |
| 1121 | return INVALID_OPERATION; |
| 1122 | } |
| 1123 | return OK; |
| 1124 | }(); |
| 1125 | if (err != OK) { |
| 1126 | mCallback->onInputSurfaceCreationFailed(err); |
| 1127 | return; |
| 1128 | } |
| 1129 | |
| 1130 | (new AMessage(kWhatCreateInputSurface, this))->post(); |
| 1131 | } |
| 1132 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1133 | sp<PersistentSurface> CCodec::CreateOmxInputSurface() { |
| 1134 | using namespace android::hardware::media::omx::V1_0; |
| 1135 | using namespace android::hardware::media::omx::V1_0::utils; |
| 1136 | using namespace android::hardware::graphics::bufferqueue::V1_0::utils; |
| 1137 | typedef android::hardware::media::omx::V1_0::Status OmxStatus; |
| 1138 | android::sp<IOmx> omx = IOmx::getService(); |
| 1139 | typedef android::hardware::graphics::bufferqueue::V1_0:: |
| 1140 | IGraphicBufferProducer HGraphicBufferProducer; |
| 1141 | typedef android::hardware::media::omx::V1_0:: |
| 1142 | IGraphicBufferSource HGraphicBufferSource; |
| 1143 | OmxStatus s; |
| 1144 | android::sp<HGraphicBufferProducer> gbp; |
| 1145 | android::sp<HGraphicBufferSource> gbs; |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1146 | |
Chong Zhang | c8ce1d8 | 2019-03-27 10:18:38 -0700 | [diff] [blame] | 1147 | using ::android::hardware::Return; |
| 1148 | Return<void> transStatus = omx->createInputSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1149 | [&s, &gbp, &gbs]( |
| 1150 | OmxStatus status, |
| 1151 | const android::sp<HGraphicBufferProducer>& producer, |
| 1152 | const android::sp<HGraphicBufferSource>& source) { |
| 1153 | s = status; |
| 1154 | gbp = producer; |
| 1155 | gbs = source; |
| 1156 | }); |
| 1157 | if (transStatus.isOk() && s == OmxStatus::OK) { |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1158 | return new PersistentSurface(new H2BGraphicBufferProducer(gbp), gbs); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return nullptr; |
| 1162 | } |
| 1163 | |
| 1164 | sp<PersistentSurface> CCodec::CreateCompatibleInputSurface() { |
| 1165 | sp<PersistentSurface> surface(CreateInputSurface()); |
| 1166 | |
| 1167 | if (surface == nullptr) { |
| 1168 | surface = CreateOmxInputSurface(); |
| 1169 | } |
| 1170 | |
| 1171 | return surface; |
| 1172 | } |
| 1173 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1174 | void CCodec::createInputSurface() { |
| 1175 | status_t err; |
| 1176 | sp<IGraphicBufferProducer> bufferProducer; |
| 1177 | |
| 1178 | sp<AMessage> inputFormat; |
| 1179 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1180 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1181 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1182 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1183 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1184 | inputFormat = config->mInputFormat; |
| 1185 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1186 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1189 | sp<PersistentSurface> persistentSurface = CreateCompatibleInputSurface(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1190 | sp<hidl::base::V1_0::IBase> hidlTarget = persistentSurface->getHidlTarget(); |
| 1191 | sp<IInputSurface> hidlInputSurface = IInputSurface::castFrom(hidlTarget); |
| 1192 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1193 | |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1194 | if (hidlInputSurface) { |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1195 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 1196 | std::make_shared<Codec2Client::InputSurface>(hidlInputSurface); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1197 | err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
Pawin Vongmasa | 1c75a23 | 2019-01-09 04:41:52 -0800 | [diff] [blame] | 1198 | inputSurface)); |
| 1199 | bufferProducer = inputSurface->getGraphicBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1200 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1201 | int32_t width = 0; |
| 1202 | (void)outputFormat->findInt32("width", &width); |
| 1203 | int32_t height = 0; |
| 1204 | (void)outputFormat->findInt32("height", &height); |
| 1205 | err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1206 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1207 | bufferProducer = persistentSurface->getBufferProducer(); |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1208 | } else { |
| 1209 | ALOGE("Corrupted input surface"); |
| 1210 | mCallback->onInputSurfaceCreationFailed(UNKNOWN_ERROR); |
| 1211 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | if (err != OK) { |
| 1215 | ALOGE("Failed to set up input surface: %d", err); |
| 1216 | mCallback->onInputSurfaceCreationFailed(err); |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | mCallback->onInputSurfaceCreated( |
| 1221 | inputFormat, |
| 1222 | outputFormat, |
| 1223 | new BufferProducerWrapper(bufferProducer)); |
| 1224 | } |
| 1225 | |
| 1226 | status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1227 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1228 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1229 | config->mUsingSurface = true; |
| 1230 | |
| 1231 | // we are now using surface - apply default color aspects to input format - as well as |
| 1232 | // get dataspace |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1233 | bool inputFormatChanged = config->updateFormats(Config::IS_INPUT); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1234 | ALOGD("input format %s to %s", |
| 1235 | inputFormatChanged ? "changed" : "unchanged", |
| 1236 | config->mInputFormat->debugString().c_str()); |
| 1237 | |
| 1238 | // configure dataspace |
| 1239 | static_assert(sizeof(int32_t) == sizeof(android_dataspace), "dataspace size mismatch"); |
| 1240 | android_dataspace dataSpace = HAL_DATASPACE_UNKNOWN; |
| 1241 | (void)config->mInputFormat->findInt32("android._dataspace", (int32_t*)&dataSpace); |
| 1242 | surface->setDataSpace(dataSpace); |
| 1243 | |
| 1244 | status_t err = mChannel->setInputSurface(surface); |
| 1245 | if (err != OK) { |
| 1246 | // undo input format update |
| 1247 | config->mUsingSurface = false; |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1248 | (void)config->updateFormats(Config::IS_INPUT); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1249 | return err; |
| 1250 | } |
| 1251 | config->mInputSurface = surface; |
| 1252 | |
| 1253 | if (config->mISConfig) { |
| 1254 | surface->configure(*config->mISConfig); |
| 1255 | } else { |
| 1256 | ALOGD("ISConfig: no configuration"); |
| 1257 | } |
| 1258 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1259 | return OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) { |
| 1263 | sp<AMessage> msg = new AMessage(kWhatSetInputSurface, this); |
| 1264 | msg->setObject("surface", surface); |
| 1265 | msg->post(); |
| 1266 | } |
| 1267 | |
| 1268 | void CCodec::setInputSurface(const sp<PersistentSurface> &surface) { |
| 1269 | sp<AMessage> inputFormat; |
| 1270 | sp<AMessage> outputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1271 | uint64_t usage = 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1272 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1273 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1274 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1275 | inputFormat = config->mInputFormat; |
| 1276 | outputFormat = config->mOutputFormat; |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 1277 | usage = config->mISConfig ? config->mISConfig->mUsage : 0; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1278 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1279 | sp<hidl::base::V1_0::IBase> hidlTarget = surface->getHidlTarget(); |
| 1280 | sp<IInputSurface> inputSurface = IInputSurface::castFrom(hidlTarget); |
| 1281 | sp<HGraphicBufferSource> gbs = HGraphicBufferSource::castFrom(hidlTarget); |
| 1282 | if (inputSurface) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1283 | status_t err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>( |
| 1284 | std::make_shared<Codec2Client::InputSurface>(inputSurface))); |
| 1285 | if (err != OK) { |
| 1286 | ALOGE("Failed to set up input surface: %d", err); |
| 1287 | mCallback->onInputSurfaceDeclined(err); |
| 1288 | return; |
| 1289 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1290 | } else if (gbs) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1291 | int32_t width = 0; |
| 1292 | (void)outputFormat->findInt32("width", &width); |
| 1293 | int32_t height = 0; |
| 1294 | (void)outputFormat->findInt32("height", &height); |
| 1295 | status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1296 | gbs, width, height, usage)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1297 | if (err != OK) { |
| 1298 | ALOGE("Failed to set up input surface: %d", err); |
| 1299 | mCallback->onInputSurfaceDeclined(err); |
| 1300 | return; |
| 1301 | } |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1302 | } else { |
| 1303 | ALOGE("Failed to set input surface: Corrupted surface."); |
| 1304 | mCallback->onInputSurfaceDeclined(UNKNOWN_ERROR); |
| 1305 | return; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1306 | } |
| 1307 | mCallback->onInputSurfaceAccepted(inputFormat, outputFormat); |
| 1308 | } |
| 1309 | |
| 1310 | void CCodec::initiateStart() { |
| 1311 | auto setStarting = [this] { |
| 1312 | Mutexed<State>::Locked state(mState); |
| 1313 | if (state->get() != ALLOCATED) { |
| 1314 | return UNKNOWN_ERROR; |
| 1315 | } |
| 1316 | state->set(STARTING); |
| 1317 | return OK; |
| 1318 | }; |
| 1319 | if (tryAndReportOnError(setStarting) != OK) { |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | (new AMessage(kWhatStart, this))->post(); |
| 1324 | } |
| 1325 | |
| 1326 | void CCodec::start() { |
| 1327 | std::shared_ptr<Codec2Client::Component> comp; |
| 1328 | auto checkStarting = [this, &comp] { |
| 1329 | Mutexed<State>::Locked state(mState); |
| 1330 | if (state->get() != STARTING) { |
| 1331 | return UNKNOWN_ERROR; |
| 1332 | } |
| 1333 | comp = state->comp; |
| 1334 | return OK; |
| 1335 | }; |
| 1336 | if (tryAndReportOnError(checkStarting) != OK) { |
| 1337 | return; |
| 1338 | } |
| 1339 | |
| 1340 | c2_status_t err = comp->start(); |
| 1341 | if (err != C2_OK) { |
| 1342 | mCallback->onError(toStatusT(err, C2_OPERATION_Component_start), |
| 1343 | ACTION_CODE_FATAL); |
| 1344 | return; |
| 1345 | } |
| 1346 | sp<AMessage> inputFormat; |
| 1347 | sp<AMessage> outputFormat; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1348 | status_t err2 = OK; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1349 | bool buffersBoundToCodec = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1350 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1351 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1352 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1353 | inputFormat = config->mInputFormat; |
Wonsik Kim | 274c832 | 2020-02-28 10:42:21 -0800 | [diff] [blame] | 1354 | // start triggers format dup |
| 1355 | outputFormat = config->mOutputFormat = config->mOutputFormat->dup(); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1356 | if (config->mInputSurface) { |
| 1357 | err2 = config->mInputSurface->start(); |
| 1358 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1359 | buffersBoundToCodec = config->mBuffersBoundToCodec; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1360 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1361 | if (err2 != OK) { |
| 1362 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1363 | return; |
| 1364 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1365 | err2 = mChannel->start(inputFormat, outputFormat, buffersBoundToCodec); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1366 | if (err2 != OK) { |
| 1367 | mCallback->onError(err2, ACTION_CODE_FATAL); |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | auto setRunning = [this] { |
| 1372 | Mutexed<State>::Locked state(mState); |
| 1373 | if (state->get() != STARTING) { |
| 1374 | return UNKNOWN_ERROR; |
| 1375 | } |
| 1376 | state->set(RUNNING); |
| 1377 | return OK; |
| 1378 | }; |
| 1379 | if (tryAndReportOnError(setRunning) != OK) { |
| 1380 | return; |
| 1381 | } |
| 1382 | mCallback->onStartCompleted(); |
| 1383 | |
| 1384 | (void)mChannel->requestInitialInputBuffers(); |
| 1385 | } |
| 1386 | |
| 1387 | void CCodec::initiateShutdown(bool keepComponentAllocated) { |
| 1388 | if (keepComponentAllocated) { |
| 1389 | initiateStop(); |
| 1390 | } else { |
| 1391 | initiateRelease(); |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | void CCodec::initiateStop() { |
| 1396 | { |
| 1397 | Mutexed<State>::Locked state(mState); |
| 1398 | if (state->get() == ALLOCATED |
| 1399 | || state->get() == RELEASED |
| 1400 | || state->get() == STOPPING |
| 1401 | || state->get() == RELEASING) { |
| 1402 | // We're already stopped, released, or doing it right now. |
| 1403 | state.unlock(); |
| 1404 | mCallback->onStopCompleted(); |
| 1405 | state.lock(); |
| 1406 | return; |
| 1407 | } |
| 1408 | state->set(STOPPING); |
| 1409 | } |
| 1410 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1411 | mChannel->reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1412 | (new AMessage(kWhatStop, this))->post(); |
| 1413 | } |
| 1414 | |
| 1415 | void CCodec::stop() { |
| 1416 | std::shared_ptr<Codec2Client::Component> comp; |
| 1417 | { |
| 1418 | Mutexed<State>::Locked state(mState); |
| 1419 | if (state->get() == RELEASING) { |
| 1420 | state.unlock(); |
| 1421 | // We're already stopped or release is in progress. |
| 1422 | mCallback->onStopCompleted(); |
| 1423 | state.lock(); |
| 1424 | return; |
| 1425 | } else if (state->get() != STOPPING) { |
| 1426 | state.unlock(); |
| 1427 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1428 | state.lock(); |
| 1429 | return; |
| 1430 | } |
| 1431 | comp = state->comp; |
| 1432 | } |
| 1433 | status_t err = comp->stop(); |
| 1434 | if (err != C2_OK) { |
| 1435 | // TODO: convert err into status_t |
| 1436 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1437 | } |
| 1438 | |
| 1439 | { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1440 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1441 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1442 | if (config->mInputSurface) { |
| 1443 | config->mInputSurface->disconnect(); |
| 1444 | config->mInputSurface = nullptr; |
| 1445 | } |
| 1446 | } |
| 1447 | { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1448 | Mutexed<State>::Locked state(mState); |
| 1449 | if (state->get() == STOPPING) { |
| 1450 | state->set(ALLOCATED); |
| 1451 | } |
| 1452 | } |
| 1453 | mCallback->onStopCompleted(); |
| 1454 | } |
| 1455 | |
| 1456 | void CCodec::initiateRelease(bool sendCallback /* = true */) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1457 | bool clearInputSurfaceIfNeeded = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1458 | { |
| 1459 | Mutexed<State>::Locked state(mState); |
| 1460 | if (state->get() == RELEASED || state->get() == RELEASING) { |
| 1461 | // We're already released or doing it right now. |
| 1462 | if (sendCallback) { |
| 1463 | state.unlock(); |
| 1464 | mCallback->onReleaseCompleted(); |
| 1465 | state.lock(); |
| 1466 | } |
| 1467 | return; |
| 1468 | } |
| 1469 | if (state->get() == ALLOCATING) { |
| 1470 | state->set(RELEASING); |
| 1471 | // With the altered state allocate() would fail and clean up. |
| 1472 | if (sendCallback) { |
| 1473 | state.unlock(); |
| 1474 | mCallback->onReleaseCompleted(); |
| 1475 | state.lock(); |
| 1476 | } |
| 1477 | return; |
| 1478 | } |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1479 | if (state->get() == STARTING |
| 1480 | || state->get() == RUNNING |
| 1481 | || state->get() == STOPPING) { |
| 1482 | // Input surface may have been started, so clean up is needed. |
| 1483 | clearInputSurfaceIfNeeded = true; |
| 1484 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1485 | state->set(RELEASING); |
| 1486 | } |
| 1487 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1488 | if (clearInputSurfaceIfNeeded) { |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1489 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1490 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 1491 | if (config->mInputSurface) { |
| 1492 | config->mInputSurface->disconnect(); |
| 1493 | config->mInputSurface = nullptr; |
| 1494 | } |
| 1495 | } |
| 1496 | |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1497 | mChannel->reset(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1498 | // thiz holds strong ref to this while the thread is running. |
| 1499 | sp<CCodec> thiz(this); |
| 1500 | std::thread([thiz, sendCallback] { thiz->release(sendCallback); }).detach(); |
| 1501 | } |
| 1502 | |
| 1503 | void CCodec::release(bool sendCallback) { |
| 1504 | std::shared_ptr<Codec2Client::Component> comp; |
| 1505 | { |
| 1506 | Mutexed<State>::Locked state(mState); |
| 1507 | if (state->get() == RELEASED) { |
| 1508 | if (sendCallback) { |
| 1509 | state.unlock(); |
| 1510 | mCallback->onReleaseCompleted(); |
| 1511 | state.lock(); |
| 1512 | } |
| 1513 | return; |
| 1514 | } |
| 1515 | comp = state->comp; |
| 1516 | } |
| 1517 | comp->release(); |
| 1518 | |
| 1519 | { |
| 1520 | Mutexed<State>::Locked state(mState); |
| 1521 | state->set(RELEASED); |
| 1522 | state->comp.reset(); |
| 1523 | } |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1524 | (new AMessage(kWhatRelease, this))->post(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1525 | if (sendCallback) { |
| 1526 | mCallback->onReleaseCompleted(); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | status_t CCodec::setSurface(const sp<Surface> &surface) { |
| 1531 | return mChannel->setSurface(surface); |
| 1532 | } |
| 1533 | |
| 1534 | void CCodec::signalFlush() { |
| 1535 | status_t err = [this] { |
| 1536 | Mutexed<State>::Locked state(mState); |
| 1537 | if (state->get() == FLUSHED) { |
| 1538 | return ALREADY_EXISTS; |
| 1539 | } |
| 1540 | if (state->get() != RUNNING) { |
| 1541 | return UNKNOWN_ERROR; |
| 1542 | } |
| 1543 | state->set(FLUSHING); |
| 1544 | return OK; |
| 1545 | }(); |
| 1546 | switch (err) { |
| 1547 | case ALREADY_EXISTS: |
| 1548 | mCallback->onFlushCompleted(); |
| 1549 | return; |
| 1550 | case OK: |
| 1551 | break; |
| 1552 | default: |
| 1553 | mCallback->onError(err, ACTION_CODE_FATAL); |
| 1554 | return; |
| 1555 | } |
| 1556 | |
| 1557 | mChannel->stop(); |
| 1558 | (new AMessage(kWhatFlush, this))->post(); |
| 1559 | } |
| 1560 | |
| 1561 | void CCodec::flush() { |
| 1562 | std::shared_ptr<Codec2Client::Component> comp; |
| 1563 | auto checkFlushing = [this, &comp] { |
| 1564 | Mutexed<State>::Locked state(mState); |
| 1565 | if (state->get() != FLUSHING) { |
| 1566 | return UNKNOWN_ERROR; |
| 1567 | } |
| 1568 | comp = state->comp; |
| 1569 | return OK; |
| 1570 | }; |
| 1571 | if (tryAndReportOnError(checkFlushing) != OK) { |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | std::list<std::unique_ptr<C2Work>> flushedWork; |
| 1576 | c2_status_t err = comp->flush(C2Component::FLUSH_COMPONENT, &flushedWork); |
| 1577 | { |
| 1578 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1579 | flushedWork.splice(flushedWork.end(), *queue); |
| 1580 | } |
| 1581 | if (err != C2_OK) { |
| 1582 | // TODO: convert err into status_t |
| 1583 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1584 | } |
| 1585 | |
| 1586 | mChannel->flush(flushedWork); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1587 | |
| 1588 | { |
| 1589 | Mutexed<State>::Locked state(mState); |
Iris Chang | ce521ee | 2019-07-05 16:18:54 +0800 | [diff] [blame] | 1590 | if (state->get() == FLUSHING) { |
| 1591 | state->set(FLUSHED); |
| 1592 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1593 | } |
| 1594 | mCallback->onFlushCompleted(); |
| 1595 | } |
| 1596 | |
| 1597 | void CCodec::signalResume() { |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1598 | std::shared_ptr<Codec2Client::Component> comp; |
| 1599 | auto setResuming = [this, &comp] { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1600 | Mutexed<State>::Locked state(mState); |
| 1601 | if (state->get() != FLUSHED) { |
| 1602 | return UNKNOWN_ERROR; |
| 1603 | } |
| 1604 | state->set(RESUMING); |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1605 | comp = state->comp; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1606 | return OK; |
| 1607 | }; |
| 1608 | if (tryAndReportOnError(setResuming) != OK) { |
| 1609 | return; |
| 1610 | } |
| 1611 | |
Wonsik Kim | e75a5da | 2020-02-14 17:29:03 -0800 | [diff] [blame] | 1612 | { |
| 1613 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1614 | const std::unique_ptr<Config> &config = *configLocked; |
| 1615 | config->queryConfiguration(comp); |
| 1616 | } |
| 1617 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 1618 | (void)mChannel->start(nullptr, nullptr, [&]{ |
| 1619 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1620 | const std::unique_ptr<Config> &config = *configLocked; |
| 1621 | return config->mBuffersBoundToCodec; |
| 1622 | }()); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1623 | |
| 1624 | { |
| 1625 | Mutexed<State>::Locked state(mState); |
| 1626 | if (state->get() != RESUMING) { |
| 1627 | state.unlock(); |
| 1628 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1629 | state.lock(); |
| 1630 | return; |
| 1631 | } |
| 1632 | state->set(RUNNING); |
| 1633 | } |
| 1634 | |
| 1635 | (void)mChannel->requestInitialInputBuffers(); |
| 1636 | } |
| 1637 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1638 | void CCodec::signalSetParameters(const sp<AMessage> &msg) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1639 | std::shared_ptr<Codec2Client::Component> comp; |
| 1640 | auto checkState = [this, &comp] { |
| 1641 | Mutexed<State>::Locked state(mState); |
| 1642 | if (state->get() == RELEASED) { |
| 1643 | return INVALID_OPERATION; |
| 1644 | } |
| 1645 | comp = state->comp; |
| 1646 | return OK; |
| 1647 | }; |
| 1648 | if (tryAndReportOnError(checkState) != OK) { |
| 1649 | return; |
| 1650 | } |
| 1651 | |
Wonsik Kim | aa484ac | 2019-02-13 16:54:02 -0800 | [diff] [blame] | 1652 | // NOTE: We used to ignore "bitrate" at setParameters; replicate |
| 1653 | // the behavior here. |
| 1654 | sp<AMessage> params = msg; |
| 1655 | int32_t bitrate; |
| 1656 | if (params->findInt32(KEY_BIT_RATE, &bitrate)) { |
| 1657 | params = msg->dup(); |
| 1658 | params->removeEntryAt(params->findEntryByName(KEY_BIT_RATE)); |
| 1659 | } |
| 1660 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1661 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1662 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1663 | |
| 1664 | /** |
| 1665 | * Handle input surface parameters |
| 1666 | */ |
| 1667 | if ((config->mDomain & (Config::IS_VIDEO | Config::IS_IMAGE)) |
Wonsik Kim | 8a6ed37 | 2019-12-03 16:05:51 -0800 | [diff] [blame] | 1668 | && (config->mDomain & Config::IS_ENCODER) |
| 1669 | && config->mInputSurface && config->mISConfig) { |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1670 | (void)params->findInt64(PARAMETER_KEY_OFFSET_TIME, &config->mISConfig->mTimeOffsetUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1671 | |
| 1672 | if (params->findInt64("skip-frames-before", &config->mISConfig->mStartAtUs)) { |
| 1673 | config->mISConfig->mStopped = false; |
| 1674 | } else if (params->findInt64("stop-time-us", &config->mISConfig->mStopAtUs)) { |
| 1675 | config->mISConfig->mStopped = true; |
| 1676 | } |
| 1677 | |
| 1678 | int32_t value; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1679 | if (params->findInt32(PARAMETER_KEY_SUSPEND, &value)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1680 | config->mISConfig->mSuspended = value; |
| 1681 | config->mISConfig->mSuspendAtUs = -1; |
Chong Zhang | 038e8f8 | 2019-02-06 19:05:14 -0800 | [diff] [blame] | 1682 | (void)params->findInt64(PARAMETER_KEY_SUSPEND_TIME, &config->mISConfig->mSuspendAtUs); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | (void)config->mInputSurface->configure(*config->mISConfig); |
| 1686 | if (config->mISConfig->mStopped) { |
| 1687 | config->mInputFormat->setInt64( |
| 1688 | "android._stop-time-offset-us", config->mISConfig->mInputDelayUs); |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
| 1693 | (void)config->getConfigUpdateFromSdkParams( |
| 1694 | comp, params, Config::IS_PARAM, C2_MAY_BLOCK, &configUpdate); |
| 1695 | // Prefer to pass parameters to the buffer channel, so they can be synchronized with the frames. |
| 1696 | // Parameter synchronization is not defined when using input surface. For now, route |
| 1697 | // these directly to the component. |
| 1698 | if (config->mInputSurface == nullptr |
| 1699 | && (property_get_bool("debug.stagefright.ccodec_delayed_params", false) |
| 1700 | || comp->getName().find("c2.android.") == 0)) { |
| 1701 | mChannel->setParameters(configUpdate); |
| 1702 | } else { |
| 1703 | (void)config->setParameters(comp, configUpdate, C2_MAY_BLOCK); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | void CCodec::signalEndOfInputStream() { |
| 1708 | mCallback->onSignaledInputEOS(mChannel->signalEndOfInputStream()); |
| 1709 | } |
| 1710 | |
| 1711 | void CCodec::signalRequestIDRFrame() { |
| 1712 | std::shared_ptr<Codec2Client::Component> comp; |
| 1713 | { |
| 1714 | Mutexed<State>::Locked state(mState); |
| 1715 | if (state->get() == RELEASED) { |
| 1716 | ALOGD("no IDR request sent since component is released"); |
| 1717 | return; |
| 1718 | } |
| 1719 | comp = state->comp; |
| 1720 | } |
| 1721 | ALOGV("request IDR"); |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1722 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1723 | const std::unique_ptr<Config> &config = *configLocked; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1724 | std::vector<std::unique_ptr<C2Param>> params; |
| 1725 | params.push_back( |
| 1726 | std::make_unique<C2StreamRequestSyncFrameTuning::output>(0u, true)); |
| 1727 | config->setParameters(comp, params, C2_MAY_BLOCK); |
| 1728 | } |
| 1729 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1730 | void CCodec::onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1731 | if (!workItems.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1732 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1733 | queue->splice(queue->end(), workItems); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1734 | } |
| 1735 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1736 | } |
| 1737 | |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1738 | void CCodec::onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) { |
| 1739 | mChannel->onInputBufferDone(frameIndex, arrayIndex); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1740 | if (arrayIndex == 0) { |
| 1741 | // We always put no more than one buffer per work, if we use an input surface. |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1742 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1743 | const std::unique_ptr<Config> &config = *configLocked; |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1744 | if (config->mInputSurface) { |
| 1745 | config->mInputSurface->onInputBufferDone(frameIndex); |
| 1746 | } |
| 1747 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1748 | } |
| 1749 | |
| 1750 | void CCodec::onMessageReceived(const sp<AMessage> &msg) { |
| 1751 | TimePoint now = std::chrono::steady_clock::now(); |
| 1752 | CCodecWatchdog::getInstance()->watch(this); |
| 1753 | switch (msg->what()) { |
| 1754 | case kWhatAllocate: { |
| 1755 | // C2ComponentStore::createComponent() should return within 100ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1756 | setDeadline(now, 1500ms, "allocate"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1757 | sp<RefBase> obj; |
| 1758 | CHECK(msg->findObject("codecInfo", &obj)); |
| 1759 | allocate((MediaCodecInfo *)obj.get()); |
| 1760 | break; |
| 1761 | } |
| 1762 | case kWhatConfigure: { |
| 1763 | // C2Component::commit_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1764 | setDeadline(now, 1500ms, "configure"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1765 | sp<AMessage> format; |
| 1766 | CHECK(msg->findMessage("format", &format)); |
| 1767 | configure(format); |
| 1768 | break; |
| 1769 | } |
| 1770 | case kWhatStart: { |
| 1771 | // C2Component::start() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1772 | setDeadline(now, 1500ms, "start"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1773 | start(); |
| 1774 | break; |
| 1775 | } |
| 1776 | case kWhatStop: { |
| 1777 | // C2Component::stop() should return within 500ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1778 | setDeadline(now, 1500ms, "stop"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1779 | stop(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1780 | break; |
| 1781 | } |
| 1782 | case kWhatFlush: { |
| 1783 | // C2Component::flush_sm() should return within 5ms. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1784 | setDeadline(now, 1500ms, "flush"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1785 | flush(); |
| 1786 | break; |
| 1787 | } |
Wonsik Kim | 936a89c | 2020-05-08 16:07:50 -0700 | [diff] [blame] | 1788 | case kWhatRelease: { |
| 1789 | mChannel->release(); |
| 1790 | mClient.reset(); |
| 1791 | mClientListener.reset(); |
| 1792 | break; |
| 1793 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1794 | case kWhatCreateInputSurface: { |
| 1795 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1796 | setDeadline(now, 1500ms, "createInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1797 | createInputSurface(); |
| 1798 | break; |
| 1799 | } |
| 1800 | case kWhatSetInputSurface: { |
| 1801 | // Surface operations may be briefly blocking. |
Wonsik Kim | 9ee5a7c | 2019-06-17 11:33:24 -0700 | [diff] [blame] | 1802 | setDeadline(now, 1500ms, "setInputSurface"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1803 | sp<RefBase> obj; |
| 1804 | CHECK(msg->findObject("surface", &obj)); |
| 1805 | sp<PersistentSurface> surface(static_cast<PersistentSurface *>(obj.get())); |
| 1806 | setInputSurface(surface); |
| 1807 | break; |
| 1808 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1809 | case kWhatWorkDone: { |
| 1810 | std::unique_ptr<C2Work> work; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1811 | bool shouldPost = false; |
| 1812 | { |
| 1813 | Mutexed<std::list<std::unique_ptr<C2Work>>>::Locked queue(mWorkDoneQueue); |
| 1814 | if (queue->empty()) { |
| 1815 | break; |
| 1816 | } |
| 1817 | work.swap(queue->front()); |
| 1818 | queue->pop_front(); |
| 1819 | shouldPost = !queue->empty(); |
| 1820 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1821 | if (shouldPost) { |
| 1822 | (new AMessage(kWhatWorkDone, this))->post(); |
| 1823 | } |
| 1824 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1825 | // handle configuration changes in work done |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1826 | Mutexed<std::unique_ptr<Config>>::Locked configLocked(mConfig); |
| 1827 | const std::unique_ptr<Config> &config = *configLocked; |
Wonsik Kim | 8ec93ab | 2020-11-13 16:17:04 -0800 | [diff] [blame] | 1828 | bool changed = false; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1829 | Config::Watcher<C2StreamInitDataInfo::output> initData = |
| 1830 | config->watch<C2StreamInitDataInfo::output>(); |
| 1831 | if (!work->worklets.empty() |
| 1832 | && (work->worklets.front()->output.flags |
| 1833 | & C2FrameData::FLAG_DISCARD_FRAME) == 0) { |
| 1834 | |
| 1835 | // copy buffer info to config |
Wonsik Kim | 5ecf383 | 2019-04-18 10:28:58 -0700 | [diff] [blame] | 1836 | std::vector<std::unique_ptr<C2Param>> updates; |
| 1837 | for (const std::unique_ptr<C2Param> ¶m |
| 1838 | : work->worklets.front()->output.configUpdate) { |
| 1839 | updates.push_back(C2Param::Copy(*param)); |
| 1840 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1841 | unsigned stream = 0; |
| 1842 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1843 | for (const std::shared_ptr<const C2Info> &info : buf->info()) { |
| 1844 | // move all info into output-stream #0 domain |
| 1845 | updates.emplace_back(C2Param::CopyAsStream(*info, true /* output */, stream)); |
| 1846 | } |
George Burgess IV | c813a59 | 2020-02-22 22:54:44 -0800 | [diff] [blame] | 1847 | |
| 1848 | const std::vector<C2ConstGraphicBlock> blocks = buf->data().graphicBlocks(); |
| 1849 | // for now only do the first block |
| 1850 | if (!blocks.empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1851 | // ALOGV("got output buffer with crop %u,%u+%u,%u and size %u,%u", |
| 1852 | // block.crop().left, block.crop().top, |
| 1853 | // block.crop().width, block.crop().height, |
| 1854 | // block.width(), block.height()); |
George Burgess IV | c813a59 | 2020-02-22 22:54:44 -0800 | [diff] [blame] | 1855 | const C2ConstGraphicBlock &block = blocks[0]; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1856 | updates.emplace_back(new C2StreamCropRectInfo::output(stream, block.crop())); |
| 1857 | updates.emplace_back(new C2StreamPictureSizeInfo::output( |
Harish Mahendrakar | f7c49e2 | 2019-05-24 14:19:16 -0700 | [diff] [blame] | 1858 | stream, block.crop().width, block.crop().height)); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1859 | } |
| 1860 | ++stream; |
| 1861 | } |
| 1862 | |
Wonsik Kim | 8ec93ab | 2020-11-13 16:17:04 -0800 | [diff] [blame] | 1863 | if (config->updateConfiguration(updates, config->mOutputDomain)) { |
| 1864 | changed = true; |
| 1865 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1866 | |
| 1867 | // copy standard infos to graphic buffers if not already present (otherwise, we |
| 1868 | // may overwrite the actual intermediate value with a final value) |
| 1869 | stream = 0; |
George Burgess IV | 3f1a090 | 2020-03-18 12:58:32 -0700 | [diff] [blame] | 1870 | const static C2Param::Index stdGfxInfos[] = { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1871 | C2StreamRotationInfo::output::PARAM_TYPE, |
| 1872 | C2StreamColorAspectsInfo::output::PARAM_TYPE, |
| 1873 | C2StreamDataSpaceInfo::output::PARAM_TYPE, |
| 1874 | C2StreamHdrStaticInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 1875 | C2StreamHdr10PlusInfo::output::PARAM_TYPE, |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1876 | C2StreamPixelAspectRatioInfo::output::PARAM_TYPE, |
| 1877 | C2StreamSurfaceScalingInfo::output::PARAM_TYPE |
| 1878 | }; |
| 1879 | for (const std::shared_ptr<C2Buffer> &buf : work->worklets.front()->output.buffers) { |
| 1880 | if (buf->data().graphicBlocks().size()) { |
| 1881 | for (C2Param::Index ix : stdGfxInfos) { |
| 1882 | if (!buf->hasInfo(ix)) { |
| 1883 | const C2Param *param = |
| 1884 | config->getConfigParameterValue(ix.withStream(stream)); |
| 1885 | if (param) { |
| 1886 | std::shared_ptr<C2Param> info(C2Param::Copy(*param)); |
| 1887 | buf->setInfo(std::static_pointer_cast<C2Info>(info)); |
| 1888 | } |
| 1889 | } |
| 1890 | } |
| 1891 | } |
| 1892 | ++stream; |
| 1893 | } |
| 1894 | } |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 1895 | if (config->mInputSurface) { |
| 1896 | config->mInputSurface->onInputBufferDone(work->input.ordinal.frameIndex); |
| 1897 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1898 | mChannel->onWorkDone( |
Wonsik Kim | 8ec93ab | 2020-11-13 16:17:04 -0800 | [diff] [blame] | 1899 | std::move(work), changed ? config->mOutputFormat->dup() : nullptr, |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1900 | initData.hasChanged() ? initData.update().get() : nullptr); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1901 | break; |
| 1902 | } |
| 1903 | case kWhatWatch: { |
| 1904 | // watch message already posted; no-op. |
| 1905 | break; |
| 1906 | } |
| 1907 | default: { |
| 1908 | ALOGE("unrecognized message"); |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
| 1912 | setDeadline(TimePoint::max(), 0ms, "none"); |
| 1913 | } |
| 1914 | |
| 1915 | void CCodec::setDeadline( |
| 1916 | const TimePoint &now, |
| 1917 | const std::chrono::milliseconds &timeout, |
| 1918 | const char *name) { |
| 1919 | int32_t mult = std::max(1, property_get_int32("debug.stagefright.ccodec_timeout_mult", 1)); |
| 1920 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
| 1921 | deadline->set(now + (timeout * mult), name); |
| 1922 | } |
| 1923 | |
| 1924 | void CCodec::initiateReleaseIfStuck() { |
| 1925 | std::string name; |
| 1926 | bool pendingDeadline = false; |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1927 | { |
| 1928 | Mutexed<NamedTimePoint>::Locked deadline(mDeadline); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1929 | if (deadline->get() < std::chrono::steady_clock::now()) { |
| 1930 | name = deadline->getName(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1931 | } |
| 1932 | if (deadline->get() != TimePoint::max()) { |
| 1933 | pendingDeadline = true; |
| 1934 | } |
| 1935 | } |
| 1936 | if (name.empty()) { |
Wonsik Kim | ab34ed6 | 2019-01-31 15:28:46 -0800 | [diff] [blame] | 1937 | constexpr std::chrono::steady_clock::duration kWorkDurationThreshold = 3s; |
| 1938 | std::chrono::steady_clock::duration elapsed = mChannel->elapsed(); |
| 1939 | if (elapsed >= kWorkDurationThreshold) { |
| 1940 | name = "queue"; |
| 1941 | } |
| 1942 | if (elapsed > 0s) { |
| 1943 | pendingDeadline = true; |
| 1944 | } |
| 1945 | } |
| 1946 | if (name.empty()) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1947 | // We're not stuck. |
| 1948 | if (pendingDeadline) { |
| 1949 | // If we are not stuck yet but still has deadline coming up, |
| 1950 | // post watch message to check back later. |
| 1951 | (new AMessage(kWhatWatch, this))->post(); |
| 1952 | } |
| 1953 | return; |
| 1954 | } |
| 1955 | |
| 1956 | ALOGW("previous call to %s exceeded timeout", name.c_str()); |
| 1957 | initiateRelease(false); |
| 1958 | mCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL); |
| 1959 | } |
| 1960 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 1961 | // static |
| 1962 | PersistentSurface *CCodec::CreateInputSurface() { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1963 | using namespace android; |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1964 | using ::android::hardware::media::omx::V1_0::implementation::TWGraphicBufferSource; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1965 | // Attempt to create a Codec2's input surface. |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1966 | std::shared_ptr<Codec2Client::InputSurface> inputSurface = |
| 1967 | Codec2Client::CreateInputSurface(); |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1968 | if (!inputSurface) { |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1969 | if (property_get_int32("debug.stagefright.c2inputsurface", 0) == -1) { |
| 1970 | sp<IGraphicBufferProducer> gbp; |
| 1971 | sp<OmxGraphicBufferSource> gbs = new OmxGraphicBufferSource(); |
| 1972 | status_t err = gbs->initCheck(); |
| 1973 | if (err != OK) { |
| 1974 | ALOGE("Failed to create persistent input surface: error %d", err); |
| 1975 | return nullptr; |
| 1976 | } |
| 1977 | return new PersistentSurface( |
Wonsik Kim | 9917d4a | 2019-10-24 12:56:38 -0700 | [diff] [blame] | 1978 | gbs->getIGraphicBufferProducer(), new TWGraphicBufferSource(gbs)); |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1979 | } else { |
| 1980 | return nullptr; |
| 1981 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1982 | } |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1983 | return new PersistentSurface( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1984 | inputSurface->getGraphicBufferProducer(), |
Pawin Vongmasa | 1858832 | 2019-05-18 01:52:13 -0700 | [diff] [blame] | 1985 | static_cast<sp<android::hidl::base::V1_0::IBase>>( |
Lajos Molnar | 4711827 | 2019-01-31 16:28:04 -0800 | [diff] [blame] | 1986 | inputSurface->getHalInterface())); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 1989 | class IntfCache { |
| 1990 | public: |
| 1991 | IntfCache() = default; |
| 1992 | |
| 1993 | status_t init(const std::string &name) { |
| 1994 | std::shared_ptr<Codec2Client::Interface> intf{ |
| 1995 | Codec2Client::CreateInterfaceByName(name.c_str())}; |
| 1996 | if (!intf) { |
| 1997 | ALOGW("IntfCache [%s]: Unrecognized interface name", name.c_str()); |
| 1998 | mInitStatus = NO_INIT; |
| 1999 | return NO_INIT; |
| 2000 | } |
| 2001 | const static C2StreamUsageTuning::input sUsage{0u /* stream id */}; |
| 2002 | mFields.push_back(C2FieldSupportedValuesQuery::Possible( |
| 2003 | C2ParamField{&sUsage, &sUsage.value})); |
| 2004 | c2_status_t err = intf->querySupportedValues(mFields, C2_MAY_BLOCK); |
| 2005 | if (err != C2_OK) { |
| 2006 | ALOGW("IntfCache [%s]: failed to query usage supported value (err=%d)", |
| 2007 | name.c_str(), err); |
| 2008 | mFields[0].status = err; |
| 2009 | } |
| 2010 | std::vector<std::unique_ptr<C2Param>> params; |
| 2011 | err = intf->query( |
| 2012 | {&mApiFeatures}, |
| 2013 | {C2PortAllocatorsTuning::input::PARAM_TYPE}, |
| 2014 | C2_MAY_BLOCK, |
| 2015 | ¶ms); |
| 2016 | if (err != C2_OK && err != C2_BAD_INDEX) { |
| 2017 | ALOGW("IntfCache [%s]: failed to query api features (err=%d)", |
| 2018 | name.c_str(), err); |
| 2019 | } |
| 2020 | while (!params.empty()) { |
| 2021 | C2Param *param = params.back().release(); |
| 2022 | params.pop_back(); |
| 2023 | if (!param) { |
| 2024 | continue; |
| 2025 | } |
| 2026 | if (param->type() == C2PortAllocatorsTuning::input::PARAM_TYPE) { |
| 2027 | mInputAllocators.reset( |
| 2028 | C2PortAllocatorsTuning::input::From(params[0].get())); |
| 2029 | } |
| 2030 | } |
| 2031 | mInitStatus = OK; |
| 2032 | return OK; |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2033 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2034 | |
| 2035 | status_t initCheck() const { return mInitStatus; } |
| 2036 | |
| 2037 | const C2FieldSupportedValuesQuery &getUsageSupportedValues() const { |
| 2038 | CHECK_EQ(1u, mFields.size()); |
| 2039 | return mFields[0]; |
| 2040 | } |
| 2041 | |
| 2042 | const C2ApiFeaturesSetting &getApiFeatures() const { |
| 2043 | return mApiFeatures; |
| 2044 | } |
| 2045 | |
| 2046 | const C2PortAllocatorsTuning::input &getInputAllocators() const { |
| 2047 | static std::unique_ptr<C2PortAllocatorsTuning::input> sInvalidated = []{ |
| 2048 | std::unique_ptr<C2PortAllocatorsTuning::input> param = |
| 2049 | C2PortAllocatorsTuning::input::AllocUnique(0); |
| 2050 | param->invalidate(); |
| 2051 | return param; |
| 2052 | }(); |
| 2053 | return mInputAllocators ? *mInputAllocators : *sInvalidated; |
| 2054 | } |
| 2055 | |
| 2056 | private: |
| 2057 | status_t mInitStatus{NO_INIT}; |
| 2058 | |
| 2059 | std::vector<C2FieldSupportedValuesQuery> mFields; |
| 2060 | C2ApiFeaturesSetting mApiFeatures; |
| 2061 | std::unique_ptr<C2PortAllocatorsTuning::input> mInputAllocators; |
| 2062 | }; |
| 2063 | |
| 2064 | static const IntfCache &GetIntfCache(const std::string &name) { |
| 2065 | static IntfCache sNullIntfCache; |
| 2066 | static std::mutex sMutex; |
| 2067 | static std::map<std::string, IntfCache> sCache; |
| 2068 | std::unique_lock<std::mutex> lock{sMutex}; |
| 2069 | auto it = sCache.find(name); |
| 2070 | if (it == sCache.end()) { |
| 2071 | lock.unlock(); |
| 2072 | IntfCache intfCache; |
| 2073 | status_t err = intfCache.init(name); |
| 2074 | if (err != OK) { |
| 2075 | return sNullIntfCache; |
| 2076 | } |
| 2077 | lock.lock(); |
| 2078 | it = sCache.insert({name, std::move(intfCache)}).first; |
| 2079 | } |
| 2080 | return it->second; |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2081 | } |
| 2082 | |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2083 | static status_t GetCommonAllocatorIds( |
| 2084 | const std::vector<std::string> &names, |
| 2085 | C2Allocator::type_t type, |
| 2086 | std::set<C2Allocator::id_t> *ids) { |
| 2087 | int poolMask = GetCodec2PoolMask(); |
| 2088 | C2PlatformAllocatorStore::id_t preferredLinearId = GetPreferredLinearAllocatorId(poolMask); |
| 2089 | C2Allocator::id_t defaultAllocatorId = |
| 2090 | (type == C2Allocator::LINEAR) ? preferredLinearId : C2PlatformAllocatorStore::GRALLOC; |
| 2091 | |
| 2092 | ids->clear(); |
| 2093 | if (names.empty()) { |
| 2094 | return OK; |
| 2095 | } |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2096 | bool firstIteration = true; |
| 2097 | for (const std::string &name : names) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2098 | const IntfCache &intfCache = GetIntfCache(name); |
| 2099 | if (intfCache.initCheck() != OK) { |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2100 | continue; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2101 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2102 | const C2PortAllocatorsTuning::input &allocators = intfCache.getInputAllocators(); |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2103 | if (firstIteration) { |
| 2104 | firstIteration = false; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2105 | if (allocators && allocators.flexCount() > 0) { |
| 2106 | ids->insert(allocators.m.values, |
| 2107 | allocators.m.values + allocators.flexCount()); |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2108 | } |
| 2109 | if (ids->empty()) { |
| 2110 | // The component does not advertise allocators. Use default. |
| 2111 | ids->insert(defaultAllocatorId); |
| 2112 | } |
| 2113 | continue; |
| 2114 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2115 | bool filtered = false; |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2116 | if (allocators && allocators.flexCount() > 0) { |
| 2117 | filtered = true; |
| 2118 | for (auto it = ids->begin(); it != ids->end(); ) { |
| 2119 | bool found = false; |
| 2120 | for (size_t j = 0; j < allocators.flexCount(); ++j) { |
| 2121 | if (allocators.m.values[j] == *it) { |
| 2122 | found = true; |
| 2123 | break; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2124 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2125 | } |
| 2126 | if (found) { |
| 2127 | ++it; |
| 2128 | } else { |
| 2129 | it = ids->erase(it); |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2130 | } |
| 2131 | } |
| 2132 | } |
| 2133 | if (!filtered) { |
| 2134 | // The component does not advertise supported allocators. Use default. |
| 2135 | bool containsDefault = (ids->count(defaultAllocatorId) > 0u); |
| 2136 | if (ids->size() != (containsDefault ? 1 : 0)) { |
| 2137 | ids->clear(); |
| 2138 | if (containsDefault) { |
| 2139 | ids->insert(defaultAllocatorId); |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | } |
| 2144 | // Finally, filter with pool masks |
| 2145 | for (auto it = ids->begin(); it != ids->end(); ) { |
| 2146 | if ((poolMask >> *it) & 1) { |
| 2147 | ++it; |
| 2148 | } else { |
| 2149 | it = ids->erase(it); |
| 2150 | } |
| 2151 | } |
| 2152 | return OK; |
| 2153 | } |
| 2154 | |
| 2155 | static status_t CalculateMinMaxUsage( |
| 2156 | const std::vector<std::string> &names, uint64_t *minUsage, uint64_t *maxUsage) { |
| 2157 | static C2StreamUsageTuning::input sUsage{0u /* stream id */}; |
| 2158 | *minUsage = 0; |
| 2159 | *maxUsage = ~0ull; |
| 2160 | for (const std::string &name : names) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2161 | const IntfCache &intfCache = GetIntfCache(name); |
| 2162 | if (intfCache.initCheck() != OK) { |
Wonsik Kim | fcf46c3 | 2020-04-22 13:50:45 -0700 | [diff] [blame] | 2163 | continue; |
| 2164 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2165 | const C2FieldSupportedValuesQuery &usageSupportedValues = |
| 2166 | intfCache.getUsageSupportedValues(); |
| 2167 | if (usageSupportedValues.status != C2_OK) { |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2168 | continue; |
| 2169 | } |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2170 | const C2FieldSupportedValues &supported = usageSupportedValues.values; |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2171 | if (supported.type != C2FieldSupportedValues::FLAGS) { |
| 2172 | continue; |
| 2173 | } |
| 2174 | if (supported.values.empty()) { |
| 2175 | *maxUsage = 0; |
| 2176 | continue; |
| 2177 | } |
| 2178 | *minUsage |= supported.values[0].u64; |
| 2179 | int64_t currentMaxUsage = 0; |
| 2180 | for (const C2Value::Primitive &flags : supported.values) { |
| 2181 | currentMaxUsage |= flags.u64; |
| 2182 | } |
| 2183 | *maxUsage &= currentMaxUsage; |
| 2184 | } |
| 2185 | return OK; |
| 2186 | } |
| 2187 | |
| 2188 | // static |
| 2189 | status_t CCodec::CanFetchLinearBlock( |
| 2190 | const std::vector<std::string> &names, const C2MemoryUsage &usage, bool *isCompatible) { |
Wonsik Kim | ffb889a | 2020-05-28 11:32:25 -0700 | [diff] [blame] | 2191 | for (const std::string &name : names) { |
| 2192 | const IntfCache &intfCache = GetIntfCache(name); |
| 2193 | if (intfCache.initCheck() != OK) { |
| 2194 | continue; |
| 2195 | } |
| 2196 | const C2ApiFeaturesSetting &features = intfCache.getApiFeatures(); |
| 2197 | if (features && !(features.value & API_SAME_INPUT_BUFFER)) { |
| 2198 | *isCompatible = false; |
| 2199 | return OK; |
| 2200 | } |
| 2201 | } |
Wonsik Kim | fb7a767 | 2019-12-27 17:13:33 -0800 | [diff] [blame] | 2202 | uint64_t minUsage = usage.expected; |
| 2203 | uint64_t maxUsage = ~0ull; |
| 2204 | std::set<C2Allocator::id_t> allocators; |
| 2205 | GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators); |
| 2206 | if (allocators.empty()) { |
| 2207 | *isCompatible = false; |
| 2208 | return OK; |
| 2209 | } |
| 2210 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2211 | *isCompatible = ((maxUsage & minUsage) == minUsage); |
| 2212 | return OK; |
| 2213 | } |
| 2214 | |
| 2215 | static std::shared_ptr<C2BlockPool> GetPool(C2Allocator::id_t allocId) { |
| 2216 | static std::mutex sMutex{}; |
| 2217 | static std::map<C2Allocator::id_t, std::shared_ptr<C2BlockPool>> sPools; |
| 2218 | std::unique_lock<std::mutex> lock{sMutex}; |
| 2219 | std::shared_ptr<C2BlockPool> pool; |
| 2220 | auto it = sPools.find(allocId); |
| 2221 | if (it == sPools.end()) { |
| 2222 | c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool); |
| 2223 | if (err == OK) { |
| 2224 | sPools.emplace(allocId, pool); |
| 2225 | } else { |
| 2226 | pool.reset(); |
| 2227 | } |
| 2228 | } else { |
| 2229 | pool = it->second; |
| 2230 | } |
| 2231 | return pool; |
| 2232 | } |
| 2233 | |
| 2234 | // static |
| 2235 | std::shared_ptr<C2LinearBlock> CCodec::FetchLinearBlock( |
| 2236 | size_t capacity, const C2MemoryUsage &usage, const std::vector<std::string> &names) { |
| 2237 | uint64_t minUsage = usage.expected; |
| 2238 | uint64_t maxUsage = ~0ull; |
| 2239 | std::set<C2Allocator::id_t> allocators; |
| 2240 | GetCommonAllocatorIds(names, C2Allocator::LINEAR, &allocators); |
| 2241 | if (allocators.empty()) { |
| 2242 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR); |
| 2243 | } |
| 2244 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2245 | if ((maxUsage & minUsage) != minUsage) { |
| 2246 | allocators.clear(); |
| 2247 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_LINEAR); |
| 2248 | } |
| 2249 | std::shared_ptr<C2LinearBlock> block; |
| 2250 | for (C2Allocator::id_t allocId : allocators) { |
| 2251 | std::shared_ptr<C2BlockPool> pool = GetPool(allocId); |
| 2252 | if (!pool) { |
| 2253 | continue; |
| 2254 | } |
| 2255 | c2_status_t err = pool->fetchLinearBlock(capacity, C2MemoryUsage{minUsage}, &block); |
| 2256 | if (err != C2_OK || !block) { |
| 2257 | block.reset(); |
| 2258 | continue; |
| 2259 | } |
| 2260 | break; |
| 2261 | } |
| 2262 | return block; |
| 2263 | } |
| 2264 | |
| 2265 | // static |
| 2266 | status_t CCodec::CanFetchGraphicBlock( |
| 2267 | const std::vector<std::string> &names, bool *isCompatible) { |
| 2268 | uint64_t minUsage = 0; |
| 2269 | uint64_t maxUsage = ~0ull; |
| 2270 | std::set<C2Allocator::id_t> allocators; |
| 2271 | GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators); |
| 2272 | if (allocators.empty()) { |
| 2273 | *isCompatible = false; |
| 2274 | return OK; |
| 2275 | } |
| 2276 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2277 | *isCompatible = ((maxUsage & minUsage) == minUsage); |
| 2278 | return OK; |
| 2279 | } |
| 2280 | |
| 2281 | // static |
| 2282 | std::shared_ptr<C2GraphicBlock> CCodec::FetchGraphicBlock( |
| 2283 | int32_t width, |
| 2284 | int32_t height, |
| 2285 | int32_t format, |
| 2286 | uint64_t usage, |
| 2287 | const std::vector<std::string> &names) { |
| 2288 | uint32_t halPixelFormat = HAL_PIXEL_FORMAT_YCBCR_420_888; |
| 2289 | if (!C2Mapper::mapPixelFormatFrameworkToCodec(format, &halPixelFormat)) { |
| 2290 | ALOGD("Unrecognized pixel format: %d", format); |
| 2291 | return nullptr; |
| 2292 | } |
| 2293 | uint64_t minUsage = 0; |
| 2294 | uint64_t maxUsage = ~0ull; |
| 2295 | std::set<C2Allocator::id_t> allocators; |
| 2296 | GetCommonAllocatorIds(names, C2Allocator::GRAPHIC, &allocators); |
| 2297 | if (allocators.empty()) { |
| 2298 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC); |
| 2299 | } |
| 2300 | CalculateMinMaxUsage(names, &minUsage, &maxUsage); |
| 2301 | minUsage |= usage; |
| 2302 | if ((maxUsage & minUsage) != minUsage) { |
| 2303 | allocators.clear(); |
| 2304 | allocators.insert(C2PlatformAllocatorStore::DEFAULT_GRAPHIC); |
| 2305 | } |
| 2306 | std::shared_ptr<C2GraphicBlock> block; |
| 2307 | for (C2Allocator::id_t allocId : allocators) { |
| 2308 | std::shared_ptr<C2BlockPool> pool; |
| 2309 | c2_status_t err = CreateCodec2BlockPool(allocId, nullptr, &pool); |
| 2310 | if (err != C2_OK || !pool) { |
| 2311 | continue; |
| 2312 | } |
| 2313 | err = pool->fetchGraphicBlock( |
| 2314 | width, height, halPixelFormat, C2MemoryUsage{minUsage}, &block); |
| 2315 | if (err != C2_OK || !block) { |
| 2316 | block.reset(); |
| 2317 | continue; |
| 2318 | } |
| 2319 | break; |
| 2320 | } |
| 2321 | return block; |
| 2322 | } |
| 2323 | |
Wonsik Kim | 155d5cb | 2019-10-09 12:49:49 -0700 | [diff] [blame] | 2324 | } // namespace android |
| 2325 | |