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