blob: b2636e963091f96e15fd78b7ac780f04ba5a69bd [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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 Vongmasaef939bf2019-03-03 04:44:59 -080020#include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080021
22#include <C2Buffer.h>
Chih-Yu Huang66cf4592021-02-04 12:07:29 +090023#include <C2BlockInternal.h>
Pawin Vongmasaef939bf2019-03-03 04:44:59 -080024
25#include <functional>
Pawin Vongmasa36653902018-11-15 00:10:25 -080026
Chih-Yu Huang66cf4592021-02-04 12:07:29 +090027namespace android {
28class GraphicBuffer;
29} // namespace android
30
Pawin Vongmasa36653902018-11-15 00:10:25 -080031class C2BufferQueueBlockPool : public C2BlockPool {
32public:
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 Leea714f112021-03-16 05:40:03 -070052 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 Vongmasa36653902018-11-15 00:10:25 -080060 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 Vongmasaef939bf2019-03-03 04:44:59 -080069 typedef ::android::hardware::graphics::bufferqueue::V2_0::
70 IGraphicBufferProducer HGraphicBufferProducer;
Pawin Vongmasa36653902018-11-15 00:10:25 -080071 /**
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 Vongmasaef939bf2019-03-03 04:44:59 -080081 virtual void configureProducer(const android::sp<HGraphicBufferProducer> &producer);
Pawin Vongmasa36653902018-11-15 00:10:25 -080082
Sungtak Leea714f112021-03-16 05:40:03 -070083 /**
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 Vongmasa36653902018-11-15 00:10:25 -0800104private:
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 Leea714f112021-03-16 05:40:03 -0700114class C2SurfaceSyncMemory;
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900115
116struct C2BufferQueueBlockPoolData : public _C2BlockPoolData {
117public:
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 Leea714f112021-03-16 05:40:03 -0700130 const android::sp<HGraphicBufferProducer>& producer,
131 std::shared_ptr<C2SurfaceSyncMemory>, int noUse);
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900132
133 virtual ~C2BufferQueueBlockPoolData() override;
134
135 virtual type_t getType() const override;
136
137 int migrate(const android::sp<HGraphicBufferProducer>& producer,
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900138 uint32_t toGeneration, uint64_t toUsage, uint64_t toBqId,
Sungtak Leea714f112021-03-16 05:40:03 -0700139 android::sp<android::GraphicBuffer>& graphicBuffer, uint32_t oldGeneration,
140 std::shared_ptr<C2SurfaceSyncMemory> syncMem);
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900141
142private:
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 Leea714f112021-03-16 05:40:03 -0700148 const android::sp<HGraphicBufferProducer>& igbp,
149 std::shared_ptr<C2SurfaceSyncMemory> syncMem);
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900150 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 Leea714f112021-03-16 05:40:03 -0700155 std::shared_ptr<C2SurfaceSyncMemory> syncMem,
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900156 uint32_t generation, uint64_t bqId, int32_t bqSlot);
157 bool displayBlockToBufferQueue();
158
159 const bool mLocal;
160 bool mHeld;
Chih-Yu Huangb849ee32021-02-04 14:01:58 +0900161
162 // Data of the corresponding buffer.
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900163 uint32_t mGeneration;
164 uint64_t mBqId;
165 int32_t mBqSlot;
Chih-Yu Huangb849ee32021-02-04 14:01:58 +0900166
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 Huang66cf4592021-02-04 12:07:29 +0900173 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 Leea714f112021-03-16 05:40:03 -0700178 std::shared_ptr<C2SurfaceSyncMemory> mSyncMem;
Chih-Yu Huang66cf4592021-02-04 12:07:29 +0900179 mutable std::mutex mLock;
180};
181
Pawin Vongmasa36653902018-11-15 00:10:25 -0800182#endif // STAGEFRIGHT_CODEC2_BUFFER_PRIV_H_