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 20 #include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h> 21 22 #include <C2Buffer.h> 23 #include <C2BlockInternal.h> 24 25 #include <functional> 26 27 namespace android { 28 class GraphicBuffer; 29 } // namespace android 30 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 getAllocatorId()37 virtual C2Allocator::id_t getAllocatorId() const override { 38 return mAllocator->getId(); 39 }; 40 getLocalId()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 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 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 69 typedef ::android::hardware::graphics::bufferqueue::V2_0:: 70 IGraphicBufferProducer HGraphicBufferProducer; 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 */ 81 virtual void configureProducer(const android::sp<HGraphicBufferProducer> &producer); 82 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 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 114 class C2SurfaceSyncMemory; 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, 130 const android::sp<HGraphicBufferProducer>& producer, 131 std::shared_ptr<C2SurfaceSyncMemory>, int noUse); 132 133 virtual ~C2BufferQueueBlockPoolData() override; 134 135 virtual type_t getType() const override; 136 137 int migrate(const android::sp<HGraphicBufferProducer>& producer, 138 uint32_t toGeneration, uint64_t toUsage, uint64_t toBqId, 139 android::sp<android::GraphicBuffer>& graphicBuffer, uint32_t oldGeneration, 140 std::shared_ptr<C2SurfaceSyncMemory> syncMem); 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, 148 const android::sp<HGraphicBufferProducer>& igbp, 149 std::shared_ptr<C2SurfaceSyncMemory> syncMem); 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, 155 std::shared_ptr<C2SurfaceSyncMemory> syncMem, 156 uint32_t generation, uint64_t bqId, int32_t bqSlot); 157 bool displayBlockToBufferQueue(); 158 159 const bool mLocal; 160 bool mHeld; 161 162 // Data of the corresponding buffer. 163 uint32_t mGeneration; 164 uint64_t mBqId; 165 int32_t mBqSlot; 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 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; 178 std::shared_ptr<C2SurfaceSyncMemory> mSyncMem; 179 mutable std::mutex mLock; 180 }; 181 182 #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_ 183