scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 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 "SkCanvas.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkSize.h" |
| 16 | #include "SkString.h" |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | class DrawBitmapMatrixGM : public GM { |
| 21 | public: |
| 22 | DrawBitmapMatrixGM() {} |
| 23 | |
| 24 | protected: |
| 25 | virtual SkString onShortName() SK_OVERRIDE { |
| 26 | return SkString("drawbitmapmatrix"); |
| 27 | } |
| 28 | |
| 29 | virtual SkISize onISize() SK_OVERRIDE { return make_isize(1024, 256); } |
| 30 | |
| 31 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 32 | SkBitmap bm; |
| 33 | this->setupBitmap(&bm); |
| 34 | |
| 35 | // Draw normally. |
| 36 | SkMatrix matrix; |
| 37 | matrix.reset(); |
| 38 | SkPaint paint; |
| 39 | paint.setAntiAlias(true); |
| 40 | paint.setDither(true); |
| 41 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 42 | |
| 43 | // Draw stretched horizontally and squished vertically. |
| 44 | canvas->translate(SkIntToScalar(bm.width() + 5), 0); |
| 45 | matrix.setScale(SkIntToScalar(2), SK_ScalarHalf); |
| 46 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 47 | |
| 48 | // Draw rotated |
| 49 | canvas->translate(SkIntToScalar(bm.width()*2 + 5), 0); |
| 50 | matrix.reset(); |
| 51 | matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2), |
| 52 | SkIntToScalar(bm.height() / 2)); |
| 53 | canvas->save(); |
| 54 | canvas->translate(0, SkIntToScalar(10)); |
| 55 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 56 | canvas->restore(); |
| 57 | |
| 58 | // Draw with perspective |
| 59 | canvas->translate(SkIntToScalar(bm.width() + 15), 0); |
| 60 | matrix.reset(); |
| 61 | matrix.setPerspX(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); |
| 62 | matrix.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); |
| 63 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 64 | |
| 65 | // Draw with skew |
| 66 | canvas->translate(SkIntToScalar(bm.width() + 5), 0); |
| 67 | matrix.reset(); |
| 68 | matrix.setSkew(SkIntToScalar(2), SkIntToScalar(2)); |
| 69 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 70 | |
| 71 | // Draw with sin/cos |
| 72 | canvas->translate(SkIntToScalar(bm.width() * 4), 0); |
| 73 | matrix.reset(); |
| 74 | matrix.setSinCos(SK_ScalarHalf, SkIntToScalar(2)); |
| 75 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 76 | } |
| 77 | private: |
| 78 | void setupBitmap(SkBitmap* bm) { |
| 79 | SkASSERT(bm); |
| 80 | static const int SIZE = 64; |
| 81 | bm->setConfig(SkBitmap::kARGB_8888_Config, SIZE, SIZE); |
| 82 | bm->allocPixels(); |
| 83 | SkCanvas canvas(*bm); |
| 84 | |
| 85 | SkPaint paint; |
| 86 | paint.setColor(SK_ColorGREEN); |
| 87 | canvas.drawPaint(paint); |
| 88 | |
| 89 | paint.setColor(SK_ColorBLUE); |
| 90 | paint.setAntiAlias(true); |
| 91 | SkRect rect = SkRect::MakeWH(SkIntToScalar(SIZE), SkIntToScalar(SIZE)); |
| 92 | SkPath path; |
| 93 | path.addOval(rect); |
| 94 | canvas.drawPath(path, paint); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | //////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; } |
| 101 | static GMRegistry reg(MyFactory); |
| 102 | |
| 103 | } |