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 | |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 49 | fSrcImage = srcSurface->makeImageSnapshot(SkBudgeted::kYes); |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 50 | fSrcImageShader = fSrcImage->makeShader(SkShader::kClamp_TileMode, |
| 51 | SkShader::kClamp_TileMode); |
| 52 | } |
| 53 | |
| 54 | void onDraw(SkCanvas* canvas) override { |
| 55 | canvas->clear(SK_ColorWHITE); |
| 56 | |
| 57 | { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 58 | // GrRenderTargetContext::drawFilledRect. |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 59 | SkAutoCanvasRestore acr(canvas, true); |
| 60 | SkPaint paint; |
| 61 | paint.setShader(fSrcImageShader); |
| 62 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 63 | canvas->clipRect(kSrcImageClip); |
| 64 | canvas->drawPaint(paint); |
| 65 | } |
| 66 | |
| 67 | { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 68 | // GrRenderTargetContext::fillRectToRect. |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 69 | SkAutoCanvasRestore acr(canvas, true); |
| 70 | SkPaint paint; |
| 71 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 72 | SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300); |
| 73 | canvas->clipRect(drawRect); |
| 74 | canvas->drawImageRect(fSrcImage.get(), |
| 75 | kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(), |
| 76 | kSrcImageClip.height()), |
| 77 | drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()), |
| 78 | &paint); |
| 79 | } |
| 80 | |
| 81 | { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 82 | // GrRenderTargetContext::fillRectWithLocalMatrix. |
csmartdalton | 97f6cd5 | 2016-07-13 13:37:08 -0700 | [diff] [blame] | 83 | SkAutoCanvasRestore acr(canvas, true); |
| 84 | SkPath path; |
| 85 | path.moveTo(kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()); |
| 86 | path.lineTo(kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()); |
| 87 | SkPaint paint; |
| 88 | paint.setStyle(SkPaint::kStroke_Style); |
| 89 | paint.setStrokeWidth(2 * kSrcImageClip.height()); |
| 90 | paint.setShader(fSrcImageShader); |
| 91 | paint.setFilterQuality(kNone_SkFilterQuality); |
| 92 | canvas->translate(-90, 263); |
| 93 | canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height()); |
| 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 | } |