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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 11 | #include "sk_tool_utils.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 12 | |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 13 | #include "GrContext.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContextPriv.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 15 | #include "SkGr.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 16 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 17 | #include "effects/GrConstColorProcessor.h" |
Brian Salomon | 8952743 | 2016-12-16 09:52:16 -0500 | [diff] [blame] | 18 | #include "ops/GrDrawOp.h" |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 19 | #include "ops/GrFillRectOp.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 20 | |
| 21 | namespace skiagm { |
| 22 | /** |
| 23 | * This GM directly exercises GrConstColorProcessor. |
| 24 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 25 | class ConstColorProcessor : public GpuGM { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 26 | public: |
| 27 | ConstColorProcessor() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 28 | this->setBGColor(0xFFDDDDDD); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | protected: |
| 32 | SkString onShortName() override { |
| 33 | return SkString("const_color_processor"); |
| 34 | } |
| 35 | |
| 36 | SkISize onISize() override { |
| 37 | return SkISize::Make(kWidth, kHeight); |
| 38 | } |
| 39 | |
| 40 | void onOnceBeforeDraw() override { |
| 41 | SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF}; |
| 42 | SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 43 | fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 44 | SkShader::kClamp_TileMode); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 47 | void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 48 | SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 49 | constexpr GrColor kColors[] = { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 50 | 0xFFFFFFFF, |
| 51 | 0xFFFF00FF, |
| 52 | 0x80000000, |
| 53 | 0x00000000, |
| 54 | }; |
| 55 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 56 | constexpr SkColor kPaintColors[] = { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 57 | 0xFFFFFFFF, |
| 58 | 0xFFFF0000, |
| 59 | 0x80FF0000, |
| 60 | 0x00000000, |
| 61 | }; |
| 62 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 63 | const char* kModeStrs[] { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 64 | "kIgnore", |
| 65 | "kModulateRGBA", |
| 66 | "kModulateA", |
| 67 | }; |
| 68 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt); |
| 69 | |
| 70 | SkScalar y = kPad; |
| 71 | SkScalar x = kPad; |
| 72 | SkScalar maxW = 0; |
| 73 | for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) { |
| 74 | for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) { |
| 75 | for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) { |
| 76 | // translate by x,y for the canvas draws and the test target draws. |
| 77 | canvas->save(); |
| 78 | canvas->translate(x, y); |
| 79 | const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y); |
| 80 | |
| 81 | // rect to draw |
| 82 | SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize); |
| 83 | |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 84 | GrPaint grPaint; |
| 85 | SkPaint skPaint; |
| 86 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 87 | skPaint.setShader(fShader); |
| 88 | } else { |
| 89 | skPaint.setColor(kPaintColors[paintType]); |
| 90 | } |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 91 | SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(), |
| 92 | skPaint, viewMatrix, &grPaint)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 93 | |
| 94 | GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m; |
Brian Osman | cb3d087 | 2018-10-16 15:19:28 -0400 | [diff] [blame] | 95 | SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(kColors[procColor]); |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 96 | auto fp = GrConstColorProcessor::Make(color, mode); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 97 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 98 | grPaint.addColorFragmentProcessor(std::move(fp)); |
Brian Salomon | ac70f84 | 2017-05-08 10:43:33 -0400 | [diff] [blame] | 99 | renderTargetContext->priv().testingOnly_addDrawOp( |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 100 | GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone, |
| 101 | viewMatrix, renderRect)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 102 | |
| 103 | // Draw labels for the input to the processor and the processor to the right of |
| 104 | // the test rect. The input label appears above the processor label. |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 105 | SkFont labelFont; |
| 106 | labelFont.setTypeface(sk_tool_utils::create_portable_typeface()); |
| 107 | labelFont.setEdging(SkFont::Edging::kAntiAlias); |
| 108 | labelFont.setSize(10.f); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 109 | SkPaint labelPaint; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 110 | labelPaint.setAntiAlias(true); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 111 | SkString inputLabel; |
| 112 | inputLabel.set("Input: "); |
| 113 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 114 | inputLabel.append("gradient"); |
| 115 | } else { |
| 116 | inputLabel.appendf("0x%08x", kPaintColors[paintType]); |
| 117 | } |
| 118 | SkString procLabel; |
| 119 | procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]); |
| 120 | |
| 121 | SkRect inputLabelBounds; |
| 122 | // get the bounds of the text in order to position it |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 123 | labelFont.measureText(inputLabel.c_str(), inputLabel.size(), |
| 124 | kUTF8_SkTextEncoding, &inputLabelBounds); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 125 | canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop, |
| 126 | labelFont, labelPaint); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 127 | // update the bounds to reflect the offset we used to draw it. |
| 128 | inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop); |
| 129 | |
| 130 | SkRect procLabelBounds; |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 131 | labelFont.measureText(procLabel.c_str(), procLabel.size(), |
| 132 | kUTF8_SkTextEncoding, &procLabelBounds); |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 133 | canvas->drawString(procLabel, renderRect.fRight + kPad, |
| 134 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop, |
| 135 | labelFont, labelPaint); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 136 | procLabelBounds.offset(renderRect.fRight + kPad, |
| 137 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop); |
| 138 | |
| 139 | labelPaint.setStrokeWidth(0); |
| 140 | labelPaint.setStyle(SkPaint::kStroke_Style); |
| 141 | canvas->drawRect(renderRect, labelPaint); |
| 142 | |
| 143 | canvas->restore(); |
| 144 | |
| 145 | // update x and y for the next test case. |
| 146 | SkScalar height = renderRect.height(); |
| 147 | SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight); |
| 148 | maxW = SkTMax(maxW, width); |
| 149 | y += height + kPad; |
| 150 | if (y + height > kHeight) { |
| 151 | y = kPad; |
| 152 | x += maxW + kPad; |
| 153 | maxW = 0; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private: |
| 161 | // Use this as a way of generating and input FP |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 162 | sk_sp<SkShader> fShader; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 163 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 164 | static constexpr SkScalar kPad = 10.f; |
| 165 | static constexpr SkScalar kRectSize = 20.f; |
| 166 | static constexpr int kWidth = 820; |
| 167 | static constexpr int kHeight = 500; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 168 | |
| 169 | typedef GM INHERITED; |
| 170 | }; |
| 171 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 172 | DEF_GM(return new ConstColorProcessor;) |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 173 | } |