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.
3#include "tools/fiddle/examples.h"
4REG_FIDDLE(purplestamp, 256, 256, false, 0) {
5// purplestamp
6
7// Whether to use a separate bitmap (with a color type) at all.
8bool drawWithRasterImage = true;
9
10// This works with either kN32_SkColorType or kAlpha_8_SkColorType.
11// Applies only if drawWithRasterImage is true.
12SkColorType colorType = kN32_SkColorType;
13
14void drawStamp(SkCanvas* canvas, int size) {
15    canvas->save();
16    canvas->clipRect(SkRect::MakeWH(size, size), SkClipOp::kIntersect, true);
17
18    canvas->clear(0x3F000000 /* translucent black */);
19
20    SkPaint paint;
21    paint.setAntiAlias(true);
22    paint.setColor(SK_ColorBLACK);
23    canvas->drawRRect(
24            SkRRect::MakeOval(SkRect::MakeXYWH(size / 4, size / 4, size / 2, size / 2)), paint);
25
26    paint.setStyle(SkPaint::kStroke_Style);
27    paint.setStrokeWidth(20);
28    canvas->drawRect(SkRect::MakeWH(size, size), paint);
29
30    canvas->restore();
31}
32
33sk_sp<SkImage> stampImage(int size) {
34    sk_sp<SkSurface> surface = SkSurface::MakeRaster(
35            SkImageInfo::Make(size, size, colorType, kPremul_SkAlphaType));
36    drawStamp(surface->getCanvas(), size);
37    return surface->makeImageSnapshot();
38}
39
40void draw(SkCanvas* canvas) {
41    SkPaint paint;
42    paint.setColorFilter(SkColorFilters::Blend(0xFF7F00FF, SkBlendMode::kSrcIn));
43    paint.setAntiAlias(true);
44
45    SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kLinear);
46
47    canvas->rotate(30);
48    canvas->translate(60, 0);
49
50    int stampSize = 200;
51    if (drawWithRasterImage) {
52        canvas->drawImage(stampImage(stampSize), 0, 0, sampling, &paint);
53    } else {
54        canvas->saveLayer(nullptr, &paint);
55        drawStamp(canvas, stampSize);
56        canvas->restore();
57    }
58}
59}  // END FIDDLE
60