Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f7dfb2b

Browse files
authored
remove use of SkCanvas and DLCanvasRecorder from ui.Canvas native code (#39599)
1 parent 89d41d1 commit f7dfb2b

File tree

4 files changed

+61
-102
lines changed

4 files changed

+61
-102
lines changed

lib/ui/painting/canvas.cc

Lines changed: 48 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,16 @@
33
// found in the LICENSE file.
44

55
#include "flutter/lib/ui/painting/canvas.h"
6-
#include "flutter/lib/ui/painting/image_filter.h"
76

87
#include <cmath>
98

10-
#include "flutter/display_list/display_list_blend_mode.h"
119
#include "flutter/display_list/display_list_builder.h"
12-
#include "flutter/display_list/display_list_canvas_dispatcher.h"
13-
#include "flutter/flow/layers/physical_shape_layer.h"
1410
#include "flutter/lib/ui/painting/image.h"
15-
#include "flutter/lib/ui/painting/matrix.h"
11+
#include "flutter/lib/ui/painting/image_filter.h"
1612
#include "flutter/lib/ui/painting/paint.h"
1713
#include "flutter/lib/ui/ui_dart_state.h"
1814
#include "flutter/lib/ui/window/platform_configuration.h"
1915
#include "flutter/lib/ui/window/window.h"
20-
#include "third_party/skia/include/core/SkBitmap.h"
21-
#include "third_party/skia/include/core/SkCanvas.h"
22-
#include "third_party/skia/include/core/SkRSXform.h"
23-
#include "third_party/tonic/converter/dart_converter.h"
24-
#include "third_party/tonic/dart_args.h"
25-
#include "third_party/tonic/dart_binding_macros.h"
26-
#include "third_party/tonic/dart_library_natives.h"
2716

2817
using tonic::ToDart;
2918

@@ -45,27 +34,19 @@ void Canvas::Create(Dart_Handle wrapper,
4534
return;
4635
}
4736

48-
// This call will implicitly initialize the |canvas_| field with an SkCanvas
49-
// whether or not we are using display_list. Now that all of the code here
50-
// in canvas.cc will direct calls to the DisplayListBuilder we could almost
51-
// stop initializing that field for the display list case. Unfortunately,
52-
// the text code in paragraph.cc still needs to present its output to an
53-
// SkCanvas* which means without significant work to the internals of the
54-
// paragraph code, we are going to continue to need the canvas adapter and
55-
// field and getter.
5637
fml::RefPtr<Canvas> canvas = fml::MakeRefCounted<Canvas>(
5738
recorder->BeginRecording(SkRect::MakeLTRB(left, top, right, bottom)));
5839
recorder->set_canvas(canvas);
59-
canvas->display_list_recorder_ = recorder->display_list_recorder();
6040
canvas->AssociateWithDartWrapper(wrapper);
6141
}
6242

63-
Canvas::Canvas(SkCanvas* canvas) : canvas_(canvas) {}
43+
Canvas::Canvas(sk_sp<DisplayListBuilder> builder)
44+
: display_list_builder_(std::move(builder)) {}
6445

6546
Canvas::~Canvas() {}
6647

