blob: 3c873e290d6cc5aea4a7290cbc57890507b5d39e [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
bsalomon@google.comfb309512011-11-30 14:13:48 +00008#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
bsalomon@google.com7d30a212012-04-25 15:52:27 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkColorPriv.h"
bsalomon@google.comfb309512011-11-30 14:13:48 +000012#include "SkGradientShader.h"
reedf803da12015-01-23 05:58:07 -080013#include "SkImage.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080014#include "SkImage_Base.h"
halcanary4dbbd042016-06-07 17:21:10 -070015#include "SkMathPriv.h"
robertphillips24eb7a82015-09-24 08:47:49 -070016#include "SkShader.h"
17#include "SkSurface.h"
18
bsalomon@google.comfb309512011-11-30 14:13:48 +000019
bsalomon@google.com7d30a212012-04-25 15:52:27 +000020static SkBitmap make_chessbm(int w, int h) {
21 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000022 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000023
24 for (int y = 0; y < bm.height(); y++) {
25 uint32_t* p = bm.getAddr32(0, y);
26 for (int x = 0; x < bm.width(); x++) {
27 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
28 }
29 }
30 bm.unlockPixels();
31 return bm;
32}
33
bsalomon7cf36cc2016-07-14 09:33:42 -070034// Creates a bitmap and a matching image.
reed9ce9d672016-03-17 10:51:11 -070035static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070036 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070037
reede8f30622016-03-23 18:59:25 -070038 auto surface(origCanvas->makeSurface(info));
robertphillips24eb7a82015-09-24 08:47:49 -070039 if (nullptr == surface) {
40 // picture canvas will return null, so fall-back to raster
reede8f30622016-03-23 18:59:25 -070041 surface = SkSurface::MakeRaster(info);
robertphillips24eb7a82015-09-24 08:47:49 -070042 }
reedf803da12015-01-23 05:58:07 -080043
robertphillips24eb7a82015-09-24 08:47:49 -070044 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000045
robertphillips24eb7a82015-09-24 08:47:49 -070046 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000047
48 SkScalar wScalar = SkIntToScalar(w);
49 SkScalar hScalar = SkIntToScalar(h);
50
51 SkPoint pt = { wScalar / 2, hScalar / 2 };
52
53 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
54
55 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
56 SK_ColorGREEN, SK_ColorMAGENTA,
57 SK_ColorBLUE, SK_ColorCYAN,
58 SK_ColorRED};
59
60 SkScalar pos[] = {0,
61 SK_Scalar1 / 6,
62 2 * SK_Scalar1 / 6,
63 3 * SK_Scalar1 / 6,
64 4 * SK_Scalar1 / 6,
65 5 * SK_Scalar1 / 6,
66 SK_Scalar1};
67
68 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000069 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
70 SkMatrix mat = SkMatrix::I();
71 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080072 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000073 pt, radius,
74 colors, pos,
75 SK_ARRAY_COUNT(colors),
76 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080077 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070078 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000079 rect.inset(wScalar / 8, hScalar / 8);
80 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
81 }
reedf803da12015-01-23 05:58:07 -080082
reed9ce9d672016-03-17 10:51:11 -070083 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070084
85 SkBitmap tempBM;
86
bsalomon7cf36cc2016-07-14 09:33:42 -070087 image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode);
robertphillips24eb7a82015-09-24 08:47:49 -070088
89 // Let backends know we won't change this, so they don't have to deep copy it defensively.
90 tempBM.setImmutable();
91 *resultBM = tempBM;
92
93 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +000094}
95
fmalitaab83da72016-08-26 13:04:14 -070096static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
97 const SkRect& dstR, const SkPaint* paint) {
98 canvas->drawBitmapRect(bm, srcR, dstR, paint);
99}
100
101static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
102 const SkRect& dstR, const SkPaint* paint) {
103 if (!bm.bounds().contains(srcR)) {
104 bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
105 return;
106 }
107
108 SkBitmap subset;
109 if (bm.extractSubset(&subset, srcR)) {
110 canvas->drawBitmapRect(subset, dstR, paint);
111 }
reedf803da12015-01-23 05:58:07 -0800112}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000113
reed84984ef2015-07-17 07:09:43 -0700114static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
fmalitaab83da72016-08-26 13:04:14 -0700115 const SkRect& dstR, const SkPaint* paint) {
116 canvas->drawImageRect(image, srcR, dstR, paint);
reedf803da12015-01-23 05:58:07 -0800117}
118
fmalitaab83da72016-08-26 13:04:14 -0700119static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
120 const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
121 if (!image->bounds().contains(srcR)) {
122 imageproc(canvas, image, bm, srcR, dstR, paint);
123 return;
124 }
125
126 if (sk_sp<SkImage> subset = image->makeSubset(srcR)) {
127 canvas->drawImageRect(subset, dstR, paint);
128 }
129}
130
131typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
132 const SkPaint*);
reedf803da12015-01-23 05:58:07 -0800133
mtkleindbfd7ab2016-09-01 11:24:54 -0700134constexpr int gSize = 1024;
135constexpr int gBmpSize = 2048;
reedf803da12015-01-23 05:58:07 -0800136
137class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000138public:
reedf803da12015-01-23 05:58:07 -0800139 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
140 fName.set("drawbitmaprect");
141 if (suffix) {
142 fName.append(suffix);
143 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000144 }
145
reed9ce9d672016-03-17 10:51:11 -0700146 DrawRectRectProc* fProc;
147 SkBitmap fLargeBitmap;
148 sk_sp<SkImage> fImage;
149 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000150
151protected:
mtklein36352bf2015-03-25 18:17:31 -0700152 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800153
mtklein36352bf2015-03-25 18:17:31 -0700154 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800155
robertphillips24eb7a82015-09-24 08:47:49 -0700156 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700157 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000158 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 void onDraw(SkCanvas* canvas) override {
robertphillips24eb7a82015-09-24 08:47:49 -0700161 if (!fImage) {
162 this->setupImage(canvas);
163 }
164
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000165 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
mtkleindbfd7ab2016-09-01 11:24:54 -0700166 const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000167
mtkleindbfd7ab2016-09-01 11:24:54 -0700168 const int kPadX = 30;
169 const int kPadY = 40;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000170 SkPaint paint;
171 paint.setAlpha(0x20);
bsalomon7cf36cc2016-07-14 09:33:42 -0700172 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000173 canvas->translate(SK_Scalar1 * kPadX / 2,
174 SK_Scalar1 * kPadY / 2);
175 SkPaint blackPaint;
176 SkScalar titleHeight = SK_Scalar1 * 24;
177 blackPaint.setColor(SK_ColorBLACK);
178 blackPaint.setTextSize(titleHeight);
179 blackPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700180 sk_tool_utils::set_portable_typeface(&blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000181 SkString title;
reedf803da12015-01-23 05:58:07 -0800182 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000183 canvas->drawText(title.c_str(), title.size(), 0,
184 titleHeight, blackPaint);
185
186 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
187 int rowCount = 0;
188 canvas->save();
189 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
190 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
191
reedf803da12015-01-23 05:58:07 -0800192 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
fmalitaab83da72016-08-26 13:04:14 -0700193 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000194
195 SkString label;
196 label.appendf("%d x %d", w, h);
197 blackPaint.setAntiAlias(true);
198 blackPaint.setStyle(SkPaint::kFill_Style);
199 blackPaint.setTextSize(SK_Scalar1 * 10);
200 SkScalar baseline = dstRect.height() +
201 blackPaint.getTextSize() + SK_Scalar1 * 3;
202 canvas->drawText(label.c_str(), label.size(),
203 0, baseline,
204 blackPaint);
205 blackPaint.setStyle(SkPaint::kStroke_Style);
206 blackPaint.setStrokeWidth(SK_Scalar1);
207 blackPaint.setAntiAlias(false);
208 canvas->drawRect(dstRect, blackPaint);
209
210 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
211 ++rowCount;
212 if ((dstRect.width() + kPadX) * rowCount > gSize) {
213 canvas->restore();
214 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
215 canvas->save();
216 rowCount = 0;
217 }
218 }
219 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000220
221 {
222 // test the following code path:
223 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
224 SkIRect srcRect;
225 SkPaint paint;
226 SkBitmap bm;
227
228 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700229 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000230
231 srcRect.setXYWH(1, 1, 3, 3);
reedefdfd512016-04-04 10:02:58 -0700232 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000233 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000234 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
fmalitaab83da72016-08-26 13:04:14 -0700235 SkBlurMaskFilter::kHighQuality_BlurFlag));
236
237 sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
238 fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000239 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000240 }
241
242private:
reedf803da12015-01-23 05:58:07 -0800243 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000244};
245
fmalitaab83da72016-08-26 13:04:14 -0700246DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
247DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
248DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
249DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )