robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 9 | #include "GrCaps.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 10 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 11 | #include "GrRenderTargetContextPriv.h" |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 12 | #include "effects/GrRRectEffect.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 13 | #include "ops/GrDrawOp.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 14 | #include "ops/GrRectOpFactory.h" |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 15 | #include "SkRRect.h" |
| 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | /////////////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | class RRectGM : public GM { |
| 22 | public: |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 23 | enum Type { |
| 24 | kBW_Draw_Type, |
| 25 | kAA_Draw_Type, |
| 26 | kBW_Clip_Type, |
| 27 | kAA_Clip_Type, |
| 28 | kEffect_Type, |
| 29 | }; |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 30 | RRectGM(Type type) : fType(type) { } |
| 31 | |
| 32 | protected: |
| 33 | |
| 34 | void onOnceBeforeDraw() override { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 35 | this->setBGColor(0xFFDDDDDD); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 36 | this->setUpRRects(); |
| 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | SkString onShortName() override { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 40 | SkString name("rrect"); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 41 | switch (fType) { |
| 42 | case kBW_Draw_Type: |
| 43 | name.append("_draw_bw"); |
| 44 | break; |
| 45 | case kAA_Draw_Type: |
| 46 | name.append("_draw_aa"); |
| 47 | break; |
| 48 | case kBW_Clip_Type: |
| 49 | name.append("_clip_bw"); |
| 50 | break; |
| 51 | case kAA_Clip_Type: |
| 52 | name.append("_clip_aa"); |
| 53 | break; |
| 54 | case kEffect_Type: |
| 55 | name.append("_effect"); |
| 56 | break; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 57 | } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 58 | return name; |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 64 | GrRenderTargetContext* renderTargetContext = |
| 65 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 66 | if (kEffect_Type == fType && !renderTargetContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 67 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 70 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 71 | GrContext* context = canvas->getGrContext(); |
| 72 | if (kEffect_Type == fType && !context) { |
| 73 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 74 | return; |
| 75 | } |
| 76 | |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 77 | SkPaint paint; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 78 | if (kAA_Draw_Type == fType) { |
| 79 | paint.setAntiAlias(true); |
| 80 | } |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 81 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 82 | const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX), |
| 83 | SkIntToScalar(kTileY)); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 84 | #ifdef SK_DEBUG |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 85 | const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth), |
| 86 | SkIntToScalar(kImageHeight)); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 87 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 88 | |
Ethan Nicholas | 1706f84 | 2017-11-10 11:58:19 -0500 | [diff] [blame] | 89 | int lastEdgeType = (kEffect_Type == fType) ? (int) GrClipEdgeType::kLast: 0; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 90 | |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 91 | int y = 1; |
bsalomon@google.com | b0ba39d | 2014-03-10 19:51:46 +0000 | [diff] [blame] | 92 | for (int et = 0; et <= lastEdgeType; ++et) { |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 93 | int x = 1; |
| 94 | for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) { |
| 95 | bool drew = true; |
| 96 | #ifdef SK_DEBUG |
| 97 | SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds())); |
| 98 | SkRect imageSpaceBounds = fRRects[curRRect].getBounds(); |
| 99 | imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 100 | SkASSERT(kMaxImageBound.contains(imageSpaceBounds)); |
| 101 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 102 | canvas->save(); |
| 103 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 104 | if (kEffect_Type == fType) { |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 105 | SkRRect rrect = fRRects[curRRect]; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 106 | rrect.offset(SkIntToScalar(x), SkIntToScalar(y)); |
Ethan Nicholas | 0f3c732 | 2017-11-09 14:51:17 -0500 | [diff] [blame] | 107 | GrClipEdgeType edgeType = (GrClipEdgeType) et; |
Brian Salomon | 1447177 | 2017-12-05 10:35:15 -0500 | [diff] [blame] | 108 | const auto& caps = *renderTargetContext->caps()->shaderCaps(); |
| 109 | auto fp = GrRRectEffect::Make(edgeType, rrect, caps); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 110 | if (fp) { |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 111 | GrPaint grPaint; |
| 112 | grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc)); |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 113 | grPaint.addCoverageFragmentProcessor(std::move(fp)); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 114 | grPaint.setColor4f(GrColor4f(0, 0, 0, 1.f)); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 115 | |
| 116 | SkRect bounds = rrect.getBounds(); |
| 117 | bounds.outset(2.f, 2.f); |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 118 | |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 119 | renderTargetContext->priv().testingOnly_addDrawOp( |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 120 | GrRectOpFactory::MakeNonAAFill(context, std::move(grPaint), |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 121 | SkMatrix::I(), bounds, |
| 122 | GrAAType::kNone)); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 123 | } else { |
| 124 | drew = false; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 125 | } |
| 126 | } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) { |
| 127 | bool aaClip = (kAA_Clip_Type == fType); |
reed | 6699838 | 2016-09-21 11:15:07 -0700 | [diff] [blame] | 128 | canvas->clipRRect(fRRects[curRRect], aaClip); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 129 | canvas->drawRect(kMaxTileBound, paint); |
| 130 | } else { |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 131 | canvas->drawRRect(fRRects[curRRect], paint); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 132 | } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 133 | canvas->restore(); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 134 | if (drew) { |
| 135 | x = x + kTileX; |
| 136 | if (x > kImageWidth) { |
| 137 | x = 1; |
| 138 | y += kTileY; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | if (x != 1) { |
| 143 | y += kTileY; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void setUpRRects() { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 149 | // each RRect must fit in a 0x0 -> (kTileX-2)x(kTileY-2) block. These will be tiled across |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 150 | // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA. |
| 151 | |
| 152 | // simple cases |
| 153 | fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2)); |
| 154 | fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2)); |
| 155 | fRRects[2].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 10); |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 156 | fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5); |
| 157 | // small circular corners are an interesting test case for gpu clipping |
| 158 | fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1); |
| 159 | fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f); |
| 160 | fRRects[6].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.2f, 0.2f); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 161 | |
| 162 | // The first complex case needs special handling since it is a square |
| 163 | fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]); |
mike@reedtribe.org | f6100c8 | 2012-12-24 13:56:17 +0000 | [diff] [blame] | 164 | for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 165 | fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private: |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 170 | Type fType; |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 171 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 172 | static constexpr int kImageWidth = 640; |
| 173 | static constexpr int kImageHeight = 480; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 174 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 175 | static constexpr int kTileX = 80; |
| 176 | static constexpr int kTileY = 40; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 177 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 178 | static constexpr int kNumSimpleCases = 7; |
| 179 | static constexpr int kNumComplexCases = 35; |
robertphillips@google.com | 5683d42 | 2012-12-17 21:58:02 +0000 | [diff] [blame] | 180 | static const SkVector gRadii[kNumComplexCases][4]; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 181 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 182 | static constexpr int kNumRRects = kNumSimpleCases + kNumComplexCases; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 183 | SkRRect fRRects[kNumRRects]; |
| 184 | |
| 185 | typedef GM INHERITED; |
| 186 | }; |
| 187 | |
| 188 | // Radii for the various test cases. Order is UL, UR, LR, LL |
| 189 | const SkVector RRectGM::gRadii[kNumComplexCases][4] = { |
| 190 | // a circle |
| 191 | { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } }, |
| 192 | |
| 193 | // odd ball cases |
| 194 | { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } }, |
| 195 | { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } }, |
| 196 | { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } }, |
| 197 | |
| 198 | // UL |
| 199 | { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 200 | { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 201 | { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 202 | |
| 203 | // UR |
| 204 | { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } }, |
| 205 | { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } }, |
| 206 | { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } }, |
| 207 | |
| 208 | // LR |
| 209 | { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } }, |
| 210 | { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } }, |
| 211 | { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } }, |
| 212 | |
| 213 | // LL |
| 214 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } }, |
| 215 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } }, |
| 216 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } }, |
| 217 | |
| 218 | // over-sized radii |
| 219 | { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } }, |
| 220 | { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } }, |
| 221 | { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } }, |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 222 | |
| 223 | // circular corner tabs |
| 224 | { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } }, |
| 225 | { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } }, |
| 226 | { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } }, |
| 227 | { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } }, |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 228 | |
commit-bot@chromium.org | 04eff72 | 2014-03-24 14:53:09 +0000 | [diff] [blame] | 229 | // small radius circular corner tabs |
| 230 | { { 0, 0 }, { 0.2f, 0.2f }, { 0.2f, 0.2f }, { 0, 0 } }, |
| 231 | { { 0.3f, 0.3f }, { 0.3f, .3f }, { 0, 0 }, { 0, 0 } }, |
| 232 | |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 233 | // single circular corner cases |
| 234 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 15 } }, |
| 235 | { { 0, 0 }, { 0, 0 }, { 15, 15 }, { 0, 0 } }, |
| 236 | { { 0, 0 }, { 15, 15 }, { 0, 0 }, { 0, 0 } }, |
| 237 | { { 15, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 238 | |
| 239 | // nine patch elliptical |
| 240 | { { 5, 7 }, { 8, 7 }, { 8, 12 }, { 5, 12 } }, |
| 241 | { { 0, 7 }, { 8, 7 }, { 8, 12 }, { 0, 12 } }, |
commit-bot@chromium.org | 04eff72 | 2014-03-24 14:53:09 +0000 | [diff] [blame] | 242 | |
| 243 | // nine patch elliptical, small radii |
| 244 | { { 0.4f, 7 }, { 8, 7 }, { 8, 12 }, { 0.4f, 12 } }, |
| 245 | { { 0.4f, 0.4f }, { 8, 0.4f }, { 8, 12 }, { 0.4f, 12 } }, |
| 246 | { { 20, 0.4f }, { 18, 0.4f }, { 18, 0.4f }, { 20, 0.4f } }, |
| 247 | { { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f }, { 0.3f, 0.4f } }, |
| 248 | |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | /////////////////////////////////////////////////////////////////////////////// |
| 252 | |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 253 | DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); ) |
| 254 | DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); ) |
| 255 | DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); ) |
| 256 | DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); ) |
| 257 | DEF_GM( return new RRectGM(RRectGM::kEffect_Type); ) |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 258 | |
| 259 | } |