| 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 |  | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 52 | virtual c2_status_t fetchGraphicBlock( | 
|  | 53 | uint32_t width, | 
|  | 54 | uint32_t height, | 
|  | 55 | uint32_t format, | 
|  | 56 | C2MemoryUsage usage, | 
|  | 57 | std::shared_ptr<C2GraphicBlock> *block /* nonnull */, | 
|  | 58 | C2Fence *fence /* nonnull */) override; | 
|  | 59 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 60 | typedef std::function<void(uint64_t producer, int32_t slot, int64_t nsecs)> OnRenderCallback; | 
|  | 61 |  | 
|  | 62 | /** | 
|  | 63 | * Sets render callback. | 
|  | 64 | * | 
|  | 65 | * \param renderCallbak callback to call for all dequeue buffer. | 
|  | 66 | */ | 
|  | 67 | virtual void setRenderCallback(const OnRenderCallback &renderCallback = OnRenderCallback()); | 
|  | 68 |  | 
| Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 69 | typedef ::android::hardware::graphics::bufferqueue::V2_0:: | 
|  | 70 | IGraphicBufferProducer HGraphicBufferProducer; | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 71 | /** | 
|  | 72 | * Configures an IGBP in order to create blocks. A newly created block is | 
|  | 73 | * dequeued from the configured IGBP. Unique Id of IGBP and the slot number of | 
|  | 74 | * blocks are passed via native_handle. Managing IGBP is responsibility of caller. | 
|  | 75 | * When IGBP is not configured, block will be created via allocator. | 
|  | 76 | * Since zero is not used for Unique Id of IGBP, if IGBP is not configured or producer | 
|  | 77 | * is configured as nullptr, unique id which is bundled in native_handle is zero. | 
|  | 78 | * | 
|  | 79 | * \param producer      the IGBP, which will be used to fetch blocks | 
|  | 80 | */ | 
| Pawin Vongmasa | ef939bf | 2019-03-03 04:44:59 -0800 | [diff] [blame] | 81 | virtual void configureProducer(const android::sp<HGraphicBufferProducer> &producer); | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 82 |  | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 83 | /** | 
|  | 84 | * Configures an IGBP in order to create blocks. A newly created block is | 
|  | 85 | * dequeued from the configured IGBP. Unique Id of IGBP and the slot number of | 
|  | 86 | * blocks are passed via native_handle. Managing IGBP is responsibility of caller. | 
|  | 87 | * When IGBP is not configured, block will be created via allocator. | 
|  | 88 | * Since zero is not used for Unique Id of IGBP, if IGBP is not configured or producer | 
|  | 89 | * is configured as nullptr, unique id which is bundled in native_handle is zero. | 
|  | 90 | * | 
|  | 91 | * \param producer      the IGBP, which will be used to fetch blocks | 
|  | 92 | * \param syncMemory    Shared memory for synchronization of allocation & deallocation. | 
|  | 93 | * \param bqId          Id of IGBP | 
|  | 94 | * \param generationId  Generation Id for rendering output | 
|  | 95 | * \param consumerUsage consumerUsage flagof the IGBP | 
|  | 96 | */ | 
|  | 97 | virtual void configureProducer( | 
|  | 98 | const android::sp<HGraphicBufferProducer> &producer, | 
|  | 99 | native_handle_t *syncMemory, | 
|  | 100 | uint64_t bqId, | 
|  | 101 | uint32_t generationId, | 
|  | 102 | uint64_t consumerUsage); | 
|  | 103 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 104 | private: | 
|  | 105 | const std::shared_ptr<C2Allocator> mAllocator; | 
|  | 106 | const local_id_t mLocalId; | 
|  | 107 |  | 
|  | 108 | class Impl; | 
|  | 109 | std::shared_ptr<Impl> mImpl; | 
|  | 110 |  | 
|  | 111 | friend struct C2BufferQueueBlockPoolData; | 
|  | 112 | }; | 
|  | 113 |  | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 114 | class C2SurfaceSyncMemory; | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 115 |  | 
|  | 116 | struct C2BufferQueueBlockPoolData : public _C2BlockPoolData { | 
|  | 117 | public: | 
|  | 118 | typedef ::android::hardware::graphics::bufferqueue::V2_0:: | 
|  | 119 | IGraphicBufferProducer HGraphicBufferProducer; | 
|  | 120 |  | 
|  | 121 | // Create a remote BlockPoolData. | 
|  | 122 | C2BufferQueueBlockPoolData( | 
|  | 123 | uint32_t generation, uint64_t bqId, int32_t bqSlot, | 
|  | 124 | const std::shared_ptr<int> &owner, | 
|  | 125 | const android::sp<HGraphicBufferProducer>& producer); | 
|  | 126 |  | 
|  | 127 | // Create a local BlockPoolData. | 
|  | 128 | C2BufferQueueBlockPoolData( | 
|  | 129 | uint32_t generation, uint64_t bqId, int32_t bqSlot, | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 130 | const android::sp<HGraphicBufferProducer>& producer, | 
|  | 131 | std::shared_ptr<C2SurfaceSyncMemory>, int noUse); | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 132 |  | 
|  | 133 | virtual ~C2BufferQueueBlockPoolData() override; | 
|  | 134 |  | 
|  | 135 | virtual type_t getType() const override; | 
|  | 136 |  | 
|  | 137 | int migrate(const android::sp<HGraphicBufferProducer>& producer, | 
| Chih-Yu Huang | b5ec89e | 2021-02-09 17:37:32 +0900 | [diff] [blame] | 138 | uint32_t toGeneration, uint64_t toUsage, uint64_t toBqId, | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 139 | android::sp<android::GraphicBuffer>& graphicBuffer, uint32_t oldGeneration, | 
|  | 140 | std::shared_ptr<C2SurfaceSyncMemory> syncMem); | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 141 |  | 
|  | 142 | private: | 
|  | 143 | friend struct _C2BlockFactory; | 
|  | 144 |  | 
|  | 145 | // Methods delegated from _C2BlockFactory. | 
|  | 146 | void getBufferQueueData(uint32_t* generation, uint64_t* bqId, int32_t* bqSlot) const; | 
|  | 147 | bool holdBlockFromBufferQueue(const std::shared_ptr<int>& owner, | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 148 | const android::sp<HGraphicBufferProducer>& igbp, | 
|  | 149 | std::shared_ptr<C2SurfaceSyncMemory> syncMem); | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 150 | bool beginTransferBlockToClient(); | 
|  | 151 | bool endTransferBlockToClient(bool transfer); | 
|  | 152 | bool beginAttachBlockToBufferQueue(); | 
|  | 153 | bool endAttachBlockToBufferQueue(const std::shared_ptr<int>& owner, | 
|  | 154 | const android::sp<HGraphicBufferProducer>& igbp, | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 155 | std::shared_ptr<C2SurfaceSyncMemory> syncMem, | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 156 | uint32_t generation, uint64_t bqId, int32_t bqSlot); | 
|  | 157 | bool displayBlockToBufferQueue(); | 
|  | 158 |  | 
|  | 159 | const bool mLocal; | 
|  | 160 | bool mHeld; | 
| Chih-Yu Huang | b849ee3 | 2021-02-04 14:01:58 +0900 | [diff] [blame] | 161 |  | 
|  | 162 | // Data of the corresponding buffer. | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 163 | uint32_t mGeneration; | 
|  | 164 | uint64_t mBqId; | 
|  | 165 | int32_t mBqSlot; | 
| Chih-Yu Huang | b849ee3 | 2021-02-04 14:01:58 +0900 | [diff] [blame] | 166 |  | 
|  | 167 | // Data of the current IGBP, updated at migrate(). If the values are | 
|  | 168 | // mismatched, then the corresponding buffer will not be cancelled back to | 
|  | 169 | // IGBP at the destructor. | 
|  | 170 | uint32_t mCurrentGeneration; | 
|  | 171 | uint64_t mCurrentBqId; | 
|  | 172 |  | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 173 | bool mTransfer; // local transfer to remote | 
|  | 174 | bool mAttach; // attach on remote | 
|  | 175 | bool mDisplay; // display on remote; | 
|  | 176 | std::weak_ptr<int> mOwner; | 
|  | 177 | android::sp<HGraphicBufferProducer> mIgbp; | 
| Sungtak Lee | a714f11 | 2021-03-16 05:40:03 -0700 | [diff] [blame^] | 178 | std::shared_ptr<C2SurfaceSyncMemory> mSyncMem; | 
| Chih-Yu Huang | 66cf459 | 2021-02-04 12:07:29 +0900 | [diff] [blame] | 179 | mutable std::mutex mLock; | 
|  | 180 | }; | 
|  | 181 |  | 
| Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 182 | #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_ |