1 /* 2 * Copyright (c) 2022 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_RENDER_ADAPTER_SKIA_CANVAS_IMAGE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_SKIA_CANVAS_IMAGE_H 18 19 #ifdef NG_BUILD 20 #include "flutter/flow/skia_gpu_object.h" 21 #include "third_party/skia/include/core/SkImage.h" 22 #else 23 #include "flutter/lib/ui/painting/image.h" 24 #endif 25 26 #include "core/components_ng/render/canvas_image.h" 27 28 namespace OHOS::Ace::NG { 29 30 class SkiaCanvasImage : public CanvasImage { DECLARE_ACE_TYPE(SkiaCanvasImage,CanvasImage)31 DECLARE_ACE_TYPE(SkiaCanvasImage, CanvasImage) 32 public: 33 #ifndef NG_BUILD 34 explicit SkiaCanvasImage(const fml::RefPtr<flutter::CanvasImage>& image) : image_(image) {} 35 #else 36 SkiaCanvasImage() = default; 37 #endif 38 39 ~SkiaCanvasImage() override = default; 40 GetCanvasImage()41 virtual sk_sp<SkImage> GetCanvasImage() const 42 { 43 #ifndef NG_BUILD 44 if (image_) { 45 return image_->image(); 46 } 47 #endif 48 return nullptr; 49 } 50 GetCompressData()51 virtual sk_sp<SkData> GetCompressData() const 52 { 53 #ifndef NG_BUILD 54 if (image_) { 55 return image_->compressData(); 56 } 57 #endif 58 return nullptr; 59 } 60 SetCompressData(sk_sp<SkData> data,int32_t w,int32_t h)61 void SetCompressData(sk_sp<SkData> data, int32_t w, int32_t h) 62 { 63 #ifndef NG_BUILD 64 if (image_) { 65 image_->setCompress(data, w, h); 66 auto skimage = image_->image(); 67 uniqueId_ = skimage ? skimage->uniqueID() : 0; 68 } 69 #endif 70 } 71 GetCompressWidth()72 virtual int32_t GetCompressWidth() const 73 { 74 #ifndef NG_BUILD 75 if (image_) { 76 return image_->compressWidth(); 77 } 78 #endif 79 return 0; 80 } 81 GetCompressHeight()82 virtual int32_t GetCompressHeight() const 83 { 84 #ifndef NG_BUILD 85 if (image_) { 86 return image_->compressHeight(); 87 } 88 #endif 89 return 0; 90 } 91 GetUniqueID()92 virtual uint32_t GetUniqueID() const 93 { 94 #ifndef NG_BUILD 95 if (image_ && image_->image()) { 96 return image_->image()->uniqueID(); 97 } 98 return uniqueId_; 99 #endif 100 return 0; 101 } 102 SetUniqueID(uint32_t id)103 virtual void SetUniqueID(uint32_t id) 104 { 105 #ifndef NG_BUILD 106 uniqueId_ = id; 107 #endif 108 } 109 110 #ifndef NG_BUILD GetFlutterCanvasImage()111 virtual fml::RefPtr<flutter::CanvasImage> GetFlutterCanvasImage() const 112 { 113 if (image_) { 114 return image_; 115 } 116 return nullptr; 117 } 118 #endif 119 120 RefPtr<CanvasImage> Clone() override; 121 122 RefPtr<PixelMap> GetPixelMap() override; 123 124 void ReplaceSkImage(flutter::SkiaGPUObject<SkImage> newSkGpuObjSkImage); 125 int32_t GetWidth() const override; 126 int32_t GetHeight() const override; 127 128 void AddFilter(SkPaint& paint); 129 void DrawToRSCanvas( 130 RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) override; 131 132 static SkImageInfo MakeSkImageInfoFromPixelMap(const RefPtr<PixelMap>& pixmap); 133 static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap); 134 static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap); 135 static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap); 136 137 private: 138 void ClipRRect(RSCanvas& canvas, const RSRect& dstRect, const BorderRadiusArray& radiusXY); 139 bool DrawWithRecordingCanvas( 140 RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY); 141 // TODO: should not deps on flutter. 142 #ifndef NG_BUILD 143 uint32_t uniqueId_; 144 fml::RefPtr<flutter::CanvasImage> image_; 145 #endif 146 }; 147 } // namespace OHOS::Ace::NG 148 149 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_SKIA_CANVAS_IMAGE_H 150