blob: f097c74f4dc484eb9b9a86859a1082f809e24bab [file] [log] [blame]
Matt Sarett547a7272017-04-12 11:52:47 -04001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkImage.h"
12#include "include/core/SkImageInfo.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/core/SkTileMode.h"
Kevin Lubick8b741882023-10-06 11:41:38 -040020#include "tools/DecodeUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "tools/Resources.h"
22#include "tools/ToolUtils.h"
Matt Sarett547a7272017-04-12 11:52:47 -040023
24static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
25 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
Mike Kleinea3f0142019-03-20 11:12:10 -050026 auto surface(ToolUtils::makeSurface(rootCanvas, info));
Matt Sarett547a7272017-04-12 11:52:47 -040027
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setColor(color);
31 surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
32 return surface->makeImageSnapshot();
33}
34
35DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
36 sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
Mike Reed1f607332020-05-21 12:11:27 -040037 SkMatrix translate = SkMatrix::Translate(100.0f, 0.0f);
Matt Sarett547a7272017-04-12 11:52:47 -040038 SkMatrix rotate;
39 rotate.setRotate(45.0f);
Brian Salomonf87ffc72022-11-02 15:30:32 -040040 sk_sp<SkShader> redImageShader = redImage->makeShader(SkSamplingOptions(), &rotate);
41 sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(translate);
Matt Sarett547a7272017-04-12 11:52:47 -040042
43 // Rotate about the origin will happen first.
44 SkPaint paint;
45 paint.setShader(redLocalMatrixShader);
46 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
47
48 sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
Brian Salomonf87ffc72022-11-02 15:30:32 -040049 sk_sp<SkShader> blueImageShader = blueImage->makeShader(SkSamplingOptions(), &translate);
50 sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(rotate);
Matt Sarett547a7272017-04-12 11:52:47 -040051
52 // Translate will happen first.
53 paint.setShader(blueLocalMatrixShader);
54 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
55
56 canvas->translate(100.0f, 0.0f);
57
58 // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
Mike Reedfae8fce2019-04-03 10:27:45 -040059 SkTileMode mode[2];
Matt Sarett547a7272017-04-12 11:52:47 -040060 SkMatrix matrix;
61 SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode);
Mike Reedb612b6c2020-12-08 21:58:35 -050062 paint.setShader(image->makeShader(mode[0], mode[1], SkSamplingOptions(), &matrix));
Matt Sarett547a7272017-04-12 11:52:47 -040063 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
64 image = blueLocalMatrixShader->isAImage(&matrix, mode);
Mike Reedb612b6c2020-12-08 21:58:35 -050065 paint.setShader(image->makeShader(mode[0], mode[1], SkSamplingOptions(), &matrix));
Matt Sarett547a7272017-04-12 11:52:47 -040066 canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
67}
Brian Osman5b6fa822018-02-14 10:16:32 -050068
69DEF_SIMPLE_GM(localmatriximageshader_filtering, canvas, 256, 256) {
70 // Test that filtering decisions (eg bicubic for upscale) are made correctly when the scale
71 // comes from a local matrix shader.
Kevin Lubick8b741882023-10-06 11:41:38 -040072 auto image = ToolUtils::GetResourceAsImage("images/mandrill_256.png");
Brian Osman5b6fa822018-02-14 10:16:32 -050073 SkPaint p;
Mike Reed1f607332020-05-21 12:11:27 -040074 SkMatrix m = SkMatrix::Scale(2, 2);
Mike Reedf3ac2af2021-02-05 12:55:38 -050075 p.setShader(image->makeShader(SkSamplingOptions(SkCubicResampler::Mitchell()))
76 ->makeWithLocalMatrix(m));
Brian Osman5b6fa822018-02-14 10:16:32 -050077
78 canvas->drawRect(SkRect::MakeXYWH(0, 0, 256, 256), p);
79}