blob: bc7e0359ed349797a8565b9e62b26ec7100846ee [file] [log] [blame]
bsalomon@google.comfb309512011-11-30 14:13:48 +00001
2/*
3 * Copyright 2011 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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
bsalomon@google.com7d30a212012-04-25 15:52:27 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkColorPriv.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000012#include "SkGradientShader.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000013#include "SkShader.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000014
15namespace skiagm {
16
bsalomon@google.com7d30a212012-04-25 15:52:27 +000017static SkBitmap make_chessbm(int w, int h) {
18 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000019 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000020
21 for (int y = 0; y < bm.height(); y++) {
22 uint32_t* p = bm.getAddr32(0, y);
23 for (int x = 0; x < bm.width(); x++) {
24 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
25 }
26 }
27 bm.unlockPixels();
28 return bm;
29}
30
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000031static void makebm(SkBitmap* bm, int w, int h) {
32 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000033 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000034
35 SkCanvas canvas(*bm);
36
37 SkScalar wScalar = SkIntToScalar(w);
38 SkScalar hScalar = SkIntToScalar(h);
39
40 SkPoint pt = { wScalar / 2, hScalar / 2 };
41
42 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
43
44 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
45 SK_ColorGREEN, SK_ColorMAGENTA,
46 SK_ColorBLUE, SK_ColorCYAN,
47 SK_ColorRED};
48
49 SkScalar pos[] = {0,
50 SK_Scalar1 / 6,
51 2 * SK_Scalar1 / 6,
52 3 * SK_Scalar1 / 6,
53 4 * SK_Scalar1 / 6,
54 5 * SK_Scalar1 / 6,
55 SK_Scalar1};
56
57 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000058 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
59 SkMatrix mat = SkMatrix::I();
60 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000061 paint.setShader(SkGradientShader::CreateRadial(
62 pt, radius,
63 colors, pos,
64 SK_ARRAY_COUNT(colors),
65 SkShader::kRepeat_TileMode,
66 NULL, 0, &mat))->unref();
bsalomon@google.comfb309512011-11-30 14:13:48 +000067 canvas.drawRect(rect, paint);
68 rect.inset(wScalar / 8, hScalar / 8);
69 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
70 }
71}
72
73static const int gSize = 1024;
74
75class DrawBitmapRectGM : public GM {
76public:
77 DrawBitmapRectGM() {
78 }
79
80 SkBitmap fLargeBitmap;
81
82protected:
83 SkString onShortName() {
84 return SkString("drawbitmaprect");
85 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
bsalomon@google.comfb309512011-11-30 14:13:48 +000087 SkISize onISize() { return make_isize(gSize, gSize); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
bsalomon@google.comfb309512011-11-30 14:13:48 +000089 virtual void onDraw(SkCanvas* canvas) {
90 static const int kBmpSize = 2048;
91 if (fLargeBitmap.isNull()) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000092 makebm(&fLargeBitmap, kBmpSize, kBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +000093 }
vandebo@chromium.org663515b2012-01-05 18:45:27 +000094 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
bsalomon@google.comfb309512011-11-30 14:13:48 +000095 static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2);
96
97 static const int kPadX = 30;
98 static const int kPadY = 40;
99 SkPaint paint;
100 paint.setAlpha(0x20);
101 canvas->drawBitmapRect(fLargeBitmap, NULL,
102 SkRect::MakeWH(gSize * SK_Scalar1,
103 gSize * SK_Scalar1),
104 &paint);
105 canvas->translate(SK_Scalar1 * kPadX / 2,
106 SK_Scalar1 * kPadY / 2);
107 SkPaint blackPaint;
108 SkScalar titleHeight = SK_Scalar1 * 24;
109 blackPaint.setColor(SK_ColorBLACK);
110 blackPaint.setTextSize(titleHeight);
111 blackPaint.setAntiAlias(true);
112 SkString title;
113 title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize);
114 canvas->drawText(title.c_str(), title.size(), 0,
115 titleHeight, blackPaint);
116
117 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
118 int rowCount = 0;
119 canvas->save();
120 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
121 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
122
123 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2,
124 (kBmpSize - h) / 2,
125 w, h);
126 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect);
127
128 SkString label;
129 label.appendf("%d x %d", w, h);
130 blackPaint.setAntiAlias(true);
131 blackPaint.setStyle(SkPaint::kFill_Style);
132 blackPaint.setTextSize(SK_Scalar1 * 10);
133 SkScalar baseline = dstRect.height() +
134 blackPaint.getTextSize() + SK_Scalar1 * 3;
135 canvas->drawText(label.c_str(), label.size(),
136 0, baseline,
137 blackPaint);
138 blackPaint.setStyle(SkPaint::kStroke_Style);
139 blackPaint.setStrokeWidth(SK_Scalar1);
140 blackPaint.setAntiAlias(false);
141 canvas->drawRect(dstRect, blackPaint);
142
143 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
144 ++rowCount;
145 if ((dstRect.width() + kPadX) * rowCount > gSize) {
146 canvas->restore();
147 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
148 canvas->save();
149 rowCount = 0;
150 }
151 }
152 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000153
154 {
155 // test the following code path:
156 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
157 SkIRect srcRect;
158 SkPaint paint;
159 SkBitmap bm;
160
161 bm = make_chessbm(5, 5);
reed@google.com44699382013-10-31 17:28:30 +0000162 paint.setFilterLevel(SkPaint::kLow_FilterLevel);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000163
164 srcRect.setXYWH(1, 1, 3, 3);
165 SkMaskFilter* mf = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000166 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000167 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000168 SkBlurMaskFilter::kHighQuality_BlurFlag |
169 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
170 paint.setMaskFilter(mf)->unref();
171 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint);
172 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000173 }
174
175private:
176 typedef GM INHERITED;
177};
178
179//////////////////////////////////////////////////////////////////////////////
180
181static GM* MyFactory(void*) { return new DrawBitmapRectGM; }
182static GMRegistry reg(MyFactory);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000183}