blob: e4bfac820aaa7d28e3464a0689e08f4d1b6fb69e [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"
32#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 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +000046 return bm;
47}
48
bsalomon7cf36cc2016-07-14 09:33:42 -070049// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070050static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070051 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070052
Mike Kleinea3f0142019-03-20 11:12:10 -050053 auto surface(ToolUtils::makeSurface(origCanvas, info));
robertphillips24eb7a82015-09-24 08:47:49 -070054 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000055
robertphillips24eb7a82015-09-24 08:47:49 -070056 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000057
58 SkScalar wScalar = SkIntToScalar(w);
59 SkScalar hScalar = SkIntToScalar(h);
60
61 SkPoint pt = { wScalar / 2, hScalar / 2 };
62
63 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
64
65 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
66 SK_ColorGREEN, SK_ColorMAGENTA,
67 SK_ColorBLUE, SK_ColorCYAN,
68 SK_ColorRED};
69
70 SkScalar pos[] = {0,
71 SK_Scalar1 / 6,
72 2 * SK_Scalar1 / 6,
73 3 * SK_Scalar1 / 6,
74 4 * SK_Scalar1 / 6,
75 5 * SK_Scalar1 / 6,
76 SK_Scalar1};
77
78 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000079 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
80 SkMatrix mat = SkMatrix::I();
81 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080082 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000083 pt, radius,
84 colors, pos,
85 SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040086 SkTileMode::kRepeat,
reed2ad1aa62016-03-09 09:50:50 -080087 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070088 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000089 rect.inset(wScalar / 8, hScalar / 8);
90 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
91 }
reedf803da12015-01-23 05:58:07 -080092
reed9ce9d672016-03-17 10:51:11 -070093 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070094
95 SkBitmap tempBM;
96
Cary Clark4f5a79c2018-02-07 15:51:00 -050097 image->asLegacyBitmap(&tempBM);
robertphillips24eb7a82015-09-24 08:47:49 -070098
99 // Let backends know we won't change this, so they don't have to deep copy it defensively.
100 tempBM.setImmutable();
101 *resultBM = tempBM;
102
103 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000104}
105
fmalitaab83da72016-08-26 13:04:14 -0700106static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
107 const SkRect& dstR, const SkPaint* paint) {
108 canvas->drawBitmapRect(bm, srcR, dstR, paint);
109}
110
111static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
112 const SkRect& dstR, const SkPaint* paint) {
113 if (!bm.bounds().contains(srcR)) {
114 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
115 return;
116 }
117
118 SkBitmap subset;
119 if (bm.extractSubset(&subset, srcR)) {
120 canvas->drawBitmapRect(subset, dstR, paint);
121 }
reedf803da12015-01-23 05:58:07 -0800122}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000123
reed84984ef2015-07-17 07:09:43 -0700124static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
fmalitaab83da72016-08-26 13:04:14 -0700125 const SkRect& dstR, const SkPaint* paint) {
126 canvas->drawImageRect(image, srcR, dstR, paint);
reedf803da12015-01-23 05:58:07 -0800127}
128
fmalitaab83da72016-08-26 13:04:14 -0700129static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
130 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
131 if (!image->bounds().contains(srcR)) {
132 imageproc(canvas, image, bm, srcR, dstR, paint);
133 return;
134 }
135
136 if (sk_sp<SkImage> subset = image->makeSubset(srcR)) {
137 canvas->drawImageRect(subset, dstR, paint);
138 }
139}
140
141typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
142 const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800143
mtkleindbfd7ab2016-09-01 11:24:54 -0700144constexpr int gSize = 1024;
145constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800146
147class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000148public:
reedf803da12015-01-23 05:58:07 -0800149 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
150 fName.set("drawbitmaprect");
151 if (suffix) {
152 fName.append(suffix);
153 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000154 }
155
reed9ce9d672016-03-17 10:51:11 -0700156 DrawRectRectProc* fProc;
157 SkBitmap fLargeBitmap;
158 sk_sp<SkImage> fImage;
159 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000160
161protected:
mtklein36352bf2015-03-25 18:17:31 -0700162 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800163
mtklein36352bf2015-03-25 18:17:31 -0700164 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800165
robertphillips24eb7a82015-09-24 08:47:49 -0700166 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700167 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000168 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169
mtklein36352bf2015-03-25 18:17:31 -0700170 void onDraw(SkCanvas* canvas) override {
Brian Salomon54268502018-05-24 13:22:01 -0400171 if (!fImage || !fImage->isValid(canvas->getGrContext())) {
robertphillips24eb7a82015-09-24 08:47:49 -0700172 this->setupImage(canvas);
173 }
174
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000175 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700176 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000177
mtkleindbfd7ab2016-09-01 11:24:54 -0700178 const int kPadX = 30;
179 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000180 SkPaint paint;
Mike Reed9407e242019-02-15 16:13:57 -0500181 paint.setAlphaf(0.125f);
bsalomon7cf36cc2016-07-14 09:33:42 -0700182 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000183 canvas->translate(SK_Scalar1 * kPadX / 2,
184 SK_Scalar1 * kPadY / 2);
185 SkPaint blackPaint;
186 SkScalar titleHeight = SK_Scalar1 * 24;
187 blackPaint.setColor(SK_ColorBLACK);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000188 blackPaint.setAntiAlias(true);
Mike Reedc4745d62019-01-07 09:31:58 -0500189
Mike Kleinea3f0142019-03-20 11:12:10 -0500190 SkFont font(ToolUtils::create_portable_typeface(), titleHeight);
Mike Reedc4745d62019-01-07 09:31:58 -0500191
bsalomon@google.comfb309512011-11-30 14:13:48 +0000192 SkString title;
reedf803da12015-01-23 05:58:07 -0800193 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
Mike Reedc4745d62019-01-07 09:31:58 -0500194 canvas->drawString(title, 0, titleHeight, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000195
196 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
197 int rowCount = 0;
198 canvas->save();
199 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
200 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
201
reedf803da12015-01-23 05:58:07 -0800202 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
fmalitaab83da72016-08-26 13:04:14 -0700203 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000204
205 SkString label;
206 label.appendf("%d x %d", w, h);
207 blackPaint.setAntiAlias(true);
208 blackPaint.setStyle(SkPaint::kFill_Style);
Mike Reedc4745d62019-01-07 09:31:58 -0500209 font.setSize(SK_Scalar1 * 10);
210 SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
211 canvas->drawString(label, 0, baseline, font, blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000212 blackPaint.setStyle(SkPaint::kStroke_Style);
213 blackPaint.setStrokeWidth(SK_Scalar1);
214 blackPaint.setAntiAlias(false);
215 canvas->drawRect(dstRect, blackPaint);
216
217 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
218 ++rowCount;
219 if ((dstRect.width() + kPadX) * rowCount > gSize) {
220 canvas->restore();
221 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
222 canvas->save();
223 rowCount = 0;
224 }
225 }
226 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000227
228 {
229 // test the following code path:
230 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
231 SkIRect srcRect;
232 SkPaint paint;
233 SkBitmap bm;
234
235 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700236 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000237
238 srcRect.setXYWH(1, 1, 3, 3);
Mike Reed1be1f8d2018-03-14 13:01:17 -0400239 paint.setMaskFilter(SkMaskFilter::MakeBlur(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000240 kNormal_SkBlurStyle,
Mike Reed1be1f8d2018-03-14 13:01:17 -0400241 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
fmalitaab83da72016-08-26 13:04:14 -0700242
243 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
244 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000245 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000246 }
247
248private:
reedf803da12015-01-23 05:58:07 -0800249 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000250};
251
fmalitaab83da72016-08-26 13:04:14 -0700252DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
253DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
254DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
255DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )