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) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 30 | this->setBGColor(0xFFDDDDDD); |
| 31 | this->setUpRRects(); |
| 32 | } |
| 33 | |
| 34 | protected: |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 35 | SkString onShortName() SK_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 | |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 57 | virtual SkISize onISize() SK_OVERRIDE { return make_isize(kImageWidth, kImageHeight); } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 58 | |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 59 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 60 | if (kEffect_Type == fType) { |
| 61 | return kGPUOnly_Flag; |
| 62 | } else { |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 68 | #if SK_SUPPORT_GPU |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 69 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
| 70 | GrContext* context = rt ? rt->getContext() : NULL; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 71 | if (kEffect_Type == fType && NULL == context) { |
| 72 | return; |
| 73 | } |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 74 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 75 | |
| 76 | SkPaint paint; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 77 | if (kAA_Draw_Type == fType) { |
| 78 | paint.setAntiAlias(true); |
| 79 | } |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 81 | static const SkRect kMaxTileBound = SkRect::MakeWH(SkIntToScalar(kTileX), |
| 82 | SkIntToScalar(kTileY)); |
| 83 | #ifdef SK_DEBUG |
| 84 | static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth), |
| 85 | SkIntToScalar(kImageHeight)); |
| 86 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 87 | |
bsalomon@google.com | b0ba39d | 2014-03-10 19:51:46 +0000 | [diff] [blame] | 88 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 89 | int lastEdgeType = (kEffect_Type == fType) ? kLast_GrEffectEdgeType: 0; |
bsalomon@google.com | b0ba39d | 2014-03-10 19:51:46 +0000 | [diff] [blame] | 90 | #else |
| 91 | int lastEdgeType = 0; |
| 92 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 93 | |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 94 | int y = 1; |
bsalomon@google.com | b0ba39d | 2014-03-10 19:51:46 +0000 | [diff] [blame] | 95 | for (int et = 0; et <= lastEdgeType; ++et) { |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 96 | int x = 1; |
| 97 | for (int curRRect = 0; curRRect < kNumRRects; ++curRRect) { |
| 98 | bool drew = true; |
| 99 | #ifdef SK_DEBUG |
| 100 | SkASSERT(kMaxTileBound.contains(fRRects[curRRect].getBounds())); |
| 101 | SkRect imageSpaceBounds = fRRects[curRRect].getBounds(); |
| 102 | imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 103 | SkASSERT(kMaxImageBound.contains(imageSpaceBounds)); |
| 104 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 105 | canvas->save(); |
| 106 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 107 | if (kEffect_Type == fType) { |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 108 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 109 | GrTestTarget tt; |
| 110 | context->getTestTarget(&tt); |
| 111 | if (NULL == tt.target()) { |
| 112 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 113 | return; |
| 114 | } |
| 115 | GrDrawState* drawState = tt.target()->drawState(); |
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)); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 119 | GrEffectEdgeType edgeType = (GrEffectEdgeType) et; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 120 | SkAutoTUnref<GrEffectRef> effect(GrRRectEffect::Create(edgeType, rrect)); |
| 121 | if (effect) { |
| 122 | drawState->addCoverageEffect(effect); |
| 123 | drawState->setIdentityViewMatrix(); |
| 124 | drawState->setRenderTarget(rt); |
| 125 | drawState->setColor(0xff000000); |
| 126 | |
| 127 | SkRect bounds = rrect.getBounds(); |
| 128 | bounds.outset(2.f, 2.f); |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 129 | |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 130 | tt.target()->drawSimpleRect(bounds); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 131 | } else { |
| 132 | drew = false; |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 133 | } |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 134 | #endif |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 135 | } else if (kBW_Clip_Type == fType || kAA_Clip_Type == fType) { |
| 136 | bool aaClip = (kAA_Clip_Type == fType); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 137 | canvas->clipRRect(fRRects[curRRect], SkRegion::kReplace_Op, aaClip); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 138 | canvas->drawRect(kMaxTileBound, paint); |
| 139 | } else { |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 140 | canvas->drawRRect(fRRects[curRRect], paint); |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 141 | } |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 142 | canvas->restore(); |
commit-bot@chromium.org | bfce48e | 2014-03-10 19:33:16 +0000 | [diff] [blame] | 143 | if (drew) { |
| 144 | x = x + kTileX; |
| 145 | if (x > kImageWidth) { |
| 146 | x = 1; |
| 147 | y += kTileY; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | if (x != 1) { |
| 152 | y += kTileY; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void setUpRRects() { |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 158 | // 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] | 159 | // the screen in kTileX x kTileY tiles. The extra empty pixels on each side are for AA. |
| 160 | |
| 161 | // simple cases |
| 162 | fRRects[0].setRect(SkRect::MakeWH(kTileX-2, kTileY-2)); |
| 163 | fRRects[1].setOval(SkRect::MakeWH(kTileX-2, kTileY-2)); |
| 164 | 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] | 165 | fRRects[3].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 10, 5); |
| 166 | // small circular corners are an interesting test case for gpu clipping |
| 167 | fRRects[4].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 1, 1); |
| 168 | fRRects[5].setRectXY(SkRect::MakeWH(kTileX-2, kTileY-2), 0.5f, 0.5f); |
| 169 | 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] | 170 | |
| 171 | // The first complex case needs special handling since it is a square |
| 172 | fRRects[kNumSimpleCases].setRectRadii(SkRect::MakeWH(kTileY-2, kTileY-2), gRadii[0]); |
mike@reedtribe.org | f6100c8 | 2012-12-24 13:56:17 +0000 | [diff] [blame] | 173 | for (size_t i = 1; i < SK_ARRAY_COUNT(gRadii); ++i) { |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 174 | fRRects[kNumSimpleCases+i].setRectRadii(SkRect::MakeWH(kTileX-2, kTileY-2), gRadii[i]); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private: |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 179 | Type fType; |
skia.committer@gmail.com | f1f66c0 | 2014-03-05 03:02:06 +0000 | [diff] [blame] | 180 | |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 181 | static const int kImageWidth = 640; |
| 182 | static const int kImageHeight = 480; |
| 183 | |
| 184 | static const int kTileX = 80; |
| 185 | static const int kTileY = 40; |
| 186 | |
commit-bot@chromium.org | c2f7824 | 2014-02-19 15:18:05 +0000 | [diff] [blame] | 187 | static const int kNumSimpleCases = 7; |
commit-bot@chromium.org | fa5edbe | 2014-03-13 18:01:05 +0000 | [diff] [blame] | 188 | static const int kNumComplexCases = 29; |
robertphillips@google.com | 5683d42 | 2012-12-17 21:58:02 +0000 | [diff] [blame] | 189 | static const SkVector gRadii[kNumComplexCases][4]; |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 190 | |
| 191 | static const int kNumRRects = kNumSimpleCases + kNumComplexCases; |
| 192 | SkRRect fRRects[kNumRRects]; |
| 193 | |
| 194 | typedef GM INHERITED; |
| 195 | }; |
| 196 | |
| 197 | // Radii for the various test cases. Order is UL, UR, LR, LL |
| 198 | const SkVector RRectGM::gRadii[kNumComplexCases][4] = { |
| 199 | // a circle |
| 200 | { { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY }, { kTileY, kTileY } }, |
| 201 | |
| 202 | // odd ball cases |
| 203 | { { 8, 8 }, { 32, 32 }, { 8, 8 }, { 32, 32 } }, |
| 204 | { { 16, 8 }, { 8, 16 }, { 16, 8 }, { 8, 16 } }, |
| 205 | { { 0, 0 }, { 16, 16 }, { 8, 8 }, { 32, 32 } }, |
| 206 | |
| 207 | // UL |
| 208 | { { 30, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 209 | { { 30, 15 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 210 | { { 15, 30 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }, |
| 211 | |
| 212 | // UR |
| 213 | { { 0, 0 }, { 30, 30 }, { 0, 0 }, { 0, 0 } }, |
| 214 | { { 0, 0 }, { 30, 15 }, { 0, 0 }, { 0, 0 } }, |
| 215 | { { 0, 0 }, { 15, 30 }, { 0, 0 }, { 0, 0 } }, |
| 216 | |
| 217 | // LR |
| 218 | { { 0, 0 }, { 0, 0 }, { 30, 30 }, { 0, 0 } }, |
| 219 | { { 0, 0 }, { 0, 0 }, { 30, 15 }, { 0, 0 } }, |
| 220 | { { 0, 0 }, { 0, 0 }, { 15, 30 }, { 0, 0 } }, |
| 221 | |
| 222 | // LL |
| 223 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 30 } }, |
| 224 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 30, 15 } }, |
| 225 | { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 15, 30 } }, |
| 226 | |
| 227 | // over-sized radii |
| 228 | { { 0, 0 }, { 100, 400 }, { 0, 0 }, { 0, 0 } }, |
| 229 | { { 0, 0 }, { 400, 400 }, { 0, 0 }, { 0, 0 } }, |
| 230 | { { 400, 400 }, { 400, 400 }, { 400, 400 }, { 400, 400 } }, |
commit-bot@chromium.org | cb3672e | 2014-02-21 22:41:56 +0000 | [diff] [blame] | 231 | |
| 232 | // circular corner tabs |
| 233 | { { 0, 0 }, { 20, 20 }, { 20, 20 }, { 0, 0 } }, |
| 234 | { { 20, 20 }, { 20, 20 }, { 0, 0 }, { 0, 0 } }, |
| 235 | { { 0, 0 }, { 0, 0 }, { 20, 20 }, { 20, 20 } }, |
| 236 | { { 20, 20 }, { 0, 0 }, { 0, 0 }, { 20, 20 } }, |
commit-bot@chromium.org | c5c748c | 2014-03-11 15:54:51 +0000 | [diff] [blame] | 237 | |
| 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 } }, |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | /////////////////////////////////////////////////////////////////////////////// |
| 250 | |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 251 | DEF_GM( return new RRectGM(RRectGM::kAA_Draw_Type); ) |
| 252 | DEF_GM( return new RRectGM(RRectGM::kBW_Draw_Type); ) |
| 253 | DEF_GM( return new RRectGM(RRectGM::kAA_Clip_Type); ) |
| 254 | DEF_GM( return new RRectGM(RRectGM::kBW_Clip_Type); ) |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 255 | #if SK_SUPPORT_GPU |
commit-bot@chromium.org | fbde87f | 2014-03-04 16:25:34 +0000 | [diff] [blame] | 256 | DEF_GM( return new RRectGM(RRectGM::kEffect_Type); ) |
bsalomon@google.com | 707bd60 | 2014-03-04 16:52:20 +0000 | [diff] [blame] | 257 | #endif |
robertphillips@google.com | 4e18c7a | 2012-12-17 21:48:19 +0000 | [diff] [blame] | 258 | |
| 259 | } |