1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef FLUTTER_LIB_UI_PAINTING_CANVAS_H_ 6 #define FLUTTER_LIB_UI_PAINTING_CANVAS_H_ 7 8 #include "flutter/lib/ui/dart_wrapper.h" 9 #include "flutter/lib/ui/painting/paint.h" 10 #include "flutter/lib/ui/painting/path.h" 11 #include "flutter/lib/ui/painting/picture.h" 12 #include "flutter/lib/ui/painting/picture_recorder.h" 13 #include "flutter/lib/ui/painting/rrect.h" 14 #include "flutter/lib/ui/painting/vertices.h" 15 #include "third_party/skia/include/core/SkCanvas.h" 16 #include "third_party/skia/include/utils/SkShadowUtils.h" 17 18 namespace tonic { 19 class DartLibraryNatives; 20 } // namespace tonic 21 22 namespace flutter { 23 class CanvasImage; 24 25 class Canvas : public RefCountedDartWrappable<Canvas> { 26 DEFINE_WRAPPERTYPEINFO(); 27 FML_FRIEND_MAKE_REF_COUNTED(Canvas); 28 29 public: 30 static fml::RefPtr<Canvas> Create(PictureRecorder* recorder, 31 double left, 32 double top, 33 double right, 34 double bottom); 35 36 ~Canvas() override; 37 38 void save(); 39 void saveLayerWithoutBounds(const Paint& paint, const PaintData& paint_data); 40 void saveLayer(double left, 41 double top, 42 double right, 43 double bottom, 44 const Paint& paint, 45 const PaintData& paint_data); 46 void restore(); 47 int getSaveCount(); 48 49 void translate(double dx, double dy); 50 void scale(double sx, double sy); 51 void rotate(double radians); 52 void skew(double sx, double sy); 53 void transform(const std::vector<double>& matrix4); 54 55 void clipRect(double left, 56 double top, 57 double right, 58 double bottom, 59 SkClipOp clipOp, 60 bool doAntiAlias = true); 61 void clipRRect(const RRect& rrect, bool doAntiAlias = true); 62 void clipPath(const CanvasPath* path, bool doAntiAlias = true); 63 64 void drawColor(SkColor color, SkBlendMode blend_mode); 65 void drawLine(double x1, 66 double y1, 67 double x2, 68 double y2, 69 const Paint& paint, 70 const PaintData& paint_data); 71 void drawPaint(const Paint& paint, const PaintData& paint_data); 72 void drawRect(double left, 73 double top, 74 double right, 75 double bottom, 76 const Paint& paint, 77 const PaintData& paint_data); 78 void drawRRect(const RRect& rrect, 79 const Paint& paint, 80 const PaintData& paint_data); 81 void drawDRRect(const RRect& outer, 82 const RRect& inner, 83 const Paint& paint, 84 const PaintData& paint_data); 85 void drawOval(double left, 86 double top, 87 double right, 88 double bottom, 89 const Paint& paint, 90 const PaintData& paint_data); 91 void drawCircle(double x, 92 double y, 93 double radius, 94 const Paint& paint, 95 const PaintData& paint_data); 96 void drawArc(double left, 97 double top, 98 double right, 99 double bottom, 100 double startAngle, 101 double sweepAngle, 102 bool useCenter, 103 const Paint& paint, 104 const PaintData& paint_data); 105 void drawPath(const CanvasPath* path, 106 const Paint& paint, 107 const PaintData& paint_data); 108 void drawImage(const CanvasImage* image, 109 double x, 110 double y, 111 const Paint& paint, 112 const PaintData& paint_data); 113 void drawImageRect(const CanvasImage* image, 114 double src_left, 115 double src_top, 116 double src_right, 117 double src_bottom, 118 double dst_left, 119 double dst_top, 120 double dst_right, 121 double dst_bottom, 122 const Paint& paint, 123 const PaintData& paint_data); 124 void drawImageNine(const CanvasImage* image, 125 double center_left, 126 double center_top, 127 double center_right, 128 double center_bottom, 129 double dst_left, 130 double dst_top, 131 double dst_right, 132 double dst_bottom, 133 const Paint& paint, 134 const PaintData& paint_data); 135 void drawPicture(Picture* picture); 136 137 // The paint argument is first for the following functions because Paint 138 // unwraps a number of C++ objects. Once we create a view unto a 139 // Float32List, we cannot re-enter the VM to unwrap objects. That means we 140 // either need to process the paint argument first. 141 142 void drawPoints(const Paint& paint, 143 const PaintData& paint_data, 144 SkCanvas::PointMode point_mode, 145 const tonic::Float32List& points); 146 147 void drawVertices(const Vertices* vertices, 148 SkBlendMode blend_mode, 149 const Paint& paint, 150 const PaintData& paint_data); 151 152 void drawAtlas(const Paint& paint, 153 const PaintData& paint_data, 154 CanvasImage* atlas, 155 const tonic::Float32List& transforms, 156 const tonic::Float32List& rects, 157 const tonic::Int32List& colors, 158 SkBlendMode blend_mode, 159 const tonic::Float32List& cull_rect); 160 161 void drawShadow(const CanvasPath* path, 162 SkColor color, 163 double elevation, 164 bool transparentOccluder); 165 canvas()166 SkCanvas* canvas() const { return canvas_; } 167 void Clear(); 168 bool IsRecording() const; 169 170 static void RegisterNatives(tonic::DartLibraryNatives* natives); 171 172 private: 173 explicit Canvas(SkCanvas* canvas); 174 175 // The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording, 176 // which does not transfer ownership. For this reason, we hold a raw 177 // pointer and manually set to null in Clear. 178 SkCanvas* canvas_; 179 }; 180 181 } // namespace flutter 182 183 #endif // FLUTTER_LIB_UI_PAINTING_CANVAS_H_ 184