• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/render/canvas_image.h"
27 #include "core/image/image_source_info.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 namespace {
32     constexpr uint64_t MAX_WAITING_TIME = 1000; // 1000ms
33 }
34 
35 class ImageObject : public virtual AceType {
36     DECLARE_ACE_TYPE(ImageObject, AceType);
37 
38 public:
39     ImageObject() = delete;
ImageObject(const ImageSourceInfo & sourceInfo,const SizeF & imageSize,const RefPtr<ImageData> & data)40     ImageObject(const ImageSourceInfo& sourceInfo, const SizeF& imageSize, const RefPtr<ImageData>& data)
41         : src_(sourceInfo), imageSize_(imageSize), data_(data)
42     {}
43     ~ImageObject() override = default;
44 
45     const SizeF& GetImageSize() const;
46     const ImageSourceInfo& GetSourceInfo() const;
47     RefPtr<ImageData> GetData() const;
48     int32_t GetFrameCount() const;
49     ImageRotateOrientation GetOrientation() const;
50     ImageRotateOrientation GetUserOrientation() const;
51 
52     void SetData(const RefPtr<ImageData>& data);
53     void SetImageSize(const SizeF& imageSize);
54     virtual void ClearData();
55     void SetFrameCount(int32_t frameCount);
56     void SetOrientation(ImageRotateOrientation orientation);
57     void SetUserOrientation(ImageRotateOrientation orientation);
58 
GetSVGDom()59     virtual RefPtr<SvgDomBase> GetSVGDom() const
60     {
61         return nullptr;
62     }
63 
64     virtual RefPtr<ImageObject> Clone() = 0;
GetDumpInfo()65     virtual std::string GetDumpInfo() { return ""; }
IsSupportCache()66     bool IsSupportCache() const
67     {
68         return src_.SupportObjCache();
69     }
70 
71     virtual void MakeCanvasImage(const RefPtr<ImageLoadingContext>& ctx, const SizeF& resizeTarget, bool forceResize,
72         bool syncLoad, bool loadInVipChannel = false) = 0;
73 
74     std::unique_lock<std::timed_mutex> GetPrepareImageDataLock(
75         const std::chrono::milliseconds& timeoutMs = std::chrono::milliseconds(MAX_WAITING_TIME))
76     {
77         return std::unique_lock<std::timed_mutex>(prepareImageDataMutex_, timeoutMs);
78     }
79 
80 protected:
81     const ImageSourceInfo src_;
82     ImageRotateOrientation orientation_ = ImageRotateOrientation::UP;
83     ImageRotateOrientation userOrientation_ = ImageRotateOrientation::UP;
84     SizeF imageSize_ { -1.0, -1.0 };
85     // Mutex to protect concurrent access to data_
86     mutable std::shared_mutex dataMutex_;
87     // no longer needed after making canvas image
88     RefPtr<ImageData> data_;
89     int32_t frameCount_ = 1;
90     // Mutex for controlling access to prepareImageData operations.
91     // This is a timed mutex to prevent long blocking, allowing a maximum wait time of 1000ms for acquiring the lock.
92     std::timed_mutex prepareImageDataMutex_;
93 
94     ACE_DISALLOW_COPY_AND_MOVE(ImageObject);
95 };
96 } // namespace OHOS::Ace::NG
97 
98 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_OBJECT_NG_H
99