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