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 virtual void getConsumerUsage(uint64_t *consumerUsage); 105 106 private: 107 const std::shared_ptr<C2Allocator> mAllocator; 108 const local_id_t mLocalId; 109 110 class Impl; 111 std::shared_ptr<Impl> mImpl; 112 113 friend struct C2BufferQueueBlockPoolData; 114 }; 115 116 class C2SurfaceSyncMemory; 117 118 struct C2BufferQueueBlockPoolData : public _C2BlockPoolData { 119 public: 120 typedef ::android::hardware::graphics::bufferqueue::V2_0:: 121 IGraphicBufferProducer HGraphicBufferProducer; 122 123 // Create a remote BlockPoolData. 124 C2BufferQueueBlockPoolData( 125 uint32_t generation, uint64_t bqId, int32_t bqSlot, 126 const std::shared_ptr<int> &owner, 127 const android::sp<HGraphicBufferProducer>& producer); 128 129 // Create a local BlockPoolData. 130 C2BufferQueueBlockPoolData( 131 uint32_t generation, uint64_t bqId, int32_t bqSlot, 132 const std::shared_ptr<int> &owner, 133 const android::sp<HGraphicBufferProducer>& producer, 134 std::shared_ptr<C2SurfaceSyncMemory>); 135 136 virtual ~C2BufferQueueBlockPoolData() override; 137 138 virtual type_t getType() const override; 139 140 int migrate(const android::sp<HGraphicBufferProducer>& producer, 141 uint32_t toGeneration, uint64_t toUsage, uint64_t toBqId, 142 android::sp<android::GraphicBuffer>& graphicBuffer, uint32_t oldGeneration, 143 std::shared_ptr<C2SurfaceSyncMemory> syncMem); 144 private: 145 friend struct _C2BlockFactory; 146 147 // Methods delegated from _C2BlockFactory. 148 void getBufferQueueData(uint32_t* generation, uint64_t* bqId, int32_t* bqSlot) const; 149 bool holdBlockFromBufferQueue(const std::shared_ptr<int>& owner, 150 const android::sp<HGraphicBufferProducer>& igbp, 151 std::shared_ptr<C2SurfaceSyncMemory> syncMem); 152 bool beginTransferBlockToClient(); 153 bool endTransferBlockToClient(bool transfer); 154 bool beginAttachBlockToBufferQueue(); 155 bool endAttachBlockToBufferQueue(const std::shared_ptr<int>& owner, 156 const android::sp<HGraphicBufferProducer>& igbp, 157 std::shared_ptr<C2SurfaceSyncMemory> syncMem, 158 uint32_t generation, uint64_t bqId, int32_t bqSlot); 159 bool displayBlockToBufferQueue(); 160 161 const bool mLocal; 162 bool mHeld; 163 164 // Data of the corresponding buffer. 165 uint32_t mGeneration; 166 uint64_t mBqId; 167 int32_t mBqSlot; 168 169 // Data of the current IGBP, updated at migrate(). If the values are 170 // mismatched, then the corresponding buffer will not be cancelled back to 171 // IGBP at the destructor. 172 uint32_t mCurrentGeneration; 173 uint64_t mCurrentBqId; 174 175 bool mTransfer; // local transfer to remote 176 bool mAttach; // attach on remote 177 bool mDisplay; // display on remote; 178 std::weak_ptr<int> mOwner; 179 android::sp<HGraphicBufferProducer> mIgbp; 180 std::shared_ptr<C2SurfaceSyncMemory> mSyncMem; 181 mutable std::mutex mLock; 182 }; 183 184 #endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_ 185