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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkColorPriv.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkShader.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/effects/SkBlurMaskFilter.h" |
| 14 | #include "include/effects/SkGradientShader.h" |
| 15 | #include "src/core/SkBlurMask.h" |
| 16 | #include "src/core/SkMathPriv.h" |
| 17 | #include "src/image/SkImage_Base.h" |
| 18 | #include "tools/ToolUtils.h" |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 20 | static SkBitmap make_chessbm(int w, int h) { |
| 21 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 22 | bm.allocN32Pixels(w, h); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 23 | |
| 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 | } |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 30 | return bm; |
| 31 | } |
| 32 | |
bsalomon | 7cf36cc | 2016-07-14 09:33:42 -0700 | [diff] [blame] | 33 | // Creates a bitmap and a matching image. |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 34 | static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) { |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 35 | SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 36 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 37 | auto surface(ToolUtils::makeSurface(origCanvas, info)); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 38 | SkCanvas* canvas = surface->getCanvas(); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 39 | |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 40 | canvas->clear(SK_ColorTRANSPARENT); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 41 | |
| 42 | SkScalar wScalar = SkIntToScalar(w); |
| 43 | SkScalar hScalar = SkIntToScalar(h); |
| 44 | |
| 45 | SkPoint pt = { wScalar / 2, hScalar / 2 }; |
| 46 | |
| 47 | SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar); |
| 48 | |
| 49 | SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW, |
| 50 | SK_ColorGREEN, SK_ColorMAGENTA, |
| 51 | SK_ColorBLUE, SK_ColorCYAN, |
| 52 | SK_ColorRED}; |
| 53 | |
| 54 | SkScalar pos[] = {0, |
| 55 | SK_Scalar1 / 6, |
| 56 | 2 * SK_Scalar1 / 6, |
| 57 | 3 * SK_Scalar1 / 6, |
| 58 | 4 * SK_Scalar1 / 6, |
| 59 | 5 * SK_Scalar1 / 6, |
| 60 | SK_Scalar1}; |
| 61 | |
| 62 | SkPaint paint; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 63 | SkRect rect = SkRect::MakeWH(wScalar, hScalar); |
| 64 | SkMatrix mat = SkMatrix::I(); |
| 65 | for (int i = 0; i < 4; ++i) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 66 | paint.setShader(SkGradientShader::MakeRadial( |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 67 | pt, radius, |
| 68 | colors, pos, |
| 69 | SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 70 | SkTileMode::kRepeat, |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 71 | 0, &mat)); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 72 | canvas->drawRect(rect, paint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 73 | rect.inset(wScalar / 8, hScalar / 8); |
| 74 | mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4); |
| 75 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 76 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 77 | auto image = surface->makeImageSnapshot(); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 78 | |
| 79 | SkBitmap tempBM; |
| 80 | |
Cary Clark | 4f5a79c | 2018-02-07 15:51:00 -0500 | [diff] [blame] | 81 | image->asLegacyBitmap(&tempBM); |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 82 | |
| 83 | // Let backends know we won't change this, so they don't have to deep copy it defensively. |
| 84 | tempBM.setImmutable(); |
| 85 | *resultBM = tempBM; |
| 86 | |
| 87 | return image; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 88 | } |
| 89 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 90 | static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR, |
| 91 | const SkRect& dstR, const SkPaint* paint) { |
| 92 | canvas->drawBitmapRect(bm, srcR, dstR, paint); |
| 93 | } |
| 94 | |
| 95 | static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR, |
| 96 | const SkRect& dstR, const SkPaint* paint) { |
| 97 | if (!bm.bounds().contains(srcR)) { |
| 98 | bitmapproc(canvas, nullptr, bm, srcR, dstR, paint); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | SkBitmap subset; |
| 103 | if (bm.extractSubset(&subset, srcR)) { |
| 104 | canvas->drawBitmapRect(subset, dstR, paint); |
| 105 | } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 106 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 107 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 108 | static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR, |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 109 | const SkRect& dstR, const SkPaint* paint) { |
| 110 | canvas->drawImageRect(image, srcR, dstR, paint); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 111 | } |
| 112 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 113 | static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm, |
| 114 | const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) { |
| 115 | if (!image->bounds().contains(srcR)) { |
| 116 | imageproc(canvas, image, bm, srcR, dstR, paint); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | if (sk_sp<SkImage> subset = image->makeSubset(srcR)) { |
| 121 | canvas->drawImageRect(subset, dstR, paint); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&, |
| 126 | const SkPaint*); |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 127 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 128 | constexpr int gSize = 1024; |
| 129 | constexpr int gBmpSize = 2048; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 130 | |
| 131 | class DrawBitmapRectGM : public skiagm::GM { |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 132 | public: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 133 | DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) { |
| 134 | fName.set("drawbitmaprect"); |
| 135 | if (suffix) { |
| 136 | fName.append(suffix); |
| 137 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 138 | } |
| 139 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 140 | DrawRectRectProc* fProc; |
| 141 | SkBitmap fLargeBitmap; |
| 142 | sk_sp<SkImage> fImage; |
| 143 | SkString fName; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 144 | |
| 145 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 146 | SkString onShortName() override { return fName; } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 147 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 148 | SkISize onISize() override { return SkISize::Make(gSize, gSize); } |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 149 | |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 150 | void setupImage(SkCanvas* canvas) { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 151 | fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 152 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 154 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 5426850 | 2018-05-24 13:22:01 -0400 | [diff] [blame] | 155 | if (!fImage || !fImage->isValid(canvas->getGrContext())) { |
robertphillips | 24eb7a8 | 2015-09-24 08:47:49 -0700 | [diff] [blame] | 156 | this->setupImage(canvas); |
| 157 | } |
| 158 | |
vandebo@chromium.org | 663515b | 2012-01-05 18:45:27 +0000 | [diff] [blame] | 159 | SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)}; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 160 | const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 161 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 162 | const int kPadX = 30; |
| 163 | const int kPadY = 40; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 164 | SkPaint paint; |
Mike Reed | 9407e24 | 2019-02-15 16:13:57 -0500 | [diff] [blame] | 165 | paint.setAlphaf(0.125f); |
bsalomon | 7cf36cc | 2016-07-14 09:33:42 -0700 | [diff] [blame] | 166 | canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &paint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 167 | canvas->translate(SK_Scalar1 * kPadX / 2, |
| 168 | SK_Scalar1 * kPadY / 2); |
| 169 | SkPaint blackPaint; |
| 170 | SkScalar titleHeight = SK_Scalar1 * 24; |
| 171 | blackPaint.setColor(SK_ColorBLACK); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 172 | blackPaint.setAntiAlias(true); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 173 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 174 | SkFont font(ToolUtils::create_portable_typeface(), titleHeight); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 175 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 176 | SkString title; |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 177 | title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 178 | canvas->drawString(title, 0, titleHeight, font, blackPaint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 179 | |
| 180 | canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight); |
| 181 | int rowCount = 0; |
| 182 | canvas->save(); |
| 183 | for (int w = 1; w <= kMaxSrcRectSize; w *= 4) { |
| 184 | for (int h = 1; h <= kMaxSrcRectSize; h *= 4) { |
| 185 | |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 186 | SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h); |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 187 | fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, nullptr); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 188 | |
| 189 | SkString label; |
| 190 | label.appendf("%d x %d", w, h); |
| 191 | blackPaint.setAntiAlias(true); |
| 192 | blackPaint.setStyle(SkPaint::kFill_Style); |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 193 | font.setSize(SK_Scalar1 * 10); |
| 194 | SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3; |
| 195 | canvas->drawString(label, 0, baseline, font, blackPaint); |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 196 | blackPaint.setStyle(SkPaint::kStroke_Style); |
| 197 | blackPaint.setStrokeWidth(SK_Scalar1); |
| 198 | blackPaint.setAntiAlias(false); |
| 199 | canvas->drawRect(dstRect, blackPaint); |
| 200 | |
| 201 | canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0); |
| 202 | ++rowCount; |
| 203 | if ((dstRect.width() + kPadX) * rowCount > gSize) { |
| 204 | canvas->restore(); |
| 205 | canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY); |
| 206 | canvas->save(); |
| 207 | rowCount = 0; |
| 208 | } |
| 209 | } |
| 210 | } |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 211 | |
| 212 | { |
| 213 | // test the following code path: |
| 214 | // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter() |
| 215 | SkIRect srcRect; |
| 216 | SkPaint paint; |
| 217 | SkBitmap bm; |
| 218 | |
| 219 | bm = make_chessbm(5, 5); |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 220 | paint.setFilterQuality(kLow_SkFilterQuality); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 221 | |
| 222 | srcRect.setXYWH(1, 1, 3, 3); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 223 | paint.setMaskFilter(SkMaskFilter::MakeBlur( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 224 | kNormal_SkBlurStyle, |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 225 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)))); |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 226 | |
| 227 | sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm)); |
| 228 | fProc(canvas, image.get(), bm, srcRect, dstRect, &paint); |
bsalomon@google.com | 7d30a21 | 2012-04-25 15:52:27 +0000 | [diff] [blame] | 229 | } |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | private: |
reed | f803da1 | 2015-01-23 05:58:07 -0800 | [diff] [blame] | 233 | typedef skiagm::GM INHERITED; |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
fmalita | ab83da7 | 2016-08-26 13:04:14 -0700 | [diff] [blame] | 236 | DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); ) |
| 237 | DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); ) |
| 238 | DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); ) |
| 239 | DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); ) |