• 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageFilter.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/effects/SkImageFilters.h"
22 
23 namespace skiagm {
24 
25 // This GM reproduces the issue in crbug.com/472795. The SkImageSource image
26 // is shifted for high quality mode between cpu and gpu.
27 class ImageSourceGM : public GM {
28 public:
ImageSourceGM(const char * suffix,const SkSamplingOptions & sampling)29     ImageSourceGM(const char* suffix, const SkSamplingOptions& sampling)
30         : fSuffix(suffix), fSampling(sampling) {
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, fSampling));
81 
82         canvas->saveLayer(nullptr, &p);
83         canvas->restore();
84     }
85 
86 private:
87     inline static constexpr int kImageSize = 503;
88 
89     SkString          fSuffix;
90     SkSamplingOptions fSampling;
91     sk_sp<SkImage>    fImage;
92 
93     using INHERITED = GM;
94 };
95 
96 //////////////////////////////////////////////////////////////////////////////
97 
98 DEF_GM(return new ImageSourceGM("none", SkSamplingOptions());)
99 DEF_GM(return new ImageSourceGM("low", SkSamplingOptions(SkFilterMode::kLinear));)
100 DEF_GM(return new ImageSourceGM("med", SkSamplingOptions(SkFilterMode::kLinear,
101                                                          SkMipmapMode::kLinear));)
102 DEF_GM(return new ImageSourceGM("high", SkSamplingOptions({1/3.0f, 1/3.0f}));)
103 }  // namespace skiagm
104