blob: 18a0c6ef7d32180f7db1df40684742e956b9a622 [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;
robertphillips504ce5d2015-11-16 11:02:05 -080068 context->getTestTarget(&tt, rt);
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;
egdanielc4b72722015-11-23 13:20:41 -080074 pipelineBuilder.setXPFactory(
75 GrPorterDuffXPFactory::Create(SkXfermode::kSrc_Mode))->unref();
bsalomonc41f4d62015-08-03 14:23:03 -070076
77 SkRRect rrect = fRRects[curRRect];
78 rrect.offset(SkIntToScalar(x), SkIntToScalar(y));
79 SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect));
80 SkASSERT(fp);
81 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -070082 pipelineBuilder.addCoverageFragmentProcessor(fp);
bsalomonc41f4d62015-08-03 14:23:03 -070083 pipelineBuilder.setRenderTarget(rt);
84
85 SkRect bounds = SkRect::MakeWH(SkIntToScalar(kMaxSize),
86 SkIntToScalar(kMaxSize));
87 bounds.outset(2.f, 2.f);
88 bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
89
joshualittd2b23e02015-08-21 10:53:34 -070090 tt.target()->drawNonAARect(pipelineBuilder,
91 0xff000000,
92 SkMatrix::I(),
93 bounds);
bsalomonc41f4d62015-08-03 14:23:03 -070094 }
95 canvas->restore();
96 x = x + kDrawOffset;
97 if (x + kMaxSize> kImageWidth) {
98 x = kPad;
99 y += kDrawOffset;
100 }
101 }
102 }
103 }
104
105 void setUpRRects() {
106 SkScalar maxSize = SkIntToScalar(kMaxSize);
107 fRRects.push()->setRect(SkRect::MakeWH(maxSize, maxSize));
108 fRRects.push()->setOval(SkRect::MakeWH(maxSize, maxSize));
109 fRRects.push()->setOval(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f));
110 fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f),
111 maxSize/2.f - 10.f, maxSize/2.f - 10.f);
112 fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10),
113 maxSize/2.f - 10.f, maxSize/2.f - 20.f);
114 }
115
116private:
117 static const int kPad = 5;
118 static const int kMaxSize = 300;
119 static const int kDrawOffset = kMaxSize + kPad;
120
121 static const int kImageWidth = 4 * kDrawOffset + kPad;
122 static const int kImageHeight = 3 * kDrawOffset + kPad;
123
124
125 SkTDArray<SkRRect> fRRects;
126 typedef GM INHERITED;
127};
128
129///////////////////////////////////////////////////////////////////////////////
130
131DEF_GM( return new BigRRectAAEffectGM (); )
132
133}
134#endif