1
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
- // FLUTTER_NOLINT
5
4
6
5
#include " flutter/lib/ui/painting/canvas.h"
7
6
@@ -77,9 +76,11 @@ fml::RefPtr<Canvas> Canvas::Create(PictureRecorder* recorder,
77
76
double top,
78
77
double right,
79
78
double bottom) {
80
- if (!recorder)
79
+ if (!recorder) {
81
80
Dart_ThrowException (
82
81
ToDart (" Canvas constructor called with non-genuine PictureRecorder." ));
82
+ return nullptr ;
83
+ }
83
84
fml::RefPtr<Canvas> canvas = fml::MakeRefCounted<Canvas>(
84
85
recorder->BeginRecording (SkRect::MakeLTRB (left, top, right, bottom)));
85
86
recorder->set_canvas (canvas);
@@ -91,15 +92,17 @@ Canvas::Canvas(SkCanvas* canvas) : canvas_(canvas) {}
91
92
Canvas::~Canvas () {}
92
93
93
94
void Canvas::save () {
94
- if (!canvas_)
95
+ if (!canvas_) {
95
96
return ;
97
+ }
96
98
canvas_->save ();
97
99
}
98
100
99
101
void Canvas::saveLayerWithoutBounds (const Paint& paint,
100
102
const PaintData& paint_data) {
101
- if (!canvas_)
103
+ if (!canvas_) {
102
104
return ;
105
+ }
103
106
canvas_->saveLayer (nullptr , paint.paint ());
104
107
}
105
108
@@ -109,51 +112,59 @@ void Canvas::saveLayer(double left,
109
112
double bottom,
110
113
const Paint& paint,
111
114
const PaintData& paint_data) {
112
- if (!canvas_)
115
+ if (!canvas_) {
113
116
return ;
117
+ }
114
118
SkRect bounds = SkRect::MakeLTRB (left, top, right, bottom);
115
119
canvas_->saveLayer (&bounds, paint.paint ());
116
120
}
117
121
118
122
void Canvas::restore () {
119
- if (!canvas_)
123
+ if (!canvas_) {
120
124
return ;
125
+ }
121
126
canvas_->restore ();
122
127
}
123
128
124
129
int Canvas::getSaveCount () {
125
- if (!canvas_)
130
+ if (!canvas_) {
126
131
return 0 ;
132
+ }
127
133
return canvas_->getSaveCount ();
128
134
}
129
135
130
136
void Canvas::translate (double dx, double dy) {
131
- if (!canvas_)
137
+ if (!canvas_) {
132
138
return ;
139
+ }
133
140
canvas_->translate (dx, dy);
134
141
}
135
142
136
143
void Canvas::scale (double sx, double sy) {
137
- if (!canvas_)
144
+ if (!canvas_) {
138
145
return ;
146
+ }
139
147
canvas_->scale (sx, sy);
140
148
}
141
149
142
150
void Canvas::rotate (double radians) {
143
- if (!canvas_)
151
+ if (!canvas_) {
144
152
return ;
153
+ }
145
154
canvas_->rotate (radians * 180.0 / M_PI);
146
155
}
147
156
148
157
void Canvas::skew (double sx, double sy) {
149
- if (!canvas_)
158
+ if (!canvas_) {
150
159
return ;
160
+ }
151
161
canvas_->skew (sx, sy);
152
162
}
153
163
154
164
void Canvas::transform (const tonic::Float64List& matrix4) {
155
- if (!canvas_)
165
+ if (!canvas_) {
156
166
return ;
167
+ }
157
168
canvas_->concat (ToSkMatrix (matrix4));
158
169
}
159
170
@@ -163,31 +174,37 @@ void Canvas::clipRect(double left,
163
174
double bottom,
164
175
SkClipOp clipOp,
165
176
bool doAntiAlias) {
166
- if (!canvas_)
177
+ if (!canvas_) {
167
178
return ;
179
+ }
168
180
canvas_->clipRect (SkRect::MakeLTRB (left, top, right, bottom), clipOp,
169
181
doAntiAlias);
170
182
}
171
183
172
184
void Canvas::clipRRect (const RRect& rrect, bool doAntiAlias) {
173
- if (!canvas_)
185
+ if (!canvas_) {
174
186
return ;
187
+ }
175
188
canvas_->clipRRect (rrect.sk_rrect , doAntiAlias);
176
189
}
177
190
178
191
void Canvas::clipPath (const CanvasPath* path, bool doAntiAlias) {
179
- if (!canvas_)
192
+ if (!canvas_) {
180
193
return ;
181
- if (!path)
194
+ }
195
+ if (!path) {
182
196
Dart_ThrowException (
183
197
ToDart (" Canvas.clipPath called with non-genuine Path." ));
198
+ return ;
199
+ }
184
200
external_allocation_size_ += path->path ().approximateBytesUsed ();
185
201
canvas_->clipPath (path->path (), doAntiAlias);
186
202
}
187
203
188
204
void Canvas::drawColor (SkColor color, SkBlendMode blend_mode) {
189
- if (!canvas_)
205
+ if (!canvas_) {
190
206
return ;
207
+ }
191
208
canvas_->drawColor (color, blend_mode);
192
209
}
193
210
@@ -197,14 +214,16 @@ void Canvas::drawLine(double x1,
197
214
double y2,
198
215
const Paint& paint,
199
216
const PaintData& paint_data) {
200
- if (!canvas_)
217
+ if (!canvas_) {
201
218
return ;
219
+ }
202
220
canvas_->drawLine (x1, y1 , x2, y2, *paint.paint ());
203
221
}
204
222
205
223
void Canvas::drawPaint (const Paint& paint, const PaintData& paint_data) {
206
- if (!canvas_)
224
+ if (!canvas_) {
207
225
return ;
226
+ }
208
227
canvas_->drawPaint (*paint.paint ());
209
228
}
210
229
@@ -214,25 +233,28 @@ void Canvas::drawRect(double left,
214
233
double bottom,
215
234
const Paint& paint,
216
235
const PaintData& paint_data) {
217
- if (!canvas_)
236
+ if (!canvas_) {
218
237
return ;
238
+ }
219
239
canvas_->drawRect (SkRect::MakeLTRB (left, top, right, bottom), *paint.paint ());
220
240
}
221
241
222
242
void Canvas::drawRRect (const RRect& rrect,
223
243
const Paint& paint,
224
244
const PaintData& paint_data) {
225
- if (!canvas_)
245
+ if (!canvas_) {
226
246
return ;
247
+ }
227
248
canvas_->drawRRect (rrect.sk_rrect , *paint.paint ());
228
249
}
229
250
230
251
void Canvas::drawDRRect (const RRect& outer,
231
252
const RRect& inner,
232
253
const Paint& paint,
233
254
const PaintData& paint_data) {
234
- if (!canvas_)
255
+ if (!canvas_) {
235
256
return ;
257
+ }
236
258
canvas_->drawDRRect (outer.sk_rrect , inner.sk_rrect , *paint.paint ());
237
259
}
238
260
@@ -242,8 +264,9 @@ void Canvas::drawOval(double left,
242
264
double bottom,
243
265
const Paint& paint,
244
266
const PaintData& paint_data) {
245
- if (!canvas_)
267
+ if (!canvas_) {
246
268
return ;
269
+ }
247
270
canvas_->drawOval (SkRect::MakeLTRB (left, top, right, bottom), *paint.paint ());
248
271
}
249
272
@@ -252,8 +275,9 @@ void Canvas::drawCircle(double x,
252
275
double radius,
253
276
const Paint& paint,
254
277
const PaintData& paint_data) {
255
- if (!canvas_)
278
+ if (!canvas_) {
256
279
return ;
280
+ }
257
281
canvas_->drawCircle (x, y, radius, *paint.paint ());
258
282
}
259
283
@@ -266,8 +290,9 @@ void Canvas::drawArc(double left,
266
290
bool useCenter,
267
291
const Paint& paint,
268
292
const PaintData& paint_data) {
269
- if (!canvas_)
293
+ if (!canvas_) {
270
294
return ;
295
+ }
271
296
canvas_->drawArc (SkRect::MakeLTRB (left, top, right, bottom),
272
297
startAngle * 180.0 / M_PI, sweepAngle * 180.0 / M_PI,
273
298
useCenter, *paint.paint ());
@@ -276,11 +301,14 @@ void Canvas::drawArc(double left,
276
301
void Canvas::drawPath (const CanvasPath* path,
277
302
const Paint& paint,
278
303
const PaintData& paint_data) {
279
- if (!canvas_)
304
+ if (!canvas_) {
280
305
return ;
281
- if (!path)
306
+ }
307
+ if (!path) {
282
308
Dart_ThrowException (
283
309
ToDart (" Canvas.drawPath called with non-genuine Path." ));
310
+ return ;
311
+ }
284
312
external_allocation_size_ += path->path ().approximateBytesUsed ();
285
313
canvas_->drawPath (path->path (), *paint.paint ());
286
314
}
@@ -290,11 +318,14 @@ void Canvas::drawImage(const CanvasImage* image,
290
318
double y,
291
319
const Paint& paint,
292
320
const PaintData& paint_data) {
293
- if (!canvas_)
321
+ if (!canvas_) {
294
322
return ;
295
- if (!image)
323
+ }
324
+ if (!image) {
296
325
Dart_ThrowException (
297
326
ToDart (" Canvas.drawImage called with non-genuine Image." ));
327
+ return ;
328
+ }
298
329
external_allocation_size_ += image->GetAllocationSize ();
299
330
canvas_->drawImage (image->image (), x, y, paint.paint ());
300
331
}
@@ -310,11 +341,14 @@ void Canvas::drawImageRect(const CanvasImage* image,
310
341
double dst_bottom,
311
342
const Paint& paint,
312
343
const PaintData& paint_data) {
313
- if (!canvas_)
344
+ if (!canvas_) {
314
345
return ;
315
- if (!image)
346
+ }
347
+ if (!image) {
316
348
Dart_ThrowException (
317
349
ToDart (" Canvas.drawImageRect called with non-genuine Image." ));
350
+ return ;
351
+ }
318
352
SkRect src = SkRect::MakeLTRB (src_left, src_top, src_right, src_bottom);
319
353
SkRect dst = SkRect::MakeLTRB (dst_left, dst_top, dst_right, dst_bottom);
320
354
external_allocation_size_ += image->GetAllocationSize ();
@@ -333,11 +367,14 @@ void Canvas::drawImageNine(const CanvasImage* image,
333
367
double dst_bottom,
334
368
const Paint& paint,
335
369
const PaintData& paint_data) {
336
- if (!canvas_)
370
+ if (!canvas_) {
337
371
return ;
338
- if (!image)
372
+ }
373
+ if (!image) {
339
374
Dart_ThrowException (
340
375
ToDart (" Canvas.drawImageNine called with non-genuine Image." ));
376
+ return ;
377
+ }
341
378
SkRect center =
342
379
SkRect::MakeLTRB (center_left, center_top, center_right, center_bottom);
343
380
SkIRect icenter;
@@ -348,11 +385,14 @@ void Canvas::drawImageNine(const CanvasImage* image,
348
385
}
349
386
350
387
void Canvas::drawPicture (Picture* picture) {
351
- if (!canvas_)
388
+ if (!canvas_) {
352
389
return ;
353
- if (!picture)
390
+ }
391
+ if (!picture) {
354
392
Dart_ThrowException (
355
393
ToDart (" Canvas.drawPicture called with non-genuine Picture." ));
394
+ return ;
395
+ }
356
396
external_allocation_size_ += picture->GetAllocationSize ();
357
397
canvas_->drawPicture (picture->picture ().get ());
358
398
}
@@ -361,8 +401,9 @@ void Canvas::drawPoints(const Paint& paint,
361
401
const PaintData& paint_data,
362
402
SkCanvas::PointMode point_mode,
363
403
const tonic::Float32List& points) {
364
- if (!canvas_)
404
+ if (!canvas_) {
365
405
return ;
406
+ }
366
407
367
408
static_assert (sizeof (SkPoint) == sizeof (float ) * 2 ,
368
409
" SkPoint doesn't use floats." );
@@ -377,11 +418,14 @@ void Canvas::drawVertices(const Vertices* vertices,
377
418
SkBlendMode blend_mode,
378
419
const Paint& paint,
379
420
const PaintData& paint_data) {
380
- if (!canvas_)
421
+ if (!canvas_) {
381
422
return ;
382
- if (!vertices)
423
+ }
424
+ if (!vertices) {
383
425
Dart_ThrowException (
384
426
ToDart (" Canvas.drawVertices called with non-genuine Vertices." ));
427
+ return ;
428
+ }
385
429
external_allocation_size_ += vertices->GetAllocationSize ();
386
430
canvas_->drawVertices (vertices->vertices (), blend_mode, *paint.paint ());
387
431
}
@@ -394,12 +438,15 @@ void Canvas::drawAtlas(const Paint& paint,
394
438
const tonic::Int32List& colors,
395
439
SkBlendMode blend_mode,
396
440
const tonic::Float32List& cull_rect) {
397
- if (!canvas_)
441
+ if (!canvas_) {
398
442
return ;
399
- if (!atlas)
443
+ }
444
+ if (!atlas) {
400
445
Dart_ThrowException (
401
446
ToDart (" Canvas.drawAtlas or Canvas.drawRawAtlas called with "
402
447
" non-genuine Image." ));
448
+ return ;
449
+ }
403
450
404
451
sk_sp<SkImage> skImage = atlas->image ();
405
452
@@ -422,9 +469,11 @@ void Canvas::drawShadow(const CanvasPath* path,
422
469
SkColor color,
423
470
double elevation,
424
471
bool transparentOccluder) {
425
- if (!path)
472
+ if (!path) {
426
473
Dart_ThrowException (
427
474
ToDart (" Canvas.drawShader called with non-genuine Path." ));
475
+ return ;
476
+ }
428
477
SkScalar dpr =
429
478
UIDartState::Current ()->window ()->viewport_metrics ().device_pixel_ratio ;
430
479
external_allocation_size_ += path->path ().approximateBytesUsed ();
0 commit comments