blob: 2e85714faff8d3778dbccd5c86818988514a5ca9 [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
13#if SK_SUPPORT_GPU
14
15#include "GrContext.h"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContextPriv.h"
Brian Osman3b655982017-03-07 16:58:08 -050017#include "SkGr.h"
bsalomonc9c3e622015-04-02 11:12:09 -070018#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080019#include "effects/GrConstColorProcessor.h"
Brian Salomon89527432016-12-16 09:52:16 -050020#include "ops/GrDrawOp.h"
Brian Salomonbaaf4392017-06-15 09:59:23 -040021#include "ops/GrRectOpFactory.h"
bsalomonc9c3e622015-04-02 11:12:09 -070022
23namespace skiagm {
24/**
25 * This GM directly exercises GrConstColorProcessor.
26 */
27class ConstColorProcessor : public GM {
28public:
29 ConstColorProcessor() {
caryclark65cdba62015-06-15 06:51:08 -070030 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomonc9c3e622015-04-02 11:12:09 -070031 }
32
33protected:
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) };
reed2ad1aa62016-03-09 09:50:50 -080045 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
46 SkShader::kClamp_TileMode);
bsalomonc9c3e622015-04-02 11:12:09 -070047 }
48
49 void onDraw(SkCanvas* canvas) override {
Brian Osman11052242016-10-27 14:47:55 -040050 GrRenderTargetContext* renderTargetContext =
51 canvas->internal_private_accessTopLayerRenderTargetContext();
52 if (!renderTargetContext) {
halcanary2a243382015-09-09 08:16:41 -070053 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomonc9c3e622015-04-02 11:12:09 -070054 return;
55 }
56
robertphillips175dd9b2016-04-28 14:32:04 -070057 GrContext* context = canvas->getGrContext();
58 if (!context) {
joshualitt04194f32016-01-13 10:08:27 -080059 return;
60 }
61
mtkleindbfd7ab2016-09-01 11:24:54 -070062 constexpr GrColor kColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070063 0xFFFFFFFF,
64 0xFFFF00FF,
65 0x80000000,
66 0x00000000,
67 };
68
mtkleindbfd7ab2016-09-01 11:24:54 -070069 constexpr SkColor kPaintColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070070 0xFFFFFFFF,
71 0xFFFF0000,
72 0x80FF0000,
73 0x00000000,
74 };
75
mtkleindbfd7ab2016-09-01 11:24:54 -070076 const char* kModeStrs[] {
bsalomonc9c3e622015-04-02 11:12:09 -070077 "kIgnore",
78 "kModulateRGBA",
79 "kModulateA",
80 };
81 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
82
83 SkScalar y = kPad;
84 SkScalar x = kPad;
85 SkScalar maxW = 0;
86 for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
87 for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
88 for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
89 // translate by x,y for the canvas draws and the test target draws.
90 canvas->save();
91 canvas->translate(x, y);
92 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
93
94 // rect to draw
95 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
96
bsalomonc9c3e622015-04-02 11:12:09 -070097 GrPaint grPaint;
98 SkPaint skPaint;
99 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
100 skPaint.setShader(fShader);
101 } else {
102 skPaint.setColor(kPaintColors[paintType]);
103 }
Brian Salomonf3569f02017-10-24 12:52:33 -0400104 SkAssertResult(SkPaintToGrPaint(context, renderTargetContext->colorSpaceInfo(),
105 skPaint, viewMatrix, &grPaint));
bsalomonc9c3e622015-04-02 11:12:09 -0700106
107 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
Brian Osman618d3042016-10-25 10:51:28 -0400108 GrColor4f color = GrColor4f::FromGrColor(kColors[procColor]);
Brian Salomonaff329b2017-08-11 09:40:37 -0400109 auto fp = GrConstColorProcessor::Make(color, mode);
bsalomonc9c3e622015-04-02 11:12:09 -0700110
robertphillips28a838e2016-06-23 14:07:00 -0700111 grPaint.addColorFragmentProcessor(std::move(fp));
Brian Salomonac70f842017-05-08 10:43:33 -0400112 renderTargetContext->priv().testingOnly_addDrawOp(
Brian Salomonbaaf4392017-06-15 09:59:23 -0400113 GrRectOpFactory::MakeNonAAFill(std::move(grPaint), viewMatrix,
114 renderRect, GrAAType::kNone));
bsalomonc9c3e622015-04-02 11:12:09 -0700115
116 // Draw labels for the input to the processor and the processor to the right of
117 // the test rect. The input label appears above the processor label.
118 SkPaint labelPaint;
caryclark1818acb2015-07-24 12:09:25 -0700119 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700120 labelPaint.setAntiAlias(true);
121 labelPaint.setTextSize(10.f);
122 SkString inputLabel;
123 inputLabel.set("Input: ");
124 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
125 inputLabel.append("gradient");
126 } else {
127 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
128 }
129 SkString procLabel;
130 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
131
132 SkRect inputLabelBounds;
133 // get the bounds of the text in order to position it
134 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
135 &inputLabelBounds);
Cary Clark2a475ea2017-04-28 15:35:12 -0400136 canvas->drawString(inputLabel,
bsalomonc9c3e622015-04-02 11:12:09 -0700137 renderRect.fRight + kPad,
138 -inputLabelBounds.fTop, labelPaint);
139 // update the bounds to reflect the offset we used to draw it.
140 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
141
142 SkRect procLabelBounds;
143 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
144 &procLabelBounds);
Cary Clark2a475ea2017-04-28 15:35:12 -0400145 canvas->drawString(procLabel,
bsalomonc9c3e622015-04-02 11:12:09 -0700146 renderRect.fRight + kPad,
147 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
148 labelPaint);
149 procLabelBounds.offset(renderRect.fRight + kPad,
150 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
151
152 labelPaint.setStrokeWidth(0);
153 labelPaint.setStyle(SkPaint::kStroke_Style);
154 canvas->drawRect(renderRect, labelPaint);
155
156 canvas->restore();
157
158 // update x and y for the next test case.
159 SkScalar height = renderRect.height();
160 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
161 maxW = SkTMax(maxW, width);
162 y += height + kPad;
163 if (y + height > kHeight) {
164 y = kPad;
165 x += maxW + kPad;
166 maxW = 0;
167 }
168 }
169 }
170 }
171 }
172
173private:
174 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800175 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700176
mtkleindbfd7ab2016-09-01 11:24:54 -0700177 static constexpr SkScalar kPad = 10.f;
178 static constexpr SkScalar kRectSize = 20.f;
179 static constexpr int kWidth = 820;
180 static constexpr int kHeight = 500;
bsalomonc9c3e622015-04-02 11:12:09 -0700181
182 typedef GM INHERITED;
183};
184
halcanary385fe4d2015-08-26 13:07:48 -0700185DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700186}
187
188#endif