blob: d4fa141482bc81ad816625e5b7960e28d4f6dede [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"
robertphillips24eb7a82015-09-24 08:47:49 -070014#include "SkShader.h"
15#include "SkSurface.h"
16
17#if SK_SUPPORT_GPU
18#include "SkGr.h"
19#endif
bsalomon@google.comfb309512011-11-30 14:13:48 +000020
bsalomon@google.com7d30a212012-04-25 15:52:27 +000021static SkBitmap make_chessbm(int w, int h) {
22 SkBitmap bm;
reed@google.comeb9a46c2014-01-25 16:46:20 +000023 bm.allocN32Pixels(w, h);
bsalomon@google.com7d30a212012-04-25 15:52:27 +000024
25 for (int y = 0; y < bm.height(); y++) {
26 uint32_t* p = bm.getAddr32(0, y);
27 for (int x = 0; x < bm.width(); x++) {
28 p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
29 }
30 }
31 bm.unlockPixels();
32 return bm;
33}
34
robertphillips24eb7a82015-09-24 08:47:49 -070035static SkImage* makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
36 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
37
38 SkAutoTUnref<SkSurface> surface(origCanvas->newSurface(info));
39 if (nullptr == surface) {
40 // picture canvas will return null, so fall-back to raster
41 surface.reset(SkSurface::NewRaster(info));
42 }
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) {
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000072 paint.setShader(SkGradientShader::CreateRadial(
73 pt, radius,
74 colors, pos,
75 SK_ARRAY_COUNT(colors),
76 SkShader::kRepeat_TileMode,
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000077 0, &mat))->unref();
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
robertphillips24eb7a82015-09-24 08:47:49 -070083 SkImage* image = surface->newImageSnapshot();
84
85 SkBitmap tempBM;
86
87#if SK_SUPPORT_GPU
88 if (image->getTexture()) {
89 GrWrapTextureInBitmap(image->getTexture(),
90 image->width(), image->height(), image->isOpaque(), &tempBM);
91 } else
92#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
reedf803da12015-01-23 05:58:07 -0800128 DrawRectRectProc* fProc;
129 SkBitmap fLargeBitmap;
130 SkAutoTUnref<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) {
139 fImage.reset(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);
reed84984ef2015-07-17 07:09:43 -0700175 fProc(canvas, fImage, 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);
214 SkMaskFilter* mf = SkBlurMaskFilter::Create(
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 |
218 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
219 paint.setMaskFilter(mf)->unref();
reed84984ef2015-07-17 07:09:43 -0700220 canvas->drawBitmapRect(bm, srcRect, dstRect, &paint);
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000221 }
bsalomon@google.comfb309512011-11-30 14:13:48 +0000222 }
223
224private:
reedf803da12015-01-23 05:58:07 -0800225 typedef skiagm::GM INHERITED;
bsalomon@google.comfb309512011-11-30 14:13:48 +0000226};
227
halcanary96fcdcc2015-08-27 07:41:13 -0700228DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); )
reedf803da12015-01-23 05:58:07 -0800229DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )