3 * Copyright 2015 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 // This test only works with the GPU backend.
15 #include "GrContext.h"
17 #include "effects/GrConstColorProcessor.h"
19 #include "SkGradientShader.h"
23 * This GM directly exercises GrConstColorProcessor.
25 class ConstColorProcessor : public GM {
27 ConstColorProcessor() {
28 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
32 SkString onShortName() override {
33 return SkString("const_color_processor");
36 SkISize onISize() override {
37 return SkISize::Make(kWidth, kHeight);
40 void onOnceBeforeDraw() override {
41 SkColor colors[] = { 0xFFFF0000, 0x2000FF00, 0xFF0000FF};
42 SkPoint pts[] = { SkPoint::Make(0, 0), SkPoint::Make(kRectSize, kRectSize) };
43 fShader.reset(SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
44 SkShader::kClamp_TileMode));
47 void onDraw(SkCanvas* canvas) override {
48 GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
52 GrContext* context = rt->getContext();
53 if (NULL == context) {
54 this->drawGpuOnlyMessage(canvas);
58 static const GrColor kColors[] = {
65 static const SkColor kPaintColors[] = {
72 static const char* kModeStrs[] {
77 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kModeStrs) == GrConstColorProcessor::kInputModeCnt);
82 for (size_t paintType = 0; paintType < SK_ARRAY_COUNT(kPaintColors) + 1; ++paintType) {
83 for (size_t procColor = 0; procColor < SK_ARRAY_COUNT(kColors); ++procColor) {
84 for (int m = 0; m < GrConstColorProcessor::kInputModeCnt; ++m) {
85 // translate by x,y for the canvas draws and the test target draws.
87 canvas->translate(x, y);
88 const SkMatrix viewMatrix = SkMatrix::MakeTrans(x, y);
91 SkRect renderRect = SkRect::MakeXYWH(0, 0, kRectSize, kRectSize);
94 context->getTestTarget(&tt);
95 if (NULL == tt.target()) {
96 SkDEBUGFAIL("Couldn't get Gr test target.");
102 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
103 skPaint.setShader(fShader);
105 skPaint.setColor(kPaintColors[paintType]);
107 SkAssertResult(SkPaint2GrPaint(context, rt, skPaint, viewMatrix, false,
110 GrConstColorProcessor::InputMode mode = (GrConstColorProcessor::InputMode) m;
111 GrColor color = kColors[procColor];
112 SkAutoTUnref<GrFragmentProcessor> fp(GrConstColorProcessor::Create(color, mode));
114 GrPipelineBuilder pipelineBuilder;
116 pipelineBuilder.setFromPaint(grPaint, rt, clip);
117 pipelineBuilder.addColorProcessor(fp);
119 tt.target()->drawSimpleRect(&pipelineBuilder,
124 // Draw labels for the input to the processor and the processor to the right of
125 // the test rect. The input label appears above the processor label.
127 sk_tool_utils::set_portable_typeface_always(&labelPaint);
128 labelPaint.setAntiAlias(true);
129 labelPaint.setTextSize(10.f);
131 inputLabel.set("Input: ");
132 if (paintType >= SK_ARRAY_COUNT(kPaintColors)) {
133 inputLabel.append("gradient");
135 inputLabel.appendf("0x%08x", kPaintColors[paintType]);
138 procLabel.printf("Proc: [0x%08x, %s]", kColors[procColor], kModeStrs[m]);
140 SkRect inputLabelBounds;
141 // get the bounds of the text in order to position it
142 labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
144 canvas->drawText(inputLabel.c_str(), inputLabel.size(),
145 renderRect.fRight + kPad,
146 -inputLabelBounds.fTop, labelPaint);
147 // update the bounds to reflect the offset we used to draw it.
148 inputLabelBounds.offset(renderRect.fRight + kPad, -inputLabelBounds.fTop);
150 SkRect procLabelBounds;
151 labelPaint.measureText(procLabel.c_str(), procLabel.size(),
153 canvas->drawText(procLabel.c_str(), procLabel.size(),
154 renderRect.fRight + kPad,
155 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
157 procLabelBounds.offset(renderRect.fRight + kPad,
158 inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop);
160 labelPaint.setStrokeWidth(0);
161 labelPaint.setStyle(SkPaint::kStroke_Style);
162 canvas->drawRect(renderRect, labelPaint);
166 // update x and y for the next test case.
167 SkScalar height = renderRect.height();
168 SkScalar width = SkTMax(inputLabelBounds.fRight, procLabelBounds.fRight);
169 maxW = SkTMax(maxW, width);
171 if (y + height > kHeight) {
182 // Use this as a way of generating and input FP
183 SkAutoTUnref<SkShader> fShader;
185 static const SkScalar kPad;
186 static const SkScalar kRectSize;
187 static const int kWidth = 820;
188 static const int kHeight = 500;
190 typedef GM INHERITED;
193 const SkScalar ConstColorProcessor::kPad = 10.f;
194 const SkScalar ConstColorProcessor::kRectSize = 20.f;
196 DEF_GM( return SkNEW(ConstColorProcessor); )