2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
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"
20 #include "include/core/SkSurface.h"
21 #include "tools/ToolUtils.h"
25 constexpr SkRect kSrcImageClip{75, 75, 275, 275};
27 static sk_sp<SkImage> create_image(SkCanvas* destCanvas) {
28 sk_sp<SkSurface> srcSurface = SkSurface::MakeRasterN32Premul(500, 500);
29 SkCanvas* srcCanvas = srcSurface->getCanvas();
31 srcCanvas->clear(SK_ColorRED);
34 paint.setColor(0xff00ff00);
35 srcCanvas->drawRect(kSrcImageClip, paint);
37 constexpr SkScalar kStrokeWidth = 10;
39 stroke.setStyle(SkPaint::kStroke_Style);
40 stroke.setStrokeWidth(kStrokeWidth);
41 stroke.setColor(0xff008800);
42 srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
44 return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot());
48 * The purpose of this test is to exercise all three codepaths in skgpu::v1::SurfaceDrawContext
49 * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
52 * The test creates an image of a green square surrounded by red background, then draws this image
53 * in various ways with the red clipped out. The test is successful if there is no visible red
54 * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
56 class CroppedRectsGM : public GM {
58 SkString onShortName() final { return SkString("croppedrects"); }
59 SkISize onISize() override { return SkISize::Make(500, 500); }
61 void onDraw(SkCanvas* canvas) override {
63 fSrcImage = create_image(canvas);
64 fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
67 canvas->clear(SK_ColorWHITE);
70 // skgpu::v1::SurfaceDrawContext::drawFilledRect.
71 SkAutoCanvasRestore acr(canvas, true);
73 paint.setShader(fSrcImageShader);
74 canvas->clipRect(kSrcImageClip);
75 canvas->drawPaint(paint);
79 // skgpu::v1::SurfaceDrawContext::fillRectToRect.
80 SkAutoCanvasRestore acr(canvas, true);
81 SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
82 canvas->clipRect(drawRect);
83 canvas->drawImageRect(fSrcImage.get(),
84 kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(),
85 kSrcImageClip.height()),
86 drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
87 SkSamplingOptions(), nullptr,
88 SkCanvas::kStrict_SrcRectConstraint);
92 // skgpu::v1::SurfaceDrawContext::fillRectWithLocalMatrix.
93 SkAutoCanvasRestore acr(canvas, true);
94 SkPath path = SkPath::Line(
95 {kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
96 {kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()});
98 paint.setStyle(SkPaint::kStroke_Style);
99 paint.setStrokeWidth(2 * kSrcImageClip.height());
100 paint.setShader(fSrcImageShader);
101 canvas->translate(23, 301);
102 canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
103 canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
104 canvas->clipRect(kSrcImageClip);
105 canvas->drawPath(path, paint);
108 // TODO: assert the draw target only has one op in the post-MDB world.
111 sk_sp<SkImage> fSrcImage;
112 sk_sp<SkShader> fSrcImageShader;
114 using INHERITED = GM;
117 DEF_GM( return new CroppedRectsGM(); )
119 } // namespace skiagm