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