blob: 060abfdc1ee55e0143225e86270c20cb9fb243a0 [file] [log] [blame]
shubangd49681e2020-02-17 21:32:30 -08001/*
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
25namespace android {
26
27struct JMediaCodecLinearBlock {
Wonsik Kim8569a662022-05-24 14:16:44 -070028 std::vector<std::string> mCodecNames;
29
shubangd49681e2020-02-17 21:32:30 -080030 std::shared_ptr<C2Buffer> mBuffer;
31 std::shared_ptr<C2ReadView> mReadonlyMapping;
32
33 std::shared_ptr<C2LinearBlock> mBlock;
34 std::shared_ptr<C2WriteView> mReadWriteMapping;
35
Wonsik Kimf7069ce2020-05-13 17:15:47 -070036 sp<IMemory> mMemory;
37 sp<hardware::HidlMemory> mHidlMemory;
38 ssize_t mHidlMemoryOffset;
39 size_t mHidlMemorySize;
shubangd49681e2020-02-17 21:32:30 -080040
41 sp<MediaCodecBuffer> mLegacyBuffer;
42
43 std::once_flag mCopyWarningFlag;
44
Wonsik Kim8569a662022-05-24 14:16:44 -070045 std::shared_ptr<C2Buffer> toC2Buffer(size_t offset, size_t size) const {
shubangd49681e2020-02-17 21:32:30 -080046 if (mBuffer) {
ted.sunb0caa0e2021-10-05 11:57:25 +080047 // TODO: if returned C2Buffer is different from mBuffer, we should
48 // find a way to connect the life cycle between this C2Buffer and
49 // mBuffer.
shubangd49681e2020-02-17 21:32:30 -080050 if (mBuffer->data().type() != C2BufferData::LINEAR) {
51 return nullptr;
52 }
53 C2ConstLinearBlock block = mBuffer->data().linearBlocks().front();
54 if (offset == 0 && size == block.capacity()) {
ted.sunb0caa0e2021-10-05 11:57:25 +080055 // Let C2Buffer be new one to queue to MediaCodec. It will allow
56 // the related input slot to be released by onWorkDone from C2
57 // Component. Currently, the life cycle of mBuffer should be
58 // protected by different flows.
59 return std::make_shared<C2Buffer>(*mBuffer);
shubangd49681e2020-02-17 21:32:30 -080060 }
Ray Lee73b682f62020-12-04 09:08:07 +000061
62 std::shared_ptr<C2Buffer> buffer =
63 C2Buffer::CreateLinearBuffer(block.subBlock(offset, size));
64 for (const std::shared_ptr<const C2Info> &info : mBuffer->info()) {
65 std::shared_ptr<C2Param> param = std::move(C2Param::Copy(*info));
66 buffer->setInfo(std::static_pointer_cast<C2Info>(param));
67 }
68 return buffer;
shubangd49681e2020-02-17 21:32:30 -080069 }
70 if (mBlock) {
71 return C2Buffer::CreateLinearBuffer(mBlock->share(offset, size, C2Fence{}));
72 }
73 return nullptr;
74 }
75
Wonsik Kim8569a662022-05-24 14:16:44 -070076 sp<hardware::HidlMemory> toHidlMemory() const {
Wonsik Kimf7069ce2020-05-13 17:15:47 -070077 if (mHidlMemory) {
78 return mHidlMemory;
shubangd49681e2020-02-17 21:32:30 -080079 }
80 return nullptr;
81 }
Wonsik Kim8569a662022-05-24 14:16:44 -070082
83 size_t capacity() const {
84 if (mBlock) {
85 return mBlock->capacity();
86 }
87 if (mMemory) {
88 return mMemory->size();
89 }
90 return 0;
91 }
shubangd49681e2020-02-17 21:32:30 -080092};
93
94} // namespace android
95
Wonsik Kimf7069ce2020-05-13 17:15:47 -070096#endif // _ANDROID_MEDIA_MEDIACODECLINEARBLOCK_H_