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" |
| 10 | #include "SkImage.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkSurface.h" |
| 13 | #include "SkData.h" |
| 14 | |
| 15 | static void check_isabitmap(skiatest::Reporter* reporter, SkShader* shader, |
| 16 | int expectedW, int expectedH, |
| 17 | SkShader::TileMode expectedX, SkShader::TileMode expectedY, |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 18 | const SkMatrix& expectedM) { |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 19 | SkShader::TileMode tileModes[2]; |
| 20 | SkMatrix localM; |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 21 | |
| 22 | #ifdef SK_SUPPORT_LEGACY_SHADER_ISABITMAP |
| 23 | SkBitmap bm; |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 24 | REPORTER_ASSERT(reporter, shader->isABitmap(&bm, &localM, tileModes)); |
| 25 | REPORTER_ASSERT(reporter, bm.width() == expectedW); |
| 26 | REPORTER_ASSERT(reporter, bm.height() == expectedH); |
| 27 | REPORTER_ASSERT(reporter, localM == expectedM); |
| 28 | REPORTER_ASSERT(reporter, tileModes[0] == expectedX); |
| 29 | REPORTER_ASSERT(reporter, tileModes[1] == expectedY); |
Mike Reed | 627778d | 2016-09-28 17:13:38 -0400 | [diff] [blame] | 30 | #endif |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 31 | |
| 32 | // wack these so we don't get a false positive |
| 33 | localM.setScale(9999, -9999); |
| 34 | tileModes[0] = tileModes[1] = (SkShader::TileMode)99; |
| 35 | |
| 36 | SkImage* image = shader->isAImage(&localM, tileModes); |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 37 | REPORTER_ASSERT(reporter, image); |
| 38 | REPORTER_ASSERT(reporter, image->width() == expectedW); |
| 39 | REPORTER_ASSERT(reporter, image->height() == expectedH); |
| 40 | REPORTER_ASSERT(reporter, localM == expectedM); |
| 41 | REPORTER_ASSERT(reporter, tileModes[0] == expectedX); |
| 42 | REPORTER_ASSERT(reporter, tileModes[1] == expectedY); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DEF_TEST(Shader_isABitmap, reporter) { |
| 46 | const int W = 100; |
| 47 | const int H = 100; |
| 48 | SkBitmap bm; |
| 49 | bm.allocN32Pixels(W, H); |
| 50 | auto img = SkImage::MakeFromBitmap(bm); |
| 51 | const SkMatrix localM = SkMatrix::MakeScale(2, 3); |
| 52 | const SkShader::TileMode tmx = SkShader::kRepeat_TileMode; |
| 53 | const SkShader::TileMode tmy = SkShader::kMirror_TileMode; |
| 54 | |
| 55 | auto shader0 = SkShader::MakeBitmapShader(bm, tmx, tmy, &localM); |
| 56 | auto shader1 = SkImage::MakeFromBitmap(bm)->makeShader(tmx, tmy, &localM); |
| 57 | |
reed | 320a40d | 2016-08-02 06:12:06 -0700 | [diff] [blame] | 58 | check_isabitmap(reporter, shader0.get(), W, H, tmx, tmy, localM); |
| 59 | check_isabitmap(reporter, shader1.get(), W, H, tmx, tmy, localM); |
reed | f1ac182 | 2016-08-01 11:24:14 -0700 | [diff] [blame] | 60 | } |