blob: 8ae93a4ae929f62f8b8eab90f58371c5244bf950 [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"
11
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
robertphillips391395d2016-03-02 09:26:36 -080015#include "GrDrawContextPriv.h"
joshualitt04194f32016-01-13 10:08:27 -080016#include "GrPipelineBuilder.h"
bsalomonf1b7a1d2015-09-28 06:26:28 -070017#include "SkGrPriv.h"
bsalomonc9c3e622015-04-02 11:12:09 -070018#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080019#include "batches/GrDrawBatch.h"
20#include "batches/GrRectBatchFactory.h"
21#include "effects/GrConstColorProcessor.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 {
robertphillips175dd9b2016-04-28 14:32:04 -070050 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
51 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -070052 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomonc9c3e622015-04-02 11:12:09 -070053 return;
54 }
55
robertphillips175dd9b2016-04-28 14:32:04 -070056 GrContext* context = canvas->getGrContext();
57 if (!context) {
joshualitt04194f32016-01-13 10:08:27 -080058 return;
59 }
60
bsalomonc9c3e622015-04-02 11:12:09 -070061 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
bsalomonc9c3e622015-04-02 11:12:09 -070096 GrPaint grPaint;
97 SkPaint skPaint;
98 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
99 skPaint.setShader(fShader);
100 } else {
101 skPaint.setColor(kPaintColors[paintType]);
102 }
brianosman898235c2016-04-06 07:38:23 -0700103 // SRGBTODO: No sRGB inputs allowed here?
104 SkAssertResult(SkPaintToGrPaint(context, skPaint, viewMatrix, false, &grPaint));
bsalomonc9c3e622015-04-02 11:12:09 -0700105
106 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
107 GrColor color = kColors[procColor];
bungeman06ca8ec2016-06-09 08:01:03 -0700108 sk_sp<GrFragmentProcessor> fp(GrConstColorProcessor::Make(color, mode));
bsalomonc9c3e622015-04-02 11:12:09 -0700109
csmartdaltonecbc12b2016-06-08 10:08:43 -0700110 GrPipelineBuilder pipelineBuilder(grPaint, drawContext->mustUseHWAA(grPaint));
bungeman06ca8ec2016-06-09 08:01:03 -0700111 pipelineBuilder.addColorFragmentProcessor(std::move(fp));
bsalomonc9c3e622015-04-02 11:12:09 -0700112
joshualitt04194f32016-01-13 10:08:27 -0800113 SkAutoTUnref<GrDrawBatch> batch(
114 GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix,
115 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800116 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
bsalomonc9c3e622015-04-02 11:12:09 -0700117
118 // Draw labels for the input to the processor and the processor to the right of
119 // the test rect. The input label appears above the processor label.
120 SkPaint labelPaint;
caryclark1818acb2015-07-24 12:09:25 -0700121 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700122 labelPaint.setAntiAlias(true);
123 labelPaint.setTextSize(10.f);
124 SkString inputLabel;
125 inputLabel.set("Input: ");
126 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
127 inputLabel.append("gradient");
128 } else {
129 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
130 }
131 SkString procLabel;
132 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
133
134 SkRect inputLabelBounds;
135 // get the bounds of the text in order to position it
136 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
137 &inputLabelBounds);
138 canvas->drawText(inputLabel.c_str(), inputLabel.size(),
139 renderRect.fRight + kPad,
140 -inputLabelBounds.fTop, labelPaint);
141 // update the bounds to reflect the offset we used to draw it.
142 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
143
144 SkRect procLabelBounds;
145 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
146 &procLabelBounds);
147 canvas->drawText(procLabel.c_str(), procLabel.size(),
148 renderRect.fRight + kPad,
149 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
150 labelPaint);
151 procLabelBounds.offset(renderRect.fRight + kPad,
152 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
153
154 labelPaint.setStrokeWidth(0);
155 labelPaint.setStyle(SkPaint::kStroke_Style);
156 canvas->drawRect(renderRect, labelPaint);
157
158 canvas->restore();
159
160 // update x and y for the next test case.
161 SkScalar height = renderRect.height();
162 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
163 maxW = SkTMax(maxW, width);
164 y += height + kPad;
165 if (y + height > kHeight) {
166 y = kPad;
167 x += maxW + kPad;
168 maxW = 0;
169 }
170 }
171 }
172 }
173 }
174
175private:
176 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800177 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700178
179 static const SkScalar kPad;
180 static const SkScalar kRectSize;
181 static const int kWidth = 820;
182 static const int kHeight = 500;
183
184 typedef GM INHERITED;
185};
186
187const SkScalar ConstColorProcessor::kPad = 10.f;
188const SkScalar ConstColorProcessor::kRectSize = 20.f;
189
halcanary385fe4d2015-08-26 13:07:48 -0700190DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700191}
192
193#endif