blob: d160274639791b3447e17701f3184887e4748c76 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/core/SkBlurMask.h"
33#include "src/core/SkMathPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "tools/ToolUtils.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000035
bsalomon@google.com7d30a212012-04-25 15:52:27 +000036static SkBitmap make_chessbm(int w, int h) {
37 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000038 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000039
40 for (int y = 0; y < bm.height(); y++) {
41 uint32_t* p = bm.getAddr32(0, y);
42 for (int x = 0; x < bm.width(); x++) {
43 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
44 }
45 }
Mike Reedd396cd52021-01-23 21:14:47 -050046 bm.setImmutable();
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,
Mike Reedd396cd52021-01-23 21:14:47 -0500108 const SkRect& dstR, const SkSamplingOptions& sampling,
109 const SkPaint* paint) {
110 canvas->drawImageRect(bm.asImage(), SkRect::Make(srcR), dstR, sampling, paint,
111 SkCanvas::kStrict_SrcRectConstraint);
fmalitaab83da72016-08-26 13:04:14 -0700112}
113
114static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500115 const SkRect& dstR, const SkSamplingOptions& sampling,
116 const SkPaint* paint) {
fmalitaab83da72016-08-26 13:04:14 -0700117 if (!bm.bounds().contains(srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500118 bitmapproc(canvas, nullptr, bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700119 return;
120 }
121
122 SkBitmap subset;
123 if (bm.extractSubset(&subset, srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500124 canvas->drawImageRect(subset.asImage(), dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700125 }
reedf803da12015-01-23 05:58:07 -0800126}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000127
reed84984ef2015-07-17 07:09:43 -0700128static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
Mike Reedd396cd52021-01-23 21:14:47 -0500129 const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
130 canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
131 SkCanvas::kStrict_SrcRectConstraint);
reedf803da12015-01-23 05:58:07 -0800132}
133
fmalitaab83da72016-08-26 13:04:14 -0700134static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
Mike Reedd396cd52021-01-23 21:14:47 -0500135 const SkIRect& srcR, const SkRect& dstR,
136 const SkSamplingOptions& sampling, const SkPaint* paint) {
fmalitaab83da72016-08-26 13:04:14 -0700137 if (!image->bounds().contains(srcR)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500138 imageproc(canvas, image, bm, srcR, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700139 return;
140 }
141
Adlai Holler872a32c2020-07-10 14:33:22 -0400142 auto direct = GrAsDirectContext(canvas->recordingContext());
143 if (sk_sp<SkImage> subset = image->makeSubset(srcR, direct)) {
Mike Reedd396cd52021-01-23 21:14:47 -0500144 canvas->drawImageRect(subset, dstR, sampling, paint);
fmalitaab83da72016-08-26 13:04:14 -0700145 }
146}
147
148typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
Mike Reedd396cd52021-01-23 21:14:47 -0500149 const SkSamplingOptions&, const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800150
mtkleindbfd7ab2016-09-01 11:24:54 -0700151constexpr int gSize = 1024;
152constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800153
154class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000155public:
reedf803da12015-01-23 05:58:07 -0800156 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
157 fName.set("drawbitmaprect");
158 if (suffix) {
159 fName.append(suffix);
160 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000161 }
162
reed9ce9d672016-03-17 10:51:11 -0700163 DrawRectRectProc* fProc;
164 SkBitmap fLargeBitmap;
165 sk_sp<SkImage> fImage;
166 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000167
168protected:
mtklein36352bf2015-03-25 18:17:31 -0700169 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800170
mtklein36352bf2015-03-25 18:17:31 -0700171 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800172
robertphillips24eb7a82015-09-24 08:47:49 -0700173 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700174 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000175 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 void onDraw(SkCanvas* canvas) override {
Robert Phillips4a3ebc22020-07-10 11:27:43 -0400178 if (!fImage || !fImage->isValid(canvas->recordingContext())) {
robertphillips24eb7a82015-09-24 08:47:49 -0700179 this->setupImage(canvas);
180 }
181
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000182 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700183 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000184
mtkleindbfd7ab2016-09-01 11:24:54 -0700185 const int kPadX = 30;
186 const int kPadY = 40;
John Stilesacf71642021-08-12 22:33:57 -0400187 SkPaint alphaPaint;
188 alphaPaint.setAlphaf(0.125f);
189 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(),
190 &alphaPaint);
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;
John Stilesacf71642021-08-12 22:33:57 -0400241 SkPaint maskPaint;
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);
John Stilesacf71642021-08-12 22:33:57 -0400245 maskPaint.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,
John Stilesacf71642021-08-12 22:33:57 -0400250 SkSamplingOptions(SkFilterMode::kLinear), &maskPaint);
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"); )