1 /* 2 * Copyright (c) 2022 Rockchip Electronics 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 * Author: Cody Xie <cody.xie@rock-chips.com> 17 */ 18 #ifndef ALGOS_ACAC_LUT_BUFFER_H 19 #define ALGOS_ACAC_LUT_BUFFER_H 20 21 #include <cstdint> 22 23 #include "common/rk_aiq_types_priv.h" 24 25 namespace RkCam { 26 27 enum class LutBufferState { 28 kInitial = 0, 29 kWait2Chip = 1, 30 kChipInUse = 2, 31 }; 32 33 struct LutBufferConfig { 34 bool IsBigMode; 35 uint32_t Width; 36 uint32_t Height; 37 uint32_t LutHCount; 38 uint32_t LutVCount; 39 uint8_t ScaleFactor; 40 uint16_t PsfCfgCount; 41 }; 42 43 struct LutBuffer { 44 LutBuffer() = delete; LutBufferLutBuffer45 explicit LutBuffer(const LutBufferConfig& config) 46 : State(LutBufferState::kInitial), Config(config), Fd(-1), Size(0), Addr(nullptr) {} LutBufferLutBuffer47 LutBuffer(const LutBufferConfig& config, const rk_aiq_cac_share_mem_info_t* mem_info) 48 : Config(config) { 49 State = static_cast<LutBufferState>(*mem_info->state); 50 Fd = mem_info->fd; 51 Addr = mem_info->addr; 52 Size = mem_info->size; 53 } 54 LutBuffer(const LutBuffer&) = delete; 55 LutBuffer& operator=(const LutBuffer&) = delete; 56 57 LutBufferState State; 58 LutBufferConfig Config; 59 int Fd; 60 int Size; 61 void* Addr; 62 }; 63 64 class LutBufferManager { 65 public: 66 LutBufferManager() = delete; LutBufferManager(const LutBufferConfig & config,const isp_drv_share_mem_ops_t * mem_ops)67 LutBufferManager(const LutBufferConfig& config, const isp_drv_share_mem_ops_t* mem_ops) 68 : mem_ops_(mem_ops), mem_ctx_(nullptr), config_(config) {} 69 LutBufferManager(const LutBufferManager&) = delete; 70 LutBufferManager& operator=(const LutBufferManager&) = delete; ~LutBufferManager()71 ~LutBufferManager() { 72 // TODO(Cody) 73 ReleaseHwBuffers(0); 74 ReleaseHwBuffers(1); 75 } 76 ImportHwBuffers(uint8_t isp_id)77 void ImportHwBuffers(uint8_t isp_id) { 78 assert(mem_ops_ != nullptr); 79 rk_aiq_share_mem_config_t hw_config; 80 hw_config.mem_type = MEM_TYPE_CAC; 81 hw_config.alloc_param.width = config_.Width; 82 hw_config.alloc_param.height = config_.Height; 83 hw_config.alloc_param.reserved[0] = 1; 84 85 mem_ops_->alloc_mem(isp_id, (void*)(mem_ops_), &hw_config, &mem_ctx_); 86 } 87 ReleaseHwBuffers(uint8_t isp_id)88 void ReleaseHwBuffers(uint8_t isp_id) { 89 if (mem_ctx_ != nullptr && mem_ops_ != nullptr) mem_ops_->release_mem(isp_id, mem_ctx_); 90 } 91 GetFreeHwBuffer(uint8_t isp_id)92 LutBuffer* GetFreeHwBuffer(uint8_t isp_id) { 93 if (mem_ops_ == nullptr || mem_ctx_ == nullptr) { 94 return nullptr; 95 } 96 97 const auto* mem_info = static_cast<const rk_aiq_cac_share_mem_info_t*>( 98 mem_ops_->get_free_item(isp_id, mem_ctx_)); 99 if (mem_info != nullptr) { 100 auto* lut_buf = new LutBuffer(config_, mem_info); 101 if (lut_buf != nullptr) { 102 return lut_buf; 103 } 104 } 105 return nullptr; 106 } 107 108 private: 109 const isp_drv_share_mem_ops_t* mem_ops_; 110 void* mem_ctx_; 111 LutBufferConfig config_; 112 }; 113 114 } // namespace RkCam 115 116 #endif // ALGOS_ACAC_LUT_BUFFER_H 117