1 /*
2  * Copyright 2016 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 "include/core/SkBitmap.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSamplingOptions.h"
19 #include "include/core/SkShader.h"
20 #include "include/core/SkTileMode.h"
21 #include "include/effects/SkPerlinNoiseShader.h"
22 #include "tests/Test.h"
23 
check_isaimage(skiatest::Reporter * reporter,SkShader * shader,int expectedW,int expectedH,SkTileMode expectedX,SkTileMode expectedY,const SkMatrix & expectedM)24 static void check_isaimage(skiatest::Reporter* reporter, SkShader* shader,
25                            int expectedW, int expectedH,
26                            SkTileMode expectedX, SkTileMode expectedY,
27                            const SkMatrix& expectedM) {
28     SkTileMode tileModes[2];
29     SkMatrix localM;
30 
31     // wack these so we don't get a false positive
32     localM.setScale(9999, -9999);
33     tileModes[0] = tileModes[1] = (SkTileMode)99;
34 
35     SkImage* image = shader->isAImage(&localM, tileModes);
36     REPORTER_ASSERT(reporter, image);
37     REPORTER_ASSERT(reporter, image->width() == expectedW);
38     REPORTER_ASSERT(reporter, image->height() == expectedH);
39     REPORTER_ASSERT(reporter, localM == expectedM);
40     REPORTER_ASSERT(reporter, tileModes[0] == expectedX);
41     REPORTER_ASSERT(reporter, tileModes[1] == expectedY);
42 }
43 
DEF_TEST(Shader_isAImage,reporter)44 DEF_TEST(Shader_isAImage, reporter) {
45     const int W = 100;
46     const int H = 100;
47     SkBitmap bm;
48     bm.allocN32Pixels(W, H);
49     auto img = bm.asImage();
50     const SkMatrix localM = SkMatrix::Scale(2, 3);
51     const SkTileMode tmx = SkTileMode::kRepeat;
52     const SkTileMode tmy = SkTileMode::kMirror;
53 
54     auto shader0 = bm.makeShader(tmx, tmy, SkSamplingOptions(), localM);
55     auto shader1 = bm.asImage()->makeShader(tmx, tmy, SkSamplingOptions(), localM);
56 
57     check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM);
58     check_isaimage(reporter, shader1.get(), W, H, tmx, tmy, localM);
59 }
60 
61 // Make sure things are ok with just a single leg.
DEF_TEST(ComposeShaderSingle,reporter)62 DEF_TEST(ComposeShaderSingle, reporter) {
63     SkBitmap srcBitmap;
64     srcBitmap.allocN32Pixels(10, 10);
65     srcBitmap.eraseColor(SK_ColorRED);
66     SkCanvas canvas(srcBitmap);
67     SkPaint p;
68     p.setShader(
69         SkShaders::Blend(SkBlendMode::kClear,
70         SkShaders::Empty(),
71         SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f)));
72     SkRRect rr;
73     SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
74     rr.setRectRadii({0, 0, 0, 0}, rd);
75     canvas.drawRRect(rr, p);
76 }
77