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]; |
| 108 | SkAutoTUnref<GrFragmentProcessor> fp(GrConstColorProcessor::Create(color, mode)); |
| 109 | |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 110 | GrClip clip; |
robertphillips | 175dd9b | 2016-04-28 14:32:04 -0700 | [diff] [blame] | 111 | GrPipelineBuilder pipelineBuilder(grPaint, |
| 112 | drawContext->accessRenderTarget(), |
| 113 | clip); |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 114 | pipelineBuilder.addColorFragmentProcessor(fp); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 115 | |
joshualitt | 04194f3 | 2016-01-13 10:08:27 -0800 | [diff] [blame] | 116 | SkAutoTUnref<GrDrawBatch> batch( |
| 117 | GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix, |
| 118 | renderRect, nullptr, nullptr)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 119 | drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 120 | |
| 121 | // Draw labels for the input to the processor and the processor to the right of |
| 122 | // the test rect. The input label appears above the processor label. |
| 123 | SkPaint labelPaint; |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 124 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 125 | labelPaint.setAntiAlias(true); |
| 126 | labelPaint.setTextSize(10.f); |
| 127 | SkString inputLabel; |
| 128 | inputLabel.set("Input: "); |
| 129 | if (paintType >= SK_ARRAY_COUNT(kPaintColors)) { |
| 130 | inputLabel.append("gradient"); |
| 131 | } else { |
| 132 | inputLabel.appendf("0x%08x", kPaintColors[paintType]); |
| 133 | } |
| 134 | SkString procLabel; |
| 135 | procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]); |
| 136 | |
| 137 | SkRect inputLabelBounds; |
| 138 | // get the bounds of the text in order to position it |
| 139 | labelPaint.measureText(inputLabel.c_str(), inputLabel.size(), |
| 140 | &inputLabelBounds); |
| 141 | canvas->drawText(inputLabel.c_str(), inputLabel.size(), |
| 142 | renderRect.fRight + kPad, |
| 143 | -inputLabelBounds.fTop, labelPaint); |
| 144 | // update the bounds to reflect the offset we used to draw it. |
| 145 | inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop); |
| 146 | |
| 147 | SkRect procLabelBounds; |
| 148 | labelPaint.measureText(procLabel.c_str(), procLabel.size(), |
| 149 | &procLabelBounds); |
| 150 | canvas->drawText(procLabel.c_str(), procLabel.size(), |
| 151 | renderRect.fRight + kPad, |
| 152 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop, |
| 153 | labelPaint); |
| 154 | procLabelBounds.offset(renderRect.fRight + kPad, |
| 155 | inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop); |
| 156 | |
| 157 | labelPaint.setStrokeWidth(0); |
| 158 | labelPaint.setStyle(SkPaint::kStroke_Style); |
| 159 | canvas->drawRect(renderRect, labelPaint); |
| 160 | |
| 161 | canvas->restore(); |
| 162 | |
| 163 | // update x and y for the next test case. |
| 164 | SkScalar height = renderRect.height(); |
| 165 | SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight); |
| 166 | maxW = SkTMax(maxW, width); |
| 167 | y += height + kPad; |
| 168 | if (y + height > kHeight) { |
| 169 | y = kPad; |
| 170 | x += maxW + kPad; |
| 171 | maxW = 0; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private: |
| 179 | // Use this as a way of generating and input FP |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 180 | sk_sp<SkShader> fShader; |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 181 | |
| 182 | static const SkScalar kPad; |
| 183 | static const SkScalar kRectSize; |
| 184 | static const int kWidth = 820; |
| 185 | static const int kHeight = 500; |
| 186 | |
| 187 | typedef GM INHERITED; |
| 188 | }; |
| 189 | |
| 190 | const SkScalar ConstColorProcessor::kPad = 10.f; |
| 191 | const SkScalar ConstColorProcessor::kRectSize = 20.f; |
| 192 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 193 | DEF_GM(return new ConstColorProcessor;) |
bsalomon | c9c3e62 | 2015-04-02 11:12:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | #endif |