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_LAYERS_CLIP_LAYER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_LAYERS_CLIP_LAYER_H 18 19 #include "base/geometry/rrect.h" 20 #include "core/pipeline/layers/layer.h" 21 #include "core/pipeline/layers/offset_layer.h" 22 #include "core/pipeline/layers/scene_builder.h" 23 #include "experimental/svg/model/SkSVGDOM.h" 24 #include "third_party/skia/include/core/SkPaint.h" 25 #include "third_party/skia/include/core/SkData.h" 26 27 namespace OHOS::Ace::Flutter { 28 29 enum class Clip { 30 NONE = 0, 31 HARD_EDGE, 32 ANTI_ALIAS, 33 ANTI_ALIAS_WITH_SAVE_LAYER, 34 }; 35 36 class ClipLayer : public OffsetLayer { DECLARE_ACE_TYPE(ClipLayer,OffsetLayer)37 DECLARE_ACE_TYPE(ClipLayer, OffsetLayer) 38 public: 39 ClipLayer(double left, double right, double top, double bottom, Clip clipBehavior) 40 : OffsetLayer(0, 0), clipBehavior_(clipBehavior) 41 { 42 rrect_.sk_rrect = SkRRect::MakeRect(SkRect::MakeLTRB(static_cast<SkScalar>(left), static_cast<SkScalar>(top), 43 static_cast<SkScalar>(right), static_cast<SkScalar>(bottom))); 44 rrect_.is_null = false; 45 } 46 ~ClipLayer() override = default; 47 48 void AddToScene(SceneBuilder& builder, double x, double y) override; 49 50 void SetClip(double left, double right, double top, double bottom, Clip clipBehavior); 51 void SetClipRRect(const RRect& rrect, Clip clipBehavior); 52 void SetClipPath(const fml::RefPtr<flutter::CanvasPath>& canvasPath, Clip clipBehavior); 53 void SetClipPath(const fml::RefPtr<flutter::CanvasPath>& canvasPath); 54 void SetClipRRect(const Rect& rect, double x, double y, Clip clipBehavior); 55 void SetClipRRect(const flutter::RRect& rrect); 56 SkVector GetSkRadii(const Radius& radius); 57 void Dump() override; 58 59 void CancelMask(); 60 void SetSvgMask(const sk_sp<SkSVGDOM>& svgDom, double x, double y, double scaleX, double scaleY); 61 void SetColorMask(const SkPaint& maskPaint); 62 void SetPathMask(const SkPaint& maskPaint, const SkPath& path); 63 64 private: 65 bool isPath_ = false; 66 fml::RefPtr<flutter::CanvasPath> canvasPath_; 67 flutter::RRect rrect_; 68 Clip clipBehavior_ { Clip::NONE }; 69 70 bool isMask_ = false; 71 bool isSvgMask_ = false; 72 bool isGradientMask_ = false; 73 bool isPathMask_ = false; 74 double svgX_ = 0.0f; 75 double svgY_ = 0.0f; 76 double scaleX_ = 1.0f; 77 double scaleY_ = 1.0f; 78 sk_sp<SkSVGDOM> svgDom_; 79 SkPaint maskPaint_; 80 SkPath maskPath_; 81 }; 82 83 } // namespace OHOS::Ace::Flutter 84 85 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_LAYERS_CLIP_LAYER_H 86