bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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" |
| 9 | #if SK_SUPPORT_GPU |
| 10 | #include "GrTest.h" |
| 11 | #include "effects/GrRRectEffect.h" |
| 12 | #include "SkDevice.h" |
| 13 | #include "SkRRect.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | |
| 19 | class BigRRectAAEffectGM : public GM { |
| 20 | public: |
| 21 | BigRRectAAEffectGM() { |
| 22 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
| 23 | this->setUpRRects(); |
| 24 | } |
| 25 | |
| 26 | protected: |
| 27 | SkString onShortName() override { |
| 28 | return SkString("big_rrect_aa_effect"); |
| 29 | } |
| 30 | |
| 31 | SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); } |
| 32 | |
| 33 | void onDraw(SkCanvas* canvas) override { |
| 34 | GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | GrContext* context = rt ? rt->getContext() : nullptr; |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 36 | if (!context) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame^] | 37 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | |
| 41 | SkPaint paint; |
| 42 | |
| 43 | #ifdef SK_DEBUG |
| 44 | static const SkRect kMaxRRectBound = SkRect::MakeWH(SkIntToScalar(kMaxSize), |
| 45 | SkIntToScalar(kMaxSize)); |
| 46 | static const SkRect kMaxImageBound = SkRect::MakeWH(SkIntToScalar(kImageWidth), |
| 47 | SkIntToScalar(kImageHeight)); |
| 48 | #endif |
| 49 | |
| 50 | int y = kPad; |
| 51 | int x = kPad; |
| 52 | static const GrPrimitiveEdgeType kEdgeTypes[] = { |
| 53 | kFillAA_GrProcessorEdgeType, |
| 54 | kInverseFillAA_GrProcessorEdgeType, |
| 55 | }; |
| 56 | for (size_t et = 0; et < SK_ARRAY_COUNT(kEdgeTypes); ++et) { |
| 57 | GrPrimitiveEdgeType edgeType = kEdgeTypes[et]; |
| 58 | for (int curRRect = 0; curRRect < fRRects.count(); ++curRRect) { |
| 59 | #ifdef SK_DEBUG |
| 60 | SkASSERT(kMaxRRectBound.contains(fRRects[curRRect].getBounds())); |
| 61 | SkRect imageSpaceBounds = fRRects[curRRect].getBounds(); |
| 62 | imageSpaceBounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 63 | SkASSERT(kMaxImageBound.contains(imageSpaceBounds)); |
| 64 | #endif |
| 65 | canvas->save(); |
| 66 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 67 | GrTestTarget tt; |
| 68 | context->getTestTarget(&tt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | if (nullptr == tt.target()) { |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 70 | SkDEBUGFAIL("Couldn't get Gr test target."); |
| 71 | return; |
| 72 | } |
| 73 | GrPipelineBuilder pipelineBuilder; |
| 74 | |
| 75 | SkRRect rrect = fRRects[curRRect]; |
| 76 | rrect.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 77 | SkAutoTUnref<GrFragmentProcessor> fp(GrRRectEffect::Create(edgeType, rrect)); |
| 78 | SkASSERT(fp); |
| 79 | if (fp) { |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 80 | pipelineBuilder.addCoverageFragmentProcessor(fp); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 81 | pipelineBuilder.setRenderTarget(rt); |
| 82 | |
| 83 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(kMaxSize), |
| 84 | SkIntToScalar(kMaxSize)); |
| 85 | bounds.outset(2.f, 2.f); |
| 86 | bounds.offset(SkIntToScalar(x), SkIntToScalar(y)); |
| 87 | |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 88 | tt.target()->drawNonAARect(pipelineBuilder, |
| 89 | 0xff000000, |
| 90 | SkMatrix::I(), |
| 91 | bounds); |
bsalomon | c41f4d6 | 2015-08-03 14:23:03 -0700 | [diff] [blame] | 92 | } |
| 93 | canvas->restore(); |
| 94 | x = x + kDrawOffset; |
| 95 | if (x + kMaxSize> kImageWidth) { |
| 96 | x = kPad; |
| 97 | y += kDrawOffset; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void setUpRRects() { |
| 104 | SkScalar maxSize = SkIntToScalar(kMaxSize); |
| 105 | fRRects.push()->setRect(SkRect::MakeWH(maxSize, maxSize)); |
| 106 | fRRects.push()->setOval(SkRect::MakeWH(maxSize, maxSize)); |
| 107 | fRRects.push()->setOval(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f)); |
| 108 | fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10.f), |
| 109 | maxSize/2.f - 10.f, maxSize/2.f - 10.f); |
| 110 | fRRects.push()->setRectXY(SkRect::MakeWH(maxSize - 1.f, maxSize - 10), |
| 111 | maxSize/2.f - 10.f, maxSize/2.f - 20.f); |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | static const int kPad = 5; |
| 116 | static const int kMaxSize = 300; |
| 117 | static const int kDrawOffset = kMaxSize + kPad; |
| 118 | |
| 119 | static const int kImageWidth = 4 * kDrawOffset + kPad; |
| 120 | static const int kImageHeight = 3 * kDrawOffset + kPad; |
| 121 | |
| 122 | |
| 123 | SkTDArray<SkRRect> fRRects; |
| 124 | typedef GM INHERITED; |
| 125 | }; |
| 126 | |
| 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | |
| 129 | DEF_GM( return new BigRRectAAEffectGM (); ) |
| 130 | |
| 131 | } |
| 132 | #endif |