1 /* 2 * Copyright (C) 2019 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 #ifndef __BENCHMARK_C2_COMMON_H__ 18 #define __BENCHMARK_C2_COMMON_H__ 19 20 #include "codec2/hidl/client.h" 21 22 #include <C2Component.h> 23 #include <C2Config.h> 24 25 #include <hidl/HidlSupport.h> 26 27 #include <C2AllocatorIon.h> 28 #include <C2Buffer.h> 29 #include <C2BufferPriv.h> 30 31 #include "BenchmarkCommon.h" 32 33 #define MAX_RETRY 20 34 #define TIME_OUT 400ms 35 #define MAX_INPUT_BUFFERS 8 36 37 using android::C2AllocatorIon; 38 39 class LinearBuffer : public C2Buffer { 40 public: LinearBuffer(const std::shared_ptr<C2LinearBlock> & block)41 explicit LinearBuffer(const std::shared_ptr<C2LinearBlock> &block) 42 : C2Buffer({block->share(block->offset(), block->size(), ::C2Fence())}) {} 43 LinearBuffer(const std::shared_ptr<C2LinearBlock> & block,size_t size)44 explicit LinearBuffer(const std::shared_ptr<C2LinearBlock> &block, size_t size) 45 : C2Buffer({block->share(block->offset(), size, ::C2Fence())}) {} 46 }; 47 48 class GraphicBuffer : public C2Buffer { 49 public: GraphicBuffer(const std::shared_ptr<C2GraphicBlock> & block)50 explicit GraphicBuffer(const std::shared_ptr<C2GraphicBlock> &block) 51 : C2Buffer({block->share(C2Rect(block->width(), block->height()), ::C2Fence())}) {} 52 }; 53 54 /** 55 * Handle Callback functions onWorkDone(), onTripped(), 56 * onError(), onDeath(), onFramesRendered() for C2 Components 57 */ 58 struct CodecListener : public android::Codec2Client::Listener { 59 public: 60 CodecListener( 61 const std::function<void(std::list<std::unique_ptr<C2Work>> &workItems)> fn = nullptr) callBackCodecListener62 : callBack(fn) {} onWorkDoneCodecListener63 virtual void onWorkDone(const std::weak_ptr<android::Codec2Client::Component> &comp, 64 std::list<std::unique_ptr<C2Work>> &workItems) override { 65 ALOGV("onWorkDone called"); 66 (void)comp; 67 if (callBack) callBack(workItems); 68 } 69 onTrippedCodecListener70 virtual void onTripped( 71 const std::weak_ptr<android::Codec2Client::Component> &comp, 72 const std::vector<std::shared_ptr<C2SettingResult>> &settingResults) override { 73 (void)comp; 74 (void)settingResults; 75 } 76 onErrorCodecListener77 virtual void onError(const std::weak_ptr<android::Codec2Client::Component> &comp, 78 uint32_t errorCode) override { 79 (void)comp; 80 ALOGV("onError called"); 81 if (errorCode != 0) ALOGE("Error : %u", errorCode); 82 } 83 onDeathCodecListener84 virtual void onDeath(const std::weak_ptr<android::Codec2Client::Component> &comp) override { 85 (void)comp; 86 } 87 onInputBufferDoneCodecListener88 virtual void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex) override { 89 (void)frameIndex; 90 (void)arrayIndex; 91 } 92 onFrameRenderedCodecListener93 virtual void onFrameRendered(uint64_t bufferQueueId, int32_t slotId, 94 int64_t timestampNs) override { 95 (void)bufferQueueId; 96 (void)slotId; 97 (void)timestampNs; 98 } 99 100 std::function<void(std::list<std::unique_ptr<C2Work>> &workItems)> callBack; 101 }; 102 103 class BenchmarkC2Common { 104 public: BenchmarkC2Common()105 BenchmarkC2Common() 106 : mEos(false), 107 mStats(nullptr), 108 mClient(nullptr), 109 mBlockPoolId(0), 110 mLinearPool(nullptr), 111 mGraphicPool(nullptr), 112 mLinearAllocator(nullptr), 113 mGraphicAllocator(nullptr) {} 114 115 int32_t setupCodec2(); 116 117 vector<string> getSupportedComponentList(bool isEncoder); 118 119 void waitOnInputConsumption(); 120 121 // callback function to process onWorkDone received by Listener 122 void handleWorkDone(std::list<std::unique_ptr<C2Work>> &workItems); 123 124 bool mEos; 125 protected: 126 Stats *mStats; 127 128 std::shared_ptr<android::Codec2Client> mClient; 129 130 C2BlockPool::local_id_t mBlockPoolId; 131 std::shared_ptr<C2BlockPool> mLinearPool; 132 std::shared_ptr<C2BlockPool> mGraphicPool; 133 std::shared_ptr<C2Allocator> mLinearAllocator; 134 std::shared_ptr<C2Allocator> mGraphicAllocator; 135 136 std::mutex mQueueLock; 137 std::condition_variable mQueueCondition; 138 std::list<std::unique_ptr<C2Work>> mWorkQueue; 139 }; 140 141 #endif // __BENCHMARK_C2_COMMON_H__ 142