2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // This test only works with the GPU backend.
14 #include "GrContext.h"
15 #include "GrRenderTargetContextPriv.h"
17 #include "SkGradientShader.h"
18 #include "batches/GrDrawOp.h"
19 #include "batches/GrRectBatchFactory.h"
20 #include "effects/GrConstColorProcessor.h"
24 * This GM directly exercises GrConstColorProcessor.
26 class ConstColorProcessor : public GM {
28 ConstColorProcessor() {
29 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
33 SkString onShortName() override {
34 return SkString("const_color_processor");
37 SkISize onISize() override {
38 return SkISize::Make(kWidth, kHeight);
41 void onOnceBeforeDraw() override {
42 SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
43 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
44 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
45 SkShader::kClamp_TileMode);
48 void onDraw(SkCanvas* canvas) override {
49 GrRenderTargetContext* renderTargetContext =
50 canvas->internal_private_accessTopLayerRenderTargetContext();
51 if (!renderTargetContext) {
52 skiagm::GM::DrawGpuOnlyMessage(canvas);
56 GrContext* context = canvas->getGrContext();
61 constexpr GrColor kColors[] = {
68 constexpr SkColor kPaintColors[] = {
75 const char* kModeStrs[] {
80 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
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.
90 canvas->translate(x, y);
91 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
94 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
98 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
99 skPaint.setShader(fShader);
101 skPaint.setColor(kPaintColors[paintType]);
103 SkAssertResult(SkPaintToGrPaint(context, renderTargetContext, skPaint,
104 viewMatrix, &grPaint));
106 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
107 GrColor4f color = GrColor4f::FromGrColor(kColors[procColor]);
108 sk_sp<GrFragmentProcessor> fp(GrConstColorProcessor::Make(color, mode));
110 grPaint.addColorFragmentProcessor(std::move(fp));
112 sk_sp<GrDrawOp> batch(
113 GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix,
114 renderRect, nullptr, nullptr));
115 renderTargetContext->priv().testingOnly_drawBatch(grPaint, batch.get());
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.
120 sk_tool_utils::set_portable_typeface(&labelPaint);
121 labelPaint.setAntiAlias(true);
122 labelPaint.setTextSize(10.f);
124 inputLabel.set("Input: ");
125 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
126 inputLabel.append("gradient");
128 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
131 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
133 SkRect inputLabelBounds;
134 // get the bounds of the text in order to position it
135 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
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);
143 SkRect procLabelBounds;
144 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
146 canvas->drawText(procLabel.c_str(), procLabel.size(),
147 renderRect.fRight + kPad,
148 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
150 procLabelBounds.offset(renderRect.fRight + kPad,
151 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
153 labelPaint.setStrokeWidth(0);
154 labelPaint.setStyle(SkPaint::kStroke_Style);
155 canvas->drawRect(renderRect, labelPaint);
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);
164 if (y + height > kHeight) {
175 // Use this as a way of generating and input FP
176 sk_sp<SkShader> fShader;
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;
183 typedef GM INHERITED;
186 DEF_GM(return new ConstColorProcessor;)