blob: af99e60e61304ab266d9c6fe597cfddafc7cca39 [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
10#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040011#include "sk_tool_utils.h"
bsalomonc9c3e622015-04-02 11:12:09 -070012
bsalomonc9c3e622015-04-02 11:12:09 -070013#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContextPriv.h"
Brian Osman3b655982017-03-07 16:58:08 -050015#include "SkGr.h"
bsalomonc9c3e622015-04-02 11:12:09 -070016#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080017#include "effects/GrConstColorProcessor.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrDrawOp.h"
Michael Ludwig72ab3462018-12-10 12:43:36 -050019#include "ops/GrFillRectOp.h"
bsalomonc9c3e622015-04-02 11:12:09 -070020
21namespace skiagm {
22/**
23 * This GM directly exercises GrConstColorProcessor.
24 */
Chris Dalton3a778372019-02-07 15:23:36 -070025class ConstColorProcessor : public GpuGM {
bsalomonc9c3e622015-04-02 11:12:09 -070026public:
27 ConstColorProcessor() {
Mike Kleind46dce32018-08-16 10:17:03 -040028 this->setBGColor(0xFFDDDDDD);
bsalomonc9c3e622015-04-02 11:12:09 -070029 }
30
31protected:
32 SkString onShortName() override {
33 return SkString("const_color_processor");
34 }
35
36 SkISize onISize() override {
37 return SkISize::Make(kWidth, kHeight);
38 }
39
40 void onOnceBeforeDraw() override {
41 SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
42 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
reed2ad1aa62016-03-09 09:50:50 -080043 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
44 SkShader::kClamp_TileMode);
bsalomonc9c3e622015-04-02 11:12:09 -070045 }
46
Chris Dalton3a778372019-02-07 15:23:36 -070047 void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
48 SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070049 constexpr GrColor kColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070050 0xFFFFFFFF,
51 0xFFFF00FF,
52 0x80000000,
53 0x00000000,
54 };
55
mtkleindbfd7ab2016-09-01 11:24:54 -070056 constexpr SkColor kPaintColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070057 0xFFFFFFFF,
58 0xFFFF0000,
59 0x80FF0000,
60 0x00000000,
61 };
62
mtkleindbfd7ab2016-09-01 11:24:54 -070063 const char* kModeStrs[] {
bsalomonc9c3e622015-04-02 11:12:09 -070064 "kIgnore",
65 "kModulateRGBA",
66 "kModulateA",
67 };
68 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
69
70 SkScalar y = kPad;
71 SkScalar x = kPad;
72 SkScalar maxW = 0;
73 for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
74 for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
75 for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
76 // translate by x,y for the canvas draws and the test target draws.
77 canvas->save();
78 canvas->translate(x, y);
79 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
80
81 // rect to draw
82 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
83
bsalomonc9c3e622015-04-02 11:12:09 -070084 GrPaint grPaint;
85 SkPaint skPaint;
86 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
87 skPaint.setShader(fShader);
88 } else {
89 skPaint.setColor(kPaintColors[paintType]);
90 }
Brian Salomonf3569f02017-10-24 12:52:33 -040091 SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(),
92 skPaint, viewMatrix, &grPaint));
bsalomonc9c3e622015-04-02 11:12:09 -070093
94 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
Brian Osmancb3d0872018-10-16 15:19:28 -040095 SkPMColor4f color = SkPMColor4f::FromBytes_RGBA(kColors[procColor]);
Brian Salomonaff329b2017-08-11 09:40:37 -040096 auto fp = GrConstColorProcessor::Make(color, mode);
bsalomonc9c3e622015-04-02 11:12:09 -070097
robertphillips28a838e2016-06-23 14:07:00 -070098 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -040099 renderTargetContext->priv().testingOnly_addDrawOp(
Michael Ludwig72ab3462018-12-10 12:43:36 -0500100 GrFillRectOp::Make(context, std::move(grPaint), GrAAType::kNone,
101 viewMatrix, renderRect));
bsalomonc9c3e622015-04-02 11:12:09 -0700102
103 // Draw labels for the input to the processor and the processor to the right of
104 // the test rect. The input label appears above the processor label.
Mike Reed94cca602018-12-02 16:04:27 -0500105 SkFont labelFont;
106 labelFont.setTypeface(sk_tool_utils::create_portable_typeface());
107 labelFont.setEdging(SkFont::Edging::kAntiAlias);
108 labelFont.setSize(10.f);
bsalomonc9c3e622015-04-02 11:12:09 -0700109 SkPaint labelPaint;
bsalomonc9c3e622015-04-02 11:12:09 -0700110 labelPaint.setAntiAlias(true);
bsalomonc9c3e622015-04-02 11:12:09 -0700111 SkString inputLabel;
112 inputLabel.set("Input: ");
113 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
114 inputLabel.append("gradient");
115 } else {
116 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
117 }
118 SkString procLabel;
119 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
120
121 SkRect inputLabelBounds;
122 // get the bounds of the text in order to position it
Mike Reed94cca602018-12-02 16:04:27 -0500123 labelFont.measureText(inputLabel.c_str(), inputLabel.size(),
124 kUTF8_SkTextEncoding, &inputLabelBounds);
Hal Canary89a644b2019-01-07 09:36:09 -0500125 canvas->drawString(inputLabel, renderRect.fRight + kPad, -inputLabelBounds.fTop,
126 labelFont, labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700127 // update the bounds to reflect the offset we used to draw it.
128 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
129
130 SkRect procLabelBounds;
Mike Reed94cca602018-12-02 16:04:27 -0500131 labelFont.measureText(procLabel.c_str(), procLabel.size(),
132 kUTF8_SkTextEncoding, &procLabelBounds);
Hal Canary89a644b2019-01-07 09:36:09 -0500133 canvas->drawString(procLabel, renderRect.fRight + kPad,
134 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
135 labelFont, labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700136 procLabelBounds.offset(renderRect.fRight + kPad,
137 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
138
139 labelPaint.setStrokeWidth(0);
140 labelPaint.setStyle(SkPaint::kStroke_Style);
141 canvas->drawRect(renderRect, labelPaint);
142
143 canvas->restore();
144
145 // update x and y for the next test case.
146 SkScalar height = renderRect.height();
147 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
148 maxW = SkTMax(maxW, width);
149 y += height + kPad;
150 if (y + height > kHeight) {
151 y = kPad;
152 x += maxW + kPad;
153 maxW = 0;
154 }
155 }
156 }
157 }
158 }
159
160private:
161 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800162 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700163
mtkleindbfd7ab2016-09-01 11:24:54 -0700164 static constexpr SkScalar kPad = 10.f;
165 static constexpr SkScalar kRectSize = 20.f;
166 static constexpr int kWidth = 820;
167 static constexpr int kHeight = 500;
bsalomonc9c3e622015-04-02 11:12:09 -0700168
169 typedef GM INHERITED;
170};
171
halcanary385fe4d2015-08-26 13:07:48 -0700172DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700173}