croppedrects.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2016 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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFilterQuality.h"
  11. #include "include/core/SkImage.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkRefCnt.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkSurface.h"
  21. namespace skiagm {
  22. constexpr SkRect kSrcImageClip{75, 75, 275, 275};
  23. /*
  24. * The purpose of this test is to exercise all three codepaths in GrRenderTargetContext
  25. * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
  26. * clip.
  27. *
  28. * The test creates an image of a green square surrounded by red background, then draws this image
  29. * in various ways with the red clipped out. The test is successful if there is no visible red
  30. * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
  31. */
  32. class CroppedRectsGM : public GM {
  33. private:
  34. SkString onShortName() override final { return SkString("croppedrects"); }
  35. SkISize onISize() override { return SkISize::Make(500, 500); }
  36. void onOnceBeforeDraw() override {
  37. sk_sp<SkSurface> srcSurface = SkSurface::MakeRasterN32Premul(500, 500);
  38. SkCanvas* srcCanvas = srcSurface->getCanvas();
  39. srcCanvas->clear(SK_ColorRED);
  40. SkPaint paint;
  41. paint.setColor(0xff00ff00);
  42. srcCanvas->drawRect(kSrcImageClip, paint);
  43. constexpr SkScalar kStrokeWidth = 10;
  44. SkPaint stroke;
  45. stroke.setStyle(SkPaint::kStroke_Style);
  46. stroke.setStrokeWidth(kStrokeWidth);
  47. stroke.setColor(0xff008800);
  48. srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
  49. fSrcImage = srcSurface->makeImageSnapshot();
  50. fSrcImageShader = fSrcImage->makeShader();
  51. }
  52. void onDraw(SkCanvas* canvas) override {
  53. canvas->clear(SK_ColorWHITE);
  54. {
  55. // GrRenderTargetContext::drawFilledRect.
  56. SkAutoCanvasRestore acr(canvas, true);
  57. SkPaint paint;
  58. paint.setShader(fSrcImageShader);
  59. paint.setFilterQuality(kNone_SkFilterQuality);
  60. canvas->clipRect(kSrcImageClip);
  61. canvas->drawPaint(paint);
  62. }
  63. {
  64. // GrRenderTargetContext::fillRectToRect.
  65. SkAutoCanvasRestore acr(canvas, true);
  66. SkPaint paint;
  67. paint.setFilterQuality(kNone_SkFilterQuality);
  68. SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
  69. canvas->clipRect(drawRect);
  70. canvas->drawImageRect(fSrcImage.get(),
  71. kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(),
  72. kSrcImageClip.height()),
  73. drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
  74. &paint);
  75. }
  76. {
  77. // GrRenderTargetContext::fillRectWithLocalMatrix.
  78. SkAutoCanvasRestore acr(canvas, true);
  79. SkPath path;
  80. path.moveTo(kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY());
  81. path.lineTo(kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY());
  82. SkPaint paint;
  83. paint.setStyle(SkPaint::kStroke_Style);
  84. paint.setStrokeWidth(2 * kSrcImageClip.height());
  85. paint.setShader(fSrcImageShader);
  86. paint.setFilterQuality(kNone_SkFilterQuality);
  87. canvas->translate(23, 301);
  88. canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
  89. canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
  90. canvas->clipRect(kSrcImageClip);
  91. canvas->drawPath(path, paint);
  92. }
  93. // TODO: assert the draw target only has one op in the post-MDB world.
  94. }
  95. sk_sp<SkImage> fSrcImage;
  96. sk_sp<SkShader> fSrcImageShader;
  97. typedef GM INHERITED;
  98. };
  99. DEF_GM( return new CroppedRectsGM(); )
  100. }