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