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