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