Flutter Engine
The Flutter Engine
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;
41 stroke.setStrokeWidth(kStrokeWidth);
42 stroke.setColor(0xff008800);
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);
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
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
@ 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)
Definition: SkCanvas.cpp:2333
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
Definition: SkPath.h:59
static SkPath Line(const SkPoint a, const SkPoint b)
Definition: SkPath.h:106
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
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)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static sk_sp< SkImage > create_image(SkCanvas *destCanvas)
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr float kStrokeWidth
constexpr SkRect kSrcImageClip
Definition: SkSize.h:16
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