robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkImage.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 14 | #include "include/core/SkImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 16 | #include "include/core/SkMatrix.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkPoint.h" |
| 19 | #include "include/core/SkRect.h" |
| 20 | #include "include/core/SkRefCnt.h" |
| 21 | #include "include/core/SkScalar.h" |
| 22 | #include "include/core/SkShader.h" |
| 23 | #include "include/core/SkSize.h" |
| 24 | #include "include/core/SkString.h" |
| 25 | #include "include/core/SkSurface.h" |
| 26 | #include "include/core/SkTileMode.h" |
| 27 | #include "include/core/SkTypes.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 28 | #include "include/gpu/GrContextOptions.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "include/private/SkTDArray.h" |
| 30 | #include "src/core/SkBlurMask.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "tools/ToolUtils.h" |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 32 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 33 | /** Creates an image with two one-pixel wide borders around a checkerboard. The checkerboard is 2x2 |
| 34 | checks where each check has as many pixels as is necessary to fill the interior. It returns |
| 35 | the image and a src rect that bounds the checkerboard portion. */ |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 36 | std::tuple<sk_sp<SkImage>, SkRect> make_ringed_image(int width, int height) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 37 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 38 | // These are kRGBA_8888_SkColorType values. |
| 39 | static constexpr uint32_t kOuterRingColor = 0xFFFF0000, |
| 40 | kInnerRingColor = 0xFF0000FF, |
| 41 | kCheckColor1 = 0xFF000000, |
| 42 | kCheckColor2 = 0xFFFFFFFF; |
| 43 | |
commit-bot@chromium.org | ec39b50 | 2013-11-08 15:09:22 +0000 | [diff] [blame] | 44 | SkASSERT(0 == width % 2 && 0 == height % 2); |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 45 | SkASSERT(width >= 6 && height >= 6); |
skia.committer@gmail.com | ed00084 | 2013-11-09 07:02:23 +0000 | [diff] [blame] | 46 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 47 | SkImageInfo info = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType, |
| 48 | kPremul_SkAlphaType); |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 49 | size_t rowBytes = SkAlign4(info.minRowBytes()); |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 50 | SkBitmap bitmap; |
| 51 | bitmap.allocPixels(info, rowBytes); |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 52 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 53 | uint32_t* scanline = bitmap.getAddr32(0, 0); |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 54 | for (int x = 0; x < width; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 55 | scanline[x] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 56 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 57 | scanline = bitmap.getAddr32(0, 1); |
| 58 | scanline[0] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 59 | for (int x = 1; x < width - 1; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 60 | scanline[x] = kInnerRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 61 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 62 | scanline[width - 1] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 63 | |
| 64 | for (int y = 2; y < height / 2; ++y) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 65 | scanline = bitmap.getAddr32(0, y); |
| 66 | scanline[0] = kOuterRingColor; |
| 67 | scanline[1] = kInnerRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 68 | for (int x = 2; x < width / 2; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 69 | scanline[x] = kCheckColor1; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 70 | } |
| 71 | for (int x = width / 2; x < width - 2; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 72 | scanline[x] = kCheckColor2; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 73 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 74 | scanline[width - 2] = kInnerRingColor; |
| 75 | scanline[width - 1] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | for (int y = height / 2; y < height - 2; ++y) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 79 | scanline = bitmap.getAddr32(0, y); |
| 80 | scanline[0] = kOuterRingColor; |
| 81 | scanline[1] = kInnerRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 82 | for (int x = 2; x < width / 2; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 83 | scanline[x] = kCheckColor2; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 84 | } |
| 85 | for (int x = width / 2; x < width - 2; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 86 | scanline[x] = kCheckColor1; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 87 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 88 | scanline[width - 2] = kInnerRingColor; |
| 89 | scanline[width - 1] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 92 | scanline = bitmap.getAddr32(0, height - 2); |
| 93 | scanline[0] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 94 | for (int x = 1; x < width - 1; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 95 | scanline[x] = kInnerRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 96 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 97 | scanline[width - 1] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 98 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 99 | scanline = bitmap.getAddr32(0, height - 1); |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 100 | for (int x = 0; x < width; ++x) { |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 101 | scanline[x] = kOuterRingColor; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 102 | } |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 103 | bitmap.setImmutable(); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 104 | return {bitmap.asImage(), SkRect::Make({2, 2, width - 2, height - 2})}; |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 107 | /** |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 108 | * These GMs exercise the behavior of the drawImageRect and its SrcRectConstraint parameter. They |
| 109 | * tests various matrices, filter qualities, and interaction with mask filters. They also exercise |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 110 | * the tiling image draws of SkGpuDevice by overriding the maximum texture size of the GrContext. |
| 111 | */ |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 112 | class SrcRectConstraintGM : public skiagm::GM { |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 113 | public: |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 114 | SrcRectConstraintGM(const char* shortName, SkCanvas::SrcRectConstraint constraint, bool batch) |
| 115 | : fShortName(shortName) |
| 116 | , fConstraint(constraint) |
Brian Salomon | 010bb46 | 2021-01-29 14:42:38 -0500 | [diff] [blame] | 117 | , fBatch(batch) { |
| 118 | // Make sure GPU SkSurfaces can be created for this GM. |
| 119 | SkASSERT(this->onISize().width() <= kMaxTextureSize && |
| 120 | this->onISize().height() <= kMaxTextureSize); |
| 121 | } |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 122 | |
| 123 | protected: |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 124 | SkString onShortName() override { return fShortName; } |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 125 | SkISize onISize() override { return SkISize::Make(800, 1000); } |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 126 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 127 | void drawImage(SkCanvas* canvas, sk_sp<SkImage> image, SkRect srcRect, SkRect dstRect, |
| 128 | const SkSamplingOptions& sampling, SkPaint* paint) { |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 129 | if (fBatch) { |
| 130 | SkCanvas::ImageSetEntry imageSetEntry[1]; |
| 131 | imageSetEntry[0].fImage = image; |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 132 | imageSetEntry[0].fSrcRect = srcRect; |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 133 | imageSetEntry[0].fDstRect = dstRect; |
| 134 | imageSetEntry[0].fAAFlags = paint->isAntiAlias() ? SkCanvas::kAll_QuadAAFlags |
| 135 | : SkCanvas::kNone_QuadAAFlags; |
| 136 | canvas->experimental_DrawEdgeAAImageSet(imageSetEntry, SK_ARRAY_COUNT(imageSetEntry), |
| 137 | /*dstClips=*/nullptr, |
| 138 | /*preViewMatrices=*/nullptr, |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 139 | sampling, paint, fConstraint); |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 140 | } else { |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 141 | canvas->drawImageRect(image.get(), srcRect, dstRect, sampling, paint, fConstraint); |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 145 | // Draw the area of interest of the small image |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 146 | void drawCase1(SkCanvas* canvas, int transX, int transY, bool aa, |
| 147 | const SkSamplingOptions& sampling) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 148 | SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY), |
| 149 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 150 | |
| 151 | SkPaint paint; |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 152 | paint.setColor(SK_ColorBLUE); |
| 153 | paint.setAntiAlias(aa); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 154 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 155 | drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 156 | } |
| 157 | |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 158 | // Draw the area of interest of the large image |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 159 | void drawCase2(SkCanvas* canvas, int transX, int transY, bool aa, |
| 160 | const SkSamplingOptions& sampling) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 161 | SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY), |
| 162 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 163 | |
| 164 | SkPaint paint; |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 165 | paint.setColor(SK_ColorBLUE); |
| 166 | paint.setAntiAlias(aa); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 167 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 168 | drawImage(canvas, fBigImage, fBigSrcRect, dst, sampling, &paint); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 169 | } |
| 170 | |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 171 | // Draw upper-left 1/4 of the area of interest of the large image |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 172 | void drawCase3(SkCanvas* canvas, int transX, int transY, bool aa, |
| 173 | const SkSamplingOptions& sampling) { |
| 174 | SkRect src = SkRect::MakeXYWH(fBigSrcRect.fLeft, |
| 175 | fBigSrcRect.fTop, |
| 176 | fBigSrcRect.width()/2, |
| 177 | fBigSrcRect.height()/2); |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 178 | SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY), |
| 179 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 180 | |
| 181 | SkPaint paint; |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 182 | paint.setColor(SK_ColorBLUE); |
| 183 | paint.setAntiAlias(aa); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 184 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 185 | drawImage(canvas, fBigImage, src, dst, sampling, &paint); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 186 | } |
| 187 | |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 188 | // Draw the area of interest of the small image with a normal blur |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 189 | void drawCase4(SkCanvas* canvas, int transX, int transY, bool aa, |
| 190 | const SkSamplingOptions& sampling) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 191 | SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY), |
| 192 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
robertphillips@google.com | 2cc0b47 | 2013-08-20 16:51:20 +0000 | [diff] [blame] | 193 | |
| 194 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 195 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 196 | SkBlurMask::ConvertRadiusToSigma(3))); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 197 | paint.setColor(SK_ColorBLUE); |
| 198 | paint.setAntiAlias(aa); |
robertphillips@google.com | 2cc0b47 | 2013-08-20 16:51:20 +0000 | [diff] [blame] | 199 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 200 | drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint); |
robertphillips@google.com | 2cc0b47 | 2013-08-20 16:51:20 +0000 | [diff] [blame] | 201 | } |
| 202 | |
bsalomon | 5c1262d | 2015-11-09 10:06:06 -0800 | [diff] [blame] | 203 | // Draw the area of interest of the small image with a outer blur |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 204 | void drawCase5(SkCanvas* canvas, int transX, int transY, bool aa, |
| 205 | const SkSamplingOptions& sampling) { |
bsalomon | f57ef1c | 2015-11-04 04:36:12 -0800 | [diff] [blame] | 206 | SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY), |
| 207 | SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize)); |
| 208 | |
| 209 | SkPaint paint; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 210 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 211 | SkBlurMask::ConvertRadiusToSigma(7))); |
bsalomon | f57ef1c | 2015-11-04 04:36:12 -0800 | [diff] [blame] | 212 | paint.setColor(SK_ColorBLUE); |
| 213 | paint.setAntiAlias(aa); |
| 214 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 215 | drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint); |
bsalomon | f57ef1c | 2015-11-04 04:36:12 -0800 | [diff] [blame] | 216 | } |
| 217 | |
bsalomon | a2e0837 | 2016-07-13 14:50:17 -0700 | [diff] [blame] | 218 | void onOnceBeforeDraw() override { |
Brian Salomon | 010bb46 | 2021-01-29 14:42:38 -0500 | [diff] [blame] | 219 | std::tie(fBigImage, fBigSrcRect) = make_ringed_image(2*kMaxTextureSize, 2*kMaxTextureSize); |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 220 | std::tie(fSmallImage, fSmallSrcRect) = make_ringed_image(kSmallSize, kSmallSize); |
bsalomon | a2e0837 | 2016-07-13 14:50:17 -0700 | [diff] [blame] | 221 | } |
| 222 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 223 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 224 | canvas->clear(SK_ColorGRAY); |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 225 | std::vector<SkMatrix> matrices; |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 226 | // Draw with identity |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 227 | matrices.push_back(SkMatrix::I()); |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 228 | |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 229 | // Draw with rotation and scale down in x, up in y. |
| 230 | SkMatrix m; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 231 | constexpr SkScalar kBottom = SkIntToScalar(kRow4Y + kBlockSize + kBlockSpacing); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 232 | m.setTranslate(0, kBottom); |
| 233 | m.preRotate(15.f, 0, kBottom + kBlockSpacing); |
| 234 | m.preScale(0.71f, 1.22f); |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 235 | matrices.push_back(m); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 236 | |
| 237 | // Align the next set with the middle of the previous in y, translated to the right in x. |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 238 | SkPoint corners[] = {{0, 0}, {0, kBottom}, {kWidth, kBottom}, {kWidth, 0}}; |
| 239 | matrices.back().mapPoints(corners, 4); |
John Stiles | acf7164 | 2021-08-12 22:33:57 -0400 | [diff] [blame^] | 240 | m.setTranslate(std::max({corners[0].fX, corners[1].fX, corners[2].fX, corners[3].fX}), |
| 241 | (corners[0].fY + corners[1].fY + corners[2].fY + corners[3].fY) / 4); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 242 | m.preScale(0.2f, 0.2f); |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 243 | matrices.push_back(m); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 244 | |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 245 | const SkSamplingOptions none(SkFilterMode::kNearest); |
| 246 | const SkSamplingOptions low(SkFilterMode::kLinear); |
Mike Reed | f3ac2af | 2021-02-05 12:55:38 -0500 | [diff] [blame] | 247 | const SkSamplingOptions high(SkCubicResampler::Mitchell()); |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 248 | |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 249 | SkScalar maxX = 0; |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 250 | for (bool antiAlias : {false, true}) { |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 251 | canvas->save(); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 252 | canvas->translate(maxX, 0); |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 253 | for (const SkMatrix& matrix : matrices) { |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 254 | canvas->save(); |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 255 | canvas->concat(matrix); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 256 | |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 257 | // First draw a column with no filtering |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 258 | this->drawCase1(canvas, kCol0X, kRow0Y, antiAlias, none); |
| 259 | this->drawCase2(canvas, kCol0X, kRow1Y, antiAlias, none); |
| 260 | this->drawCase3(canvas, kCol0X, kRow2Y, antiAlias, none); |
| 261 | this->drawCase4(canvas, kCol0X, kRow3Y, antiAlias, none); |
| 262 | this->drawCase5(canvas, kCol0X, kRow4Y, antiAlias, none); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 263 | |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 264 | // Then draw a column with low filtering |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 265 | this->drawCase1(canvas, kCol1X, kRow0Y, antiAlias, low); |
| 266 | this->drawCase2(canvas, kCol1X, kRow1Y, antiAlias, low); |
| 267 | this->drawCase3(canvas, kCol1X, kRow2Y, antiAlias, low); |
| 268 | this->drawCase4(canvas, kCol1X, kRow3Y, antiAlias, low); |
| 269 | this->drawCase5(canvas, kCol1X, kRow4Y, antiAlias, low); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 270 | |
Brian Salomon | 2a7eff9 | 2020-05-19 10:22:03 -0400 | [diff] [blame] | 271 | // Then draw a column with high filtering. Skip it if in kStrict mode and MIP |
| 272 | // mapping will be used. On GPU we allow bleeding at non-base levels because |
| 273 | // building a new MIP chain for the subset is expensive. |
| 274 | SkScalar scales[2]; |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 275 | SkAssertResult(matrix.getMinMaxScales(scales)); |
Brian Salomon | 2a7eff9 | 2020-05-19 10:22:03 -0400 | [diff] [blame] | 276 | if (fConstraint != SkCanvas::kStrict_SrcRectConstraint || scales[0] >= 1.f) { |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 277 | this->drawCase1(canvas, kCol2X, kRow0Y, antiAlias, high); |
| 278 | this->drawCase2(canvas, kCol2X, kRow1Y, antiAlias, high); |
| 279 | this->drawCase3(canvas, kCol2X, kRow2Y, antiAlias, high); |
| 280 | this->drawCase4(canvas, kCol2X, kRow3Y, antiAlias, high); |
| 281 | this->drawCase5(canvas, kCol2X, kRow4Y, antiAlias, high); |
Brian Salomon | 2a7eff9 | 2020-05-19 10:22:03 -0400 | [diff] [blame] | 282 | } |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 283 | |
John Stiles | cbe4e28 | 2020-06-01 10:38:31 -0400 | [diff] [blame] | 284 | SkPoint innerCorners[] = {{0, 0}, {0, kBottom}, {kWidth, kBottom}, {kWidth, 0}}; |
| 285 | matrix.mapPoints(innerCorners, 4); |
| 286 | SkScalar x = kBlockSize + std::max({innerCorners[0].fX, innerCorners[1].fX, |
| 287 | innerCorners[2].fX, innerCorners[3].fX}); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 288 | maxX = std::max(maxX, x); |
bsalomon | 9003d1e | 2015-10-23 11:13:01 -0700 | [diff] [blame] | 289 | canvas->restore(); |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 290 | } |
commit-bot@chromium.org | d6ca4ac | 2013-11-22 20:34:59 +0000 | [diff] [blame] | 291 | canvas->restore(); |
| 292 | } |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 293 | } |
| 294 | |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 295 | void modifyGrContextOptions(GrContextOptions* options) override { |
Brian Salomon | 010bb46 | 2021-01-29 14:42:38 -0500 | [diff] [blame] | 296 | options->fMaxTextureSizeOverride = kMaxTextureSize; |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 297 | } |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 298 | |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 299 | private: |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 300 | static constexpr int kBlockSize = 70; |
| 301 | static constexpr int kBlockSpacing = 12; |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 302 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 303 | static constexpr int kCol0X = kBlockSpacing; |
| 304 | static constexpr int kCol1X = 2*kBlockSpacing + kBlockSize; |
| 305 | static constexpr int kCol2X = 3*kBlockSpacing + 2*kBlockSize; |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 306 | static constexpr int kWidth = 4*kBlockSpacing + 3*kBlockSize; |
robertphillips@google.com | d7ca661 | 2013-08-20 12:09:32 +0000 | [diff] [blame] | 307 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 308 | static constexpr int kRow0Y = kBlockSpacing; |
| 309 | static constexpr int kRow1Y = 2*kBlockSpacing + kBlockSize; |
| 310 | static constexpr int kRow2Y = 3*kBlockSpacing + 2*kBlockSize; |
| 311 | static constexpr int kRow3Y = 4*kBlockSpacing + 3*kBlockSize; |
| 312 | static constexpr int kRow4Y = 5*kBlockSpacing + 4*kBlockSize; |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 313 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 314 | static constexpr int kSmallSize = 6; |
Brian Salomon | 010bb46 | 2021-01-29 14:42:38 -0500 | [diff] [blame] | 315 | // This must be at least as large as the GM width and height so that a surface can be made. |
| 316 | static constexpr int kMaxTextureSize = 1000; |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 317 | |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 318 | SkString fShortName; |
Brian Salomon | 6ef2933 | 2020-05-15 09:52:29 -0400 | [diff] [blame] | 319 | sk_sp<SkImage> fBigImage; |
| 320 | sk_sp<SkImage> fSmallImage; |
Mike Reed | 8d29ab6 | 2021-01-23 18:10:39 -0500 | [diff] [blame] | 321 | SkRect fBigSrcRect; |
| 322 | SkRect fSmallSrcRect; |
Brian Salomon | ca76920 | 2020-05-18 16:40:59 -0400 | [diff] [blame] | 323 | SkCanvas::SrcRectConstraint fConstraint; |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 324 | bool fBatch = false; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 325 | using INHERITED = GM; |
robertphillips@google.com | aaa9b29 | 2013-07-25 21:34:00 +0000 | [diff] [blame] | 326 | }; |
| 327 | |
John Stiles | d20a134 | 2020-05-26 10:38:24 -0400 | [diff] [blame] | 328 | DEF_GM(return new SrcRectConstraintGM("strict_constraint_no_red_allowed", |
| 329 | SkCanvas::kStrict_SrcRectConstraint, |
| 330 | /*batch=*/false);); |
| 331 | DEF_GM(return new SrcRectConstraintGM("strict_constraint_batch_no_red_allowed", |
| 332 | SkCanvas::kStrict_SrcRectConstraint, |
| 333 | /*batch=*/true);); |
| 334 | DEF_GM(return new SrcRectConstraintGM("fast_constraint_red_is_allowed", |
| 335 | SkCanvas::kFast_SrcRectConstraint, |
| 336 | /*batch=*/false);); |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 337 | |
| 338 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 339 | |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 340 | // Construct an image and return the inner "src" rect. Build the image such that the interior is |
| 341 | // blue, with a margin of blue (2px) but then an outer margin of red. |
| 342 | // |
| 343 | // Show that kFast_SrcRectConstraint sees even the red margin (due to mipmapping) when the image |
| 344 | // is scaled down far enough. |
| 345 | // |
| 346 | static sk_sp<SkImage> make_image(SkCanvas* canvas, SkRect* srcR) { |
egdaniel | 26318c9 | 2016-07-26 08:26:46 -0700 | [diff] [blame] | 347 | // Intentially making the size a power of 2 to avoid the noise from how different GPUs will |
| 348 | // produce different mipmap filtering when we have an odd sized texture. |
| 349 | const int N = 10 + 2 + 8 + 2 + 10; |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 350 | SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 351 | auto surface = ToolUtils::makeSurface(canvas, info); |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 352 | SkCanvas* c = surface->getCanvas(); |
| 353 | SkRect r = SkRect::MakeIWH(info.width(), info.height()); |
| 354 | SkPaint paint; |
| 355 | |
| 356 | paint.setColor(SK_ColorRED); |
| 357 | c->drawRect(r, paint); |
egdaniel | 26318c9 | 2016-07-26 08:26:46 -0700 | [diff] [blame] | 358 | r.inset(10, 10); |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 359 | paint.setColor(SK_ColorBLUE); |
| 360 | c->drawRect(r, paint); |
| 361 | |
| 362 | *srcR = r.makeInset(2, 2); |
| 363 | return surface->makeImageSnapshot(); |
| 364 | } |
| 365 | |
| 366 | DEF_SIMPLE_GM(bleed_downscale, canvas, 360, 240) { |
| 367 | SkRect src; |
| 368 | sk_sp<SkImage> img = make_image(canvas, &src); |
| 369 | SkPaint paint; |
| 370 | |
| 371 | canvas->translate(10, 10); |
| 372 | |
| 373 | const SkCanvas::SrcRectConstraint constraints[] = { |
| 374 | SkCanvas::kStrict_SrcRectConstraint, SkCanvas::kFast_SrcRectConstraint |
| 375 | }; |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 376 | const SkSamplingOptions samplings[] = { |
| 377 | SkSamplingOptions(SkFilterMode::kNearest), |
| 378 | SkSamplingOptions(SkFilterMode::kLinear), |
| 379 | SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear), |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 380 | }; |
| 381 | for (auto constraint : constraints) { |
| 382 | canvas->save(); |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 383 | for (auto sampling : samplings) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 384 | auto surf = ToolUtils::makeSurface(canvas, SkImageInfo::MakeN32Premul(1, 1)); |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 385 | surf->getCanvas()->drawImageRect(img, src, SkRect::MakeWH(1, 1), sampling, |
| 386 | nullptr, constraint); |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 387 | // now blow up the 1 pixel result |
Mike Reed | d396cd5 | 2021-01-23 21:14:47 -0500 | [diff] [blame] | 388 | canvas->drawImageRect(surf->makeImageSnapshot(), SkRect::MakeWH(100, 100), |
| 389 | SkSamplingOptions()); |
reed | 2adecda | 2016-07-25 08:11:58 -0700 | [diff] [blame] | 390 | canvas->translate(120, 0); |
| 391 | } |
| 392 | canvas->restore(); |
| 393 | canvas->translate(0, 120); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |