blob: 4e0e68d2cbd89d6f380db2cb1bbd3e1192229599 [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];
108 SkAutoTUnref<GrFragmentProcessor> fp(GrConstColorProcessor::Create(color, mode));
109
bsalomonc9c3e622015-04-02 11:12:09 -0700110 GrClip clip;
robertphillips175dd9b2016-04-28 14:32:04 -0700111 GrPipelineBuilder pipelineBuilder(grPaint,
112 drawContext->accessRenderTarget(),
113 clip);
bsalomonac856c92015-08-27 06:30:17 -0700114 pipelineBuilder.addColorFragmentProcessor(fp);
bsalomonc9c3e622015-04-02 11:12:09 -0700115
joshualitt04194f32016-01-13 10:08:27 -0800116 SkAutoTUnref<GrDrawBatch> batch(
117 GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix,
118 renderRect, nullptr, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800119 drawContext->drawContextPriv().testingOnly_drawBatch(pipelineBuilder, batch);
bsalomonc9c3e622015-04-02 11:12:09 -0700120
121 // Draw labels for the input to the processor and the processor to the right of
122 // the test rect. The input label appears above the processor label.
123 SkPaint labelPaint;
caryclark1818acb2015-07-24 12:09:25 -0700124 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700125 labelPaint.setAntiAlias(true);
126 labelPaint.setTextSize(10.f);
127 SkString inputLabel;
128 inputLabel.set("Input: ");
129 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
130 inputLabel.append("gradient");
131 } else {
132 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
133 }
134 SkString procLabel;
135 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
136
137 SkRect inputLabelBounds;
138 // get the bounds of the text in order to position it
139 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
140 &inputLabelBounds);
141 canvas->drawText(inputLabel.c_str(), inputLabel.size(),
142 renderRect.fRight + kPad,
143 -inputLabelBounds.fTop, labelPaint);
144 // update the bounds to reflect the offset we used to draw it.
145 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
146
147 SkRect procLabelBounds;
148 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
149 &procLabelBounds);
150 canvas->drawText(procLabel.c_str(), procLabel.size(),
151 renderRect.fRight + kPad,
152 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
153 labelPaint);
154 procLabelBounds.offset(renderRect.fRight + kPad,
155 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
156
157 labelPaint.setStrokeWidth(0);
158 labelPaint.setStyle(SkPaint::kStroke_Style);
159 canvas->drawRect(renderRect, labelPaint);
160
161 canvas->restore();
162
163 // update x and y for the next test case.
164 SkScalar height = renderRect.height();
165 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
166 maxW = SkTMax(maxW, width);
167 y += height + kPad;
168 if (y + height > kHeight) {
169 y = kPad;
170 x += maxW + kPad;
171 maxW = 0;
172 }
173 }
174 }
175 }
176 }
177
178private:
179 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800180 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700181
182 static const SkScalar kPad;
183 static const SkScalar kRectSize;
184 static const int kWidth = 820;
185 static const int kHeight = 500;
186
187 typedef GM INHERITED;
188};
189
190const SkScalar ConstColorProcessor::kPad = 10.f;
191const SkScalar ConstColorProcessor::kRectSize = 20.f;
192
halcanary385fe4d2015-08-26 13:07:48 -0700193DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700194}
195
196#endif