bsalomon | c9c3e62 | 2015-04-02 11:12:09 -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 | // This test only works with the GPU backend. |
| 9 | |
| 10 | #include "gm.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | |
| 14 | #include "GrContext.h" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 15 | #include "GrDrawContextPriv.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 16 | #include "GrPipelineBuilder.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 17 | #include "SkGrPriv.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 18 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 19 | #include "batches/GrDrawBatch.h" |
| 20 | #include "batches/GrRectBatchFactory.h" |
| 21 | #include "effects/GrConstColorProcessor.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 22 | |
| 23 | namespace skiagm { |
| 24 | /** |
| 25 | * This GM directly exercises GrConstColorProcessor. |
| 26 | */ |
| 27 | class ConstColorProcessor : public GM { |
| 28 | public: |
| 29 | ConstColorProcessor() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 30 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | protected: |
| 34 | SkString onShortName() override { |
| 35 | return SkString("const_color_processor"); |
| 36 | } |
| 37 | |
| 38 | SkISize onISize() override { |
| 39 | return SkISize::Make(kWidth, kHeight); |
| 40 | } |
| 41 | |
| 42 | void onOnceBeforeDraw() override { |
| 43 | SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF}; |
| 44 | SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 45 | fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 46 | SkShader::kClamp_TileMode); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 50 | GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext(); |
| 51 | if (!drawContext) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 52 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 56 | GrContext* context = canvas->getGrContext(); |
| 57 | if (!context) { |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 61 | static const GrColor kColors[] = { |
| 62 | 0xFFFFFFFF, |
| 63 | 0xFFFF00FF, |
| 64 | 0x80000000, |
| 65 | 0x00000000, |
| 66 | }; |
| 67 | |
| 68 | static const SkColor kPaintColors[] = { |
| 69 | 0xFFFFFFFF, |
| 70 | 0xFFFF0000, |
| 71 | 0x80FF0000, |
| 72 | 0x00000000, |
| 73 | }; |
| 74 | |
| 75 | static const char* kModeStrs[] { |
| 76 | "kIgnore", |
| 77 | "kModulateRGBA", |
| 78 | "kModulateA", |
| 79 | }; |
| 80 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt); |
| 81 | |
| 82 | SkScalar y = kPad; |
| 83 | SkScalar x = kPad; |
| 84 | SkScalar maxW = 0; |
| 85 | for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) { |
| 86 | for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) { |
| 87 | for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) { |
| 88 | // translate by x,y for the canvas draws and the test target draws. |
| 89 | canvas->save(); |
| 90 | canvas->translate(x, y); |
| 91 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
| 92 | |
| 93 | // rect to draw |
| 94 | SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize); |
| 95 | |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 96 | GrPaint grPaint; |
| 97 | SkPaint skPaint; |
| 98 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 99 | skPaint.setShader(fShader); |
| 100 | } else { |
| 101 | skPaint.setColor(kPaintColors[paintType]); |
| 102 | } |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 103 | // SRGBTODO: No sRGB inputs allowed here? |
| 104 | SkAssertResult(SkPaintToGrPaint(context, skPaint, viewMatrix, false, &grPaint)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 105 | |
| 106 | GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m; |
| 107 | GrColor color = kColors[procColor]; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 108 | sk_sp<GrFragmentProcessor> fp(GrConstColorProcessor::Make(color, mode)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 109 | |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 110 | GrPipelineBuilder pipelineBuilder(grPaint, drawContext->mustUseHWAA(grPaint)); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame^] | 111 | pipelineBuilder.addColorFragmentProcessor(std::move(fp)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 112 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 113 | SkAutoTUnref<GrDrawBatch> batch( |
| 114 | GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix, |
| 115 | renderRect, nullptr, nullptr)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 116 | drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 117 | |
| 118 | // Draw labels for the input to the processor and the processor to the right of |
| 119 | // the test rect. The input label appears above the processor label. |
| 120 | SkPaint labelPaint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 121 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 122 | labelPaint.setAntiAlias(true); |
| 123 | labelPaint.setTextSize(10.f); |
| 124 | SkString inputLabel; |
| 125 | inputLabel.set("Input: "); |
| 126 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 127 | inputLabel.append("gradient"); |
| 128 | } else { |
| 129 | inputLabel.appendf("0x%08x", kPaintColors[paintType]); |
| 130 | } |
| 131 | SkString procLabel; |
| 132 | procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]); |
| 133 | |
| 134 | SkRect inputLabelBounds; |
| 135 | // get the bounds of the text in order to position it |
| 136 | labelPaint.measureText(inputLabel.c_str(), inputLabel.size(), |
| 137 | &inputLabelBounds); |
| 138 | canvas->drawText(inputLabel.c_str(), inputLabel.size(), |
| 139 | renderRect.fRight + kPad, |
| 140 | -inputLabelBounds.fTop, labelPaint); |
| 141 | // update the bounds to reflect the offset we used to draw it. |
| 142 | inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop); |
| 143 | |
| 144 | SkRect procLabelBounds; |
| 145 | labelPaint.measureText(procLabel.c_str(), procLabel.size(), |
| 146 | &procLabelBounds); |
| 147 | canvas->drawText(procLabel.c_str(), procLabel.size(), |
| 148 | renderRect.fRight + kPad, |
| 149 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop, |
| 150 | labelPaint); |
| 151 | procLabelBounds.offset(renderRect.fRight + kPad, |
| 152 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop); |
| 153 | |
| 154 | labelPaint.setStrokeWidth(0); |
| 155 | labelPaint.setStyle(SkPaint::kStroke_Style); |
| 156 | canvas->drawRect(renderRect, labelPaint); |
| 157 | |
| 158 | canvas->restore(); |
| 159 | |
| 160 | // update x and y for the next test case. |
| 161 | SkScalar height = renderRect.height(); |
| 162 | SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight); |
| 163 | maxW = SkTMax(maxW, width); |
| 164 | y += height + kPad; |
| 165 | if (y + height > kHeight) { |
| 166 | y = kPad; |
| 167 | x += maxW + kPad; |
| 168 | maxW = 0; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | // Use this as a way of generating and input FP |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 177 | sk_sp<SkShader> fShader; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 178 | |
| 179 | static const SkScalar kPad; |
| 180 | static const SkScalar kRectSize; |
| 181 | static const int kWidth = 820; |
| 182 | static const int kHeight = 500; |
| 183 | |
| 184 | typedef GM INHERITED; |
| 185 | }; |
| 186 | |
| 187 | const SkScalar ConstColorProcessor::kPad = 10.f; |
| 188 | const SkScalar ConstColorProcessor::kRectSize = 20.f; |
| 189 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 190 | DEF_GM(return new ConstColorProcessor;) |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | #endif |