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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "Codec2-GraphicBufferAllocator"
18
19
20 #include <media/stagefright/foundation/ADebug.h>
21
22 #include <codec2/aidl/GraphicBufferAllocator.h>
23 #include <codec2/aidl/GraphicsTracker.h>
24
25 namespace aidl::android::hardware::media::c2::implementation {
26
allocate(const IGraphicBufferAllocator::Description & in_desc,IGraphicBufferAllocator::Allocation * _aidl_return)27 ::ndk::ScopedAStatus GraphicBufferAllocator::allocate(
28 const IGraphicBufferAllocator::Description& in_desc,
29 IGraphicBufferAllocator::Allocation* _aidl_return) {
30 AHardwareBuffer *buf;
31 ::android::sp<::android::Fence> fence;
32 c2_status_t ret = allocate(
33 in_desc.width, in_desc.height, in_desc.format, in_desc.usage,
34 &buf, &fence);
35 if (ret == C2_OK) {
36 _aidl_return->buffer.reset(buf);
37 _aidl_return->fence = ::ndk::ScopedFileDescriptor(fence->dup());
38 return ::ndk::ScopedAStatus::ok();
39 }
40 return ::ndk::ScopedAStatus::fromServiceSpecificError(ret);
41 }
42
deallocate(int64_t in_id,bool * _aidl_return)43 ::ndk::ScopedAStatus GraphicBufferAllocator::deallocate(int64_t in_id, bool* _aidl_return) {
44 *_aidl_return = deallocate(in_id, ::android::Fence::NO_FENCE);
45 return ::ndk::ScopedAStatus::ok();
46 }
47
getWaitableFd(::ndk::ScopedFileDescriptor * _aidl_return)48 ::ndk::ScopedAStatus GraphicBufferAllocator::getWaitableFd(
49 ::ndk::ScopedFileDescriptor* _aidl_return) {
50 int pipeFd;
51 c2_status_t ret = mGraphicsTracker->getWaitableFd(&pipeFd);
52 if (ret == C2_OK) {
53 _aidl_return->set(pipeFd);
54 return ::ndk::ScopedAStatus::ok();
55 }
56 return ::ndk::ScopedAStatus::fromServiceSpecificError(ret);
57 }
58
configure(const::android::sp<IGraphicBufferProducer> & igbp,uint32_t generation,int maxDequeueBufferCount)59 bool GraphicBufferAllocator::configure(
60 const ::android::sp<IGraphicBufferProducer>& igbp,
61 uint32_t generation,
62 int maxDequeueBufferCount) {
63 c2_status_t ret = C2_OK;
64
65 ret = mGraphicsTracker->configureGraphics(igbp, generation);
66 if (ret != C2_OK) {
67 ALOGE("configuring igbp failed gen #(%d), configuring max dequeue count didn't happen",
68 (unsigned int)generation);
69 return false;
70 }
71
72 ret = mGraphicsTracker->configureMaxDequeueCount(maxDequeueBufferCount);
73 if (ret != C2_OK) {
74 ALOGE("configuring max dequeue count to %d failed", maxDequeueBufferCount);
75 return false;
76 }
77 return true;
78 }
79
updateMaxDequeueBufferCount(int count)80 void GraphicBufferAllocator::updateMaxDequeueBufferCount(int count) {
81 c2_status_t ret = mGraphicsTracker->configureMaxDequeueCount(count);
82 if (ret != C2_OK) {
83 ALOGE("updating max dequeue buffer count failed %d", ret);
84 }
85 }
86
reset()87 void GraphicBufferAllocator::reset() {
88 mGraphicsTracker->stop();
89 }
90
onBufferReleased(uint32_t generation)91 void GraphicBufferAllocator::onBufferReleased(uint32_t generation) {
92 mGraphicsTracker->onReleased(generation);
93 }
94
onBufferAttached(uint32_t generation)95 void GraphicBufferAllocator::onBufferAttached(uint32_t generation) {
96 mGraphicsTracker->onAttached(generation);
97 }
98
pollForRenderedFrames(FrameEventHistoryDelta * delta)99 void GraphicBufferAllocator::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
100 mGraphicsTracker->pollForRenderedFrames(delta);
101 }
102
allocate(uint32_t width,uint32_t height,::android::PixelFormat format,uint64_t usage,AHardwareBuffer ** buf,::android::sp<::android::Fence> * fence)103 c2_status_t GraphicBufferAllocator::allocate(
104 uint32_t width, uint32_t height, ::android::PixelFormat format, uint64_t usage,
105 AHardwareBuffer **buf, ::android::sp<::android::Fence> *fence) {
106 return mGraphicsTracker->allocate(width, height, format, usage, buf, fence);
107 }
108
deallocate(const uint64_t id,const::android::sp<::android::Fence> & fence)109 bool GraphicBufferAllocator::deallocate(const uint64_t id,
110 const ::android::sp<::android::Fence> &fence) {
111 c2_status_t ret = mGraphicsTracker->deallocate(id, fence);
112 if (ret != C2_OK) {
113 ALOGW("deallocate() %llu was not successful %d", (unsigned long long)id, ret);
114 return false;
115 }
116 return true;
117 }
118
displayBuffer(const C2ConstGraphicBlock & block,const IGraphicBufferProducer::QueueBufferInput & input,IGraphicBufferProducer::QueueBufferOutput * output)119 c2_status_t GraphicBufferAllocator::displayBuffer(
120 const C2ConstGraphicBlock& block,
121 const IGraphicBufferProducer::QueueBufferInput& input,
122 IGraphicBufferProducer::QueueBufferOutput *output) {
123 return mGraphicsTracker->render(block, input, output);
124 }
125
onRequestStop()126 void GraphicBufferAllocator::onRequestStop() {
127 mGraphicsTracker->onRequestStop();
128 }
129
~GraphicBufferAllocator()130 GraphicBufferAllocator::~GraphicBufferAllocator() {}
131
CreateGraphicBufferAllocator(int maxDequeueCount)132 std::shared_ptr<GraphicBufferAllocator> GraphicBufferAllocator::CreateGraphicBufferAllocator(
133 int maxDequeueCount) {
134 return ::ndk::SharedRefBase::make<GraphicBufferAllocator>(maxDequeueCount);
135 }
136
GraphicBufferAllocator(int maxDequeueCount)137 GraphicBufferAllocator::GraphicBufferAllocator(int maxDequeueCount)
138 : mGraphicsTracker(GraphicsTracker::CreateGraphicsTracker(maxDequeueCount)) {}
139
140 } // namespace aidl::android::hardware::media::c2::implementation
141