• 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_PROVIDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_PROVIDER_H
18 
19 #include <functional>
20 #include <set>
21 #include <unordered_map>
22 
23 #include "base/thread/cancelable_callback.h"
24 #include "core/components_ng/image_provider/image_data.h"
25 #include "core/components_ng/image_provider/image_state_manager.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 using DataReadyNotifyTask = std::function<void(const ImageSourceInfo& src)>;
32 using LoadSuccessNotifyTask = std::function<void(const ImageSourceInfo& src)>;
33 using LoadFailNotifyTask = std::function<void(const ImageSourceInfo& src)>;
34 
35 struct LoadNotifier {
LoadNotifierLoadNotifier36     LoadNotifier(DataReadyNotifyTask&& dataReadyNotifyTask, LoadSuccessNotifyTask&& loadSuccessNotifyTask,
37         LoadFailNotifyTask&& loadFailNotifyTask)
38         : onDataReady_(std::move(dataReadyNotifyTask)), onLoadSuccess_(std::move(loadSuccessNotifyTask)),
39           onLoadFail_(std::move(loadFailNotifyTask))
40     {}
41 
42     DataReadyNotifyTask onDataReady_;
43     LoadSuccessNotifyTask onLoadSuccess_;
44     LoadFailNotifyTask onLoadFail_;
45 };
46 
47 class ImageObject;
48 
49 // load & decode images
50 class ImageProvider : public virtual AceType {
51     DECLARE_ACE_TYPE(ImageProvider, AceType);
52 
53 public:
54     /** Fetch image data and create ImageObject from ImageSourceInfo.
55      *
56      *    @param src                  image source info
57      *    @param ctxWp                ImageLoadingContext that initiates the task, to be stored in the map
58      *    @param sync                 if true, run task synchronously
59      */
60     static void CreateImageObject(const ImageSourceInfo& src, const WeakPtr<ImageLoadingContext>& ctxWp, bool sync);
61 
62     /** Decode image data and make CanvasImage from ImageObject.
63      *
64      *    @param obj                  imageObject, contains image data
65      *    @param targetSize           target size of canvasImage
66      *    @param forceResize          force resize image to target size
67      *    @param sync                 if true, run task synchronously
68      *    @return                     true if MakeCanvasImage was successful
69      */
70     static void MakeCanvasImage(const RefPtr<ImageObject>& obj, const WeakPtr<ImageLoadingContext>& ctxWp,
71         const SizeF& targetSize, bool forceResize = false, bool sync = false);
72 
73     /** Check if data is present in imageObj, if not, reload image data.
74      *
75      *    @param imageObj         contains image source and image data
76      *    @return                 true if image data is prepared
77      */
78     static bool PrepareImageData(const RefPtr<ImageObject>& imageObj);
79 
80     // Query imageObj from cache, if hit, notify dataReady and returns true
81     static RefPtr<ImageObject> QueryImageObjectFromCache(const ImageSourceInfo& src);
82 
83     // cancel a scheduled background task
84     static void CancelTask(const std::string& key, const WeakPtr<ImageLoadingContext>& ctx);
85 
86 private:
87     /** Check if task is already running and register task in the task map,
88      * making sure the same task runs only once (CreateImageObject with same
89      * [src], MakeCanvasImage with the same [imageObj] and [size]).
90      *
91      *    @param key              task key, based on [src] +? [size]
92      *    @param ctx              ImageLoadingContext that initiates the task, to be stored in the amp
93      *    @return                 true if task is new, false if task is already running
94      */
95     static bool RegisterTask(const std::string& key, const WeakPtr<ImageLoadingContext>& ctx);
96 
97     // mark a task as finished, erase from map and retrieve corresponding ctxs
98     static std::set<WeakPtr<ImageLoadingContext>> EndTask(const std::string& key);
99 
100     static RefPtr<ImageObject> BuildImageObject(const ImageSourceInfo& src, const RefPtr<ImageData>& data);
101 
102     static RefPtr<ImageObject> QueryThumbnailCache(const ImageSourceInfo& src);
103 
104     // helper function to create image object from ImageSourceInfo
105     static void CreateImageObjHelper(const ImageSourceInfo& src, bool sync = false);
106 
107     static void MakeCanvasImageHelper(const RefPtr<ImageObject>& obj, const SizeF& targetSize, const std::string& key,
108         bool forceResize, bool sync = false);
109 
110     // helper functions to end task and callback to LoadingContexts
111     static void SuccessCallback(const RefPtr<CanvasImage>& canvasImage, const std::string& key, bool sync = false);
112     static void FailCallback(const std::string& key, const std::string& errorMsg, bool sync = false);
113 
114     struct Task {
115         CancelableCallback<void()> bgTask_;
116         std::set<WeakPtr<ImageLoadingContext>> ctxs_;
117     };
118 
119     static std::mutex taskMtx_;
120     static std::unordered_map<std::string, Task> tasks_;
121 };
122 
123 } // namespace OHOS::Ace::NG
124 
125 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_IMAGE_PROVIDER_IMAGE_PROVIDER_H
126