blob: 6aad01413fd6e2a7405f1a616536e3048d93d0a5 [file] [log] [blame]
bsalomonc9c3e622015-04-02 11:12:09 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050010#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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 Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/effects/SkGradientShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "include/gpu/GrConfig.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/gpu/GrContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "include/private/GrTypesPriv.h"
31#include "include/private/SkColorData.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040032#include "src/gpu/GrColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrFragmentProcessor.h"
34#include "src/gpu/GrPaint.h"
35#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#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 Wagner7fde8e12019-05-01 17:28:53 -040041#include "tools/ToolUtils.h"
42
43#include <utility>
bsalomonc9c3e622015-04-02 11:12:09 -070044
45namespace skiagm {
46/**
47 * This GM directly exercises GrConstColorProcessor.
48 */
Chris Dalton3a778372019-02-07 15:23:36 -070049class ConstColorProcessor : public GpuGM {
bsalomonc9c3e622015-04-02 11:12:09 -070050public:
51 ConstColorProcessor() {
Mike Kleind46dce32018-08-16 10:17:03 -040052 this->setBGColor(0xFFDDDDDD);
bsalomonc9c3e622015-04-02 11:12:09 -070053 }
54
55protected:
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) };
reed2ad1aa62016-03-09 09:50:50 -080067 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040068 SkTileMode::kClamp);
bsalomonc9c3e622015-04-02 11:12:09 -070069 }
70
Chris Dalton3a778372019-02-07 15:23:36 -070071 void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
72 SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070073 constexpr GrColor kColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070074 0xFFFFFFFF,
75 0xFFFF00FF,
76 0x80000000,
77 0x00000000,
78 };
79
mtkleindbfd7ab2016-09-01 11:24:54 -070080 constexpr SkColor kPaintColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070081 0xFFFFFFFF,
82 0xFFFF0000,
83 0x80FF0000,
84 0x00000000,
85 };
86
mtkleindbfd7ab2016-09-01 11:24:54 -070087 const char* kModeStrs[] {
bsalomonc9c3e622015-04-02 11:12:09 -070088 "kIgnore",
89 "kModulateRGBA",
90 "kModulateA",
91 };
Brian Salomon4dea72a2019-12-18 10:43:10 -050092 static_assert(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
bsalomonc9c3e622015-04-02 11:12:09 -070093
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
bsalomonc9c3e622015-04-02 11:12:09 -0700108 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 Salomon4bc0c1f2019-09-30 15:12:27 -0400115 SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorInfo(),
Brian Salomonf3569f02017-10-24 12:52:33 -0400116 skPaint, viewMatrix, &grPaint));
bsalomonc9c3e622015-04-02 11:12:09 -0700117
118 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
Brian Osmancb3d0872018-10-16 15:19:28 -0400119 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(kColors[procColor]);
Brian Salomonaff329b2017-08-11 09:40:37 -0400120 auto fp = GrConstColorProcessor::Make(color, mode);
bsalomonc9c3e622015-04-02 11:12:09 -0700121
robertphillips28a838e2016-06-23 14:07:00 -0700122 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -0400123 renderTargetContext->priv().testingOnly_addDrawOp(
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400124 GrFillRectOp::MakeNonAARect(context, std::move(grPaint),
125 viewMatrix, renderRect));
bsalomonc9c3e622015-04-02 11:12:09 -0700126
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 Reed94cca602018-12-02 16:04:27 -0500129 SkFont labelFont;
Mike Kleinea3f0142019-03-20 11:12:10 -0500130 labelFont.setTypeface(ToolUtils::create_portable_typeface());
Mike Reed94cca602018-12-02 16:04:27 -0500131 labelFont.setEdging(SkFont::Edging::kAntiAlias);
132 labelFont.setSize(10.f);
bsalomonc9c3e622015-04-02 11:12:09 -0700133 SkPaint labelPaint;
bsalomonc9c3e622015-04-02 11:12:09 -0700134 labelPaint.setAntiAlias(true);
bsalomonc9c3e622015-04-02 11:12:09 -0700135 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 Reed94cca602018-12-02 16:04:27 -0500147 labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
Ben Wagner51e15a62019-05-07 15:38:46 -0400148 SkTextEncoding::kUTF8, &inputLabelBounds);
Hal Canary89a644b2019-01-07 09:36:09 -0500149 canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
150 labelFont, labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700151 // 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 Reed94cca602018-12-02 16:04:27 -0500155 labelFont.measureText(procLabel.c_str(), procLabel.size(),
Ben Wagner51e15a62019-05-07 15:38:46 -0400156 SkTextEncoding::kUTF8, &procLabelBounds);
Hal Canary89a644b2019-01-07 09:36:09 -0500157 canvas->drawString(procLabel, renderRect.fRight + kPad,
158 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
159 labelFont, labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700160 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
184private:
185 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800186 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700187
mtkleindbfd7ab2016-09-01 11:24:54 -0700188 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;
bsalomonc9c3e622015-04-02 11:12:09 -0700192
193 typedef GM INHERITED;
194};
195
halcanary385fe4d2015-08-26 13:07:48 -0700196DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700197}