1 /* 2 * Copyright (c) 2022-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_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_OBJECT_NG_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_OBJECT_NG_H 18 19 #include <shared_mutex> 20 21 #include "base/utils/noncopyable.h" 22 #include "core/components/common/layout/constants.h" 23 #include "core/components_ng/image_provider/image_data.h" 24 #include "core/components_ng/image_provider/image_provider.h" 25 #include "core/components_ng/image_provider/svg_dom_base.h" 26 #include "core/components_ng/pattern/image/image_dfx.h" 27 #include "core/components_ng/render/canvas_image.h" 28 #include "core/image/image_source_info.h" 29 30 namespace OHOS::Ace::NG { 31 32 namespace { 33 constexpr uint64_t MAX_WAITING_TIME = 1000; // 1000ms 34 } 35 36 class ImageObject : public virtual AceType { 37 DECLARE_ACE_TYPE(ImageObject, AceType); 38 39 public: 40 ImageObject() = delete; ImageObject(const ImageSourceInfo & sourceInfo,const SizeF & imageSize,const RefPtr<ImageData> & data)41 ImageObject(const ImageSourceInfo& sourceInfo, const SizeF& imageSize, const RefPtr<ImageData>& data) 42 : src_(sourceInfo), imageSize_(imageSize), data_(data) 43 { 44 imageDataSize_ = data ? data_->GetSize() : 0; 45 } 46 ~ImageObject() override = default; 47 48 const SizeF& GetImageSize() const; 49 const ImageSourceInfo& GetSourceInfo() const; 50 void SetImageSourceInfoHdr(bool isHdr); 51 RefPtr<ImageData> GetData() const; 52 int32_t GetFrameCount() const; 53 ImageRotateOrientation GetOrientation() const; 54 ImageRotateOrientation GetUserOrientation() const; 55 56 void SetData(const RefPtr<ImageData>& data); 57 void SetImageSize(const SizeF& imageSize); 58 virtual void ClearData(); 59 void SetFrameCount(int32_t frameCount); 60 void SetOrientation(ImageRotateOrientation orientation); 61 void SetUserOrientation(ImageRotateOrientation orientation); 62 void SetImageFileSize(size_t fileSize); 63 size_t GetImageFileSize() const; 64 size_t GetImageDataSize() const; 65 GetSVGDom()66 virtual RefPtr<SvgDomBase> GetSVGDom() const 67 { 68 return nullptr; 69 } 70 71 virtual RefPtr<ImageObject> Clone() = 0; GetDumpInfo()72 virtual std::string GetDumpInfo() { return ""; } IsSupportCache()73 bool IsSupportCache() const 74 { 75 return src_.SupportObjCache(); 76 } 77 SetImageDfxConfig(const ImageDfxConfig & imageDfxConfig)78 void SetImageDfxConfig(const ImageDfxConfig& imageDfxConfig) 79 { 80 imageDfxConfig_ = imageDfxConfig; 81 } 82 GetImageDfxConfig()83 ImageDfxConfig GetImageDfxConfig() 84 { 85 return imageDfxConfig_; 86 } 87 88 virtual void MakeCanvasImage( 89 const WeakPtr<ImageLoadingContext>& ctxWp, const SizeF& resizeTarget, bool forceResize, bool syncLoad) = 0; 90 91 std::unique_lock<std::timed_mutex> GetPrepareImageDataLock( 92 const std::chrono::milliseconds& timeoutMs = std::chrono::milliseconds(MAX_WAITING_TIME)) 93 { 94 return std::unique_lock<std::timed_mutex>(prepareImageDataMutex_, timeoutMs); 95 } 96 97 protected: 98 ImageSourceInfo src_; 99 ImageRotateOrientation orientation_ = ImageRotateOrientation::UP; 100 ImageRotateOrientation userOrientation_ = ImageRotateOrientation::UP; 101 SizeF imageSize_ { -1.0, -1.0 }; 102 // Mutex to protect concurrent access to data_ 103 mutable std::shared_mutex dataMutex_; 104 // no longer needed after making canvas image 105 RefPtr<ImageData> data_; 106 size_t fileSize_ = 0; // size of file in bytes 107 size_t imageDataSize_ = 0; // size of image data in bytes 108 int32_t frameCount_ = 1; 109 ImageDfxConfig imageDfxConfig_; 110 // Mutex for controlling access to prepareImageData operations. 111 // This is a timed mutex to prevent long blocking, allowing a maximum wait time of 1000ms for acquiring the lock. 112 std::timed_mutex prepareImageDataMutex_; 113 114 ACE_DISALLOW_COPY_AND_MOVE(ImageObject); 115 }; 116 } // namespace OHOS::Ace::NG 117 118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_OBJECT_NG_H 119