blob: a5676b7d60554cedc46af5917f5943bf22357eb5 [file] [log] [blame]
Sungtak Lee72dfba62023-09-07 23:26:30 +00001/*
2 * Copyright (C) 2023 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#pragma once
17
18#include <C2Buffer.h>
19
20#include <memory>
21
22namespace aidl::android::hardware::media::c2 {
23 class IGraphicBufferAllocator;
24}
25
26/**
27 * Codec2-AIDL IGraphicBufferAllocator backed C2BlockPool
28 *
29 * Graphic Blocks are created using IGraphicBufferAllocator C2AIDL interface.
30 */
31class C2IgbaBlockPool : public C2BlockPool {
32public:
33 explicit C2IgbaBlockPool(
34 const std::shared_ptr<C2Allocator> &allocator,
35 const std::shared_ptr<
36 ::aidl::android::hardware::media::c2::IGraphicBufferAllocator> &igba,
37 const local_id_t localId);
38
39 virtual ~C2IgbaBlockPool() = default;
40
41 virtual C2Allocator::id_t getAllocatorId() const override {
42 return mAllocator->getId();
43 }
44
45 virtual local_id_t getLocalId() const override {
46 return mLocalId;
47 }
48
49 /* Note: this is blocking due to H/W fence waiting */
50 virtual c2_status_t fetchGraphicBlock(
51 uint32_t width,
52 uint32_t height,
53 uint32_t format,
54 C2MemoryUsage usage,
55 std::shared_ptr<C2GraphicBlock> *block /* nonnull */) override;
56
57 virtual c2_status_t fetchGraphicBlock(
58 uint32_t width,
59 uint32_t height,
60 uint32_t format,
61 C2MemoryUsage usage,
62 std::shared_ptr<C2GraphicBlock> *block /* nonnull */,
63 C2Fence *fence /* nonnull */) override;
64
65 // Do we need this?
66 void invalidate();
67
68private:
69 c2_status_t _fetchGraphicBlock(
70 uint32_t width,
71 uint32_t height,
72 uint32_t format,
73 C2MemoryUsage usage,
74 c2_nsecs_t timeoutNs,
75 uint64_t *origId /* nonnull */,
76 std::shared_ptr<C2GraphicBlock> *block /* nonnull */,
77 C2Fence *fence /* nonnull */);
78
79 const std::shared_ptr<C2Allocator> mAllocator;
80 const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba;
81 const local_id_t mLocalId;
82 std::atomic<bool> mValid;
83 C2Fence mWaitFence;
84};
85
86typedef struct AHardwareBuffer AHardwareBuffer;
87
88struct C2IgbaBlockPoolData : public _C2BlockPoolData {
89
90 C2IgbaBlockPoolData(
91 const AHardwareBuffer *buffer,
92 const std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator>
93 &igba);
94
95 virtual ~C2IgbaBlockPoolData() override;
96
97 virtual type_t getType() const override;
98
99private:
100 friend struct _C2BlockFactory;
101
102 void getAHardwareBuffer(AHardwareBuffer **pBuf) const;
103
104 void disown();
105
106 bool mOwned;
107 const AHardwareBuffer *mBuffer;
108 const std::weak_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> mIgba;
109};