blob: bd6fffec6376b7de2b38df815cc644a14a91f5df [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"
robertphillips24eb7a82015-09-24 08:47:49 -070015#include "SkShader.h"
16#include "SkSurface.h"
17
18#if SK_SUPPORT_GPU
19#include "SkGr.h"
20#endif
bsalomon@google.comfb309512011-11-30 14:13:48 +000021
bsalomon@google.com7d30a212012-04-25 15:52:27 +000022static SkBitmap make_chessbm(int w, int h) {
23 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000024 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000025
26 for (int y = 0; y < bm.height(); y++) {
27 uint32_t* p = bm.getAddr32(0, y);
28 for (int x = 0; x < bm.width(); x++) {
29 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
30 }
31 }
32 bm.unlockPixels();
33 return bm;
34}
35
reed9ce9d672016-03-17 10:51:11 -070036static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
robertphillips24eb7a82015-09-24 08:47:49 -070037 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
halcanary9d524f22016-03-29 09:03:52 -070038
reede8f30622016-03-23 18:59:25 -070039 auto surface(origCanvas->makeSurface(info));
robertphillips24eb7a82015-09-24 08:47:49 -070040 if (nullptr == surface) {
41 // picture canvas will return null, so fall-back to raster
reede8f30622016-03-23 18:59:25 -070042 surface = SkSurface::MakeRaster(info);
robertphillips24eb7a82015-09-24 08:47:49 -070043 }
reedf803da12015-01-23 05:58:07 -080044
robertphillips24eb7a82015-09-24 08:47:49 -070045 SkCanvas* canvas = surface->getCanvas();
bsalomon@google.comfb309512011-11-30 14:13:48 +000046
robertphillips24eb7a82015-09-24 08:47:49 -070047 canvas->clear(SK_ColorTRANSPARENT);
bsalomon@google.comfb309512011-11-30 14:13:48 +000048
49 SkScalar wScalar = SkIntToScalar(w);
50 SkScalar hScalar = SkIntToScalar(h);
51
52 SkPoint pt = { wScalar / 2, hScalar / 2 };
53
54 SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
55
56 SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
57 SK_ColorGREEN, SK_ColorMAGENTA,
58 SK_ColorBLUE, SK_ColorCYAN,
59 SK_ColorRED};
60
61 SkScalar pos[] = {0,
62 SK_Scalar1 / 6,
63 2 * SK_Scalar1 / 6,
64 3 * SK_Scalar1 / 6,
65 4 * SK_Scalar1 / 6,
66 5 * SK_Scalar1 / 6,
67 SK_Scalar1};
68
69 SkPaint paint;
bsalomon@google.comfb309512011-11-30 14:13:48 +000070 SkRect rect = SkRect::MakeWH(wScalar, hScalar);
71 SkMatrix mat = SkMatrix::I();
72 for (int i = 0; i < 4; ++i) {
reed2ad1aa62016-03-09 09:50:50 -080073 paint.setShader(SkGradientShader::MakeRadial(
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000074 pt, radius,
75 colors, pos,
76 SK_ARRAY_COUNT(colors),
77 SkShader::kRepeat_TileMode,
reed2ad1aa62016-03-09 09:50:50 -080078 0, &mat));
robertphillips24eb7a82015-09-24 08:47:49 -070079 canvas->drawRect(rect, paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +000080 rect.inset(wScalar / 8, hScalar / 8);
81 mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
82 }
reedf803da12015-01-23 05:58:07 -080083
reed9ce9d672016-03-17 10:51:11 -070084 auto image = surface->makeImageSnapshot();
robertphillips24eb7a82015-09-24 08:47:49 -070085
86 SkBitmap tempBM;
87
88#if SK_SUPPORT_GPU
bsalomon84a4e5a2016-02-29 11:41:52 -080089 if (GrTexture* texture = as_IB(image)->peekTexture()) {
90 GrWrapTextureInBitmap(texture, image->width(), image->height(), image->isOpaque(), &tempBM);
halcanary9d524f22016-03-29 09:03:52 -070091 } else
robertphillips24eb7a82015-09-24 08:47:49 -070092#endif
93 {
94 image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode);
95 }
96
97 // Let backends know we won't change this, so they don't have to deep copy it defensively.
98 tempBM.setImmutable();
99 *resultBM = tempBM;
100
101 return image;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000102}
103
reed84984ef2015-07-17 07:09:43 -0700104static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -0800105 const SkRect& dstR) {
halcanary96fcdcc2015-08-27 07:41:13 -0700106 canvas->drawBitmapRect(bm, srcR, dstR, nullptr);
reedf803da12015-01-23 05:58:07 -0800107}
bsalomon@google.comfb309512011-11-30 14:13:48 +0000108
reed84984ef2015-07-17 07:09:43 -0700109static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
reedf803da12015-01-23 05:58:07 -0800110 const SkRect& dstR) {
halcanary96fcdcc2015-08-27 07:41:13 -0700111 canvas->drawImageRect(image, srcR, dstR, nullptr);
reedf803da12015-01-23 05:58:07 -0800112}
113
reed84984ef2015-07-17 07:09:43 -0700114typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&);
reedf803da12015-01-23 05:58:07 -0800115
116static const int gSize = 1024;
117static const int gBmpSize = 2048;
118
119class DrawBitmapRectGM : public skiagm::GM {
bsalomon@google.comfb309512011-11-30 14:13:48 +0000120public:
reedf803da12015-01-23 05:58:07 -0800121 DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
122 fName.set("drawbitmaprect");
123 if (suffix) {
124 fName.append(suffix);
125 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000126 }
127
reed9ce9d672016-03-17 10:51:11 -0700128 DrawRectRectProc* fProc;
129 SkBitmap fLargeBitmap;
130 sk_sp<SkImage> fImage;
131 SkString fName;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000132
133protected:
mtklein36352bf2015-03-25 18:17:31 -0700134 SkString onShortName() override { return fName; }
reedf803da12015-01-23 05:58:07 -0800135
mtklein36352bf2015-03-25 18:17:31 -0700136 SkISize onISize() override { return SkISize::Make(gSize, gSize); }
reedf803da12015-01-23 05:58:07 -0800137
robertphillips24eb7a82015-09-24 08:47:49 -0700138 void setupImage(SkCanvas* canvas) {
reed9ce9d672016-03-17 10:51:11 -0700139 fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000140 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000141
mtklein36352bf2015-03-25 18:17:31 -0700142 void onDraw(SkCanvas* canvas) override {
robertphillips24eb7a82015-09-24 08:47:49 -0700143 if (!fImage) {
144 this->setupImage(canvas);
145 }
146
vandebo@chromium.org663515b2012-01-05 18:45:27 +0000147 SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
reedf803da12015-01-23 05:58:07 -0800148 static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000149
150 static const int kPadX = 30;
151 static const int kPadY = 40;
152 SkPaint paint;
153 paint.setAlpha(0x20);
reede47829b2015-08-06 10:02:53 -0700154 canvas->drawBitmapRect(fLargeBitmap, SkRect::MakeIWH(gSize, gSize), &paint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000155 canvas->translate(SK_Scalar1 * kPadX / 2,
156 SK_Scalar1 * kPadY / 2);
157 SkPaint blackPaint;
158 SkScalar titleHeight = SK_Scalar1 * 24;
159 blackPaint.setColor(SK_ColorBLACK);
160 blackPaint.setTextSize(titleHeight);
161 blackPaint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -0700162 sk_tool_utils::set_portable_typeface(&blackPaint);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000163 SkString title;
reedf803da12015-01-23 05:58:07 -0800164 title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000165 canvas->drawText(title.c_str(), title.size(), 0,
166 titleHeight, blackPaint);
167
168 canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
169 int rowCount = 0;
170 canvas->save();
171 for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
172 for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
173
reedf803da12015-01-23 05:58:07 -0800174 SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
reed9ce9d672016-03-17 10:51:11 -0700175 fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000176
177 SkString label;
178 label.appendf("%d x %d", w, h);
179 blackPaint.setAntiAlias(true);
180 blackPaint.setStyle(SkPaint::kFill_Style);
181 blackPaint.setTextSize(SK_Scalar1 * 10);
182 SkScalar baseline = dstRect.height() +
183 blackPaint.getTextSize() + SK_Scalar1 * 3;
184 canvas->drawText(label.c_str(), label.size(),
185 0, baseline,
186 blackPaint);
187 blackPaint.setStyle(SkPaint::kStroke_Style);
188 blackPaint.setStrokeWidth(SK_Scalar1);
189 blackPaint.setAntiAlias(false);
190 canvas->drawRect(dstRect, blackPaint);
191
192 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
193 ++rowCount;
194 if ((dstRect.width() + kPadX) * rowCount > gSize) {
195 canvas->restore();
196 canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
197 canvas->save();
198 rowCount = 0;
199 }
200 }
201 }
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000202
203 {
204 // test the following code path:
205 // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
206 SkIRect srcRect;
207 SkPaint paint;
208 SkBitmap bm;
209
210 bm = make_chessbm(5, 5);
reed93a12152015-03-16 10:08:34 -0700211 paint.setFilterQuality(kLow_SkFilterQuality);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000212
213 srcRect.setXYWH(1, 1, 3, 3);
reedefdfd512016-04-04 10:02:58 -0700214 paint.setMaskFilter(SkBlurMaskFilter::Make(
commit-bot@chromium.orge3964552014-04-28 16:25:35 +0000215 kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +0000216 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000217 SkBlurMaskFilter::kHighQuality_BlurFlag |
reedefdfd512016-04-04 10:02:58 -0700218 SkBlurMaskFilter::kIgnoreTransform_BlurFlag));
reed84984ef2015-07-17 07:09:43 -0700219 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000220 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000221 }
222
223private:
reedf803da12015-01-23 05:58:07 -0800224 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000225};
226
halcanary96fcdcc2015-08-27 07:41:13 -0700227DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); )
reedf803da12015-01-23 05:58:07 -0800228DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )