Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
croppedrects.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkPath.h"
14#include "include/core/SkRect.h"
18#include "include/core/SkSize.h"
21#include "tools/GpuToolUtils.h"
22#include "tools/ToolUtils.h"
23
24namespace skiagm {
25
26constexpr SkRect kSrcImageClip{75, 75, 275, 275};
27
30 SkCanvas* srcCanvas = srcSurface->getCanvas();
31
32 srcCanvas->clear(SK_ColorRED);
33
35 paint.setColor(0xff00ff00);
36 srcCanvas->drawRect(kSrcImageClip, paint);
37
38 constexpr SkScalar kStrokeWidth = 10;
39 SkPaint stroke;
42 stroke.setColor(0xff008800);
43 srcCanvas->drawRect(kSrcImageClip.makeInset(kStrokeWidth / 2, kStrokeWidth / 2), stroke);
44
45 return ToolUtils::MakeTextureImage(destCanvas, srcSurface->makeImageSnapshot());
46}
47
48/*
49 * The purpose of this test is to exercise all three codepaths in skgpu::ganesh::SurfaceDrawContext
50 * (drawFilledRect, fillRectToRect, fillRectWithLocalMatrix) that pre-crop filled rects based on the
51 * clip.
52 *
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
55 * background, scissor is never used, and ideally, all the rectangles draw in one GrDrawOp.
56 */
57class CroppedRectsGM : public GM {
58private:
59 SkString getName() const override { return SkString("croppedrects"); }
60 SkISize getISize() override { return SkISize::Make(500, 500); }
61
62 void onDraw(SkCanvas* canvas) override {
63 if (!fSrcImage) {
64 fSrcImage = create_image(canvas);
65 if (fSrcImage) {
66 fSrcImageShader = fSrcImage->makeShader(SkSamplingOptions());
67 }
68 }
69
70 canvas->clear(SK_ColorWHITE);
71
72 {
73 // skgpu::ganesh::SurfaceDrawContext::drawFilledRect.
74 SkAutoCanvasRestore acr(canvas, true);
76 paint.setShader(fSrcImageShader);
77 canvas->clipRect(kSrcImageClip);
78 canvas->drawPaint(paint);
79 }
80
81 {
82 // skgpu::ganesh::SurfaceDrawContext::fillRectToRect.
83 SkAutoCanvasRestore acr(canvas, true);
84 SkRect drawRect = SkRect::MakeXYWH(350, 100, 100, 300);
85 canvas->clipRect(drawRect);
86 canvas->drawImageRect(fSrcImage.get(),
89 drawRect.makeOutset(0.5f * drawRect.width(), drawRect.height()),
90 SkSamplingOptions(), nullptr,
92 }
93
94 {
95 // skgpu::ganesh::SurfaceDrawContext::fillRectWithLocalMatrix.
96 SkAutoCanvasRestore acr(canvas, true);
97 SkPath path = SkPath::Line(
102 paint.setStrokeWidth(2 * kSrcImageClip.height());
103 paint.setShader(fSrcImageShader);
104 canvas->translate(23, 301);
105 canvas->scale(300 / kSrcImageClip.width(), 100 / kSrcImageClip.height());
107 canvas->clipRect(kSrcImageClip);
108 canvas->drawPath(path, paint);
109 }
110
111 // TODO: assert the draw target only has one op in the post-MDB world.
112 }
113
114 sk_sp<SkImage> fSrcImage;
115 sk_sp<SkShader> fSrcImageShader;
116
117 using INHERITED = GM;
118};
119
120DEF_GM( return new CroppedRectsGM(); )
121
122} // namespace skiagm
static sk_sp< SkImage > create_image()
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void translate(SkScalar dx, SkScalar dy)
void drawPaint(const SkPaint &paint)
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
void drawPath(const SkPath &path, const SkPaint &paint)
void scale(SkScalar sx, SkScalar sy)
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkImage.cpp:179
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
static SkPath Line(const SkPoint a, const SkPoint b)
Definition SkPath.h:106
T * get() const
Definition SkRefCnt.h:303
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
sk_sp< SkImage > MakeTextureImage(SkCanvas *canvas, sk_sp< SkImage > orig)
static constexpr float kStrokeWidth
constexpr SkRect kSrcImageClip
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
constexpr float left() const
Definition SkRect.h:734
constexpr float top() const
Definition SkRect.h:741
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkRect makeOutset(float dx, float dy) const
Definition SkRect.h:1002
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
SkRect makeInset(float dx, float dy) const
Definition SkRect.h:987
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
constexpr float height() const
Definition SkRect.h:769
constexpr float centerY() const
Definition SkRect.h:785
constexpr float width() const
Definition SkRect.h:762