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

Commit 8b42ca8

Browse files
authored
Revert "Remove dlCanvasRecorder from flutter::PictureRecorder (#38127)"
This reverts commit 6aa4ccd.
1 parent 84abf21 commit 8b42ca8

File tree

6 files changed

+78
-54
lines changed

6 files changed

+78
-54
lines changed

lib/ui/painting/canvas.cc

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ void Canvas::Create(Dart_Handle wrapper,
5656
fml::RefPtr<Canvas> canvas = fml::MakeRefCounted<Canvas>(
5757
recorder->BeginRecording(SkRect::MakeLTRB(left, top, right, bottom)));
5858
recorder->set_canvas(canvas);
59+
canvas->display_list_recorder_ = recorder->display_list_recorder();
5960
canvas->AssociateWithDartWrapper(wrapper);
6061
}
6162

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

6565
Canvas::~Canvas() {}
6666

6767
void Canvas::save() {
68-
if (display_list_builder_) {
68+
if (display_list_recorder_) {
6969
builder()->save();
7070
}
7171
}
@@ -75,7 +75,7 @@ void Canvas::saveLayerWithoutBounds(Dart_Handle paint_objects,
7575
Paint paint(paint_objects, paint_data);
7676

7777
FML_DCHECK(paint.isNotNull());
78-
if (display_list_builder_) {
78+
if (display_list_recorder_) {
7979
bool restore_with_paint =
8080
paint.sync_to(builder(), kSaveLayerWithPaintFlags);
8181
FML_DCHECK(restore_with_paint);
@@ -94,7 +94,7 @@ void Canvas::saveLayer(double left,
9494

9595
FML_DCHECK(paint.isNotNull());
9696
SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
97-
if (display_list_builder_) {
97+
if (display_list_recorder_) {
9898
bool restore_with_paint =
9999
paint.sync_to(builder(), kSaveLayerWithPaintFlags);
100100
FML_DCHECK(restore_with_paint);
@@ -104,53 +104,53 @@ void Canvas::saveLayer(double left,
104104
}
105105

106106
void Canvas::restore() {
107-
if (display_list_builder_) {
107+
if (display_list_recorder_) {
108108
builder()->restore();
109109
}
110110
}
111111

112112
int Canvas::getSaveCount() {
113-
if (display_list_builder_) {
113+
if (display_list_recorder_) {
114114
return builder()->getSaveCount();
115115
} else {
116116
return 0;
117117
}
118118
}
119119

120120
void Canvas::restoreToCount(int count) {
121-
if (display_list_builder_ && count < getSaveCount()) {
121+
if (display_list_recorder_ && count < getSaveCount()) {
122122
builder()->restoreToCount(count);
123123
}
124124
}
125125

126126
void Canvas::translate(double dx, double dy) {
127-
if (display_list_builder_) {
127+
if (display_list_recorder_) {
128128
builder()->translate(dx, dy);
129129
}
130130
}
131131

132132
void Canvas::scale(double sx, double sy) {
133-
if (display_list_builder_) {
133+
if (display_list_recorder_) {
134134
builder()->scale(sx, sy);
135135
}
136136
}
137137

138138
void Canvas::rotate(double radians) {
139-
if (display_list_builder_) {
139+
if (display_list_recorder_) {
140140
builder()->rotate(radians * 180.0 / M_PI);
141141
}
142142
}
143143

144144
void Canvas::skew(double sx, double sy) {
145-
if (display_list_builder_) {
145+
if (display_list_recorder_) {
146146
builder()->skew(sx, sy);
147147
}
148148
}
149149

150150
void Canvas::transform(const tonic::Float64List& matrix4) {
151151
// The Float array stored by Dart Matrix4 is in column-major order
152152
// Both DisplayList and SkM44 constructor take row-major matrix order
153-
if (display_list_builder_) {
153+
if (display_list_recorder_) {
154154
// clang-format off
155155
builder()->transformFullPerspective(
156156
matrix4[ 0], matrix4[ 4], matrix4[ 8], matrix4[12],
@@ -162,7 +162,10 @@ void Canvas::transform(const tonic::Float64List& matrix4) {
162162
}
163163

164164
void Canvas::getTransform(Dart_Handle matrix4_handle) {
165-
SkM44 sk_m44 = display_list_builder_->getTransformFullPerspective();
165+
SkM44 sk_m44 =
166+
display_list_recorder_
167+
? display_list_recorder_->builder()->getTransformFullPerspective()
168+
: canvas_->getLocalToDevice();
166169
SkScalar m44_values[16];
167170
// The Float array stored by Dart Matrix4 is in column-major order
168171
sk_m44.getColMajor(m44_values);
@@ -178,14 +181,14 @@ void Canvas::clipRect(double left,
178181
double bottom,
179182
SkClipOp clipOp,
180183
bool doAntiAlias) {
181-
if (display_list_builder_) {
184+
if (display_list_recorder_) {
182185
builder()->clipRect(SkRect::MakeLTRB(left, top, right, bottom), clipOp,
183186
doAntiAlias);
184187
}
185188
}
186189

187190
void Canvas::clipRRect(const RRect& rrect, bool doAntiAlias) {
188-
if (display_list_builder_) {
191+
if (display_list_recorder_) {
189192
builder()->clipRRect(rrect.sk_rrect, SkClipOp::kIntersect, doAntiAlias);
190193
}
191194
}
@@ -196,13 +199,13 @@ void Canvas::clipPath(const CanvasPath* path, bool doAntiAlias) {
196199
ToDart("Canvas.clipPath called with non-genuine Path."));
197200
return;
198201
}
199-
if (display_list_builder_) {
202+
if (display_list_recorder_) {
200203
builder()->clipPath(path->path(), SkClipOp::kIntersect, doAntiAlias);
201204
}
202205
}
203206

204207
void Canvas::getDestinationClipBounds(Dart_Handle rect_handle) {
205-
if (display_list_builder_) {
208+
if (display_list_recorder_) {
206209
auto rect = tonic::Float64List(rect_handle);
207210
SkRect bounds = builder()->getDestinationClipBounds();
208211
rect[0] = bounds.fLeft;
@@ -213,9 +216,9 @@ void Canvas::getDestinationClipBounds(Dart_Handle rect_handle) {
213216
}
214217

215218
void Canvas::getLocalClipBounds(Dart_Handle rect_handle) {
216-
if (display_list_builder_) {
219+
if (display_list_recorder_) {
217220
auto rect = tonic::Float64List(rect_handle);
218-
SkRect bounds = display_list_builder_->getLocalClipBounds();
221+
SkRect bounds = display_list_recorder_->builder()->getLocalClipBounds();
219222
rect[0] = bounds.fLeft;
220223
rect[1] = bounds.fTop;
221224
rect[2] = bounds.fRight;
@@ -224,7 +227,7 @@ void Canvas::getLocalClipBounds(Dart_Handle rect_handle) {
224227
}
225228

226229
void Canvas::drawColor(SkColor color, DlBlendMode blend_mode) {
227-
if (display_list_builder_) {
230+
if (display_list_recorder_) {
228231
builder()->drawColor(color, blend_mode);
229232
}
230233
}
@@ -238,7 +241,7 @@ void Canvas::drawLine(double x1,
238241
Paint paint(paint_objects, paint_data);
239242

240243
FML_DCHECK(paint.isNotNull());
241-
if (display_list_builder_) {
244+
if (display_list_recorder_) {
242245
paint.sync_to(builder(), kDrawLineFlags);
243246
builder()->drawLine(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2));
244247
}
@@ -248,7 +251,7 @@ void Canvas::drawPaint(Dart_Handle paint_objects, Dart_Handle paint_data) {
248251
Paint paint(paint_objects, paint_data);
249252

250253
FML_DCHECK(paint.isNotNull());
251-
if (display_list_builder_) {
254+
if (display_list_recorder_) {
252255
paint.sync_to(builder(), kDrawPaintFlags);
253256
std::shared_ptr<const DlImageFilter> filter = builder()->getImageFilter();
254257
if (filter && !filter->asColorFilter()) {
@@ -269,7 +272,7 @@ void Canvas::drawRect(double left,
269272
Paint paint(paint_objects, paint_data);
270273

271274
FML_DCHECK(paint.isNotNull());
272-
if (display_list_builder_) {
275+
if (display_list_recorder_) {
273276
paint.sync_to(builder(), kDrawRectFlags);
274277
builder()->drawRect(SkRect::MakeLTRB(left, top, right, bottom));
275278
}
@@ -281,7 +284,7 @@ void Canvas::drawRRect(const RRect& rrect,
281284
Paint paint(paint_objects, paint_data);
282285

283286
FML_DCHECK(paint.isNotNull());
284-
if (display_list_builder_) {
287+
if (display_list_recorder_) {
285288
paint.sync_to(builder(), kDrawRRectFlags);
286289
builder()->drawRRect(rrect.sk_rrect);
287290
}
@@ -294,7 +297,7 @@ void Canvas::drawDRRect(const RRect& outer,
294297
Paint paint(paint_objects, paint_data);
295298

296299
FML_DCHECK(paint.isNotNull());
297-
if (display_list_builder_) {
300+
if (display_list_recorder_) {
298301
paint.sync_to(builder(), kDrawDRRectFlags);
299302
builder()->drawDRRect(outer.sk_rrect, inner.sk_rrect);
300303
}
@@ -309,7 +312,7 @@ void Canvas::drawOval(double left,
309312
Paint paint(paint_objects, paint_data);
310313

311314
FML_DCHECK(paint.isNotNull());
312-
if (display_list_builder_) {
315+
if (display_list_recorder_) {
313316
paint.sync_to(builder(), kDrawOvalFlags);
314317
builder()->drawOval(SkRect::MakeLTRB(left, top, right, bottom));
315318
}
@@ -323,7 +326,7 @@ void Canvas::drawCircle(double x,
323326
Paint paint(paint_objects, paint_data);
324327

325328
FML_DCHECK(paint.isNotNull());
326-
if (display_list_builder_) {
329+
if (display_list_recorder_) {
327330
paint.sync_to(builder(), kDrawCircleFlags);
328331
builder()->drawCircle(SkPoint::Make(x, y), radius);
329332
}
@@ -341,7 +344,7 @@ void Canvas::drawArc(double left,
341344
Paint paint(paint_objects, paint_data);
342345

343346
FML_DCHECK(paint.isNotNull());
344-
if (display_list_builder_) {
347+
if (display_list_recorder_) {
345348
paint.sync_to(builder(),
346349
useCenter //
347350
? kDrawArcWithCenterFlags
@@ -363,7 +366,7 @@ void Canvas::drawPath(const CanvasPath* path,
363366
ToDart("Canvas.drawPath called with non-genuine Path."));
364367
return;
365368
}
366-
if (display_list_builder_) {
369+
if (display_list_recorder_) {
367370
paint.sync_to(builder(), kDrawPathFlags);
368371
builder()->drawPath(path->path());
369372
}
@@ -392,7 +395,7 @@ Dart_Handle Canvas::drawImage(const CanvasImage* image,
392395
}
393396

394397
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
395-
if (display_list_builder_) {
398+
if (display_list_recorder_) {
396399
bool with_attributes = paint.sync_to(builder(), kDrawImageWithPaintFlags);
397400
builder()->drawImage(dl_image, SkPoint::Make(x, y), sampling,
398401
with_attributes);
@@ -431,7 +434,7 @@ Dart_Handle Canvas::drawImageRect(const CanvasImage* image,
431434
SkRect src = SkRect::MakeLTRB(src_left, src_top, src_right, src_bottom);
432435
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
433436
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
434-
if (display_list_builder_) {
437+
if (display_list_recorder_) {
435438
bool with_attributes =
436439
paint.sync_to(builder(), kDrawImageRectWithPaintFlags);
437440
builder()->drawImageRect(dl_image, src, dst, sampling, with_attributes,
@@ -473,7 +476,7 @@ Dart_Handle Canvas::drawImageNine(const CanvasImage* image,
473476
center.round(&icenter);
474477
SkRect dst = SkRect::MakeLTRB(dst_left, dst_top, dst_right, dst_bottom);
475478
auto filter = ImageFilter::FilterModeFromIndex(bitmapSamplingIndex);
476-
if (display_list_builder_) {
479+
if (display_list_recorder_) {
477480
bool with_attributes =
478481
paint.sync_to(builder(), kDrawImageNineWithPaintFlags);
479482
builder()->drawImageNine(dl_image, icenter, dst, filter, with_attributes);
@@ -488,8 +491,10 @@ void Canvas::drawPicture(Picture* picture) {
488491
return;
489492
}
490493
if (picture->display_list()) {
491-
if (display_list_builder_) {
494+
if (display_list_recorder_) {
492495
builder()->drawDisplayList(picture->display_list());
496+
} else if (canvas_) {
497+
picture->display_list()->RenderTo(canvas_);
493498
}
494499
} else {
495500
FML_DCHECK(false);
@@ -506,7 +511,7 @@ void Canvas::drawPoints(Dart_Handle paint_objects,
506511
"SkPoint doesn't use floats.");
507512

508513
FML_DCHECK(paint.isNotNull());
509-
if (display_list_builder_) {
514+
if (display_list_recorder_) {
510515
switch (point_mode) {
511516
case SkCanvas::kPoints_PointMode:
512517
paint.sync_to(builder(), kDrawPointsAsPointsFlags);
@@ -536,7 +541,7 @@ void Canvas::drawVertices(const Vertices* vertices,
536541
return;
537542
}
538543
FML_DCHECK(paint.isNotNull());
539-
if (display_list_builder_) {
544+
if (display_list_recorder_) {
540545
paint.sync_to(builder(), kDrawVerticesFlags);
541546
builder()->drawVertices(vertices->vertices(), blend_mode);
542547
}
@@ -573,7 +578,7 @@ Dart_Handle Canvas::drawAtlas(Dart_Handle paint_objects,
573578
auto sampling = ImageFilter::SamplingFromIndex(filterQualityIndex);
574579

575580
FML_DCHECK(paint.isNotNull());
576-
if (display_list_builder_) {
581+
if (display_list_recorder_) {
577582
tonic::Float32List transforms(transforms_handle);
578583
tonic::Float32List rects(rects_handle);
579584
tonic::Int32List colors(colors_handle);
@@ -605,7 +610,7 @@ void Canvas::drawShadow(const CanvasPath* path,
605610
->get_window(0)
606611
->viewport_metrics()
607612
.device_pixel_ratio;
608-
if (display_list_builder_) {
613+
if (display_list_recorder_) {
609614
// The DrawShadow mechanism results in non-public operations to be
610615
// performed on the canvas involving an SkDrawShadowRec. Since we
611616
// cannot include the header that defines that structure, we cannot
@@ -619,7 +624,8 @@ void Canvas::drawShadow(const CanvasPath* path,
619624
}
620625

621626
void Canvas::Invalidate() {
622-
display_list_builder_ = nullptr;
627+
canvas_ = nullptr;
628+
display_list_recorder_ = nullptr;
623629
if (dart_wrapper()) {
624630
ClearDartWrapper();
625631
}

lib/ui/painting/canvas.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "flutter/lib/ui/painting/rrect.h"
1515
#include "flutter/lib/ui/painting/vertices.h"
1616
#include "flutter/lib/ui/ui_dart_state.h"
17+
#include "third_party/skia/include/core/SkCanvas.h"
18+
#include "third_party/skia/include/utils/SkShadowUtils.h"
1719
#include "third_party/tonic/typed_data/typed_list.h"
1820

1921
namespace flutter {
@@ -185,20 +187,29 @@ class Canvas : public RefCountedDartWrappable<Canvas>, DisplayListOpFlags {
185187
double elevation,
186188
bool transparentOccluder);
187189

190+
SkCanvas* canvas() const { return canvas_; }
188191
void Invalidate();
189192

190-
DisplayListBuilder* builder() { return display_list_builder_.get(); }
193+
DisplayListBuilder* builder() {
194+
return display_list_recorder_ ? display_list_recorder_->builder().get()
195+
: nullptr;
196+
}
191197

192198
private:
193-
explicit Canvas(sk_sp<DisplayListBuilder> canvas);
199+
explicit Canvas(SkCanvas* canvas);
200+
201+
// The SkCanvas is supplied by a call to SkPictureRecorder::beginRecording,
202+
// which does not transfer ownership. For this reason, we hold a raw
203+
// pointer and manually set to null in Clear.
204+
SkCanvas* canvas_;
194205

195206
// A copy of the recorder used by the SkCanvas->DisplayList adapter for cases
196207
// where we cannot record the SkCanvas method call through the various OnOp()
197208
// virtual methods or where we can be more efficient by talking directly in
198209
// the DisplayList operation lexicon. The recorder has a method for recording
199210
// paint attributes from an SkPaint and an operation type as well as access
200211
// to the raw DisplayListBuilder for emitting custom rendering operations.
201-
sk_sp<DisplayListBuilder> display_list_builder_;
212+
sk_sp<DisplayListCanvasRecorder> display_list_recorder_;
202213
};
203214

204215
} // namespace flutter

lib/ui/painting/picture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flutter/lib/ui/dart_wrapper.h"
1212
#include "flutter/lib/ui/painting/image.h"
1313
#include "flutter/lib/ui/ui_dart_state.h"
14+
#include "third_party/skia/include/core/SkPicture.h"
1415

1516
namespace flutter {
1617
class Canvas;

0 commit comments

Comments
 (0)