1 /*
2 * Copyright 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2-OutputBufferQueue"
19 #include <android-base/logging.h>
20
21 #include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h>
22 #include <codec2/hidl/1.0/OutputBufferQueue.h>
23 #include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
24
25 #include <C2AllocatorGralloc.h>
26 #include <C2BlockInternal.h>
27 #include <C2Buffer.h>
28 #include <C2PlatformSupport.h>
29
30 #include <iomanip>
31
32 namespace android {
33 namespace hardware {
34 namespace media {
35 namespace c2 {
36 namespace V1_0 {
37 namespace utils {
38
39 using HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
40 V2_0::IGraphicBufferProducer;
41 using B2HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
42 V2_0::utils::B2HGraphicBufferProducer;
43
44 namespace /* unnamed */ {
45
46 // Create a GraphicBuffer object from a graphic block.
createGraphicBuffer(const C2ConstGraphicBlock & block)47 sp<GraphicBuffer> createGraphicBuffer(const C2ConstGraphicBlock& block) {
48 uint32_t width;
49 uint32_t height;
50 uint32_t format;
51 uint64_t usage;
52 uint32_t stride;
53 uint32_t generation;
54 uint64_t bqId;
55 int32_t bqSlot;
56 _UnwrapNativeCodec2GrallocMetadata(
57 block.handle(), &width, &height, &format, &usage,
58 &stride, &generation, &bqId, reinterpret_cast<uint32_t*>(&bqSlot));
59 native_handle_t *grallocHandle =
60 UnwrapNativeCodec2GrallocHandle(block.handle());
61 sp<GraphicBuffer> graphicBuffer =
62 new GraphicBuffer(grallocHandle,
63 GraphicBuffer::CLONE_HANDLE,
64 width, height, format,
65 1, usage, stride);
66 native_handle_delete(grallocHandle);
67 return graphicBuffer;
68 }
69
70 template <typename BlockProcessor>
forEachBlock(C2FrameData & frameData,BlockProcessor process)71 void forEachBlock(C2FrameData& frameData,
72 BlockProcessor process) {
73 for (const std::shared_ptr<C2Buffer>& buffer : frameData.buffers) {
74 if (buffer) {
75 for (const C2ConstGraphicBlock& block :
76 buffer->data().graphicBlocks()) {
77 process(block);
78 }
79 }
80 }
81 }
82
83 template <typename BlockProcessor>
forEachBlock(const std::list<std::unique_ptr<C2Work>> & workList,BlockProcessor process)84 void forEachBlock(const std::list<std::unique_ptr<C2Work>>& workList,
85 BlockProcessor process) {
86 for (const std::unique_ptr<C2Work>& work : workList) {
87 if (!work) {
88 continue;
89 }
90 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
91 if (worklet) {
92 forEachBlock(worklet->output, process);
93 }
94 }
95 }
96 }
97
getHgbp(const sp<IGraphicBufferProducer> & igbp)98 sp<HGraphicBufferProducer> getHgbp(const sp<IGraphicBufferProducer>& igbp) {
99 sp<HGraphicBufferProducer> hgbp =
100 igbp->getHalInterface<HGraphicBufferProducer>();
101 return hgbp ? hgbp :
102 new B2HGraphicBufferProducer(igbp);
103 }
104
attachToBufferQueue(const C2ConstGraphicBlock & block,const sp<IGraphicBufferProducer> & igbp,uint32_t generation,int32_t * bqSlot)105 status_t attachToBufferQueue(const C2ConstGraphicBlock& block,
106 const sp<IGraphicBufferProducer>& igbp,
107 uint32_t generation,
108 int32_t* bqSlot) {
109 if (!igbp) {
110 LOG(WARNING) << "attachToBufferQueue -- null producer.";
111 return NO_INIT;
112 }
113
114 sp<GraphicBuffer> graphicBuffer = createGraphicBuffer(block);
115 graphicBuffer->setGenerationNumber(generation);
116
117 LOG(VERBOSE) << "attachToBufferQueue -- attaching buffer:"
118 << " block dimension " << block.width() << "x"
119 << block.height()
120 << ", graphicBuffer dimension " << graphicBuffer->getWidth() << "x"
121 << graphicBuffer->getHeight()
122 << std::hex << std::setfill('0')
123 << ", format 0x" << std::setw(8) << graphicBuffer->getPixelFormat()
124 << ", usage 0x" << std::setw(16) << graphicBuffer->getUsage()
125 << std::dec << std::setfill(' ')
126 << ", stride " << graphicBuffer->getStride()
127 << ", generation " << graphicBuffer->getGenerationNumber();
128
129 status_t result = igbp->attachBuffer(bqSlot, graphicBuffer);
130 if (result != OK) {
131 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
132 "status = " << result << ".";
133 return result;
134 }
135 LOG(VERBOSE) << "attachToBufferQueue -- attachBuffer returned slot #"
136 << *bqSlot << ".";
137 return OK;
138 }
139
getBufferQueueAssignment(const C2ConstGraphicBlock & block,uint32_t * generation,uint64_t * bqId,int32_t * bqSlot)140 bool getBufferQueueAssignment(const C2ConstGraphicBlock& block,
141 uint32_t* generation,
142 uint64_t* bqId,
143 int32_t* bqSlot) {
144 return _C2BlockFactory::GetBufferQueueData(
145 _C2BlockFactory::GetGraphicBlockPoolData(block),
146 generation, bqId, bqSlot);
147 }
148
149 } // unnamed namespace
150
OutputBufferQueue()151 OutputBufferQueue::OutputBufferQueue()
152 : mGeneration{0}, mBqId{0} {
153 }
154
~OutputBufferQueue()155 OutputBufferQueue::~OutputBufferQueue() {
156 }
157
configure(const sp<IGraphicBufferProducer> & igbp,uint32_t generation,uint64_t bqId)158 bool OutputBufferQueue::configure(const sp<IGraphicBufferProducer>& igbp,
159 uint32_t generation,
160 uint64_t bqId) {
161 size_t tryNum = 0;
162 size_t success = 0;
163 sp<GraphicBuffer> buffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
164 std::weak_ptr<_C2BlockPoolData>
165 poolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
166 {
167 std::scoped_lock<std::mutex> l(mMutex);
168 if (generation == mGeneration) {
169 return false;
170 }
171 mIgbp = igbp;
172 mGeneration = generation;
173 mBqId = bqId;
174 mOwner = std::make_shared<int>(0);
175 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
176 if (mBqId == 0 || !mBuffers[i]) {
177 continue;
178 }
179 std::shared_ptr<_C2BlockPoolData> data = mPoolDatas[i].lock();
180 if (!data ||
181 !_C2BlockFactory::BeginAttachBlockToBufferQueue(data)) {
182 continue;
183 }
184 ++tryNum;
185 int bqSlot;
186 mBuffers[i]->setGenerationNumber(generation);
187 status_t result = igbp->attachBuffer(&bqSlot, mBuffers[i]);
188 if (result != OK) {
189 continue;
190 }
191 bool attach =
192 _C2BlockFactory::EndAttachBlockToBufferQueue(
193 data, mOwner, getHgbp(mIgbp),
194 generation, bqId, bqSlot);
195 if (!attach) {
196 igbp->cancelBuffer(bqSlot, Fence::NO_FENCE);
197 continue;
198 }
199 buffers[bqSlot] = mBuffers[i];
200 poolDatas[bqSlot] = data;
201 ++success;
202 }
203 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
204 mBuffers[i] = buffers[i];
205 mPoolDatas[i] = poolDatas[i];
206 }
207 }
208 ALOGD("remote graphic buffer migration %zu/%zu", success, tryNum);
209 return true;
210 }
211
registerBuffer(const C2ConstGraphicBlock & block)212 bool OutputBufferQueue::registerBuffer(const C2ConstGraphicBlock& block) {
213 std::shared_ptr<_C2BlockPoolData> data =
214 _C2BlockFactory::GetGraphicBlockPoolData(block);
215 if (!data) {
216 return false;
217 }
218 std::scoped_lock<std::mutex> l(mMutex);
219
220 if (!mIgbp) {
221 return false;
222 }
223
224 uint32_t oldGeneration;
225 uint64_t oldId;
226 int32_t oldSlot;
227 // If the block is not bufferqueue-based, do nothing.
228 if (!_C2BlockFactory::GetBufferQueueData(
229 data, &oldGeneration, &oldId, &oldSlot) || (oldId == 0)) {
230 return false;
231 }
232 // If the block's bqId is the same as the desired bqId, just hold.
233 if ((oldId == mBqId) && (oldGeneration == mGeneration)) {
234 LOG(VERBOSE) << "holdBufferQueueBlock -- import without attaching:"
235 << " bqId " << oldId
236 << ", bqSlot " << oldSlot
237 << ", generation " << mGeneration
238 << ".";
239 _C2BlockFactory::HoldBlockFromBufferQueue(data, mOwner, getHgbp(mIgbp));
240 mPoolDatas[oldSlot] = data;
241 mBuffers[oldSlot] = createGraphicBuffer(block);
242 mBuffers[oldSlot]->setGenerationNumber(mGeneration);
243 return true;
244 }
245 int32_t d = (int32_t) mGeneration - (int32_t) oldGeneration;
246 LOG(WARNING) << "receiving stale buffer: generation "
247 << mGeneration << " , diff " << d << " : slot "
248 << oldSlot;
249 return false;
250 }
251
outputBuffer(const C2ConstGraphicBlock & block,const BnGraphicBufferProducer::QueueBufferInput & input,BnGraphicBufferProducer::QueueBufferOutput * output)252 status_t OutputBufferQueue::outputBuffer(
253 const C2ConstGraphicBlock& block,
254 const BnGraphicBufferProducer::QueueBufferInput& input,
255 BnGraphicBufferProducer::QueueBufferOutput* output) {
256 uint32_t generation;
257 uint64_t bqId;
258 int32_t bqSlot;
259 bool display = displayBufferQueueBlock(block);
260 if (!getBufferQueueAssignment(block, &generation, &bqId, &bqSlot) ||
261 bqId == 0) {
262 // Block not from bufferqueue -- it must be attached before queuing.
263
264 mMutex.lock();
265 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
266 uint32_t outputGeneration = mGeneration;
267 mMutex.unlock();
268
269 status_t status = attachToBufferQueue(
270 block, outputIgbp, outputGeneration, &bqSlot);
271 if (status != OK) {
272 LOG(WARNING) << "outputBuffer -- attaching failed.";
273 return INVALID_OPERATION;
274 }
275
276 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
277 input, output);
278 if (status != OK) {
279 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
280 "on non-bufferqueue-based block. "
281 "Error = " << status << ".";
282 return status;
283 }
284 return OK;
285 }
286
287 mMutex.lock();
288 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
289 uint32_t outputGeneration = mGeneration;
290 uint64_t outputBqId = mBqId;
291 mMutex.unlock();
292
293 if (!outputIgbp) {
294 LOG(VERBOSE) << "outputBuffer -- output surface is null.";
295 return NO_INIT;
296 }
297
298 if (!display) {
299 LOG(WARNING) << "outputBuffer -- cannot display "
300 "bufferqueue-based block to the bufferqueue.";
301 return UNKNOWN_ERROR;
302 }
303 if (bqId != outputBqId || generation != outputGeneration) {
304 int32_t diff = (int32_t) outputGeneration - (int32_t) generation;
305 LOG(WARNING) << "outputBuffer -- buffers from old generation to "
306 << outputGeneration << " , diff: " << diff
307 << " , slot: " << bqSlot;
308 return DEAD_OBJECT;
309 }
310
311 status_t status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
312 input, output);
313 if (status != OK) {
314 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
315 "on bufferqueue-based block. "
316 "Error = " << status << ".";
317 return status;
318 }
319 return OK;
320 }
321
holdBufferQueueBlocks(const std::list<std::unique_ptr<C2Work>> & workList)322 void OutputBufferQueue::holdBufferQueueBlocks(
323 const std::list<std::unique_ptr<C2Work>>& workList) {
324 forEachBlock(workList,
325 std::bind(&OutputBufferQueue::registerBuffer,
326 this, std::placeholders::_1));
327 }
328
329 } // namespace utils
330 } // namespace V1_0
331 } // namespace c2
332 } // namespace media
333 } // namespace hardware
334 } // namespace android
335
336