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