blob: d4a0a59ffae976faf7c503b68bf44e7d2c8be4ff [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 }
Mike Reedd396cd52021-01-23 21:14:47 -050047 bm.setImmutable();
bsalomon@google.com7d30a212012-04-25 15:52:27 +000048 return bm;
49}
50
bsalomon7cf36cc2016-07-14 09:33:42 -070051// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070052static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070053 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070054
Mike Kleinea3f0142019-03-20 11:12:10 -050055 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070056 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000057
robertphillips24eb7a82015-09-24 08:47:49 -070058 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000059
60 SkScalar wScalar = SkIntToScalar(w);
61 SkScalar hScalar = SkIntToScalar(h);
62
63 SkPoint pt = { wScalar / 2, hScalar / 2 };
64
Brian Osman116b33e2020-02-05 13:34:09 -050065 SkScalar radius = 4 * std::max(wScalar, hScalar);
bsalomon@google.comfb309512011-11-30 14:13:48 +000066
67 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
68 SK_ColorGREEN, SK_ColorMAGENTA,
69 SK_ColorBLUE, SK_ColorCYAN,
70 SK_ColorRED};
71
72 SkScalar pos[] = {0,
73 SK_Scalar1 / 6,
74 2 * SK_Scalar1 / 6,
75 3 * SK_Scalar1 / 6,
76 4 * SK_Scalar1 / 6,
77 5 * SK_Scalar1 / 6,
78 SK_Scalar1};
79
80 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000081 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
82 SkMatrix mat = SkMatrix::I();
83 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080084 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000085 pt, radius,
86 colors, pos,
87 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040088 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080089 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070090 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000091 rect.inset(wScalar / 8, hScalar / 8);
92 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
93 }
reedf803da12015-01-23 05:58:07 -080094
reed9ce9d672016-03-17 10:51:11 -070095 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070096
97 SkBitmap tempBM;
98
Cary Clark4f5a79c2018-02-07 15:51:00 -050099 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -0700100
101 // Let backends know we won't change this, so they don't have to deep copy it defensively.
102 tempBM.setImmutable();
103 *resultBM = tempBM;
104
105 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000106}
107
fmalitaab83da72016-08-26 13:04:14 -0700108static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500109 const SkRect& dstR, const SkSamplingOptions& sampling,
110 const SkPaint* paint) {
111 canvas->drawImageRect(bm.asImage(), SkRect::Make(srcR), dstR, sampling, paint,
112 SkCanvas::kStrict_SrcRectConstraint);
fmalitaab83da72016-08-26 13:04:14 -0700113}
114
115static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500116 const SkRect& dstR, const SkSamplingOptions& sampling,
117 const SkPaint* paint) {
fmalitaab83da72016-08-26 13:04:14 -0700118 if (!bm.bounds().contains(srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500119 bitmapproc(canvas, nullptr, bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700120 return;
121 }
122
123 SkBitmap subset;
124 if (bm.extractSubset(&subset, srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500125 canvas->drawImageRect(subset.asImage(), dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700126 }
reedf803da12015-01-23 05:58:07 -0800127}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000128
reed84984ef2015-07-17 07:09:43 -0700129static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500130 const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
131 canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
132 SkCanvas::kStrict_SrcRectConstraint);
reedf803da12015-01-23 05:58:07 -0800133}
134
fmalitaab83da72016-08-26 13:04:14 -0700135static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
Mike Reedd396cd52021-01-23 21:14:47 -0500136 const SkIRect& srcR, const SkRect& dstR,
137 const SkSamplingOptions& sampling, const SkPaint* paint) {
fmalitaab83da72016-08-26 13:04:14 -0700138 if (!image->bounds().contains(srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500139 imageproc(canvas, image, bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700140 return;
141 }
142
Adlai Holler872a32c2020-07-10 14:33:22 -0400143 auto direct = GrAsDirectContext(canvas->recordingContext());
144 if (sk_sp<SkImage> subset = image->makeSubset(srcR, direct)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500145 canvas->drawImageRect(subset, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700146 }
147}
148
149typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
Mike Reedd396cd52021-01-23 21:14:47 -0500150 const SkSamplingOptions&, const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800151
mtkleindbfd7ab2016-09-01 11:24:54 -0700152constexpr int gSize = 1024;
153constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800154
155class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000156public:
reedf803da12015-01-23 05:58:07 -0800157 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
158 fName.set("drawbitmaprect");
159 if (suffix) {
160 fName.append(suffix);
161 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000162 }
163
reed9ce9d672016-03-17 10:51:11 -0700164 DrawRectRectProc* fProc;
165 SkBitmap fLargeBitmap;
166 sk_sp<SkImage> fImage;
167 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000168
169protected:
mtklein36352bf2015-03-25 18:17:31 -0700170 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800171
mtklein36352bf2015-03-25 18:17:31 -0700172 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800173
robertphillips24eb7a82015-09-24 08:47:49 -0700174 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700175 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000176 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177
mtklein36352bf2015-03-25 18:17:31 -0700178 void onDraw(SkCanvas* canvas) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400179 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
robertphillips24eb7a82015-09-24 08:47:49 -0700180 this->setupImage(canvas);
181 }
182
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000183 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700184 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000185
mtkleindbfd7ab2016-09-01 11:24:54 -0700186 const int kPadX = 30;
187 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000188 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500189 paint.setAlphaf(0.125f);
Mike Reedd396cd52021-01-23 21:14:47 -0500190 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000191 canvas->translate(SK_Scalar1 * kPadX / 2,
192 SK_Scalar1 * kPadY / 2);
193 SkPaint blackPaint;
194 SkScalar titleHeight = SK_Scalar1 * 24;
195 blackPaint.setColor(SK_ColorBLACK);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000196 blackPaint.setAntiAlias(true);
Mike Reedc4745d62019-01-07 09:31:58 -0500197
Mike Kleinea3f0142019-03-20 11:12:10 -0500198 SkFont font(ToolUtils::create_portable_typeface(), titleHeight);
Mike Reedc4745d62019-01-07 09:31:58 -0500199
bsalomon@google.comfb309512011-11-30 14:13:48 +0000200 SkString title;
reedf803da12015-01-23 05:58:07 -0800201 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
Mike Reedc4745d62019-01-07 09:31:58 -0500202 canvas->drawString(title, 0, titleHeight, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000203
204 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
205 int rowCount = 0;
206 canvas->save();
207 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
208 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
209
reedf803da12015-01-23 05:58:07 -0800210 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
Mike Reedd396cd52021-01-23 21:14:47 -0500211 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, SkSamplingOptions(),
212 nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000213
214 SkString label;
215 label.appendf("%d x %d", w, h);
216 blackPaint.setAntiAlias(true);
217 blackPaint.setStyle(SkPaint::kFill_Style);
Mike Reedc4745d62019-01-07 09:31:58 -0500218 font.setSize(SK_Scalar1 * 10);
219 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
220 canvas->drawString(label, 0, baseline, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000221 blackPaint.setStyle(SkPaint::kStroke_Style);
222 blackPaint.setStrokeWidth(SK_Scalar1);
223 blackPaint.setAntiAlias(false);
224 canvas->drawRect(dstRect, blackPaint);
225
226 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
227 ++rowCount;
228 if ((dstRect.width() + kPadX) * rowCount > gSize) {
229 canvas->restore();
230 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
231 canvas->save();
232 rowCount = 0;
233 }
234 }
235 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000236
237 {
238 // test the following code path:
239 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
240 SkIRect srcRect;
241 SkPaint paint;
Mike Reedd396cd52021-01-23 21:14:47 -0500242 SkBitmap bm = make_chessbm(5, 5);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000243
244 srcRect.setXYWH(1, 1, 3, 3);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400245 paint.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000246 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -0400247 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
fmalitaab83da72016-08-26 13:04:14 -0700248
Mike Reedd396cd52021-01-23 21:14:47 -0500249 fProc(canvas, bm.asImage().get(), bm, srcRect, dstRect,
250 SkSamplingOptions(SkFilterMode::kLinear), &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000251 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000252 }
253
254private:
John Stiles7571f9e2020-09-02 22:42:33 -0400255 using INHERITED = skiagm::GM;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000256};
257
fmalitaab83da72016-08-26 13:04:14 -0700258DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
259DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
260DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
261DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )