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