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 
     10 #include "Resources.h"
     11 #include "SkBitmapProcState.h"
     12 #include "SkBitmapScaler.h"
     13 #include "SkGradientShader.h"
     14 #include "SkImageDecoder.h"
     15 #include "SkImageEncoder.h"
     16 #include "SkStream.h"
     17 #include "SkTypeface.h"
     18 
     19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
     20     SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
     21                                    SkIntToScalar(bm.height()));
     22     mat.mapRect(&bounds);
     23     return SkSize::Make(bounds.width(), bounds.height());
     24 }
     25 
     26 static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
     27     SkPaint paint;
     28 
     29     SkAutoCanvasRestore acr(canvas, true);
     30 
     31     canvas->drawBitmapMatrix(bm, mat, &paint);
     32 
     33     paint.setFilterLevel(SkPaint::kLow_FilterLevel);
     34     canvas->translate(dx, 0);
     35     canvas->drawBitmapMatrix(bm, mat, &paint);
     36 
     37     paint.setFilterLevel(SkPaint::kMedium_FilterLevel);
     38     canvas->translate(dx, 0);
     39     canvas->drawBitmapMatrix(bm, mat, &paint);
     40 
     41     paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
     42     canvas->translate(dx, 0);
     43     canvas->drawBitmapMatrix(bm, mat, &paint);
     44 }
     45 
     46 class FilterIndiaBoxGM : public skiagm::GM {
     47     void onOnceBeforeDraw() {
     48         this->makeBitmap();
     49 
     50         SkScalar cx = SkScalarHalf(fBM.width());
     51         SkScalar cy = SkScalarHalf(fBM.height());
     52 
     53         float vertScale = 30.0f/55.0f;
     54         float horizScale = 150.0f/200.0f;
     55 
     56         fMatrix[0].setScale(horizScale, vertScale);
     57         fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
     58     }
     59 
     60 public:
     61     SkBitmap    fBM;
     62     SkMatrix    fMatrix[2];
     63     SkString    fName;
     64 
     65     FilterIndiaBoxGM() {
     66         this->setBGColor(0xFFDDDDDD);
     67     }
     68 
     69     FilterIndiaBoxGM(const char filename[]) : fFilename(filename) {
     70         fName.printf("filterindiabox");
     71     }
     72 
     73 protected:
     74     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     75         return kSkipTiled_Flag;
     76     }
     77 
     78     virtual SkString onShortName() SK_OVERRIDE {
     79         return fName;
     80     }
     81 
     82     virtual SkISize onISize() SK_OVERRIDE {
     83         return SkISize::Make(1024, 768);
     84     }
     85 
     86     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     87         canvas->translate(10, 10);
     88         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
     89             SkSize size = computeSize(fBM, fMatrix[i]);
     90             size.fWidth += 20;
     91             size.fHeight += 20;
     92 
     93             draw_row(canvas, fBM, fMatrix[i], size.fWidth);
     94             canvas->translate(0, size.fHeight);
     95         }
     96     }
     97 
     98   protected:
     99       SkString fFilename;
    100       int fSize;
    101 
    102       SkScalar getScale() {
    103           return 192.f/fSize;
    104       }
    105 
    106       void makeBitmap() {
    107           SkString resourcePath = GetResourcePath();
    108           resourcePath.append("/");
    109           resourcePath.append(fFilename);
    110 
    111           SkImageDecoder* codec = NULL;
    112           SkFILEStream stream(resourcePath.c_str());
    113           if (stream.isValid()) {
    114               codec = SkImageDecoder::Factory(&stream);
    115           }
    116           if (codec) {
    117               stream.rewind();
    118               codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
    119               SkDELETE(codec);
    120           } else {
    121               fBM.allocN32Pixels(1, 1);
    122               *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
    123           }
    124           fSize = fBM.height();
    125       }
    126   private:
    127     typedef skiagm::GM INHERITED;
    128 };
    129 
    130 //////////////////////////////////////////////////////////////////////////////
    131 
    132 
    133 DEF_GM( return new FilterIndiaBoxGM("box.gif"); )
    134