blob: 0bda495e198490f837f5e5d53ec5f294cc3412eb [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
10#include "include/core/SkBlurTypes.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFilterQuality.h"
14#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040016#include "include/core/SkImageInfo.h"
17#include "include/core/SkMaskFilter.h"
18#include "include/core/SkMatrix.h"
19#include "include/core/SkPaint.h"
20#include "include/core/SkPoint.h"
21#include "include/core/SkRect.h"
22#include "include/core/SkRefCnt.h"
23#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/core/SkSize.h"
26#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "include/core/SkTileMode.h"
29#include "include/core/SkTypeface.h"
30#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "include/effects/SkGradientShader.h"
Adlai Holler872a32c2020-07-10 14:33:22 -040032#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkBlurMask.h"
34#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "tools/ToolUtils.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000036
bsalomon@google.com7d30a212012-04-25 15:52:27 +000037static SkBitmap make_chessbm(int w, int h) {
38 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000039 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000040
41 for (int y = 0; y < bm.height(); y++) {
42 uint32_t* p = bm.getAddr32(0, y);
43 for (int x = 0; x < bm.width(); x++) {
44 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
45 }
46 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +000047 return bm;
48}
49
bsalomon7cf36cc2016-07-14 09:33:42 -070050// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070051static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070052 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070053
Mike Kleinea3f0142019-03-20 11:12:10 -050054 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070055 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000056
robertphillips24eb7a82015-09-24 08:47:49 -070057 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000058
59 SkScalar wScalar = SkIntToScalar(w);
60 SkScalar hScalar = SkIntToScalar(h);
61
62 SkPoint pt = { wScalar / 2, hScalar / 2 };
63
Brian Osman116b33e2020-02-05 13:34:09 -050064 SkScalar radius = 4 * std::max(wScalar, hScalar);
bsalomon@google.comfb309512011-11-30 14:13:48 +000065
66 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
67 SK_ColorGREEN, SK_ColorMAGENTA,
68 SK_ColorBLUE, SK_ColorCYAN,
69 SK_ColorRED};
70
71 SkScalar pos[] = {0,
72 SK_Scalar1 / 6,
73 2 * SK_Scalar1 / 6,
74 3 * SK_Scalar1 / 6,
75 4 * SK_Scalar1 / 6,
76 5 * SK_Scalar1 / 6,
77 SK_Scalar1};
78
79 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000080 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
81 SkMatrix mat = SkMatrix::I();
82 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080083 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000084 pt, radius,
85 colors, pos,
86 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040087 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080088 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070089 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000090 rect.inset(wScalar / 8, hScalar / 8);
91 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
92 }
reedf803da12015-01-23 05:58:07 -080093
reed9ce9d672016-03-17 10:51:11 -070094 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070095
96 SkBitmap tempBM;
97
Cary Clark4f5a79c2018-02-07 15:51:00 -050098 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -070099
100 // Let backends know we won't change this, so they don't have to deep copy it defensively.
101 tempBM.setImmutable();
102 *resultBM = tempBM;
103
104 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000105}
106
fmalitaab83da72016-08-26 13:04:14 -0700107static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
108 const SkRect& dstR, const SkPaint* paint) {
109 canvas->drawBitmapRect(bm, srcR, dstR, paint);
110}
111
112static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
113 const SkRect& dstR, const SkPaint* paint) {
114 if (!bm.bounds().contains(srcR)) {
115 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
116 return;
117 }
118
119 SkBitmap subset;
120 if (bm.extractSubset(&subset, srcR)) {
121 canvas->drawBitmapRect(subset, dstR, paint);
122 }
reedf803da12015-01-23 05:58:07 -0800123}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000124
reed84984ef2015-07-17 07:09:43 -0700125static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
fmalitaab83da72016-08-26 13:04:14 -0700126 const SkRect& dstR, const SkPaint* paint) {
127 canvas->drawImageRect(image, srcR, dstR, paint);
reedf803da12015-01-23 05:58:07 -0800128}
129
fmalitaab83da72016-08-26 13:04:14 -0700130static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
131 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
132 if (!image->bounds().contains(srcR)) {
133 imageproc(canvas, image, bm, srcR, dstR, paint);
134 return;
135 }
136
Adlai Holler872a32c2020-07-10 14:33:22 -0400137 auto direct = GrAsDirectContext(canvas->recordingContext());
138 if (sk_sp<SkImage> subset = image->makeSubset(srcR, direct)) {
fmalitaab83da72016-08-26 13:04:14 -0700139 canvas->drawImageRect(subset, dstR, paint);
140 }
141}
142
143typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
144 const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800145
mtkleindbfd7ab2016-09-01 11:24:54 -0700146constexpr int gSize = 1024;
147constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800148
149class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000150public:
reedf803da12015-01-23 05:58:07 -0800151 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
152 fName.set("drawbitmaprect");
153 if (suffix) {
154 fName.append(suffix);
155 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000156 }
157
reed9ce9d672016-03-17 10:51:11 -0700158 DrawRectRectProc* fProc;
159 SkBitmap fLargeBitmap;
160 sk_sp<SkImage> fImage;
161 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000162
163protected:
mtklein36352bf2015-03-25 18:17:31 -0700164 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800165
mtklein36352bf2015-03-25 18:17:31 -0700166 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800167
robertphillips24eb7a82015-09-24 08:47:49 -0700168 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700169 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000170 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171
mtklein36352bf2015-03-25 18:17:31 -0700172 void onDraw(SkCanvas* canvas) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400173 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
robertphillips24eb7a82015-09-24 08:47:49 -0700174 this->setupImage(canvas);
175 }
176
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000177 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700178 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000179
mtkleindbfd7ab2016-09-01 11:24:54 -0700180 const int kPadX = 30;
181 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000182 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500183 paint.setAlphaf(0.125f);
bsalomon7cf36cc2016-07-14 09:33:42 -0700184 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000185 canvas->translate(SK_Scalar1 * kPadX / 2,
186 SK_Scalar1 * kPadY / 2);
187 SkPaint blackPaint;
188 SkScalar titleHeight = SK_Scalar1 * 24;
189 blackPaint.setColor(SK_ColorBLACK);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000190 blackPaint.setAntiAlias(true);
Mike Reedc4745d62019-01-07 09:31:58 -0500191
Mike Kleinea3f0142019-03-20 11:12:10 -0500192 SkFont font(ToolUtils::create_portable_typeface(), titleHeight);
Mike Reedc4745d62019-01-07 09:31:58 -0500193
bsalomon@google.comfb309512011-11-30 14:13:48 +0000194 SkString title;
reedf803da12015-01-23 05:58:07 -0800195 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
Mike Reedc4745d62019-01-07 09:31:58 -0500196 canvas->drawString(title, 0, titleHeight, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000197
198 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
199 int rowCount = 0;
200 canvas->save();
201 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
202 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
203
reedf803da12015-01-23 05:58:07 -0800204 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
fmalitaab83da72016-08-26 13:04:14 -0700205 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000206
207 SkString label;
208 label.appendf("%d x %d", w, h);
209 blackPaint.setAntiAlias(true);
210 blackPaint.setStyle(SkPaint::kFill_Style);
Mike Reedc4745d62019-01-07 09:31:58 -0500211 font.setSize(SK_Scalar1 * 10);
212 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
213 canvas->drawString(label, 0, baseline, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000214 blackPaint.setStyle(SkPaint::kStroke_Style);
215 blackPaint.setStrokeWidth(SK_Scalar1);
216 blackPaint.setAntiAlias(false);
217 canvas->drawRect(dstRect, blackPaint);
218
219 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
220 ++rowCount;
221 if ((dstRect.width() + kPadX) * rowCount > gSize) {
222 canvas->restore();
223 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
224 canvas->save();
225 rowCount = 0;
226 }
227 }
228 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000229
230 {
231 // test the following code path:
232 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
233 SkIRect srcRect;
234 SkPaint paint;
235 SkBitmap bm;
236
237 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700238 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000239
240 srcRect.setXYWH(1, 1, 3, 3);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400241 paint.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000242 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -0400243 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
fmalitaab83da72016-08-26 13:04:14 -0700244
245 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
246 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000247 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000248 }
249
250private:
John Stiles7571f9e2020-09-02 22:42:33 -0400251 using INHERITED = skiagm::GM;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000252};
253
fmalitaab83da72016-08-26 13:04:14 -0700254DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
255DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
256DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
257DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )