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_COMPONENTS_IMAGE_FLUTTER_RENDER_IMAGE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_FLUTTER_RENDER_IMAGE_H 18 19 #include "experimental/svg/model/SkSVGDOM.h" 20 #include "flutter/fml/memory/ref_counted.h" 21 #include "flutter/lib/ui/painting/image.h" 22 23 #include "core/components/image/render_image.h" 24 #include "core/image/image_provider.h" 25 #include "core/pipeline/base/scoped_canvas_state.h" 26 #include "core/pipeline/layers/offset_layer.h" 27 #include "frameworks/core/components/svg/parse/svg_dom.h" 28 29 namespace OHOS::Ace { 30 31 class FlutterRenderImage final : public RenderImage, public ImageProviderLoader { 32 DECLARE_ACE_TYPE(FlutterRenderImage, RenderImage); 33 34 public: 35 FlutterRenderImage(); 36 ~FlutterRenderImage() override; 37 38 void Update(const RefPtr<Component>& component) override; 39 void Paint(RenderContext& context, const Offset& offset) override; 40 void UpdateData(const std::string& uri, const std::vector<uint8_t>& memData) override; 41 bool IsRepaintBoundary() const override; 42 GetRenderLayer()43 RenderLayer GetRenderLayer() override 44 { 45 if (IsRepaintBoundary()) { 46 if (!layer_) { 47 layer_ = AceType::MakeRefPtr<Flutter::OffsetLayer>(); 48 } 49 } else { 50 layer_ = nullptr; 51 } 52 return AceType::RawPtr(layer_); 53 } 54 55 void OnHiddenChanged(bool hidden) override; 56 57 void UpLoadImageDataForPaint(); 58 59 void PaintBgImage(const flutter::Paint& paint, const Offset& offset, const ScopedCanvas& canvas) const; 60 61 void FetchImageObject() override; 62 SupportOpacity()63 bool SupportOpacity() override 64 { 65 return true; 66 } 67 68 bool IsSourceWideGamut() const override; 69 virtual bool RetryLoading() override; 70 static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap); 71 static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap); 72 static SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap); 73 static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap); 74 static void UploadImageObjToGpuForRender( 75 const RefPtr<ImageObject>& imageObj, 76 const WeakPtr<PipelineContext> context, 77 RefPtr<FlutterRenderTaskHolder>& renderTaskHolder, 78 UploadSuccessCallback uploadSuccessCallback, 79 FailedCallback failedCallback, 80 Size resizeTarget, 81 bool forceResize, 82 bool syncMode = false); 83 84 void ImageDataPaintSuccess(const fml::RefPtr<flutter::CanvasImage>& image); 85 void ImageObjReady(const RefPtr<ImageObject>& imageObj); 86 void ImageObjFailed(); 87 bool NeedUploadImageObjToGpu(); 88 SetFetchImageObjBackgroundTask(CancelableTask task)89 void SetFetchImageObjBackgroundTask(CancelableTask task) 90 { 91 if (fetchImageObjTask_) { 92 fetchImageObjTask_.Cancel(false); 93 } 94 fetchImageObjTask_ = task; 95 } 96 97 void OnAppHide() override; 98 void OnAppShow() override; 99 void OnVisibleChanged() override; 100 101 void PerformLayoutPixmap() override; 102 void PerformLayoutSvgImage() override; 103 104 Size MeasureForPixmap() override; 105 Size MeasureForSvgImage() override; 106 Size MeasureForNormalImage() override; 107 108 protected: 109 bool MaybeRelease() override; 110 void ClearRenderObject() override; 111 void LayoutImageObject() override; 112 113 private: 114 void InitializeCallbacks(); 115 Size Measure() override; 116 void UpdateRenderAltImage(const RefPtr<Component>& component); 117 void SetSkRadii(const Radius& radius, SkVector& radii); 118 void SetClipRadius(); 119 void CanvasDrawImageRect( 120 const flutter::Paint& paint, 121 const Offset& offset, 122 const ScopedCanvas& canvas, 123 const Rect& paintRect); 124 void DrawImageOnCanvas( 125 const Rect& srcRect, 126 const Rect& dstRect, 127 const ScopedCanvas& canvas, 128 const flutter::Paint& paint, 129 const Offset& dstOffset = Offset()) const; 130 void PaintSVGImage(const sk_sp<SkData>& skData, bool onlyLayoutSelf = false); 131 void DrawSVGImage(const Offset& offset, ScopedCanvas& canvas); 132 void DrawSVGImageCustom(RenderContext& context, const Offset& offset); 133 void UpdateLoadSuccessState(); 134 Rect RecalculateSrcRect(const Size& realImageSize); 135 void ApplyColorFilter(flutter::Paint& paint); 136 void ApplyInterpolation(flutter::Paint& paint); 137 void ApplyBorderRadius(const Offset& offset, const ScopedCanvas& canvas, const Rect& paintRect); 138 void AddSvgChild(); 139 void CreateAnimatedPlayer(const RefPtr<ImageProvider>& provider, SkCodec* codecPtr, bool forceResize); 140 bool VerifySkImageDataFromPixmap(const RefPtr<PixelMap>& pixmap) const; 141 void PerformLayoutSvgCustom(); 142 void CancelBackgroundTasks(); 143 void CacheImageObject(); 144 void UpdatePixmap(const RefPtr<PixelMap>& pixmap); 145 void UpdateSharedMemoryImage(const RefPtr<PipelineContext>& context); 146 void ProcessPixmapForPaint(); 147 148 sk_sp<SkSVGDOM> skiaDom_; 149 RefPtr<SvgDom> svgDom_; 150 fml::RefPtr<flutter::CanvasImage> image_; 151 bool loadSvgAfterLayout_ = false; 152 bool loadSvgOnPaint_ = false; // only load svg trees without box and bind 153 RefPtr<Flutter::OffsetLayer> layer_; 154 SkVector radii_[4] = { { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0 } }; 155 Size formerRawImageSize_; 156 bool imageDataNotReady_ = false; 157 158 ImageSourceInfo curSourceInfo_; 159 ImageObjSuccessCallback imageObjSuccessCallback_; 160 UploadSuccessCallback uploadSuccessCallback_; 161 FailedCallback failedCallback_; 162 OnPostBackgroundTask onPostBackgroundTask_; 163 RefPtr<ImageObject> imageObj_; 164 RefPtr<FlutterRenderTaskHolder> renderTaskHolder_; 165 166 CancelableTask fetchImageObjTask_; 167 bool backgroundTaskCancled_ = false; 168 }; 169 170 } // namespace OHOS::Ace 171 172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_IMAGE_FLUTTER_RENDER_IMAGE_H 173