1 /* 2 * Copyright (c) 2021-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_IMAGE_IMAGE_PROVIDER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_PROVIDER_H 18 19 #include <string> 20 21 #include "include/codec/SkCodec.h" 22 #include "draw/color.h" 23 24 #include "base/memory/ace_type.h" 25 #include "base/resource/internal_resource.h" 26 #include "core/components/common/layout/constants.h" 27 #include "core/components_ng/render/canvas_image.h" 28 #include "core/components_ng/render/drawing_forward.h" 29 #include "core/image/image_loader.h" 30 #include "core/image/image_source_info.h" 31 #include "core/pipeline/pipeline_base.h" 32 33 class SkSVGDOM; 34 namespace OHOS::Ace { 35 36 class ImageObject; 37 38 class SvgDom; 39 using ImageObjSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<ImageObject>)>; 40 using UploadSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<NG::CanvasImage>&)>; 41 using SvgDomSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<SvgDom>&)>; 42 using FailedCallback = std::function<void(ImageSourceInfo, const std::string&)>; 43 using CancelableTask = CancelableCallback<void()>; 44 using OnPostBackgroundTask = std::function<void(CancelableTask)>; 45 46 struct LoadCallback { LoadCallbackLoadCallback47 LoadCallback( 48 const ImageObjSuccessCallback& success, const UploadSuccessCallback& upload, const FailedCallback& failed) 49 : successCallback(success), uploadCallback(upload), failedCallback(failed) 50 {} 51 ~LoadCallback() = default; 52 53 ImageObjSuccessCallback successCallback; 54 UploadSuccessCallback uploadCallback; 55 FailedCallback failedCallback; 56 }; 57 58 class ImageProvider { 59 public: 60 static void TryLoadImageInfo(const RefPtr<PipelineBase>& context, const std::string& src, 61 std::function<void(bool, int32_t, int32_t)>&& loadCallback); 62 63 static void GetSVGImageDOMAsyncFromSrc(const std::string& src, std::function<void(const sk_sp<SkSVGDOM>&)> callback, 64 std::function<void()> failedCallback, const WeakPtr<PipelineBase> context, uint64_t svgThemeColor = 0, 65 OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr); 66 67 static void GetSVGImageDOMAsyncFromData(const std::shared_ptr<RSData>& data, 68 std::function<void(const sk_sp<SkSVGDOM>&)> callback, std::function<void()> failedCallback, 69 const WeakPtr<PipelineBase> context, uint64_t svgThemeColor = 0, 70 OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr); 71 72 // upload image data to gpu context for painting asynchronously. 73 static void UploadImageToGPUForRender(const WeakPtr<PipelineBase> context, 74 const std::shared_ptr<RSImage>& image, 75 const std::shared_ptr<RSData>& data, 76 const std::function<void(std::shared_ptr<RSImage>, 77 std::shared_ptr<RSData>)>&& callback, 78 const std::string src); 79 80 // get out source image data asynchronously. 81 static void FetchImageObject(const ImageSourceInfo& imageInfo, const ImageObjSuccessCallback& successCallback, 82 const UploadSuccessCallback& uploadSuccessCallback, const FailedCallback& failedCallback, 83 const WeakPtr<PipelineBase>& context, bool syncMode, bool useSkiaSvg, bool needAutoResize, 84 const OnPostBackgroundTask& onBackgroundTaskPostCallback = nullptr); 85 86 static std::shared_ptr<RSImage> ResizeDrawingImage( 87 const std::shared_ptr<RSImage>& rawImage, const std::string& src, Size imageSize, bool forceResize = false); 88 89 static std::shared_ptr<RSImage> ApplySizeToDrawingImage( 90 const std::shared_ptr<RSImage>& rawRSImage, int32_t dstWidth, int32_t dstHeight, 91 const std::string& srcKey = std::string()); 92 93 static bool IsWideGamut(const std::shared_ptr<RSColorSpace>& rsColorSpace); 94 95 static bool NeedExchangeWidthAndHeight(SkEncodedOrigin origin); 96 97 // This is a synchronization interface for getting out source image. 98 static std::shared_ptr<RSImage> GetDrawingImage( 99 const std::string& src, const WeakPtr<PipelineBase> context, Size targetSize = Size()); 100 101 static RefPtr<ImageObject> GeneratorAceImageObject( 102 const ImageSourceInfo& imageInfo, const RefPtr<PipelineBase> context, bool useSkiaSvg); 103 104 static std::shared_ptr<RSData> LoadImageRawData( 105 const ImageSourceInfo& imageInfo, const RefPtr<PipelineBase> context); 106 107 static std::shared_ptr<RSData> LoadImageRawDataFromFileCache( 108 const RefPtr<PipelineBase> context, const std::string key, const std::string suffix = ""); 109 110 static RefPtr<ImageObject> QueryImageObjectFromCache( 111 const ImageSourceInfo& imageInfo, const RefPtr<PipelineBase>& pipelineContext); 112 static Rosen::Drawing::ColorType PixelFormatToDrawingColorType(const RefPtr<PixelMap>& pixmap); 113 static Rosen::Drawing::AlphaType AlphaTypeToDrawingAlphaType(const RefPtr<PixelMap>& pixmap); 114 static RSBitmapFormat MakeRSBitmapFormatFromPixelMap(const RefPtr<PixelMap>& pixmap); 115 static std::shared_ptr<RSColorSpace> ColorSpaceToDrawingColorSpace(const RefPtr<PixelMap>& pixmap); 116 static RSImageInfo MakeRSImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap); 117 118 static bool TrySetLoadingImage(const ImageSourceInfo& imageInfo, const ImageObjSuccessCallback& successCallback, 119 const UploadSuccessCallback& uploadCallback, const FailedCallback& failedCallback); 120 121 static void ProccessLoadingResult(const RefPtr<TaskExecutor>& taskExecutor, const ImageSourceInfo& imageInfo, 122 bool canStartUploadImageObj, const RefPtr<ImageObject>& imageObj, const RefPtr<PipelineBase>& context, 123 const std::string& errorMsg = ""); 124 125 static bool TryUploadingImage( 126 const std::string& key, const UploadSuccessCallback& successCallback, const FailedCallback& failedCallback); 127 128 static void ProccessUploadResult(const RefPtr<TaskExecutor>& taskExecutor, const ImageSourceInfo& imageInfo, 129 const Size& imageSize, const RefPtr<NG::CanvasImage>& canvasImage, const std::string& errorMsg = ""); 130 131 private: 132 static std::mutex loadingImageMutex_; 133 static std::unordered_map<std::string, std::vector<LoadCallback>> loadingImage_; 134 135 static std::mutex uploadMutex_; 136 static std::unordered_map<std::string, std::vector<LoadCallback>> uploadingImage_; 137 }; 138 139 } // namespace OHOS::Ace 140 141 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_PROVIDER_H 142