reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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" |
Herb Derby | e556bf7 | 2017-02-28 11:14:47 -0500 | [diff] [blame] | 10 | #include "SkCanvas.h" |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 11 | #include "SkImage.h" |
Herb Derby | e556bf7 | 2017-02-28 11:14:47 -0500 | [diff] [blame] | 12 | #include "SkPerlinNoiseShader.h" |
| 13 | #include "SkRRect.h" |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 14 | #include "SkShader.h" |
| 15 | #include "SkSurface.h" |
| 16 | #include "SkData.h" |
| 17 | |
Mike Reed | 09bc78b | 2018-10-15 12:39:03 -0400 | [diff] [blame] | 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) { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 22 | SkShader::TileMode tileModes[2]; |
| 23 | SkMatrix localM; |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 24 | |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 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); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Mike Reed | 09bc78b | 2018-10-15 12:39:03 -0400 | [diff] [blame] | 38 | DEF_TEST(Shader_isAImage, reporter) { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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 | |
Mike Reed | 09bc78b | 2018-10-15 12:39:03 -0400 | [diff] [blame] | 51 | check_isaimage(reporter, shader0.get(), W, H, tmx, tmy, localM); |
| 52 | check_isaimage(reporter, shader1.get(), W, H, tmx, tmy, localM); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 53 | } |
Herb Derby | e556bf7 | 2017-02-28 11:14:47 -0500 | [diff] [blame] | 54 | |
| 55 | // Make sure things are ok with just a single leg. |
| 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 | } |