6748
void Canvas::save() {
68-
if (display_list_recorder_) {
49+
if (display_list_builder_) {
6950
builder()->save();
7051
}
7152
}
@@ -75,7 +56,7 @@ void Canvas::saveLayerWithoutBounds(Dart_Handle paint_objects,
7556
Paint paint(paint_objects, paint_data);
7657

7758
FML_DCHECK(paint.isNotNull());
78-
if (display_list_recorder_) {
59+
if (display_list_builder_) {
7960
bool restore_with_paint =
8061
paint.sync_to(builder(), kSaveLayerWithPaintFlags);
8162
FML_DCHECK(restore_with_paint);
@@ -94,7 +75,7 @@ void Canvas::saveLayer(double left,
9475

9576
FML_DCHECK(paint.isNotNull());
9677
SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
97-
if (display_list_recorder_) {
78+
if (display_list_builder_) {
9879
bool restore_with_paint =
9980
paint.sync_to(builder(), kSaveLayerWithPaintFlags);
10081
FML_DCHECK(restore_with_paint);
@@ -104,53 +85,53 @@ void Canvas::saveLayer(double left,
10485
}
10586

10687
void Canvas::restore() {
107-
if (display_list_recorder_) {
88+
if (display_list_builder_) {
10889
builder()->restore();
10990
}
11091
}
11192

11293
int Canvas::getSaveCount() {
113-
if (display_list_recorder_) {
94+
if (display_list_builder_) {
11495
return builder()->getSaveCount();
11596
} else {
11697
return 0;
11798
}
11899
}
119100

120101
void Canvas::restoreToCount(int count) {
121-
if (display_list_recorder_ && count < getSaveCount()) {
102+
if (display_list_builder_ && count < getSaveCount()) {
122103
builder()->restoreToCount(count);
123104
}
124105
}
125106

126107
void Canvas::translate(double dx, double dy) {
127-
if (display_list_recorder_) {
108+
if (display_list_builder_) {
128109
builder()->translate(dx, dy);
129110
}
130111
}
131112

132113
void Canvas::scale(double sx, double sy) {
133-
if (display_list_recorder_) {
114+
if (display_list_builder_) {
134115
builder()->scale(sx, sy);
135116
}
136117
}
137118

138119
void Canvas::rotate(double radians) {
139-
if (display_list_recorder_) {
120+
if (display_list_builder_) {
140121
builder()->rotate(radians * 180.0 / M_PI);
141122
}
142123
}
143124

144125
void Canvas::skew(double sx, double sy) {
145-
if (display_list_recorder_) {
126+
if (display_list_builder_) {
146127
builder()->skew(sx, sy);
147128
}
148129
}
149130

150131
void Canvas::transform(const tonic::Float64List& matrix4) {
151132
// The Float array stored by Dart Matrix4 is in column-major order
152133
// Both DisplayList and SkM44 constructor take row-major matrix order
153-
if (display_list_recorder_) {
134+
if (display_list_builder_) {
154135
// clang-format off
155136
builder()->transformFullPerspective(
156137
matrix4[ 0], matrix4[ 4], matrix4[ 8], matrix4[12],
@@ -162,16 +143,15 @@ void Canvas::transform(const tonic::Float64List& matrix4) {
162143
}
163144

164145
void Canvas::getTransform(Dart_Handle matrix4_handle) {
165-
SkM44 sk_m44 =
166-
display_list_recorder_
167-
? display_list_recorder_->builder()->getTransformFullPerspective()
168-
: canvas_->getLocalToDevice();
169-
SkScalar m44_values[16];
170-
// The Float array stored by Dart Matrix4 is in column-major order
171-
sk_m44.getColMajor(m44_values);
172-
auto matrix4 = tonic::Float64List(matrix4_handle);
173-
for (int i = 0; i < 16; i++) {
174-
matrix4[i] = m44_values[i];
146+
if (display_list_builder_) {
147+
SkM44 sk_m44 = builder()->getTransformFullPerspective();
148+
SkScalar m44_values[16];
149+
// The Float array stored by Dart Matrix4 is in column-major order
150+
sk_m44.getColMajor(m44_values);
151+
auto matrix4 = tonic::Float64List(matrix4_handle);
152+
for (int i = 0; i < 16; i++) {
153+
matrix4[i] = m44_values[i];
154+
}
175155
}
176156
}
177157

@@ -181,14 +161,14 @@ void Canvas::clipRect(double left,
181161
double bottom,
182162
SkClipOp clipOp,
183163
bool doAntiAlias) {
184-
if (display_list_recorder_) {
164+
if (display_list_builder_) {
185165
builder()->clipRect(SkRect::MakeLTRB(left, top, right, bottom), clipOp,
186166
doAntiAlias);
187167
}
188168
}
189169

190170
void Canvas::clipRRect(const RRect& rrect, bool doAntiAlias) {
191-
if (display_list_recorder_) {
171+
if (display_list_builder_) {
192172
builder()->clipRRect(rrect.sk_rrect, SkClipOp::kIntersect, doAntiAlias);
193173
}
194174
}
@@ -199,13 +179,13 @@ void Canvas::clipPath(const CanvasPath* path, bool doAntiAlias) {
199179
ToDart("Canvas.clipPath called with non-genuine Path."));
200180
return;
201181
}
202-
if (display_list_recorder_) {
182+
if (display_list_builder_) {
203183
builder()->clipPath(path->path(), SkClipOp::kIntersect, doAntiAlias);
204184
}
205185
}
206186

207187
void Canvas::getDestinationClipBounds(Dart_Handle rect_handle) {
208-
if (display_list_recorder_) {
188+
if (display_list_builder_) {
209189
auto rect = tonic::Float64List(rect_handle);
210190
SkRect bounds = builder()->getDestinationClipBounds();
211191
rect[0] = bounds.fLeft;
@@ -216,9 +196,9 @@ void Canvas::getDestinationClipBounds(Dart_Handle rect_handle) {
216196
}
217197

218198
void Canvas::getLocalClipBounds(Dart_Handle rect_handle) {
219-
if (display_list_recorder_) {
199+
if (display_list_builder_) {
220200
auto rect = tonic::Float64List(rect_handle);
221-
SkRect bounds = display_list_recorder_->builder()->getLocalClipBounds();
201+
SkRect bounds = builder()->getLocalClipBounds();
222202
rect[0] = bounds.fLeft;
223203
rect[1] = bounds.fTop;
224204
rect[2] = bounds.fRight;
@@ -227,7 +207,7 @@ void Canvas::getLocalClipBounds(Dart_Handle rect_handle) {
227207
}
228208

229209
void Canvas::drawColor(SkColor color, DlBlendMode blend_mode) {
230-
if (display_list_recorder_) {
210+
if (display_list_builder_) {
231211
builder()->drawColor(color, blend_mode);
232212
}
233213
}
@@ -241,7 +221,7 @@ void Canvas::drawLine(double x1,
241221
Paint paint(paint_objects, paint_data);
242222

243223
FML_DCHECK(paint.isNotNull());
244-
if (display_list_recorder_) {
224+
if (display_list_builder_) {
245225
paint.sync_to(builder(), kDrawLineFlags);
246226
builder()->drawLine(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2));
247227
}
@@ -251,7 +231,7 @@ void Canvas::drawPaint(Dart_Handle paint_objects, Dart_Handle paint_data) {
251231
Paint paint(paint_objects, paint_data);
252232

253233
FML_DCHECK(paint.isNotNull());
254-
if (display_list_recorder_) {
234+
if (display_list_builder_) {
255235
paint.sync_to(builder(), kDrawPaintFlags);
256236
std::shared_ptr<const DlImageFilter> filter = builder()->getImageFilter();
257237
if (filter && !filter->asColorFilter()) {
@@ -272,7 +252,7 @@ void Canvas::drawRect(double left,
272252
Paint paint(paint_objects, paint_data);
273253

274254
FML_DCHECK(paint.isNotNull());
275-
if (display_list_recorder_) {
255+
if (display_list_builder_) {
276256
paint.sync_to(builder(), kDrawRectFlags);
277257
builder()->drawRect(SkRect::MakeLTRB(left, top, right, bottom));
278258
}
@@ -284,7 +264,7 @@ void Canvas::drawRRect(const RRect& rrect,
284264
Paint paint(paint_objects, paint_data);
285265

286266
FML_DCHECK(paint.isNotNull());
287-
if (display_list_recorder_) {
267+
if (display_list_builder_) {
288268
paint.sync_to(builder(), kDrawRRectFlags);
289269
builder()->drawRRect(rrect.sk_rrect);
290270
}
@@ -297,7 +277,7 @@ void Canvas::drawDRRect(const RRect& outer,
297277
Paint paint(paint_objects, paint_data);
298278

299279
FML_DCHECK(paint.isNotNull());
300-
if (display_list_recorder_) {
280+
if (display_list_builder_) {
301281
paint.sync_to(builder(), kDrawDRRectFlags);
302282
builder()->drawDRRect(outer.sk_rrect, inner.sk_rrect);
303283
}
@@ -312,7 +292,7 @@ void Canvas::drawOval(double left,
312292
Paint paint(paint_objects, paint_data);
313293

314294
FML_DCHECK(paint.isNotNull());
315-
if (display_list_recorder_) {
295+
if (display_list_builder_) {
316296
paint.sync_to(builder(), kDrawOvalFlags);
317297
builder()->drawOval(SkRect::MakeLTRB(left, top, right, bottom));
318298
}
@@ -326,7 +306,7 @@ void Canvas::drawCircle(double x,
326306
Paint paint(paint_objects, paint_data);
327307

328308
FML_DCHECK(paint.isNotNull());
329-
if (display_list_recorder_) {
309+
if (display_list_builder_) {
330310
paint.sync_to(builder(), kDrawCircleFlags);
331311
builder()->drawCircle(SkPoint::Make(x, y), radius);
332312
}
@@ -344,7 +324,7 @@ void Canvas::drawArc(double left,
344324
Paint paint(paint_objects, paint_data);
345325

346326
FML_DCHECK(paint.isNotNull());
347-
if (display_list_recorder_) {
327+
if (display_list_builder_) {
348328
paint.sync_to(builder(),
349329
useCenter //
350330
? kDrawArcWithCenterFlags
@@ -366,7 +346,7 @@ void Canvas::drawPath(const CanvasPath* path,
366346
ToDart("Canvas.drawPath called with non-genuine Path."));
367347
return;
368348
}
369-
if (display_list_recorder_) {
349+
if (display_list_builder_) {
370350
paint.sync_to(builder(), kDrawPathFlags);
371351
builder()->drawPath(path->path());
372352
}
@@ -395,7 +375,7 @@ Dart_Handle Canvas::drawImage(const CanvasImage* image,
395375
}
396376

397377
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
398-
if (display_list_recorder_) {
378+
if (display_list_builder_) {
399379
bool with_attributes = paint.sync_to(builder(), kDrawImageWithPaintFlags);
400380
builder()->drawImage(dl_image, SkPoint::Make(x, y), sampling,
401381
with_attributes);
@@ -434,7 +414,7 @@ Dart_Handle Canvas::drawImageRect(const CanvasImage* image,
434414
SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
435415
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
436416
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
437-
if (display_list_recorder_) {
417+
if (display_list_builder_) {
438418
bool with_attributes =
439419
paint.sync_to(builder(), kDrawImageRectWithPaintFlags);
440420
builder()->drawImageRect(dl_image, src, dst, sampling, with_attributes,
@@ -476,7 +456,7 @@ Dart_Handle Canvas::drawImageNine(const CanvasImage* image,
476456
center.round(&icenter);
477457
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
478458
auto filter = ImageFilter::FilterModeFromIndex(bitmapSamplingIndex);
479-
if (display_list_recorder_) {
459+
if (display_list_builder_) {
480460
bool with_attributes =
481461
paint.sync_to(builder(), kDrawImageNineWithPaintFlags);
482462
builder()->drawImageNine(dl_image, icenter, dst, filter, with_attributes);
@@ -491,10 +471,8 @@ void Canvas::drawPicture(Picture* picture) {
491471
return;
492472
}
493473
if (picture->display_list()) {
494-
if (display_list_recorder_) {
474+
if (display_list_builder_) {
495475
builder()->drawDisplayList(picture->display_list());
496-
} else if (canvas_) {
497-
picture->display_list()->RenderTo(canvas_);
498476
}
499477
} else {
500478
FML_DCHECK(false);
@@ -511,7 +489,7 @@ void Canvas::drawPoints(Dart_Handle paint_objects,
511489
"SkPoint doesn't use floats.");
512490

513491
FML_DCHECK(paint.isNotNull());
514-
if (display_list_recorder_) {
492+
if (display_list_builder_) {
515493
switch (point_mode) {
516494
case SkCanvas::kPoints_PointMode:
517495
paint.sync_to(builder(), kDrawPointsAsPointsFlags);
@@ -541,7 +519,7 @@ void Canvas::drawVertices(const Vertices* vertices,
541519
return;
542520
}
543521
FML_DCHECK(paint.isNotNull());
544-
if (display_list_recorder_) {
522+
if (display_list_builder_) {
545523
paint.sync_to(builder(), kDrawVerticesFlags);
546524
builder()->drawVertices(vertices->vertices(), blend_mode);
547525
}
@@ -578,7 +556,7 @@ Dart_Handle Canvas::drawAtlas(Dart_Handle paint_objects,
578556
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
579557

580558
FML_DCHECK(paint.isNotNull());
581-
if (display_list_recorder_) {
559+
if (display_list_builder_) {
582560
tonic::Float32List transforms(transforms_handle);
583561
tonic::Float32List rects(rects_handle);
584562
tonic::Int32List colors(colors_handle);
@@ -610,7 +588,7 @@ void Canvas::drawShadow(const CanvasPath* path,
610588
->get_window(0)
611589
->viewport_metrics()
612590
.device_pixel_ratio;
613-
if (display_list_recorder_) {
591+
if (display_list_builder_) {
614592
// The DrawShadow mechanism results in non-public operations to be
615593
// performed on the canvas involving an SkDrawShadowRec. Since we
616594
// cannot include the header that defines that structure, we cannot
@@ -624,8 +602,7 @@ void Canvas::drawShadow(const CanvasPath* path,
624602
}
625603

626604
void Canvas::Invalidate() {
627-
canvas_ = nullptr;
628-
display_list_recorder_ = nullptr;
605+
display_list_builder_ = nullptr;
629606
if (dart_wrapper()) {
630607
ClearDartWrapper();
631608
}

0 commit comments

Comments
 (0)