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/gm.h"
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"
14 #include "include/core/SkPath.h"
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"
21 #include "include/core/SkSurface.h"
22 
23 namespace skiagm {
24 
25 constexpr SkRect kSrcImageClip{75, 75, 275, 275};
26 
27 /*
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.
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
34  * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
35  */
36 class CroppedRectsGM : public GM {
37 private:
onShortName()38     SkString onShortName() final { return SkString("croppedrects"); }
onISize()39     SkISize onISize() override { return SkISize::Make(500, 500); }
40 
onOnceBeforeDraw()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 
58         fSrcImage = srcSurface->makeImageSnapshot();
59         fSrcImageShader = fSrcImage->makeShader();
60     }
61 
onDraw(SkCanvas * canvas)62     void onDraw(SkCanvas* canvas) override {
63         canvas->clear(SK_ColorWHITE);
64 
65         {
66             // GrRenderTargetContext::drawFilledRect.
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         {
76             // GrRenderTargetContext::fillRectToRect.
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         {
90             // GrRenderTargetContext::fillRectWithLocalMatrix.
91             SkAutoCanvasRestore acr(canvas, true);
92             SkPath path = SkPath::Line(
93                    {kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
94                    {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);
100             canvas->translate(23, 301);
101             canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
102             canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
103             canvas->clipRect(kSrcImageClip);
104             canvas->drawPath(path, paint);
105         }
106 
107         // TODO: assert the draw target only has one op in the post-MDB world.
108     }
109 
110     sk_sp<SkImage> fSrcImage;
111     sk_sp<SkShader> fSrcImageShader;
112 
113     using INHERITED = GM;
114 };
115 
116 DEF_GM( return new CroppedRectsGM(); )
117 
118 }  // namespace skiagm
119