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" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
robertphillips@google.com | 67d91be | 2012-09-28 17:24:44 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkColor.h" |
| 14 | #include "SkMatrix.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkRect.h" |
| 17 | #include "SkSize.h" |
| 18 | #include "SkString.h" |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | class DrawBitmapMatrixGM : public GM { |
| 23 | public: |
| 24 | DrawBitmapMatrixGM() {} |
| 25 | |
| 26 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 27 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 28 | return kSkipTiled_Flag; |
| 29 | } |
| 30 | |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 31 | virtual SkString onShortName() SK_OVERRIDE { |
| 32 | return SkString("drawbitmapmatrix"); |
| 33 | } |
| 34 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(1024, 256); } |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 36 | |
| 37 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 38 | SkBitmap bm; |
| 39 | this->setupBitmap(&bm); |
| 40 | |
| 41 | // Draw normally. |
| 42 | SkMatrix matrix; |
| 43 | matrix.reset(); |
| 44 | SkPaint paint; |
| 45 | paint.setAntiAlias(true); |
| 46 | paint.setDither(true); |
| 47 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 48 | |
| 49 | // Draw stretched horizontally and squished vertically. |
| 50 | canvas->translate(SkIntToScalar(bm.width() + 5), 0); |
| 51 | matrix.setScale(SkIntToScalar(2), SK_ScalarHalf); |
| 52 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 53 | |
| 54 | // Draw rotated |
| 55 | canvas->translate(SkIntToScalar(bm.width()*2 + 5), 0); |
| 56 | matrix.reset(); |
| 57 | matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2), |
| 58 | SkIntToScalar(bm.height() / 2)); |
| 59 | canvas->save(); |
| 60 | canvas->translate(0, SkIntToScalar(10)); |
| 61 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 62 | canvas->restore(); |
| 63 | |
| 64 | // Draw with perspective |
| 65 | canvas->translate(SkIntToScalar(bm.width() + 15), 0); |
| 66 | matrix.reset(); |
| 67 | matrix.setPerspX(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); |
| 68 | matrix.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); |
| 69 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 70 | |
| 71 | // Draw with skew |
| 72 | canvas->translate(SkIntToScalar(bm.width() + 5), 0); |
| 73 | matrix.reset(); |
| 74 | matrix.setSkew(SkIntToScalar(2), SkIntToScalar(2)); |
| 75 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 76 | |
| 77 | // Draw with sin/cos |
| 78 | canvas->translate(SkIntToScalar(bm.width() * 4), 0); |
| 79 | matrix.reset(); |
| 80 | matrix.setSinCos(SK_ScalarHalf, SkIntToScalar(2)); |
| 81 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
robertphillips@google.com | 67d91be | 2012-09-28 17:24:44 +0000 | [diff] [blame] | 82 | |
| 83 | { |
| 84 | // test the following code path: |
| 85 | // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() |
| 86 | SkPaint paint; |
| 87 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 88 | paint.setFilterLevel(SkPaint::kLow_FilterLevel); |
robertphillips@google.com | 67d91be | 2012-09-28 17:24:44 +0000 | [diff] [blame] | 89 | |
| 90 | SkMaskFilter* mf = SkBlurMaskFilter::Create( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 91 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 92 | SkBlurMask::ConvertRadiusToSigma(5), |
robertphillips@google.com | 67d91be | 2012-09-28 17:24:44 +0000 | [diff] [blame] | 93 | SkBlurMaskFilter::kHighQuality_BlurFlag | |
| 94 | SkBlurMaskFilter::kIgnoreTransform_BlurFlag); |
| 95 | paint.setMaskFilter(mf)->unref(); |
| 96 | |
| 97 | canvas->translate(SkIntToScalar(bm.width()*2 + 20), 0); |
| 98 | |
| 99 | matrix.reset(); |
| 100 | matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2), |
| 101 | SkIntToScalar(bm.height() / 2)); |
| 102 | |
| 103 | canvas->save(); |
| 104 | canvas->translate(0, SkIntToScalar(20)); |
| 105 | canvas->drawBitmapMatrix(bm, matrix, &paint); |
| 106 | canvas->restore(); |
| 107 | } |
| 108 | |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 109 | } |
| 110 | private: |
| 111 | void setupBitmap(SkBitmap* bm) { |
| 112 | SkASSERT(bm); |
| 113 | static const int SIZE = 64; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 114 | bm->allocN32Pixels(SIZE, SIZE); |
scroggo@google.com | 58be682 | 2012-07-30 14:40:01 +0000 | [diff] [blame] | 115 | SkCanvas canvas(*bm); |
| 116 | |
| 117 | SkPaint paint; |
| 118 | paint.setColor(SK_ColorGREEN); |
| 119 | canvas.drawPaint(paint); |
| 120 | |
| 121 | paint.setColor(SK_ColorBLUE); |
| 122 | paint.setAntiAlias(true); |
| 123 | SkRect rect = SkRect::MakeWH(SkIntToScalar(SIZE), SkIntToScalar(SIZE)); |
| 124 | SkPath path; |
| 125 | path.addOval(rect); |
| 126 | canvas.drawPath(path, paint); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | //////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; } |
| 133 | static GMRegistry reg(MyFactory); |
| 134 | |
| 135 | } |