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 "SkBlurMask.h" 11 #include "SkBlurMaskFilter.h" 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: 27 virtual SkString onShortName() SK_OVERRIDE { 28 return SkString("drawbitmapmatrix"); 29 } 30 31 virtual SkISize onISize() SK_OVERRIDE { return make_isize(1024, 256); } 32 33 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 34 SkBitmap bm; 35 this->setupBitmap(&bm); 36 37 // Draw normally. 38 SkMatrix matrix; 39 matrix.reset(); 40 SkPaint paint; 41 paint.setAntiAlias(true); 42 paint.setDither(true); 43 canvas->drawBitmapMatrix(bm, matrix, &paint); 44 45 // Draw stretched horizontally and squished vertically. 46 canvas->translate(SkIntToScalar(bm.width() + 5), 0); 47 matrix.setScale(SkIntToScalar(2), SK_ScalarHalf); 48 canvas->drawBitmapMatrix(bm, matrix, &paint); 49 50 // Draw rotated 51 canvas->translate(SkIntToScalar(bm.width()*2 + 5), 0); 52 matrix.reset(); 53 matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2), 54 SkIntToScalar(bm.height() / 2)); 55 canvas->save(); 56 canvas->translate(0, SkIntToScalar(10)); 57 canvas->drawBitmapMatrix(bm, matrix, &paint); 58 canvas->restore(); 59 60 // Draw with perspective 61 canvas->translate(SkIntToScalar(bm.width() + 15), 0); 62 matrix.reset(); 63 matrix.setPerspX(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); 64 matrix.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000))); 65 canvas->drawBitmapMatrix(bm, matrix, &paint); 66 67 // Draw with skew 68 canvas->translate(SkIntToScalar(bm.width() + 5), 0); 69 matrix.reset(); 70 matrix.setSkew(SkIntToScalar(2), SkIntToScalar(2)); 71 canvas->drawBitmapMatrix(bm, matrix, &paint); 72 73 // Draw with sin/cos 74 canvas->translate(SkIntToScalar(bm.width() * 4), 0); 75 matrix.reset(); 76 matrix.setSinCos(SK_ScalarHalf, SkIntToScalar(2)); 77 canvas->drawBitmapMatrix(bm, matrix, &paint); 78 79 { 80 // test the following code path: 81 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() 82 SkPaint paint; 83 84 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 85 86 SkMaskFilter* mf = SkBlurMaskFilter::Create( 87 SkBlurMaskFilter::kNormal_BlurStyle, 88 SkBlurMask::ConvertRadiusToSigma(5), 89 SkBlurMaskFilter::kHighQuality_BlurFlag | 90 SkBlurMaskFilter::kIgnoreTransform_BlurFlag); 91 paint.setMaskFilter(mf)->unref(); 92 93 canvas->translate(SkIntToScalar(bm.width()*2 + 20), 0); 94 95 matrix.reset(); 96 matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2), 97 SkIntToScalar(bm.height() / 2)); 98 99 canvas->save(); 100 canvas->translate(0, SkIntToScalar(20)); 101 canvas->drawBitmapMatrix(bm, matrix, &paint); 102 canvas->restore(); 103 } 104 105 } 106 private: 107 void setupBitmap(SkBitmap* bm) { 108 SkASSERT(bm); 109 static const int SIZE = 64; 110 bm->setConfig(SkBitmap::kARGB_8888_Config, SIZE, SIZE); 111 bm->allocPixels(); 112 SkCanvas canvas(*bm); 113 114 SkPaint paint; 115 paint.setColor(SK_ColorGREEN); 116 canvas.drawPaint(paint); 117 118 paint.setColor(SK_ColorBLUE); 119 paint.setAntiAlias(true); 120 SkRect rect = SkRect::MakeWH(SkIntToScalar(SIZE), SkIntToScalar(SIZE)); 121 SkPath path; 122 path.addOval(rect); 123 canvas.drawPath(path, paint); 124 } 125 }; 126 127 //////////////////////////////////////////////////////////////////////////////// 128 129 static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; } 130 static GMRegistry reg(MyFactory); 131 132 } 133