bleed.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. #include "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkBlurTypes.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkFilterQuality.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkMaskFilter.h"
  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"
  28. #include "include/effects/SkGradientShader.h"
  29. #include "include/gpu/GrContextOptions.h"
  30. #include "include/private/SkTDArray.h"
  31. #include "src/core/SkBlurMask.h"
  32. #include "tools/ToolUtils.h"
  33. /** Holds either a bitmap or image to be rendered and a rect that indicates what part of the bitmap
  34. or image should be tested by the GM. The area outside of the rect is present to check
  35. for bleed due to filtering/blurring. */
  36. struct TestPixels {
  37. enum Type {
  38. kBitmap,
  39. kImage
  40. };
  41. Type fType;
  42. SkBitmap fBitmap;
  43. sk_sp<SkImage> fImage;
  44. SkIRect fRect; // The region of the bitmap/image that should be rendered.
  45. };
  46. /** Creates a bitmap with two one-pixel rings around a checkerboard. The checkerboard is 2x2
  47. logically where each check has as many pixels as is necessary to fill the interior. The rect
  48. to draw is set to the checkerboard portion. */
  49. template<typename PIXEL_TYPE>
  50. bool make_ringed_bitmap(TestPixels* result, int width, int height,
  51. SkColorType ct, SkAlphaType at,
  52. PIXEL_TYPE outerRingColor, PIXEL_TYPE innerRingColor,
  53. PIXEL_TYPE checkColor1, PIXEL_TYPE checkColor2) {
  54. SkASSERT(0 == width % 2 && 0 == height % 2);
  55. SkASSERT(width >= 6 && height >= 6);
  56. result->fType = TestPixels::kBitmap;
  57. SkImageInfo info = SkImageInfo::Make(width, height, ct, at);
  58. size_t rowBytes = SkAlign4(info.minRowBytes());
  59. result->fBitmap.allocPixels(info, rowBytes);
  60. PIXEL_TYPE* scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, 0);
  61. for (int x = 0; x < width; ++x) {
  62. scanline[x] = outerRingColor;
  63. }
  64. scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, 1);
  65. scanline[0] = outerRingColor;
  66. for (int x = 1; x < width - 1; ++x) {
  67. scanline[x] = innerRingColor;
  68. }
  69. scanline[width - 1] = outerRingColor;
  70. for (int y = 2; y < height / 2; ++y) {
  71. scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, y);
  72. scanline[0] = outerRingColor;
  73. scanline[1] = innerRingColor;
  74. for (int x = 2; x < width / 2; ++x) {
  75. scanline[x] = checkColor1;
  76. }
  77. for (int x = width / 2; x < width - 2; ++x) {
  78. scanline[x] = checkColor2;
  79. }
  80. scanline[width - 2] = innerRingColor;
  81. scanline[width - 1] = outerRingColor;
  82. }
  83. for (int y = height / 2; y < height - 2; ++y) {
  84. scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, y);
  85. scanline[0] = outerRingColor;
  86. scanline[1] = innerRingColor;
  87. for (int x = 2; x < width / 2; ++x) {
  88. scanline[x] = checkColor2;
  89. }
  90. for (int x = width / 2; x < width - 2; ++x) {
  91. scanline[x] = checkColor1;
  92. }
  93. scanline[width - 2] = innerRingColor;
  94. scanline[width - 1] = outerRingColor;
  95. }
  96. scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, height - 2);
  97. scanline[0] = outerRingColor;
  98. for (int x = 1; x < width - 1; ++x) {
  99. scanline[x] = innerRingColor;
  100. }
  101. scanline[width - 1] = outerRingColor;
  102. scanline = (PIXEL_TYPE*)result->fBitmap.getAddr(0, height - 1);
  103. for (int x = 0; x < width; ++x) {
  104. scanline[x] = outerRingColor;
  105. }
  106. result->fBitmap.setImmutable();
  107. result->fRect.set(2, 2, width - 2, height - 2);
  108. return true;
  109. }
  110. /** Create a black and white checked bitmap with 2 1-pixel rings around the outside edge.
  111. The inner ring is red and the outer ring is blue. */
  112. static bool make_ringed_color_bitmap(TestPixels* result, int width, int height) {
  113. const SkPMColor kBlue = SkPreMultiplyColor(SK_ColorBLUE);
  114. const SkPMColor kRed = SkPreMultiplyColor(SK_ColorRED);
  115. const SkPMColor kBlack = SkPreMultiplyColor(SK_ColorBLACK);
  116. const SkPMColor kWhite = SkPreMultiplyColor(SK_ColorWHITE);
  117. return make_ringed_bitmap<SkPMColor>(result, width, height, kN32_SkColorType,
  118. kPremul_SkAlphaType, kBlue, kRed, kBlack, kWhite);
  119. }
  120. /** Makes a alpha bitmap with 1 wide rect/ring of 0s, an inset of 1s, and the interior is a 2x2
  121. checker board of 3/4 and 1/2. The inner checkers are large enough to fill the interior with
  122. the 2x2 checker grid. */
  123. static bool make_ringed_alpha_bitmap(TestPixels* result, int width, int height) {
  124. constexpr uint8_t kZero = 0x00;
  125. constexpr uint8_t kHalf = 0x80;
  126. constexpr uint8_t k3Q = 0xC0;
  127. constexpr uint8_t kOne = 0xFF;
  128. return make_ringed_bitmap<uint8_t>(result, width, height, kAlpha_8_SkColorType,
  129. kPremul_SkAlphaType, kZero, kOne, k3Q, kHalf);
  130. }
  131. /** Helper to reuse above functions to produce images rather than bmps */
  132. static void bmp_to_image(TestPixels* result) {
  133. SkASSERT(TestPixels::kBitmap == result->fType);
  134. result->fImage = SkImage::MakeFromBitmap(result->fBitmap);
  135. SkASSERT(result->fImage);
  136. result->fType = TestPixels::kImage;
  137. result->fBitmap.reset();
  138. }
  139. /** Color image case. */
  140. bool make_ringed_color_image(TestPixels* result, int width, int height) {
  141. if (make_ringed_color_bitmap(result, width, height)) {
  142. bmp_to_image(result);
  143. return true;
  144. }
  145. return false;
  146. }
  147. /** Alpha image case. */
  148. bool make_ringed_alpha_image(TestPixels* result, int width, int height) {
  149. if (make_ringed_alpha_bitmap(result, width, height)) {
  150. bmp_to_image(result);
  151. return true;
  152. }
  153. return false;
  154. }
  155. static sk_sp<SkShader> make_shader() {
  156. constexpr SkPoint pts[] = { {0, 0}, {20, 20} };
  157. constexpr SkColor colors[] = { SK_ColorGREEN, SK_ColorYELLOW };
  158. return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kMirror);
  159. }
  160. static sk_sp<SkShader> make_null_shader() { return nullptr; }
  161. enum BleedTest {
  162. kUseBitmap_BleedTest,
  163. kUseImage_BleedTest,
  164. kUseAlphaBitmap_BleedTest,
  165. kUseAlphaImage_BleedTest,
  166. kUseAlphaBitmapShader_BleedTest,
  167. kUseAlphaImageShader_BleedTest,
  168. };
  169. const struct {
  170. const char* fName;
  171. bool (*fPixelMaker)(TestPixels* result, int width, int height);
  172. sk_sp<SkShader> (*fShaderMaker)();
  173. } gBleedRec[] = {
  174. { "bleed", make_ringed_color_bitmap, make_null_shader },
  175. { "bleed_image", make_ringed_color_image, make_null_shader },
  176. { "bleed_alpha_bmp", make_ringed_alpha_bitmap, make_null_shader },
  177. { "bleed_alpha_image", make_ringed_alpha_image, make_null_shader },
  178. { "bleed_alpha_bmp_shader", make_ringed_alpha_bitmap, make_shader },
  179. { "bleed_alpha_image_shader", make_ringed_alpha_image, make_shader },
  180. };
  181. /** This GM exercises the behavior of the drawBitmapRect & drawImageRect calls. Specifically their
  182. handling of :
  183. - SrcRectConstraint(bleed vs.no - bleed)
  184. - handling of the sub - region feature(area - of - interest) of drawBitmap*
  185. - handling of 8888 vs. A8 (including presence of a shader in the A8 case).
  186. In particular, we should never see the padding outside of an SkBitmap's sub - region (green for
  187. 8888, 1/4 for alpha). In some instances we can see the two outer rings outside of the area o
  188. interest (i.e., the inner four checks) due to AA or filtering if allowed by the
  189. SrcRectConstraint.
  190. */
  191. class BleedGM : public skiagm::GM {
  192. public:
  193. BleedGM(BleedTest bt) : fBT(bt){}
  194. protected:
  195. SkString onShortName() override {
  196. return SkString(gBleedRec[fBT].fName);
  197. }
  198. SkISize onISize() override {
  199. return SkISize::Make(1200, 1080);
  200. }
  201. void drawPixels(SkCanvas* canvas, const TestPixels& pixels, const SkRect& src,
  202. const SkRect& dst, const SkPaint* paint,
  203. SkCanvas::SrcRectConstraint constraint) {
  204. if (TestPixels::kBitmap == pixels.fType) {
  205. canvas->drawBitmapRect(pixels.fBitmap, src, dst, paint, constraint);
  206. } else {
  207. canvas->drawImageRect(pixels.fImage.get(), src, dst, paint, constraint);
  208. }
  209. }
  210. // Draw the area of interest of the small image
  211. void drawCase1(SkCanvas* canvas, int transX, int transY, bool aa,
  212. SkCanvas::SrcRectConstraint constraint, SkFilterQuality filter) {
  213. SkRect src = SkRect::Make(fSmallTestPixels.fRect);
  214. SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
  215. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  216. SkPaint paint;
  217. paint.setFilterQuality(filter);
  218. paint.setShader(fShader);
  219. paint.setColor(SK_ColorBLUE);
  220. paint.setAntiAlias(aa);
  221. this->drawPixels(canvas, fSmallTestPixels, src, dst, &paint, constraint);
  222. }
  223. // Draw the area of interest of the large image
  224. void drawCase2(SkCanvas* canvas, int transX, int transY, bool aa,
  225. SkCanvas::SrcRectConstraint constraint, SkFilterQuality filter) {
  226. SkRect src = SkRect::Make(fBigTestPixels.fRect);
  227. SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
  228. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  229. SkPaint paint;
  230. paint.setFilterQuality(filter);
  231. paint.setShader(fShader);
  232. paint.setColor(SK_ColorBLUE);
  233. paint.setAntiAlias(aa);
  234. this->drawPixels(canvas, fBigTestPixels, src, dst, &paint, constraint);
  235. }
  236. // Draw upper-left 1/4 of the area of interest of the large image
  237. void drawCase3(SkCanvas* canvas, int transX, int transY, bool aa,
  238. SkCanvas::SrcRectConstraint constraint, SkFilterQuality filter) {
  239. SkRect src = SkRect::MakeXYWH(SkIntToScalar(fBigTestPixels.fRect.fLeft),
  240. SkIntToScalar(fBigTestPixels.fRect.fTop),
  241. fBigTestPixels.fRect.width()/2.f,
  242. fBigTestPixels.fRect.height()/2.f);
  243. SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
  244. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  245. SkPaint paint;
  246. paint.setFilterQuality(filter);
  247. paint.setShader(fShader);
  248. paint.setColor(SK_ColorBLUE);
  249. paint.setAntiAlias(aa);
  250. this->drawPixels(canvas, fBigTestPixels, src, dst, &paint, constraint);
  251. }
  252. // Draw the area of interest of the small image with a normal blur
  253. void drawCase4(SkCanvas* canvas, int transX, int transY, bool aa,
  254. SkCanvas::SrcRectConstraint constraint, SkFilterQuality filter) {
  255. SkRect src = SkRect::Make(fSmallTestPixels.fRect);
  256. SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
  257. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  258. SkPaint paint;
  259. paint.setFilterQuality(filter);
  260. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
  261. SkBlurMask::ConvertRadiusToSigma(3)));
  262. paint.setShader(fShader);
  263. paint.setColor(SK_ColorBLUE);
  264. paint.setAntiAlias(aa);
  265. this->drawPixels(canvas, fSmallTestPixels, src, dst, &paint, constraint);
  266. }
  267. // Draw the area of interest of the small image with a outer blur
  268. void drawCase5(SkCanvas* canvas, int transX, int transY, bool aa,
  269. SkCanvas::SrcRectConstraint constraint, SkFilterQuality filter) {
  270. SkRect src = SkRect::Make(fSmallTestPixels.fRect);
  271. SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
  272. SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
  273. SkPaint paint;
  274. paint.setFilterQuality(filter);
  275. paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle,
  276. SkBlurMask::ConvertRadiusToSigma(7)));
  277. paint.setShader(fShader);
  278. paint.setColor(SK_ColorBLUE);
  279. paint.setAntiAlias(aa);
  280. this->drawPixels(canvas, fSmallTestPixels, src, dst, &paint, constraint);
  281. }
  282. void onOnceBeforeDraw() override {
  283. SkAssertResult(gBleedRec[fBT].fPixelMaker(&fSmallTestPixels, kSmallSize, kSmallSize));
  284. SkAssertResult(gBleedRec[fBT].fPixelMaker(&fBigTestPixels, 2 * kMaxTileSize,
  285. 2 * kMaxTileSize));
  286. }
  287. void onDraw(SkCanvas* canvas) override {
  288. fShader = gBleedRec[fBT].fShaderMaker();
  289. canvas->clear(SK_ColorGRAY);
  290. SkTDArray<SkMatrix> matrices;
  291. // Draw with identity
  292. *matrices.append() = SkMatrix::I();
  293. // Draw with rotation and scale down in x, up in y.
  294. SkMatrix m;
  295. constexpr SkScalar kBottom = SkIntToScalar(kRow4Y + kBlockSize + kBlockSpacing);
  296. m.setTranslate(0, kBottom);
  297. m.preRotate(15.f, 0, kBottom + kBlockSpacing);
  298. m.preScale(0.71f, 1.22f);
  299. *matrices.append() = m;
  300. // Align the next set with the middle of the previous in y, translated to the right in x.
  301. SkPoint corners[] = {{0, 0}, { 0, kBottom }, { kWidth, kBottom }, {kWidth, 0} };
  302. matrices[matrices.count()-1].mapPoints(corners, 4);
  303. SkScalar y = (corners[0].fY + corners[1].fY + corners[2].fY + corners[3].fY) / 4;
  304. SkScalar x = SkTMax(SkTMax(corners[0].fX, corners[1].fX),
  305. SkTMax(corners[2].fX, corners[3].fX));
  306. m.setTranslate(x, y);
  307. m.preScale(0.2f, 0.2f);
  308. *matrices.append() = m;
  309. SkScalar maxX = 0;
  310. for (int antiAlias = 0; antiAlias < 2; ++antiAlias) {
  311. canvas->save();
  312. canvas->translate(maxX, 0);
  313. for (int m = 0; m < matrices.count(); ++m) {
  314. canvas->save();
  315. canvas->concat(matrices[m]);
  316. bool aa = SkToBool(antiAlias);
  317. // First draw a column with no bleeding and no filtering
  318. this->drawCase1(canvas, kCol0X, kRow0Y, aa, SkCanvas::kStrict_SrcRectConstraint, kNone_SkFilterQuality);
  319. this->drawCase2(canvas, kCol0X, kRow1Y, aa, SkCanvas::kStrict_SrcRectConstraint, kNone_SkFilterQuality);
  320. this->drawCase3(canvas, kCol0X, kRow2Y, aa, SkCanvas::kStrict_SrcRectConstraint, kNone_SkFilterQuality);
  321. this->drawCase4(canvas, kCol0X, kRow3Y, aa, SkCanvas::kStrict_SrcRectConstraint, kNone_SkFilterQuality);
  322. this->drawCase5(canvas, kCol0X, kRow4Y, aa, SkCanvas::kStrict_SrcRectConstraint, kNone_SkFilterQuality);
  323. // Then draw a column with no bleeding and low filtering
  324. this->drawCase1(canvas, kCol1X, kRow0Y, aa, SkCanvas::kStrict_SrcRectConstraint, kLow_SkFilterQuality);
  325. this->drawCase2(canvas, kCol1X, kRow1Y, aa, SkCanvas::kStrict_SrcRectConstraint, kLow_SkFilterQuality);
  326. this->drawCase3(canvas, kCol1X, kRow2Y, aa, SkCanvas::kStrict_SrcRectConstraint, kLow_SkFilterQuality);
  327. this->drawCase4(canvas, kCol1X, kRow3Y, aa, SkCanvas::kStrict_SrcRectConstraint, kLow_SkFilterQuality);
  328. this->drawCase5(canvas, kCol1X, kRow4Y, aa, SkCanvas::kStrict_SrcRectConstraint, kLow_SkFilterQuality);
  329. // Then draw a column with no bleeding and high filtering
  330. this->drawCase1(canvas, kCol2X, kRow0Y, aa, SkCanvas::kStrict_SrcRectConstraint, kHigh_SkFilterQuality);
  331. this->drawCase2(canvas, kCol2X, kRow1Y, aa, SkCanvas::kStrict_SrcRectConstraint, kHigh_SkFilterQuality);
  332. this->drawCase3(canvas, kCol2X, kRow2Y, aa, SkCanvas::kStrict_SrcRectConstraint, kHigh_SkFilterQuality);
  333. this->drawCase4(canvas, kCol2X, kRow3Y, aa, SkCanvas::kStrict_SrcRectConstraint, kHigh_SkFilterQuality);
  334. this->drawCase5(canvas, kCol2X, kRow4Y, aa, SkCanvas::kStrict_SrcRectConstraint, kHigh_SkFilterQuality);
  335. // Then draw a column with bleeding and no filtering (bleed should have no effect w/out blur)
  336. this->drawCase1(canvas, kCol3X, kRow0Y, aa, SkCanvas::kFast_SrcRectConstraint, kNone_SkFilterQuality);
  337. this->drawCase2(canvas, kCol3X, kRow1Y, aa, SkCanvas::kFast_SrcRectConstraint, kNone_SkFilterQuality);
  338. this->drawCase3(canvas, kCol3X, kRow2Y, aa, SkCanvas::kFast_SrcRectConstraint, kNone_SkFilterQuality);
  339. this->drawCase4(canvas, kCol3X, kRow3Y, aa, SkCanvas::kFast_SrcRectConstraint, kNone_SkFilterQuality);
  340. this->drawCase5(canvas, kCol3X, kRow4Y, aa, SkCanvas::kFast_SrcRectConstraint, kNone_SkFilterQuality);
  341. // Then draw a column with bleeding and low filtering
  342. this->drawCase1(canvas, kCol4X, kRow0Y, aa, SkCanvas::kFast_SrcRectConstraint, kLow_SkFilterQuality);
  343. this->drawCase2(canvas, kCol4X, kRow1Y, aa, SkCanvas::kFast_SrcRectConstraint, kLow_SkFilterQuality);
  344. this->drawCase3(canvas, kCol4X, kRow2Y, aa, SkCanvas::kFast_SrcRectConstraint, kLow_SkFilterQuality);
  345. this->drawCase4(canvas, kCol4X, kRow3Y, aa, SkCanvas::kFast_SrcRectConstraint, kLow_SkFilterQuality);
  346. this->drawCase5(canvas, kCol4X, kRow4Y, aa, SkCanvas::kFast_SrcRectConstraint, kLow_SkFilterQuality);
  347. // Finally draw a column with bleeding and high filtering
  348. this->drawCase1(canvas, kCol5X, kRow0Y, aa, SkCanvas::kFast_SrcRectConstraint, kHigh_SkFilterQuality);
  349. this->drawCase2(canvas, kCol5X, kRow1Y, aa, SkCanvas::kFast_SrcRectConstraint, kHigh_SkFilterQuality);
  350. this->drawCase3(canvas, kCol5X, kRow2Y, aa, SkCanvas::kFast_SrcRectConstraint, kHigh_SkFilterQuality);
  351. this->drawCase4(canvas, kCol5X, kRow3Y, aa, SkCanvas::kFast_SrcRectConstraint, kHigh_SkFilterQuality);
  352. this->drawCase5(canvas, kCol5X, kRow4Y, aa, SkCanvas::kFast_SrcRectConstraint, kHigh_SkFilterQuality);
  353. SkPoint corners[] = { { 0, 0 },{ 0, kBottom },{ kWidth, kBottom },{ kWidth, 0 } };
  354. matrices[m].mapPoints(corners, 4);
  355. SkScalar x = kBlockSize + SkTMax(SkTMax(corners[0].fX, corners[1].fX),
  356. SkTMax(corners[2].fX, corners[3].fX));
  357. maxX = SkTMax(maxX, x);
  358. canvas->restore();
  359. }
  360. canvas->restore();
  361. }
  362. }
  363. void modifyGrContextOptions(GrContextOptions* options) override {
  364. options->fMaxTileSizeOverride = kMaxTileSize;
  365. }
  366. private:
  367. static constexpr int kBlockSize = 70;
  368. static constexpr int kBlockSpacing = 12;
  369. static constexpr int kCol0X = kBlockSpacing;
  370. static constexpr int kCol1X = 2*kBlockSpacing + kBlockSize;
  371. static constexpr int kCol2X = 3*kBlockSpacing + 2*kBlockSize;
  372. static constexpr int kCol3X = 4*kBlockSpacing + 3*kBlockSize;
  373. static constexpr int kCol4X = 5*kBlockSpacing + 4*kBlockSize;
  374. static constexpr int kCol5X = 6*kBlockSpacing + 5*kBlockSize;
  375. static constexpr int kWidth = 7*kBlockSpacing + 6*kBlockSize;
  376. static constexpr int kRow0Y = kBlockSpacing;
  377. static constexpr int kRow1Y = 2*kBlockSpacing + kBlockSize;
  378. static constexpr int kRow2Y = 3*kBlockSpacing + 2*kBlockSize;
  379. static constexpr int kRow3Y = 4*kBlockSpacing + 3*kBlockSize;
  380. static constexpr int kRow4Y = 5*kBlockSpacing + 4*kBlockSize;
  381. static constexpr int kSmallSize = 6;
  382. static constexpr int kMaxTileSize = 32;
  383. TestPixels fBigTestPixels;
  384. TestPixels fSmallTestPixels;
  385. sk_sp<SkShader> fShader;
  386. const BleedTest fBT;
  387. typedef GM INHERITED;
  388. };
  389. DEF_GM( return new BleedGM(kUseBitmap_BleedTest); )
  390. DEF_GM( return new BleedGM(kUseImage_BleedTest); )
  391. DEF_GM( return new BleedGM(kUseAlphaBitmap_BleedTest); )
  392. DEF_GM( return new BleedGM(kUseAlphaImage_BleedTest); )
  393. DEF_GM( return new BleedGM(kUseAlphaBitmapShader_BleedTest); )
  394. DEF_GM( return new BleedGM(kUseAlphaImageShader_BleedTest); )
  395. ///////////////////////////////////////////////////////////////////////////////////////////////////
  396. // Construct an image and return the inner "src" rect. Build the image such that the interior is
  397. // blue, with a margin of blue (2px) but then an outer margin of red.
  398. //
  399. // Show that kFast_SrcRectConstraint sees even the red margin (due to mipmapping) when the image
  400. // is scaled down far enough.
  401. //
  402. static sk_sp<SkImage> make_image(SkCanvas* canvas, SkRect* srcR) {
  403. // Intentially making the size a power of 2 to avoid the noise from how different GPUs will
  404. // produce different mipmap filtering when we have an odd sized texture.
  405. const int N = 10 + 2 + 8 + 2 + 10;
  406. SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
  407. auto surface = ToolUtils::makeSurface(canvas, info);
  408. SkCanvas* c = surface->getCanvas();
  409. SkRect r = SkRect::MakeIWH(info.width(), info.height());
  410. SkPaint paint;
  411. paint.setColor(SK_ColorRED);
  412. c->drawRect(r, paint);
  413. r.inset(10, 10);
  414. paint.setColor(SK_ColorBLUE);
  415. c->drawRect(r, paint);
  416. *srcR = r.makeInset(2, 2);
  417. return surface->makeImageSnapshot();
  418. }
  419. DEF_SIMPLE_GM(bleed_downscale, canvas, 360, 240) {
  420. SkRect src;
  421. sk_sp<SkImage> img = make_image(canvas, &src);
  422. SkPaint paint;
  423. canvas->translate(10, 10);
  424. const SkCanvas::SrcRectConstraint constraints[] = {
  425. SkCanvas::kStrict_SrcRectConstraint, SkCanvas::kFast_SrcRectConstraint
  426. };
  427. const SkFilterQuality qualities[] = {
  428. kNone_SkFilterQuality, kLow_SkFilterQuality, kMedium_SkFilterQuality
  429. };
  430. for (auto constraint : constraints) {
  431. canvas->save();
  432. for (auto quality : qualities) {
  433. paint.setFilterQuality(quality);
  434. auto surf = ToolUtils::makeSurface(canvas, SkImageInfo::MakeN32Premul(1, 1));
  435. surf->getCanvas()->drawImageRect(img, src, SkRect::MakeWH(1, 1), &paint, constraint);
  436. // now blow up the 1 pixel result
  437. canvas->drawImageRect(surf->makeImageSnapshot(), SkRect::MakeWH(100, 100), nullptr);
  438. canvas->translate(120, 0);
  439. }
  440. canvas->restore();
  441. canvas->translate(0, 120);
  442. }
  443. }