1 /* 2 * Copyright (c) 2021-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 FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H 18 19 #include <condition_variable> 20 #include <regex> 21 #include <string> 22 23 #include "include/core/SkImage.h" 24 25 #include "base/geometry/size.h" 26 #include "base/memory/ace_type.h" 27 #include "base/network/download_manager.h" 28 #include "base/resource/internal_resource.h" 29 #include "base/resource/shared_image_manager.h" 30 #include "core/components/common/layout/constants.h" 31 #include "core/components_ng/image_provider/image_data.h" 32 #include "core/components_ng/render/drawing_forward.h" 33 #include "core/image/image_source_info.h" 34 #include "core/pipeline/pipeline_base.h" 35 36 namespace OHOS::Ace { 37 38 class ImageLoader : public virtual AceType { 39 DECLARE_ACE_TYPE(ImageLoader, AceType); 40 41 public: 42 #ifndef USE_ROSEN_DRAWING 43 virtual sk_sp<SkData> LoadImageData( 44 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) = 0; 45 #else 46 virtual std::shared_ptr<RSData> LoadImageData( 47 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) = 0; 48 #endif LoadDecodedImageData(const ImageSourceInfo &,const WeakPtr<PipelineBase> &)49 virtual RefPtr<NG::ImageData> LoadDecodedImageData( 50 const ImageSourceInfo& /*imageSourceInfo*/, const WeakPtr<PipelineBase>& /*context*/) 51 { 52 return nullptr; 53 } 54 55 RefPtr<NG::ImageData> GetImageData( 56 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr); 57 58 static std::string RemovePathHead(const std::string& uri); 59 static RefPtr<ImageLoader> CreateImageLoader(const ImageSourceInfo& imageSourceInfo); 60 61 #ifndef USE_ROSEN_DRAWING 62 static sk_sp<SkData> LoadDataFromCachedFile(const std::string& uri); 63 #else 64 static std::shared_ptr<RSData> LoadDataFromCachedFile(const std::string& uri); 65 #endif 66 67 static std::shared_ptr<RSData> QueryImageDataFromImageCache(const ImageSourceInfo& sourceInfo); 68 static void CacheImageData(const std::string& key, const RefPtr<NG::ImageData>& data); 69 static RefPtr<NG::ImageData> LoadImageDataFromFileCache(const std::string& key, const std::string& suffix); 70 71 static void WriteCacheToFile(const std::string& uri, const std::vector<uint8_t>& imageData); 72 static void WriteCacheToFile(const std::string& uri, const std::string& imageData); 73 }; 74 75 // File image provider: read image from file. 76 class FileImageLoader : public ImageLoader { 77 public: 78 FileImageLoader() = default; 79 ~FileImageLoader() override = default; 80 #ifndef USE_ROSEN_DRAWING 81 sk_sp<SkData> LoadImageData( 82 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 83 #else 84 std::shared_ptr<RSData> LoadImageData( 85 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 86 #endif 87 }; 88 89 // data provider image loader. 90 class DataProviderImageLoader : public ImageLoader { 91 public: 92 DataProviderImageLoader() = default; 93 ~DataProviderImageLoader() override = default; 94 #ifndef USE_ROSEN_DRAWING 95 sk_sp<SkData> LoadImageData( 96 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 97 #else 98 std::shared_ptr<RSData> LoadImageData( 99 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 100 #endif 101 }; 102 103 class DecodedDataProviderImageLoader : public ImageLoader { 104 public: 105 DecodedDataProviderImageLoader() = default; 106 ~DecodedDataProviderImageLoader() override = default; 107 #ifndef USE_ROSEN_DRAWING 108 sk_sp<SkData> LoadImageData( 109 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 110 #else 111 std::shared_ptr<RSData> LoadImageData( 112 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 113 #endif 114 RefPtr<NG::ImageData> LoadDecodedImageData( 115 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 116 117 private: 118 static std::string GetThumbnailOrientation(const ImageSourceInfo& src); 119 }; 120 121 class AssetImageLoader final : public ImageLoader { 122 public: 123 AssetImageLoader() = default; 124 ~AssetImageLoader() override = default; 125 #ifndef USE_ROSEN_DRAWING 126 sk_sp<SkData> LoadImageData( 127 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 128 #else 129 std::shared_ptr<RSData> LoadImageData( 130 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 131 #endif 132 std::string LoadJsonData(const std::string& src, const WeakPtr<PipelineBase> context = nullptr); 133 }; 134 135 // Network image provider: read image from network. 136 class NetworkImageLoader final : public ImageLoader { 137 public: 138 NetworkImageLoader() = default; 139 ~NetworkImageLoader() override = default; 140 #ifndef USE_ROSEN_DRAWING 141 sk_sp<SkData> LoadImageData( 142 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 143 #else 144 std::shared_ptr<RSData> LoadImageData( 145 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 146 #endif 147 static bool DownloadImage(DownloadCallback&& downloadCallback, const std::string& src, bool sync, int32_t nodeId); 148 }; 149 150 class InternalImageLoader final : public ImageLoader { 151 public: 152 InternalImageLoader() = default; 153 ~InternalImageLoader() override = default; 154 #ifndef USE_ROSEN_DRAWING 155 sk_sp<SkData> LoadImageData( 156 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 157 #else 158 std::shared_ptr<RSData> LoadImageData( 159 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 160 #endif 161 }; 162 163 class Base64ImageLoader final : public ImageLoader { 164 public: 165 Base64ImageLoader() = default; 166 ~Base64ImageLoader() override = default; 167 static std::string_view GetBase64ImageCode(const std::string& uri); 168 #ifndef USE_ROSEN_DRAWING 169 sk_sp<SkData> LoadImageData( 170 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 171 #else 172 std::shared_ptr<RSData> LoadImageData( 173 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 174 #endif 175 }; 176 177 class ResourceImageLoader final : public ImageLoader { 178 public: 179 ResourceImageLoader() = default; 180 ~ResourceImageLoader() override = default; 181 #ifndef USE_ROSEN_DRAWING 182 sk_sp<SkData> LoadImageData( 183 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 184 #else 185 std::shared_ptr<RSData> LoadImageData( 186 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 187 #endif 188 189 private: 190 bool GetResourceId(const std::string& uri, uint32_t& resId) const; 191 bool GetResourceId(const std::string& uri, std::string& path) const; 192 bool GetResourceName(const std::string& uri, std::string& resName) const; 193 }; 194 195 class PixelMapImageLoader : public ImageLoader { 196 public: 197 PixelMapImageLoader() = default; 198 ~PixelMapImageLoader() override = default; 199 #ifndef USE_ROSEN_DRAWING 200 sk_sp<SkData> LoadImageData( 201 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 202 #else 203 std::shared_ptr<RSData> LoadImageData( 204 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 205 #endif 206 RefPtr<NG::ImageData> LoadDecodedImageData( 207 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 208 }; 209 210 class SharedMemoryImageLoader : public ImageLoader, public ImageProviderLoader { 211 DECLARE_ACE_TYPE(SharedMemoryImageLoader, ImageLoader); 212 213 public: 214 SharedMemoryImageLoader() = default; 215 ~SharedMemoryImageLoader() override = default; 216 #ifndef USE_ROSEN_DRAWING 217 sk_sp<SkData> LoadImageData(const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override; 218 #else 219 std::shared_ptr<RSData> LoadImageData( 220 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context) override; 221 #endif 222 void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) override; 223 224 private: 225 std::condition_variable cv_; 226 std::mutex mtx_; 227 std::vector<uint8_t> data_; 228 }; 229 class AstcImageLoader : public ImageLoader { 230 public: 231 AstcImageLoader() = default; 232 ~AstcImageLoader() override = default; 233 #ifndef USE_ROSEN_DRAWING 234 sk_sp<SkData> LoadImageData( 235 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 236 #else 237 std::shared_ptr<RSData> LoadImageData( 238 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 239 #endif 240 RefPtr<NG::ImageData> LoadDecodedImageData( 241 const ImageSourceInfo& imageSourceInfo, const WeakPtr<PipelineBase>& context = nullptr) override; 242 243 private: 244 static std::string GetThumbnailOrientation(const ImageSourceInfo& src); 245 }; 246 } // namespace OHOS::Ace 247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_LOADER_H 248