Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 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 
     10 #include "Resources.h"
     11 #include "SkGradientShader.h"
     12 #include "SkTypeface.h"
     13 #include "SkImageDecoder.h"
     14 #include "SkStream.h"
     15 #include "SkPaint.h"
     16 
     17 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
     18     sk_tool_utils::set_portable_typeface(paint, name, style);
     19 }
     20 
     21 class DownsampleBitmapGM : public skiagm::GM {
     22 public:
     23     SkBitmap    fBM;
     24     SkString    fName;
     25     bool        fBitmapMade;
     26     SkPaint::FilterLevel fFilterLevel;
     27 
     28     DownsampleBitmapGM(SkPaint::FilterLevel filterLevel)
     29         : fFilterLevel(filterLevel)
     30     {
     31         this->setBGColor(0xFFDDDDDD);
     32         fBitmapMade = false;
     33     }
     34 
     35     const char* filterLevelToString() {
     36         static const char *filterLevelNames[] = {
     37             "none", "low", "medium", "high"
     38         };
     39         return filterLevelNames[fFilterLevel];
     40     }
     41 
     42 protected:
     43     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     44         return kSkipTiled_Flag;
     45     }
     46 
     47     virtual SkString onShortName() SK_OVERRIDE {
     48         return fName;
     49     }
     50 
     51     virtual SkISize onISize() SK_OVERRIDE {
     52         make_bitmap_wrapper();
     53         return SkISize::Make(fBM.width(), 4 * fBM.height());
     54     }
     55 
     56     void make_bitmap_wrapper() {
     57         if (!fBitmapMade) {
     58             fBitmapMade = true;
     59             make_bitmap();
     60         }
     61     }
     62 
     63     virtual void make_bitmap() = 0;
     64 
     65     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     66         make_bitmap_wrapper();
     67 
     68         int curY = 0;
     69         int curHeight;
     70         float curScale = 1;
     71         do {
     72 
     73             SkMatrix matrix;
     74             matrix.setScale( curScale, curScale );
     75 
     76             SkPaint paint;
     77             paint.setFilterLevel(fFilterLevel);
     78 
     79             canvas->save();
     80             canvas->translate(0, (SkScalar)curY);
     81             canvas->drawBitmapMatrix( fBM, matrix, &paint );
     82             canvas->restore();
     83 
     84             curHeight = (int) (fBM.height() * curScale + 2);
     85             curY += curHeight;
     86             curScale *= 0.75f;
     87         } while (curHeight >= 2 && curY < 4*fBM.height());
     88     }
     89 
     90 private:
     91     typedef skiagm::GM INHERITED;
     92 };
     93 
     94 class DownsampleBitmapTextGM: public DownsampleBitmapGM {
     95   public:
     96       DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel)
     97       : INHERITED(filterLevel), fTextSize(textSize)
     98         {
     99             fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize);
    100         }
    101 
    102   protected:
    103       float fTextSize;
    104 
    105       virtual void make_bitmap() SK_OVERRIDE {
    106           fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6));
    107           SkCanvas canvas(fBM);
    108           canvas.drawColor(SK_ColorWHITE);
    109 
    110           SkPaint paint;
    111           paint.setAntiAlias(true);
    112           paint.setSubpixelText(true);
    113           paint.setTextSize(fTextSize);
    114 
    115           setTypeface(&paint, "Times", SkTypeface::kNormal);
    116           canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint);
    117           setTypeface(&paint, "Times", SkTypeface::kBold);
    118           canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint);
    119           setTypeface(&paint, "Times", SkTypeface::kItalic);
    120           canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint);
    121           setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
    122           canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint);
    123       }
    124   private:
    125       typedef DownsampleBitmapGM INHERITED;
    126 };
    127 
    128 class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
    129   public:
    130       DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel)
    131       : INHERITED(filterLevel), fSize(size), fNumChecks(numChecks)
    132         {
    133             fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks);
    134         }
    135 
    136   protected:
    137       int fSize;
    138       int fNumChecks;
    139 
    140       virtual void make_bitmap() SK_OVERRIDE {
    141           fBM.allocN32Pixels(fSize, fSize);
    142           for (int y = 0; y < fSize; ++y) {
    143               for (int x = 0; x < fSize; ++x) {
    144                   SkPMColor* s = fBM.getAddr32(x, y);
    145                   int cx = (x * fNumChecks) / fSize;
    146                   int cy = (y * fNumChecks) / fSize;
    147                   if ((cx+cy)%2) {
    148                       *s = 0xFFFFFFFF;
    149                   } else {
    150                       *s = 0xFF000000;
    151                   }
    152               }
    153           }
    154       }
    155   private:
    156       typedef DownsampleBitmapGM INHERITED;
    157 };
    158 
    159 class DownsampleBitmapImageGM: public DownsampleBitmapGM {
    160   public:
    161       DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel)
    162       : INHERITED(filterLevel), fFilename(filename)
    163         {
    164             fName.printf("downsamplebitmap_image_%s_%s", this->filterLevelToString(), filename);
    165         }
    166 
    167   protected:
    168       SkString fFilename;
    169       int fSize;
    170 
    171       virtual void make_bitmap() SK_OVERRIDE {
    172           SkImageDecoder* codec = NULL;
    173           SkString resourcePath = GetResourcePath(fFilename.c_str());
    174           SkFILEStream stream(resourcePath.c_str());
    175           if (stream.isValid()) {
    176               codec = SkImageDecoder::Factory(&stream);
    177           }
    178           if (codec) {
    179               stream.rewind();
    180               codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
    181               SkDELETE(codec);
    182           } else {
    183               fBM.allocN32Pixels(1, 1);
    184               *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
    185           }
    186           fSize = fBM.height();
    187       }
    188   private:
    189       typedef DownsampleBitmapGM INHERITED;
    190 };
    191 
    192 //////////////////////////////////////////////////////////////////////////////
    193 
    194 DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); )
    195 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); )
    196 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); )
    197 
    198 DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); )
    199 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); )
    200 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); )
    201 
    202 DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); )
    203 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); )
    204 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); )
    205 
    206 DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); )
    207 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); )
    208 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); )
    209