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 "Resources.h"
      9 #include "gm.h"
     10 #include "sk_tool_utils.h"
     11 
     12 static const char* kFilterQualityNames[] = { "none", "low", "medium", "high" };
     13 
     14 struct DownsampleBitmapGM : public skiagm::GM {
     15     SkBitmap      (*fMakeBitmap)(SkImageInfo);
     16     SkString        fName;
     17     SkFilterQuality fFilterQuality;
     18 
     19     DownsampleBitmapGM(SkBitmap (*fn)(SkImageInfo), const char* kind, SkFilterQuality fq)
     20         : fMakeBitmap(fn)
     21         , fName(SkStringPrintf("downsamplebitmap_%s_%s", kind, kFilterQualityNames[fq]))
     22         , fFilterQuality(fq)
     23     {
     24         this->setBGColor(0xFFDDDDDD);
     25     }
     26 
     27     SkString onShortName() override { return fName; }
     28 
     29     SkISize onISize() override {
     30         SkBitmap bm = fMakeBitmap(SkImageInfo::MakeN32Premul(1,1)/*whatever*/);
     31         return SkISize::Make(bm.width(), 4 * bm.height());
     32     }
     33 
     34     void onDraw(SkCanvas* canvas) override {
     35         SkImageInfo info = canvas->imageInfo();
     36         if (!info.colorType()) { info = info.makeColorType(   kN32_SkColorType); }
     37         if (!info.alphaType()) { info = info.makeAlphaType(kPremul_SkAlphaType); }
     38 
     39         SkBitmap bm = fMakeBitmap(info);
     40 
     41         int curY = 0;
     42         int curHeight;
     43         float curScale = 1;
     44         do {
     45 
     46             SkMatrix matrix;
     47             matrix.setScale( curScale, curScale );
     48 
     49             SkPaint paint;
     50             paint.setFilterQuality(fFilterQuality);
     51 
     52             canvas->save();
     53                 canvas->translate(0, (SkScalar)curY);
     54                 canvas->concat(matrix);
     55                 canvas->drawBitmap(bm, 0, 0, &paint);
     56             canvas->restore();
     57 
     58             curHeight = (int) (bm.height() * curScale + 2);
     59             curY += curHeight;
     60             curScale *= 0.75f;
     61         } while (curHeight >= 2 && curY < 4*bm.height());
     62     }
     63 };
     64 
     65 static SkBitmap convert_bitmap_format(SkBitmap src, SkImageInfo info) {
     66     SkBitmap dst;
     67     dst.allocPixels(info.makeWH(src.width(), src.height()));
     68 
     69     SkPixmap pm;
     70     SkAssertResult(dst.peekPixels(&pm));
     71     SkAssertResult(src.readPixels(pm));
     72 
     73     return dst;
     74 }
     75 
     76 
     77 static SkBitmap make_text(SkImageInfo info) {
     78     const SkScalar textSize = 72;
     79 
     80     SkBitmap bm;
     81     bm.allocPixels(info.makeWH(int(textSize * 8), int(textSize * 6)));
     82     SkCanvas canvas(bm);
     83     canvas.drawColor(SK_ColorWHITE);
     84 
     85     SkPaint paint;
     86     SkFont font;
     87     font.setSubpixel(true);
     88     font.setSize(textSize);
     89 
     90     font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle()));
     91     canvas.drawString("Hamburgefons", textSize/2, 1.2f*textSize, font, paint);
     92     font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()));
     93     canvas.drawString("Hamburgefons", textSize/2, 2.4f*textSize, font, paint);
     94     font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()));
     95     canvas.drawString("Hamburgefons", textSize/2, 3.6f*textSize, font, paint);
     96     font.setTypeface(sk_tool_utils::create_portable_typeface("serif", SkFontStyle::BoldItalic()));
     97     canvas.drawString("Hamburgefons", textSize/2, 4.8f*textSize, font, paint);
     98 
     99     return bm;
    100 }
    101 DEF_GM( return new DownsampleBitmapGM(make_text, "text",   kHigh_SkFilterQuality); )
    102 DEF_GM( return new DownsampleBitmapGM(make_text, "text", kMedium_SkFilterQuality); )
    103 DEF_GM( return new DownsampleBitmapGM(make_text, "text",    kLow_SkFilterQuality); )
    104 DEF_GM( return new DownsampleBitmapGM(make_text, "text",   kNone_SkFilterQuality); )
    105 
    106 
    107 static SkBitmap make_checkerboard(SkImageInfo info) {
    108     const auto size      = 512;
    109     const auto numChecks = 256;
    110 
    111     SkBitmap bm;
    112     bm.allocN32Pixels(size,size);
    113     for (int y = 0; y < size; ++y) {
    114         for (int x = 0; x < size; ++x) {
    115             SkPMColor* s = bm.getAddr32(x, y);
    116             int cx = (x * numChecks) / size;
    117             int cy = (y * numChecks) / size;
    118             if ((cx+cy)%2) {
    119                 *s = 0xFFFFFFFF;
    120             } else {
    121                 *s = 0xFF000000;
    122             }
    123         }
    124     }
    125     return convert_bitmap_format(bm, info);
    126 }
    127 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard",   kHigh_SkFilterQuality); )
    128 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard", kMedium_SkFilterQuality); )
    129 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard",    kLow_SkFilterQuality); )
    130 DEF_GM( return new DownsampleBitmapGM(make_checkerboard, "checkerboard",   kNone_SkFilterQuality); )
    131 
    132 
    133 static SkBitmap make_image(SkImageInfo info) {
    134     SkBitmap bm;
    135     if (!GetResourceAsBitmap("images/mandrill_512.png", &bm)) {
    136         bm.allocN32Pixels(1, 1);
    137         bm.eraseARGB(255, 255, 0 , 0); // red == bad
    138     }
    139     return convert_bitmap_format(bm, info);
    140 }
    141 DEF_GM( return new DownsampleBitmapGM(make_image, "image",   kHigh_SkFilterQuality); )
    142 DEF_GM( return new DownsampleBitmapGM(make_image, "image", kMedium_SkFilterQuality); )
    143 DEF_GM( return new DownsampleBitmapGM(make_image, "image",    kLow_SkFilterQuality); )
    144 DEF_GM( return new DownsampleBitmapGM(make_image, "image",   kNone_SkFilterQuality); )
    145