blob: dd14afbe68312df3b9ecdb640b889cfb9b07992a [file] [log] [blame]
csmartdalton97f6cd52016-07-13 13:37:08 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkImage.h"
12#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#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 Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
Kevin Lubick9b028372023-10-05 15:04:54 -040021#include "tools/GpuToolUtils.h"
Robert Phillips0fb10ab2022-04-20 14:57:03 -040022#include "tools/ToolUtils.h"
csmartdalton97f6cd52016-07-13 13:37:08 -070023
24namespace skiagm {
25
26constexpr SkRect kSrcImageClip{75, 75, 275, 275};
27
Robert Phillips0fb10ab2022-04-20 14:57:03 -040028static sk_sp<SkImage> create_image(SkCanvas* destCanvas) {
Kevin Lubick5c93acf2023-05-09 12:11:43 -040029 sk_sp<SkSurface> srcSurface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(500, 500));
Robert Phillips0fb10ab2022-04-20 14:57:03 -040030 SkCanvas* srcCanvas = srcSurface->getCanvas();
31
32 srcCanvas->clear(SK_ColorRED);
33
34 SkPaint paint;
35 paint.setColor(0xff00ff00);
36 srcCanvas->drawRect(kSrcImageClip, paint);
37
38 constexpr SkScalar kStrokeWidth = 10;
39 SkPaint stroke;
40 stroke.setStyle(SkPaint::kStroke_Style);
41 stroke.setStrokeWidth(kStrokeWidth);
42 stroke.setColor(0xff008800);
43 srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
44
45 return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot());
46}
47
csmartdalton97f6cd52016-07-13 13:37:08 -070048/*
Kevin Lubickf24283f2023-03-17 16:10:23 -040049 * The purpose of this test is to exercise all three codepaths in skgpu::ganesh::SurfaceDrawContext
Brian Osman11052242016-10-27 14:47:55 -040050 * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
51 * clip.
csmartdalton97f6cd52016-07-13 13:37:08 -070052 *
53 * The test creates an image of a green square surrounded by red background, then draws this image
54 * in various ways with the red clipped out. The test is successful if there is no visible red
Brian Salomon09d994e2016-12-21 11:14:46 -050055 * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
csmartdalton97f6cd52016-07-13 13:37:08 -070056 */
57class CroppedRectsGM : public GM {
58private:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000059 SkString getName() const override { return SkString("croppedrects"); }
Leandro Lovisolo8f023882023-08-15 21:13:52 +000060 SkISize getISize() override { return SkISize::Make(500, 500); }
csmartdalton97f6cd52016-07-13 13:37:08 -070061
Robert Phillips163a7ea2022-04-19 20:55:02 +000062 void onDraw(SkCanvas* canvas) override {
Robert Phillips0fb10ab2022-04-20 14:57:03 -040063 if (!fSrcImage) {
64 fSrcImage = create_image(canvas);
Kevin Lubickc2ec5d02022-10-17 15:49:00 -040065 if (fSrcImage) {
66 fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
67 }
Robert Phillips0fb10ab2022-04-20 14:57:03 -040068 }
69
csmartdalton97f6cd52016-07-13 13:37:08 -070070 canvas->clear(SK_ColorWHITE);
71
72 {
Kevin Lubickf24283f2023-03-17 16:10:23 -040073 // skgpu::ganesh::SurfaceDrawContext::drawFilledRect.
csmartdalton97f6cd52016-07-13 13:37:08 -070074 SkAutoCanvasRestore acr(canvas, true);
75 SkPaint paint;
76 paint.setShader(fSrcImageShader);
csmartdalton97f6cd52016-07-13 13:37:08 -070077 canvas->clipRect(kSrcImageClip);
78 canvas->drawPaint(paint);
79 }
80
81 {
Kevin Lubickf24283f2023-03-17 16:10:23 -040082 // skgpu::ganesh::SurfaceDrawContext::fillRectToRect.
csmartdalton97f6cd52016-07-13 13:37:08 -070083 SkAutoCanvasRestore acr(canvas, true);
csmartdalton97f6cd52016-07-13 13:37:08 -070084 SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
85 canvas->clipRect(drawRect);
86 canvas->drawImageRect(fSrcImage.get(),
87 kSrcImageClip.makeOutset(0.5f * kSrcImageClip.width(),
88 kSrcImageClip.height()),
89 drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
Mike Reed07c5f522021-01-23 12:23:23 -050090 SkSamplingOptions(), nullptr,
91 SkCanvas::kStrict_SrcRectConstraint);
csmartdalton97f6cd52016-07-13 13:37:08 -070092 }
93
94 {
Kevin Lubickf24283f2023-03-17 16:10:23 -040095 // skgpu::ganesh::SurfaceDrawContext::fillRectWithLocalMatrix.
csmartdalton97f6cd52016-07-13 13:37:08 -070096 SkAutoCanvasRestore acr(canvas, true);
Mike Reed92f6eb12020-08-25 11:48:41 -040097 SkPath path = SkPath::Line(
98 {kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
99 {kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()});
csmartdalton97f6cd52016-07-13 13:37:08 -0700100 SkPaint paint;
101 paint.setStyle(SkPaint::kStroke_Style);
102 paint.setStrokeWidth(2 * kSrcImageClip.height());
103 paint.setShader(fSrcImageShader);
Chris Daltonb246b942017-06-06 17:20:43 -0600104 canvas->translate(23, 301);
csmartdalton97f6cd52016-07-13 13:37:08 -0700105 canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
Chris Daltonb246b942017-06-06 17:20:43 -0600106 canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
csmartdalton97f6cd52016-07-13 13:37:08 -0700107 canvas->clipRect(kSrcImageClip);
108 canvas->drawPath(path, paint);
109 }
110
Brian Salomon09d994e2016-12-21 11:14:46 -0500111 // TODO: assert the draw target only has one op in the post-MDB world.
csmartdalton97f6cd52016-07-13 13:37:08 -0700112 }
113
114 sk_sp<SkImage> fSrcImage;
115 sk_sp<SkShader> fSrcImageShader;
116
John Stiles7571f9e2020-09-02 22:42:33 -0400117 using INHERITED = GM;
csmartdalton97f6cd52016-07-13 13:37:08 -0700118};
119
120DEF_GM( return new CroppedRectsGM(); )
121
John Stilesa6841be2020-08-06 14:11:56 -0400122} // namespace skiagm