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/geometry/ng/rect_t.h" 24 #include "base/thread/cancelable_callback.h" 25 #include "base/utils/noncopyable.h" 26 #include "core/components_ng/image_provider/image_data.h" 27 #include "core/components_ng/image_provider/image_state_manager.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)>; 36 37 struct LoadNotifier { LoadNotifierLoadNotifier38 LoadNotifier(DataReadyNotifyTask&& dataReadyNotifyTask, LoadSuccessNotifyTask&& loadSuccessNotifyTask, 39 LoadFailNotifyTask&& loadFailNotifyTask) 40 : dataReadyNotifyTask_(std::move(dataReadyNotifyTask)), 41 loadSuccessNotifyTask_(std::move(loadSuccessNotifyTask)), loadFailNotifyTask_(std::move(loadFailNotifyTask)) 42 {} 43 44 DataReadyNotifyTask dataReadyNotifyTask_; 45 LoadSuccessNotifyTask loadSuccessNotifyTask_; 46 LoadFailNotifyTask loadFailNotifyTask_; 47 }; 48 49 class ImageObject; 50 51 struct RenderTaskHolder : public virtual AceType { 52 DECLARE_ACE_TYPE(RenderTaskHolder, AceType); 53 54 public: 55 RenderTaskHolder() = default; 56 ~RenderTaskHolder() override = default; 57 58 ACE_DISALLOW_COPY_AND_MOVE(RenderTaskHolder); 59 }; 60 61 // load & paint images on background threads 62 // cache loaded/painted image data in memory 63 class ImageProvider : public virtual AceType { 64 DECLARE_ACE_TYPE(ImageProvider, AceType); 65 66 public: 67 static void CreateImageObject(const ImageSourceInfo& src, const WeakPtr<ImageLoadingContext>& ctxWp, bool sync); 68 69 static void MakeCanvasImage(const WeakPtr<ImageObject>& objWp, const WeakPtr<ImageLoadingContext>& ctxWp, 70 const SizeF& targetSize, bool forceResize = false, bool sync = false); 71 72 // Query [CanvasImage] from cache, if hit, notify load success immediately and returns true 73 static RefPtr<CanvasImage> QueryCanvasImageFromCache(const ImageSourceInfo& src, const SizeF& targetSize); 74 75 // Query imageObj from cache, if hit, notify dataReady and returns true 76 static RefPtr<ImageObject> QueryImageObjectFromCache(const ImageSourceInfo& src); 77 // generate cache key for canvasImage, combining src and image size 78 static std::string GenerateImageKey(const ImageSourceInfo& src, const NG::SizeF& targetSize); 79 80 // cancel a scheduled background task 81 static void CancelTask(const std::string& key, const WeakPtr<ImageLoadingContext>& ctx); 82 83 private: 84 // create RenderTaskHolder for skiaGPUObject 85 static RefPtr<RenderTaskHolder> CreateRenderTaskHolder(); 86 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 /** Check if data is present in imageObj, if not, load image data. 101 * 102 * @param imageObj contains image source and image data 103 * @return true if image data is prepared 104 */ 105 static bool PrepareImageData(const RefPtr<ImageObject>& imageObj); 106 107 static RefPtr<ImageObject> BuildImageObject(const ImageSourceInfo& src, const RefPtr<ImageData>& data); 108 109 static void CacheCanvasImage(const RefPtr<CanvasImage>& canvasImage, const std::string& key); 110 111 // helper function to create image object from ImageSourceInfo 112 static void CreateImageObjHelper(const ImageSourceInfo& src, bool sync = false); 113 114 /** Helper function to create canvasImage and upload it to GPU for rendering. 115 * 116 * @param imageObjWp weakPtr of imageObj, contains image data 117 * @param renderTaskHolder passed in to create SkiaGPUObject 118 */ 119 static void MakeCanvasImageHelper(const WeakPtr<ImageObject>& imageObjWp, const SizeF& targetSize, 120 const RefPtr<RenderTaskHolder>& renderTaskHolder, bool forceResize, bool sync = false); 121 122 static void UploadImageToGPUForRender(const RefPtr<CanvasImage>& canvasImage, 123 std::function<void(RefPtr<CanvasImage>)>&& callback, const RefPtr<RenderTaskHolder>& renderTaskHolder, 124 const std::string& key, const SizeF& resizeTarget, const RefPtr<ImageData>& data, bool syncLoad); 125 126 // helper functions to end task and callback to LoadingContexts 127 static void SuccessCallback(const RefPtr<CanvasImage>& canvasImage, const std::string& key, bool sync = 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