blob: 924704a5acec1dbfbf9bb78dae9d77e79b0faced [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
joshualitt04194f32016-01-13 10:08:27 -080010#include "GrContext.h"
robertphillips391395d2016-03-02 09:26:36 -080011#include "GrDrawContextPriv.h"
joshualitt04194f32016-01-13 10:08:27 -080012#include "GrPipelineBuilder.h"
bsalomonc41f4d62015-08-03 14:23:03 -070013#include "SkRRect.h"
joshualitt04194f32016-01-13 10:08:27 -080014#include "batches/GrDrawBatch.h"
15#include "batches/GrRectBatchFactory.h"
16#include "effects/GrRRectEffect.h"
bsalomonc41f4d62015-08-03 14:23:03 -070017
18namespace skiagm {
19
20///////////////////////////////////////////////////////////////////////////////
21
22class BigRRectAAEffectGM : public GM {
23public:
bsalomon4a4f14b2015-12-09 10:17:35 -080024 BigRRectAAEffectGM(const SkRRect& rrect, const char* name)
25 : fRRect(rrect)
26 , fName(name) {
27 this->setBGColor(sk_tool_utils::color_to_565(SK_ColorBLUE));
28 // Each test case draws the rrect with gaps around it.
29 fTestWidth = SkScalarCeilToInt(rrect.width()) + 2 * kGap;
30 fTestHeight = SkScalarCeilToInt(rrect.height()) + 2 * kGap;
31
32 // Add a pad between test cases.
33 fTestOffsetX = fTestWidth + kPad;
34 fTestOffsetY = fTestHeight + kPad;
35
36 // We draw two tests in x (fill and inv-fill) and pad around
37 // all four sides of the image.
38 fWidth = 2 * fTestOffsetX + kPad;
39 fHeight = fTestOffsetY + kPad;
bsalomonc41f4d62015-08-03 14:23:03 -070040 }
41
42protected:
43 SkString onShortName() override {
bsalomon4a4f14b2015-12-09 10:17:35 -080044 SkString name;
45 name.printf("big_rrect_%s_aa_effect", fName);
46 return name;
bsalomonc41f4d62015-08-03 14:23:03 -070047 }
48
bsalomon4a4f14b2015-12-09 10:17:35 -080049 SkISize onISize() override { return SkISize::Make(fWidth, fHeight); }
bsalomonc41f4d62015-08-03 14:23:03 -070050
51 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -070052 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
joshualitt04194f32016-01-13 10:08:27 -080053 if (!drawContext) {
robertphillips175dd9b2016-04-28 14:32:04 -070054 skiagm::GM::DrawGpuOnlyMessage(canvas);
joshualitt04194f32016-01-13 10:08:27 -080055 return;
56 }
57
bsalomonc41f4d62015-08-03 14:23:03 -070058 SkPaint paint;
59
bsalomonc41f4d62015-08-03 14:23:03 -070060 int y = kPad;
61 int x = kPad;
62 static const GrPrimitiveEdgeType kEdgeTypes[] = {
63 kFillAA_GrProcessorEdgeType,
64 kInverseFillAA_GrProcessorEdgeType,
65 };
bsalomon4a4f14b2015-12-09 10:17:35 -080066 SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
bsalomonc41f4d62015-08-03 14:23:03 -070067 for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
68 GrPrimitiveEdgeType edgeType = kEdgeTypes[et];
bsalomon4a4f14b2015-12-09 10:17:35 -080069 canvas->save();
halcanary9d524f22016-03-29 09:03:52 -070070 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
bsalomonc41f4d62015-08-03 14:23:03 -070071
bsalomon4a4f14b2015-12-09 10:17:35 -080072 // Draw a background for the test case
73 SkPaint paint;
74 paint.setColor(SK_ColorWHITE);
75 canvas->drawRect(testBounds, paint);
bsalomonc41f4d62015-08-03 14:23:03 -070076
bsalomon4a4f14b2015-12-09 10:17:35 -080077 GrPipelineBuilder pipelineBuilder;
bungeman06ca8ec2016-06-09 08:01:03 -070078 pipelineBuilder.setXPFactory(GrPorterDuffXPFactory::Make(SkXfermode::kSrc_Mode));
bsalomon4a4f14b2015-12-09 10:17:35 -080079
80 SkRRect rrect = fRRect;
81 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
bungeman06ca8ec2016-06-09 08:01:03 -070082 sk_sp<GrFragmentProcessor> fp(GrRRectEffect::Make(edgeType, rrect));
bsalomon4a4f14b2015-12-09 10:17:35 -080083 SkASSERT(fp);
84 if (fp) {
bungeman06ca8ec2016-06-09 08:01:03 -070085 pipelineBuilder.addCoverageFragmentProcessor(std::move(fp));
bsalomon4a4f14b2015-12-09 10:17:35 -080086
87 SkRect bounds = testBounds;
88 bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
89
joshualitt04194f32016-01-13 10:08:27 -080090 SkAutoTUnref<GrDrawBatch> batch(
91 GrRectBatchFactory::CreateNonAAFill(0xff000000, SkMatrix::I(), bounds,
92 nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -080093 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
bsalomon4a4f14b2015-12-09 10:17:35 -080094 }
95 canvas->restore();
96 x = x + fTestOffsetX;
bsalomonc41f4d62015-08-03 14:23:03 -070097 }
98 }
99
bsalomonc41f4d62015-08-03 14:23:03 -0700100private:
bsalomon4a4f14b2015-12-09 10:17:35 -0800101 // pad between test cases
102 static const int kPad = 7;
103 // gap between rect for each case that is rendered and exterior of rrect
104 static const int kGap = 3;
bsalomonc41f4d62015-08-03 14:23:03 -0700105
bsalomon4a4f14b2015-12-09 10:17:35 -0800106 SkRRect fRRect;
107 int fWidth;
108 int fHeight;
109 int fTestWidth;
110 int fTestHeight;
111 int fTestOffsetX;
112 int fTestOffsetY;
113 const char* fName;
bsalomonc41f4d62015-08-03 14:23:03 -0700114 typedef GM INHERITED;
115};
116
117///////////////////////////////////////////////////////////////////////////////
bsalomon4a4f14b2015-12-09 10:17:35 -0800118// This value is motivated by bug chromium:477684. It has to be large to cause overflow in
119// the shader
120static const int kSize = 700;
bsalomonc41f4d62015-08-03 14:23:03 -0700121
bsalomon4a4f14b2015-12-09 10:17:35 -0800122DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
123DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
124DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
125// The next two have small linear segments between the corners
126DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 10.f), "circular_corner"); )
127DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 15.f), "elliptical_corner"); )
bsalomonc41f4d62015-08-03 14:23:03 -0700128
129}
130#endif