• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "SkImage.h"
10 #include "SkImageSource.h"
11 #include "SkSurface.h"
12 
13 namespace skiagm {
14 
15 // This GM reproduces the issue in crbug.com/472795. The SkImageSource image
16 // is shifted for high quality mode between cpu and gpu.
17 class ImageSourceGM : public GM {
18 public:
ImageSourceGM(const char * suffix,SkFilterQuality filter)19     ImageSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) {
20         this->setBGColor(0xFFFFFFFF);
21     }
22 
23 protected:
onShortName()24     SkString onShortName() override {
25         SkString name("imagesrc2_");
26         name.append(fSuffix);
27         return name;
28     }
29 
onISize()30     SkISize onISize() override { return SkISize::Make(256, 256); }
31 
32     // Create an image with high frequency vertical stripes
onOnceBeforeDraw()33     void onOnceBeforeDraw() override {
34         constexpr SkPMColor gColors[] = {
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 
44         auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize));
45         SkCanvas* canvas = surface->getCanvas();
46 
47         int curColor = 0;
48 
49         for (int x = 0; x < kImageSize; x += 3) {
50             SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
51                                         SkIntToScalar(3), SkIntToScalar(kImageSize));
52             SkPaint p;
53             p.setColor(gColors[curColor]);
54             canvas->drawRect(r, p);
55 
56             curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
57         }
58 
59         fImage = surface->makeImageSnapshot();
60     }
61 
onDraw(SkCanvas * canvas)62     void onDraw(SkCanvas* canvas) override {
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);
67 
68         SkPaint p;
69         p.setImageFilter(SkImageSource::Make(fImage, srcRect, dstRect, fFilter));
70 
71         canvas->saveLayer(nullptr, &p);
72         canvas->restore();
73     }
74 
75 private:
76     static constexpr int kImageSize = 503;
77 
78     SkString fSuffix;
79     SkFilterQuality fFilter;
80     sk_sp<SkImage>  fImage;
81 
82     typedef GM INHERITED;
83 };
84 
85 //////////////////////////////////////////////////////////////////////////////
86 
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);)
91 }
92