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