robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFilterQuality.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkImageFilter.h" |
| 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
| 19 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/effects/SkImageSource.h" |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 23 | |
| 24 | namespace skiagm { |
| 25 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 26 | // This GM reproduces the issue in crbug.com/472795. The SkImageSource image |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 27 | // is shifted for high quality mode between cpu and gpu. |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 28 | class ImageSourceGM : public GM { |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 29 | public: |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 30 | ImageSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) { |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 31 | this->setBGColor(0xFFFFFFFF); |
| 32 | } |
| 33 | |
| 34 | protected: |
| 35 | SkString onShortName() override { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 36 | SkString name("imagesrc2_"); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 37 | name.append(fSuffix); |
| 38 | return name; |
| 39 | } |
| 40 | |
| 41 | SkISize onISize() override { return SkISize::Make(256, 256); } |
| 42 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 43 | // Create an image with high frequency vertical stripes |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 44 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 45 | constexpr SkPMColor gColors[] = { |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 46 | SK_ColorRED, SK_ColorGRAY, |
| 47 | SK_ColorGREEN, SK_ColorGRAY, |
| 48 | SK_ColorBLUE, SK_ColorGRAY, |
| 49 | SK_ColorCYAN, SK_ColorGRAY, |
| 50 | SK_ColorMAGENTA, SK_ColorGRAY, |
| 51 | SK_ColorYELLOW, SK_ColorGRAY, |
| 52 | SK_ColorWHITE, SK_ColorGRAY, |
| 53 | }; |
| 54 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 55 | auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 56 | SkCanvas* canvas = surface->getCanvas(); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 57 | |
| 58 | int curColor = 0; |
| 59 | |
| 60 | for (int x = 0; x < kImageSize; x += 3) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 61 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0), |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 62 | SkIntToScalar(3), SkIntToScalar(kImageSize)); |
| 63 | SkPaint p; |
| 64 | p.setColor(gColors[curColor]); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 65 | canvas->drawRect(r, p); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 66 | |
| 67 | curColor = (curColor+1) % SK_ARRAY_COUNT(gColors); |
| 68 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 69 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 70 | fImage = surface->makeImageSnapshot(); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 74 | const SkRect srcRect = SkRect::MakeLTRB(0, 0, |
| 75 | SkIntToScalar(kImageSize), |
| 76 | SkIntToScalar(kImageSize)); |
| 77 | const SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 78 | |
| 79 | SkPaint p; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 80 | p.setImageFilter(SkImageSource::Make(fImage, srcRect, dstRect, fFilter)); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 81 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | canvas->saveLayer(nullptr, &p); |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 83 | canvas->restore(); |
| 84 | } |
| 85 | |
| 86 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 87 | static constexpr int kImageSize = 503; |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 88 | |
| 89 | SkString fSuffix; |
| 90 | SkFilterQuality fFilter; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 91 | sk_sp<SkImage> fImage; |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 92 | |
| 93 | typedef GM INHERITED; |
| 94 | }; |
| 95 | |
| 96 | ////////////////////////////////////////////////////////////////////////////// |
| 97 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 98 | DEF_GM(return new ImageSourceGM("none", kNone_SkFilterQuality);) |
| 99 | DEF_GM(return new ImageSourceGM("low", kLow_SkFilterQuality);) |
| 100 | DEF_GM(return new ImageSourceGM("med", kMedium_SkFilterQuality);) |
| 101 | DEF_GM(return new ImageSourceGM("high", kHigh_SkFilterQuality);) |
robertphillips | e275fdf | 2015-04-09 06:47:12 -0700 | [diff] [blame] | 102 | } |