blob: 2e8b06c2b2fdf3eedf99794b1bc596240149f46c [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"
csmartdalton97f6cd52016-07-13 13:37:08 -070021
22namespace skiagm {
23
24constexpr SkRect kSrcImageClip{75, 75, 275, 275};
25
26/*
Robert Phillips4dca8312021-07-28 15:13:20 -040027 * The purpose of this test is to exercise all three codepaths in skgpu::v1::SurfaceDrawContext
Brian Osman11052242016-10-27 14:47:55 -040028 * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
29 * clip.
csmartdalton97f6cd52016-07-13 13:37:08 -070030 *
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 Salomon09d994e2016-12-21 11:14:46 -050033 * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
csmartdalton97f6cd52016-07-13 13:37:08 -070034 */
35class CroppedRectsGM : public GM {
36private:
John Stiles1cf2c8d2020-08-13 22:58:04 -040037 SkString onShortName() final { return SkString("croppedrects"); }
csmartdalton97f6cd52016-07-13 13:37:08 -070038 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 Phillipsac6b1fa2017-03-20 08:38:50 -040057 fSrcImage = srcSurface->makeImageSnapshot();
Mike Reedb612b6c2020-12-08 21:58:35 -050058 fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
csmartdalton97f6cd52016-07-13 13:37:08 -070059 }
60
61 void onDraw(SkCanvas* canvas) override {
62 canvas->clear(SK_ColorWHITE);
63
64 {
Robert Phillips4dca8312021-07-28 15:13:20 -040065 // skgpu::v1::SurfaceDrawContext::drawFilledRect.
csmartdalton97f6cd52016-07-13 13:37:08 -070066 SkAutoCanvasRestore acr(canvas, true);
67 SkPaint paint;
68 paint.setShader(fSrcImageShader);
csmartdalton97f6cd52016-07-13 13:37:08 -070069 canvas->clipRect(kSrcImageClip);
70 canvas->drawPaint(paint);
71 }
72
73 {
Robert Phillips4dca8312021-07-28 15:13:20 -040074 // skgpu::v1::SurfaceDrawContext::fillRectToRect.
csmartdalton97f6cd52016-07-13 13:37:08 -070075 SkAutoCanvasRestore acr(canvas, true);
csmartdalton97f6cd52016-07-13 13:37:08 -070076 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 Reed07c5f522021-01-23 12:23:23 -050082 SkSamplingOptions(), nullptr,
83 SkCanvas::kStrict_SrcRectConstraint);
csmartdalton97f6cd52016-07-13 13:37:08 -070084 }
85
86 {
Robert Phillips4dca8312021-07-28 15:13:20 -040087 // skgpu::v1::SurfaceDrawContext::fillRectWithLocalMatrix.
csmartdalton97f6cd52016-07-13 13:37:08 -070088 SkAutoCanvasRestore acr(canvas, true);
Mike Reed92f6eb12020-08-25 11:48:41 -040089 SkPath path = SkPath::Line(
90 {kSrcImageClip.fLeft - kSrcImageClip.width(), kSrcImageClip.centerY()},
91 {kSrcImageClip.fRight + 3 * kSrcImageClip.width(), kSrcImageClip.centerY()});
csmartdalton97f6cd52016-07-13 13:37:08 -070092 SkPaint paint;
93 paint.setStyle(SkPaint::kStroke_Style);
94 paint.setStrokeWidth(2 * kSrcImageClip.height());
95 paint.setShader(fSrcImageShader);
Chris Daltonb246b942017-06-06 17:20:43 -060096 canvas->translate(23, 301);
csmartdalton97f6cd52016-07-13 13:37:08 -070097 canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
Chris Daltonb246b942017-06-06 17:20:43 -060098 canvas->translate(-kSrcImageClip.left(), -kSrcImageClip.top());
csmartdalton97f6cd52016-07-13 13:37:08 -070099 canvas->clipRect(kSrcImageClip);
100 canvas->drawPath(path, paint);
101 }
102
Brian Salomon09d994e2016-12-21 11:14:46 -0500103 // TODO: assert the draw target only has one op in the post-MDB world.
csmartdalton97f6cd52016-07-13 13:37:08 -0700104 }
105
106 sk_sp<SkImage> fSrcImage;
107 sk_sp<SkShader> fSrcImageShader;
108
John Stiles7571f9e2020-09-02 22:42:33 -0400109 using INHERITED = GM;
csmartdalton97f6cd52016-07-13 13:37:08 -0700110};
111
112DEF_GM( return new CroppedRectsGM(); )
113
John Stilesa6841be2020-08-06 14:11:56 -0400114} // namespace skiagm