blob: 052d1533164195af39bb1cb6e2af286913800170 [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:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027 virtual uint32_t onGetFlags() const SK_OVERRIDE {
28 return kSkipTiled_Flag;
29 }
30
scroggo@google.com58be6822012-07-30 14:40:01 +000031 virtual SkString onShortName() SK_OVERRIDE {
32 return SkString("drawbitmapmatrix");
33 }
34
tfarinaf5393182014-06-09 23:59:03 -070035 virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(1024, 256); }
scroggo@google.com58be6822012-07-30 14:40:01 +000036
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.com67d91be2012-09-28 17:24:44 +000082
83 {
84 // test the following code path:
85 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
86 SkPaint paint;
87
reed@google.com44699382013-10-31 17:28:30 +000088 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
robertphillips@google.com67d91be2012-09-28 17:24:44 +000089
90 SkMaskFilter* mf = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000091 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000092 SkBlurMask::ConvertRadiusToSigma(5),
robertphillips@google.com67d91be2012-09-28 17:24:44 +000093 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.com58be6822012-07-30 14:40:01 +0000109 }
110private:
111 void setupBitmap(SkBitmap* bm) {
112 SkASSERT(bm);
113 static const int SIZE = 64;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000114 bm->allocN32Pixels(SIZE, SIZE);
scroggo@google.com58be6822012-07-30 14:40:01 +0000115 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
132static GM* MyFactory(void*) { return new DrawBitmapMatrixGM; }
133static GMRegistry reg(MyFactory);
134
135}