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 "SkBitmapSource.h"
10 
11 namespace skiagm {
12 
13 // This GM reproduces the issue in crbug.com/472795. The SkBitmapSource image
14 // is shifted for high quality mode between cpu and gpu.
15 class BitmapSourceGM : public GM {
16 public:
BitmapSourceGM(const char * suffix,SkFilterQuality filter)17     BitmapSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) {
18         this->setBGColor(0xFFFFFFFF);
19     }
20 
21 protected:
onShortName()22     SkString onShortName() override {
23         SkString name("bitmapsrc2_");
24         name.append(fSuffix);
25         return name;
26     }
27 
onISize()28     SkISize onISize() override { return SkISize::Make(256, 256); }
29 
30     // Create a bitmap with high frequency vertical stripes
onOnceBeforeDraw()31     void onOnceBeforeDraw() override {
32         static const SkPMColor gColors[] = {
33             SK_ColorRED,     SK_ColorGRAY,
34             SK_ColorGREEN,   SK_ColorGRAY,
35             SK_ColorBLUE,    SK_ColorGRAY,
36             SK_ColorCYAN,    SK_ColorGRAY,
37             SK_ColorMAGENTA, SK_ColorGRAY,
38             SK_ColorYELLOW,  SK_ColorGRAY,
39             SK_ColorWHITE,   SK_ColorGRAY,
40         };
41 
42         fBM.allocN32Pixels(kImageSize, kImageSize, true);
43 
44         SkCanvas canvas(fBM);
45 
46         int curColor = 0;
47 
48         for (int x = 0; x < kImageSize; x += 3) {
49             SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
50                                         SkIntToScalar(3), SkIntToScalar(kImageSize));
51             SkPaint p;
52             p.setColor(gColors[curColor]);
53             canvas.drawRect(r, p);
54 
55             curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
56         }
57     }
58 
onDraw(SkCanvas * canvas)59     void onDraw(SkCanvas* canvas) override {
60         SkRect srcRect = SkRect::MakeLTRB(0, 0,
61                                           SkIntToScalar(kImageSize), SkIntToScalar(kImageSize));
62         SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f);
63 
64         SkAutoTUnref<SkImageFilter> filter(SkBitmapSource::Create(fBM, srcRect, dstRect, fFilter));
65 
66         SkPaint p;
67         p.setImageFilter(filter);
68 
69         canvas->saveLayer(NULL, &p);
70         canvas->restore();
71     }
72 
73 private:
74     static const int kImageSize = 503;
75 
76     SkString fSuffix;
77     SkFilterQuality fFilter;
78     SkBitmap fBM;
79 
80     typedef GM INHERITED;
81 };
82 
83 //////////////////////////////////////////////////////////////////////////////
84 
85 DEF_GM( return SkNEW_ARGS(BitmapSourceGM, ("none", kNone_SkFilterQuality) );   )
86 DEF_GM( return SkNEW_ARGS(BitmapSourceGM, ("low",  kLow_SkFilterQuality) );    )
87 DEF_GM( return SkNEW_ARGS(BitmapSourceGM, ("med",  kMedium_SkFilterQuality) ); )
88 DEF_GM( return SkNEW_ARGS(BitmapSourceGM, ("high", kHigh_SkFilterQuality) );   )
89 
90 }
91