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