Flutter Engine
The Flutter Engine
aarecteffect.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 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// This test only works with the GPU backend.
9
10#include "gm/gm.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
26#include "tools/gpu/TestOps.h"
27
28#include <memory>
29#include <utility>
30
31class GrAppliedClip;
32
33namespace skiagm {
34
35/**
36 * This GM directly exercises a GrProcessor that clips against rects.
37 */
38class AARectEffect : public GpuGM {
39public:
40 AARectEffect() { this->setBGColor(0xFFFFFFFF); }
41
42protected:
43 SkString getName() const override { return SkString("aa_rect_effect"); }
44
45 SkISize getISize() override { return SkISize::Make(210, 250); }
46
47 void onOnceBeforeDraw() override {}
48
49 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
51 if (!sdc) {
52 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
53 return DrawResult::kSkip;
54 }
55
56 SkScalar y = 12.f;
57 static constexpr SkScalar kDX = 12.f;
58 static constexpr SkScalar kOutset = 5.f;
59
60 static constexpr SkRect kRects[] = {
61 // integer edges
62 SkRect::MakeLTRB(5.f, 1.f, 30.f, 25.f),
63 // half-integer edges
64 SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 24.5f),
65 // vertically/horizontally thin rects that cover pixel centers
66 SkRect::MakeLTRB(5.25f, 0.5f, 5.75f, 24.5f),
67 SkRect::MakeLTRB(5.5f, 0.5f, 29.5f, 0.75f),
68 // vertically/horizontally thin rects that don't cover pixel centers
69 SkRect::MakeLTRB(5.55f, 0.5f, 5.75f, 24.5f),
70 SkRect::MakeLTRB(5.5f, .05f, 29.5f, .25f),
71 // small in x and y
72 SkRect::MakeLTRB(5.05f, .55f, 5.45f, .85f),
73 };
74
75 for (auto r : kRects) {
76 SkScalar x = kDX;
77
78 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
79 SkRect rect = r.makeOffset(x, y);
80 GrClipEdgeType edgeType = static_cast<GrClipEdgeType>(et);
81 auto fp = GrFragmentProcessor::Rect(/*inputFP=*/nullptr, edgeType, rect);
82 SkASSERT(fp);
83
84 GrPaint grPaint;
85 grPaint.setColor4f({ 0, 0, 0, 1.f });
87 grPaint.setCoverageFragmentProcessor(std::move(fp));
88 auto drawRect = rect.makeOutset(kOutset, kOutset);
89 auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(grPaint), drawRect);
90 sdc->addDrawOp(std::move(op));
91
92 x += SkScalarCeilToScalar(rect.width() + kDX);
93 }
94
95 // Draw rect without and with AA using normal API for reference
96 canvas->save();
97 canvas->translate(x, y);
99 canvas->drawRect(r, paint);
100 x += SkScalarCeilToScalar(r.width() + kDX);
101 paint.setAntiAlias(true);
102 canvas->drawRect(r, paint);
103 canvas->restore();
104
105 y += SkScalarCeilToScalar(r.height() + 20.f);
106 }
107
108 return DrawResult::kOk;
109 }
110
111private:
112 using INHERITED = GM;
113};
114
115DEF_GM(return new AARectEffect;)
116} // namespace skiagm
static const int kGrClipEdgeTypeCnt
Definition: GrTypesPriv.h:369
GrClipEdgeType
Definition: GrTypesPriv.h:361
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SkScalarCeilToScalar(x)
Definition: SkScalar.h:31
static std::unique_ptr< GrFragmentProcessor > Rect(std::unique_ptr< GrFragmentProcessor >, GrClipEdgeType, SkRect)
void setXPFactory(const GrXPFactory *xpFactory)
Definition: GrPaint.h:53
void setColor4f(const SkPMColor4f &color)
Definition: GrPaint.h:50
void setCoverageFragmentProcessor(std::unique_ptr< GrFragmentProcessor > fp)
Definition: GrPaint.h:75
static const GrXPFactory * Get(SkBlendMode blendMode)
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
SkString getName() const override
SkISize getISize() override
DrawResult onDraw(GrRecordingContext *rContext, SkCanvas *canvas, SkString *errorMsg) override
void onOnceBeforeDraw() override
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
const uint32_t fp
GrOp::Owner MakeRect(GrRecordingContext *context, GrPaint &&paint, const SkRect &drawRect, const SkRect &localRect, const SkMatrix &localM)
Definition: TestOps.cpp:227
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition: GrCanvas.cpp:20
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646