blob: 9db5ca71dae4b303245ce8428cc631ad300b45d7 [file] [log] [blame]
bsalomon@google.comfb309512011-11-30 14:13:48 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reedf803da12015-01-23 05:58:07 -08007
bsalomon@google.comfb309512011-11-30 14:13:48 +00008#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"
reedf803da12015-01-23 05:58:07 -080014#include "SkImage.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000015
bsalomon@google.com7d30a212012-04-25 15:52:27 +000016static SkBitmap make_chessbm(int w, int h) {
17 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000018 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000019
20 for (int y = 0; y < bm.height(); y++) {
21 uint32_t* p = bm.getAddr32(0, y);
22 for (int x = 0; x < bm.width(); x++) {
23 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
24 }
25 }
26 bm.unlockPixels();
27 return bm;
28}
29
reedf803da12015-01-23 05:58:07 -080030static SkImage* image_from_bitmap(const SkBitmap& bm) {
31 SkBitmap b(bm);
32 b.lockPixels();
33 return SkImage::NewRasterCopy(b.info(), b.getPixels(), b.rowBytes());
34}
35
36static SkImage* makebm(SkBitmap* bm, int w, int h) {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000037 bm->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000038 bm->eraseColor(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000039
40 SkCanvas canvas(*bm);
41
42 SkScalar wScalar = SkIntToScalar(w);
43 SkScalar hScalar = SkIntToScalar(h);
44
45 SkPoint pt = { wScalar / 2, hScalar / 2 };
46
47 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
48
49 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
50 SK_ColorGREEN, SK_ColorMAGENTA,
51 SK_ColorBLUE, SK_ColorCYAN,
52 SK_ColorRED};
53
54 SkScalar pos[] = {0,
55 SK_Scalar1 / 6,
56 2 * SK_Scalar1 / 6,
57 3 * SK_Scalar1 / 6,
58 4 * SK_Scalar1 / 6,
59 5 * SK_Scalar1 / 6,
60 SK_Scalar1};
61
62 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000063 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
64 SkMatrix mat = SkMatrix::I();
65 for (int i = 0; i < 4; ++i) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000066 paint.setShader(SkGradientShader::CreateRadial(
67 pt, radius,
68 colors, pos,
69 SK_ARRAY_COUNT(colors),
70 SkShader::kRepeat_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000071 0, &mat))->unref();
bsalomon@google.comfb309512011-11-30 14:13:48 +000072 canvas.drawRect(rect, paint);
73 rect.inset(wScalar / 8, hScalar / 8);
74 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
75 }
commit-bot@chromium.org5d415072014-05-02 14:22:38 +000076 // Let backends know we won't change this, so they don't have to deep copy it defensively.
77 bm->setImmutable();
reedf803da12015-01-23 05:58:07 -080078
79 return image_from_bitmap(*bm);
bsalomon@google.comfb309512011-11-30 14:13:48 +000080}
81
reed84984ef2015-07-17 07:09:43 -070082static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -080083 const SkRect& dstR) {
reed84984ef2015-07-17 07:09:43 -070084 canvas->drawBitmapRect(bm, srcR, dstR, NULL);
reedf803da12015-01-23 05:58:07 -080085}
bsalomon@google.comfb309512011-11-30 14:13:48 +000086
reed84984ef2015-07-17 07:09:43 -070087static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -080088 const SkRect& dstR) {
reed84984ef2015-07-17 07:09:43 -070089 canvas->drawImageRect(image, srcR, dstR, NULL);
reedf803da12015-01-23 05:58:07 -080090}
91
reed84984ef2015-07-17 07:09:43 -070092static void imagescaleproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcIR,
reedf803da12015-01-23 05:58:07 -080093 const SkRect& dstR) {
94 const int newW = SkScalarRoundToInt(dstR.width());
95 const int newH = SkScalarRoundToInt(dstR.height());
reed84984ef2015-07-17 07:09:43 -070096 SkAutoTUnref<SkImage> newImage(image->newImage(newW, newH, &srcIR));
reedf803da12015-01-23 05:58:07 -080097
98#ifdef SK_DEBUG
99 const SkIRect baseR = SkIRect::MakeWH(image->width(), image->height());
reed84984ef2015-07-17 07:09:43 -0700100 const bool containsSubset = baseR.contains(srcIR);
reedf803da12015-01-23 05:58:07 -0800101#endif
102
103 if (newImage) {
104 SkASSERT(containsSubset);
105 canvas->drawImage(newImage, dstR.x(), dstR.y());
106 } else {
107 // newImage() does not support subsets that are not contained by the original
108 // but drawImageRect does, so we just draw an X in its place to indicate that we are
109 // deliberately not drawing here.
110 SkASSERT(!containsSubset);
111 SkPaint paint;
112 paint.setStyle(SkPaint::kStroke_Style);
113 paint.setStrokeWidth(4);
114 canvas->drawLine(4, 4, newW - 4.0f, newH - 4.0f, paint);
115 canvas->drawLine(4, newH - 4.0f, newW - 4.0f, 4, paint);
116 }
117}
118
reed84984ef2015-07-17 07:09:43 -0700119typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&);
reedf803da12015-01-23 05:58:07 -0800120
121static const int gSize = 1024;
122static const int gBmpSize = 2048;
123
124class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000125public:
reedf803da12015-01-23 05:58:07 -0800126 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
127 fName.set("drawbitmaprect");
128 if (suffix) {
129 fName.append(suffix);
130 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000131 }
132
reedf803da12015-01-23 05:58:07 -0800133 DrawRectRectProc* fProc;
134 SkBitmap fLargeBitmap;
135 SkAutoTUnref<SkImage> fImage;
136 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000137
138protected:
mtklein36352bf2015-03-25 18:17:31 -0700139 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800140
mtklein36352bf2015-03-25 18:17:31 -0700141 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800142
mtklein36352bf2015-03-25 18:17:31 -0700143 void onOnceBeforeDraw() override {
reedf803da12015-01-23 05:58:07 -0800144 fImage.reset(makebm(&fLargeBitmap, gBmpSize, gBmpSize));
bsalomon@google.comfb309512011-11-30 14:13:48 +0000145 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
mtklein36352bf2015-03-25 18:17:31 -0700147 void onDraw(SkCanvas* canvas) override {
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000148 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
reedf803da12015-01-23 05:58:07 -0800149 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000150
151 static const int kPadX = 30;
152 static const int kPadY = 40;
153 SkPaint paint;
154 paint.setAlpha(0x20);
155 canvas->drawBitmapRect(fLargeBitmap, NULL,
156 SkRect::MakeWH(gSize * SK_Scalar1,
157 gSize * SK_Scalar1),
158 &paint);
159 canvas->translate(SK_Scalar1 * kPadX / 2,
160 SK_Scalar1 * kPadY / 2);
161 SkPaint blackPaint;
162 SkScalar titleHeight = SK_Scalar1 * 24;
163 blackPaint.setColor(SK_ColorBLACK);
164 blackPaint.setTextSize(titleHeight);
165 blackPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400166 sk_tool_utils::set_portable_typeface(&blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000167 SkString title;
reedf803da12015-01-23 05:58:07 -0800168 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000169 canvas->drawText(title.c_str(), title.size(), 0,
170 titleHeight, blackPaint);
171
172 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
173 int rowCount = 0;
174 canvas->save();
175 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
176 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
177
reedf803da12015-01-23 05:58:07 -0800178 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
reed84984ef2015-07-17 07:09:43 -0700179 fProc(canvas, fImage, fLargeBitmap, srcRect, dstRect);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000180
181 SkString label;
182 label.appendf("%d x %d", w, h);
183 blackPaint.setAntiAlias(true);
184 blackPaint.setStyle(SkPaint::kFill_Style);
185 blackPaint.setTextSize(SK_Scalar1 * 10);
186 SkScalar baseline = dstRect.height() +
187 blackPaint.getTextSize() + SK_Scalar1 * 3;
188 canvas->drawText(label.c_str(), label.size(),
189 0, baseline,
190 blackPaint);
191 blackPaint.setStyle(SkPaint::kStroke_Style);
192 blackPaint.setStrokeWidth(SK_Scalar1);
193 blackPaint.setAntiAlias(false);
194 canvas->drawRect(dstRect, blackPaint);
195
196 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
197 ++rowCount;
198 if ((dstRect.width() + kPadX) * rowCount > gSize) {
199 canvas->restore();
200 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
201 canvas->save();
202 rowCount = 0;
203 }
204 }
205 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000206
207 {
208 // test the following code path:
209 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
210 SkIRect srcRect;
211 SkPaint paint;
212 SkBitmap bm;
213
214 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700215 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000216
217 srcRect.setXYWH(1, 1, 3, 3);
218 SkMaskFilter* mf = SkBlurMaskFilter::Create(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000219 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000220 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000221 SkBlurMaskFilter::kHighQuality_BlurFlag |
222 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
223 paint.setMaskFilter(mf)->unref();
reed84984ef2015-07-17 07:09:43 -0700224 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000225 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000226 }
227
228private:
reedf803da12015-01-23 05:58:07 -0800229 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000230};
231
reedf803da12015-01-23 05:58:07 -0800232DEF_GM( return new DrawBitmapRectGM(canvasproc, NULL); )
233DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )
234DEF_GM( return new DrawBitmapRectGM(imagescaleproc, "-imagescale"); )