blob: 106f6d393e20e3e56dec7185cfa603bfa19cf75d [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;
19 bm.setConfig(SkBitmap::kARGB_8888_Config , w, h);
20 bm.allocPixels();
21
22 for (int y = 0; y < bm.height(); y++) {
23 uint32_t* p = bm.getAddr32(0, y);
24 for (int x = 0; x < bm.width(); x++) {
25 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
26 }
27 }
28 bm.unlockPixels();
29 return bm;
30}
31
bsalomon@google.comfb309512011-11-30 14:13:48 +000032static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
33 bm->setConfig(config, w, h);
34 bm->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000035 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000036
37 SkCanvas canvas(*bm);
38
39 SkScalar wScalar = SkIntToScalar(w);
40 SkScalar hScalar = SkIntToScalar(h);
41
42 SkPoint pt = { wScalar / 2, hScalar / 2 };
43
44 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
45
46 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
47 SK_ColorGREEN, SK_ColorMAGENTA,
48 SK_ColorBLUE, SK_ColorCYAN,
49 SK_ColorRED};
50
51 SkScalar pos[] = {0,
52 SK_Scalar1 / 6,
53 2 * SK_Scalar1 / 6,
54 3 * SK_Scalar1 / 6,
55 4 * SK_Scalar1 / 6,
56 5 * SK_Scalar1 / 6,
57 SK_Scalar1};
58
59 SkPaint paint;
60 paint.setShader(SkGradientShader::CreateRadial(
61 pt, radius,
62 colors, pos,
63 SK_ARRAY_COUNT(colors),
64 SkShader::kRepeat_TileMode))->unref();
65 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
66 SkMatrix mat = SkMatrix::I();
67 for (int i = 0; i < 4; ++i) {
68 paint.getShader()->setLocalMatrix(mat);
69 canvas.drawRect(rect, paint);
70 rect.inset(wScalar / 8, hScalar / 8);
71 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
72 }
73}
74
75static const int gSize = 1024;
76
77class DrawBitmapRectGM : public GM {
78public:
79 DrawBitmapRectGM() {
80 }
81
82 SkBitmap fLargeBitmap;
83
84protected:
85 SkString onShortName() {
86 return SkString("drawbitmaprect");
87 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000088
bsalomon@google.comfb309512011-11-30 14:13:48 +000089 SkISize onISize() { return make_isize(gSize, gSize); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
bsalomon@google.comfb309512011-11-30 14:13:48 +000091 virtual void onDraw(SkCanvas* canvas) {
92 static const int kBmpSize = 2048;
93 if (fLargeBitmap.isNull()) {
94 makebm(&fLargeBitmap,
95 SkBitmap::kARGB_8888_Config,
96 kBmpSize, kBmpSize);
97 }
vandebo@chromium.org663515b2012-01-05 18:45:27 +000098 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
bsalomon@google.comfb309512011-11-30 14:13:48 +000099 static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2);
100
101 static const int kPadX = 30;
102 static const int kPadY = 40;
103 SkPaint paint;
104 paint.setAlpha(0x20);
105 canvas->drawBitmapRect(fLargeBitmap, NULL,
106 SkRect::MakeWH(gSize * SK_Scalar1,
107 gSize * SK_Scalar1),
108 &paint);
109 canvas->translate(SK_Scalar1 * kPadX / 2,
110 SK_Scalar1 * kPadY / 2);
111 SkPaint blackPaint;
112 SkScalar titleHeight = SK_Scalar1 * 24;
113 blackPaint.setColor(SK_ColorBLACK);
114 blackPaint.setTextSize(titleHeight);
115 blackPaint.setAntiAlias(true);
116 SkString title;
117 title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize);
118 canvas->drawText(title.c_str(), title.size(), 0,
119 titleHeight, blackPaint);
120
121 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
122 int rowCount = 0;
123 canvas->save();
124 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
125 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
126
127 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2,
128 (kBmpSize - h) / 2,
129 w, h);
130 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect);
131
132 SkString label;
133 label.appendf("%d x %d", w, h);
134 blackPaint.setAntiAlias(true);
135 blackPaint.setStyle(SkPaint::kFill_Style);
136 blackPaint.setTextSize(SK_Scalar1 * 10);
137 SkScalar baseline = dstRect.height() +
138 blackPaint.getTextSize() + SK_Scalar1 * 3;
139 canvas->drawText(label.c_str(), label.size(),
140 0, baseline,
141 blackPaint);
142 blackPaint.setStyle(SkPaint::kStroke_Style);
143 blackPaint.setStrokeWidth(SK_Scalar1);
144 blackPaint.setAntiAlias(false);
145 canvas->drawRect(dstRect, blackPaint);
146
147 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
148 ++rowCount;
149 if ((dstRect.width() + kPadX) * rowCount > gSize) {
150 canvas->restore();
151 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
152 canvas->save();
153 rowCount = 0;
154 }
155 }
156 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000157
158 {
159 // test the following code path:
160 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
161 SkIRect srcRect;
162 SkPaint paint;
163 SkBitmap bm;
164
165 bm = make_chessbm(5, 5);
166 paint.setFilterBitmap(true);
167
168 srcRect.setXYWH(1, 1, 3, 3);
169 SkMaskFilter* mf = SkBlurMaskFilter::Create(
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000170 SkBlurMaskFilter::kNormal_BlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000171 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000172 SkBlurMaskFilter::kHighQuality_BlurFlag |
173 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
174 paint.setMaskFilter(mf)->unref();
175 canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint);
176 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000177 }
178
179private:
180 typedef GM INHERITED;
181};
182
183//////////////////////////////////////////////////////////////////////////////
184
borenet@google.comceb2a032012-06-25 21:03:04 +0000185#ifndef SK_BUILD_FOR_ANDROID
bsalomon@google.comfb309512011-11-30 14:13:48 +0000186static GM* MyFactory(void*) { return new DrawBitmapRectGM; }
187static GMRegistry reg(MyFactory);
borenet@google.comceb2a032012-06-25 21:03:04 +0000188#endif
bsalomon@google.comfb309512011-11-30 14:13:48 +0000189}