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 | |
| 18 | static void check_isabitmap(skiatest::Reporter* reporter, SkShader* shader, |
| 19 | int expectedW, int expectedH, |
| 20 | SkShader::TileMode expectedX, SkShader::TileMode expectedY, |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 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 | |
| 25 | #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP |
| 26 | SkBitmap bm; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 27 | REPORTER_ASSERT(reporter, shader->isABitmap(&bm, &localM, tileModes)); |
| 28 | REPORTER_ASSERT(reporter, bm.width() == expectedW); |
| 29 | REPORTER_ASSERT(reporter, bm.height() == expectedH); |
| 30 | REPORTER_ASSERT(reporter, localM == expectedM); |
| 31 | REPORTER_ASSERT(reporter, tileModes[0] == expectedX); |
| 32 | REPORTER_ASSERT(reporter, tileModes[1] == expectedY); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 33 | #endif |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 34 | |
| 35 | // wack these so we don't get a false positive |
| 36 | localM.setScale(9999, -9999); |
| 37 | tileModes[0] = tileModes[1] = (SkShader::TileMode)99; |
| 38 | |
| 39 | SkImage* image = shader->isAImage(&localM, tileModes); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 40 | REPORTER_ASSERT(reporter, image); |
| 41 | REPORTER_ASSERT(reporter, image->width() == expectedW); |
| 42 | REPORTER_ASSERT(reporter, image->height() == expectedH); |
| 43 | REPORTER_ASSERT(reporter, localM == expectedM); |
| 44 | REPORTER_ASSERT(reporter, tileModes[0] == expectedX); |
| 45 | REPORTER_ASSERT(reporter, tileModes[1] == expectedY); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DEF_TEST(Shader_isABitmap, reporter) { |
| 49 | const int W = 100; |
| 50 | const int H = 100; |
| 51 | SkBitmap bm; |
| 52 | bm.allocN32Pixels(W, H); |
| 53 | auto img = SkImage::MakeFromBitmap(bm); |
| 54 | const SkMatrix localM = SkMatrix::MakeScale(2, 3); |
| 55 | const SkShader::TileMode tmx = SkShader::kRepeat_TileMode; |
| 56 | const SkShader::TileMode tmy = SkShader::kMirror_TileMode; |
| 57 | |
| 58 | auto shader0 = SkShader::MakeBitmapShader(bm, tmx, tmy, &localM); |
| 59 | auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, &localM); |
| 60 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 61 | check_isabitmap(reporter, shader0.get(), W, H, tmx, tmy, localM); |
| 62 | check_isabitmap(reporter, shader1.get(), W, H, tmx, tmy, localM); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 63 | } |
Herb Derby | e556bf7 | 2017-02-28 11:14:47 -0500 | [diff] [blame^] | 64 | |
| 65 | // Make sure things are ok with just a single leg. |
| 66 | DEF_TEST(ComposeShaderSingle, reporter) { |
| 67 | SkBitmap srcBitmap; |
| 68 | srcBitmap.allocN32Pixels(10, 10); |
| 69 | srcBitmap.eraseColor(SK_ColorRED); |
| 70 | SkCanvas canvas(srcBitmap); |
| 71 | SkPaint p; |
| 72 | p.setShader( |
| 73 | SkShader::MakeComposeShader( |
| 74 | SkShader::MakeEmptyShader(), |
| 75 | SkPerlinNoiseShader::MakeFractalNoise(1.0f, 1.0f, 2, 0.0f), |
| 76 | SkBlendMode::kClear)); |
| 77 | SkRRect rr; |
| 78 | SkVector rd[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}}; |
| 79 | rr.setRectRadii({0, 0, 0, 0}, rd); |
| 80 | canvas.drawRRect(rr, p); |
| 81 | } |