Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 2 | * Copyright 2024, The Android Open Source Project |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 18 | #define LOG_TAG "C2NodeImpl" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 19 | #include <log/log.h> |
| 20 | |
| 21 | #include <C2AllocatorGralloc.h> |
| 22 | #include <C2BlockInternal.h> |
| 23 | #include <C2Component.h> |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 24 | #include <C2Config.h> |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 25 | #include <C2Debug.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 26 | #include <C2PlatformSupport.h> |
| 27 | |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 28 | #include <android/fdsan.h> |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 29 | #include <media/stagefright/foundation/ColorUtils.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 30 | #include <ui/Fence.h> |
| 31 | #include <ui/GraphicBuffer.h> |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 32 | #include <utils/Errors.h> |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 33 | #include <utils/Thread.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 34 | |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 35 | #include "utils/Codec2Mapper.h" |
Sungtak Lee | 10f3f5a | 2024-03-26 03:36:50 +0000 | [diff] [blame] | 36 | #include "C2NodeImpl.h" |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 37 | #include "Codec2Buffer.h" |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 41 | using ::aidl::android::media::IAidlBufferSource; |
| 42 | using ::aidl::android::media::IAidlNode; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 43 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 44 | using ::android::media::BUFFERFLAG_EOS; |
| 45 | |
| 46 | namespace { |
Wonsik Kim | 414eb15 | 2020-11-12 11:14:42 -0800 | [diff] [blame] | 47 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 48 | class Buffer2D : public C2Buffer { |
| 49 | public: |
| 50 | explicit Buffer2D(C2ConstGraphicBlock block) : C2Buffer({ block }) {} |
| 51 | }; |
| 52 | |
| 53 | } // namespace |
| 54 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 55 | class C2NodeImpl::QueueThread : public Thread { |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 56 | public: |
| 57 | QueueThread() : Thread(false) {} |
| 58 | ~QueueThread() override = default; |
| 59 | void queue( |
| 60 | const std::shared_ptr<Codec2Client::Component> &comp, |
| 61 | int fenceFd, |
| 62 | std::unique_ptr<C2Work> &&work, |
| 63 | android::base::unique_fd &&fd0, |
| 64 | android::base::unique_fd &&fd1) { |
| 65 | Mutexed<Jobs>::Locked jobs(mJobs); |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 66 | auto it = jobs->queues.try_emplace(comp, comp).first; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 67 | it->second.workList.emplace_back( |
| 68 | std::move(work), fenceFd, std::move(fd0), std::move(fd1)); |
| 69 | jobs->cond.broadcast(); |
| 70 | } |
| 71 | |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 72 | void setDataspace(android_dataspace dataspace) { |
| 73 | Mutexed<Jobs>::Locked jobs(mJobs); |
| 74 | ColorUtils::convertDataSpaceToV0(dataspace); |
| 75 | jobs->configUpdate.emplace_back(new C2StreamDataSpaceInfo::input(0u, dataspace)); |
| 76 | int32_t standard; |
| 77 | int32_t transfer; |
| 78 | int32_t range; |
| 79 | ColorUtils::getColorConfigFromDataSpace(dataspace, &range, &standard, &transfer); |
| 80 | std::unique_ptr<C2StreamColorAspectsInfo::input> colorAspects = |
| 81 | std::make_unique<C2StreamColorAspectsInfo::input>(0u); |
| 82 | if (C2Mapper::map(standard, &colorAspects->primaries, &colorAspects->matrix) |
| 83 | && C2Mapper::map(transfer, &colorAspects->transfer) |
| 84 | && C2Mapper::map(range, &colorAspects->range)) { |
| 85 | jobs->configUpdate.push_back(std::move(colorAspects)); |
| 86 | } |
| 87 | } |
| 88 | |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 89 | void setPriority(int priority) { |
| 90 | androidSetThreadPriority(getTid(), priority); |
| 91 | } |
| 92 | |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 93 | protected: |
| 94 | bool threadLoop() override { |
| 95 | constexpr nsecs_t kIntervalNs = nsecs_t(10) * 1000 * 1000; // 10ms |
| 96 | constexpr nsecs_t kWaitNs = kIntervalNs * 2; |
| 97 | for (int i = 0; i < 2; ++i) { |
| 98 | Mutexed<Jobs>::Locked jobs(mJobs); |
| 99 | nsecs_t nowNs = systemTime(); |
| 100 | bool queued = false; |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 101 | for (auto it = jobs->queues.begin(); it != jobs->queues.end(); ) { |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 102 | Queue &queue = it->second; |
| 103 | if (queue.workList.empty() |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 104 | || (queue.lastQueuedTimestampNs != 0 && |
| 105 | nowNs - queue.lastQueuedTimestampNs < kIntervalNs)) { |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 106 | ++it; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 107 | continue; |
| 108 | } |
| 109 | std::shared_ptr<Codec2Client::Component> comp = queue.component.lock(); |
| 110 | if (!comp) { |
| 111 | it = jobs->queues.erase(it); |
| 112 | continue; |
| 113 | } |
| 114 | std::list<std::unique_ptr<C2Work>> items; |
| 115 | std::vector<int> fenceFds; |
| 116 | std::vector<android::base::unique_fd> uniqueFds; |
| 117 | while (!queue.workList.empty()) { |
| 118 | items.push_back(std::move(queue.workList.front().work)); |
| 119 | fenceFds.push_back(queue.workList.front().fenceFd); |
| 120 | uniqueFds.push_back(std::move(queue.workList.front().fd0)); |
| 121 | uniqueFds.push_back(std::move(queue.workList.front().fd1)); |
| 122 | queue.workList.pop_front(); |
| 123 | } |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 124 | for (const std::unique_ptr<C2Param> ¶m : jobs->configUpdate) { |
| 125 | items.front()->input.configUpdate.emplace_back(C2Param::Copy(*param)); |
| 126 | } |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 127 | |
| 128 | jobs.unlock(); |
| 129 | for (int fenceFd : fenceFds) { |
| 130 | sp<Fence> fence(new Fence(fenceFd)); |
| 131 | fence->waitForever(LOG_TAG); |
| 132 | } |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 133 | queue.lastQueuedTimestampNs = nowNs; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 134 | comp->queue(&items); |
| 135 | for (android::base::unique_fd &ufd : uniqueFds) { |
| 136 | (void)ufd.release(); |
| 137 | } |
| 138 | jobs.lock(); |
| 139 | |
Wonsik Kim | 0ca30c3 | 2019-06-28 14:15:02 -0700 | [diff] [blame] | 140 | it = jobs->queues.upper_bound(comp); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 141 | queued = true; |
| 142 | } |
| 143 | if (queued) { |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 144 | jobs->configUpdate.clear(); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | if (i == 0) { |
| 148 | jobs.waitForConditionRelative(jobs->cond, kWaitNs); |
| 149 | } |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | struct WorkFence { |
| 156 | WorkFence(std::unique_ptr<C2Work> &&w, int fd) : work(std::move(w)), fenceFd(fd) {} |
| 157 | |
| 158 | WorkFence( |
| 159 | std::unique_ptr<C2Work> &&w, |
| 160 | int fd, |
| 161 | android::base::unique_fd &&uniqueFd0, |
| 162 | android::base::unique_fd &&uniqueFd1) |
| 163 | : work(std::move(w)), |
| 164 | fenceFd(fd), |
| 165 | fd0(std::move(uniqueFd0)), |
| 166 | fd1(std::move(uniqueFd1)) {} |
| 167 | |
| 168 | std::unique_ptr<C2Work> work; |
| 169 | int fenceFd; |
| 170 | android::base::unique_fd fd0; |
| 171 | android::base::unique_fd fd1; |
| 172 | }; |
| 173 | struct Queue { |
Sungtak Lee | 45d8059 | 2020-02-25 13:54:14 -0800 | [diff] [blame] | 174 | Queue(const std::shared_ptr<Codec2Client::Component> &comp) |
| 175 | : component(comp), lastQueuedTimestampNs(0) {} |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 176 | Queue(const Queue &) = delete; |
| 177 | Queue &operator =(const Queue &) = delete; |
| 178 | |
| 179 | std::weak_ptr<Codec2Client::Component> component; |
| 180 | std::list<WorkFence> workList; |
| 181 | nsecs_t lastQueuedTimestampNs; |
| 182 | }; |
| 183 | struct Jobs { |
| 184 | std::map<std::weak_ptr<Codec2Client::Component>, |
| 185 | Queue, |
| 186 | std::owner_less<std::weak_ptr<Codec2Client::Component>>> queues; |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 187 | std::vector<std::unique_ptr<C2Param>> configUpdate; |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 188 | Condition cond; |
| 189 | }; |
| 190 | Mutexed<Jobs> mJobs; |
| 191 | }; |
| 192 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 193 | C2NodeImpl::C2NodeImpl(const std::shared_ptr<Codec2Client::Component> &comp, bool aidl) |
Wonsik Kim | 9eac4d1 | 2019-05-23 12:58:48 -0700 | [diff] [blame] | 194 | : mComp(comp), mFrameIndex(0), mWidth(0), mHeight(0), mUsage(0), |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 195 | mAdjustTimestampGapUs(0), mFirstInputFrame(true), |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 196 | mQueueThread(new QueueThread), mAidlHal(aidl) { |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 197 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ALWAYS); |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 198 | mQueueThread->run("C2NodeImpl", PRIORITY_AUDIO); |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 199 | |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 200 | android_dataspace ds = HAL_DATASPACE_UNKNOWN; |
| 201 | mDataspace.lock().set(ds); |
| 202 | uint32_t pf = PIXEL_FORMAT_UNKNOWN; |
| 203 | mPixelFormat.lock().set(pf); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 206 | C2NodeImpl::~C2NodeImpl() { |
| 207 | } |
| 208 | |
| 209 | status_t C2NodeImpl::freeNode() { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 210 | mComp.reset(); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 211 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 212 | return mQueueThread->requestExitAndWait(); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 215 | void C2NodeImpl::onFirstInputFrame() { |
| 216 | mFirstInputFrame = true; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 219 | void C2NodeImpl::getConsumerUsageBits(uint64_t *usage) { |
| 220 | *usage = mUsage; |
| 221 | } |
| 222 | |
| 223 | void C2NodeImpl::getInputBufferParams(IAidlNode::InputBufferParams *params) { |
| 224 | params->bufferCountActual = 16; |
| 225 | |
| 226 | // WORKAROUND: having more slots improve performance while consuming |
| 227 | // more memory. This is a temporary workaround to reduce memory for |
| 228 | // larger-than-4K scenario. |
| 229 | if (mWidth * mHeight > 4096 * 2340) { |
| 230 | std::shared_ptr<Codec2Client::Component> comp = mComp.lock(); |
| 231 | C2PortActualDelayTuning::input inputDelay(0); |
| 232 | C2ActualPipelineDelayTuning pipelineDelay(0); |
| 233 | c2_status_t c2err = C2_NOT_FOUND; |
| 234 | if (comp) { |
| 235 | c2err = comp->query( |
| 236 | {&inputDelay, &pipelineDelay}, {}, C2_DONT_BLOCK, nullptr); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 237 | } |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 238 | if (c2err == C2_OK || c2err == C2_BAD_INDEX) { |
| 239 | params->bufferCountActual = 4; |
| 240 | params->bufferCountActual += (inputDelay ? inputDelay.value : 0u); |
| 241 | params->bufferCountActual += (pipelineDelay ? pipelineDelay.value : 0u); |
Sungtak Lee | 0cd4fbc | 2023-02-02 00:59:01 +0000 | [diff] [blame] | 242 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 243 | } |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 244 | |
| 245 | params->frameWidth = mWidth; |
| 246 | params->frameHeight = mHeight; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 249 | void C2NodeImpl::setConsumerUsageBits(uint64_t usage) { |
| 250 | mUsage = usage; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 253 | void C2NodeImpl::setAdjustTimestampGapUs(int32_t gapUs) { |
| 254 | mAdjustTimestampGapUs = gapUs; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 257 | status_t C2NodeImpl::setInputSurface(const sp<IOMXBufferSource> &bufferSource) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 258 | c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator( |
| 259 | C2PlatformAllocatorStore::GRALLOC, |
| 260 | &mAllocator); |
| 261 | if (err != OK) { |
| 262 | return UNKNOWN_ERROR; |
| 263 | } |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 264 | CHECK(!mAidlHal); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 265 | mBufferSource = bufferSource; |
| 266 | return OK; |
| 267 | } |
| 268 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 269 | status_t C2NodeImpl::setAidlInputSurface( |
| 270 | const std::shared_ptr<IAidlBufferSource> &aidlBufferSource) { |
| 271 | c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator( |
| 272 | C2PlatformAllocatorStore::GRALLOC, |
| 273 | &mAllocator); |
| 274 | if (err != OK) { |
| 275 | return UNKNOWN_ERROR; |
| 276 | } |
| 277 | CHECK(mAidlHal); |
| 278 | mAidlBufferSource = aidlBufferSource; |
| 279 | return OK; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 282 | status_t C2NodeImpl::submitBuffer( |
| 283 | uint32_t buffer, const sp<GraphicBuffer> &graphicBuffer, |
| 284 | uint32_t flags, int64_t timestamp, int fenceFd) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 285 | std::shared_ptr<Codec2Client::Component> comp = mComp.lock(); |
| 286 | if (!comp) { |
| 287 | return NO_INIT; |
| 288 | } |
| 289 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 290 | uint32_t c2Flags = (flags & BUFFERFLAG_EOS) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 291 | ? C2FrameData::FLAG_END_OF_STREAM : 0; |
| 292 | std::shared_ptr<C2GraphicBlock> block; |
| 293 | |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 294 | android::base::unique_fd fd0, fd1; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 295 | C2Handle *handle = nullptr; |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 296 | if (graphicBuffer) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 297 | std::shared_ptr<C2GraphicAllocation> alloc; |
| 298 | handle = WrapNativeCodec2GrallocHandle( |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 299 | graphicBuffer->handle, |
| 300 | graphicBuffer->width, |
| 301 | graphicBuffer->height, |
| 302 | graphicBuffer->format, |
| 303 | graphicBuffer->usage, |
| 304 | graphicBuffer->stride); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 305 | if (handle != nullptr) { |
| 306 | // unique_fd takes ownership of the fds, we'll get warning if these |
| 307 | // fds get closed by somebody else. Onwership will be released before |
| 308 | // we return, so that the fds get closed as usually when this function |
| 309 | // goes out of scope (when both items and block are gone). |
| 310 | native_handle_t *nativeHandle = reinterpret_cast<native_handle_t*>(handle); |
| 311 | fd0.reset(nativeHandle->numFds > 0 ? nativeHandle->data[0] : -1); |
| 312 | fd1.reset(nativeHandle->numFds > 1 ? nativeHandle->data[1] : -1); |
| 313 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 314 | c2_status_t err = mAllocator->priorGraphicAllocation(handle, &alloc); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 315 | if (err != OK) { |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 316 | (void)fd0.release(); |
| 317 | (void)fd1.release(); |
Chih-Yu Huang | c0ac355 | 2021-03-11 14:37:10 +0900 | [diff] [blame] | 318 | native_handle_close(handle); |
| 319 | native_handle_delete(handle); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 320 | return UNKNOWN_ERROR; |
| 321 | } |
| 322 | block = _C2BlockFactory::CreateGraphicBlock(alloc); |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 323 | } else if (!(flags & BUFFERFLAG_EOS)) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 324 | return BAD_VALUE; |
| 325 | } |
| 326 | |
| 327 | std::unique_ptr<C2Work> work(new C2Work); |
| 328 | work->input.flags = (C2FrameData::flags_t)c2Flags; |
| 329 | work->input.ordinal.timestamp = timestamp; |
| 330 | |
| 331 | // WORKAROUND: adjust timestamp based on gapUs |
| 332 | { |
| 333 | work->input.ordinal.customOrdinal = timestamp; // save input timestamp |
| 334 | if (mFirstInputFrame) { |
| 335 | // grab timestamps on first frame |
| 336 | mPrevInputTimestamp = timestamp; |
| 337 | mPrevCodecTimestamp = timestamp; |
| 338 | mFirstInputFrame = false; |
| 339 | } else if (mAdjustTimestampGapUs > 0) { |
| 340 | work->input.ordinal.timestamp = |
| 341 | mPrevCodecTimestamp |
| 342 | + c2_min((timestamp - mPrevInputTimestamp).peek(), mAdjustTimestampGapUs); |
| 343 | } else if (mAdjustTimestampGapUs < 0) { |
| 344 | work->input.ordinal.timestamp = mPrevCodecTimestamp - mAdjustTimestampGapUs; |
| 345 | } |
| 346 | mPrevInputTimestamp = work->input.ordinal.customOrdinal; |
| 347 | mPrevCodecTimestamp = work->input.ordinal.timestamp; |
| 348 | ALOGV("adjusting %lld to %lld (gap=%lld)", |
| 349 | work->input.ordinal.customOrdinal.peekll(), |
| 350 | work->input.ordinal.timestamp.peekll(), |
| 351 | (long long)mAdjustTimestampGapUs); |
| 352 | } |
| 353 | |
| 354 | work->input.ordinal.frameIndex = mFrameIndex++; |
| 355 | work->input.buffers.clear(); |
| 356 | if (block) { |
| 357 | std::shared_ptr<C2Buffer> c2Buffer( |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 358 | new Buffer2D(block->share( |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 359 | C2Rect(block->width(), block->height()), ::C2Fence()))); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 360 | work->input.buffers.push_back(c2Buffer); |
Wonsik Kim | a79c552 | 2022-01-18 16:29:24 -0800 | [diff] [blame] | 361 | std::shared_ptr<C2StreamHdrStaticInfo::input> staticInfo; |
| 362 | std::shared_ptr<C2StreamHdrDynamicMetadataInfo::input> dynamicInfo; |
| 363 | GetHdrMetadataFromGralloc4Handle( |
| 364 | block->handle(), |
| 365 | &staticInfo, |
| 366 | &dynamicInfo); |
| 367 | if (staticInfo && *staticInfo) { |
| 368 | c2Buffer->setInfo(staticInfo); |
| 369 | } |
| 370 | if (dynamicInfo && *dynamicInfo) { |
| 371 | c2Buffer->setInfo(dynamicInfo); |
| 372 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 373 | } |
| 374 | work->worklets.clear(); |
| 375 | work->worklets.emplace_back(new C2Worklet); |
Wonsik Kim | 831b8d7 | 2019-06-11 17:52:56 -0700 | [diff] [blame] | 376 | mBufferIdsInUse.lock()->emplace(work->input.ordinal.frameIndex.peeku(), buffer); |
| 377 | mQueueThread->queue(comp, fenceFd, std::move(work), std::move(fd0), std::move(fd1)); |
Chong Zhang | 442be45 | 2019-06-18 10:20:54 -0700 | [diff] [blame] | 378 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 379 | return OK; |
| 380 | } |
| 381 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 382 | status_t C2NodeImpl::onDataspaceChanged(uint32_t dataSpace, uint32_t pixelFormat) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 383 | ALOGD("dataspace changed to %#x pixel format: %#x", dataSpace, pixelFormat); |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 384 | android_dataspace d = (android_dataspace)dataSpace; |
| 385 | mQueueThread->setDataspace(d); |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 386 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 387 | mDataspace.lock().set(d); |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 388 | mPixelFormat.lock().set(pixelFormat); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 389 | return OK; |
| 390 | } |
| 391 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 392 | sp<IOMXBufferSource> C2NodeImpl::getSource() { |
| 393 | CHECK(!mAidlHal); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 394 | return mBufferSource; |
| 395 | } |
| 396 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 397 | std::shared_ptr<IAidlBufferSource> C2NodeImpl::getAidlSource() { |
| 398 | CHECK(mAidlHal); |
| 399 | return mAidlBufferSource; |
| 400 | } |
| 401 | |
| 402 | void C2NodeImpl::setFrameSize(uint32_t width, uint32_t height) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 403 | mWidth = width; |
| 404 | mHeight = height; |
| 405 | } |
| 406 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 407 | void C2NodeImpl::onInputBufferDone(c2_cntr64_t index) { |
| 408 | if (mAidlHal) { |
| 409 | if (!mAidlBufferSource) { |
| 410 | ALOGD("Buffer source not set (index=%llu)", index.peekull()); |
| 411 | return; |
| 412 | } |
| 413 | } else { |
| 414 | if (!mBufferSource) { |
| 415 | ALOGD("Buffer source not set (index=%llu)", index.peekull()); |
| 416 | return; |
| 417 | } |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 418 | } |
Wonsik Kim | a261e38 | 2019-04-10 11:37:50 -0700 | [diff] [blame] | 419 | |
| 420 | int32_t bufferId = 0; |
| 421 | { |
| 422 | decltype(mBufferIdsInUse)::Locked bufferIds(mBufferIdsInUse); |
| 423 | auto it = bufferIds->find(index.peeku()); |
| 424 | if (it == bufferIds->end()) { |
| 425 | ALOGV("Untracked input index %llu (maybe already removed)", index.peekull()); |
| 426 | return; |
| 427 | } |
| 428 | bufferId = it->second; |
| 429 | (void)bufferIds->erase(it); |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 430 | } |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 431 | if (mAidlHal) { |
| 432 | ::ndk::ScopedFileDescriptor nullFence; |
| 433 | (void)mAidlBufferSource->onInputBufferEmptied(bufferId, nullFence); |
| 434 | } else { |
| 435 | (void)mBufferSource->onInputBufferEmptied(bufferId, -1); |
| 436 | } |
Wonsik Kim | 4f3314d | 2019-03-26 17:00:34 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 439 | android_dataspace C2NodeImpl::getDataspace() { |
Wonsik Kim | 673dd19 | 2021-01-29 14:58:12 -0800 | [diff] [blame] | 440 | return *mDataspace.lock(); |
| 441 | } |
| 442 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 443 | uint32_t C2NodeImpl::getPixelFormat() { |
Songyue Han | ad01f6a | 2023-08-17 05:45:35 +0000 | [diff] [blame] | 444 | return *mPixelFormat.lock(); |
| 445 | } |
| 446 | |
Sungtak Lee | 64c9d93 | 2024-03-26 03:46:53 +0000 | [diff] [blame^] | 447 | void C2NodeImpl::setPriority(int priority) { |
Wonsik Kim | a1335e1 | 2021-04-22 16:28:29 -0700 | [diff] [blame] | 448 | mQueueThread->setPriority(priority); |
| 449 | } |
| 450 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 451 | } // namespace android |