1 /* 2 * Copyright (c) 2019-2022 Rockchip Eletronics Co., Ltd. 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 RKCAM_SHARED_ITEM_POOL_H 18 #define RKCAM_SHARED_ITEM_POOL_H 19 20 #include <iostream> 21 #include <typeinfo> 22 23 #include "safe_list.h" 24 #include "buffer_pool.h" 25 26 using namespace XCam; 27 28 namespace RkCam { 29 30 class SharedItemBase : public BufferProxy { 31 public: SharedItemBase(const SmartPtr<BufferData> & data)32 explicit SharedItemBase (const SmartPtr<BufferData> &data):BufferProxy(data) {} 33 virtual ~SharedItemBase () = default; 34 setType(uint32_t type)35 void setType(uint32_t type) { _type = type; } setId(uint32_t id)36 void setId(uint32_t id) { _id = id; } 37 getType()38 int getType() { return _type; } getId()39 uint32_t getId() { return _id; } 40 41 protected: 42 XCAM_DEAD_COPY (SharedItemBase); 43 44 uint32_t _type = -1; 45 uint32_t _id = -1; 46 }; 47 48 class RkAiqFullParams; 49 class RkAiqIspStats; 50 typedef struct RkAiqSofInfoWrapper_s RkAiqSofInfoWrapper_t; 51 52 /** 53 * Two SFINAE helpers to check if class has typedef or has member function 54 * Reference: https://tinyurl.com/v4f2f5m 55 */ 56 /** 57 * @class : HAS_TYPEDEF 58 * @brief : This macro will be used to check if a class has a particular 59 * typedef or not. 60 * @param typedef_name : Name of Typedef 61 * @param name : Name of struct which is going to be run the test for 62 * the given particular typedef specified in typedef_name 63 */ 64 #define HAS_TYPEDEF(typedef_name, name) \ 65 template <typename T> \ 66 struct name { \ 67 typedef char yes[1]; \ 68 typedef char no[2]; \ 69 template <typename U> \ 70 struct type_check; \ 71 template <typename _1> \ 72 static yes& chk(type_check<typename _1::typedef_name>*); \ 73 template <typename> \ 74 static no& chk(...); \ 75 static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \ 76 } 77 78 /** 79 * @class : HAS_MEM_FUNC 80 * @brief : This macro will be used to check if a class has a particular 81 * member function implemented in the public section or not. 82 * @param func : Name of Member Function 83 * @param name : Name of struct which is going to be run the test for 84 * the given particular member function name specified in func 85 * @param return_type: Return type of the member function 86 * @param ellipsis(...) : Since this is macro should provide test case for every 87 * possible member function we use variadic macros to cover all possibilities 88 */ 89 #define HAS_MEM_FUNC(func, name, return_type, ...) \ 90 template <typename T> \ 91 struct name { \ 92 typedef return_type (T::*Sign)(__VA_ARGS__); \ 93 typedef char yes[1]; \ 94 typedef char no[2]; \ 95 template <typename U, U> \ 96 struct type_check; \ 97 template <typename _1> \ 98 static yes& chk(type_check<Sign, &_1::func>*); \ 99 template <typename> \ 100 static no& chk(...); \ 101 static bool const value = sizeof(chk<T>(0)) == sizeof(yes); \ 102 } 103 104 HAS_MEM_FUNC(reset, has_reset, void, void); 105 106 template<typename T> 107 class SharedItemProxy : public SharedItemBase 108 { 109 public: SharedItemProxy(const SmartPtr<T> & data)110 explicit SharedItemProxy(const SmartPtr<T> &data) : SharedItemBase(data), _data(data) {}; ~SharedItemProxy()111 virtual ~SharedItemProxy() { 112 reset(); 113 _data.release(); 114 LOG1_ANALYZER("Release item : %s", typeid(T).name()); 115 }; 116 117 template <typename U = T> reset()118 typename std::enable_if<has_reset<U>::value, bool>::type reset() { 119 _data->reset(); 120 return true; 121 } 122 123 template <typename U = T> reset()124 typename std::enable_if<!has_reset<U>::value, bool>::type reset() { 125 return false; 126 } 127 128 data()129 SmartPtr<T> &data() { 130 return _data; 131 } 132 map()133 uint8_t *map () { 134 return (uint8_t *)_data.ptr(); 135 } 136 private: 137 SmartPtr<T> _data; 138 XCAM_DEAD_COPY (SharedItemProxy); 139 }; 140 141 template<typename T> 142 class SharedItemPool 143 : public BufferPool 144 { 145 friend class SharedItemProxy<T>; 146 147 public: 148 explicit SharedItemPool(const char* name, uint32_t max_count = 8); 149 virtual ~SharedItemPool(); 150 151 SmartPtr<SharedItemProxy<T>> get_item(); 152 int8_t init(uint32_t max_count = 8); has_free_items()153 bool has_free_items () { 154 return has_free_buffers(); 155 } 156 private: 157 XCAM_DEAD_COPY (SharedItemPool); 158 159 protected: 160 const char* _name; 161 uint32_t _max_count; 162 virtual SmartPtr<BufferData> allocate_data (const VideoBufferInfo &buffer_info); 163 virtual SmartPtr<BufferProxy> create_buffer_from_data (SmartPtr<BufferData> &data); 164 }; 165 166 } 167 168 #include "shared_item_pool.cpp" 169 170 #endif //RKCAM_SHARED_ITEM_POOL_H 171 172 173