blob: 6b5f6d9920c36acf371d390c12a5f65627a9461e [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkBlurMask.h"
bsalomon@google.com7d30a212012-04-25 15:52:27 +000011#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000012#include "SkColorPriv.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000013#include "SkGradientShader.h"
reedf803da12015-01-23 05:58:07 -080014#include "SkImage.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080015#include "SkImage_Base.h"
halcanary4dbbd042016-06-07 17:21:10 -070016#include "SkMathPriv.h"
robertphillips24eb7a82015-09-24 08:47:49 -070017#include "SkShader.h"
18#include "SkSurface.h"
19
bsalomon@google.comfb309512011-11-30 14:13:48 +000020
bsalomon@google.com7d30a212012-04-25 15:52:27 +000021static SkBitmap make_chessbm(int w, int h) {
22 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000023 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000024
25 for (int y = 0; y < bm.height(); y++) {
26 uint32_t* p = bm.getAddr32(0, y);
27 for (int x = 0; x < bm.width(); x++) {
28 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
29 }
30 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +000031 return bm;
32}
33
bsalomon7cf36cc2016-07-14 09:33:42 -070034// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070035static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070036 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070037
Mike Reed46596ae2018-01-02 15:40:29 -050038 auto surface(sk_tool_utils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070039 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000040
robertphillips24eb7a82015-09-24 08:47:49 -070041 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000042
43 SkScalar wScalar = SkIntToScalar(w);
44 SkScalar hScalar = SkIntToScalar(h);
45
46 SkPoint pt = { wScalar / 2, hScalar / 2 };
47
48 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
49
50 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
51 SK_ColorGREEN, SK_ColorMAGENTA,
52 SK_ColorBLUE, SK_ColorCYAN,
53 SK_ColorRED};
54
55 SkScalar pos[] = {0,
56 SK_Scalar1 / 6,
57 2 * SK_Scalar1 / 6,
58 3 * SK_Scalar1 / 6,
59 4 * SK_Scalar1 / 6,
60 5 * SK_Scalar1 / 6,
61 SK_Scalar1};
62
63 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000064 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
65 SkMatrix mat = SkMatrix::I();
66 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080067 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000068 pt, radius,
69 colors, pos,
70 SK_ARRAY_COUNT(colors),
71 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080072 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070073 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000074 rect.inset(wScalar / 8, hScalar / 8);
75 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
76 }
reedf803da12015-01-23 05:58:07 -080077
reed9ce9d672016-03-17 10:51:11 -070078 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070079
80 SkBitmap tempBM;
81
Cary Clark4f5a79c2018-02-07 15:51:00 -050082 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -070083
84 // Let backends know we won't change this, so they don't have to deep copy it defensively.
85 tempBM.setImmutable();
86 *resultBM = tempBM;
87
88 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +000089}
90
fmalitaab83da72016-08-26 13:04:14 -070091static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
92 const SkRect& dstR, const SkPaint* paint) {
93 canvas->drawBitmapRect(bm, srcR, dstR, paint);
94}
95
96static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
97 const SkRect& dstR, const SkPaint* paint) {
98 if (!bm.bounds().contains(srcR)) {
99 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
100 return;
101 }
102
103 SkBitmap subset;
104 if (bm.extractSubset(&subset, srcR)) {
105 canvas->drawBitmapRect(subset, dstR, paint);
106 }
reedf803da12015-01-23 05:58:07 -0800107}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000108
reed84984ef2015-07-17 07:09:43 -0700109static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
fmalitaab83da72016-08-26 13:04:14 -0700110 const SkRect& dstR, const SkPaint* paint) {
111 canvas->drawImageRect(image, srcR, dstR, paint);
reedf803da12015-01-23 05:58:07 -0800112}
113
fmalitaab83da72016-08-26 13:04:14 -0700114static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
115 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
116 if (!image->bounds().contains(srcR)) {
117 imageproc(canvas, image, bm, srcR, dstR, paint);
118 return;
119 }
120
121 if (sk_sp<SkImage> subset = image->makeSubset(srcR)) {
122 canvas->drawImageRect(subset, dstR, paint);
123 }
124}
125
126typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
127 const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800128
mtkleindbfd7ab2016-09-01 11:24:54 -0700129constexpr int gSize = 1024;
130constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800131
132class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000133public:
reedf803da12015-01-23 05:58:07 -0800134 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
135 fName.set("drawbitmaprect");
136 if (suffix) {
137 fName.append(suffix);
138 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000139 }
140
reed9ce9d672016-03-17 10:51:11 -0700141 DrawRectRectProc* fProc;
142 SkBitmap fLargeBitmap;
143 sk_sp<SkImage> fImage;
144 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000145
146protected:
mtklein36352bf2015-03-25 18:17:31 -0700147 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800148
mtklein36352bf2015-03-25 18:17:31 -0700149 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800150
robertphillips24eb7a82015-09-24 08:47:49 -0700151 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700152 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000153 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
mtklein36352bf2015-03-25 18:17:31 -0700155 void onDraw(SkCanvas* canvas) override {
robertphillips24eb7a82015-09-24 08:47:49 -0700156 if (!fImage) {
157 this->setupImage(canvas);
158 }
159
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000160 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700161 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000162
mtkleindbfd7ab2016-09-01 11:24:54 -0700163 const int kPadX = 30;
164 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000165 SkPaint paint;
166 paint.setAlpha(0x20);
bsalomon7cf36cc2016-07-14 09:33:42 -0700167 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000168 canvas->translate(SK_Scalar1 * kPadX / 2,
169 SK_Scalar1 * kPadY / 2);
170 SkPaint blackPaint;
171 SkScalar titleHeight = SK_Scalar1 * 24;
172 blackPaint.setColor(SK_ColorBLACK);
173 blackPaint.setTextSize(titleHeight);
174 blackPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700175 sk_tool_utils::set_portable_typeface(&blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000176 SkString title;
reedf803da12015-01-23 05:58:07 -0800177 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
Cary Clark2a475ea2017-04-28 15:35:12 -0400178 canvas->drawString(title, 0,
bsalomon@google.comfb309512011-11-30 14:13:48 +0000179 titleHeight, blackPaint);
180
181 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
182 int rowCount = 0;
183 canvas->save();
184 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
185 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
186
reedf803da12015-01-23 05:58:07 -0800187 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
fmalitaab83da72016-08-26 13:04:14 -0700188 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000189
190 SkString label;
191 label.appendf("%d x %d", w, h);
192 blackPaint.setAntiAlias(true);
193 blackPaint.setStyle(SkPaint::kFill_Style);
194 blackPaint.setTextSize(SK_Scalar1 * 10);
195 SkScalar baseline = dstRect.height() +
196 blackPaint.getTextSize() + SK_Scalar1 * 3;
Cary Clark2a475ea2017-04-28 15:35:12 -0400197 canvas->drawString(label,
bsalomon@google.comfb309512011-11-30 14:13:48 +0000198 0, baseline,
199 blackPaint);
200 blackPaint.setStyle(SkPaint::kStroke_Style);
201 blackPaint.setStrokeWidth(SK_Scalar1);
202 blackPaint.setAntiAlias(false);
203 canvas->drawRect(dstRect, blackPaint);
204
205 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
206 ++rowCount;
207 if ((dstRect.width() + kPadX) * rowCount > gSize) {
208 canvas->restore();
209 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
210 canvas->save();
211 rowCount = 0;
212 }
213 }
214 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000215
216 {
217 // test the following code path:
218 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
219 SkIRect srcRect;
220 SkPaint paint;
221 SkBitmap bm;
222
223 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700224 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000225
226 srcRect.setXYWH(1, 1, 3, 3);
reedefdfd512016-04-04 10:02:58 -0700227 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000228 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000229 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
fmalitaab83da72016-08-26 13:04:14 -0700230 SkBlurMaskFilter::kHighQuality_BlurFlag));
231
232 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
233 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000234 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000235 }
236
237private:
reedf803da12015-01-23 05:58:07 -0800238 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000239};
240
fmalitaab83da72016-08-26 13:04:14 -0700241DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
242DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
243DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
244DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )