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