shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | #ifndef _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_ |
| 18 | #define _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_ |
| 19 | |
| 20 | #include <C2Buffer.h> |
| 21 | #include <binder/MemoryHeapBase.h> |
| 22 | #include <hidl/HidlSupport.h> |
| 23 | #include <media/MediaCodecBuffer.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | struct JMediaCodecLinearBlock { |
| 28 | std::shared_ptr<C2Buffer> mBuffer; |
| 29 | std::shared_ptr<C2ReadView> mReadonlyMapping; |
| 30 | |
| 31 | std::shared_ptr<C2LinearBlock> mBlock; |
| 32 | std::shared_ptr<C2WriteView> mReadWriteMapping; |
| 33 | |
Wonsik Kim | f7069ce | 2020-05-13 17:15:47 -0700 | [diff] [blame] | 34 | sp<IMemory> mMemory; |
| 35 | sp<hardware::HidlMemory> mHidlMemory; |
| 36 | ssize_t mHidlMemoryOffset; |
| 37 | size_t mHidlMemorySize; |
shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 38 | |
| 39 | sp<MediaCodecBuffer> mLegacyBuffer; |
| 40 | |
| 41 | std::once_flag mCopyWarningFlag; |
| 42 | |
| 43 | std::shared_ptr<C2Buffer> toC2Buffer(size_t offset, size_t size) { |
| 44 | if (mBuffer) { |
ted.sun | b0caa0e | 2021-10-05 11:57:25 +0800 | [diff] [blame^] | 45 | // TODO: if returned C2Buffer is different from mBuffer, we should |
| 46 | // find a way to connect the life cycle between this C2Buffer and |
| 47 | // mBuffer. |
shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 48 | if (mBuffer->data().type() != C2BufferData::LINEAR) { |
| 49 | return nullptr; |
| 50 | } |
| 51 | C2ConstLinearBlock block = mBuffer->data().linearBlocks().front(); |
| 52 | if (offset == 0 && size == block.capacity()) { |
ted.sun | b0caa0e | 2021-10-05 11:57:25 +0800 | [diff] [blame^] | 53 | // Let C2Buffer be new one to queue to MediaCodec. It will allow |
| 54 | // the related input slot to be released by onWorkDone from C2 |
| 55 | // Component. Currently, the life cycle of mBuffer should be |
| 56 | // protected by different flows. |
| 57 | return std::make_shared<C2Buffer>(*mBuffer); |
shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 58 | } |
Ray Lee | 73b682f6 | 2020-12-04 09:08:07 +0000 | [diff] [blame] | 59 | |
| 60 | std::shared_ptr<C2Buffer> buffer = |
| 61 | C2Buffer::CreateLinearBuffer(block.subBlock(offset, size)); |
| 62 | for (const std::shared_ptr<const C2Info> &info : mBuffer->info()) { |
| 63 | std::shared_ptr<C2Param> param = std::move(C2Param::Copy(*info)); |
| 64 | buffer->setInfo(std::static_pointer_cast<C2Info>(param)); |
| 65 | } |
| 66 | return buffer; |
shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 67 | } |
| 68 | if (mBlock) { |
| 69 | return C2Buffer::CreateLinearBuffer(mBlock->share(offset, size, C2Fence{})); |
| 70 | } |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | sp<hardware::HidlMemory> toHidlMemory() { |
Wonsik Kim | f7069ce | 2020-05-13 17:15:47 -0700 | [diff] [blame] | 75 | if (mHidlMemory) { |
| 76 | return mHidlMemory; |
shubang | d49681e | 2020-02-17 21:32:30 -0800 | [diff] [blame] | 77 | } |
| 78 | return nullptr; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | } // namespace android |
| 83 | |
Wonsik Kim | f7069ce | 2020-05-13 17:15:47 -0700 | [diff] [blame] | 84 | #endif // _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_ |