1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CMD_LIST_HELPER_H 17 #define CMD_LIST_HELPER_H 18 19 #include <utility> 20 #include <memory> 21 22 #include "image/image.h" 23 #include "recording/cmd_list.h" 24 #include "utils/log.h" 25 26 namespace OHOS { 27 namespace Media { 28 class PixelMap; 29 } 30 namespace Rosen { 31 namespace Drawing { 32 class CmdListHelper { 33 public: 34 CmdListHelper() = default; 35 ~CmdListHelper() = default; 36 37 static ImageHandle AddImageToCmdList(CmdList& cmdList, const Image& image); 38 static ImageHandle AddImageToCmdList(CmdList& cmdList, const std::shared_ptr<Image>& image); 39 static std::shared_ptr<Image> GetImageFromCmdList(const CmdList& cmdList, const ImageHandle& imageHandle); 40 static ImageHandle AddBitmapToCmdList(CmdList& cmdList, const Bitmap& bitmap); 41 static std::shared_ptr<Bitmap> GetBitmapFromCmdList(const CmdList& cmdList, const ImageHandle& bitmapHandle); 42 static ImageHandle AddPixelMapToCmdList(CmdList& cmdList, const std::shared_ptr<Media::PixelMap>& pixelMap); 43 static std::shared_ptr<Media::PixelMap> GetPixelMapFromCmdList( 44 const CmdList& cmdList, const ImageHandle& pixelMapHandle); 45 static ImageHandle AddPictureToCmdList(CmdList& cmdList, const Picture& picture); 46 static std::shared_ptr<Picture> GetPictureFromCmdList(const CmdList& cmdList, const ImageHandle& pictureHandle); 47 static ImageHandle AddCompressDataToCmdList(CmdList& cmdList, const std::shared_ptr<Data>& data); 48 static std::shared_ptr<Data> GetCompressDataFromCmdList(const CmdList& cmdList, const ImageHandle& imageHandle); 49 50 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType & recorded)51 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType& recorded) 52 { 53 if (recorded.GetDrawingType() != DrawingType::RECORDING) { 54 LOGE("recorded is invalid!"); 55 return { 0 }; 56 } 57 58 auto recording = static_cast<const RecordingType&>(recorded); 59 if (recording.GetCmdList() == nullptr) { 60 LOGE("recorded cmdlist is invalid!"); 61 return { 0 }; 62 } 63 64 return AddChildToCmdList(cmdList, recording.GetCmdList()); 65 } 66 67 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const std::shared_ptr<CommonType> & recorded)68 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const std::shared_ptr<CommonType>& recorded) 69 { 70 if (recorded == nullptr) { 71 return { 0 }; 72 } 73 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 74 LOGE("recorded is invalid!"); 75 return { 0 }; 76 } 77 78 auto recording = std::static_pointer_cast<RecordingType>(recorded); 79 if (recording->GetCmdList() == nullptr) { 80 LOGE("recorded cmdlist is invalid!"); 81 return { 0 }; 82 } 83 84 return AddChildToCmdList(cmdList, recording->GetCmdList()); 85 } 86 87 template<typename RecordingType, typename CommonType> AddRecordedToCmdList(CmdList & cmdList,const CommonType * recorded)88 static CmdListHandle AddRecordedToCmdList(CmdList& cmdList, const CommonType* recorded) 89 { 90 if (recorded == nullptr) { 91 return { 0 }; 92 } 93 if (recorded->GetDrawingType() != DrawingType::RECORDING) { 94 LOGE("recorded is invalid!"); 95 return { 0 }; 96 } 97 98 auto recording = static_cast<const RecordingType*>(recorded); 99 if (recording->GetCmdList() == nullptr) { 100 LOGE("recorded cmdlist is invalid!"); 101 return { 0 }; 102 } 103 104 return AddChildToCmdList(cmdList, recording->GetCmdList()); 105 } 106 107 template<typename CmdListType, typename Type> GetFromCmdList(const CmdList & cmdList,const CmdListHandle & handler)108 static std::shared_ptr<Type> GetFromCmdList(const CmdList& cmdList, const CmdListHandle& handler) 109 { 110 auto childCmdList = GetChildFromCmdList<CmdListType>(cmdList, handler); 111 if (childCmdList == nullptr) { 112 LOGE("child cmdlist is invalid!"); 113 return nullptr; 114 } 115 116 return childCmdList->Playback(); 117 } 118 119 template<typename Type> AddVectorToCmdList(CmdList & cmdList,const std::vector<Type> & vec)120 static std::pair<uint32_t, size_t> AddVectorToCmdList(CmdList& cmdList, const std::vector<Type>& vec) 121 { 122 std::pair<uint32_t, size_t> ret(0, 0); 123 if (!vec.empty()) { 124 const void* data = static_cast<const void*>(vec.data()); 125 size_t size = vec.size() * sizeof(Type); 126 auto offset = cmdList.AddCmdListData(std::make_pair(data, size)); 127 ret = { offset, size }; 128 } 129 130 return ret; 131 } 132 133 template<typename Type> GetVectorFromCmdList(const CmdList & cmdList,std::pair<uint32_t,size_t> info)134 static std::vector<Type> GetVectorFromCmdList(const CmdList& cmdList, std::pair<uint32_t, size_t> info) 135 { 136 std::vector<Type> ret; 137 const auto* values = static_cast<const Type*>(cmdList.GetCmdListData(info.first)); 138 auto size = info.second / sizeof(Type); 139 if (values != nullptr && size > 0) { 140 for (size_t i = 0; i < size; i++) { 141 ret.push_back(*values); 142 values++; 143 } 144 } 145 return ret; 146 } 147 148 static CmdListHandle AddChildToCmdList(CmdList& cmdList, const std::shared_ptr<CmdList>& child); 149 150 template<typename CmdListType> GetChildFromCmdList(const CmdList & cmdList,const CmdListHandle & childHandle)151 static std::shared_ptr<CmdListType> GetChildFromCmdList(const CmdList& cmdList, const CmdListHandle& childHandle) 152 { 153 if (childHandle.size == 0) { 154 return std::make_shared<CmdListType>(); 155 } 156 157 const void* childData = cmdList.GetCmdListData(childHandle.offset); 158 if (childData == nullptr) { 159 LOGE("child offset is invalid!"); 160 return nullptr; 161 } 162 163 auto childCmdList = CmdListType::CreateFromData({ childData, childHandle.size }); 164 if (childCmdList == nullptr) { 165 LOGE("create child CmdList failed!"); 166 return nullptr; 167 } 168 169 if (childHandle.imageSize > 0 && cmdList.GetImageData(childHandle.imageOffset) != nullptr) { 170 if (!childCmdList->SetUpImageData(cmdList.GetImageData(childHandle.imageOffset), childHandle.imageSize)) { 171 LOGE("set up child image data failed!"); 172 } 173 } 174 175 return childCmdList; 176 } 177 }; 178 } // namespace Drawing 179 } // namespace Rosen 180 } // namespace OHOS 181 #endif