blob: 166014d4c9987ba6e648f7e551a7a5a38c80f8de [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"
bsalomonf1b7a1d2015-09-28 06:26:28 -070016#include "SkGrPriv.h"
bsalomonc9c3e622015-04-02 11:12:09 -070017#include "SkGradientShader.h"
joshualitt04194f32016-01-13 10:08:27 -080018#include "batches/GrDrawBatch.h"
19#include "batches/GrRectBatchFactory.h"
20#include "effects/GrConstColorProcessor.h"
bsalomonc9c3e622015-04-02 11:12:09 -070021
22namespace skiagm {
23/**
24 * This GM directly exercises GrConstColorProcessor.
25 */
26class ConstColorProcessor : public GM {
27public:
28 ConstColorProcessor() {
caryclark65cdba62015-06-15 06:51:08 -070029 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomonc9c3e622015-04-02 11:12:09 -070030 }
31
32protected:
33 SkString onShortName() override {
34 return SkString("const_color_processor");
35 }
36
37 SkISize onISize() override {
38 return SkISize::Make(kWidth, kHeight);
39 }
40
41 void onOnceBeforeDraw() override {
42 SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
43 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
reed2ad1aa62016-03-09 09:50:50 -080044 fShader = SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
45 SkShader::kClamp_TileMode);
bsalomonc9c3e622015-04-02 11:12:09 -070046 }
47
48 void onDraw(SkCanvas* canvas) override {
robertphillips175dd9b2016-04-28 14:32:04 -070049 GrDrawContext* drawContext = canvas->internal_private_accessTopLayerDrawContext();
50 if (!drawContext) {
halcanary2a243382015-09-09 08:16:41 -070051 skiagm::GM::DrawGpuOnlyMessage(canvas);
bsalomonc9c3e622015-04-02 11:12:09 -070052 return;
53 }
54
robertphillips175dd9b2016-04-28 14:32:04 -070055 GrContext* context = canvas->getGrContext();
56 if (!context) {
joshualitt04194f32016-01-13 10:08:27 -080057 return;
58 }
59
mtkleindbfd7ab2016-09-01 11:24:54 -070060 constexpr GrColor kColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070061 0xFFFFFFFF,
62 0xFFFF00FF,
63 0x80000000,
64 0x00000000,
65 };
66
mtkleindbfd7ab2016-09-01 11:24:54 -070067 constexpr SkColor kPaintColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070068 0xFFFFFFFF,
69 0xFFFF0000,
70 0x80FF0000,
71 0x00000000,
72 };
73
mtkleindbfd7ab2016-09-01 11:24:54 -070074 const char* kModeStrs[] {
bsalomonc9c3e622015-04-02 11:12:09 -070075 "kIgnore",
76 "kModulateRGBA",
77 "kModulateA",
78 };
79 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
80
81 SkScalar y = kPad;
82 SkScalar x = kPad;
83 SkScalar maxW = 0;
84 for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
85 for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
86 for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
87 // translate by x,y for the canvas draws and the test target draws.
88 canvas->save();
89 canvas->translate(x, y);
90 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
91
92 // rect to draw
93 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
94
bsalomonc9c3e622015-04-02 11:12:09 -070095 GrPaint grPaint;
96 SkPaint skPaint;
97 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
98 skPaint.setShader(fShader);
99 } else {
100 skPaint.setColor(kPaintColors[paintType]);
101 }
brianosman898235c2016-04-06 07:38:23 -0700102 // SRGBTODO: No sRGB inputs allowed here?
brianosman8fe485b2016-07-25 12:31:51 -0700103 SkAssertResult(SkPaintToGrPaint(context, drawContext, skPaint, viewMatrix,
brianosman1638c0d2016-07-25 05:12:53 -0700104 &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
robertphillips28a838e2016-06-23 14:07:00 -0700110 grPaint.addColorFragmentProcessor(std::move(fp));
bsalomonc9c3e622015-04-02 11:12:09 -0700111
joshualitt04194f32016-01-13 10:08:27 -0800112 SkAutoTUnref<GrDrawBatch> batch(
113 GrRectBatchFactory::CreateNonAAFill(grPaint.getColor(), viewMatrix,
114 renderRect, nullptr, nullptr));
robertphillips28a838e2016-06-23 14:07:00 -0700115 drawContext->drawContextPriv().testingOnly_drawBatch(grPaint, batch);
bsalomonc9c3e622015-04-02 11:12:09 -0700116
117 // Draw labels for the input to the processor and the processor to the right of
118 // the test rect. The input label appears above the processor label.
119 SkPaint labelPaint;
caryclark1818acb2015-07-24 12:09:25 -0700120 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomonc9c3e622015-04-02 11:12:09 -0700121 labelPaint.setAntiAlias(true);
122 labelPaint.setTextSize(10.f);
123 SkString inputLabel;
124 inputLabel.set("Input: ");
125 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
126 inputLabel.append("gradient");
127 } else {
128 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
129 }
130 SkString procLabel;
131 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
132
133 SkRect inputLabelBounds;
134 // get the bounds of the text in order to position it
135 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
136 &inputLabelBounds);
137 canvas->drawText(inputLabel.c_str(), inputLabel.size(),
138 renderRect.fRight + kPad,
139 -inputLabelBounds.fTop, labelPaint);
140 // update the bounds to reflect the offset we used to draw it.
141 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
142
143 SkRect procLabelBounds;
144 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
145 &procLabelBounds);
146 canvas->drawText(procLabel.c_str(), procLabel.size(),
147 renderRect.fRight + kPad,
148 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
149 labelPaint);
150 procLabelBounds.offset(renderRect.fRight + kPad,
151 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
152
153 labelPaint.setStrokeWidth(0);
154 labelPaint.setStyle(SkPaint::kStroke_Style);
155 canvas->drawRect(renderRect, labelPaint);
156
157 canvas->restore();
158
159 // update x and y for the next test case.
160 SkScalar height = renderRect.height();
161 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
162 maxW = SkTMax(maxW, width);
163 y += height + kPad;
164 if (y + height > kHeight) {
165 y = kPad;
166 x += maxW + kPad;
167 maxW = 0;
168 }
169 }
170 }
171 }
172 }
173
174private:
175 // Use this as a way of generating and input FP
reed2ad1aa62016-03-09 09:50:50 -0800176 sk_sp<SkShader> fShader;
bsalomonc9c3e622015-04-02 11:12:09 -0700177
mtkleindbfd7ab2016-09-01 11:24:54 -0700178 static constexpr SkScalar kPad = 10.f;
179 static constexpr SkScalar kRectSize = 20.f;
180 static constexpr int kWidth = 820;
181 static constexpr int kHeight = 500;
bsalomonc9c3e622015-04-02 11:12:09 -0700182
183 typedef GM INHERITED;
184};
185
halcanary385fe4d2015-08-26 13:07:48 -0700186DEF_GM(return new ConstColorProcessor;)
bsalomonc9c3e622015-04-02 11:12:09 -0700187}
188
189#endif