blob: 46098919f3592a407ad9caf23b86c6f4f75c0a94 [file] [log] [blame]
scroggo@google.com58be6822012-07-30 14:40:01 +00001
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.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
robertphillips@google.com67d91be2012-09-28 17:24:44 +000011#include "SkBlurMaskFilter.h"
scroggo@google.com58be6822012-07-30 14:40:01 +000012#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
20namespace skiagm {
21
22class DrawBitmapMatrixGM : public GM {
23public:
24 DrawBitmapMatrixGM() {}
25
26protected:
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);
robertphillips@google.com67d91be2012-09-28 17:24:44 +000078
79 {
80 // test the following code path:
81 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
82 SkPaint paint;
83
reed@google.com44699382013-10-31 17:28:30 +000084 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
robertphillips@google.com67d91be2012-09-28 17:24:44 +000085
86 SkMaskFilter* mf = SkBlurMaskFilter::Create(
robertphillips@google.com67d91be2012-09-28 17:24:44 +000087 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 SkBlurMask::ConvertRadiusToSigma(5),
robertphillips@google.com67d91be2012-09-28 17:24:44 +000089 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
scroggo@google.com58be6822012-07-30 14:40:01 +0000105 }
106private:
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
129static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; }
130static GMRegistry reg(MyFactory);
131
132}