2017-04-12 15:52:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 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"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFilterQuality.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/SkShader.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-04-12 15:52:47 +00:00
|
|
|
|
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
|
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::makeSurface(rootCanvas, info));
|
2017-04-12 15:52:47 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
paint.setColor(color);
|
|
|
|
surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
|
|
|
|
return surface->makeImageSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
|
|
|
|
sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix translate = SkMatrix::Translate(100.0f, 0.0f);
|
2017-04-12 15:52:47 +00:00
|
|
|
SkMatrix rotate;
|
|
|
|
rotate.setRotate(45.0f);
|
2017-04-28 15:12:19 +00:00
|
|
|
sk_sp<SkShader> redImageShader = redImage->makeShader(&translate);
|
2017-04-12 15:52:47 +00:00
|
|
|
sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);
|
|
|
|
|
|
|
|
// Rotate about the origin will happen first.
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setShader(redLocalMatrixShader);
|
|
|
|
canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
|
|
|
|
|
|
|
|
sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
|
2017-04-28 15:12:19 +00:00
|
|
|
sk_sp<SkShader> blueImageShader = blueImage->makeShader(&rotate);
|
2017-04-12 15:52:47 +00:00
|
|
|
sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate);
|
|
|
|
|
|
|
|
// Translate will happen first.
|
|
|
|
paint.setShader(blueLocalMatrixShader);
|
|
|
|
canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
|
|
|
|
|
|
|
|
canvas->translate(100.0f, 0.0f);
|
|
|
|
|
|
|
|
// Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode mode[2];
|
2017-04-12 15:52:47 +00:00
|
|
|
SkMatrix matrix;
|
|
|
|
SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode);
|
|
|
|
paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
|
|
|
|
canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
|
|
|
|
image = blueLocalMatrixShader->isAImage(&matrix, mode);
|
|
|
|
paint.setShader(image->makeShader(mode[0], mode[1], &matrix));
|
|
|
|
canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
|
|
|
|
}
|
2018-02-14 15:16:32 +00:00
|
|
|
|
|
|
|
DEF_SIMPLE_GM(localmatriximageshader_filtering, canvas, 256, 256) {
|
|
|
|
// Test that filtering decisions (eg bicubic for upscale) are made correctly when the scale
|
|
|
|
// comes from a local matrix shader.
|
|
|
|
auto image = GetResourceAsImage("images/mandrill_256.png");
|
|
|
|
SkPaint p;
|
|
|
|
p.setFilterQuality(kHigh_SkFilterQuality);
|
2020-05-21 16:11:27 +00:00
|
|
|
SkMatrix m = SkMatrix::Scale(2, 2);
|
2018-02-14 15:16:32 +00:00
|
|
|
p.setShader(image->makeShader()->makeWithLocalMatrix(m));
|
|
|
|
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 256, 256), p);
|
|
|
|
}
|