Home | History | Annotate | Download | only in gm
      1 
      2 /*
      3  * Copyright 2012 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "gm.h"
      9 #include "SkBitmap.h"
     10 #include "SkBlurMaskFilter.h"
     11 #include "SkCanvas.h"
     12 #include "SkColor.h"
     13 #include "SkMatrix.h"
     14 #include "SkPath.h"
     15 #include "SkRect.h"
     16 #include "SkSize.h"
     17 #include "SkString.h"
     18 
     19 namespace skiagm {
     20 
     21 class DrawBitmapMatrixGM : public GM {
     22 public:
     23     DrawBitmapMatrixGM() {}
     24 
     25 protected:
     26     virtual SkString onShortName() SK_OVERRIDE {
     27         return SkString("drawbitmapmatrix");
     28     }
     29 
     30     virtual SkISize onISize() SK_OVERRIDE { return make_isize(1024, 256); }
     31 
     32     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     33         SkBitmap bm;
     34         this->setupBitmap(&bm);
     35 
     36         // Draw normally.
     37         SkMatrix matrix;
     38         matrix.reset();
     39         SkPaint paint;
     40         paint.setAntiAlias(true);
     41         paint.setDither(true);
     42         canvas->drawBitmapMatrix(bm, matrix, &paint);
     43 
     44         // Draw stretched horizontally and squished vertically.
     45         canvas->translate(SkIntToScalar(bm.width() + 5), 0);
     46         matrix.setScale(SkIntToScalar(2), SK_ScalarHalf);
     47         canvas->drawBitmapMatrix(bm, matrix, &paint);
     48 
     49         // Draw rotated
     50         canvas->translate(SkIntToScalar(bm.width()*2 + 5), 0);
     51         matrix.reset();
     52         matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2),
     53                          SkIntToScalar(bm.height() / 2));
     54         canvas->save();
     55         canvas->translate(0, SkIntToScalar(10));
     56         canvas->drawBitmapMatrix(bm, matrix, &paint);
     57         canvas->restore();
     58 
     59         // Draw with perspective
     60         canvas->translate(SkIntToScalar(bm.width() + 15), 0);
     61         matrix.reset();
     62         matrix.setPerspX(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
     63         matrix.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
     64         canvas->drawBitmapMatrix(bm, matrix, &paint);
     65 
     66         // Draw with skew
     67         canvas->translate(SkIntToScalar(bm.width() + 5), 0);
     68         matrix.reset();
     69         matrix.setSkew(SkIntToScalar(2), SkIntToScalar(2));
     70         canvas->drawBitmapMatrix(bm, matrix, &paint);
     71 
     72         // Draw with sin/cos
     73         canvas->translate(SkIntToScalar(bm.width() * 4), 0);
     74         matrix.reset();
     75         matrix.setSinCos(SK_ScalarHalf, SkIntToScalar(2));
     76         canvas->drawBitmapMatrix(bm, matrix, &paint);
     77 
     78         {
     79             // test the following code path:
     80             // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
     81             SkPaint paint;
     82 
     83             paint.setFilterBitmap(true);
     84 
     85             SkMaskFilter* mf = SkBlurMaskFilter::Create(
     86                 5,
     87                 SkBlurMaskFilter::kNormal_BlurStyle,
     88                 SkBlurMaskFilter::kHighQuality_BlurFlag |
     89                 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
     90             paint.setMaskFilter(mf)->unref();
     91 
     92             canvas->translate(SkIntToScalar(bm.width()*2 + 20), 0);
     93 
     94             matrix.reset();
     95             matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2),
     96                              SkIntToScalar(bm.height() / 2));
     97 
     98             canvas->save();
     99             canvas->translate(0, SkIntToScalar(20));
    100             canvas->drawBitmapMatrix(bm, matrix, &paint);
    101             canvas->restore();
    102         }
    103 
    104     }
    105 private:
    106     void setupBitmap(SkBitmap* bm) {
    107         SkASSERT(bm);
    108         static const int SIZE = 64;
    109         bm->setConfig(SkBitmap::kARGB_8888_Config, SIZE, SIZE);
    110         bm->allocPixels();
    111         SkCanvas canvas(*bm);
    112 
    113         SkPaint paint;
    114         paint.setColor(SK_ColorGREEN);
    115         canvas.drawPaint(paint);
    116 
    117         paint.setColor(SK_ColorBLUE);
    118         paint.setAntiAlias(true);
    119         SkRect rect = SkRect::MakeWH(SkIntToScalar(SIZE), SkIntToScalar(SIZE));
    120         SkPath path;
    121         path.addOval(rect);
    122         canvas.drawPath(path, paint);
    123     }
    124 };
    125 
    126 ////////////////////////////////////////////////////////////////////////////////
    127 
    128 static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; }
    129 static GMRegistry reg(MyFactory);
    130 
    131 }
    132