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 "sk_tool_utils.h"
     10 
     11 #include "Resources.h"
     12 #include "SkBitmapProcState.h"
     13 #include "SkGradientShader.h"
     14 #include "SkImageEncoder.h"
     15 #include "SkStream.h"
     16 #include "SkTypeface.h"
     17 
     18 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
     19     SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
     20                                    SkIntToScalar(bm.height()));
     21     mat.mapRect(&bounds);
     22     return SkSize::Make(bounds.width(), bounds.height());
     23 }
     24 
     25 static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx,
     26                       SkFilterQuality lvl) {
     27     SkPaint paint;
     28     paint.setFilterQuality(lvl);
     29 
     30     SkAutoCanvasRestore acr(canvas, true);
     31 
     32     canvas->translate(dx, 0);
     33     canvas->concat(mat);
     34     canvas->drawBitmap(bm, 0, 0, &paint);
     35 }
     36 
     37 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
     38     draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality);
     39     draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality);
     40     draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality);
     41     draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality);
     42 }
     43 
     44 class FilterIndiaBoxGM : public skiagm::GM {
     45     void onOnceBeforeDraw() override {
     46         this->makeBitmap();
     47 
     48         SkScalar cx = SkScalarHalf(fBM.width());
     49         SkScalar cy = SkScalarHalf(fBM.height());
     50 
     51         float vertScale = 30.0f/55.0f;
     52         float horizScale = 150.0f/200.0f;
     53 
     54         fMatrix[0].setScale(horizScale, vertScale);
     55         fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
     56     }
     57 
     58 public:
     59     SkBitmap    fBM;
     60     SkMatrix    fMatrix[2];
     61     SkString    fName;
     62 
     63     FilterIndiaBoxGM() {
     64         this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
     65     }
     66 
     67     FilterIndiaBoxGM(const char filename[]) : fFilename(filename) {
     68         fName.printf("filterindiabox");
     69     }
     70 
     71 protected:
     72     SkString onShortName() override {
     73         return fName;
     74     }
     75 
     76     SkISize onISize() override {
     77         return SkISize::Make(680, 130);
     78     }
     79 
     80     void onDraw(SkCanvas* canvas) override {
     81         canvas->translate(10, 10);
     82         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
     83             SkSize size = computeSize(fBM, fMatrix[i]);
     84             size.fWidth += 20;
     85             size.fHeight += 20;
     86 
     87             draw_row(canvas, fBM, fMatrix[i], size.fWidth);
     88             canvas->translate(0, size.fHeight);
     89         }
     90     }
     91 
     92   protected:
     93       SkString fFilename;
     94       int fSize;
     95 
     96       SkScalar getScale() {
     97           return 192.f/fSize;
     98       }
     99 
    100       void makeBitmap() {
    101         if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) {
    102             fBM.allocN32Pixels(1, 1);
    103             fBM.eraseARGB(255, 255, 0 , 0); // red == bad
    104         }
    105         fSize = fBM.height();
    106       }
    107   private:
    108     typedef skiagm::GM INHERITED;
    109 };
    110 
    111 //////////////////////////////////////////////////////////////////////////////
    112 
    113 
    114 DEF_GM( return new FilterIndiaBoxGM("images/box.gif"); )
    115