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 20 #include <android-base/unique_fd.h> 21 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, 37 const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> 38 &igba, 39 ::android::base::unique_fd &&ufd, 40 const bool blockFence, 41 const local_id_t localId); 42 43 virtual ~C2IgbaBlockPool() = default; 44 getAllocatorId()45 virtual C2Allocator::id_t getAllocatorId() const override { 46 return mAllocator->getId(); 47 } 48 getLocalId()49 virtual local_id_t getLocalId() const override { 50 return mLocalId; 51 } 52 53 /* Note: this is blocking due to H/W fence waiting */ 54 virtual c2_status_t fetchGraphicBlock( 55 uint32_t width, 56 uint32_t height, 57 uint32_t format, 58 C2MemoryUsage usage, 59 std::shared_ptr<C2GraphicBlock> *block /* nonnull */) override; 60 61 virtual c2_status_t fetchGraphicBlock( 62 uint32_t width, 63 uint32_t height, 64 uint32_t format, 65 C2MemoryUsage usage, 66 std::shared_ptr<C2GraphicBlock> *block /* nonnull */, 67 C2Fence *fence /* nonnull */) override; 68 69 // Do we need this? 70 void invalidate(); 71 72 private: 73 c2_status_t _fetchGraphicBlock( 74 uint32_t width, 75 uint32_t height, 76 uint32_t format, 77 C2MemoryUsage usage, 78 c2_nsecs_t timeoutNs, 79 bool blockFence, 80 uint64_t *origId /* nonnull */, 81 std::shared_ptr<C2GraphicBlock> *block /* nonnull */, 82 C2Fence *fence /* nonnull */); 83 84 const std::shared_ptr<C2Allocator> mAllocator; 85 const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba; 86 const bool mBlockFence; 87 const local_id_t mLocalId; 88 std::atomic<bool> mValid; 89 C2Fence mWaitFence; 90 }; 91 92 typedef struct AHardwareBuffer AHardwareBuffer; 93 94 struct C2IgbaBlockPoolData : public _C2BlockPoolData { 95 96 C2IgbaBlockPoolData( 97 const AHardwareBuffer *buffer, 98 std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> &igba); 99 100 virtual ~C2IgbaBlockPoolData() override; 101 102 virtual type_t getType() const override; 103 104 private: 105 friend struct _C2BlockFactory; 106 107 void getAHardwareBuffer(AHardwareBuffer **pBuf) const; 108 109 void disown(); 110 111 void registerIgba(std::shared_ptr< 112 ::aidl::android::hardware::media::c2::IGraphicBufferAllocator> &igba); 113 114 bool mOwned; 115 const AHardwareBuffer *mBuffer; 116 std::weak_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba; 117 }; 118