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" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 15 | #include "GrRenderTargetContextPriv.h" |
bsalomon | f1b7a1d | 2015-09-28 06:26:28 -0700 | [diff] [blame] | 16 | #include "SkGrPriv.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 17 | #include "SkGradientShader.h" |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 18 | #include "batches/GrDrawBatch.h" |
| 19 | #include "batches/GrRectBatchFactory.h" |
| 20 | #include "effects/GrConstColorProcessor.h" |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 21 | |
| 22 | namespace skiagm { |
| 23 | /** |
| 24 | * This GM directly exercises GrConstColorProcessor. |
| 25 | */ |
| 26 | class ConstColorProcessor : public GM { |
| 27 | public: |
| 28 | ConstColorProcessor() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 29 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | protected: |
| 33 | SkString onShortName() override { |
| 34 | return SkString("const_color_processor"); |
| 35 | } |
| 36 | |
| 37 | SkISize onISize() override { |
| 38 | return SkISize::Make(kWidth, kHeight); |
| 39 | } |
| 40 | |
| 41 | void onOnceBeforeDraw() override { |
| 42 | SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF}; |
| 43 | SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 44 | fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 45 | SkShader::kClamp_TileMode); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void onDraw(SkCanvas* canvas) override { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 49 | GrRenderTargetContext* renderTargetContext = |
| 50 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 51 | if (!renderTargetContext) { |
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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 61 | constexpr GrColor kColors[] = { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 62 | 0xFFFFFFFF, |
| 63 | 0xFFFF00FF, |
| 64 | 0x80000000, |
| 65 | 0x00000000, |
| 66 | }; |
| 67 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 68 | constexpr SkColor kPaintColors[] = { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 69 | 0xFFFFFFFF, |
| 70 | 0xFFFF0000, |
| 71 | 0x80FF0000, |
| 72 | 0x00000000, |
| 73 | }; |
| 74 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 75 | const char* kModeStrs[] { |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 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 | } |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 103 | SkAssertResult(SkPaintToGrPaint(context, renderTargetContext, skPaint, |
| 104 | viewMatrix, &grPaint)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 105 | |
| 106 | GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m; |
Brian Osman | 618d304 | 2016-10-25 10:51:28 -0400 | [diff] [blame] | 107 | GrColor4f color = GrColor4f::FromGrColor(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 | |
robertphillips | 28a838e | 2016-06-23 14:07:00 -0700 | [diff] [blame] | 110 | grPaint.addColorFragmentProcessor(std::move(fp)); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 111 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 112 | SkAutoTUnref<GrDrawBatch> batch( |
| 113 | GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix, |
| 114 | renderRect, nullptr, nullptr)); |
Brian Osman | 693a540 | 2016-10-27 15:13:22 -0400 | [diff] [blame^] | 115 | renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 116 | |
| 117 | // Draw labels for the input to the processor and the processor to the right of |
| 118 | // the test rect. The input label appears above the processor label. |
| 119 | SkPaint labelPaint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 120 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 121 | labelPaint.setAntiAlias(true); |
| 122 | labelPaint.setTextSize(10.f); |
| 123 | SkString inputLabel; |
| 124 | inputLabel.set("Input: "); |
| 125 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 126 | inputLabel.append("gradient"); |
| 127 | } else { |
| 128 | inputLabel.appendf("0x%08x", kPaintColors[paintType]); |
| 129 | } |
| 130 | SkString procLabel; |
| 131 | procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]); |
| 132 | |
| 133 | SkRect inputLabelBounds; |
| 134 | // get the bounds of the text in order to position it |
| 135 | labelPaint.measureText(inputLabel.c_str(), inputLabel.size(), |
| 136 | &inputLabelBounds); |
| 137 | canvas->drawText(inputLabel.c_str(), inputLabel.size(), |
| 138 | renderRect.fRight + kPad, |
| 139 | -inputLabelBounds.fTop, labelPaint); |
| 140 | // update the bounds to reflect the offset we used to draw it. |
| 141 | inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop); |
| 142 | |
| 143 | SkRect procLabelBounds; |
| 144 | labelPaint.measureText(procLabel.c_str(), procLabel.size(), |
| 145 | &procLabelBounds); |
| 146 | canvas->drawText(procLabel.c_str(), procLabel.size(), |
| 147 | renderRect.fRight + kPad, |
| 148 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop, |
| 149 | labelPaint); |
| 150 | procLabelBounds.offset(renderRect.fRight + kPad, |
| 151 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop); |
| 152 | |
| 153 | labelPaint.setStrokeWidth(0); |
| 154 | labelPaint.setStyle(SkPaint::kStroke_Style); |
| 155 | canvas->drawRect(renderRect, labelPaint); |
| 156 | |
| 157 | canvas->restore(); |
| 158 | |
| 159 | // update x and y for the next test case. |
| 160 | SkScalar height = renderRect.height(); |
| 161 | SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight); |
| 162 | maxW = SkTMax(maxW, width); |
| 163 | y += height + kPad; |
| 164 | if (y + height > kHeight) { |
| 165 | y = kPad; |
| 166 | x += maxW + kPad; |
| 167 | maxW = 0; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | // Use this as a way of generating and input FP |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 176 | sk_sp<SkShader> fShader; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 177 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 178 | static constexpr SkScalar kPad = 10.f; |
| 179 | static constexpr SkScalar kRectSize = 20.f; |
| 180 | static constexpr int kWidth = 820; |
| 181 | static constexpr int kHeight = 500; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 182 | |
| 183 | typedef GM INHERITED; |
| 184 | }; |
| 185 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 186 | DEF_GM(return new ConstColorProcessor;) |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | #endif |