blob: 08c9325880d647538e59236892856f013af52dcd [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 2016 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 "C2Buffer"
19#include <utils/Log.h>
20
21#include <list>
22#include <map>
23#include <mutex>
24
Pin-chih Linaa18ea52019-11-19 18:48:50 +080025#include <C2AllocatorBlob.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080026#include <C2AllocatorGralloc.h>
Pin-chih Linaa18ea52019-11-19 18:48:50 +080027#include <C2AllocatorIon.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080028#include <C2BufferPriv.h>
29#include <C2BlockInternal.h>
Pin-chih Linaa18ea52019-11-19 18:48:50 +080030#include <C2PlatformSupport.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031#include <bufferpool/ClientManager.h>
32
33namespace {
34
Pin-chih Linaa18ea52019-11-19 18:48:50 +080035using android::C2AllocatorBlob;
Pawin Vongmasa36653902018-11-15 00:10:25 -080036using android::C2AllocatorGralloc;
37using android::C2AllocatorIon;
38using android::hardware::media::bufferpool::BufferPoolData;
Sungtak Leed3318082018-09-07 15:52:43 -070039using android::hardware::media::bufferpool::V2_0::ResultStatus;
40using android::hardware::media::bufferpool::V2_0::implementation::BufferPoolAllocation;
41using android::hardware::media::bufferpool::V2_0::implementation::BufferPoolAllocator;
42using android::hardware::media::bufferpool::V2_0::implementation::ClientManager;
43using android::hardware::media::bufferpool::V2_0::implementation::ConnectionId;
44using android::hardware::media::bufferpool::V2_0::implementation::INVALID_CONNECTIONID;
Pawin Vongmasa36653902018-11-15 00:10:25 -080045
46// This anonymous namespace contains the helper classes that allow our implementation to create
47// block/buffer objects.
48//
49// Inherit from the parent, share with the friend.
50class ReadViewBuddy : public C2ReadView {
51 using C2ReadView::C2ReadView;
52 friend class ::C2ConstLinearBlock;
53};
54
55class WriteViewBuddy : public C2WriteView {
56 using C2WriteView::C2WriteView;
57 friend class ::C2LinearBlock;
58};
59
60class ConstLinearBlockBuddy : public C2ConstLinearBlock {
61 using C2ConstLinearBlock::C2ConstLinearBlock;
62 friend class ::C2LinearBlock;
63};
64
65class LinearBlockBuddy : public C2LinearBlock {
66 using C2LinearBlock::C2LinearBlock;
67 friend class ::C2BasicLinearBlockPool;
68};
69
70class AcquirableReadViewBuddy : public C2Acquirable<C2ReadView> {
71 using C2Acquirable::C2Acquirable;
72 friend class ::C2ConstLinearBlock;
73};
74
75class AcquirableWriteViewBuddy : public C2Acquirable<C2WriteView> {
76 using C2Acquirable::C2Acquirable;
77 friend class ::C2LinearBlock;
78};
79
80class GraphicViewBuddy : public C2GraphicView {
81 using C2GraphicView::C2GraphicView;
82 friend class ::C2ConstGraphicBlock;
83 friend class ::C2GraphicBlock;
84};
85
86class AcquirableConstGraphicViewBuddy : public C2Acquirable<const C2GraphicView> {
87 using C2Acquirable::C2Acquirable;
88 friend class ::C2ConstGraphicBlock;
89};
90
91class AcquirableGraphicViewBuddy : public C2Acquirable<C2GraphicView> {
92 using C2Acquirable::C2Acquirable;
93 friend class ::C2GraphicBlock;
94};
95
96class ConstGraphicBlockBuddy : public C2ConstGraphicBlock {
97 using C2ConstGraphicBlock::C2ConstGraphicBlock;
98 friend class ::C2GraphicBlock;
99};
100
101class GraphicBlockBuddy : public C2GraphicBlock {
102 using C2GraphicBlock::C2GraphicBlock;
103 friend class ::C2BasicGraphicBlockPool;
104};
105
106class BufferDataBuddy : public C2BufferData {
107 using C2BufferData::C2BufferData;
108 friend class ::C2Buffer;
109};
110
111} // namespace
112
113/* ========================================== 1D BLOCK ========================================= */
114
115/**
116 * This class is the base class for all 1D block and view implementations.
117 *
118 * This is basically just a placeholder for the underlying 1D allocation and the range of the
119 * alloted portion to this block. There is also a placeholder for a blockpool data.
120 */
121class C2_HIDE _C2Block1DImpl : public _C2LinearRangeAspect {
122public:
123 _C2Block1DImpl(const std::shared_ptr<C2LinearAllocation> &alloc,
124 const std::shared_ptr<_C2BlockPoolData> &poolData = nullptr,
125 size_t offset = 0, size_t size = ~(size_t)0)
126 : _C2LinearRangeAspect(alloc.get(), offset, size),
127 mAllocation(alloc),
128 mPoolData(poolData) { }
129
130 _C2Block1DImpl(const _C2Block1DImpl &other, size_t offset = 0, size_t size = ~(size_t)0)
131 : _C2LinearRangeAspect(&other, offset, size),
132 mAllocation(other.mAllocation),
133 mPoolData(other.mPoolData) { }
134
135 /** returns pool data */
136 std::shared_ptr<_C2BlockPoolData> poolData() const {
137 return mPoolData;
138 }
139
140 /** returns native handle */
141 const C2Handle *handle() const {
142 return mAllocation ? mAllocation->handle() : nullptr;
143 }
144
145 /** returns the allocator's ID */
146 C2Allocator::id_t getAllocatorId() const {
147 // BAD_ID can only happen if this Impl class is initialized for a view - never for a block.
148 return mAllocation ? mAllocation->getAllocatorId() : C2Allocator::BAD_ID;
149 }
150
151 std::shared_ptr<C2LinearAllocation> getAllocation() const {
152 return mAllocation;
153 }
154
155private:
156 std::shared_ptr<C2LinearAllocation> mAllocation;
157 std::shared_ptr<_C2BlockPoolData> mPoolData;
158};
159
160/**
161 * This class contains the mapped data pointer, and the potential error.
162 *
163 * range is the mapped range of the underlying allocation (which is part of the allotted
164 * range).
165 */
166class C2_HIDE _C2MappedBlock1DImpl : public _C2Block1DImpl {
167public:
168 _C2MappedBlock1DImpl(const _C2Block1DImpl &block, uint8_t *data,
169 size_t offset = 0, size_t size = ~(size_t)0)
170 : _C2Block1DImpl(block, offset, size), mData(data), mError(C2_OK) { }
171
172 _C2MappedBlock1DImpl(c2_status_t error)
173 : _C2Block1DImpl(nullptr), mData(nullptr), mError(error) {
174 // CHECK(error != C2_OK);
175 }
176
177 const uint8_t *data() const {
178 return mData;
179 }
180
181 uint8_t *data() {
182 return mData;
183 }
184
185 c2_status_t error() const {
186 return mError;
187 }
188
189private:
190 uint8_t *mData;
191 c2_status_t mError;
192};
193
194/**
195 * Block implementation.
196 */
197class C2Block1D::Impl : public _C2Block1DImpl {
198 using _C2Block1DImpl::_C2Block1DImpl;
199};
200
201const C2Handle *C2Block1D::handle() const {
202 return mImpl->handle();
203};
204
205C2Allocator::id_t C2Block1D::getAllocatorId() const {
206 return mImpl->getAllocatorId();
207};
208
209C2Block1D::C2Block1D(std::shared_ptr<Impl> impl, const _C2LinearRangeAspect &range)
210 // always clamp subrange to parent (impl) range for safety
211 : _C2LinearRangeAspect(impl.get(), range.offset(), range.size()), mImpl(impl) {
212}
213
214/**
215 * Read view implementation.
216 *
217 * range of Impl is the mapped range of the underlying allocation (which is part of the allotted
218 * range). range of View is 0 to capacity() (not represented as an actual range). This maps to a
219 * subrange of Impl range starting at mImpl->offset() + _mOffset.
220 */
221class C2ReadView::Impl : public _C2MappedBlock1DImpl {
222 using _C2MappedBlock1DImpl::_C2MappedBlock1DImpl;
223};
224
225C2ReadView::C2ReadView(std::shared_ptr<Impl> impl, uint32_t offset, uint32_t size)
226 : _C2LinearCapacityAspect(C2LinearCapacity(impl->size()).range(offset, size).size()),
227 mImpl(impl),
228 mOffset(C2LinearCapacity(impl->size()).range(offset, size).offset()) { }
229
230C2ReadView::C2ReadView(c2_status_t error)
231 : _C2LinearCapacityAspect(0u), mImpl(std::make_shared<Impl>(error)), mOffset(0u) {
232 // CHECK(error != C2_OK);
233}
234
235const uint8_t *C2ReadView::data() const {
236 return mImpl->error() ? nullptr : mImpl->data() + mOffset;
237}
238
239c2_status_t C2ReadView::error() const {
240 return mImpl->error();
241}
242
243C2ReadView C2ReadView::subView(size_t offset, size_t size) const {
244 C2LinearRange subRange(*this, offset, size);
245 return C2ReadView(mImpl, mOffset + subRange.offset(), subRange.size());
246}
247
248/**
249 * Write view implementation.
250 */
251class C2WriteView::Impl : public _C2MappedBlock1DImpl {
252 using _C2MappedBlock1DImpl::_C2MappedBlock1DImpl;
253};
254
255C2WriteView::C2WriteView(std::shared_ptr<Impl> impl)
256// UGLY: _C2LinearRangeAspect requires a bona-fide object for capacity to prevent spoofing, so
257// this is what we have to do.
258// TODO: use childRange
259 : _C2EditableLinearRangeAspect(std::make_unique<C2LinearCapacity>(impl->size()).get()), mImpl(impl) { }
260
261C2WriteView::C2WriteView(c2_status_t error)
262 : _C2EditableLinearRangeAspect(nullptr), mImpl(std::make_shared<Impl>(error)) {}
263
264uint8_t *C2WriteView::base() { return mImpl->data(); }
265
266uint8_t *C2WriteView::data() { return mImpl->data() + offset(); }
267
268c2_status_t C2WriteView::error() const { return mImpl->error(); }
269
270/**
271 * Const linear block implementation.
272 */
273C2ConstLinearBlock::C2ConstLinearBlock(std::shared_ptr<Impl> impl, const _C2LinearRangeAspect &range, C2Fence fence)
274 : C2Block1D(impl, range), mFence(fence) { }
275
276C2Acquirable<C2ReadView> C2ConstLinearBlock::map() const {
277 void *base = nullptr;
278 uint32_t len = size();
279 c2_status_t error = mImpl->getAllocation()->map(
280 offset(), len, { C2MemoryUsage::CPU_READ, 0 }, nullptr, &base);
281 // TODO: wait on fence
282 if (error == C2_OK) {
283 std::shared_ptr<ReadViewBuddy::Impl> rvi = std::shared_ptr<ReadViewBuddy::Impl>(
284 new ReadViewBuddy::Impl(*mImpl, (uint8_t *)base, offset(), len),
285 [base, len](ReadViewBuddy::Impl *i) {
286 (void)i->getAllocation()->unmap(base, len, nullptr);
287 delete i;
288 });
289 return AcquirableReadViewBuddy(error, C2Fence(), ReadViewBuddy(rvi, 0, len));
290 } else {
291 return AcquirableReadViewBuddy(error, C2Fence(), ReadViewBuddy(error));
292 }
293}
294
295C2ConstLinearBlock C2ConstLinearBlock::subBlock(size_t offset_, size_t size_) const {
296 C2LinearRange subRange(*mImpl, offset_, size_);
297 return C2ConstLinearBlock(mImpl, subRange, mFence);
298}
299
300/**
301 * Linear block implementation.
302 */
303C2LinearBlock::C2LinearBlock(std::shared_ptr<Impl> impl, const _C2LinearRangeAspect &range)
304 : C2Block1D(impl, range) { }
305
306C2Acquirable<C2WriteView> C2LinearBlock::map() {
307 void *base = nullptr;
308 uint32_t len = size();
309 c2_status_t error = mImpl->getAllocation()->map(
310 offset(), len, { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE }, nullptr, &base);
311 // TODO: wait on fence
312 if (error == C2_OK) {
313 std::shared_ptr<WriteViewBuddy::Impl> rvi = std::shared_ptr<WriteViewBuddy::Impl>(
314 new WriteViewBuddy::Impl(*mImpl, (uint8_t *)base, 0, len),
315 [base, len](WriteViewBuddy::Impl *i) {
316 (void)i->getAllocation()->unmap(base, len, nullptr);
317 delete i;
318 });
319 return AcquirableWriteViewBuddy(error, C2Fence(), WriteViewBuddy(rvi));
320 } else {
321 return AcquirableWriteViewBuddy(error, C2Fence(), WriteViewBuddy(error));
322 }
323}
324
325C2ConstLinearBlock C2LinearBlock::share(size_t offset_, size_t size_, C2Fence fence) {
326 return ConstLinearBlockBuddy(mImpl, C2LinearRange(*this, offset_, size_), fence);
327}
328
329C2BasicLinearBlockPool::C2BasicLinearBlockPool(
330 const std::shared_ptr<C2Allocator> &allocator)
331 : mAllocator(allocator) { }
332
333c2_status_t C2BasicLinearBlockPool::fetchLinearBlock(
334 uint32_t capacity,
335 C2MemoryUsage usage,
336 std::shared_ptr<C2LinearBlock> *block /* nonnull */) {
337 block->reset();
338
339 std::shared_ptr<C2LinearAllocation> alloc;
340 c2_status_t err = mAllocator->newLinearAllocation(capacity, usage, &alloc);
341 if (err != C2_OK) {
342 return err;
343 }
344
345 *block = _C2BlockFactory::CreateLinearBlock(alloc);
346
347 return C2_OK;
348}
349
350struct C2_HIDE C2PooledBlockPoolData : _C2BlockPoolData {
351
352 virtual type_t getType() const override {
353 return TYPE_BUFFERPOOL;
354 }
355
356 void getBufferPoolData(std::shared_ptr<BufferPoolData> *data) const {
357 *data = mData;
358 }
359
360 C2PooledBlockPoolData(const std::shared_ptr<BufferPoolData> &data) : mData(data) {}
361
362 virtual ~C2PooledBlockPoolData() override {}
363
364private:
365 std::shared_ptr<BufferPoolData> mData;
366};
367
368bool _C2BlockFactory::GetBufferPoolData(
369 const std::shared_ptr<const _C2BlockPoolData> &data,
370 std::shared_ptr<BufferPoolData> *bufferPoolData) {
371 if (data && data->getType() == _C2BlockPoolData::TYPE_BUFFERPOOL) {
372 const std::shared_ptr<const C2PooledBlockPoolData> poolData =
373 std::static_pointer_cast<const C2PooledBlockPoolData>(data);
374 poolData->getBufferPoolData(bufferPoolData);
375 return true;
376 }
377 return false;
378}
379
380std::shared_ptr<C2LinearBlock> _C2BlockFactory::CreateLinearBlock(
381 const std::shared_ptr<C2LinearAllocation> &alloc,
382 const std::shared_ptr<_C2BlockPoolData> &data, size_t offset, size_t size) {
383 std::shared_ptr<C2Block1D::Impl> impl =
384 std::make_shared<C2Block1D::Impl>(alloc, data, offset, size);
385 return std::shared_ptr<C2LinearBlock>(new C2LinearBlock(impl, *impl));
386}
387
388std::shared_ptr<_C2BlockPoolData> _C2BlockFactory::GetLinearBlockPoolData(
389 const C2Block1D &block) {
390 if (block.mImpl) {
391 return block.mImpl->poolData();
392 }
393 return nullptr;
394}
395
396std::shared_ptr<C2LinearBlock> _C2BlockFactory::CreateLinearBlock(
397 const C2Handle *handle) {
398 // TODO: get proper allocator? and mutex?
John Stultzf2d67112020-09-19 06:06:11 +0000399 static std::shared_ptr<C2Allocator> sAllocator = []{
400 std::shared_ptr<C2Allocator> allocator;
401 std::shared_ptr<C2AllocatorStore> allocatorStore = android::GetCodec2PlatformAllocatorStore();
402 allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &allocator);
403
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800404 return allocator;
405 }();
406
407 if (sAllocator == nullptr)
408 return nullptr;
409
John Stultz653ddd12020-09-19 05:26:24 +0000410 bool isValidHandle = sAllocator->checkHandle(handle);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800411
412 std::shared_ptr<C2LinearAllocation> alloc;
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800413 if (isValidHandle) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800414 c2_status_t err = sAllocator->priorLinearAllocation(handle, &alloc);
415 if (err == C2_OK) {
416 std::shared_ptr<C2LinearBlock> block = _C2BlockFactory::CreateLinearBlock(alloc);
417 return block;
418 }
419 }
420 return nullptr;
421}
422
423std::shared_ptr<C2LinearBlock> _C2BlockFactory::CreateLinearBlock(
424 const C2Handle *cHandle, const std::shared_ptr<BufferPoolData> &data) {
425 // TODO: get proper allocator? and mutex?
John Stultzf2d67112020-09-19 06:06:11 +0000426 static std::shared_ptr<C2Allocator> sAllocator = []{
427 std::shared_ptr<C2Allocator> allocator;
428 std::shared_ptr<C2AllocatorStore> allocatorStore = android::GetCodec2PlatformAllocatorStore();
429 allocatorStore->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &allocator);
430
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800431 return allocator;
432 }();
433
434 if (sAllocator == nullptr)
435 return nullptr;
436
John Stultz653ddd12020-09-19 05:26:24 +0000437 bool isValidHandle = sAllocator->checkHandle(cHandle);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800438
439 std::shared_ptr<C2LinearAllocation> alloc;
Pin-chih Linaa18ea52019-11-19 18:48:50 +0800440 if (isValidHandle) {
Sungtak Leedc5cb622019-07-25 14:22:36 -0700441 c2_status_t err = sAllocator->priorLinearAllocation(cHandle, &alloc);
442 const std::shared_ptr<C2PooledBlockPoolData> poolData =
443 std::make_shared<C2PooledBlockPoolData>(data);
444 if (err == C2_OK && poolData) {
445 // TODO: config params?
446 std::shared_ptr<C2LinearBlock> block =
447 _C2BlockFactory::CreateLinearBlock(alloc, poolData);
448 return block;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800449 }
450 }
451 return nullptr;
452};
453
454/**
455 * Wrapped C2Allocator which is injected to buffer pool on behalf of
456 * C2BlockPool.
457 */
458class _C2BufferPoolAllocator : public BufferPoolAllocator {
459public:
460 _C2BufferPoolAllocator(const std::shared_ptr<C2Allocator> &allocator)
461 : mAllocator(allocator) {}
462
463 ~_C2BufferPoolAllocator() override {}
464
465 ResultStatus allocate(const std::vector<uint8_t> &params,
466 std::shared_ptr<BufferPoolAllocation> *alloc,
467 size_t *allocSize) override;
468
469 bool compatible(const std::vector<uint8_t> &newParams,
470 const std::vector<uint8_t> &oldParams) override;
471
472 // Methods for codec2 component (C2BlockPool).
473 /**
474 * Transforms linear allocation parameters for C2Allocator to parameters
475 * for buffer pool.
476 *
477 * @param capacity size of linear allocation
478 * @param usage memory usage pattern for linear allocation
479 * @param params allocation parameters for buffer pool
480 */
481 void getLinearParams(uint32_t capacity, C2MemoryUsage usage,
482 std::vector<uint8_t> *params);
483
484 /**
485 * Transforms graphic allocation parameters for C2Allocator to parameters
486 * for buffer pool.
487 *
488 * @param width width of graphic allocation
489 * @param height height of graphic allocation
490 * @param format color format of graphic allocation
491 * @param params allocation parameter for buffer pool
492 */
493 void getGraphicParams(uint32_t width, uint32_t height,
494 uint32_t format, C2MemoryUsage usage,
495 std::vector<uint8_t> *params);
496
497 /**
498 * Transforms an existing native handle to an C2LinearAllcation.
499 * Wrapper to C2Allocator#priorLinearAllocation
500 */
501 c2_status_t priorLinearAllocation(
502 const C2Handle *handle,
503 std::shared_ptr<C2LinearAllocation> *c2Allocation);
504
505 /**
506 * Transforms an existing native handle to an C2GraphicAllcation.
507 * Wrapper to C2Allocator#priorGraphicAllocation
508 */
509 c2_status_t priorGraphicAllocation(
510 const C2Handle *handle,
511 std::shared_ptr<C2GraphicAllocation> *c2Allocation);
512
513private:
514 static constexpr int kMaxIntParams = 5; // large enough number;
515
516 enum AllocType : uint8_t {
517 ALLOC_NONE = 0,
518
519 ALLOC_LINEAR,
520 ALLOC_GRAPHIC,
521 };
522
523 union AllocParams {
524 struct {
525 AllocType allocType;
526 C2MemoryUsage usage;
527 uint32_t params[kMaxIntParams];
528 } data;
529 uint8_t array[0];
530
531 AllocParams() : data{ALLOC_NONE, {0, 0}, {0}} {}
532 AllocParams(C2MemoryUsage usage, uint32_t capacity)
533 : data{ALLOC_LINEAR, usage, {[0] = capacity}} {}
534 AllocParams(
535 C2MemoryUsage usage,
536 uint32_t width, uint32_t height, uint32_t format)
537 : data{ALLOC_GRAPHIC, usage, {width, height, format}} {}
538 };
539
540 const std::shared_ptr<C2Allocator> mAllocator;
541};
542
543struct LinearAllocationDtor {
544 LinearAllocationDtor(const std::shared_ptr<C2LinearAllocation> &alloc)
545 : mAllocation(alloc) {}
546
547 void operator()(BufferPoolAllocation *poolAlloc) { delete poolAlloc; }
548
549 const std::shared_ptr<C2LinearAllocation> mAllocation;
550};
551
552struct GraphicAllocationDtor {
553 GraphicAllocationDtor(const std::shared_ptr<C2GraphicAllocation> &alloc)
554 : mAllocation(alloc) {}
555
556 void operator()(BufferPoolAllocation *poolAlloc) { delete poolAlloc; }
557
558 const std::shared_ptr<C2GraphicAllocation> mAllocation;
559};
560
561ResultStatus _C2BufferPoolAllocator::allocate(
562 const std::vector<uint8_t> &params,
563 std::shared_ptr<BufferPoolAllocation> *alloc,
564 size_t *allocSize) {
565 AllocParams c2Params;
566 memcpy(&c2Params, params.data(), std::min(sizeof(AllocParams), params.size()));
567 c2_status_t status = C2_BAD_VALUE;
568 switch(c2Params.data.allocType) {
569 case ALLOC_NONE:
570 break;
571 case ALLOC_LINEAR: {
572 std::shared_ptr<C2LinearAllocation> c2Linear;
573 status = mAllocator->newLinearAllocation(
574 c2Params.data.params[0], c2Params.data.usage, &c2Linear);
575 if (status == C2_OK && c2Linear) {
576 BufferPoolAllocation *ptr = new BufferPoolAllocation(c2Linear->handle());
577 if (ptr) {
578 *alloc = std::shared_ptr<BufferPoolAllocation>(
579 ptr, LinearAllocationDtor(c2Linear));
580 if (*alloc) {
581 *allocSize = (size_t)c2Params.data.params[0];
582 return ResultStatus::OK;
583 }
584 delete ptr;
585 }
586 return ResultStatus::NO_MEMORY;
587 }
588 break;
589 }
590 case ALLOC_GRAPHIC: {
591 std::shared_ptr<C2GraphicAllocation> c2Graphic;
592 status = mAllocator->newGraphicAllocation(
593 c2Params.data.params[0],
594 c2Params.data.params[1],
595 c2Params.data.params[2],
596 c2Params.data.usage, &c2Graphic);
597 if (status == C2_OK && c2Graphic) {
598 BufferPoolAllocation *ptr = new BufferPoolAllocation(c2Graphic->handle());
599 if (ptr) {
600 *alloc = std::shared_ptr<BufferPoolAllocation>(
601 ptr, GraphicAllocationDtor(c2Graphic));
602 if (*alloc) {
603 *allocSize = c2Params.data.params[0] * c2Params.data.params[1];
604 return ResultStatus::OK;
605 }
606 delete ptr;
607 }
608 return ResultStatus::NO_MEMORY;
609 }
610 break;
611 }
612 default:
613 break;
614 }
615 return ResultStatus::CRITICAL_ERROR;
616}
617
618bool _C2BufferPoolAllocator::compatible(
619 const std::vector<uint8_t> &newParams,
620 const std::vector<uint8_t> &oldParams) {
621 AllocParams newAlloc;
622 AllocParams oldAlloc;
623 memcpy(&newAlloc, newParams.data(), std::min(sizeof(AllocParams), newParams.size()));
624 memcpy(&oldAlloc, oldParams.data(), std::min(sizeof(AllocParams), oldParams.size()));
625
626 // TODO: support not exact matching. e.g) newCapacity < oldCapacity
627 if (newAlloc.data.allocType == oldAlloc.data.allocType &&
628 newAlloc.data.usage.expected == oldAlloc.data.usage.expected) {
629 for (int i = 0; i < kMaxIntParams; ++i) {
630 if (newAlloc.data.params[i] != oldAlloc.data.params[i]) {
631 return false;
632 }
633 }
634 return true;
635 }
636 return false;
637}
638
639void _C2BufferPoolAllocator::getLinearParams(
640 uint32_t capacity, C2MemoryUsage usage, std::vector<uint8_t> *params) {
641 AllocParams c2Params(usage, capacity);
642 params->assign(c2Params.array, c2Params.array + sizeof(AllocParams));
643}
644
645void _C2BufferPoolAllocator::getGraphicParams(
646 uint32_t width, uint32_t height, uint32_t format, C2MemoryUsage usage,
647 std::vector<uint8_t> *params) {
648 AllocParams c2Params(usage, width, height, format);
649 params->assign(c2Params.array, c2Params.array + sizeof(AllocParams));
650}
651
652c2_status_t _C2BufferPoolAllocator::priorLinearAllocation(
653 const C2Handle *handle,
654 std::shared_ptr<C2LinearAllocation> *c2Allocation) {
655 return mAllocator->priorLinearAllocation(handle, c2Allocation);
656}
657
658c2_status_t _C2BufferPoolAllocator::priorGraphicAllocation(
659 const C2Handle *handle,
660 std::shared_ptr<C2GraphicAllocation> *c2Allocation) {
661 return mAllocator->priorGraphicAllocation(handle, c2Allocation);
662}
663
664class C2PooledBlockPool::Impl {
665public:
666 Impl(const std::shared_ptr<C2Allocator> &allocator)
667 : mInit(C2_OK),
668 mBufferPoolManager(ClientManager::getInstance()),
669 mAllocator(std::make_shared<_C2BufferPoolAllocator>(allocator)) {
670 if (mAllocator && mBufferPoolManager) {
671 if (mBufferPoolManager->create(
672 mAllocator, &mConnectionId) == ResultStatus::OK) {
673 return;
674 }
675 }
676 mInit = C2_NO_INIT;
677 }
678
679 ~Impl() {
680 if (mInit == C2_OK) {
681 mBufferPoolManager->close(mConnectionId);
682 }
683 }
684
685 c2_status_t fetchLinearBlock(
686 uint32_t capacity, C2MemoryUsage usage,
687 std::shared_ptr<C2LinearBlock> *block /* nonnull */) {
688 block->reset();
689 if (mInit != C2_OK) {
690 return mInit;
691 }
692 std::vector<uint8_t> params;
693 mAllocator->getLinearParams(capacity, usage, &params);
694 std::shared_ptr<BufferPoolData> bufferPoolData;
695 native_handle_t *cHandle = nullptr;
696 ResultStatus status = mBufferPoolManager->allocate(
697 mConnectionId, params, &cHandle, &bufferPoolData);
698 if (status == ResultStatus::OK) {
Sungtak Leedc5cb622019-07-25 14:22:36 -0700699 std::shared_ptr<C2LinearAllocation> alloc;
700 std::shared_ptr<C2PooledBlockPoolData> poolData =
701 std::make_shared<C2PooledBlockPoolData>(bufferPoolData);
702 c2_status_t err = mAllocator->priorLinearAllocation(cHandle, &alloc);
703 if (err == C2_OK && poolData && alloc) {
704 *block = _C2BlockFactory::CreateLinearBlock(alloc, poolData, 0, capacity);
705 if (*block) {
706 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800707 }
708 }
709 return C2_NO_MEMORY;
710 }
711 if (status == ResultStatus::NO_MEMORY) {
712 return C2_NO_MEMORY;
713 }
714 return C2_CORRUPTED;
715 }
716
717 c2_status_t fetchGraphicBlock(
718 uint32_t width, uint32_t height, uint32_t format,
719 C2MemoryUsage usage,
720 std::shared_ptr<C2GraphicBlock> *block) {
721 block->reset();
722 if (mInit != C2_OK) {
723 return mInit;
724 }
725 std::vector<uint8_t> params;
726 mAllocator->getGraphicParams(width, height, format, usage, &params);
727 std::shared_ptr<BufferPoolData> bufferPoolData;
728 native_handle_t *cHandle = nullptr;
729 ResultStatus status = mBufferPoolManager->allocate(
730 mConnectionId, params, &cHandle, &bufferPoolData);
731 if (status == ResultStatus::OK) {
Sungtak Leedc5cb622019-07-25 14:22:36 -0700732 std::shared_ptr<C2GraphicAllocation> alloc;
733 std::shared_ptr<C2PooledBlockPoolData> poolData =
734 std::make_shared<C2PooledBlockPoolData>(bufferPoolData);
735 c2_status_t err = mAllocator->priorGraphicAllocation(
736 cHandle, &alloc);
737 if (err == C2_OK && poolData && alloc) {
738 *block = _C2BlockFactory::CreateGraphicBlock(
739 alloc, poolData, C2Rect(width, height));
740 if (*block) {
741 return C2_OK;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800742 }
743 }
744 return C2_NO_MEMORY;
745 }
746 if (status == ResultStatus::NO_MEMORY) {
747 return C2_NO_MEMORY;
748 }
749 return C2_CORRUPTED;
750 }
751
752 ConnectionId getConnectionId() {
753 return mInit != C2_OK ? INVALID_CONNECTIONID : mConnectionId;
754 }
755
756private:
757 c2_status_t mInit;
758 const android::sp<ClientManager> mBufferPoolManager;
759 ConnectionId mConnectionId; // locally
760 const std::shared_ptr<_C2BufferPoolAllocator> mAllocator;
761};
762
763C2PooledBlockPool::C2PooledBlockPool(
764 const std::shared_ptr<C2Allocator> &allocator, const local_id_t localId)
765 : mAllocator(allocator), mLocalId(localId), mImpl(new Impl(allocator)) {}
766
767C2PooledBlockPool::~C2PooledBlockPool() {
768}
769
770c2_status_t C2PooledBlockPool::fetchLinearBlock(
771 uint32_t capacity,
772 C2MemoryUsage usage,
773 std::shared_ptr<C2LinearBlock> *block /* nonnull */) {
774 if (mImpl) {
775 return mImpl->fetchLinearBlock(capacity, usage, block);
776 }
777 return C2_CORRUPTED;
778}
779
780c2_status_t C2PooledBlockPool::fetchGraphicBlock(
781 uint32_t width,
782 uint32_t height,
783 uint32_t format,
784 C2MemoryUsage usage,
785 std::shared_ptr<C2GraphicBlock> *block) {
786 if (mImpl) {
787 return mImpl->fetchGraphicBlock(width, height, format, usage, block);
788 }
789 return C2_CORRUPTED;
790}
791
792int64_t C2PooledBlockPool::getConnectionId() {
793 if (mImpl) {
794 return mImpl->getConnectionId();
795 }
796 return 0;
797}
798
799/* ========================================== 2D BLOCK ========================================= */
800
801/**
802 * Implementation that is shared between all 2D blocks and views.
803 *
804 * For blocks' Impl's crop is always the allotted crop, even if it is a sub block.
805 *
806 * For views' Impl's crop is the mapped portion - which for now is always the
807 * allotted crop.
808 */
809class C2_HIDE _C2Block2DImpl : public _C2PlanarSectionAspect {
810public:
811 /**
812 * Impl's crop is always the or part of the allotted crop of the allocation.
813 */
814 _C2Block2DImpl(const std::shared_ptr<C2GraphicAllocation> &alloc,
815 const std::shared_ptr<_C2BlockPoolData> &poolData = nullptr,
816 const C2Rect &allottedCrop = C2Rect(~0u, ~0u))
817 : _C2PlanarSectionAspect(alloc.get(), allottedCrop),
818 mAllocation(alloc),
819 mPoolData(poolData) { }
820
821 virtual ~_C2Block2DImpl() = default;
822
823 /** returns pool data */
824 std::shared_ptr<_C2BlockPoolData> poolData() const {
825 return mPoolData;
826 }
827
828 /** returns native handle */
829 const C2Handle *handle() const {
830 return mAllocation ? mAllocation->handle() : nullptr;
831 }
832
833 /** returns the allocator's ID */
834 C2Allocator::id_t getAllocatorId() const {
835 // BAD_ID can only happen if this Impl class is initialized for a view - never for a block.
836 return mAllocation ? mAllocation->getAllocatorId() : C2Allocator::BAD_ID;
837 }
838
839 std::shared_ptr<C2GraphicAllocation> getAllocation() const {
840 return mAllocation;
841 }
842
843private:
844 std::shared_ptr<C2GraphicAllocation> mAllocation;
845 std::shared_ptr<_C2BlockPoolData> mPoolData;
846};
847
848class C2_HIDE _C2MappingBlock2DImpl
849 : public _C2Block2DImpl, public std::enable_shared_from_this<_C2MappingBlock2DImpl> {
850public:
851 using _C2Block2DImpl::_C2Block2DImpl;
852
853 virtual ~_C2MappingBlock2DImpl() override = default;
854
855 /**
856 * This class contains the mapped data pointer, and the potential error.
857 */
858 struct Mapped {
859 private:
860 friend class _C2MappingBlock2DImpl;
861
862 Mapped(const std::shared_ptr<_C2Block2DImpl> &impl, bool writable, C2Fence *fence __unused)
863 : mImpl(impl), mWritable(writable) {
864 memset(mData, 0, sizeof(mData));
865 const C2Rect crop = mImpl->crop();
866 // gralloc requires mapping the whole region of interest as we cannot
867 // map multiple regions
868 mError = mImpl->getAllocation()->map(
869 crop,
870 { C2MemoryUsage::CPU_READ, writable ? C2MemoryUsage::CPU_WRITE : 0 },
871 nullptr,
872 &mLayout,
873 mData);
874 if (mError != C2_OK) {
875 memset(&mLayout, 0, sizeof(mLayout));
876 memset(mData, 0, sizeof(mData));
877 memset(mOffsetData, 0, sizeof(mData));
878 } else {
879 // TODO: validate plane layout and
880 // adjust data pointers to the crop region's top left corner.
881 // fail if it is not on a subsampling boundary
882 for (size_t planeIx = 0; planeIx < mLayout.numPlanes; ++planeIx) {
883 const uint32_t colSampling = mLayout.planes[planeIx].colSampling;
884 const uint32_t rowSampling = mLayout.planes[planeIx].rowSampling;
885 if (crop.left % colSampling || crop.right() % colSampling
886 || crop.top % rowSampling || crop.bottom() % rowSampling) {
887 // cannot calculate data pointer
888 mImpl->getAllocation()->unmap(mData, crop, nullptr);
889 memset(&mLayout, 0, sizeof(mLayout));
890 memset(mData, 0, sizeof(mData));
891 memset(mOffsetData, 0, sizeof(mData));
892 mError = C2_BAD_VALUE;
893 return;
894 }
895 mOffsetData[planeIx] =
896 mData[planeIx] + (ssize_t)crop.left * mLayout.planes[planeIx].colInc
897 + (ssize_t)crop.top * mLayout.planes[planeIx].rowInc;
898 }
899 }
900 }
901
902 explicit Mapped(c2_status_t error)
903 : mImpl(nullptr), mWritable(false), mError(error) {
904 // CHECK(error != C2_OK);
905 memset(&mLayout, 0, sizeof(mLayout));
906 memset(mData, 0, sizeof(mData));
907 memset(mOffsetData, 0, sizeof(mData));
908 }
909
910 public:
911 ~Mapped() {
912 if (mData[0] != nullptr) {
913 mImpl->getAllocation()->unmap(mData, mImpl->crop(), nullptr);
914 }
915 }
916
917 /** returns mapping status */
918 c2_status_t error() const { return mError; }
919
920 /** returns data pointer */
921 uint8_t *const *data() const { return mOffsetData; }
922
923 /** returns the plane layout */
924 C2PlanarLayout layout() const { return mLayout; }
925
926 /** returns whether the mapping is writable */
927 bool writable() const { return mWritable; }
928
929 private:
930 const std::shared_ptr<_C2Block2DImpl> mImpl;
931 bool mWritable;
932 c2_status_t mError;
933 uint8_t *mData[C2PlanarLayout::MAX_NUM_PLANES];
934 uint8_t *mOffsetData[C2PlanarLayout::MAX_NUM_PLANES];
935 C2PlanarLayout mLayout;
936 };
937
938 /**
939 * Maps the allotted region.
940 *
941 * If already mapped and it is currently in use, returns the existing mapping.
942 * If fence is provided, an acquire fence is stored there.
943 */
944 std::shared_ptr<Mapped> map(bool writable, C2Fence *fence) {
945 std::lock_guard<std::mutex> lock(mMappedLock);
946 std::shared_ptr<Mapped> existing = mMapped.lock();
947 if (!existing) {
948 existing = std::shared_ptr<Mapped>(new Mapped(shared_from_this(), writable, fence));
949 mMapped = existing;
950 } else {
951 // if we mapped the region read-only, we cannot remap it read-write
952 if (writable && !existing->writable()) {
953 existing = std::shared_ptr<Mapped>(new Mapped(C2_CANNOT_DO));
954 }
955 if (fence != nullptr) {
956 *fence = C2Fence();
957 }
958 }
959 return existing;
960 }
961
962private:
963 std::weak_ptr<Mapped> mMapped;
964 std::mutex mMappedLock;
965};
966
967class C2_HIDE _C2MappedBlock2DImpl : public _C2Block2DImpl {
968public:
969 _C2MappedBlock2DImpl(const _C2Block2DImpl &impl,
970 std::shared_ptr<_C2MappingBlock2DImpl::Mapped> mapping)
971 : _C2Block2DImpl(impl), mMapping(mapping) {
972 }
973
974 virtual ~_C2MappedBlock2DImpl() override = default;
975
976 std::shared_ptr<_C2MappingBlock2DImpl::Mapped> mapping() const { return mMapping; }
977
978private:
979 std::shared_ptr<_C2MappingBlock2DImpl::Mapped> mMapping;
980};
981
982/**
983 * Block implementation.
984 */
985class C2Block2D::Impl : public _C2MappingBlock2DImpl {
986public:
987 using _C2MappingBlock2DImpl::_C2MappingBlock2DImpl;
988 virtual ~Impl() override = default;
989};
990
991const C2Handle *C2Block2D::handle() const {
992 return mImpl->handle();
993}
994
995C2Allocator::id_t C2Block2D::getAllocatorId() const {
996 return mImpl->getAllocatorId();
997}
998
999C2Block2D::C2Block2D(std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect &section)
1000 // always clamp subsection to parent (impl) crop for safety
1001 : _C2PlanarSectionAspect(impl.get(), section.crop()), mImpl(impl) {
1002}
1003
1004/**
1005 * Graphic view implementation.
1006 *
1007 * range of Impl is the mapped range of the underlying allocation. range of View is the current
1008 * crop.
1009 */
1010class C2GraphicView::Impl : public _C2MappedBlock2DImpl {
1011public:
1012 using _C2MappedBlock2DImpl::_C2MappedBlock2DImpl;
1013 virtual ~Impl() override = default;
1014};
1015
1016C2GraphicView::C2GraphicView(std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect &section)
1017 : _C2EditablePlanarSectionAspect(impl.get(), section.crop()), mImpl(impl) {
1018}
1019
1020const uint8_t *const *C2GraphicView::data() const {
1021 return mImpl->mapping()->data();
1022}
1023
1024uint8_t *const *C2GraphicView::data() {
1025 return mImpl->mapping()->data();
1026}
1027
1028const C2PlanarLayout C2GraphicView::layout() const {
1029 return mImpl->mapping()->layout();
1030}
1031
1032const C2GraphicView C2GraphicView::subView(const C2Rect &rect) const {
1033 return C2GraphicView(mImpl, C2PlanarSection(*mImpl, rect));
1034}
1035
1036C2GraphicView C2GraphicView::subView(const C2Rect &rect) {
1037 return C2GraphicView(mImpl, C2PlanarSection(*mImpl, rect));
1038}
1039
1040c2_status_t C2GraphicView::error() const {
1041 return mImpl->mapping()->error();
1042}
1043
1044/**
1045 * Const graphic block implementation.
1046 */
1047C2ConstGraphicBlock::C2ConstGraphicBlock(
1048 std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect &section, C2Fence fence)
1049 : C2Block2D(impl, section), mFence(fence) { }
1050
1051C2Acquirable<const C2GraphicView> C2ConstGraphicBlock::map() const {
1052 C2Fence fence;
1053 std::shared_ptr<_C2MappingBlock2DImpl::Mapped> mapping =
1054 mImpl->map(false /* writable */, &fence);
1055 std::shared_ptr<GraphicViewBuddy::Impl> gvi =
1056 std::shared_ptr<GraphicViewBuddy::Impl>(new GraphicViewBuddy::Impl(*mImpl, mapping));
1057 return AcquirableConstGraphicViewBuddy(
1058 mapping->error(), fence, GraphicViewBuddy(gvi, C2PlanarSection(*mImpl, crop())));
1059}
1060
1061C2ConstGraphicBlock C2ConstGraphicBlock::subBlock(const C2Rect &rect) const {
1062 return C2ConstGraphicBlock(mImpl, C2PlanarSection(*mImpl, crop().intersect(rect)), mFence);
1063}
1064
1065/**
1066 * Graphic block implementation.
1067 */
1068C2GraphicBlock::C2GraphicBlock(
1069 std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect &section)
1070 : C2Block2D(impl, section) { }
1071
1072C2Acquirable<C2GraphicView> C2GraphicBlock::map() {
1073 C2Fence fence;
1074 std::shared_ptr<_C2MappingBlock2DImpl::Mapped> mapping =
1075 mImpl->map(true /* writable */, &fence);
1076 std::shared_ptr<GraphicViewBuddy::Impl> gvi =
1077 std::shared_ptr<GraphicViewBuddy::Impl>(new GraphicViewBuddy::Impl(*mImpl, mapping));
1078 return AcquirableGraphicViewBuddy(
1079 mapping->error(), fence, GraphicViewBuddy(gvi, C2PlanarSection(*mImpl, crop())));
1080}
1081
1082C2ConstGraphicBlock C2GraphicBlock::share(const C2Rect &crop, C2Fence fence) {
1083 return ConstGraphicBlockBuddy(mImpl, C2PlanarSection(*mImpl, crop), fence);
1084}
1085
1086/**
1087 * Basic block pool implementations.
1088 */
1089C2BasicGraphicBlockPool::C2BasicGraphicBlockPool(
1090 const std::shared_ptr<C2Allocator> &allocator)
1091 : mAllocator(allocator) {}
1092
1093c2_status_t C2BasicGraphicBlockPool::fetchGraphicBlock(
1094 uint32_t width,
1095 uint32_t height,
1096 uint32_t format,
1097 C2MemoryUsage usage,
1098 std::shared_ptr<C2GraphicBlock> *block /* nonnull */) {
1099 block->reset();
1100
1101 std::shared_ptr<C2GraphicAllocation> alloc;
1102 c2_status_t err = mAllocator->newGraphicAllocation(width, height, format, usage, &alloc);
1103 if (err != C2_OK) {
1104 return err;
1105 }
1106
1107 *block = _C2BlockFactory::CreateGraphicBlock(alloc);
1108
1109 return C2_OK;
1110}
1111
1112std::shared_ptr<C2GraphicBlock> _C2BlockFactory::CreateGraphicBlock(
1113 const std::shared_ptr<C2GraphicAllocation> &alloc,
1114 const std::shared_ptr<_C2BlockPoolData> &data, const C2Rect &allottedCrop) {
1115 std::shared_ptr<C2Block2D::Impl> impl =
1116 std::make_shared<C2Block2D::Impl>(alloc, data, allottedCrop);
1117 return std::shared_ptr<C2GraphicBlock>(new C2GraphicBlock(impl, *impl));
1118}
1119
1120std::shared_ptr<_C2BlockPoolData> _C2BlockFactory::GetGraphicBlockPoolData(
1121 const C2Block2D &block) {
1122 if (block.mImpl) {
1123 return block.mImpl->poolData();
1124 }
1125 return nullptr;
1126}
1127
1128std::shared_ptr<C2GraphicBlock> _C2BlockFactory::CreateGraphicBlock(
1129 const C2Handle *cHandle,
1130 const std::shared_ptr<BufferPoolData> &data) {
1131 // TODO: get proper allocator? and mutex?
1132 static std::unique_ptr<C2AllocatorGralloc> sAllocator = std::make_unique<C2AllocatorGralloc>(0);
1133
1134 std::shared_ptr<C2GraphicAllocation> alloc;
John Stultz653ddd12020-09-19 05:26:24 +00001135 if (sAllocator->isValid(cHandle)) {
Sungtak Leedc5cb622019-07-25 14:22:36 -07001136 c2_status_t err = sAllocator->priorGraphicAllocation(cHandle, &alloc);
1137 const std::shared_ptr<C2PooledBlockPoolData> poolData =
1138 std::make_shared<C2PooledBlockPoolData>(data);
1139 if (err == C2_OK && poolData) {
1140 // TODO: config setup?
1141 std::shared_ptr<C2GraphicBlock> block =
1142 _C2BlockFactory::CreateGraphicBlock(alloc, poolData);
1143 return block;
Pawin Vongmasa36653902018-11-15 00:10:25 -08001144 }
1145 }
1146 return nullptr;
1147};
1148
1149
1150/* ========================================== BUFFER ========================================= */
1151
1152class C2BufferData::Impl {
1153public:
1154 explicit Impl(const std::vector<C2ConstLinearBlock> &blocks)
1155 : mType(blocks.size() == 1 ? LINEAR : LINEAR_CHUNKS),
1156 mLinearBlocks(blocks) {
1157 }
1158
1159 explicit Impl(const std::vector<C2ConstGraphicBlock> &blocks)
1160 : mType(blocks.size() == 1 ? GRAPHIC : GRAPHIC_CHUNKS),
1161 mGraphicBlocks(blocks) {
1162 }
1163
1164 type_t type() const { return mType; }
1165 const std::vector<C2ConstLinearBlock> &linearBlocks() const { return mLinearBlocks; }
1166 const std::vector<C2ConstGraphicBlock> &graphicBlocks() const { return mGraphicBlocks; }
1167
1168private:
1169 type_t mType;
1170 std::vector<C2ConstLinearBlock> mLinearBlocks;
1171 std::vector<C2ConstGraphicBlock> mGraphicBlocks;
1172};
1173
1174C2BufferData::C2BufferData(const std::vector<C2ConstLinearBlock> &blocks) : mImpl(new Impl(blocks)) {}
1175C2BufferData::C2BufferData(const std::vector<C2ConstGraphicBlock> &blocks) : mImpl(new Impl(blocks)) {}
1176
1177C2BufferData::type_t C2BufferData::type() const { return mImpl->type(); }
1178
1179const std::vector<C2ConstLinearBlock> C2BufferData::linearBlocks() const {
1180 return mImpl->linearBlocks();
1181}
1182
1183const std::vector<C2ConstGraphicBlock> C2BufferData::graphicBlocks() const {
1184 return mImpl->graphicBlocks();
1185}
1186
1187class C2Buffer::Impl {
1188public:
1189 Impl(C2Buffer *thiz, const std::vector<C2ConstLinearBlock> &blocks)
1190 : mThis(thiz), mData(blocks) {}
1191 Impl(C2Buffer *thiz, const std::vector<C2ConstGraphicBlock> &blocks)
1192 : mThis(thiz), mData(blocks) {}
1193
1194 ~Impl() {
1195 for (const auto &pair : mNotify) {
1196 pair.first(mThis, pair.second);
1197 }
1198 }
1199
1200 const C2BufferData &data() const { return mData; }
1201
1202 c2_status_t registerOnDestroyNotify(OnDestroyNotify onDestroyNotify, void *arg) {
1203 auto it = std::find_if(
1204 mNotify.begin(), mNotify.end(),
1205 [onDestroyNotify, arg] (const auto &pair) {
1206 return pair.first == onDestroyNotify && pair.second == arg;
1207 });
1208 if (it != mNotify.end()) {
1209 return C2_DUPLICATE;
1210 }
1211 mNotify.emplace_back(onDestroyNotify, arg);
1212 return C2_OK;
1213 }
1214
1215 c2_status_t unregisterOnDestroyNotify(OnDestroyNotify onDestroyNotify, void *arg) {
1216 auto it = std::find_if(
1217 mNotify.begin(), mNotify.end(),
1218 [onDestroyNotify, arg] (const auto &pair) {
1219 return pair.first == onDestroyNotify && pair.second == arg;
1220 });
1221 if (it == mNotify.end()) {
1222 return C2_NOT_FOUND;
1223 }
1224 mNotify.erase(it);
1225 return C2_OK;
1226 }
1227
1228 std::vector<std::shared_ptr<const C2Info>> info() const {
1229 std::vector<std::shared_ptr<const C2Info>> result(mInfos.size());
1230 std::transform(
1231 mInfos.begin(), mInfos.end(), result.begin(),
1232 [] (const auto &elem) { return elem.second; });
1233 return result;
1234 }
1235
1236 c2_status_t setInfo(const std::shared_ptr<C2Info> &info) {
1237 // To "update" you need to erase the existing one if any, and then insert.
1238 (void) mInfos.erase(info->coreIndex());
1239 (void) mInfos.insert({ info->coreIndex(), info });
1240 return C2_OK;
1241 }
1242
1243 bool hasInfo(C2Param::Type index) const {
1244 return mInfos.count(index.coreIndex()) > 0;
1245 }
1246
1247 std::shared_ptr<const C2Info> getInfo(C2Param::Type index) const {
1248 auto it = mInfos.find(index.coreIndex());
1249 if (it == mInfos.end()) {
1250 return nullptr;
1251 }
1252 return std::const_pointer_cast<const C2Info>(it->second);
1253 }
1254
1255 std::shared_ptr<C2Info> removeInfo(C2Param::Type index) {
1256 auto it = mInfos.find(index.coreIndex());
1257 if (it == mInfos.end()) {
1258 return nullptr;
1259 }
1260 std::shared_ptr<C2Info> ret = it->second;
1261 (void) mInfos.erase(it);
1262 return ret;
1263 }
1264
1265private:
1266 C2Buffer * const mThis;
1267 BufferDataBuddy mData;
1268 std::map<C2Param::CoreIndex, std::shared_ptr<C2Info>> mInfos;
1269 std::list<std::pair<OnDestroyNotify, void *>> mNotify;
1270};
1271
1272C2Buffer::C2Buffer(const std::vector<C2ConstLinearBlock> &blocks)
1273 : mImpl(new Impl(this, blocks)) {}
1274
1275C2Buffer::C2Buffer(const std::vector<C2ConstGraphicBlock> &blocks)
1276 : mImpl(new Impl(this, blocks)) {}
1277
1278const C2BufferData C2Buffer::data() const { return mImpl->data(); }
1279
1280c2_status_t C2Buffer::registerOnDestroyNotify(OnDestroyNotify onDestroyNotify, void *arg) {
1281 return mImpl->registerOnDestroyNotify(onDestroyNotify, arg);
1282}
1283
1284c2_status_t C2Buffer::unregisterOnDestroyNotify(OnDestroyNotify onDestroyNotify, void *arg) {
1285 return mImpl->unregisterOnDestroyNotify(onDestroyNotify, arg);
1286}
1287
1288const std::vector<std::shared_ptr<const C2Info>> C2Buffer::info() const {
1289 return mImpl->info();
1290}
1291
1292c2_status_t C2Buffer::setInfo(const std::shared_ptr<C2Info> &info) {
1293 return mImpl->setInfo(info);
1294}
1295
1296bool C2Buffer::hasInfo(C2Param::Type index) const {
1297 return mImpl->hasInfo(index);
1298}
1299
1300std::shared_ptr<const C2Info> C2Buffer::getInfo(C2Param::Type index) const {
1301 return mImpl->getInfo(index);
1302}
1303
1304std::shared_ptr<C2Info> C2Buffer::removeInfo(C2Param::Type index) {
1305 return mImpl->removeInfo(index);
1306}
1307
1308// static
1309std::shared_ptr<C2Buffer> C2Buffer::CreateLinearBuffer(const C2ConstLinearBlock &block) {
1310 return std::shared_ptr<C2Buffer>(new C2Buffer({ block }));
1311}
1312
1313// static
1314std::shared_ptr<C2Buffer> C2Buffer::CreateGraphicBuffer(const C2ConstGraphicBlock &block) {
1315 return std::shared_ptr<C2Buffer>(new C2Buffer({ block }));
1316}