bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 7 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 10 | #include "SkBlurMask.h" |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 11 | #include "SkBlurMaskFilter.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 13 | #include "SkGradientShader.h" |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 14 | #include "SkImage.h" |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 15 | #include "SkImage_Base.h" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 16 | #include "SkMathPriv.h" |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 17 | #include "SkShader.h" |
| 18 | #include "SkSurface.h" |
| 19 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 20 | |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 21 | static SkBitmap make_chessbm(int w, int h) { |
| 22 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 23 | bm.allocN32Pixels(w, h); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 24 | |
| 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 | } |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 31 | return bm; |
| 32 | } |
| 33 | |
bsalomon | 7cf36cc | 2016-07-14 09:33:42 -0700 | [diff] [blame] | 34 | // Creates a bitmap and a matching image. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 35 | static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 36 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 37 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 38 | auto surface(origCanvas->makeSurface(info)); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 39 | if (nullptr == surface) { |
| 40 | // picture canvas will return null, so fall-back to raster |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 41 | surface = SkSurface::MakeRaster(info); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 42 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 43 | |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 44 | SkCanvas* canvas = surface->getCanvas(); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 45 | |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 46 | canvas->clear(SK_ColorTRANSPARENT); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 47 | |
| 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.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 69 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 70 | SkMatrix mat = SkMatrix::I(); |
| 71 | for (int i = 0; i < 4; ++i) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 72 | paint.setShader(SkGradientShader::MakeRadial( |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 73 | pt, radius, |
| 74 | colors, pos, |
| 75 | SK_ARRAY_COUNT(colors), |
| 76 | SkShader::kRepeat_TileMode, |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 77 | 0, &mat)); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 78 | canvas->drawRect(rect, paint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 79 | rect.inset(wScalar / 8, hScalar / 8); |
| 80 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 81 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 82 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 83 | auto image = surface->makeImageSnapshot(); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 84 | |
| 85 | SkBitmap tempBM; |
| 86 | |
bsalomon | 7cf36cc | 2016-07-14 09:33:42 -0700 | [diff] [blame] | 87 | image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 88 | |
| 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.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 94 | } |
| 95 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 96 | static 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 | |
| 101 | static 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 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 112 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 113 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 114 | static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR, |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 115 | const SkRect& dstR, const SkPaint* paint) { |
| 116 | canvas->drawImageRect(image, srcR, dstR, paint); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 117 | } |
| 118 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 119 | static 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 | |
| 131 | typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&, |
| 132 | const SkPaint*); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 133 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 134 | constexpr int gSize = 1024; |
| 135 | constexpr int gBmpSize = 2048; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 136 | |
| 137 | class DrawBitmapRectGM : public skiagm::GM { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 138 | public: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 139 | DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { |
| 140 | fName.set("drawbitmaprect"); |
| 141 | if (suffix) { |
| 142 | fName.append(suffix); |
| 143 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 144 | } |
| 145 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 146 | DrawRectRectProc* fProc; |
| 147 | SkBitmap fLargeBitmap; |
| 148 | sk_sp<SkImage> fImage; |
| 149 | SkString fName; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 150 | |
| 151 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 152 | SkString onShortName() override { return fName; } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | SkISize onISize() override { return SkISize::Make(gSize, gSize); } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 155 | |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 156 | void setupImage(SkCanvas* canvas) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 157 | fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 160 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 161 | if (!fImage) { |
| 162 | this->setupImage(canvas); |
| 163 | } |
| 164 | |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 165 | SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 166 | const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 167 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 168 | const int kPadX = 30; |
| 169 | const int kPadY = 40; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 170 | SkPaint paint; |
| 171 | paint.setAlpha(0x20); |
bsalomon | 7cf36cc | 2016-07-14 09:33:42 -0700 | [diff] [blame] | 172 | canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 173 | 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); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 180 | sk_tool_utils::set_portable_typeface(&blackPaint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 181 | SkString title; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 182 | title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 183 | canvas->drawString(title, 0, |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 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 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 192 | SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h); |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 193 | fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 194 | |
| 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; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 202 | canvas->drawString(label, |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 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.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 220 | |
| 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); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 229 | paint.setFilterQuality(kLow_SkFilterQuality); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 230 | |
| 231 | srcRect.setXYWH(1, 1, 3, 3); |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 232 | paint.setMaskFilter(SkBlurMaskFilter::Make( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 233 | kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 234 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 235 | SkBlurMaskFilter::kHighQuality_BlurFlag)); |
| 236 | |
| 237 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
| 238 | fProc(canvas, image.get(), bm, srcRect, dstRect, &paint); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 239 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | private: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 243 | typedef skiagm::GM INHERITED; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 246 | DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); ) |
| 247 | DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); ) |
| 248 | DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); ) |
| 249 | DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); ) |