Robert Phillips | 8ced688 | 2016-12-16 11:47:46 -0500 | [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 "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkColorPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 15 | #include "include/core/SkImageInfo.h" |
| 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/core/SkRefCnt.h" |
| 20 | #include "include/core/SkShader.h" |
| 21 | #include "include/core/SkSize.h" |
| 22 | #include "include/core/SkString.h" |
| 23 | #include "include/core/SkTileMode.h" |
Robert Phillips | 8ced688 | 2016-12-16 11:47:46 -0500 | [diff] [blame] | 24 | |
| 25 | static const sk_sp<SkImage> make_image(int firstBlackRow, int lastBlackRow) { |
| 26 | static const int kWidth = 25; |
| 27 | static const int kHeight = 27; |
| 28 | |
| 29 | SkBitmap bm; |
| 30 | bm.allocN32Pixels(kWidth, kHeight); |
| 31 | bm.eraseColor(SK_ColorWHITE); |
| 32 | for (int y = firstBlackRow; y < lastBlackRow; ++y) { |
| 33 | for (int x = 0; x < kWidth; ++x) { |
| 34 | *bm.getAddr32(x, y) = SkPackARGB32(0xFF, 0x0, 0x0, 0x0); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bm.setAlphaType(SkAlphaType::kOpaque_SkAlphaType); |
| 39 | bm.setImmutable(); |
| 40 | |
| 41 | return SkImage::MakeFromBitmap(bm); |
| 42 | } |
| 43 | |
| 44 | // GM to reproduce crbug.com/673261. |
| 45 | class FilterBugGM : public skiagm::GM { |
| 46 | public: |
| 47 | FilterBugGM() { this->setBGColor(SK_ColorRED); } |
| 48 | |
| 49 | protected: |
| 50 | SkString onShortName() override { return SkString("filterbug"); } |
| 51 | |
| 52 | SkISize onISize() override { return SkISize::Make(150, 150); } |
| 53 | |
| 54 | void onOnceBeforeDraw() override { |
| 55 | // The top texture has 5 black rows on top and then 22 white rows on the bottom |
| 56 | fTop = make_image(0, 5); |
| 57 | // The bottom texture has 5 black rows on the bottom and then 22 white rows on the top |
| 58 | fBot = make_image(22, 27); |
| 59 | } |
| 60 | |
| 61 | void onDraw(SkCanvas* canvas) override { |
| 62 | static const SkFilterQuality kFilterQuality = SkFilterQuality::kHigh_SkFilterQuality; |
| 63 | static const bool kDoAA = true; |
| 64 | |
| 65 | { |
| 66 | SkRect r1 = SkRect::MakeXYWH(50.0f, 0.0f, 50.0f, 50.0f); |
| 67 | SkPaint p1; |
| 68 | p1.setAntiAlias(kDoAA); |
| 69 | p1.setFilterQuality(kFilterQuality); |
| 70 | SkMatrix localMat; |
| 71 | localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 0.0f); |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 72 | p1.setShader(fTop->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &localMat)); |
Robert Phillips | 8ced688 | 2016-12-16 11:47:46 -0500 | [diff] [blame] | 73 | |
| 74 | canvas->drawRect(r1, p1); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | SkRect r2 = SkRect::MakeXYWH(50.0f, 50.0f, 50.0f, 36.0f); |
| 79 | |
| 80 | SkPaint p2; |
| 81 | p2.setColor(SK_ColorWHITE); |
| 82 | p2.setAntiAlias(kDoAA); |
| 83 | p2.setFilterQuality(kFilterQuality); |
| 84 | |
| 85 | canvas->drawRect(r2, p2); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | SkRect r3 = SkRect::MakeXYWH(50.0f, 86.0f, 50.0f, 50.0f); |
| 90 | |
| 91 | SkPaint p3; |
| 92 | p3.setAntiAlias(kDoAA); |
| 93 | p3.setFilterQuality(kFilterQuality); |
| 94 | SkMatrix localMat; |
| 95 | localMat.setScaleTranslate(2.0f, 2.0f, 50.0f, 86.0f); |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 96 | p3.setShader(fBot->makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &localMat)); |
Robert Phillips | 8ced688 | 2016-12-16 11:47:46 -0500 | [diff] [blame] | 97 | |
| 98 | canvas->drawRect(r3, p3); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | sk_sp<SkImage> fTop; |
| 104 | sk_sp<SkImage> fBot; |
| 105 | |
| 106 | typedef skiagm::GM INHERITED; |
| 107 | }; |
| 108 | |
| 109 | ////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | DEF_GM(return new FilterBugGM;) |