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