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