skia2/docs/examples/purplestamp.cpp
Mike Reed b339d051e0 pass sampling to drawImage
Bug: skia:7650
Change-Id: I0fa4c5f3fb350705f78ee91f4bdfb4edd83d0ed9
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/361360
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2021-01-28 17:56:52 +00:00

60 lines
1.8 KiB
C++

// Copyright 2020 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
REG_FIDDLE(purplestamp, 256, 256, false, 0) {
// purplestamp
// Whether to use a separate bitmap (with a color type) at all.
bool drawWithRasterImage = true;
// This works with either kN32_SkColorType or kAlpha_8_SkColorType.
// Applies only if drawWithRasterImage is true.
SkColorType colorType = kN32_SkColorType;
void drawStamp(SkCanvas* canvas, int size) {
canvas->save();
canvas->clipRect(SkRect::MakeWH(size, size), SkClipOp::kIntersect, true);
canvas->clear(0x3F000000 /* translucent black */);
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorBLACK);
canvas->drawRRect(
SkRRect::MakeOval(SkRect::MakeXYWH(size / 4, size / 4, size / 2, size / 2)), paint);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(20);
canvas->drawRect(SkRect::MakeWH(size, size), paint);
canvas->restore();
}
sk_sp<SkImage> stampImage(int size) {
sk_sp<SkSurface> surface = SkSurface::MakeRaster(
SkImageInfo::Make(size, size, colorType, kPremul_SkAlphaType));
drawStamp(surface->getCanvas(), size);
return surface->makeImageSnapshot();
}
void draw(SkCanvas* canvas) {
SkPaint paint;
paint.setColorFilter(SkColorFilters::Blend(0xFF7F00FF, SkBlendMode::kSrcIn));
paint.setAntiAlias(true);
SkSamplingOptions sampling(SkFilterMode::kLinear, SkMipmapMode::kLinear);
canvas->rotate(30);
canvas->translate(60, 0);
int stampSize = 200;
if (drawWithRasterImage) {
canvas->drawImage(stampImage(stampSize), 0, 0, sampling, &paint);
} else {
canvas->saveLayer(nullptr, &paint);
drawStamp(canvas, stampSize);
canvas->restore();
}
}
} // END FIDDLE