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/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColor.h" 11 #include "include/core/SkFilterQuality.h" 12 #include "include/core/SkImage.h" 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" 20 #include "include/core/SkSurface.h" 21 #include "include/core/SkTypes.h" 22 #include "include/effects/SkImageFilters.h" 23 24 namespace skiagm { 25 26 // This GM reproduces the issue in crbug.com/472795. The SkImageSource image 27 // is shifted for high quality mode between cpu and gpu. 28 class ImageSourceGM : public GM { 29 public: ImageSourceGM(const char * suffix,SkFilterQuality filter)30 ImageSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) { 31 this->setBGColor(0xFFFFFFFF); 32 } 33 34 protected: onShortName()35 SkString onShortName() override { 36 SkString name("imagesrc2_"); 37 name.append(fSuffix); 38 return name; 39 } 40 onISize()41 SkISize onISize() override { return SkISize::Make(256, 256); } 42 43 // Create an image with high frequency vertical stripes onOnceBeforeDraw()44 void onOnceBeforeDraw() override { 45 constexpr SkPMColor gColors[] = { 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 55 auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize)); 56 SkCanvas* canvas = surface->getCanvas(); 57 58 int curColor = 0; 59 60 for (int x = 0; x < kImageSize; x += 3) { 61 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0), 62 SkIntToScalar(3), SkIntToScalar(kImageSize)); 63 SkPaint p; 64 p.setColor(gColors[curColor]); 65 canvas->drawRect(r, p); 66 67 curColor = (curColor+1) % SK_ARRAY_COUNT(gColors); 68 } 69 70 fImage = surface->makeImageSnapshot(); 71 } 72 onDraw(SkCanvas * canvas)73 void onDraw(SkCanvas* canvas) override { 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); 78 79 SkPaint p; 80 p.setImageFilter(SkImageFilters::Image(fImage, srcRect, dstRect, fFilter)); 81 82 canvas->saveLayer(nullptr, &p); 83 canvas->restore(); 84 } 85 86 private: 87 static constexpr int kImageSize = 503; 88 89 SkString fSuffix; 90 SkFilterQuality fFilter; 91 sk_sp<SkImage> fImage; 92 93 typedef GM INHERITED; 94 }; 95 96 ////////////////////////////////////////////////////////////////////////////// 97 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);) 102 } 103