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 ~ImageObject() override = default; 45 46 const SizeF& GetImageSize() const; 47 const ImageSourceInfo& GetSourceInfo() const; 48 RefPtr<ImageData> GetData() const; 49 int32_t GetFrameCount() const; 50 ImageRotateOrientation GetOrientation() const; 51 ImageRotateOrientation GetUserOrientation() const; 52 53 void SetData(const RefPtr<ImageData>& data); 54 void SetImageSize(const SizeF& imageSize); 55 virtual void ClearData(); 56 void SetFrameCount(int32_t frameCount); 57 void SetOrientation(ImageRotateOrientation orientation); 58 void SetUserOrientation(ImageRotateOrientation orientation); 59 GetSVGDom()60 virtual RefPtr<SvgDomBase> GetSVGDom() const 61 { 62 return nullptr; 63 } 64 65 virtual RefPtr<ImageObject> Clone() = 0; GetDumpInfo()66 virtual std::string GetDumpInfo() { return ""; } IsSupportCache()67 bool IsSupportCache() const 68 { 69 return src_.SupportObjCache(); 70 } 71 SetImageDfxConfig(const ImageDfxConfig & imageDfxConfig)72 void SetImageDfxConfig(const ImageDfxConfig& imageDfxConfig) 73 { 74 imageDfxConfig_ = imageDfxConfig; 75 } 76 GetImageDfxConfig()77 ImageDfxConfig GetImageDfxConfig() 78 { 79 return imageDfxConfig_; 80 } 81 82 virtual void MakeCanvasImage( 83 const WeakPtr<ImageLoadingContext>& ctxWp, const SizeF& resizeTarget, bool forceResize, bool syncLoad) = 0; 84 85 std::unique_lock<std::timed_mutex> GetPrepareImageDataLock( 86 const std::chrono::milliseconds& timeoutMs = std::chrono::milliseconds(MAX_WAITING_TIME)) 87 { 88 return std::unique_lock<std::timed_mutex>(prepareImageDataMutex_, timeoutMs); 89 } 90 91 protected: 92 const ImageSourceInfo src_; 93 ImageRotateOrientation orientation_ = ImageRotateOrientation::UP; 94 ImageRotateOrientation userOrientation_ = ImageRotateOrientation::UP; 95 SizeF imageSize_ { -1.0, -1.0 }; 96 // Mutex to protect concurrent access to data_ 97 mutable std::shared_mutex dataMutex_; 98 // no longer needed after making canvas image 99 RefPtr<ImageData> data_; 100 int32_t frameCount_ = 1; 101 ImageDfxConfig imageDfxConfig_; 102 // Mutex for controlling access to prepareImageData operations. 103 // This is a timed mutex to prevent long blocking, allowing a maximum wait time of 1000ms for acquiring the lock. 104 std::timed_mutex prepareImageDataMutex_; 105 106 ACE_DISALLOW_COPY_AND_MOVE(ImageObject); 107 }; 108 } // namespace OHOS::Ace::NG 109 110 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_OBJECT_NG_H 111