2018-04-04 18:17:30 +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"
|
2020-12-23 15:11:33 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBlendMode.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/SkImage.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2020-06-02 16:55:10 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2018-04-04 18:17:30 +00:00
|
|
|
|
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* rootCanvas) {
|
|
|
|
static constexpr SkScalar kSize = 50;
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(kSize, kSize);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface = ToolUtils::makeSurface(rootCanvas, info);
|
2018-04-04 18:17:30 +00:00
|
|
|
|
|
|
|
SkPaint p;
|
|
|
|
p.setAntiAlias(true);
|
|
|
|
p.setColor(SK_ColorGREEN);
|
|
|
|
|
|
|
|
surface->getCanvas()->drawCircle(kSize / 2, kSize / 2, kSize / 2, p);
|
|
|
|
|
|
|
|
p.setStyle(SkPaint::kStroke_Style);
|
|
|
|
p.setColor(SK_ColorRED);
|
|
|
|
surface->getCanvas()->drawLine(kSize * .25f, kSize * .50f, kSize * .75f, kSize * .50f, p);
|
|
|
|
surface->getCanvas()->drawLine(kSize * .50f, kSize * .25f, kSize * .50f, kSize * .75f, p);
|
|
|
|
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(localmatrixshader_nested, canvas, 450, 1200) {
|
|
|
|
auto image = make_image(canvas);
|
|
|
|
|
|
|
|
using FactoryT = sk_sp<SkShader> (*)(const sk_sp<SkImage>&,
|
|
|
|
const SkMatrix& inner,
|
|
|
|
const SkMatrix& outer);
|
|
|
|
static const FactoryT gFactories[] = {
|
|
|
|
// SkLocalMatrixShader(SkImageShader(inner), outer)
|
|
|
|
[](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
|
2020-12-09 02:58:35 +00:00
|
|
|
return img->makeShader(SkSamplingOptions(), inner)->makeWithLocalMatrix(outer);
|
2018-04-04 18:17:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// SkLocalMatrixShader(SkLocalMatrixShader(SkImageShader(I), inner), outer)
|
|
|
|
[](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
|
2020-12-09 02:58:35 +00:00
|
|
|
return img->makeShader(SkSamplingOptions())->makeWithLocalMatrix(inner)->makeWithLocalMatrix(outer);
|
2018-04-04 18:17:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// SkLocalMatrixShader(SkComposeShader(SkImageShader(inner)), outer)
|
|
|
|
[](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
|
2019-04-09 17:55:36 +00:00
|
|
|
return SkShaders::Blend(SkBlendMode::kSrcOver,
|
|
|
|
SkShaders::Color(SK_ColorTRANSPARENT),
|
2020-12-09 02:58:35 +00:00
|
|
|
img->makeShader(SkSamplingOptions(), inner))
|
2018-04-04 18:17:30 +00:00
|
|
|
->makeWithLocalMatrix(outer);
|
|
|
|
},
|
|
|
|
|
|
|
|
// SkLocalMatrixShader(SkComposeShader(SkLocalMatrixShader(SkImageShader(I), inner)), outer)
|
|
|
|
[](const sk_sp<SkImage>& img, const SkMatrix& inner, const SkMatrix& outer) {
|
2019-04-09 17:55:36 +00:00
|
|
|
return SkShaders::Blend(SkBlendMode::kSrcOver,
|
|
|
|
SkShaders::Color(SK_ColorTRANSPARENT),
|
2020-12-09 02:58:35 +00:00
|
|
|
img->makeShader(SkSamplingOptions())->makeWithLocalMatrix(inner))
|
2018-04-04 18:17:30 +00:00
|
|
|
->makeWithLocalMatrix(outer);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-05-21 16:11:27 +00:00
|
|
|
static const auto inner = SkMatrix::Scale(2, 2),
|
|
|
|
outer = SkMatrix::Translate(20, 20);
|
2018-04-04 18:17:30 +00:00
|
|
|
|
|
|
|
SkPaint border;
|
|
|
|
border.setAntiAlias(true);
|
|
|
|
border.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
auto rect = SkRect::Make(image->bounds());
|
|
|
|
SkAssertResult(SkMatrix::Concat(inner, outer).mapRect(&rect));
|
|
|
|
|
|
|
|
const auto drawColumn = [&]() {
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
for (const auto& f : gFactories) {
|
|
|
|
SkPaint p;
|
|
|
|
p.setShader(f(image, inner, outer));
|
|
|
|
|
|
|
|
canvas->drawRect(rect, p);
|
|
|
|
canvas->drawRect(rect, border);
|
|
|
|
|
|
|
|
canvas->translate(0, rect.height() * 1.5f);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
drawColumn();
|
|
|
|
|
|
|
|
{
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->translate(0, rect.height() * SK_ARRAY_COUNT(gFactories) * 1.5f);
|
|
|
|
drawColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas->translate(rect.width() * 1.5f, 0);
|
|
|
|
canvas->scale(2, 2);
|
|
|
|
drawColumn();
|
|
|
|
}
|
2020-06-02 16:55:10 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(localmatrixshader_persp, canvas, 542, 266) {
|
|
|
|
auto image = GetResourceAsImage("images/yellow_rose.png");
|
|
|
|
|
|
|
|
SkBitmap downsized;
|
|
|
|
downsized.allocPixels(image->imageInfo().makeWH(128, 128));
|
2020-11-23 22:41:35 +00:00
|
|
|
image->scalePixels(downsized.pixmap(), SkSamplingOptions(SkFilterMode::kLinear,
|
|
|
|
SkMipmapMode::kNone));
|
2020-12-23 15:11:33 +00:00
|
|
|
image = downsized.asImage();
|
2020-06-02 16:55:10 +00:00
|
|
|
SkRect imgRect = SkRect::MakeIWH(image->width(), image->height());
|
|
|
|
|
|
|
|
// scale matrix
|
|
|
|
SkMatrix scale = SkMatrix::Scale(1.f / 5.f, 1.f / 5.f);
|
|
|
|
|
|
|
|
// perspective matrix
|
|
|
|
SkPoint src[4];
|
|
|
|
imgRect.toQuad(src);
|
|
|
|
SkPoint dst[4] = {{0, 10.f},
|
|
|
|
{image->width() + 28.f, -100.f},
|
|
|
|
{image->width() - 28.f, image->height() + 100.f},
|
|
|
|
{0.f, image->height() - 10.f}};
|
|
|
|
SkMatrix persp;
|
|
|
|
SkAssertResult(persp.setPolyToPoly(src, dst, 4));
|
|
|
|
|
|
|
|
// combined persp * scale
|
|
|
|
SkMatrix perspScale = SkMatrix::Concat(persp, scale);
|
|
|
|
|
|
|
|
auto draw = [&](sk_sp<SkShader> shader, bool applyPerspToCTM) {
|
|
|
|
canvas->save();
|
|
|
|
canvas->clipRect(imgRect);
|
|
|
|
if (applyPerspToCTM) {
|
|
|
|
canvas->concat(persp);
|
|
|
|
}
|
|
|
|
SkPaint imgShaderPaint;
|
|
|
|
imgShaderPaint.setShader(std::move(shader));
|
|
|
|
canvas->drawPaint(imgShaderPaint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(10.f + image->width(), 0.f); // advance
|
|
|
|
};
|
|
|
|
|
|
|
|
// SkImageShader
|
|
|
|
canvas->save();
|
|
|
|
// 4 variants that all attempt to apply sample at persp * scale w/ an image shader
|
|
|
|
// 1. scale provided to SkImage::makeShader(...) but drawn with persp
|
2020-12-09 02:58:35 +00:00
|
|
|
auto s1 = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkSamplingOptions(), &scale);
|
2020-06-02 16:55:10 +00:00
|
|
|
draw(s1, true);
|
|
|
|
|
|
|
|
// 2. persp provided to SkImage::makeShader, then wrapped in scale makeWithLocalMatrix
|
|
|
|
// These pre-concat, so it ends up as persp * scale.
|
2020-12-09 02:58:35 +00:00
|
|
|
auto s2 = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkSamplingOptions(), &persp)
|
2020-06-02 16:55:10 +00:00
|
|
|
->makeWithLocalMatrix(scale);
|
|
|
|
draw(s2, false);
|
|
|
|
|
|
|
|
// 3. Providing pre-computed persp*scale to SkImage::makeShader()
|
2020-12-09 02:58:35 +00:00
|
|
|
auto s3 = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
|
|
|
|
SkSamplingOptions(), &perspScale);
|
2020-06-02 16:55:10 +00:00
|
|
|
draw(s3, false);
|
|
|
|
|
|
|
|
// 4. Providing pre-computed persp*scale to makeWithLocalMatrix
|
2020-12-09 02:58:35 +00:00
|
|
|
auto s4 = image->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions())
|
2020-06-02 16:55:10 +00:00
|
|
|
->makeWithLocalMatrix(perspScale);
|
|
|
|
draw(s4, false);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(0.f, 10.f + image->height()); // advance to next row
|
|
|
|
|
|
|
|
// SkGradientShader
|
|
|
|
const SkColor kGradColors[] = { SK_ColorBLACK, SK_ColorTRANSPARENT };
|
|
|
|
canvas->save();
|
|
|
|
// 1. scale provided to Make, drawn with persp
|
|
|
|
auto g1 = SkGradientShader::MakeRadial({imgRect.centerX(), imgRect.centerY()},
|
|
|
|
imgRect.width() / 2.f, kGradColors, nullptr, 2,
|
|
|
|
SkTileMode::kRepeat, 0, &scale);
|
|
|
|
draw(g1, true);
|
|
|
|
|
|
|
|
// 2. persp provided to Make, then wrapped with makeWithLocalMatrix (pre-concat as before).
|
|
|
|
auto g2 = SkGradientShader::MakeRadial({imgRect.centerX(), imgRect.centerY()},
|
|
|
|
imgRect.width() / 2.f, kGradColors, nullptr, 2,
|
|
|
|
SkTileMode::kRepeat, 0, &persp)
|
|
|
|
->makeWithLocalMatrix(scale);
|
|
|
|
draw(g2, false);
|
|
|
|
|
|
|
|
// 3. Provide per-computed persp*scale to Make
|
|
|
|
auto g3 = SkGradientShader::MakeRadial({imgRect.centerX(), imgRect.centerY()},
|
|
|
|
imgRect.width() / 2.f, kGradColors, nullptr, 2,
|
|
|
|
SkTileMode::kRepeat, 0, &perspScale);
|
|
|
|
draw(g3, false);
|
|
|
|
|
|
|
|
// 4. Providing pre-computed persp*scale to makeWithLocalMatrix
|
|
|
|
auto g4 = SkGradientShader::MakeRadial({imgRect.centerX(), imgRect.centerY()},
|
|
|
|
imgRect.width() / 2.f, kGradColors, nullptr, 2,
|
|
|
|
SkTileMode::kRepeat)
|
|
|
|
->makeWithLocalMatrix(perspScale);
|
|
|
|
draw(g4, false);
|
|
|
|
canvas->restore();
|
|
|
|
}
|