1 /* 2 * Copyright (c) 2021 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 "flutter/fml/memory/ref_counted.h" 22 #include "flutter/lib/ui/painting/image.h" 23 #include "third_party/skia/include/codec/SkCodec.h" 24 25 #include "base/memory/ace_type.h" 26 #include "base/resource/internal_resource.h" 27 #include "core/components/common/layout/constants.h" 28 #include "core/image/image_source_info.h" 29 #include "core/image/image_loader.h" 30 #include "core/pipeline/pipeline_context.h" 31 32 class SkSVGDOM; 33 namespace OHOS::Ace { 34 35 class ImageObject; 36 struct FlutterRenderTaskHolder : public virtual AceType { 37 DECLARE_ACE_TYPE(FlutterRenderTaskHolder, AceType); 38 public: FlutterRenderTaskHolderFlutterRenderTaskHolder39 FlutterRenderTaskHolder( 40 fml::RefPtr<flutter::SkiaUnrefQueue> queue, 41 fml::WeakPtr<flutter::IOManager> manager, 42 fml::RefPtr<fml::TaskRunner> taskRunner) 43 : unrefQueue(queue), 44 ioManager(manager), 45 ioTaskRunner(taskRunner) 46 {} 47 48 fml::RefPtr<flutter::SkiaUnrefQueue> unrefQueue; 49 // weak reference of io manager must be check and used on io thread, because io manager is created on io thread. 50 fml::WeakPtr<flutter::IOManager> ioManager; 51 fml::RefPtr<fml::TaskRunner> ioTaskRunner; 52 }; 53 54 class SvgDom; 55 using ImageObjSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<ImageObject>)>; 56 using UploadSuccessCallback = std::function<void(ImageSourceInfo, const fml::RefPtr<flutter::CanvasImage>&)>; 57 using SvgDomSuccessCallback = std::function<void(ImageSourceInfo, const RefPtr<SvgDom>&)>; 58 using FailedCallback = std::function<void(ImageSourceInfo)>; 59 using CancelableTask = CancelableCallback<void()>; 60 using OnPostBackgroundTask = std::function<void(CancelableTask)>; 61 62 class FlutterRenderImage; 63 class ImageProvider { 64 public: 65 static void TryLoadImageInfo(const RefPtr<PipelineContext>& context, const std::string& src, 66 std::function<void(bool, int32_t, int32_t)>&& loadCallback); 67 68 static void GetSVGImageDOMAsyncFromSrc( 69 const std::string& src, 70 std::function<void(const sk_sp<SkSVGDOM>&)> callback, 71 std::function<void()> failedCallback, 72 const WeakPtr<PipelineContext> context, 73 uint64_t svgThemeColor = 0, 74 OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr); 75 76 static void GetSVGImageDOMAsyncFromData( 77 const sk_sp<SkData>& skData, 78 std::function<void(const sk_sp<SkSVGDOM>&)> callback, 79 std::function<void()> failedCallback, 80 const WeakPtr<PipelineContext> context, 81 uint64_t svgThemeColor = 0, 82 OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr); 83 84 // upload image data to gpu context for painting asynchronously. 85 static void UploadImageToGPUForRender( 86 const sk_sp<SkImage>& image, 87 const std::function<void(flutter::SkiaGPUObject<SkImage>)>&& callback, 88 const RefPtr<FlutterRenderTaskHolder>& renderTaskHolder); 89 90 // get out source image data asynchronously. 91 static void FetchImageObject( 92 ImageSourceInfo imageInfo, 93 ImageObjSuccessCallback successCallback, 94 UploadSuccessCallback uploadSuccessCallback, 95 FailedCallback failedCallback, 96 const WeakPtr<PipelineContext> context, 97 bool syncMode, 98 bool useSkiaSvg, 99 bool needAutoResize, 100 RefPtr<FlutterRenderTaskHolder>& renderTaskHolder, 101 OnPostBackgroundTask onBackgroundTaskPostCallback = nullptr); 102 103 static sk_sp<SkImage> ResizeSkImage( 104 const sk_sp<SkImage>& rawImage, 105 const std::string& src, 106 Size imageSize, 107 bool forceResize = false); 108 109 static sk_sp<SkImage> ApplySizeToSkImage( 110 const sk_sp<SkImage>& rawImage, 111 int32_t dstWidth, 112 int32_t dstHeight, 113 const std::string& srcKey = std::string()); 114 115 static bool IsWideGamut(const sk_sp<SkColorSpace>& colorSpace); 116 117 static bool NeedExchangeWidthAndHeight(SkEncodedOrigin origin); 118 119 // This is a synchronization interface for getting out source image. 120 static sk_sp<SkImage> GetSkImage( 121 const std::string& src, 122 const WeakPtr<PipelineContext> context, 123 Size targetSize = Size()); 124 125 static RefPtr<ImageObject> GeneraterAceImageObject( 126 const ImageSourceInfo& imageInfo, 127 const RefPtr<PipelineContext> context, 128 bool useSkiaSvg); 129 130 static sk_sp<SkData> LoadImageRawData( 131 const ImageSourceInfo& imageInfo, 132 const RefPtr<PipelineContext> context, 133 const Size& targetSize = Size()); 134 135 static RefPtr<ImageObject> QueryImageObjectFromCache( 136 const ImageSourceInfo& imageInfo, const RefPtr<PipelineContext>& pipelineContext); 137 static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap); 138 static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap); 139 static SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap); 140 static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap); 141 }; 142 143 } // namespace OHOS::Ace 144 145 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_IMAGE_IMAGE_PROVIDER_H 146