2018-01-18 21:06:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkCoverageMode.h"
|
|
|
|
#include "include/core/SkFont.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageFilter.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPictureRecorder.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2019-08-02 19:21:23 +00:00
|
|
|
#include "include/effects/SkImageFilters.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkShaderMaskFilter.h"
|
|
|
|
#include "include/utils/SkTextUtils.h"
|
|
|
|
#include "src/core/SkBlendModePriv.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2018-01-18 21:06:54 +00:00
|
|
|
|
2019-05-01 21:28:53 +00:00
|
|
|
#include <initializer_list>
|
|
|
|
|
2018-01-18 21:06:54 +00:00
|
|
|
static void draw_masked_image(SkCanvas* canvas, const SkImage* image, SkScalar x, SkScalar y,
|
2018-02-05 20:59:23 +00:00
|
|
|
const SkImage* mask, sk_sp<SkMaskFilter> outer, SkBlendMode mode) {
|
2018-01-18 21:06:54 +00:00
|
|
|
SkMatrix matrix = SkMatrix::MakeScale(SkIntToScalar(image->width()) / mask->width(),
|
|
|
|
SkIntToScalar(image->height() / mask->height()));
|
2019-02-11 16:09:15 +00:00
|
|
|
// The geometry of the drawImage is also translated by (x,y) so make the mask filter's
|
|
|
|
// coordinate system align with the rendered rectangle.
|
|
|
|
matrix.postTranslate(x, y);
|
2018-01-18 21:06:54 +00:00
|
|
|
SkPaint paint;
|
2018-01-24 21:34:02 +00:00
|
|
|
auto mf = SkShaderMaskFilter::Make(mask->makeShader(&matrix));
|
|
|
|
if (outer) {
|
2020-03-09 20:33:17 +00:00
|
|
|
mf = SkMaskFilter::MakeCompose(std::move(outer), std::move(mf));
|
2018-01-24 21:34:02 +00:00
|
|
|
}
|
|
|
|
paint.setMaskFilter(mf);
|
2018-02-01 16:24:53 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-02-05 20:59:23 +00:00
|
|
|
paint.setBlendMode(mode);
|
2018-01-18 21:06:54 +00:00
|
|
|
canvas->drawImage(image, x, y, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static sk_sp<SkShader> make_shader(const SkRect& r) {
|
|
|
|
const SkPoint pts[] = {
|
|
|
|
{ r.fLeft, r.fTop }, { r.fRight, r.fBottom },
|
|
|
|
};
|
|
|
|
const SkColor colors[] = { 0, SK_ColorWHITE };
|
2019-04-03 14:27:45 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
|
2018-01-18 21:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(shadermaskfilter_gradient, canvas, 512, 512) {
|
|
|
|
SkRect r = { 0, 0, 100, 150 };
|
|
|
|
auto shader = make_shader(r);
|
|
|
|
auto mf = SkShaderMaskFilter::Make(shader);
|
|
|
|
|
|
|
|
canvas->translate(20, 20);
|
|
|
|
canvas->scale(2, 2);
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setMaskFilter(mf);
|
|
|
|
paint.setColor(SK_ColorRED);
|
2018-02-01 16:24:53 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-01-18 21:06:54 +00:00
|
|
|
canvas->drawOval(r, paint);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DEF_SIMPLE_GM_CAN_FAIL(shadermaskfilter_image, canvas, errorMsg, 560, 370) {
|
2018-01-18 21:06:54 +00:00
|
|
|
canvas->scale(1.25f, 1.25f);
|
|
|
|
|
|
|
|
auto image = GetResourceAsImage("images/mandrill_128.png");
|
|
|
|
auto mask = GetResourceAsImage("images/color_wheel.png");
|
2018-11-26 20:34:12 +00:00
|
|
|
if (!image || !mask) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Could not load images. Did you forget to set the resourcePath?";
|
|
|
|
return skiagm::DrawResult::kFail;
|
2018-11-26 20:34:12 +00:00
|
|
|
}
|
2018-03-14 17:01:17 +00:00
|
|
|
auto blurmf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 5);
|
2018-02-05 20:59:23 +00:00
|
|
|
auto gradmf = SkShaderMaskFilter::Make(make_shader(SkRect::MakeIWH(mask->width(),
|
|
|
|
mask->height())));
|
2018-01-18 21:06:54 +00:00
|
|
|
|
2018-02-05 20:59:23 +00:00
|
|
|
const sk_sp<SkMaskFilter> array[] = { nullptr , blurmf, gradmf };
|
|
|
|
for (SkBlendMode mode : {SkBlendMode::kSrcOver, SkBlendMode::kSrcIn}) {
|
|
|
|
canvas->save();
|
|
|
|
for (sk_sp<SkMaskFilter> mf : array) {
|
|
|
|
draw_masked_image(canvas, image.get(), 10, 10, mask.get(), mf, mode);
|
|
|
|
canvas->translate(image->width() + 20.f, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, image->height() + 20.f);
|
|
|
|
}
|
2019-02-07 23:20:09 +00:00
|
|
|
return skiagm::DrawResult::kOk;
|
2018-01-24 21:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static sk_sp<SkMaskFilter> make_path_mf(const SkPath& path, unsigned alpha) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
|
|
|
|
SkPictureRecorder recorder;
|
|
|
|
recorder.beginRecording(1000, 1000)->drawPath(path, paint);
|
2019-04-03 14:27:45 +00:00
|
|
|
auto shader = recorder.finishRecordingAsPicture()->makeShader(SkTileMode::kClamp,
|
|
|
|
SkTileMode::kClamp);
|
2018-01-24 21:34:02 +00:00
|
|
|
return SkShaderMaskFilter::Make(shader);
|
|
|
|
}
|
|
|
|
|
2018-01-26 16:42:38 +00:00
|
|
|
typedef void (*MakePathsProc)(const SkRect&, SkPath*, SkPath*);
|
|
|
|
|
|
|
|
const char* gCoverageName[] = {
|
|
|
|
"union", "sect", "diff", "rev-diff", "xor"
|
|
|
|
};
|
|
|
|
|
2018-02-05 20:59:23 +00:00
|
|
|
DEF_SIMPLE_GM(combinemaskfilter, canvas, 560, 510) {
|
2018-01-24 21:34:02 +00:00
|
|
|
const SkRect r = { 0, 0, 100, 100 };
|
|
|
|
|
2018-01-26 16:42:38 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
2018-12-24 04:19:14 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSize(20);
|
2018-01-26 16:42:38 +00:00
|
|
|
|
2018-01-24 21:34:02 +00:00
|
|
|
const SkRect r2 = r.makeOutset(1.5f, 1.5f);
|
2018-02-05 20:59:23 +00:00
|
|
|
SkPaint strokePaint;
|
|
|
|
strokePaint.setStyle(SkPaint::kStroke_Style);
|
2018-01-24 21:34:02 +00:00
|
|
|
|
2018-01-26 16:42:38 +00:00
|
|
|
auto proc0 = [](const SkRect& r, SkPath* pathA, SkPath* pathB) {
|
|
|
|
pathA->moveTo(r.fLeft, r.fBottom);
|
|
|
|
pathA->lineTo(r.fRight, r.fTop);
|
|
|
|
pathA->lineTo(r.fRight, r.fBottom);
|
|
|
|
pathB->moveTo(r.fLeft, r.fTop);
|
|
|
|
pathB->lineTo(r.fRight, r.fBottom);
|
|
|
|
pathB->lineTo(r.fLeft, r.fBottom);
|
|
|
|
};
|
|
|
|
auto proc1 = [](const SkRect& r, SkPath* pathA, SkPath* pathB) {
|
|
|
|
pathA->addCircle(r.width()*0.25f, r.height()*0.25f, r.width()*0.5f);
|
|
|
|
pathB->addCircle(r.width()*0.75f, r.height()*0.75f, r.width()*0.5f);
|
|
|
|
};
|
|
|
|
MakePathsProc procs[] = { proc0, proc1 };
|
|
|
|
|
|
|
|
sk_sp<SkMaskFilter> mfA[2], mfB[2];
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
SkPath a, b;
|
|
|
|
procs[i](r, &a, &b);
|
|
|
|
mfA[i] = make_path_mf(a, 1 * 0xFF / 3);
|
|
|
|
mfB[i] = make_path_mf(b, 2 * 0xFF / 3);
|
|
|
|
}
|
2018-01-24 21:34:02 +00:00
|
|
|
|
2018-01-26 16:42:38 +00:00
|
|
|
canvas->translate(10, 10 + 20);
|
2018-01-24 21:34:02 +00:00
|
|
|
canvas->save();
|
2018-01-26 16:42:38 +00:00
|
|
|
for (int i = 0; i < 5; ++i) {
|
2018-12-24 04:19:14 +00:00
|
|
|
SkTextUtils::DrawString(canvas, gCoverageName[i], r.width()*0.5f, -10, font, SkPaint(),
|
2018-10-30 16:53:21 +00:00
|
|
|
SkTextUtils::kCenter_Align);
|
2018-01-26 16:42:38 +00:00
|
|
|
|
2018-02-05 20:59:23 +00:00
|
|
|
SkCoverageMode cmode = static_cast<SkCoverageMode>(i);
|
2018-01-26 16:42:38 +00:00
|
|
|
canvas->save();
|
2018-02-05 20:59:23 +00:00
|
|
|
// esp. on gpu side, its valuable to exercise modes that do and do-not convolve coverage
|
|
|
|
// with alpha. SrcOver and SrcIn have these properties, but also happen to "look" the same
|
|
|
|
// for this test.
|
|
|
|
const SkBlendMode bmodes[] = { SkBlendMode::kSrcOver, SkBlendMode::kSrcIn };
|
|
|
|
SkASSERT( SkBlendMode_SupportsCoverageAsAlpha(bmodes[0])); // test as-alpha
|
|
|
|
SkASSERT(!SkBlendMode_SupportsCoverageAsAlpha(bmodes[1])); // test not-as-alpha
|
|
|
|
for (auto bmode : bmodes) {
|
|
|
|
paint.setBlendMode(bmode);
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeCombine(mfA[j], mfB[j], cmode));
|
|
|
|
canvas->drawRect(r2, strokePaint);
|
|
|
|
canvas->drawRect(r, paint);
|
|
|
|
canvas->translate(0, r.height() + 10);
|
|
|
|
}
|
|
|
|
canvas->translate(0, 40);
|
2018-01-24 21:34:02 +00:00
|
|
|
}
|
2018-01-26 16:42:38 +00:00
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(r.width() + 10, 0);
|
2018-01-24 21:34:02 +00:00
|
|
|
}
|
|
|
|
canvas->restore();
|
2018-01-18 21:06:54 +00:00
|
|
|
}
|