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