blob: 17a1bc58207fb9426137d6677a01c376ea57ceab [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"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContextPriv.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 "effects/GrConstColorProcessor.h"
Brian Salomon89527432016-12-16 09:52:16 -050019#include "ops/GrDrawOp.h"
20#include "ops/GrRectOpFactory.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 {
Brian Osman11052242016-10-27 14:47:55 -040049 GrRenderTargetContext* renderTargetContext =
50 canvas->internal_private_accessTopLayerRenderTargetContext();
51 if (!renderTargetContext) {
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
mtkleindbfd7ab2016-09-01 11:24:54 -070061 constexpr GrColor kColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070062 0xFFFFFFFF,
63 0xFFFF00FF,
64 0x80000000,
65 0x00000000,
66 };
67
mtkleindbfd7ab2016-09-01 11:24:54 -070068 constexpr SkColor kPaintColors[] = {
bsalomonc9c3e622015-04-02 11:12:09 -070069 0xFFFFFFFF,
70 0xFFFF0000,
71 0x80FF0000,
72 0x00000000,
73 };
74
mtkleindbfd7ab2016-09-01 11:24:54 -070075 const char* kModeStrs[] {
bsalomonc9c3e622015-04-02 11:12:09 -070076 "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 }
Brian Osman11052242016-10-27 14:47:55 -0400103 SkAssertResult(SkPaintToGrPaint(context, renderTargetContext, skPaint,
104 viewMatrix, &grPaint));
bsalomonc9c3e622015-04-02 11:12:09 -0700105
106 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
Brian Osman618d3042016-10-25 10:51:28 -0400107 GrColor4f color = GrColor4f::FromGrColor(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
Brian Salomonf8334782017-01-03 09:42:58 -0500112 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
Brian Salomon6a639042016-12-14 11:08:17 -0500113 grPaint.getColor(), viewMatrix, renderRect, nullptr, nullptr));
Brian Salomon82f44312017-01-11 13:42:54 -0500114 renderTargetContext->priv().testingOnly_addDrawOp(
115 std::move(grPaint), GrAAType::kNone, std::move(op));
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