blob: 613cf2d984efc2ae5f6e25905a7eb28f17abe7d0 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Ethan Nicholaseace9352018-10-15 20:09:54 +000010#include "GrCaps.h"
joshualitt04194f32016-01-13 10:08:27 -080011#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContextPriv.h"
bsalomonc41f4d62015-08-03 14:23:03 -070013#include "SkRRect.h"
joshualitt04194f32016-01-13 10:08:27 -080014#include "effects/GrRRectEffect.h"
Brian Salomon89527432016-12-16 09:52:16 -050015#include "ops/GrDrawOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050016#include "ops/GrFillRectOp.h"
bsalomonc41f4d62015-08-03 14:23:03 -070017
18namespace skiagm {
19
20///////////////////////////////////////////////////////////////////////////////
21
Chris Dalton3a778372019-02-07 15:23:36 -070022class BigRRectAAEffectGM : public GpuGM {
bsalomonc41f4d62015-08-03 14:23:03 -070023public:
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
Chris Dalton3a778372019-02-07 15:23:36 -070051 void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
52 SkCanvas* canvas) override {
bsalomonc41f4d62015-08-03 14:23:03 -070053 SkPaint paint;
54
bsalomonc41f4d62015-08-03 14:23:03 -070055 int y = kPad;
56 int x = kPad;
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050057 constexpr GrClipEdgeType kEdgeTypes[] = {
Ethan Nicholas1706f842017-11-10 11:58:19 -050058 GrClipEdgeType::kFillAA,
59 GrClipEdgeType::kInverseFillAA,
bsalomonc41f4d62015-08-03 14:23:03 -070060 };
bsalomon4a4f14b2015-12-09 10:17:35 -080061 SkRect testBounds = SkRect::MakeIWH(fTestWidth, fTestHeight);
bsalomonc41f4d62015-08-03 14:23:03 -070062 for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) {
Ethan Nicholas0f3c7322017-11-09 14:51:17 -050063 GrClipEdgeType edgeType = kEdgeTypes[et];
bsalomon4a4f14b2015-12-09 10:17:35 -080064 canvas->save();
halcanary9d524f22016-03-29 09:03:52 -070065 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
bsalomonc41f4d62015-08-03 14:23:03 -070066
bsalomon4a4f14b2015-12-09 10:17:35 -080067 // Draw a background for the test case
68 SkPaint paint;
69 paint.setColor(SK_ColorWHITE);
70 canvas->drawRect(testBounds, paint);
bsalomonc41f4d62015-08-03 14:23:03 -070071
bsalomon4a4f14b2015-12-09 10:17:35 -080072 SkRRect rrect = fRRect;
73 rrect.offset(SkIntToScalar(x + kGap), SkIntToScalar(y + kGap));
Ethan Nicholaseace9352018-10-15 20:09:54 +000074 const auto& caps = *renderTargetContext->caps()->shaderCaps();
75 auto fp = GrRRectEffect::Make(edgeType, rrect, caps);
bsalomon4a4f14b2015-12-09 10:17:35 -080076 SkASSERT(fp);
77 if (fp) {
Brian Salomon82f44312017-01-11 13:42:54 -050078 GrPaint grPaint;
Brian Osmancb3d0872018-10-16 15:19:28 -040079 grPaint.setColor4f({ 0, 0, 0, 1.f });
Brian Salomon82f44312017-01-11 13:42:54 -050080 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
robertphillips28a838e2016-06-23 14:07:00 -070081 grPaint.addCoverageFragmentProcessor(std::move(fp));
bsalomon4a4f14b2015-12-09 10:17:35 -080082
83 SkRect bounds = testBounds;
84 bounds.offset(SkIntToScalar(x), SkIntToScalar(y));
85
Brian Salomonac70f842017-05-08 10:43:33 -040086 renderTargetContext->priv().testingOnly_addDrawOp(
Michael Ludwig72ab3462018-12-10 12:43:36 -050087 GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone,
88 SkMatrix::I(), bounds));
bsalomon4a4f14b2015-12-09 10:17:35 -080089 }
90 canvas->restore();
91 x = x + fTestOffsetX;
bsalomonc41f4d62015-08-03 14:23:03 -070092 }
93 }
94
bsalomonc41f4d62015-08-03 14:23:03 -070095private:
bsalomon4a4f14b2015-12-09 10:17:35 -080096 // pad between test cases
mtkleindbfd7ab2016-09-01 11:24:54 -070097 static constexpr int kPad = 7;
bsalomon4a4f14b2015-12-09 10:17:35 -080098 // gap between rect for each case that is rendered and exterior of rrect
mtkleindbfd7ab2016-09-01 11:24:54 -070099 static constexpr int kGap = 3;
bsalomonc41f4d62015-08-03 14:23:03 -0700100
bsalomon4a4f14b2015-12-09 10:17:35 -0800101 SkRRect fRRect;
102 int fWidth;
103 int fHeight;
104 int fTestWidth;
105 int fTestHeight;
106 int fTestOffsetX;
107 int fTestOffsetY;
108 const char* fName;
bsalomonc41f4d62015-08-03 14:23:03 -0700109 typedef GM INHERITED;
110};
111
112///////////////////////////////////////////////////////////////////////////////
bsalomon4a4f14b2015-12-09 10:17:35 -0800113// This value is motivated by bug chromium:477684. It has to be large to cause overflow in
114// the shader
mtkleindbfd7ab2016-09-01 11:24:54 -0700115constexpr int kSize = 700;
bsalomonc41f4d62015-08-03 14:23:03 -0700116
bsalomon4a4f14b2015-12-09 10:17:35 -0800117DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRect(SkRect::MakeIWH(kSize, kSize)), "rect"); )
118DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize, kSize)), "circle"); )
119DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeOval(SkRect::MakeIWH(kSize - 1, kSize - 10)), "ellipse"); )
120// The next two have small linear segments between the corners
121DEF_GM( return new BigRRectAAEffectGM (SkRRect::MakeRectXY(SkRect::MakeIWH(kSize - 1, kSize - 10), kSize/2.f - 10.f, kSize/2.f - 10.f), "circular_corner"); )
122DEF_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 -0700123
124}