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 } // namespace NG 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 SrcType ResolveURIType(const std::string& uri); 64 static bool IsValidBase64Head(const std::string& uri, const std::string& pattern); 65 static bool IsUriOfDataAbilityEncoded(const std::string& uri, const std::string& pattern); 66 static ImageSourceInfo CreateImageSourceInfoWithHost(const RefPtr<NG::FrameNode>& host); 67 68 // operators 69 bool operator==(const ImageSourceInfo& info) const; 70 bool operator!=(const ImageSourceInfo& info) const; 71 72 // interfaces to change [ImageSourceInfo] 73 void SetSrc(const std::string& src, std::optional<Color> fillColor = std::nullopt); 74 void SetResourceId(InternalResource::ResourceId id, std::optional<Color> fillColor = std::nullopt); 75 void SetPixMap(const RefPtr<PixelMap>& pixmap, std::optional<Color> fillColor = std::nullopt); 76 void SetDimension(Dimension width, Dimension Height); 77 78 [[deprecated("use ImageRenderProperty::SetFillColor or SvgCanvasImage::SetFillColor")]] void SetFillColor( 79 const Color& color); 80 void SetBundleName(const std::string& bundleName); 81 void SetModuleName(const std::string& moduleName); 82 void SetIsUriPureNumber(bool isUriPureNumber = false) 83 { 84 isUriPureNumber_ = isUriPureNumber; 85 } 86 void Reset(); 87 88 // interfaces to get infomation from [ImageSourceInfo] 89 void GenerateCacheKey(); 90 bool IsInternalResource() const; 91 bool IsValid() const; 92 bool IsSvg() const; 93 bool IsPixmap() const; 94 bool IsSourceDimensionValid() const; 95 const std::string& GetBundleName() const; 96 const std::string& GetModuleName() const; 97 std::string ToString(bool isNeedTruncated = true) const; 98 InternalResource::ResourceId GetResourceId() const; 99 SrcType GetSrcType() const; 100 Size GetSourceSize() const; 101 const std::string& GetSrc() const; 102 const std::optional<Color>& GetFillColor() const; 103 const RefPtr<PixelMap>& GetPixmap() const; 104 std::string GetKey() const; 105 // Generates a task key that includes the current running container ID. 106 std::string GetTaskKey() const; 107 void SetContainerId(int32_t containerId); 108 int32_t GetContainerId() const; GetIsUriPureNumber()109 bool GetIsUriPureNumber() const 110 { 111 return isUriPureNumber_; 112 } 113 bool SupportObjCache() const; SetNeedCache(bool needCache)114 void SetNeedCache(bool needCache) 115 { 116 needCache_ = needCache; 117 } 118 GetLocalColorMode()119 ColorMode GetLocalColorMode() const 120 { 121 return localColorMode_; 122 } IsFromReset()123 bool IsFromReset() 124 { 125 return isFromReset_; 126 } SetIsFromReset(bool isFromReset)127 void SetIsFromReset(bool isFromReset) 128 { 129 isFromReset_ = isFromReset; 130 } 131 SetImageDfxConfig(const NG::ImageDfxConfig & imageDfxConfig)132 void SetImageDfxConfig(const NG::ImageDfxConfig& imageDfxConfig) 133 { 134 imageDfxConfig_ = imageDfxConfig; 135 } 136 137 void SetImageHdr(bool isHdr); 138 bool IsImageHdr() const; GetImageDfxConfig()139 NG::ImageDfxConfig GetImageDfxConfig() const 140 { 141 return imageDfxConfig_; 142 } 143 IsSurportCachePixelmap()144 bool IsSurportCachePixelmap() const 145 { 146 return srcType_ == SrcType::NETWORK || srcType_ == SrcType::RESOURCE; 147 } 148 149 private: 150 SrcType ResolveSrcType() const; 151 152 std::string src_; 153 std::shared_ptr<std::string> srcRef_ = nullptr; 154 std::string cacheKey_; 155 int32_t containerId_ = 0; 156 // Interim programme 157 std::string bundleName_; 158 std::string moduleName_; 159 Dimension sourceWidth_ = Dimension(-1); 160 Dimension sourceHeight_ = Dimension(-1); 161 InternalResource::ResourceId resourceId_ = InternalResource::ResourceId::NO_ID; 162 RefPtr<PixelMap> pixmap_; 163 bool isSvg_ = false; 164 bool needCache_ = true; 165 bool isUriPureNumber_ = false; 166 bool isFromReset_ = false; 167 [[deprecated("in NG")]] std::optional<Color> fillColor_; 168 const uint8_t* pixmapBuffer_ = nullptr; 169 NG::ImageDfxConfig imageDfxConfig_; 170 bool isHdr_ = false; 171 172 // image source type for example:FILE, ASSET, NETWORK, MEMORY, BASE64, INTERNAL, RESOURCE or DATA_ABILITY, 173 SrcType srcType_ = SrcType::UNSUPPORTED; 174 175 ColorMode localColorMode_ = ColorMode::COLOR_MODE_UNDEFINED; 176 }; 177 178 } // namespace OHOS::Ace 179 180 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_ACE_IMAGE_SOURCE_INFO_H 181