Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #pragma once |
| 17 | |
| 18 | #include <C2Buffer.h> |
| 19 | |
Sungtak Lee | aa71b5c | 2023-11-04 09:21:15 +0000 | [diff] [blame^] | 20 | #include <android-base/unique_fd.h> |
| 21 | |
Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | |
| 24 | namespace aidl::android::hardware::media::c2 { |
| 25 | class IGraphicBufferAllocator; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Codec2-AIDL IGraphicBufferAllocator backed C2BlockPool |
| 30 | * |
| 31 | * Graphic Blocks are created using IGraphicBufferAllocator C2AIDL interface. |
| 32 | */ |
| 33 | class C2IgbaBlockPool : public C2BlockPool { |
| 34 | public: |
| 35 | explicit C2IgbaBlockPool( |
| 36 | const std::shared_ptr<C2Allocator> &allocator, |
Sungtak Lee | aa71b5c | 2023-11-04 09:21:15 +0000 | [diff] [blame^] | 37 | const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> |
| 38 | &igba, |
| 39 | ::android::base::unique_fd &&ufd, |
Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 40 | const local_id_t localId); |
| 41 | |
| 42 | virtual ~C2IgbaBlockPool() = default; |
| 43 | |
| 44 | virtual C2Allocator::id_t getAllocatorId() const override { |
| 45 | return mAllocator->getId(); |
| 46 | } |
| 47 | |
| 48 | virtual local_id_t getLocalId() const override { |
| 49 | return mLocalId; |
| 50 | } |
| 51 | |
| 52 | /* Note: this is blocking due to H/W fence waiting */ |
| 53 | virtual c2_status_t fetchGraphicBlock( |
| 54 | uint32_t width, |
| 55 | uint32_t height, |
| 56 | uint32_t format, |
| 57 | C2MemoryUsage usage, |
| 58 | std::shared_ptr<C2GraphicBlock> *block /* nonnull */) override; |
| 59 | |
| 60 | virtual c2_status_t fetchGraphicBlock( |
| 61 | uint32_t width, |
| 62 | uint32_t height, |
| 63 | uint32_t format, |
| 64 | C2MemoryUsage usage, |
| 65 | std::shared_ptr<C2GraphicBlock> *block /* nonnull */, |
| 66 | C2Fence *fence /* nonnull */) override; |
| 67 | |
| 68 | // Do we need this? |
| 69 | void invalidate(); |
| 70 | |
| 71 | private: |
| 72 | c2_status_t _fetchGraphicBlock( |
| 73 | uint32_t width, |
| 74 | uint32_t height, |
| 75 | uint32_t format, |
| 76 | C2MemoryUsage usage, |
| 77 | c2_nsecs_t timeoutNs, |
| 78 | uint64_t *origId /* nonnull */, |
| 79 | std::shared_ptr<C2GraphicBlock> *block /* nonnull */, |
| 80 | C2Fence *fence /* nonnull */); |
| 81 | |
| 82 | const std::shared_ptr<C2Allocator> mAllocator; |
| 83 | const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba; |
| 84 | const local_id_t mLocalId; |
| 85 | std::atomic<bool> mValid; |
| 86 | C2Fence mWaitFence; |
| 87 | }; |
| 88 | |
| 89 | typedef struct AHardwareBuffer AHardwareBuffer; |
| 90 | |
| 91 | struct C2IgbaBlockPoolData : public _C2BlockPoolData { |
| 92 | |
| 93 | C2IgbaBlockPoolData( |
| 94 | const AHardwareBuffer *buffer, |
Sungtak Lee | aa71b5c | 2023-11-04 09:21:15 +0000 | [diff] [blame^] | 95 | std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> &igba); |
Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 96 | |
| 97 | virtual ~C2IgbaBlockPoolData() override; |
| 98 | |
| 99 | virtual type_t getType() const override; |
| 100 | |
| 101 | private: |
| 102 | friend struct _C2BlockFactory; |
| 103 | |
| 104 | void getAHardwareBuffer(AHardwareBuffer **pBuf) const; |
| 105 | |
| 106 | void disown(); |
| 107 | |
Sungtak Lee | aa71b5c | 2023-11-04 09:21:15 +0000 | [diff] [blame^] | 108 | void registerIgba(std::shared_ptr< |
| 109 | ::aidl::android::hardware::media::c2::IGraphicBufferAllocator> &igba); |
| 110 | |
Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 111 | bool mOwned; |
| 112 | const AHardwareBuffer *mBuffer; |
Sungtak Lee | aa71b5c | 2023-11-04 09:21:15 +0000 | [diff] [blame^] | 113 | std::weak_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba; |
Sungtak Lee | 72dfba6 | 2023-09-07 23:26:30 +0000 | [diff] [blame] | 114 | }; |