blob: 0747ab0910e36aea612bf346d38ae88a931ebd0f [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
reed84984ef2015-07-17 07:09:43 -070096static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -080097 const SkRect& dstR) {
halcanary96fcdcc2015-08-27 07:41:13 -070098 canvas->drawBitmapRect(bm, srcR, dstR, nullptr);
reedf803da12015-01-23 05:58:07 -080099}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000100
reed84984ef2015-07-17 07:09:43 -0700101static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -0800102 const SkRect& dstR) {
halcanary96fcdcc2015-08-27 07:41:13 -0700103 canvas->drawImageRect(image, srcR, dstR, nullptr);
reedf803da12015-01-23 05:58:07 -0800104}
105
reed84984ef2015-07-17 07:09:43 -0700106typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&);
reedf803da12015-01-23 05:58:07 -0800107
108static const int gSize = 1024;
109static const int gBmpSize = 2048;
110
111class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000112public:
reedf803da12015-01-23 05:58:07 -0800113 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
114 fName.set("drawbitmaprect");
115 if (suffix) {
116 fName.append(suffix);
117 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000118 }
119
reed9ce9d672016-03-17 10:51:11 -0700120 DrawRectRectProc* fProc;
121 SkBitmap fLargeBitmap;
122 sk_sp<SkImage> fImage;
123 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000124
125protected:
mtklein36352bf2015-03-25 18:17:31 -0700126 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800127
mtklein36352bf2015-03-25 18:17:31 -0700128 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800129
robertphillips24eb7a82015-09-24 08:47:49 -0700130 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700131 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
mtklein36352bf2015-03-25 18:17:31 -0700134 void onDraw(SkCanvas* canvas) override {
robertphillips24eb7a82015-09-24 08:47:49 -0700135 if (!fImage) {
136 this->setupImage(canvas);
137 }
138
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000139 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
reedf803da12015-01-23 05:58:07 -0800140 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000141
142 static const int kPadX = 30;
143 static const int kPadY = 40;
144 SkPaint paint;
145 paint.setAlpha(0x20);
bsalomon7cf36cc2016-07-14 09:33:42 -0700146 canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000147 canvas->translate(SK_Scalar1 * kPadX / 2,
148 SK_Scalar1 * kPadY / 2);
149 SkPaint blackPaint;
150 SkScalar titleHeight = SK_Scalar1 * 24;
151 blackPaint.setColor(SK_ColorBLACK);
152 blackPaint.setTextSize(titleHeight);
153 blackPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700154 sk_tool_utils::set_portable_typeface(&blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000155 SkString title;
reedf803da12015-01-23 05:58:07 -0800156 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000157 canvas->drawText(title.c_str(), title.size(), 0,
158 titleHeight, blackPaint);
159
160 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
161 int rowCount = 0;
162 canvas->save();
163 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
164 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
165
reedf803da12015-01-23 05:58:07 -0800166 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
reed9ce9d672016-03-17 10:51:11 -0700167 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000168
169 SkString label;
170 label.appendf("%d x %d", w, h);
171 blackPaint.setAntiAlias(true);
172 blackPaint.setStyle(SkPaint::kFill_Style);
173 blackPaint.setTextSize(SK_Scalar1 * 10);
174 SkScalar baseline = dstRect.height() +
175 blackPaint.getTextSize() + SK_Scalar1 * 3;
176 canvas->drawText(label.c_str(), label.size(),
177 0, baseline,
178 blackPaint);
179 blackPaint.setStyle(SkPaint::kStroke_Style);
180 blackPaint.setStrokeWidth(SK_Scalar1);
181 blackPaint.setAntiAlias(false);
182 canvas->drawRect(dstRect, blackPaint);
183
184 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
185 ++rowCount;
186 if ((dstRect.width() + kPadX) * rowCount > gSize) {
187 canvas->restore();
188 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
189 canvas->save();
190 rowCount = 0;
191 }
192 }
193 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000194
195 {
196 // test the following code path:
197 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
198 SkIRect srcRect;
199 SkPaint paint;
200 SkBitmap bm;
201
202 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700203 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000204
205 srcRect.setXYWH(1, 1, 3, 3);
reedefdfd512016-04-04 10:02:58 -0700206 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000207 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000208 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000209 SkBlurMaskFilter::kHighQuality_BlurFlag |
reedefdfd512016-04-04 10:02:58 -0700210 SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
reed84984ef2015-07-17 07:09:43 -0700211 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000212 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000213 }
214
215private:
reedf803da12015-01-23 05:58:07 -0800216 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000217};
218
halcanary96fcdcc2015-08-27 07:41:13 -0700219DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); )
reedf803da12015-01-23 05:58:07 -0800220DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )