1 /* 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 17 #pragma once 18 19 #include <aidl/android/hardware/media/c2/BnGraphicBufferAllocator.h> 20 21 #include <android-base/unique_fd.h> 22 #include <gui/IGraphicBufferProducer.h> 23 24 #include <memory> 25 26 #include <C2Buffer.h> 27 28 namespace aidl::android::hardware::media::c2::implementation { 29 30 // forward declarations 31 class GraphicsTracker; 32 33 struct GraphicBufferAllocator : public BnGraphicBufferAllocator { 34 public: 35 // HAL interfaces 36 ::ndk::ScopedAStatus allocate(const IGraphicBufferAllocator::Description& in_desc, 37 IGraphicBufferAllocator::Allocation* _aidl_return) override; 38 39 ::ndk::ScopedAStatus deallocate(int64_t in_id, bool* _aidl_return) override; 40 41 ::ndk::ScopedAStatus getWaitableFd( 42 ::ndk::ScopedFileDescriptor* _aidl_return) override; 43 44 /** 45 * Configuring Surface/BufferQueue for the interface. 46 * 47 * Configure Surface, generation # and max dequeueBuffer() count for 48 * allocate interface. 49 * 50 * @param igbp Surface where to allocate. 51 * @param generation Generation # for allocations. 52 * @param maxDequeueBufferCount 53 * Maximum # of pending allocations. 54 */ 55 bool configure(const ::android::sp<::android::IGraphicBufferProducer>& igbp, 56 uint32_t generation, 57 int maxDequeueBufferCount); 58 59 /** 60 * Update max dequeue buffer count of BufferQueue. 61 * 62 * BufferQueue does not update this value if count is smaller 63 * than the currently dequeued count. 64 * TODO: better to update the value inside this interface. 65 * for return value inspection from BQ, also for delayed updates. 66 * 67 * @param count the new value to update 68 */ 69 void updateMaxDequeueBufferCount(int count); 70 71 void reset(); 72 73 /** 74 * Create a listener for buffer being released. 75 * 76 * Surface will register this listener and notify whenever the consumer 77 * releases a buffer. 78 * 79 * @param generation generation # for the BufferQueue. 80 * @return IProducerListener can be used when connect# to Surface. 81 */ 82 const ::android::sp<::android::IProducerListener> createReleaseListener( 83 uint32_t generation); 84 85 /** 86 * Notifies a buffer being released. 87 * 88 * @param generation generation # for the BufferQueue. 89 */ 90 void onBufferReleased(uint32_t generation); 91 92 /** 93 * Allocates a buffer. 94 * 95 * @param width width of the requested buffer. 96 * @param height height of the requested buffer. 97 * @param format format of the requested buffer. 98 * @param usage usage of the requested buffer. 99 * @param buf out param for created buffer. 100 * @param fence out param for a pending fence. 101 * 102 * @return OK When an allocation was created. 103 * C2_BAD_STATE Client is not in the state for allocating 104 * C2_BLOCKING operation is blocked. Waitable fds can be 105 * used to know when it unblocks. 106 * C2_CORRUPTED Failed with a serious reason. 107 */ 108 c2_status_t allocate(uint32_t width, uint32_t height, 109 ::android::PixelFormat format, uint64_t usage, 110 AHardwareBuffer **buf, ::android::sp<::android::Fence> *fence); 111 112 /** 113 * De-allocate a buffer. 114 * 115 * @param id unique id for a buffer. 116 * @param fence write fence if it's deallocated due to 117 * cancellation of displaying 118 */ 119 bool deallocate(const uint64_t id, const ::android::sp<::android::Fence> &fence); 120 121 /** 122 * Display a graphic buffer to BufferQueue. 123 * 124 * @param block block to display to Surface. 125 * @param input input parameter for displaying. 126 * @param output out parameter from Surface. 127 */ 128 c2_status_t displayBuffer( 129 const C2ConstGraphicBlock& block, 130 const ::android::IGraphicBufferProducer::QueueBufferInput& input, 131 ::android::IGraphicBufferProducer::QueueBufferOutput *output); 132 133 ~GraphicBufferAllocator(); 134 135 /** 136 * Create the interface. 137 * 138 * The interface and codec instance's relationship is 1 to 1. 139 * The interface will be cretaed in the beginning of Codec createion. And 140 * lives until the instance destroyed. 141 * 142 * @param maxDequeueCount Initial max allocatable count 143 */ 144 static std::shared_ptr<GraphicBufferAllocator> CreateGraphicBufferAllocator( 145 int maxDequeueCount); 146 private: 147 GraphicBufferAllocator(int maxDequeueCount); 148 149 std::shared_ptr<GraphicsTracker> mGraphicsTracker; 150 151 friend class ::ndk::SharedRefBase; 152 }; 153 154 } // namespace aidl::android::hardware::media::c2::implementation 155