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