blob: ebc768e007527f932da832dda25d4954bbe3a6a4 [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
robertphillips@google.comb7061172013-09-06 14:16:12 +00008#include "SkBlurMask.h"
bsalomon@google.com7d30a212012-04-25 15:52:27 +00009#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000010#include "SkColorPriv.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000011#include "SkGradientShader.h"
reedf803da12015-01-23 05:58:07 -080012#include "SkImage.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080013#include "SkImage_Base.h"
halcanary4dbbd042016-06-07 17:21:10 -070014#include "SkMathPriv.h"
robertphillips24eb7a82015-09-24 08:47:49 -070015#include "SkShader.h"
16#include "SkSurface.h"
Mike Kleinea3f0142019-03-20 11:12:10 -050017#include "ToolUtils.h"
18#include "gm.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000019
bsalomon@google.com7d30a212012-04-25 15:52:27 +000020static SkBitmap make_chessbm(int w, int h) {
21 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000022 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000023
24 for (int y = 0; y < bm.height(); y++) {
25 uint32_t* p = bm.getAddr32(0, y);
26 for (int x = 0; x < bm.width(); x++) {
27 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
28 }
29 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +000030 return bm;
31}
32
bsalomon7cf36cc2016-07-14 09:33:42 -070033// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070034static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070035 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070036
Mike Kleinea3f0142019-03-20 11:12:10 -050037 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070038 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000039
robertphillips24eb7a82015-09-24 08:47:49 -070040 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000041
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) {
reed2ad1aa62016-03-09 09:50:50 -080066 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000067 pt, radius,
68 colors, pos,
69 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040070 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080071 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070072 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000073 rect.inset(wScalar / 8, hScalar / 8);
74 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
75 }
reedf803da12015-01-23 05:58:07 -080076
reed9ce9d672016-03-17 10:51:11 -070077 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070078
79 SkBitmap tempBM;
80
Cary Clark4f5a79c2018-02-07 15:51:00 -050081 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -070082
83 // Let backends know we won't change this, so they don't have to deep copy it defensively.
84 tempBM.setImmutable();
85 *resultBM = tempBM;
86
87 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +000088}
89
fmalitaab83da72016-08-26 13:04:14 -070090static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
91 const SkRect& dstR, const SkPaint* paint) {
92 canvas->drawBitmapRect(bm, srcR, dstR, paint);
93}
94
95static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
96 const SkRect& dstR, const SkPaint* paint) {
97 if (!bm.bounds().contains(srcR)) {
98 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
99 return;
100 }
101
102 SkBitmap subset;
103 if (bm.extractSubset(&subset, srcR)) {
104 canvas->drawBitmapRect(subset, dstR, paint);
105 }
reedf803da12015-01-23 05:58:07 -0800106}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000107
reed84984ef2015-07-17 07:09:43 -0700108static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
fmalitaab83da72016-08-26 13:04:14 -0700109 const SkRect& dstR, const SkPaint* paint) {
110 canvas->drawImageRect(image, srcR, dstR, paint);
reedf803da12015-01-23 05:58:07 -0800111}
112
fmalitaab83da72016-08-26 13:04:14 -0700113static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
114 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
115 if (!image->bounds().contains(srcR)) {
116 imageproc(canvas, image, bm, srcR, dstR, paint);
117 return;
118 }
119
120 if (sk_sp<SkImage> subset = image->makeSubset(srcR)) {
121 canvas->drawImageRect(subset, dstR, paint);
122 }
123}
124
125typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
126 const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800127
mtkleindbfd7ab2016-09-01 11:24:54 -0700128constexpr int gSize = 1024;
129constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800130
131class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000132public:
reedf803da12015-01-23 05:58:07 -0800133 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
134 fName.set("drawbitmaprect");
135 if (suffix) {
136 fName.append(suffix);
137 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000138 }
139
reed9ce9d672016-03-17 10:51:11 -0700140 DrawRectRectProc* fProc;
141 SkBitmap fLargeBitmap;
142 sk_sp<SkImage> fImage;
143 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000144
145protected:
mtklein36352bf2015-03-25 18:17:31 -0700146 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800147
mtklein36352bf2015-03-25 18:17:31 -0700148 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800149
robertphillips24eb7a82015-09-24 08:47:49 -0700150 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700151 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000152 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000153
mtklein36352bf2015-03-25 18:17:31 -0700154 void onDraw(SkCanvas* canvas) override {
Brian Salomon54268502018-05-24 13:22:01 -0400155 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
robertphillips24eb7a82015-09-24 08:47:49 -0700156 this->setupImage(canvas);
157 }
158
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000159 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700160 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000161
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 const int kPadX = 30;
163 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000164 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500165 paint.setAlphaf(0.125f);
bsalomon7cf36cc2016-07-14 09:33:42 -0700166 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000167 canvas->translate(SK_Scalar1 * kPadX / 2,
168 SK_Scalar1 * kPadY / 2);
169 SkPaint blackPaint;
170 SkScalar titleHeight = SK_Scalar1 * 24;
171 blackPaint.setColor(SK_ColorBLACK);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000172 blackPaint.setAntiAlias(true);
Mike Reedc4745d62019-01-07 09:31:58 -0500173
Mike Kleinea3f0142019-03-20 11:12:10 -0500174 SkFont font(ToolUtils::create_portable_typeface(), titleHeight);
Mike Reedc4745d62019-01-07 09:31:58 -0500175
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);
Mike Reedc4745d62019-01-07 09:31:58 -0500178 canvas->drawString(title, 0, titleHeight, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000179
180 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
181 int rowCount = 0;
182 canvas->save();
183 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
184 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
185
reedf803da12015-01-23 05:58:07 -0800186 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
fmalitaab83da72016-08-26 13:04:14 -0700187 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000188
189 SkString label;
190 label.appendf("%d x %d", w, h);
191 blackPaint.setAntiAlias(true);
192 blackPaint.setStyle(SkPaint::kFill_Style);
Mike Reedc4745d62019-01-07 09:31:58 -0500193 font.setSize(SK_Scalar1 * 10);
194 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
195 canvas->drawString(label, 0, baseline, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000196 blackPaint.setStyle(SkPaint::kStroke_Style);
197 blackPaint.setStrokeWidth(SK_Scalar1);
198 blackPaint.setAntiAlias(false);
199 canvas->drawRect(dstRect, blackPaint);
200
201 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
202 ++rowCount;
203 if ((dstRect.width() + kPadX) * rowCount > gSize) {
204 canvas->restore();
205 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
206 canvas->save();
207 rowCount = 0;
208 }
209 }
210 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000211
212 {
213 // test the following code path:
214 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
215 SkIRect srcRect;
216 SkPaint paint;
217 SkBitmap bm;
218
219 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700220 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000221
222 srcRect.setXYWH(1, 1, 3, 3);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400223 paint.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000224 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -0400225 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
fmalitaab83da72016-08-26 13:04:14 -0700226
227 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
228 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000229 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000230 }
231
232private:
reedf803da12015-01-23 05:58:07 -0800233 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000234};
235
fmalitaab83da72016-08-26 13:04:14 -0700236DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
237DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
238DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
239DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )