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"
4 REG_FIDDLE(purplestamp, 256, 256, false, 0) {
7 // Whether to use a separate bitmap (with a color type) at all.
8 bool drawWithRasterImage = true;
10 // This works with either kN32_SkColorType or kAlpha_8_SkColorType.
11 // Applies only if drawWithRasterImage is true.
12 SkColorType colorType = kN32_SkColorType;
14 void drawStamp(SkCanvas* canvas, int size) {
16 canvas->clipRect(SkRect::MakeWH(size, size), SkClipOp::kIntersect, true);
18 canvas->clear(0x3F000000 /* translucent black */);
21 paint.setAntiAlias(true);
22 paint.setColor(SK_ColorBLACK);
24 SkRRect::MakeOval(SkRect::MakeXYWH(size / 4, size / 4, size / 2, size / 2)), paint);
26 paint.setStyle(SkPaint::kStroke_Style);
27 paint.setStrokeWidth(20);
28 canvas->drawRect(SkRect::MakeWH(size, size), paint);
33 sk_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();
40 void draw(SkCanvas* canvas) {
42 paint.setColorFilter(SkColorFilters::Blend(0xFF7F00FF, SkBlendMode::kSrcIn));
43 paint.setAntiAlias(true);
45 SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kLinear);
48 canvas->translate(60, 0);
51 if (drawWithRasterImage) {
52 canvas->drawImage(stampImage(stampSize), 0, 0, sampling, &paint);
54 canvas->saveLayer(nullptr, &paint);
55 drawStamp(canvas, stampSize);