aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/bigrrectaaeffect.cpp
blob: bc0b56a72c11c35128959282214a227404f83e44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#if SK_SUPPORT_GPU
#include "GrTest.h"
#include "effects/GrRRectEffect.h"
#include "SkDevice.h"
#include "SkRRect.h"

namespace skiagm {

///////////////////////////////////////////////////////////////////////////////

class BigRRectAAEffectGM : public GM {
public:
    BigRRectAAEffectGM() {
        this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
        this->setUpRRects();
    }

protected:
    SkString onShortName() override {
        return SkString("big_rrect_aa_effect");
    }

    SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }

    void onDraw(SkCanvas* canvas) override {
        GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
        GrContext* context = rt ? rt->getContext() : nullptr;
        if (!context) {
            skiagm::GM::DrawGpuOnlyMessage(canvas);
            return;
        }

        SkPaint paint;

#ifdef SK_DEBUG
        static const SkRect kMaxRRectBound = SkRect::MakeWH(SkIntToScalar(kMaxSize),
                                                            SkIntToScalar(kMaxSize));
        static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
                                                            SkIntToScalar(kImageHeight));
#endif

        int y = kPad;
        int x = kPad;
        static const GrPrimitiveEdgeType kEdgeTypes[] = {
            kFillAA_GrProcessorEdgeType,
            kInverseFillAA_GrProcessorEdgeType,
        };
        for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
            GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
            for (int curRRect = 0; curRRect < fRRects.count(); ++curRRect) {
#ifdef SK_DEBUG
                SkASSERT(kMaxRRectBound.contains(fRRects[curRRect].getBounds()));
                SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
                imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
                SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
#endif
                canvas->save();
                    canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
                    GrTestTarget tt;
                    context->getTestTarget(&tt);
                    if (nullptr == tt.target()) {
                        SkDEBUGFAIL("Couldn't get Gr test target.");
                        return;
                    }
                    GrPipelineBuilder pipelineBuilder;

                    SkRRect rrect = fRRects[curRRect];
                    rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
                    SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect));
                    SkASSERT(fp);
                    if (fp) {
                        pipelineBuilder.addCoverageFragmentProcessor(fp);
                        pipelineBuilder.setRenderTarget(rt);

                        SkRect bounds = SkRect::MakeWH(SkIntToScalar(kMaxSize),
                                                       SkIntToScalar(kMaxSize));
                        bounds.outset(2.f, 2.f);
                        bounds.offset(SkIntToScalar(x), SkIntToScalar(y));

                        tt.target()->drawNonAARect(pipelineBuilder,
                                                   0xff000000,
                                                   SkMatrix::I(),
                                                   bounds);
                    }
                canvas->restore();
                x = x + kDrawOffset;
                if (x + kMaxSize> kImageWidth) {
                    x = kPad;
                    y += kDrawOffset;
                }
            }
        }
    }

    void setUpRRects() {
        SkScalar maxSize = SkIntToScalar(kMaxSize);
        fRRects.push()->setRect(SkRect::MakeWH(maxSize, maxSize));
        fRRects.push()->setOval(SkRect::MakeWH(maxSize, maxSize));
        fRRects.push()->setOval(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f));
        fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f),
                                  maxSize/2.f - 10.f, maxSize/2.f - 10.f);
        fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10),
                                  maxSize/2.f - 10.f, maxSize/2.f - 20.f);
    }

private:
    static const int kPad = 5;
    static const int kMaxSize = 300;
    static const int kDrawOffset = kMaxSize + kPad;

    static const int kImageWidth = 4 * kDrawOffset + kPad;
    static const int kImageHeight = 3 * kDrawOffset + kPad;


    SkTDArray<SkRRect> fRRects;
    typedef GM INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

DEF_GM( return new BigRRectAAEffectGM (); )

}
#endif