Flutter Engine
The Flutter Engine
Canvas_saveLayer_4.cpp
Go to the documentation of this file.
1// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4REG_FIDDLE(Canvas_saveLayer_4, 256, 256, false, 3) {
5void draw(SkCanvas* canvas) {
6 SkPaint pRed;
8
9 SkPaint pSolidBlue;
10 pSolidBlue.setColor(SK_ColorBLUE);
11
12 SkPaint pThirtyBlue;
13 pThirtyBlue.setColor(SK_ColorBLUE);
14 pThirtyBlue.setAlphaf(0.3);
15
16 SkPaint alpha;
17 alpha.setAlphaf(0.3);
18
19 // First row: Draw two opaque red rectangles into the 0th layer. Then draw two blue
20 // rectangles overlapping the red, one is solid, the other is 30% transparent.
21 canvas->drawRect(SkRect::MakeLTRB(10, 10, 60, 60), pRed);
22 canvas->drawRect(SkRect::MakeLTRB(150, 10, 200, 60), pRed);
23
24 canvas->drawRect(SkRect::MakeLTRB(30, 10, 80, 60), pSolidBlue);
25 canvas->drawRect(SkRect::MakeLTRB(170, 10, 220, 60), pThirtyBlue);
26
27 // Second row: Draw two opaque red rectangles into the 0th layer. Then save a new layer;
28 // when the 1st layer gets merged onto the 0th layer (i.e. when restore() is called), it will
29 // use the provided paint to do so. In this case, the paint is set to have 30% opacity, but
30 // it could also have things set like blend modes or image filters.
31 canvas->drawRect(SkRect::MakeLTRB(10, 70, 60, 120), pRed);
32 canvas->drawRect(SkRect::MakeLTRB(150, 70, 200, 120), pRed);
33
34 canvas->saveLayer(nullptr, &alpha);
35
36 // In the 1st layer, draw the same blue overlapping rectangles as in the first row. Notice in
37 // the final output, we have two different shades of purple. The layer's alpha made the
38 // opaque blue rectangle transparent, and it made the transparent blue rectangle even more so
39 canvas->drawRect(SkRect::MakeLTRB(30, 70, 80, 120), pSolidBlue);
40 canvas->drawRect(SkRect::MakeLTRB(170, 70, 220, 120), pThirtyBlue);
41
42 canvas->restore();
43
44 // Third row: save the layer first, before drawing the two red rectangle, followed by the
45 // overlapping blue rectangles. Notice that the blue overwrites the red in the same way as
46 // the first row because the alpha of the layer is not applied until the layer is restored.
47 canvas->saveLayer(nullptr, &alpha);
48
49 canvas->drawRect(SkRect::MakeLTRB(10, 130, 60, 180), pRed);
50 canvas->drawRect(SkRect::MakeLTRB(150, 130, 200, 180), pRed);
51
52 canvas->drawRect(SkRect::MakeLTRB(30, 130, 80, 180), pSolidBlue);
53 canvas->drawRect(SkRect::MakeLTRB(170, 130, 220, 180), pThirtyBlue);
54
55 canvas->restore();
56}
57} // END FIDDLE
REG_FIDDLE(Canvas_saveLayer_4, 256, 256, false, 3)
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAlphaf(float a)
Definition: SkPaint.cpp:130
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646