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_ACE_IMAGE_SOURCE_INFO_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H 18 19 #include <optional> 20 #include <utility> 21 22 #include "base/geometry/dimension.h" 23 #include "base/geometry/size.h" 24 #include "base/image/pixel_map.h" 25 #include "base/resource/internal_resource.h" 26 #include "base/utils/device_config.h" 27 #include "core/components/common/layout/constants.h" 28 #include "core/components/common/properties/color.h" 29 #include "core/components_ng/pattern/image/image_dfx.h" 30 31 namespace OHOS::Ace { 32 namespace NG { 33 class FrameNode; 34 } 35 36 class ACE_FORCE_EXPORT ImageSourceInfo { 37 public: 38 // add constructor method for decompressed hap 39 ImageSourceInfo(std::string imageSrc, std::string bundleName, std::string moduleName, 40 Dimension width = Dimension(-1), Dimension height = Dimension(-1), 41 InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID, 42 const RefPtr<PixelMap>& pixmap = nullptr); 43 44 ImageSourceInfo(const std::shared_ptr<std::string> &imageSrc, std::string bundleName, std::string moduleName, 45 Dimension width = Dimension(-1), Dimension height = Dimension(-1), 46 InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID, 47 const RefPtr<PixelMap>& pixmap = nullptr); 48 49 explicit ImageSourceInfo(std::string imageSrc, Dimension width = Dimension(-1), Dimension height = Dimension(-1), 50 InternalResource::ResourceId resourceId = InternalResource::ResourceId::NO_ID, 51 const RefPtr<PixelMap>& pixmap = nullptr) 52 : ImageSourceInfo(std::move(imageSrc), "", "", width, height, resourceId, pixmap) 53 {} 54 ImageSourceInfo(const RefPtr<PixelMap> & pixmap)55 explicit ImageSourceInfo(const RefPtr<PixelMap>& pixmap) 56 : ImageSourceInfo("", Dimension(-1), Dimension(-1), InternalResource::ResourceId::NO_ID, pixmap) 57 {} 58 ImageSourceInfo() = default; 59 ~ImageSourceInfo() = default; 60 61 // static functions 62 static bool IsSVGSource(const std::string& imageSrc, SrcType srcType, InternalResource::ResourceId resourceId); 63 static bool IsPngSource(const std::string& src, InternalResource::ResourceId resourceId); 64 static SrcType ResolveURIType(const std::string& uri); 65 static bool IsValidBase64Head(const std::string& uri, const std::string& pattern); 66 static bool IsUriOfDataAbilityEncoded(const std::string& uri, const std::string& pattern); 67 static ImageSourceInfo CreateImageSourceInfoWithHost(const RefPtr<NG::FrameNode>& host); 68 69 // operators 70 bool operator==(const ImageSourceInfo& info) const; 71 bool operator!=(const ImageSourceInfo& info) const; 72 73 // interfaces to change [ImageSourceInfo] 74 void SetSrc(const std::string& src, std::optional<Color> fillColor = std::nullopt); 75 void SetResourceId(InternalResource::ResourceId id, std::optional<Color> fillColor = std::nullopt); 76 void SetPixMap(const RefPtr<PixelMap>& pixmap, std::optional<Color> fillColor = std::nullopt); 77 void SetDimension(Dimension width, Dimension Height); 78 79 [[deprecated("use ImageRenderProperty::SetFillColor or SvgCanvasImage::SetFillColor")]] 80 void SetFillColor(const Color& color); 81 void SetBundleName(const std::string& bundleName); 82 void SetModuleName(const std::string& moduleName); 83 void SetIsUriPureNumber(bool isUriPureNumber = false) 84 { 85 isUriPureNumber_ = isUriPureNumber; 86 } 87 void Reset(); 88 89 // interfaces to get infomation from [ImageSourceInfo] 90 void GenerateCacheKey(); 91 bool IsInternalResource() const; 92 bool IsValid() const; 93 bool IsPng() const; 94 bool IsSvg() const; 95 bool IsPixmap() const; 96 bool IsSourceDimensionValid() const; 97 const std::string& GetBundleName() const; 98 const std::string& GetModuleName() const; 99 std::string ToString(bool isNeedTruncated = true) const; 100 InternalResource::ResourceId GetResourceId() const; 101 SrcType GetSrcType() const; 102 Size GetSourceSize() const; 103 const std::string& GetSrc() const; 104 const std::optional<Color>& GetFillColor() const; 105 const RefPtr<PixelMap>& GetPixmap() const; 106 std::string GetKey() const; 107 // Generates a task key that includes the current running container ID. 108 std::string GetTaskKey() const; 109 void SetContainerId(int32_t containerId); 110 int32_t GetContainerId() const; GetIsUriPureNumber()111 bool GetIsUriPureNumber() const 112 { 113 return isUriPureNumber_; 114 } 115 bool SupportObjCache() const; SetNeedCache(bool needCache)116 void SetNeedCache(bool needCache) 117 { 118 needCache_ = needCache; 119 } 120 GetLocalColorMode()121 ColorMode GetLocalColorMode() const 122 { 123 return localColorMode_; 124 } IsFromReset()125 bool IsFromReset() 126 { 127 return isFromReset_; 128 } SetIsFromReset(bool isFromReset)129 void SetIsFromReset(bool isFromReset) 130 { 131 isFromReset_ = isFromReset; 132 } 133 SetImageDfxConfig(const NG::ImageDfxConfig & imageDfxConfig)134 void SetImageDfxConfig(const NG::ImageDfxConfig& imageDfxConfig) 135 { 136 imageDfxConfig_ = imageDfxConfig; 137 } 138 GetImageDfxConfig()139 NG::ImageDfxConfig GetImageDfxConfig() const 140 { 141 return imageDfxConfig_; 142 } 143 144 private: 145 SrcType ResolveSrcType() const; 146 147 std::string src_; 148 std::shared_ptr<std::string> srcRef_ = nullptr; 149 std::string cacheKey_; 150 int32_t containerId_ = 0; 151 // Interim programme 152 std::string bundleName_; 153 std::string moduleName_; 154 Dimension sourceWidth_ = Dimension(-1); 155 Dimension sourceHeight_ = Dimension(-1); 156 InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID; 157 RefPtr<PixelMap> pixmap_; 158 bool isSvg_ = false; 159 bool isPng_ = false; 160 bool needCache_ = true; 161 bool isUriPureNumber_ = false; 162 bool isFromReset_ = false; 163 [[deprecated("in NG")]] 164 std::optional<Color> fillColor_; 165 const uint8_t* pixmapBuffer_ = nullptr; 166 NG::ImageDfxConfig imageDfxConfig_; 167 168 // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY, 169 SrcType srcType_ = SrcType::UNSUPPORTED; 170 171 ColorMode localColorMode_ = ColorMode::COLOR_MODE_UNDEFINED; 172 }; 173 174 } // namespace OHOS::Ace 175 176 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H 177