Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2011 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 "SkCanvas.h"
     10 #include "SkColorPriv.h"
     11 #include "SkShader.h"
     12 
     13 /*
     14  *  Want to ensure that our bitmap sampler (in bitmap shader) keeps plenty of
     15  *  precision when scaling very large images (where the dx might get very small.
     16  */
     17 
     18 #define W   257
     19 #define H   161
     20 
     21 class GiantBitmapGM : public skiagm::GM {
     22     SkBitmap* fBM;
     23     SkShader::TileMode fMode;
     24     bool fDoFilter;
     25     bool fDoRotate;
     26 
     27     const SkBitmap& getBitmap() {
     28         if (nullptr == fBM) {
     29             fBM = new SkBitmap;
     30             fBM->allocN32Pixels(W, H);
     31             fBM->eraseColor(SK_ColorWHITE);
     32 
     33             const SkColor colors[] = {
     34                 SK_ColorBLUE, SK_ColorRED, SK_ColorBLACK, SK_ColorGREEN
     35             };
     36 
     37             SkCanvas canvas(*fBM);
     38             SkPaint paint;
     39             paint.setAntiAlias(true);
     40             paint.setStrokeWidth(SkIntToScalar(20));
     41 
     42 #if 0
     43             for (int y = -H*2; y < H; y += 50) {
     44                 SkScalar yy = SkIntToScalar(y);
     45                 paint.setColor(colors[y/50 & 0x3]);
     46                 canvas.drawLine(0, yy, SkIntToScalar(W), yy + SkIntToScalar(W),
     47                                 paint);
     48             }
     49 #else
     50             for (int x = -W; x < W; x += 60) {
     51                 paint.setColor(colors[x/60 & 0x3]);
     52 
     53                 SkScalar xx = SkIntToScalar(x);
     54                 canvas.drawLine(xx, 0, xx, SkIntToScalar(H),
     55                                 paint);
     56             }
     57 #endif
     58         }
     59         return *fBM;
     60     }
     61 
     62 public:
     63     GiantBitmapGM(SkShader::TileMode mode, bool doFilter, bool doRotate) : fBM(nullptr) {
     64         fMode = mode;
     65         fDoFilter = doFilter;
     66         fDoRotate = doRotate;
     67     }
     68 
     69     ~GiantBitmapGM() override { delete fBM; }
     70 
     71 protected:
     72 
     73     SkString onShortName() override {
     74         SkString str("giantbitmap_");
     75         switch (fMode) {
     76             case SkShader::kClamp_TileMode:
     77                 str.append("clamp");
     78                 break;
     79             case SkShader::kRepeat_TileMode:
     80                 str.append("repeat");
     81                 break;
     82             case SkShader::kMirror_TileMode:
     83                 str.append("mirror");
     84                 break;
     85             default:
     86                 break;
     87         }
     88         str.append(fDoFilter ? "_bilerp" : "_point");
     89         str.append(fDoRotate ? "_rotate" : "_scale");
     90         return str;
     91     }
     92 
     93     SkISize onISize() override { return SkISize::Make(640, 480); }
     94 
     95     void onDraw(SkCanvas* canvas) override {
     96         SkPaint paint;
     97 
     98         SkMatrix m;
     99         if (fDoRotate) {
    100 //            m.setRotate(SkIntToScalar(30), 0, 0);
    101             m.setSkew(SK_Scalar1, 0, 0, 0);
    102 //            m.postScale(2*SK_Scalar1/3, 2*SK_Scalar1/3);
    103         } else {
    104             SkScalar scale = 11*SK_Scalar1/12;
    105             m.setScale(scale, scale);
    106         }
    107         paint.setShader(SkShader::MakeBitmapShader(getBitmap(), fMode, fMode, &m));
    108         paint.setFilterQuality(fDoFilter ? kLow_SkFilterQuality : kNone_SkFilterQuality);
    109 
    110         canvas->translate(SkIntToScalar(50), SkIntToScalar(50));
    111 
    112 //        SkRect r = SkRect::MakeXYWH(-50, -50, 32, 16);
    113 //        canvas->drawRect(r, paint); return;
    114         canvas->drawPaint(paint);
    115     }
    116 
    117 private:
    118     typedef GM INHERITED;
    119 };
    120 
    121 ///////////////////////////////////////////////////////////////////////////////
    122 
    123 DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, false); )
    124 DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, false); )
    125 DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, false); )
    126 DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, false); )
    127 DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, false); )
    128 DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, false); )
    129 
    130 DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, false, true); )
    131 DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, false, true); )
    132 DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, false, true); )
    133 DEF_GM( return new GiantBitmapGM(SkShader::kClamp_TileMode, true, true); )
    134 DEF_GM( return new GiantBitmapGM(SkShader::kRepeat_TileMode, true, true); )
    135 DEF_GM( return new GiantBitmapGM(SkShader::kMirror_TileMode, true, true); )
    136