• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #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"
18 #include "include/core/SkSurface.h"
19 #include "include/core/SkTileMode.h"
20 #include "tools/Resources.h"
21 #include "tools/ToolUtils.h"
22 
make_image(SkCanvas * rootCanvas,SkColor color)23 static sk_sp<SkImage> make_image(SkCanvas* rootCanvas, SkColor color) {
24     SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
25     auto        surface(ToolUtils::makeSurface(rootCanvas, info));
26 
27     SkPaint paint;
28     paint.setAntiAlias(true);
29     paint.setColor(color);
30     surface->getCanvas()->drawIRect(SkIRect::MakeXYWH(25, 25, 50, 50), paint);
31     return surface->makeImageSnapshot();
32 }
33 
34 DEF_SIMPLE_GM(localmatriximageshader, canvas, 250, 250) {
35     sk_sp<SkImage> redImage = make_image(canvas, SK_ColorRED);
36     SkMatrix translate = SkMatrix::Translate(100.0f, 0.0f);
37     SkMatrix rotate;
38     rotate.setRotate(45.0f);
39     sk_sp<SkShader> redImageShader = redImage->makeShader(SkSamplingOptions(), &translate);
40     sk_sp<SkShader> redLocalMatrixShader = redImageShader->makeWithLocalMatrix(rotate);
41 
42     // Rotate about the origin will happen first.
43     SkPaint paint;
44     paint.setShader(redLocalMatrixShader);
45     canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
46 
47     sk_sp<SkImage> blueImage = make_image(canvas, SK_ColorBLUE);
48     sk_sp<SkShader> blueImageShader = blueImage->makeShader(SkSamplingOptions(), &rotate);
49     sk_sp<SkShader> blueLocalMatrixShader = blueImageShader->makeWithLocalMatrix(translate);
50 
51     // Translate will happen first.
52     paint.setShader(blueLocalMatrixShader);
53     canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
54 
55     canvas->translate(100.0f, 0.0f);
56 
57     // Use isAImage() and confirm that the shaders will draw exactly the same (to the right by 100).
58     SkTileMode mode[2];
59     SkMatrix matrix;
60     SkImage* image = redLocalMatrixShader->isAImage(&matrix, mode);
61     paint.setShader(image->makeShader(mode[0], mode[1], SkSamplingOptions(), &matrix));
62     canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
63     image = blueLocalMatrixShader->isAImage(&matrix, mode);
64     paint.setShader(image->makeShader(mode[0], mode[1], SkSamplingOptions(), &matrix));
65     canvas->drawIRect(SkIRect::MakeWH(250, 250), paint);
66 }
67 
68 DEF_SIMPLE_GM(localmatriximageshader_filtering, canvas, 256, 256) {
69     // Test that filtering decisions (eg bicubic for upscale) are made correctly when the scale
70     // comes from a local matrix shader.
71     auto image = GetResourceAsImage("images/mandrill_256.png");
72     SkPaint p;
73     SkMatrix m = SkMatrix::Scale(2, 2);
74     p.setShader(image->makeShader(SkSamplingOptions(SkCubicResampler::Mitchell()))
75                 ->makeWithLocalMatrix(m));
76 
77     canvas->drawRect(SkRect::MakeXYWH(0, 0, 256, 256), p);
78 }
79