Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 STAGEFRIGHT_CODEC2_BQ_BUFFER_PRIV_H_ |
| 18 | #define STAGEFRIGHT_CODEC2_BQ_BUFFER_PRIV_H_ |
| 19 | |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 20 | #include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 21 | |
| 22 | #include <C2Buffer.h> |
Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame^] | 23 | #include <C2BlockInternal.h> |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 24 | |
| 25 | #include <functional> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 26 | |
Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame^] | 27 | namespace android { |
| 28 | class GraphicBuffer; |
| 29 | } // namespace android |
| 30 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 31 | class C2BufferQueueBlockPool : public C2BlockPool { |
| 32 | public: |
| 33 | C2BufferQueueBlockPool(const std::shared_ptr<C2Allocator> &allocator, const local_id_t localId); |
| 34 | |
| 35 | virtual ~C2BufferQueueBlockPool() override; |
| 36 | |
| 37 | virtual C2Allocator::id_t getAllocatorId() const override { |
| 38 | return mAllocator->getId(); |
| 39 | }; |
| 40 | |
| 41 | virtual local_id_t getLocalId() const override { |
| 42 | return mLocalId; |
| 43 | }; |
| 44 | |
| 45 | virtual c2_status_t fetchGraphicBlock( |
| 46 | uint32_t width, |
| 47 | uint32_t height, |
| 48 | uint32_t format, |
| 49 | C2MemoryUsage usage, |
| 50 | std::shared_ptr<C2GraphicBlock> *block /* nonnull */) override; |
| 51 | |
| 52 | typedef std::function<void(uint64_t producer, int32_t slot, int64_t nsecs)> OnRenderCallback; |
| 53 | |
| 54 | /** |
| 55 | * Sets render callback. |
| 56 | * |
| 57 | * \param renderCallbak callback to call for all dequeue buffer. |
| 58 | */ |
| 59 | virtual void setRenderCallback(const OnRenderCallback &renderCallback = OnRenderCallback()); |
| 60 | |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 61 | typedef ::android::hardware::graphics::bufferqueue::V2_0:: |
| 62 | IGraphicBufferProducer HGraphicBufferProducer; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Configures an IGBP in order to create blocks. A newly created block is |
| 65 | * dequeued from the configured IGBP. Unique Id of IGBP and the slot number of |
| 66 | * blocks are passed via native_handle. Managing IGBP is responsibility of caller. |
| 67 | * When IGBP is not configured, block will be created via allocator. |
| 68 | * Since zero is not used for Unique Id of IGBP, if IGBP is not configured or producer |
| 69 | * is configured as nullptr, unique id which is bundled in native_handle is zero. |
| 70 | * |
| 71 | * \param producer the IGBP, which will be used to fetch blocks |
| 72 | */ |
Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 73 | virtual void configureProducer(const android::sp<HGraphicBufferProducer> &producer); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 74 | |
| 75 | private: |
| 76 | const std::shared_ptr<C2Allocator> mAllocator; |
| 77 | const local_id_t mLocalId; |
| 78 | |
| 79 | class Impl; |
| 80 | std::shared_ptr<Impl> mImpl; |
| 81 | |
| 82 | friend struct C2BufferQueueBlockPoolData; |
| 83 | }; |
| 84 | |
Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame^] | 85 | |
| 86 | struct C2BufferQueueBlockPoolData : public _C2BlockPoolData { |
| 87 | public: |
| 88 | typedef ::android::hardware::graphics::bufferqueue::V2_0:: |
| 89 | IGraphicBufferProducer HGraphicBufferProducer; |
| 90 | |
| 91 | // Create a remote BlockPoolData. |
| 92 | C2BufferQueueBlockPoolData( |
| 93 | uint32_t generation, uint64_t bqId, int32_t bqSlot, |
| 94 | const std::shared_ptr<int> &owner, |
| 95 | const android::sp<HGraphicBufferProducer>& producer); |
| 96 | |
| 97 | // Create a local BlockPoolData. |
| 98 | C2BufferQueueBlockPoolData( |
| 99 | uint32_t generation, uint64_t bqId, int32_t bqSlot, |
| 100 | const std::shared_ptr<C2BufferQueueBlockPool::Impl>& pool); |
| 101 | |
| 102 | virtual ~C2BufferQueueBlockPoolData() override; |
| 103 | |
| 104 | virtual type_t getType() const override; |
| 105 | |
| 106 | int migrate(const android::sp<HGraphicBufferProducer>& producer, |
| 107 | uint32_t toGeneration, uint64_t toBqId, |
| 108 | android::sp<android::GraphicBuffer> *buffers, uint32_t oldGeneration); |
| 109 | |
| 110 | private: |
| 111 | friend struct _C2BlockFactory; |
| 112 | |
| 113 | // Methods delegated from _C2BlockFactory. |
| 114 | void getBufferQueueData(uint32_t* generation, uint64_t* bqId, int32_t* bqSlot) const; |
| 115 | bool holdBlockFromBufferQueue(const std::shared_ptr<int>& owner, |
| 116 | const android::sp<HGraphicBufferProducer>& igbp); |
| 117 | bool beginTransferBlockToClient(); |
| 118 | bool endTransferBlockToClient(bool transfer); |
| 119 | bool beginAttachBlockToBufferQueue(); |
| 120 | bool endAttachBlockToBufferQueue(const std::shared_ptr<int>& owner, |
| 121 | const android::sp<HGraphicBufferProducer>& igbp, |
| 122 | uint32_t generation, uint64_t bqId, int32_t bqSlot); |
| 123 | bool displayBlockToBufferQueue(); |
| 124 | |
| 125 | const bool mLocal; |
| 126 | bool mHeld; |
| 127 | uint32_t mGeneration; |
| 128 | uint64_t mBqId; |
| 129 | int32_t mBqSlot; |
| 130 | bool mTransfer; // local transfer to remote |
| 131 | bool mAttach; // attach on remote |
| 132 | bool mDisplay; // display on remote; |
| 133 | std::weak_ptr<int> mOwner; |
| 134 | android::sp<HGraphicBufferProducer> mIgbp; |
| 135 | std::shared_ptr<C2BufferQueueBlockPool::Impl> mLocalPool; |
| 136 | mutable std::mutex mLock; |
| 137 | }; |
| 138 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 139 | #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_ |