blob: 1a39aa0c731ee1f0e90558932dd579ccf4554b27 [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040015#include "include/core/SkImageInfo.h"
16#include "include/core/SkMaskFilter.h"
17#include "include/core/SkMatrix.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPoint.h"
20#include "include/core/SkRect.h"
21#include "include/core/SkRefCnt.h"
22#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040024#include "include/core/SkSize.h"
25#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/core/SkTileMode.h"
28#include "include/core/SkTypeface.h"
29#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/effects/SkGradientShader.h"
Adlai Holler872a32c2020-07-10 14:33:22 -040031#include "include/gpu/GrDirectContext.h"
Kevin Lubick1b3aa8b2023-01-19 14:03:31 -050032#include "src/base/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/core/SkBlurMask.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040034#include "tools/GpuToolUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040036#include "tools/fonts/FontToolUtils.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000037
bsalomon@google.com7d30a212012-04-25 15:52:27 +000038static SkBitmap make_chessbm(int w, int h) {
39 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000040 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000041
42 for (int y = 0; y < bm.height(); y++) {
43 uint32_t* p = bm.getAddr32(0, y);
44 for (int x = 0; x < bm.width(); x++) {
45 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
46 }
47 }
Mike Reedd396cd52021-01-23 21:14:47 -050048 bm.setImmutable();
bsalomon@google.com7d30a212012-04-25 15:52:27 +000049 return bm;
50}
51
bsalomon7cf36cc2016-07-14 09:33:42 -070052// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070053static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070054 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070055
Mike Kleinea3f0142019-03-20 11:12:10 -050056 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070057 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000058
robertphillips24eb7a82015-09-24 08:47:49 -070059 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000060
61 SkScalar wScalar = SkIntToScalar(w);
62 SkScalar hScalar = SkIntToScalar(h);
63
64 SkPoint pt = { wScalar / 2, hScalar / 2 };
65
Brian Osman116b33e2020-02-05 13:34:09 -050066 SkScalar radius = 4 * std::max(wScalar, hScalar);
bsalomon@google.comfb309512011-11-30 14:13:48 +000067
68 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
69 SK_ColorGREEN, SK_ColorMAGENTA,
70 SK_ColorBLUE, SK_ColorCYAN,
71 SK_ColorRED};
72
73 SkScalar pos[] = {0,
74 SK_Scalar1 / 6,
75 2 * SK_Scalar1 / 6,
76 3 * SK_Scalar1 / 6,
77 4 * SK_Scalar1 / 6,
78 5 * SK_Scalar1 / 6,
79 SK_Scalar1};
80
81 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000082 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
83 SkMatrix mat = SkMatrix::I();
84 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080085 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000086 pt, radius,
87 colors, pos,
Herb Derbyc37b3862022-06-21 09:49:17 -040088 std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040089 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080090 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070091 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000092 rect.inset(wScalar / 8, hScalar / 8);
93 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
94 }
reedf803da12015-01-23 05:58:07 -080095
reed9ce9d672016-03-17 10:51:11 -070096 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070097
98 SkBitmap tempBM;
99
Cary Clark4f5a79c2018-02-07 15:51:00 -0500100 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -0700101
102 // Let backends know we won't change this, so they don't have to deep copy it defensively.
103 tempBM.setImmutable();
104 *resultBM = tempBM;
105
106 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000107}
108
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400109static void bitmapproc(SkCanvas* canvas, sk_sp<SkImage> image,
110 const SkBitmap&, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500111 const SkRect& dstR, const SkSamplingOptions& sampling,
112 const SkPaint* paint) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400113 canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
Mike Reedd396cd52021-01-23 21:14:47 -0500114 SkCanvas::kStrict_SrcRectConstraint);
fmalitaab83da72016-08-26 13:04:14 -0700115}
116
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400117static void bitmapsubsetproc(SkCanvas* canvas, sk_sp<SkImage> image,
118 const SkBitmap& bm, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500119 const SkRect& dstR, const SkSamplingOptions& sampling,
120 const SkPaint* paint) {
fmalitaab83da72016-08-26 13:04:14 -0700121 if (!bm.bounds().contains(srcR)) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400122 bitmapproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700123 return;
124 }
125
126 SkBitmap subset;
127 if (bm.extractSubset(&subset, srcR)) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400128 sk_sp<SkImage> subsetImg = ToolUtils::MakeTextureImage(canvas, subset.asImage());
129 canvas->drawImageRect(subsetImg, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700130 }
reedf803da12015-01-23 05:58:07 -0800131}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000132
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400133static void imageproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap&, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500134 const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400135 sk_sp<SkImage> tmp = ToolUtils::MakeTextureImage(canvas, std::move(image));
136 canvas->drawImageRect(tmp, SkRect::Make(srcR), dstR, sampling, paint,
Mike Reedd396cd52021-01-23 21:14:47 -0500137 SkCanvas::kStrict_SrcRectConstraint);
reedf803da12015-01-23 05:58:07 -0800138}
139
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400140static void imagesubsetproc(SkCanvas* canvas, sk_sp<SkImage> image, const SkBitmap& bm,
Mike Reedd396cd52021-01-23 21:14:47 -0500141 const SkIRect& srcR, const SkRect& dstR,
142 const SkSamplingOptions& sampling, const SkPaint* paint) {
Kevin Lubick77472bf2023-03-24 07:11:17 -0400143 SkASSERT_RELEASE(image);
fmalitaab83da72016-08-26 13:04:14 -0700144 if (!image->bounds().contains(srcR)) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400145 imageproc(canvas, std::move(image), bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700146 return;
147 }
148
Adlai Holler872a32c2020-07-10 14:33:22 -0400149 auto direct = GrAsDirectContext(canvas->recordingContext());
Kevin Lubick14d319b2023-04-27 15:43:07 -0400150 if (sk_sp<SkImage> subset = image->makeSubset(direct, srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500151 canvas->drawImageRect(subset, dstR, sampling, paint);
Robert Phillips01f3be42023-06-16 13:22:18 -0400152 return;
fmalitaab83da72016-08-26 13:04:14 -0700153 }
Kevin Lubick0f7b44e2023-02-28 09:13:11 -0500154#if defined(SK_GRAPHITE)
Kevin Lubick14d319b2023-04-27 15:43:07 -0400155 if (sk_sp<SkImage> subset = image->makeSubset(canvas->recorder(), srcR, {})) {
Robert Phillips6f8ecbe2023-01-13 15:43:40 -0500156 canvas->drawImageRect(subset, dstR, sampling, paint);
157 }
158#endif
fmalitaab83da72016-08-26 13:04:14 -0700159}
160
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400161typedef void DrawRectRectProc(SkCanvas*, sk_sp<SkImage>, const SkBitmap&,
162 const SkIRect& srcR, const SkRect& dstR,
Mike Reedd396cd52021-01-23 21:14:47 -0500163 const SkSamplingOptions&, const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800164
mtkleindbfd7ab2016-09-01 11:24:54 -0700165constexpr int gSize = 1024;
166constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800167
168class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000169public:
reedf803da12015-01-23 05:58:07 -0800170 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
171 fName.set("drawbitmaprect");
172 if (suffix) {
173 fName.append(suffix);
174 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000175 }
176
reed9ce9d672016-03-17 10:51:11 -0700177 DrawRectRectProc* fProc;
178 SkBitmap fLargeBitmap;
179 sk_sp<SkImage> fImage;
180 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000181
182protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000183 SkString getName() const override { return fName; }
reedf803da12015-01-23 05:58:07 -0800184
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000185 SkISize getISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800186
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400187 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400188 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400189 fImage = ToolUtils::MakeTextureImage(canvas,
190 makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize));
191 if (!fImage) {
192 *errorMsg = "Image creation failed";
193 return DrawResult::kSkip;
194 }
robertphillips24eb7a82015-09-24 08:47:49 -0700195 }
196
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000197 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700198 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000199
mtkleindbfd7ab2016-09-01 11:24:54 -0700200 const int kPadX = 30;
201 const int kPadY = 40;
John Stilesacf71642021-08-12 22:33:57 -0400202 SkPaint alphaPaint;
203 alphaPaint.setAlphaf(0.125f);
204 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(),
205 &alphaPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000206 canvas->translate(SK_Scalar1 * kPadX / 2,
207 SK_Scalar1 * kPadY / 2);
208 SkPaint blackPaint;
209 SkScalar titleHeight = SK_Scalar1 * 24;
210 blackPaint.setColor(SK_ColorBLACK);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000211 blackPaint.setAntiAlias(true);
Mike Reedc4745d62019-01-07 09:31:58 -0500212
Kevin Lubicke836c3a2023-10-20 06:55:35 -0400213 SkFont font(ToolUtils::DefaultPortableTypeface(), titleHeight);
Mike Reedc4745d62019-01-07 09:31:58 -0500214
bsalomon@google.comfb309512011-11-30 14:13:48 +0000215 SkString title;
reedf803da12015-01-23 05:58:07 -0800216 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
Mike Reedc4745d62019-01-07 09:31:58 -0500217 canvas->drawString(title, 0, titleHeight, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000218
219 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
220 int rowCount = 0;
221 canvas->save();
222 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
223 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
224
reedf803da12015-01-23 05:58:07 -0800225 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400226 fProc(canvas, fImage, fLargeBitmap, srcRect, dstRect, SkSamplingOptions(),
Mike Reedd396cd52021-01-23 21:14:47 -0500227 nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000228
229 SkString label;
230 label.appendf("%d x %d", w, h);
231 blackPaint.setAntiAlias(true);
232 blackPaint.setStyle(SkPaint::kFill_Style);
Mike Reedc4745d62019-01-07 09:31:58 -0500233 font.setSize(SK_Scalar1 * 10);
234 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
235 canvas->drawString(label, 0, baseline, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000236 blackPaint.setStyle(SkPaint::kStroke_Style);
237 blackPaint.setStrokeWidth(SK_Scalar1);
238 blackPaint.setAntiAlias(false);
239 canvas->drawRect(dstRect, blackPaint);
240
241 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
242 ++rowCount;
243 if ((dstRect.width() + kPadX) * rowCount > gSize) {
244 canvas->restore();
245 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
246 canvas->save();
247 rowCount = 0;
248 }
249 }
250 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000251
252 {
253 // test the following code path:
254 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
255 SkIRect srcRect;
John Stilesacf71642021-08-12 22:33:57 -0400256 SkPaint maskPaint;
Mike Reedd396cd52021-01-23 21:14:47 -0500257 SkBitmap bm = make_chessbm(5, 5);
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400258 sk_sp<SkImage> img = ToolUtils::MakeTextureImage(canvas, bm.asImage());
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000259
260 srcRect.setXYWH(1, 1, 3, 3);
John Stilesacf71642021-08-12 22:33:57 -0400261 maskPaint.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000262 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -0400263 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
fmalitaab83da72016-08-26 13:04:14 -0700264
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400265 fProc(canvas, img, bm, srcRect, dstRect,
John Stilesacf71642021-08-12 22:33:57 -0400266 SkSamplingOptions(SkFilterMode::kLinear), &maskPaint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000267 }
Robert Phillips0fb10ab2022-04-20 14:57:03 -0400268
269 return DrawResult::kOk;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000270 }
271
272private:
John Stiles7571f9e2020-09-02 22:42:33 -0400273 using INHERITED = skiagm::GM;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000274};
275
fmalitaab83da72016-08-26 13:04:14 -0700276DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
277DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
278DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
279DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )