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