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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkData.h" |
| 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkRRect.h" |
| 13 | #include "include/core/SkShader.h" |
| 14 | #include "include/core/SkSurface.h" |
| 15 | #include "include/effects/SkPerlinNoiseShader.h" |
| 16 | #include "tests/Test.h" |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 20 | SkTileMode expectedX, SkTileMode expectedY, |
Mike Reed | 09bc78b | 2018-10-15 12:39:03 -0400 | [diff] [blame] | 21 | const SkMatrix& expectedM) { |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 22 | SkTileMode tileModes[2]; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 27 | tileModes[0] = tileModes[1] = (SkTileMode)99; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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); |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 44 | const SkMatrix localM = SkMatrix::Scale(2, 3); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 45 | const SkTileMode tmx = SkTileMode::kRepeat; |
| 46 | const SkTileMode tmy = SkTileMode::kMirror; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 47 | |
Mike Reed | 50acf8f | 2019-04-08 13:20:23 -0400 | [diff] [blame] | 48 | auto shader0 = bm.makeShader(tmx, tmy, &localM); |
Mike Reed | 4106819 | 2020-12-09 21:48:52 -0500 | [diff] [blame] | 49 | auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, SkSamplingOptions(), localM); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 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( |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 63 | SkShaders::Blend(SkBlendMode::kClear, |
| 64 | SkShaders::Empty(), |
| 65 | SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f))); |
Herb Derby | e556bf7 | 2017-02-28 11:14:47 -0500 | [diff] [blame] | 66 | SkRRect rr; |
| 67 | SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 68 | rr.setRectRadii({0, 0, 0, 0}, rd); |
| 69 | canvas.drawRRect(rr, p); |
| 70 | } |