blob: bc0b56a72c11c35128959282214a227404f83e44 [file] [log] [blame]
bsalomonc41f4d62015-08-03 14:23:03 -07001/*
2 * Copyright 2015 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.h"
9#if SK_SUPPORT_GPU
10#include "GrTest.h"
11#include "effects/GrRRectEffect.h"
12#include "SkDevice.h"
13#include "SkRRect.h"
14
15namespace skiagm {
16
17///////////////////////////////////////////////////////////////////////////////
18
19class BigRRectAAEffectGM : public GM {
20public:
21 BigRRectAAEffectGM() {
22 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
23 this->setUpRRects();
24 }
25
26protected:
27 SkString onShortName() override {
28 return SkString("big_rrect_aa_effect");
29 }
30
31 SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); }
32
33 void onDraw(SkCanvas* canvas) override {
34 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
halcanary96fcdcc2015-08-27 07:41:13 -070035 GrContext* context = rt ? rt->getContext() : nullptr;
bsalomonc41f4d62015-08-03 14:23:03 -070036 if (!context) {
halcanary2a243382015-09-09 08:16:41 -070037 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomonc41f4d62015-08-03 14:23:03 -070038 return;
39 }
40
41 SkPaint paint;
42
43#ifdef SK_DEBUG
44 static const SkRect kMaxRRectBound = SkRect::MakeWH(SkIntToScalar(kMaxSize),
45 SkIntToScalar(kMaxSize));
46 static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth),
47 SkIntToScalar(kImageHeight));
48#endif
49
50 int y = kPad;
51 int x = kPad;
52 static const GrPrimitiveEdgeType kEdgeTypes[] = {
53 kFillAA_GrProcessorEdgeType,
54 kInverseFillAA_GrProcessorEdgeType,
55 };
56 for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
57 GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
58 for (int curRRect = 0; curRRect < fRRects.count(); ++curRRect) {
59#ifdef SK_DEBUG
60 SkASSERT(kMaxRRectBound.contains(fRRects[curRRect].getBounds()));
61 SkRect imageSpaceBounds = fRRects[curRRect].getBounds();
62 imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y));
63 SkASSERT(kMaxImageBound.contains(imageSpaceBounds));
64#endif
65 canvas->save();
66 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
67 GrTestTarget tt;
68 context->getTestTarget(&tt);
halcanary96fcdcc2015-08-27 07:41:13 -070069 if (nullptr == tt.target()) {
bsalomonc41f4d62015-08-03 14:23:03 -070070 SkDEBUGFAIL("Couldn't get Gr test target.");
71 return;
72 }
73 GrPipelineBuilder pipelineBuilder;
74
75 SkRRect rrect = fRRects[curRRect];
76 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
77 SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect));
78 SkASSERT(fp);
79 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -070080 pipelineBuilder.addCoverageFragmentProcessor(fp);
bsalomonc41f4d62015-08-03 14:23:03 -070081 pipelineBuilder.setRenderTarget(rt);
82
83 SkRect bounds = SkRect::MakeWH(SkIntToScalar(kMaxSize),
84 SkIntToScalar(kMaxSize));
85 bounds.outset(2.f, 2.f);
86 bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
87
joshualittd2b23e02015-08-21 10:53:34 -070088 tt.target()->drawNonAARect(pipelineBuilder,
89 0xff000000,
90 SkMatrix::I(),
91 bounds);
bsalomonc41f4d62015-08-03 14:23:03 -070092 }
93 canvas->restore();
94 x = x + kDrawOffset;
95 if (x + kMaxSize> kImageWidth) {
96 x = kPad;
97 y += kDrawOffset;
98 }
99 }
100 }
101 }
102
103 void setUpRRects() {
104 SkScalar maxSize = SkIntToScalar(kMaxSize);
105 fRRects.push()->setRect(SkRect::MakeWH(maxSize, maxSize));
106 fRRects.push()->setOval(SkRect::MakeWH(maxSize, maxSize));
107 fRRects.push()->setOval(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f));
108 fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f),
109 maxSize/2.f - 10.f, maxSize/2.f - 10.f);
110 fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10),
111 maxSize/2.f - 10.f, maxSize/2.f - 20.f);
112 }
113
114private:
115 static const int kPad = 5;
116 static const int kMaxSize = 300;
117 static const int kDrawOffset = kMaxSize + kPad;
118
119 static const int kImageWidth = 4 * kDrawOffset + kPad;
120 static const int kImageHeight = 3 * kDrawOffset + kPad;
121
122
123 SkTDArray<SkRRect> fRRects;
124 typedef GM INHERITED;
125};
126
127///////////////////////////////////////////////////////////////////////////////
128
129DEF_GM( return new BigRRectAAEffectGM (); )
130
131}
132#endif