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